/// <summary> /// Generates a random sample from the distribution based on /// current distribution parameters. /// </summary> /// <param name="sdScalingFactor"></param> /// <param name="incrementalStandardDeviation"></param> /// <returns></returns> public double GenerateSample(double sdScalingFactor = 1.0, Func <double, double> incrementalStandardDeviation = null) { double tau = DistributionRandomDraws.GammaRandomDraw(Alpha, Beta); double variance = Lambda / (Lambda * tau); double incrementalSDValue = 0; if (incrementalStandardDeviation != null) { incrementalSDValue = incrementalStandardDeviation(System.Math.Sqrt(variance)); } double stdDev = System.Math.Sqrt(variance) + incrementalSDValue; return(DistributionRandomDraws.GaussianRandomDraw(Mu, stdDev * sdScalingFactor)); }
public static void Test() { List <float> vals = new List <float>(); BayesianNormalEstimator bayes = new BayesianNormalEstimator(0, 1, 1.2, 0.01); for (int i = 0; i < 200; i++) { double r = DistributionRandomDraws.GaussianRandomDraw(-2, 0.3); bayes.AddSample(r); } Console.WriteLine(bayes.ToString()); Console.WriteLine(); for (int j = 0; j < 100; j++) { double x = bayes.GenerateSample(); vals.Add((float)x); } Console.WriteLine(StatUtils.Average(vals)); Console.WriteLine(StatUtils.StdDev(vals)); }