public ParkMillerNormal(long seed) : base(seed) { bReady = false; // Initially, no stored variate avaiable! dStored = -PZMath_machine.PZMath_DBL_MAX; // Set to arbitrary number _ran0 = new ParkMillerUniform(seed); _ran1 = new ParkMillerUniform(seed + 100); }
/// <summary> /// reset seed and work space /// </summary> protected override void Reset() { _random = new ParkMillerUniform(_seed); }
/// <summary> /// ChiSquare distribution example /// </summary> public static void ExampleSample() { ParkMillerUniform random = new ParkMillerUniform(); string folderName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\"; double k = 4.0; string fileName1 = folderName + "ChiSquare distribution.txt"; FileStream fs1 = new FileStream(fileName1, FileMode.Create, FileAccess.Write); StreamWriter w1 = new StreamWriter(fs1); for (int i = 0; i < 10000; i++) w1.WriteLine(ChiSquareDistribution.Sampler(random, k)); w1.Close(); fs1.Close(); // computational cost of the method DateTime startTime1 = DateTime.Now; double result1 = 0.0; for (int i = 0; i < 10000; i++) result1 = ChiSquareDistribution.Sampler(random, k); DateTime endTime1 = DateTime.Now; TimeSpan duration1 = endTime1 - startTime1; Console.WriteLine("ChiSquare distribution running time (n = 10,000): " + duration1); }
/// <summary> /// reset seed and work space /// </summary> protected override void Reset() { bReady = false; // Initially, no stored variate avaiable! dStored = -PZMath_machine.PZMath_DBL_MAX; // Set to arbitrary number _ran0 = new ParkMillerUniform(_seed); _ran1 = new ParkMillerUniform(_seed + 100); }
/// <summary> /// reset seed and work space /// </summary> protected override void Reset() { _alpha = _k; _beta = 1 / _theta; _random = new ParkMillerUniform(_seed); }
/// <summary> /// Gamma distribution example /// </summary> public static void ExampleSample() { ParkMillerUniform random = new ParkMillerUniform(); string folderName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\"; double k = 2.0; double theta = 2.0; double alpha = k; double beta = 1 / theta; // Knuth method string fileName1 = folderName + "Gamma distribution Knuth method.txt"; FileStream fs1 = new FileStream(fileName1, FileMode.Create, FileAccess.Write); StreamWriter w1 = new StreamWriter(fs1); for (int i = 0; i < 10000; i++) w1.WriteLine(GammaDistribution.SampleKnuth(random, k, theta)); w1.Close(); fs1.Close(); // computational cost of Kunth method DateTime startTime1 = DateTime.Now; double result1 = 0.0; for (int i = 0; i < 10000; i++) result1 = GammaDistribution.SampleKnuth(random, k, theta); DateTime endTime1 = DateTime.Now; TimeSpan duration1 = endTime1 - startTime1; Console.WriteLine("ChiSquare distribution Knuth method running time (n = 10,000): " + duration1); // MT method string fileName2 = folderName + "Gamma distribution MT method.txt"; FileStream fs2 = new FileStream(fileName2, FileMode.Create, FileAccess.Write); StreamWriter w2 = new StreamWriter(fs2); for (int i = 0; i < 10000; i++) w2.WriteLine(GammaDistribution.SampleMarsagliaAndTsang(random, alpha, beta)); w2.Close(); fs2.Close(); // computational cost of the method DateTime startTime2 = DateTime.Now; double result2 = 0.0; for (int i = 0; i < 10000; i++) result2 = GammaDistribution.SampleKnuth(random, alpha, beta); DateTime endTime2 = DateTime.Now; TimeSpan duration2 = endTime2 - startTime2; Console.WriteLine("ChiSquare distribution Knuth method running time (n = 10,000): " + duration2); }
/// <summary> /// normal distribution example /// </summary> public static void ExampleSample() { ParkMillerUniform random = new ParkMillerUniform(); string folderName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\"; double mu = 1.0; double sigma = 2.0; // example of Polar method string fileName1 = folderName + "Polar method.txt"; FileStream fs1 = new FileStream(fileName1, FileMode.Create, FileAccess.Write); StreamWriter w1 = new StreamWriter(fs1); for (int i = 0; i < 10000; i ++) w1.WriteLine(NormalDistribution.SamplerPolar(random, mu, sigma)); w1.Close(); fs1.Close(); // computational cost of Polar method DateTime startTime1 = DateTime.Now; double result1 = 0.0; for (int i = 0; i < 10000; i++) result1 = NormalDistribution.SamplerPolar(random, mu, sigma); DateTime endTime1 = DateTime.Now; TimeSpan duration1 = endTime1 - startTime1; Console.WriteLine("Polar method running time (n = 10,000): " + duration1); // example of Ratio method string fileName2 = folderName + "Ratio method.txt"; FileStream fs2 = new FileStream(fileName2, FileMode.Create, FileAccess.Write); StreamWriter w2 = new StreamWriter(fs2); for (int i = 0; i < 10000; i ++) w2.WriteLine(NormalDistribution.SamplerRatio(random, mu, sigma)); w2.Close(); fs2.Close(); // computational cost of Polar method DateTime startTime2 = DateTime.Now; double result2 = 0.0; for (int i = 0; i < 10000; i++) result2 = NormalDistribution.SamplerRatio(random, mu, sigma); DateTime endTime2 = DateTime.Now; TimeSpan duration2 = endTime2 - startTime1; Console.WriteLine("Ratio method running time (n = 10,000): " + duration2); }
/// <summary> /// evolve Strauss process, C. J. Geyer 1999 /// start from empty point array /// birth-death algorithm /// </summary> private void EvolveStraussProcessStartFromEmpty(List<PZPoint[]> sampleList, double theta1, double theta2, double r, double lamda, double width, double height, int total, int burIn, int space, List<double> birthAcceptRateList, List<double> deathAcceptRateList, List<int> numberPointsList, List<int> numberClosePointsList) { ParkMillerUniform uniformRandom = new ParkMillerUniform(); PZPoint[] oldPointArray = new PZPoint[0]; PZPoint[] newPointArray; double birthRate = 0; double deathRate = 0; double birthAcceptRate = 0; double deathAcceptRate = 0; bool acceptNewPointArray = false; int spaceCount = 0; // MCMC evolving process for (int i = 0; i < _total; i++) { // birth or death if (oldPointArray.Length == 0) // birth only { birthRate = 1.0; deathRate = 0.0; } else { birthRate = uniformRandom.Sample(); deathRate = 1 - birthRate; } int nx = oldPointArray.Length; int sx = S(oldPointArray, r); if (birthRate >= deathRate) // birth { // simulate xi distributed proportional to lamda PZPoint xi = new PZPoint(width * uniformRandom.Sample(), height * uniformRandom.Sample()); newPointArray = new PZPoint[nx + 1]; // get new point array Array.Copy(oldPointArray, newPointArray, nx); newPointArray[nx] = xi; // birth accept rate birthAcceptRate = BirthAcceptRate(oldPointArray, newPointArray, lamda, width, height, theta1, theta2, r); birthAcceptRate = birthAcceptRate < 1.0 ? birthAcceptRate : 1.0; acceptNewPointArray = uniformRandom.Sample() < birthAcceptRate; } else // death { int removeIndex = (int)System.Math.Floor(nx * uniformRandom.Sample()); int nxNew = nx - 1; newPointArray = new PZPoint[nxNew]; // get new point array if (nx - 1 > 0) { for (int indexNew = 0, indexOld = 0; indexNew < nxNew; indexNew++, indexOld ++) { if (indexOld != removeIndex) { newPointArray[indexNew] = oldPointArray[indexOld]; } else { indexNew--; } } } // death accept rate deathAcceptRate = DeathAcceptRate(oldPointArray, newPointArray, lamda, width, height, theta1, theta2, r); deathAcceptRate = deathAcceptRate < 1.0 ? deathAcceptRate : 1.0; acceptNewPointArray = uniformRandom.Sample() < deathAcceptRate; } if (acceptNewPointArray) // accept new point array { oldPointArray = newPointArray; } else // reject new point array { } // record MCMC if (i > burIn) { if (spaceCount == space) { numberPointsList.Add(nx); numberClosePointsList.Add(sx); if (birthRate > deathRate) birthAcceptRateList.Add(birthAcceptRate); else deathAcceptRateList.Add(deathAcceptRate); spaceCount = 0; PZPoint[] recordPointArray = new PZPoint[oldPointArray.Length]; Array.Copy(oldPointArray, recordPointArray, oldPointArray.Length); sampleList.Add(recordPointArray); } } spaceCount++; } }