Dear All,
I have the following data:
value = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
weights = {11396160,108204708,262511086,298022123,200846772,91005916,29926435,7513828,1495797,242405,32428,3602,358,42,1};
ListPlot[{value,weights}//Transpose]
Where the weights can be interpreted as probabilities (though not normalized). and the pairs of values as a distribution.
wd = WeightedData[value,weights]
In order to calculate the variance, one can use the WeightedData to help out. At first I calculate the mean:
mean = N@Mean[wd] (* same as value.weights/Total[weights] *)
5.99879
Which seems ok, I calculated it in two ways, and they match. Now for the variance:
((value - mean)^2).weights/Total[weights]
CentralMoment[wd,2]//N
MomentConvert[CentralMoment[2],"Moment"]/.Moment[x_]:>Moment[wd,x]//N
Variance[wd]//N
1.78035
1.78035
1.78035
2.26599
The first three are identical, but the fourth one is not. Of course it might be because CentralMoment divides by Length[value] and Variance by Length[value]-1...we can correct for that:
Variance[wd](Length[value]-1)/Length[value]//N
2.11492
But still not the same value! How does Variance work with WeightedData? And why does it differ from CentralMoment[...,2]?