public void TestRidge() { var rng = new Random(0); const double alpha = 1.0; foreach (var solver in new[] { RidgeSolver.Svd, RidgeSolver.DenseCholesky, RidgeSolver.Lsqr }) { // With more samples than features int nSamples = 6; int nFeatures = 5; var normal = new Normal { RandomSource = rng }; Vector y = DenseVector.CreateRandom(nSamples, normal); Matrix x = DenseMatrix.CreateRandom(nSamples, nFeatures, normal); var ridge = new RidgeRegression(alpha: alpha, solver: solver); ridge.Fit(x, y); Assert.AreEqual(ridge.Coef.Row(0).Count, x.ColumnCount); Assert.IsTrue(ridge.Score(x, y) > 0.47); ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0)); Assert.IsTrue(ridge.Score(x, y) > 0.47); // With more features than samples nSamples = 5; nFeatures = 10; y = DenseVector.CreateRandom(nSamples, normal); x = DenseMatrix.CreateRandom(nSamples, nFeatures, normal); ridge = new RidgeRegression(alpha: alpha, solver: solver); ridge.Fit(x, y); Assert.IsTrue(ridge.Score(x, y) > 0.9); ridge.Fit(x, y, sampleWeight: DenseVector.Create(nSamples, i => 1.0)); Assert.IsTrue(ridge.Score(x, y) > 0.9); } }
public void NullRandomNumberGenerator() { var random = MersenneTwister.Default; var normal = new Normal(0.0, 1.0, random); var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => Normal.PDF(x, 0.1, y), x => Normal.Sample(random, x, 0.1), 10); Assert.That(() => ms.RandomSource = null, Throws.TypeOf<ArgumentNullException>()); }
public Chromosome() { weights_distr_mut = new Normal(0, 8); biases_distr_mut = new Normal(0, 1.5); t_distr_mut = new Normal(0, 3); weights_distr_init = new Normal(0, 8); biases_distr_init = new Normal(2.75, 1.5); t_distr_init = new Normal(0, 3); mut_counter = 0; weights = new float[100]; biases = new float[10]; t_consts = new float[10]; fitness = 0; for (int i = 0; i < 100; i++) { if (i < 10) { t_consts [i] = (float)t_distr_init.Sample(); biases [i] = (float)biases_distr_init.Sample(); } weights [i] = (float)weights_distr_init.Sample(); } }
/// <summary>Initializes a float matrix with normal distributed (Gaussian) noise</summary> /// <param name="matrix">the matrix to initialize</param> /// <param name="mean">the mean of the normal distribution drawn from</param> /// <param name="stddev">the standard deviation of the normal distribution</param> static public void InitNormal(this Matrix<float> matrix, double mean, double stddev) { var nd = new Normal(mean, stddev); nd.RandomSource = MyMediaLite.Random.GetInstance(); for (int i = 0; i < matrix.data.Length; i++) matrix.data[i] = (float) nd.Sample(); }
/// <summary>Initializes one column of a float matrix with normal distributed (Gaussian) noise</summary> /// <param name="matrix">the matrix to initialize</param> /// <param name="mean">the mean of the normal distribution drawn from</param> /// <param name="stddev">the standard deviation of the normal distribution</param> /// <param name="column">the column to be initialized</param> static public void ColumnInitNormal(this Matrix<float> matrix, int column, double mean, double stddev) { var nd = new Normal(mean, stddev); nd.RandomSource = MyMediaLite.Random.GetInstance(); for (int i = 0; i < matrix.dim1; i++) matrix[i, column] = (float) nd.Sample(); }
/// <summary>Initializes one column of a double matrix with normal distributed (Gaussian) noise</summary> /// <param name="matrix">the matrix to initialize</param> /// <param name="mean">the mean of the normal distribution drawn from</param> /// <param name="stddev">the standard deviation of the normal distribution</param> /// <param name="column">the column to be initialized</param> public static void ColumnInitNormal(this Matrix<double> matrix, int column, double mean, double stddev) { var nd = new Normal(mean, stddev); nd.RandomSource = Util.Random.GetInstance(); for (int i = 0; i < matrix.dim1; i++) matrix[i, column] = nd.Sample(); }
/// <summary>Initialize a collection of floats with values from a normal distribution</summary> /// <param name="vector">the vector to initialize</param> /// <param name="mean">the mean of the normal distribution</param> /// <param name="stddev">the standard deviation of the normal distribution</param> static public void InitNormal(this IList<float> vector, double mean, double stddev) { var nd = new Normal(mean, stddev); nd.RandomSource = MyMediaLite.Random.GetInstance(); for (int i = 0; i < vector.Count; i++) vector[i] = (float) nd.Sample(); }
/// <summary>Initializes one row of a float matrix with normal distributed (Gaussian) noise</summary> /// <param name="matrix">the matrix to initialize</param> /// <param name="row">the row to be initialized</param> /// <param name="mean">the mean of the normal distribution drawn from</param> /// <param name="stddev">the standard deviation of the normal distribution</param> static public void RowInitNormal(this Matrix<float> matrix, int row, double mean, double stddev) { var nd = new Normal(mean, stddev); nd.RandomSource = MyMediaLite.Random.GetInstance(); for (int j = 0; j < matrix.dim2; j++) matrix[row, j] = (float) nd.Sample(); }
public override void Initialize(List<Agent> agents) { var appSettings = ConfigurationManager.AppSettings; var activityMean = int.Parse(appSettings["RegularAgentActivityMean"]); var activityStd = int.Parse(appSettings["RegularAgentActivityStd"]); var distribution = new Normal(activityMean, activityStd); var interval = distribution.Sample(); ActivityInterval = (int)interval; var contactsMean = int.Parse(appSettings["RegularAgentContactsMean"]); var contactsStd = int.Parse(appSettings["RegularAgentContactsStd"]); distribution = new Normal(contactsMean, contactsStd); var contactsNumber = 0; while (contactsNumber == 0) { contactsNumber = (int)distribution.Sample(); } var strongConnectionsMean = int.Parse(appSettings["RegularAgentStrongConnectionsMean"]); var strongConnectionsStd = int.Parse(appSettings["RegularAgentStrongConnectionsStd"]); distribution = new Normal(strongConnectionsMean, strongConnectionsStd); var strongConnectionsNumber = (int)distribution.Sample(); var strongConnectionsInterval = 0.75 / strongConnectionsNumber; var strongConnectionsIntervalMin = 0.8 * strongConnectionsInterval; var strongConnectionsIntervalDiff = strongConnectionsInterval - strongConnectionsIntervalMin; var random = new Random(); var total = 1.0; var usedIndices = new List<int>(); for (int i = 0; i < contactsNumber; i++) { var currentAgentIndex = random.Next(agents.Count); if (usedIndices.Contains(currentAgentIndex)) { i--; continue; } usedIndices.Add(currentAgentIndex); var currentAgent = agents.ElementAt(currentAgentIndex); var probability = 0.0; if (i < strongConnectionsNumber) { probability = strongConnectionsIntervalMin + random.NextDouble() * strongConnectionsIntervalDiff; } else { if(i == contactsNumber - 1) { probability = total; } else { probability = 0.2 * total; } } total -= probability; Contacts.Add(currentAgent, probability); } }
public RandomLiquidityMaker(IRandomNumberGenerator randomNumberGenerator, double maxOrderSize, double maxPriceDifferential, double doNothingProbability, Normal normalDist,int decimals) { _rng = randomNumberGenerator; _maxOrderSize = maxOrderSize; _maxPriceDifferential = maxPriceDifferential; _doNothingProbability = doNothingProbability; _normal = normalDist; _decimals = decimals; }
public void SampleTest() { var normal = new Normal(0.0, 1.0); var rnd = new MersenneTwister(); var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(rnd, x, 0.1), 10); ms.RandomSource = rnd; double sample = ms.Sample(); }
public static IEnumerable<float> TestData() { var g = new Normal(0.0, 1.0); var randy = new MersenneTwister(); g.RandomSource = randy; var dbls = new double[100000]; g.Samples(dbls); return dbls.Select(d => (float) d); }
/// <summary> /// Produce a vector of curves where the element at index i is a realization of a simulation at /// simulationDates i. If you require the rates directly use <see cref="GetSimulatedRates(Date[])"/> /// </summary> /// <param name="simulationDates">Dates on which the simulation is run. Must all be greater than the /// anchor date.</param> /// <returns></returns> public ICurve[] GetSimulatedCurves(Date[] simulationDates, Currency curveCcy = null) { if (curveCcy == null) { curveCcy = Currency.ANY; } ICurve[] results = new ICurve[simulationDates.Length]; MathNet.Numerics.Distributions.Normal dist = new MathNet.Numerics.Distributions.Normal(); Date previousDate = anchorDate; double[] previousRates = initialRates.Clone() as double[]; double[] currentRates = new double[initialRates.Length]; // Iterate through the simulation dates for (int simCounter = 0; simCounter < simulationDates.Length; simCounter++) { Date currentDate = simulationDates[simCounter]; double dt = (currentDate - previousDate) / 365.0; double sdt = Math.Sqrt(dt); Date[] curveDates = new Date[initialRates.Length]; // Random realizations to be used in simulation. double eps1 = dist.Sample(); double eps2 = dist.Sample(); double eps3 = dist.Sample(); // Iterate thrrough the dates on the curve for (int i = 0; i < initialRates.Length; i++) { curveDates[i] = simulationDates[simCounter].AddTenor(tenors[i]); if (useRelative) { //TODO: add mean correction. double exponent = components[0, i] * vols[0] * sdt * eps1 + components[1, i] * vols[1] * sdt * eps2 + components[2, i] * vols[2] * sdt * eps3; currentRates[i] = previousRates[i] * Math.Exp(exponent); } else { double change = components[0, i] * vols[0] * sdt * eps1 + components[1, i] * vols[1] * sdt * eps2 + components[2, i] * vols[2] * sdt * eps3; currentRates[i] = previousRates[i] + change; if (floorAtZero) { currentRates[i] = Math.Max(0.0, currentRates[i]); } } } currentRates = currentRates.Multiply(multiplier); results[simCounter] = new DatesAndRates(curveCcy, simulationDates[simCounter], curveDates, currentRates, simulationDates[simCounter].AddMonths(360)); previousRates = currentRates.Clone() as double[]; previousDate = new Date(currentDate); } return(results); }
public void build_prob_map() { Normal N_x = new Normal(X / 2, STD_X); Normal N_y = new Normal(Y / 2, STD_Y); DenseMatrix M_x = new DenseMatrix(Y, X, 0.0); DenseMatrix M_y = new DenseMatrix(Y, X, 0.0); DenseVector V_x = new DenseVector(X); for (int i = 0; i < X; i++) { V_x[i] = N_x.Density(i); } for (int j = 0; j < Y; j++) { M_x.SetRow(j, V_x); } DenseVector V_y = new DenseVector(Y); for (int i = 0; i < Y; i++) { V_y[i] = N_y.Density(i); } for (int j = 0; j < X; j++) { M_y.SetColumn(j, V_y); } DenseMatrix MULT = (DenseMatrix)M_x.PointwiseMultiply(M_y); double s = MULT.Data.Sum(); MULT = (DenseMatrix)MULT.PointwiseDivide(new DenseMatrix(Y, X, s)); //this.dataGridView1.DataSource = MULT; //Console.WriteLine(MULT.Data.Sum()); PROB_MAP_ORIG = MULT; s = MULT[Y / 2, X / 2]; MULT = (DenseMatrix)MULT.PointwiseDivide(new DenseMatrix(Y, X, s)); /* for (int i = 0; i < Y; i++) { Console.Write(i + " - "); for (int j = 0; j < X; j++) { Console.Write(MULT[i, j] + " "); } Console.WriteLine(); Console.WriteLine(); } */ PROB_MAP = MULT; }
/// <summary> /// Initialize a matrix with normal distributed values with mean = 0.0 std = 0.01 /// </summary> /// <param name="ids">Identifiers.</param> /// <param name="M">Matrix to initialize</param> private static void initMatrixNormal(int size, ref double[,] M, int K) { MathNet.Numerics.Distributions.Normal normalDist = new MathNet.Numerics.Distributions.Normal(0.0, 0.01); for (int i = 0; i < size; i++) { for (int j = 0; j < K; j++) { M [i, j] = normalDist.Sample(); } } }
private void CreateNormalFromString(string distributionString) { var regex = new Regex(@"Normal\(\s*(\d+)\s*,\s*(\d+)\s*\)", RegexOptions.IgnoreCase); var match = regex.Match(distributionString); var mean = double.Parse(match.Groups[1].Value); var stddev = double.Parse(match.Groups[2].Value); _normal = new Normal(mean, stddev); }
public void MetropolisConstructor() { var normal = new Normal(0.0, 1.0); var rnd = new MersenneTwister(); var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(rnd, x, 0.1), 10); Assert.IsNotNull(ms.RandomSource); ms.RandomSource = rnd; Assert.IsNotNull(ms.RandomSource); }
public static IEnumerable<double> Paths(double S, double sigma, double maturity, double N) { double var = sigma*sigma*maturity; Normal n = new Normal(); for (int i = 0; i < N; i++ ) { double normalsample = n.Sample(); double Sf = S * Math.Exp(-0.5 * var + Math.Sqrt(var) * normalsample); yield return Sf; } }
public void SampleArrayTest() { var normal = new Normal(0.0, 1.0); var rnd = new SystemRandomSource(1); var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => Normal.PDF(x, 0.1, y), x => Normal.Sample(rnd, x, 0.1), 10) { RandomSource = rnd }; ms.Sample(5); }
public void SampleTest() { var normal = new Normal(0.0, 1.0); var rnd = new SystemRandomSource(1); var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(rnd, x, 0.1), 10) { RandomSource = rnd }; ms.Sample(); }
public static List <double> normcdf(List <double> x, double mu, double sig) { List <double> res = new List <double>(); Normal normal = new MathNet.Numerics.Distributions.Normal(mu, sig); for (int i = 0; i < x.Count; i++) { res.Add(normal.CumulativeDistribution(x[i])); } return(res); }
public Node(string ip, int port,int type) { this.type = type; this.ip = ip; this.port = port; thread = new Thread(new ThreadStart(beginNode)); end = false; nextIntimacyDistributionSpeaking = new Normal(4.0, 1.0); nextIntimacyDistributionListening = new Normal(5.0, 1.0); intimacyLengthDistribution = new Normal(1.0, 0.2); tracker = new FaceTrackerProxy(ip,port); tracker.setWholeBodyOn(false); }
public void SetupDistributions() { dists = new IDistribution[8]; dists[0] = new Beta(1.0, 1.0); dists[1] = new ContinuousUniform(0.0, 1.0); dists[2] = new Gamma(1.0, 1.0); dists[3] = new Normal(0.0, 1.0); dists[4] = new Bernoulli(0.6); dists[5] = new Weibull(1.0, 1.0); dists[6] = new DiscreteUniform(1, 10); dists[7] = new LogNormal(1.0, 1.0); }
private static Matrix <double> RandInitializeWeights(int rows, int columns) { var srs = new MathNet.Numerics.Random.SystemRandomSource(); IContinuousDistribution dist = new MathNet.Numerics.Distributions.Normal(srs); double epsilon = 0.12; //Matrix<double> w = Matrix<double>.Build.Random(rows, columns) * (2 * epsilon) - epsilon; var seq = srs.NextDoubleSequence().Take(rows * columns); Matrix <double> w = Matrix <double> .Build.DenseOfColumnMajor(rows, columns, seq); w = w * (2 * epsilon) - epsilon; return(w); }
private static double GetProbabilityValue(Normal nr, double step, double p) { var result = nr.Mean; while (true) { double prob = GetProbability(nr, result); if (prob <= p) { break; } result += step; } return result; }
public void MetropolisHastingsConstructor() { var normal = new Normal(0.0, 1.0); var rnd = new SystemRandomSource(1); var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => Normal.PDF(x, 0.1, y), x => Normal.Sample(rnd, x, 0.1), 10) { RandomSource = rnd }; Assert.IsNotNull(ms.RandomSource); ms.RandomSource = new System.Random(0); Assert.IsNotNull(ms.RandomSource); }
public void NormalityTestAlgorithmTest() { int sampleSize = 100; int numberOfMonteCarloTests = 5; double result = 0; NormalityTest.NormalityTestFunc normalityTestFunc = NormalityTest.JaqueBeraTest; // normal distribution Console.WriteLine("normal var"); for (int i = 0; i < numberOfMonteCarloTests; i++) { var distribution = new Normal(50, 1); distribution.RandomSource = new Random(DateTime.Now.Millisecond * i); var normalSamples = distribution.Samples().Take(sampleSize); double[] reallIsNormalSamples = normalSamples.ToArray(); result = normalityTestFunc(reallIsNormalSamples); Console.WriteLine("reallIsNormalSamples_" + i + ": " + result); } Console.WriteLine(); // random distribution. Console.WriteLine("rand var"); Random rnd = new Random(); double[] randomArray = new double[sampleSize]; for (int i = 0; i < numberOfMonteCarloTests; i++) { for (int j = 0; j < sampleSize; j++) { randomArray[j] = rnd.Next(1, 100); } result = normalityTestFunc(randomArray); Console.WriteLine("randomArray_" + i + ": " + result); } Console.WriteLine(); // Uniform distribution Console.WriteLine("rand var"); var uniformSamples = new Normal(100, 0).Samples().Take(sampleSize); double[] uniformSamplesRandomVar = uniformSamples.ToArray(); result = normalityTestFunc(uniformSamplesRandomVar); Console.WriteLine("uniformSamples: " + result); }
/// <summary> /// Initialize a matrix with normal distributed values with mean = 0.0 std = 0.01 /// </summary> /// <param name="ids">Identifiers.</param> /// <param name="M">Matrix to initialize</param> private static void initMatrixNormal(IList <int> ids, ref double[,] M, ref Dictionary <int, int> mapper, int K) { MathNet.Numerics.Distributions.Normal normalDist = new MathNet.Numerics.Distributions.Normal(0.0, 0.01); int i = 0; foreach (int id in ids) { for (int j = 0; j < K; j++) { M [i, j] = normalDist.Sample(); mapper [id] = i; } i++; } }
private static double[] GenerateFunamentalValuePath(int simulationSteps, double fundamentalValueInitial, double fundamentalValueDrift, double fundamentalValueVariance) { Normal standardNormal = new Normal(0, 1); var fundamentalValue = new double[simulationSteps]; fundamentalValue[0] = fundamentalValueInitial; for (int i = 1; i < simulationSteps; i++) { fundamentalValue[i] = CalculateNextValue(1, fundamentalValue[i-1], fundamentalValueDrift, fundamentalValueVariance, standardNormal); } return fundamentalValue; }
public void SameVariance() { double actualBayesRisk = 30.514; double actualCommonPoint =0.5; double testedCommonPoint =0.0; double p1= 0.4; double p2 = 0.6; Normal gen1 =new Normal(0,1); Normal gen2 =new Normal(1,1); NaiveBayes bayes = new NaiveBayes(new List<IContinuousDistribution>(){gen1},new List<IContinuousDistribution>(){gen2},p1,p2); Assert.That(NaiveBayes.FindCommonPointNormalDistribution(gen1, gen2, ref testedCommonPoint), Is.True); Assert.That(Math.Abs(actualCommonPoint-testedCommonPoint),Is.LessThan(0.001)); //Assert.That(Math.Abs(actualBayesRisk - NaiveBayes.CalculateBayesRisk(gen1, gen2, p1, p2)), Is.LessThan(0.001)); }
private void GenerateUCube(DISTRIBUTION d) { if (array != null) { return; } array = new double[number_of_sims][][]; var uniform = new MathNet.Numerics.Random.MersenneTwister(seed); var normal = new MathNet.Numerics.Distributions.Normal(0, 1, uniform); Func <double[]> rgn = null; switch (d) { case DISTRIBUTION.UNIFORM: rgn = () => { return(uniform.NextDoubles((int)number_of_steps)); }; break; case DISTRIBUTION.NORMAL: rgn = () => { double[] r = new double[number_of_steps]; normal.Samples(r); return(r); }; break; } for (UInt64 i = 0; i < number_of_sims; ++i) { array[i] = new double[number_of_dims][]; for (UInt64 j = 0; j < number_of_dims; ++j) { array[i][j] = rgn(); } } }
/// <summary> /// Uses Infer3DExactLMS to infer a random world point from a number of different projections of the point. /// It writes out the squared errors corresponding to different number of projections into a file. /// </summary> /// <param name="numProjections">the max number of projections to use.</param> /// <param name="gaussianNoiseSigma">the standard deviation of the gaussian noise to be added to the projected points.</param> public static void ShowErrorInfer3DExactLMS(int numProjections, double gaussianNoiseSigma, string fileName) { ContinuousUniform dist = new ContinuousUniform(0, 1); Normal gaussianNoise = new Normal(0, gaussianNoiseSigma); DenseMatrix worldPoint = new DenseMatrix(4,1); worldPoint = (DenseMatrix) worldPoint.Random(4,1, dist); ProjectedPoint[] projections = new ProjectedPoint[numProjections]; for (int i = 0; i < projections.Length; i++) { projections[i] = new ProjectedPoint(); projections[i].worldToImage = new DenseMatrix(3, 4); projections[i].worldToImage = (DenseMatrix)projections[i].worldToImage.Random(3, 4, dist); projections[i].projectedPoint = (projections[i].worldToImage * worldPoint); projections[i].projectedPoint += (DenseMatrix)projections[i].projectedPoint.Random(3, 1, gaussianNoise); } File.WriteAllLines(fileName, Enumerable.Range(2, numProjections) .Select(i => String.Format("{0}\t{1}", i, (worldPoint - Infer3DExactLMS(projections.Take(i))).L2Norm()))); }
public object NormalDistSample(int mean, int stddev, int size, int nbuckets) { var normal = new Normal(mean, stddev); var data = new double[size]; for (var i = 0; i < data.Length; i++) { data[i] = normal.Sample(); } var histogram = new Histogram(data, nbuckets); var buckets = new List<Bucket>(); for (var i = 0; i < histogram.BucketCount; i++) { buckets.Add(histogram[i]); } return buckets.Select(x=> new {lowerBound = x.LowerBound, upperBound = x.UpperBound, count = x.Count}).ToList(); }
private static double Nu(double x, double tol) { double nu; double lnu0, lnu1, dk, xk; int i, k; // fpnorm(x): pnorm(x, 0, 1, lower.tail=TRUE, log.p=FALSE) // calculates P(X <= x) var norm = new Normal(); // N(0, 1) if (x > 0.01) { lnu1 = Math.Log(2.0) - 2 * Math.Log(x); lnu0 = lnu1; k = 2; dk = 0; for (i = 0; i < k; i++) { dk = dk + 1; xk = -x * Math.Sqrt(dk) / 2.0; lnu1 = lnu1 - 2.0 * norm.CumulativeDistribution(xk) / dk; } while (Math.Abs((lnu1 - lnu0) / lnu1) > tol) { lnu0 = lnu1; for (i = 0; i < k; i++) { dk = dk + 1; xk = -x * Math.Sqrt(dk) / 2.0; lnu1 = lnu1 - 2.0 * norm.CumulativeDistribution(xk) / dk; } k *= 2; } } else { lnu1 = -0.583 * x; } nu = Math.Exp(lnu1); return nu; }
private static AggregationResult RunAggregation(ClusterNetwork net, double bias) { Dictionary <Vertex, double> _attributes = new Dictionary <Vertex, double>(); Dictionary <Vertex, double> _aggregates = new Dictionary <Vertex, double>(); MathNet.Numerics.Distributions.Normal normal = new MathNet.Numerics.Distributions.Normal(0d, 5d); AggregationResult result = new AggregationResult(); result.Modularity = net.NewmanModularityUndirected; double average = 0d; foreach (Vertex v in net.Vertices) { _attributes[v] = normal.Sample(); _aggregates[v] = _attributes[v]; average += _attributes[v]; } average /= (double)net.VertexCount; double avgEstimate = double.MaxValue; result.FinalVariance = double.MaxValue; result.FinalOffset = 0d; for (int k = 0; k < Properties.Settings.Default.ConsensusRounds; k++) { foreach (Vertex v in net.Vertices.ToArray()) { Vertex w = v.RandomNeighbor; List <Vertex> intraNeighbors = new List <Vertex>(); List <Vertex> interNeighbors = new List <Vertex>(); ClassifyNeighbors(net, v, intraNeighbors, interNeighbors); double r = net.NextRandomDouble(); if (r <= bias && interNeighbors.Count > 0) { w = interNeighbors.ElementAt(net.NextRandom(interNeighbors.Count)); } _aggregates[v] = aggregate(_aggregates[v], _aggregates[w]); _aggregates[w] = aggregate(_aggregates[v], _aggregates[w]); } avgEstimate = 0d; foreach (Vertex v in net.Vertices.ToArray()) { avgEstimate += _aggregates[v]; } avgEstimate /= (double)net.VertexCount; result.FinalVariance = 0d; foreach (Vertex v in net.Vertices.ToArray()) { result.FinalVariance += Math.Pow(_aggregates[v] - avgEstimate, 2d); } result.FinalVariance /= (double)net.VertexCount; double intraVar = 0d; foreach (int c in net.ClusterIDs) { double localavg = 0d; double localvar = 0d; foreach (Vertex v in net.GetNodesInCluster(c)) { localavg += _aggregates[v]; } localavg /= net.GetClusterSize(c); foreach (Vertex v in net.GetNodesInCluster(c)) { localvar += Math.Pow(_aggregates[v] - localavg, 2d); } localvar /= net.GetClusterSize(c); intraVar += localvar; } intraVar /= 50d; //Console.WriteLine("i = {0:0000}, Avg = {1:0.000}, Estimate = {2:0.000}, Intra-Var = {3:0.000}, Total Var = {4:0.000}", result.iterations, average, avgEstimate, intraVar, totalVar); } result.FinalOffset = average - avgEstimate; return(result); }
//put the cars to be spawned into a map that tells each spawner node when to spawn some number of cars public void setupSpawnersDistribution() { //for testing int sum = 0; int spawnerNum = GridManagerScript.spawners.Count; //make sure we don't devide by 0 if (spawnerNum > 0) { int toAddToFirst = spawnNumber % spawnerNum; int toDistribute = spawnNumber - toAddToFirst; int carsForEach = toDistribute / spawnerNum; //distribute cars for the first node (with the added remainder) using firstNodeCars int firstNodeCars = carsForEach + toAddToFirst; // non deterministic MathNet.Numerics.Distributions.Normal normalDist = new MathNet.Numerics.Distributions.Normal(rushHour, standartDeviation); //now do the rest foreach (var spawner in GridManagerScript.spawners) { int loopsToAllocate = carsForEach; if (spawner == GridManagerScript.spawners.First()) { loopsToAllocate = firstNodeCars; } Dictionary <double, int> carSpawnerBuckets = new Dictionary <double, int>(); for (int i = 0; i < 24; i++) { for (int c = 0; c < 60; c++) { double t = System.Math.Round(i + c / 100.0, 2); carSpawnerBuckets[t] = 0; } } //allocate new buckets for (int i = 0; i < loopsToAllocate; i++) { double spawnTime = normalDist.Sample();//in hours if (spawnTime < 0) { spawnTime = 24 + spawnTime; } double minutes = (int)( (spawnTime - System.Math.Truncate(spawnTime)) * 60) / 100.0; double hours = System.Math.Truncate(spawnTime); double timeOfDay = System.Math.Round((minutes + hours) % 24, 2); try { carSpawnerBuckets[timeOfDay] += 1; sum += 1; } catch (System.Exception) { Debug.Log(timeOfDay); Debug.Log(carSpawnerBuckets.ContainsKey(timeOfDay)); } } spawner.addSpawnBucket(this, carSpawnerBuckets); } //end for non-deterministic //Deterministic // for (int i = 0; i < 24; i++) // { // for (int c = 0; c < 60; c++) // { // //deterministic // } // } //distribute cars for the other nodes (with no remainder) using carsForEach // bool flagFirst = true; // foreach (var spawner in GridManagerScript.spawners) // { // if (flagFirst)//skip first // { // flagFirst = false; // continue; // } // } } }
/// <summary> /// Samples student-t distributed random variables. /// </summary> /// <remarks>The algorithm is method 2 in section 5, chapter 9 /// in L. Devroye's "Non-Uniform Random Variate Generation"</remarks> /// <param name="rnd">The random number generator to use.</param> /// <param name="location">The location (μ) of the distribution.</param> /// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param> /// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param> /// <returns>a random number from the standard student-t distribution.</returns> static double SampleUnchecked(System.Random rnd, double location, double scale, double freedom) { var gamma = Gamma.SampleUnchecked(rnd, 0.5 * freedom, 0.5); return(Normal.Sample(rnd, location, scale * Math.Sqrt(freedom / gamma))); }
public void CanCreateStandardNormal() { var n = new Normal(); Assert.AreEqual(0.0, n.Mean); Assert.AreEqual(1.0, n.StdDev); }
/// <summary> /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// </summary> /// <returns>a sample from the distribution.</returns> public double Sample() { return(Math.Exp(Normal.Sample(_random, _mu, _sigma))); }
/// <summary> /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// </summary> /// <returns>a sample from the distribution.</returns> public double Sample() { return(Math.Exp(Normal.SampleUnchecked(RandomSource, _mu, _sigma))); }
/// <summary> /// Generates a sequence of samples from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// </summary> /// <returns>a sequence of samples from the distribution.</returns> public IEnumerable <double> Samples() { return(Normal.Samples(_random, _mu, _sigma).Select(Math.Exp)); }
/// <summary> /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="mu">The log-scale (μ) of the distribution.</param> /// <param name="sigma">The shape (σ) of the distribution. Range: σ ≥ 0.</param> /// <returns>a sample from the distribution.</returns> public static double Sample(System.Random rnd, double mu, double sigma) { return(Math.Exp(Normal.Sample(rnd, mu, sigma))); }
/// <summary> /// Generates a sequence of samples from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="mu">The log-scale (μ) of the distribution.</param> /// <param name="sigma">The shape (σ) of the distribution. Range: σ ≥ 0.</param> /// <returns>a sequence of samples from the distribution.</returns> public static IEnumerable <double> Samples(System.Random rnd, double mu, double sigma) { return(Normal.Samples(rnd, mu, sigma).Select(Math.Exp)); }
public void CanEstimateParameters(double mean, double stddev) { var original = new Normal(mean, stddev, new Random(100)); var estimated = Normal.Estimate(original.Samples().Take(10000)); AssertHelpers.AlmostEqualRelative(mean, estimated.Mean, 1); AssertHelpers.AlmostEqualRelative(stddev, estimated.StdDev, 1); }
private void obtenerFrecuenciasEsperadas() { double media = MathNet.Numerics.Statistics.ArrayStatistics.Mean(datos.ToArray()); switch (distribucionElegida) { case TipoDistribucion.continuaExponencial: /* * En el caso de haber elegido la distribucion exponencial, tenemos que: * * lambda(media) = 1 / (media muestral); * */ //obtenemos el lambda para esta distribucion double lambda = 1 / media; //generamos la distribucion para el lambda dado: Exponential exponencial = new Exponential(lambda); //recorremos los intervalos para obtener los valores minimos y maximos, y asi calcular la frecuencia esperada foreach (Intervalo intervalo in intervalos) { intervalo.frecuenciaEsperada = (exponencial.CumulativeDistribution(intervalo.limiteSuperior) - exponencial.CumulativeDistribution(intervalo.limiteInferior)) * datos.Count(); intervalo.frecuenciaEsperada = Math.Round(intervalo.frecuenciaEsperada, CANTIDAD_DECIMALES); } break; case TipoDistribucion.continuaUniforme: /* * En el caso de haber elegido uniforme, tenemos que: * * FE = cantidad de datos de la muestra / cantidad de intervalos; * */ //Entonces obtenemos este dato y se lo asignamos a todos los intervalos. double frecuenciaEsperada = (double)datos.Count() / (double)intervalos.Count(); foreach (Intervalo intervalo in intervalos) { intervalo.frecuenciaEsperada = frecuenciaEsperada; intervalo.frecuenciaEsperada = Math.Round(intervalo.frecuenciaEsperada, CANTIDAD_DECIMALES); } break; case TipoDistribucion.continuaNormal: /* * Para distribucion normal tenemos: * * desviacion estandar (sigma) = raiz cuadrada de la media; * */ //obtenemos la varianza double varianza = MathNet.Numerics.Statistics.ArrayStatistics.Variance(datos.ToArray()); //calculamos la deviacion estandar double desviacionEstandar = Math.Sqrt(varianza); //creo la distribucion normal MathNet.Numerics.Distributions.Normal normal = new MathNet.Numerics.Distributions.Normal(media, desviacionEstandar); foreach (Intervalo intervalo in intervalos) { intervalo.frecuenciaEsperada = (normal.CumulativeDistribution(intervalo.limiteSuperior) - normal.CumulativeDistribution(intervalo.limiteInferior)) * datos.Count(); intervalo.frecuenciaEsperada = Math.Round(intervalo.frecuenciaEsperada, CANTIDAD_DECIMALES); } break; case TipoDistribucion.continuaPoisson: double lambdaPoisson = 1 / media; Poisson poisson = new Poisson(media); //recorremos los intervalos para obtener los valores minimos y maximos, y asi calcular la frecuencia esperada foreach (Intervalo intervalo in intervalos) { intervalo.frecuenciaEsperada = (poisson.CumulativeDistribution(intervalo.limiteInferior) - poisson.CumulativeDistribution(intervalo.limiteInferior - 1)) * datos.Count(); intervalo.frecuenciaEsperada = Math.Round(intervalo.frecuenciaEsperada, CANTIDAD_DECIMALES); } break; } }
public void CanCreateNormal(double mean, double sdev) { var n = new Normal(mean, sdev); Assert.AreEqual(mean, n.Mean); Assert.AreEqual(sdev, n.StdDev); }
static double SampleUnchecked(System.Random rnd, double mu, double sigma) { return(Math.Exp(Normal.SampleUnchecked(rnd, mu, sigma))); }