' ******************************************************************
' Skewness and kurtosis
' ******************************************************************

FUNCTION Skewness(X() AS DOUBLE, M AS DOUBLE,_
                  Sigma AS DOUBLE) AS DOUBLE
' Skewness of sample X

  DIM AS DOUBLE  D, SD3
  DIM AS INTEGER Lb, Ub, I, N

  Lb = LBOUND(X)
  Ub = UBOUND(X)
  N = Ub - Lb + 1

  SD3 = 0
  FOR I = Lb TO Ub
    D = X(I) - M
    SD3 = SD3 + D * D * D
  NEXT I

  Skewness = SD3 / (N * Sigma * Sigma * Sigma)
END FUNCTION

FUNCTION Kurtosis(X() AS DOUBLE, M AS DOUBLE,_
                  Sigma AS DOUBLE) AS DOUBLE
' Kurtosis of sample X

  DIM AS DOUBLE  D, D2, SD4, V
  DIM AS INTEGER Lb, Ub, I, N

  Lb = LBOUND(X)
  Ub = UBOUND(X)
  N = Ub - Lb + 1

  SD4 = 0
  FOR I = Lb TO Ub
    D = X(I) - M
    D2 = D * D
    SD4 = SD4 + D2 * D2
  NEXT I

  V = Sigma * Sigma
  Kurtosis = SD4 / (N * V * V) - 3
END FUNCTION
