/// <summary> /// Returns a random number from a normal distribution with the given mean and standard deviation. /// </summary> public double NextNormal(double mean, double standardDeviation) { double U = NextDouble(); double N = Distributions.NormalCumulativeDistributionFunctionInverse(U, mean, standardDeviation); return(N); }
/// <summary> /// Assumes returns are normally distributed. /// WARNING: Not very accurate. Intended for educational use and as a baseline for comparison to other models. /// </summary> /// <param name="returnArray">Historical returns from which VaR is to be calculated. The last value, with the highest index is assumed to be the most recent data point.</param> /// <param name="windowLength">Length of the VaR window. The number of historical returns that will be used to calculate the VaR.</param> /// <param name="confidenceLevel">VaR confidence level. 95% and 99% are typical values. If confidenceLevel is 95% then we expect 95% of days to be better (have a more positive return) than the VaR.</param> public static double NormalValueAtRisk(double[] returnArray, int windowLength, double confidenceLevel) { double[] da = new double[windowLength]; for (int i = 0; i < windowLength; i++) { da[i] = returnArray[returnArray.Length - windowLength + i]; } double mean = Moments.Mean(da); double standardDeviation = Moments.StandardDeviation(da); double var = Distributions.NormalCumulativeDistributionFunctionInverse(1 - confidenceLevel, mean, standardDeviation); return(-var); //By convention VaR is quoted as a positive value, even though it corresponds to a loss. }