public override object SimulateData(object inputs, int simSeed) { var times = inputs as List <DateTime>; if (times == null) { return(null); // inputs should be a list of DateTimes } int n = times.Count; var simulated = new TimeSeries(); var randomSource = new Palf(simSeed); var stdnormal = new Normal(); stdnormal.RandomSource = randomSource; double mVar = GetVariance(Parameters); Vector <double> ss = Vector <double> .Build.Dense(n); for (int i = 0; i < n; ++i) { double variance = GetConditionalSig2(i, simulated, ss, Parameters, mVar); double simLR = stdnormal.RandomSource.NextDouble() * Math.Sqrt(variance); simulated.Add(times[i], simLR, false); } simulated.Title = "Simulation"; simulated.Description = "Simulation from " + Description; return(simulated); }
/// <summary> /// Run example /// </summary> /// <seealso cref="http://en.wikipedia.org/wiki/Random_number_generation">Random number generation</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Linear_congruential_generator">Linear congruential generator</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Mersenne_twister">Mersenne twister</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Lagged_Fibonacci_generator">Lagged Fibonacci generator</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Xorshift">Xorshift</seealso> public void Run() { // All RNG classes in MathNet have next counstructors: // - RNG(int seed, bool threadSafe): initializes a new instance with specific seed value and thread safe property // - RNG(int seed): iуууnitializes a new instance with specific seed value. Thread safe property is set to Control.ThreadSafeRandomNumberGenerators // - RNG(bool threadSafe) : initializes a new instance with the seed value set to DateTime.Now.Ticks and specific thread safe property // - RNG(bool threadSafe) : initializes a new instance with the seed value set to DateTime.Now.Ticks and thread safe property set to Control.ThreadSafeRandomNumberGenerators // All RNG classes in MathNet have next methods to produce random values: // - double[] NextDouble(int n): returns an "n"-size array of uniformly distributed random doubles in the interval [0.0,1.0]; // - int Next(): returns a nonnegative random number; // - int Next(int maxValue): returns a random number less then a specified maximum; // - int Next(int minValue, int maxValue): returns a random number within a specified range; // - void NextBytes(byte[] buffer): fills the elements of a specified array of bytes with random numbers; // All RNG classes in MathNet have next extension methods to produce random values: // - long NextInt64(): returns a nonnegative random number less than "Int64.MaxValue"; // - int NextFullRangeInt32(): returns a random number of the full Int32 range; // - long NextFullRangeInt64(): returns a random number of the full Int64 range; // - decimal NextDecimal(): returns a nonnegative decimal floating point random number less than 1.0; // 1. Multiplicative congruential generator using a modulus of 2^31-1 and a multiplier of 1132489760 var mcg31M1 = new Mcg31m1(1); Console.WriteLine(@"1. Generate 10 random double values using Multiplicative congruential generator with a modulus of 2^31-1 and a multiplier of 1132489760"); var randomValues = mcg31M1.NextDouble(10); for (var i = 0; i < randomValues.Length; i++) { Console.Write(randomValues[i].ToString("N") + @" "); } Console.WriteLine(); Console.WriteLine(); // 2. Multiplicative congruential generator using a modulus of 2^59 and a multiplier of 13^13 var mcg59 = new Mcg59(1); Console.WriteLine(@"2. Generate 10 random integer values using Multiplicative congruential generator with a modulus of 2^59 and a multiplier of 13^13"); for (var i = 0; i < 10; i++) { Console.Write(mcg59.Next() + @" "); } Console.WriteLine(); Console.WriteLine(); // 3. Random number generator using Mersenne Twister 19937 algorithm var mersenneTwister = new MersenneTwister(1); Console.WriteLine(@"3. Generate 10 random integer values less then 100 using Mersenne Twister 19937 algorithm"); for (var i = 0; i < 10; i++) { Console.Write(mersenneTwister.Next(100) + @" "); } Console.WriteLine(); Console.WriteLine(); // 4. Multiple recursive generator with 2 components of order 3 var mrg32K3A = new Mrg32k3a(1); Console.WriteLine(@"4. Generate 10 random integer values in range [50;100] using multiple recursive generator with 2 components of order 3"); for (var i = 0; i < 10; i++) { Console.Write(mrg32K3A.Next(50, 100) + @" "); } Console.WriteLine(); Console.WriteLine(); // 5. Parallel Additive Lagged Fibonacci pseudo-random number generator var palf = new Palf(1); Console.WriteLine(@"5. Generate 10 random bytes using Parallel Additive Lagged Fibonacci pseudo-random number generator"); var bytes = new byte[10]; palf.NextBytes(bytes); for (var i = 0; i < bytes.Length; i++) { Console.Write(bytes[i] + @" "); } Console.WriteLine(); Console.WriteLine(); // 6. A random number generator based on the "System.Security.Cryptography.RandomNumberGenerator" class in the .NET library var systemCryptoRandomNumberGenerator = new SystemCryptoRandomNumberGenerator(); Console.WriteLine(@"6. Generate 10 random decimal values using RNG based on the 'System.Security.Cryptography.RandomNumberGenerator'"); for (var i = 0; i < 10; i++) { Console.Write(systemCryptoRandomNumberGenerator.NextDecimal().ToString("N") + @" "); } Console.WriteLine(); Console.WriteLine(); // 7. Wichmann-Hill’s 1982 combined multiplicative congruential generator var rngWh1982 = new WH1982(); Console.WriteLine(@"7. Generate 10 random full Int32 range values using Wichmann-Hill’s 1982 combined multiplicative congruential generator"); for (var i = 0; i < 10; i++) { Console.Write(rngWh1982.NextFullRangeInt32() + @" "); } Console.WriteLine(); Console.WriteLine(); // 8. Wichmann-Hill’s 2006 combined multiplicative congruential generator. var rngWh2006 = new WH2006(); Console.WriteLine(@"8. Generate 10 random full Int64 range values using Wichmann-Hill’s 2006 combined multiplicative congruential generator"); for (var i = 0; i < 10; i++) { Console.Write(rngWh2006.NextFullRangeInt32() + @" "); } Console.WriteLine(); Console.WriteLine(); // 9. Multiply-with-carry Xorshift pseudo random number generator var xorshift = new Xorshift(); Console.WriteLine(@"9. Generate 10 random nonnegative values less than Int64.MaxValue using Multiply-with-carry Xorshift pseudo random number generator"); for (var i = 0; i < 10; i++) { Console.Write(xorshift.NextInt64() + @" "); } Console.WriteLine(); }
public void StaticSamplesConsistent() { Assert.That(Palf.Doubles(1000, 1), Is.EqualTo(new Palf(1).NextDoubles(1000)).Within(1e-12).AsCollection); }
public override object SimulateData(object inputs, int randomSeed) { var times = inputs as List <DateTime>; if (times == null) { return(null); // inputs should be a list of DateTimes } // Simulation here uses the Durbin-Levinson recursions to simulate from // successive one-step predictive d-ns. // (This works for long-memory processes as well, unlike the obvious constructive approach for ARMA models.) int nn = times.Count; Vector <double> acf = ComputeACF(nn + 1, false); var nu = Vector <double> .Build.Dense(nn); var olda = Vector <double> .Build.Dense(nn); var a = Vector <double> .Build.Dense(nn); var simd = Vector <double> .Build.Dense(nn); var rs = new Palf(randomSeed); var sd = new Normal();//new StandardDistribution(rs); sd.RandomSource = rs; nu[0] = acf[0]; // nu(0) = 1-step pred. variance of X(0) simd[0] = sd.RandomSource.NextDouble() * Math.Sqrt(nu[0]); for (int t = 1; t < nn; ++t) { for (int j = 0; j < nn; ++j) { olda[j] = a[j]; } // compute the new a vector double sum = 0.0; for (int j = 1; j < t; ++j) { sum += olda[j - 1] * acf[t - j]; } a[t - 1] = 1 / nu[t - 1] * (acf[t] - sum); for (int j = 0; j < t - 1; ++j) { a[j] = olda[j] - a[t - 1] * olda[t - 2 - j]; } // update nu nu[t] = nu[t - 1] * (1 - a[t - 1] * a[t - 1]); // compute xhat sum = 0.0; for (int j = 0; j < t; ++j) { sum += a[j] * simd[t - 1 - j]; } simd[t] = sd.RandomSource.NextDouble() * Math.Sqrt(nu[t]) + sum; } var simulated = new TimeSeries { Title = "Simul.", Description = "Simulation from " + Description }; for (int i = 0; i < nn; ++i) { simulated.Add(times[i], simd[i] + Mu, false); } return(simulated); }
public void ThrowsArgumentExceptionWhenLongLagIsNotGreaterThanShortLag() { var random = new Palf(1, true, 10, 10); }
public void ThrowsArgumentExceptionWhenShortLagIsNonPositive() { var random = new Palf(1, true, 0, 10); }