开发者

f# - Recursive formula for kurtosis

I am now adapting my formula for skewness to make a kurtosis function in F#. Unfortunately it is again return incorrect results.

Here is my code

 let kurtosis_aux (m, m2, m3, m4, k) x =
       m  + (x - m)/k,
       m2 + ((x - m)*(x - m)*(k - 1.0))/k,
       m3 + ((x - m)*(x - m)*(x - m)*(k - 1.0)*(k - 2.0))/(k * k) - (3.0 * (x - m) * m2)/k,
       m4 + ((x - m)*(x - m)*(x - m)*(x - m)*(k - 1.0)*(k * k - (3.0 * k) + 3.0))/(k * k * k) + 6.0 * (x - m)*(x - m)* m2/(k * k) - (4.0*(x - m)* m3)/k ,
       k + 1.0;;

 let kurtosis xs =
      let _, m2, m3, m4, n = Seq.fold kurtosis_aux (0.0, 0.0, 0.0, 0.0, 1.0) xs
      ((n - 1.0) * m4 / ( m2 * m2 )) - 3.0;;

Finally I test on a small vector, and should get approximately 2.94631

  kurtosis [|9.0; 2.0; 6.0; 3.0; 29开发者_Go百科.0|];;

But instead FSI returns -0.05369308728.

The error must be in part m4 of the kurtosis_aux function or in the kurtosis function itself. The other variables are all used in the skewness function and work correctly.

Again I am hugely grateful for any and all help.


remove the -3.0 in the last line. with -3.0 you are calculating excess kurtosis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜