问题
I'm sure there must be a straight forward command for this, but I've searched and can't find one. How do I get the expected value from a vector?
Here are the values
y <- c(0.05, 0.01, -0.1)
And their probabilities
p <- c(0.2, 0.7, 0.1)
I can get E(Y) by doing
sum(y*p)
But I think there is probably a command for it right, I just can't find it. Thanks!
回答1:
You can use weighted.mean:
weighted.mean(y, p)
# [1] 0.007
回答2:
Here's another option:
> c(y %*% p)
[1] 0.007
来源:https://stackoverflow.com/questions/26959127/does-an-expected-value-command-exist-in-r