Expected value is the average outcome you’d expect from a random process. It’s crucial in statistics, probability, finance, and decision-making. In my work with risk analysis and portfolio optimization, calculating expected values correctly is absolutely critical.

When to Use Expected Value

You’ll calculate expected value when:

  • Analyzing probability distributions
  • Evaluating financial investments
  • Risk assessment and decision analysis
  • Game theory and betting problems
  • Machine learning loss functions

Two Scenarios

Scenario 1: When you have probabilities - Use weighted average

E[X] = Σ(value × probability)

Scenario 2: When you have sample data - Use simple average

E[X] = mean(data)

Important Distinction

  • Probability-weighted sum: Use when you have explicit probabilities for outcomes
  • Simple mean: Use when you have sample data (assumes equal probability)

Examples with Explanations

Using sum() Function

Let’s see how to use sum() function to calculate expected value when we have sample data and probability values:

# Define values
data <- c(12,23,34,45,56,67)

# Define probability values
probability <- c(0.1,0.2,0.3,0.4,0.5,0.6)

# Calculate expected value
sum(data*probability)

Output:

[1] 102.2

Here the output shows expected value which calculated using sample data and probability values.

Using mean() Function

Let’s see how to use mean() function to calculate expected value:

# Define values
data <- c(12,23,34,45,56,67)

# Calculate expected value
mean(data)

Output:

[1] 39.5

Here the output shows the expected value calculated using the mean() function on sample data.

Common Mistakes to Avoid

Mistake 1: Confusing E[X] = mean with weighted average

# ❌ WRONG - Using mean() when you have probabilities
values <- c(10, 20, 30)
prob <- c(0.2, 0.5, 0.3)
expected <- mean(values)  # [1] 20 - WRONG!

# ✅ CORRECT - Use probability-weighted sum
expected <- sum(values * prob)  # [1] 21 - CORRECT

Mistake 2: Forgetting probabilities must sum to 1

# ❌ ERROR - Probabilities don't sum to 1
values <- c(100, 200, 300)
prob <- c(0.2, 0.3, 0.4)  # Sum = 0.9, not 1!
expected <- sum(values * prob)  # Result is wrong

# ✅ VERIFY - Check probabilities sum to 1
sum(prob)  # Should be 1.0

Mistake 3: Wrong probability assignment

# ❌ WRONG - Mixing up value order with probability order
values <- c(10, 20, 30)
prob <- c(0.5, 0.3, 0.2)
result <- sum(values * prob)  # Make sure order matches!

# ✅ CAREFUL - Verify alignment
data.frame(values, prob)  # Visually confirm pairing

Mistake 4: Not distinguishing between E[X] and variance

# Remember: Expected value ≠ Most likely value
values <- c(-1000, 100, 100, 100)
prob <- c(0.01, 0.33, 0.33, 0.33)
E_X <- sum(values * prob)  # [-9.66]
# Most likely: 100, but expected: -9.66!

Pro Tips

  1. Use data.frame to organize:

    outcomes <- data.frame(value=c(10,20,30), prob=c(0.2,0.5,0.3))
    sum(outcomes$value * outcomes$prob)
    
  2. Variance formula: E[X²] - (E[X])²

  3. Standard deviation: sqrt(variance)

  4. Compare investment options: Higher expected value + lower variance = better choice

Real-World Example

# Comparing two investment strategies
Strategy_A <- c(value=c(1000,500,-200), prob=c(0.3,0.5,0.2))
E_A <- sum(Strategy_A[1:3] * Strategy_A[4:6])

Strategy_B <- c(value=c(800,600,400), prob=c(0.4,0.4,0.2))
E_B <- sum(Strategy_B[1:3] * Strategy_B[4:6])

cat("Strategy A expected value:", E_A, "\n")
cat("Strategy B expected value:", E_B, "\n")

See Also