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!
You can use weighted.mean
:
weighted.mean(y, p)
# [1] 0.007
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With