public SimpleSimultaneousGaussianProposal(double[] sigma) { this.sigma = sigma; gen = new Troschuetz.Random.Generators.MT19937Generator(); gDist = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(gen, 0, 1); dim = sigma.Length; }
public SimpleSequentialGaussianProposal(double[] sigma) { this.sigma = sigma; gen = new Troschuetz.Random.Generators.MT19937Generator(); gDist = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(gen, 0, 1); dim = sigma.Length; duDist = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen, 0, dim - 1); }
//--------------------------------------------------------------------- /// <summary> /// Initializes the plug-in with a data file. /// </summary> /// <param name="dataFile"> /// Path to the file with initialization data. /// </param> /// <param name="startTime"> /// Initial timestep (year): the timestep that will be passed to the /// first call to the component's Run method. /// </param> public override void Initialize() { HurricaneEvent.WindMortalityTable = new HurricaneWindMortalityTable(parameters.WindSpeedMortalityProbabilities); if (parameters.InputUnitsEnglish) { parameters.LowBoundLandfallWindSpeed *= 1.60934; parameters.ModeLandfallWindSpeed *= 1.60934; parameters.HighBoundLandfallWindspeed *= 1.60934; parameters.CenterPointDistanceInland *= 1.60934; //HurricaneEvent.WindMortalityTable.ChangeSpeedsFromEnglishToMetric(); } HurricaneEvent.WindSpeedGenerator = new WindSpeedGenerator(this.parameters.LowBoundLandfallWindSpeed, this.parameters.ModeLandfallWindSpeed, this.parameters.HighBoundLandfallWindspeed); //parameters.AdjustValuesFromEnglishToMetric(); List <string> colnames = new List <string>(); foreach (IEcoregion ecoregion in modelCore.Ecoregions) { colnames.Add(ecoregion.Name); } ExtensionMetadata.ColumnNames = colnames; MetadataHandler.InitializeMetadata(parameters.Timestep, parameters.MapNamesTemplate); Timestep = parameters.Timestep; mapNameTemplate = parameters.MapNamesTemplate; SiteVars.Initialize(); this.ContinentalGrid = new ContinentalGrid( this.parameters.CenterPointLatitude, PlugIn.ModelCore.CellLength, PlugIn.ModelCore.Landscape.Columns, PlugIn.ModelCore.Landscape.Rows, this.parameters.CenterPointDistanceInland ); if (parameters.HurricaneRandomNumberSeed > 0) { HurricaneEvent.HurricaneRandomNumber = true; HurricaneGeneratorStandard = new Troschuetz.Random.Generators.MT19937Generator((uint)parameters.HurricaneRandomNumberSeed); HurricaneGeneratorLogNormal = new Troschuetz.Random.Distributions.Continuous.LognormalDistribution((uint)parameters.HurricaneRandomNumberSeed); } //double testDouble = hurricaneGenerator.NextDouble(); }
public ParallelHierarchicalSampler(int _total, int _burnIn, int _numSubSpaces, int _dim) { OutputDirectory = Directory.GetCurrentDirectory(); numAuxiliary = _numSubSpaces; dim = _dim; total = _total; burnIn = _burnIn; mean = new double[dim]; if (!SkipCovarianceComputation) { covariance = new double[dim, dim]; } CI = new double[dim]; mcmc = new AuxiliaryChain[numAuxiliary]; for (int i = 0; i < numAuxiliary; i++) { mcmc[i] = new AuxiliaryChain(); } gen = new Troschuetz.Random.Generators.MT19937Generator(); discreteUniformDist = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen, 0, numAuxiliary - 1); }
public MCMC() { mt = new Troschuetz.Random.Generators.MT19937Generator(); uRand = new Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution(mt, 0, 1); OutputDirectory = Directory.GetCurrentDirectory(); }
public AuxiliaryChain() { mt = new Troschuetz.Random.Generators.MT19937Generator(); uRand = new Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution(mt, 0, 1); }
static PMF() { generator = new Troschuetz.Random.Generators.MT19937Generator(); uniGen = new Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution(generator); }
public static Tuple <List <Ball <T> >, bool, int> Cluster2FixedNumberOfBalls(List <T> elements, Metric metric, int nBalls, int maxNumSteps) { if (elements.Count <= nBalls) { // nothing to do return(new Tuple <List <Ball <T> >, bool, int>(null, false, 0)); } List <Ball <T> > balls = new List <Ball <T> >(); for (int i = 0; i < nBalls; i++) { balls.Add(new Ball <T>(metric)); } Troschuetz.Random.Generators.MT19937Generator gen = new Troschuetz.Random.Generators.MT19937Generator(); Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution dud = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen); dud.Alpha = 0; dud.Beta = nBalls - 1; // start by assigning elements to balls at random Dictionary <T, Ball <T> > assigmnent = new Dictionary <T, Ball <T> >(); foreach (T element in elements) { balls[dud.Next()].AddElement(element); } // check for empty balls for (int i = 0; i < nBalls; i++) { if (balls[i].Elements.Count == 0) { // select another ball at random bool done = false; while (!done) { int iBall = dud.Next(); if (balls[iBall].Elements.Count > 1) { T elementToMove = balls[iBall].Elements[0]; balls[iBall].RemoveElement(elementToMove); balls[i].AddElement(elementToMove); done = true; } } } } foreach (Ball <T> ball in balls) { foreach (T element in ball.elements) { assigmnent.Add(element, ball); } } int nStepsTaken = maxNumSteps; bool converged = false; for (int iStep = 0; iStep < maxNumSteps; iStep++) { int nChanged = 0; foreach (T element in elements) { double smallestDistance = double.MaxValue; Ball <T> nearestBall = null; foreach (Ball <T> ball in balls) { double dist = metric(element, ball.Center); if (dist < smallestDistance) { smallestDistance = dist; nearestBall = ball; } } if (nearestBall.Elements.Contains(element)) { continue; } nearestBall.AddElement(element); assigmnent[element].RemoveElement(element); assigmnent.Remove(element); assigmnent.Add(element, nearestBall); nChanged++; } if (nChanged == 0) { nStepsTaken = iStep; converged = true; break; } } return(new Tuple <List <Ball <T> >, bool, int>(balls, converged, nStepsTaken)); }
public static Tuple <List <Ball <T> >, bool, int> ClusterFixedNumberOfBalls(List <T> elements, Metric metric, int nBalls, int maxNumSteps) { if (elements.Count <= nBalls) { // nothing to do return(new Tuple <List <Ball <T> >, bool, int>(null, false, 0)); } List <Ball <T> > balls = new List <Ball <T> >(); for (int i = 0; i < nBalls; i++) { balls.Add(new Ball <T>(metric)); } Troschuetz.Random.Generators.MT19937Generator gen = new Troschuetz.Random.Generators.MT19937Generator(); Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution dud = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen); dud.Alpha = 0; dud.Beta = nBalls - 1; // start by assigning elements to balls at random Dictionary <T, Ball <T> > assigmnent = new Dictionary <T, Ball <T> >(); foreach (T element in elements) { balls[dud.Next()].AddElement(element); } // check for empty balls for (int i = 0; i < nBalls; i++) { if (balls[i].Elements.Count == 0) { // select another ball at random bool done = false; while (!done) { int iBall = dud.Next(); if (balls[iBall].Elements.Count > 1) { T elementToMove = balls[iBall].Elements[0]; balls[iBall].RemoveElement(elementToMove); balls[i].AddElement(elementToMove); done = true; } } } } foreach (Ball <T> ball in balls) { foreach (T element in ball.elements) { assigmnent.Add(element, ball); } } Dictionary <T, double> distanceToCenter = new Dictionary <T, double>(); double greatestRadius = double.MinValue; Ball <T> ballWithGreatestRadius = null; foreach (Ball <T> ball in balls) { if (ball.radius > greatestRadius) { greatestRadius = ball.radius; ballWithGreatestRadius = ball; } } int nStepsTaken = maxNumSteps; bool converged = false; for (int iStep = 0; iStep < maxNumSteps; iStep++) { T outlier = ballWithGreatestRadius.MostDistantElement; double smallestDistance = double.MaxValue; Ball <T> nearestBall = null; foreach (Ball <T> ball in balls) { double dist = metric(outlier, ball.Center); if (dist < smallestDistance) { smallestDistance = dist; nearestBall = ball; } } // if the closest center is closer than current center, move it. Else end. if (smallestDistance < ballWithGreatestRadius.radius) { // move outlier ballWithGreatestRadius.RemoveElement(outlier); nearestBall.AddElement(outlier); } else { converged = true; nStepsTaken = iStep; break; } // find the next outlier greatestRadius = double.MinValue; foreach (Ball <T> ball in balls) { if (ball.radius > greatestRadius) { greatestRadius = ball.radius; ballWithGreatestRadius = ball; } } } return(new Tuple <List <Ball <T> >, bool, int>(balls, converged, nStepsTaken)); }
public Annealer() { mt = new Troschuetz.Random.Generators.MT19937Generator(); uRand = new Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution(mt); zRand = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(mt); }
static DiscreteDistribution() { mt = new Troschuetz.Random.Generators.MT19937Generator(); }
//private void mcmcButton_Click(object sender, RoutedEventArgs e) //{ // // initialize rng // Troschuetz.Random.Generators.MT19937Generator mt = new Troschuetz.Random.Generators.MT19937Generator(); // Troschuetz.Random.Distributions.Continuous.NormalDistribution z = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(mt, 0, 1); // // make data // double alpha = 0; // double beta = 0.333; // int n = 10; // double tau = 0.1; // double[] x = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // double[] y = new double[n]; // for (int i = 0; i < n; i++) // { // y[i] = alpha + beta * x[i] + Math.Sqrt(tau) * z.NextDouble(); // } // List<double> estimates = linearRegression(x, y); // TestStatistics ts = new TestStatistics(); // ts.LoadData(x, y); // MCMC mcmc = new MCMC(); // //SimpleSequentialGaussianProposal ssgp = new SimpleSequentialGaussianProposal(new double[] { 0.1, 0.1, 0.1 }); // //SimpleSimultaneousGaussianProposal sgp = new SimpleSimultaneousGaussianProposal(new double[] { 0.01, 0.01, 0.01 }); // SimpleSimultaneousGaussianProposal sgp = new SimpleSimultaneousGaussianProposal(new double[] { 0.01, 0.01, 0.01 }); // int burnin = 0; // int total = 1000; // mcmc.Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, sgp.Proposal, 3, total, burnin); // double tauInit = Math.Log(estimates[2]); // double[] pars = new double[]{estimates[0],estimates[1],tauInit}; // mcmc.MCMCRun(pars); //} private void mcmcButton_Click(object sender, RoutedEventArgs e) { // initialize rng Troschuetz.Random.Generators.MT19937Generator mt = new Troschuetz.Random.Generators.MT19937Generator(); Troschuetz.Random.Distributions.Continuous.NormalDistribution z = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(mt, 0, 1); // make data double alpha = 0; double beta = 0.333; int n = 10; double tau = 0.1; double[] x = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; double[] y = new double[n]; for (int i = 0; i < n; i++) { y[i] = alpha + beta * x[i] + Math.Sqrt(tau) * z.NextDouble(); } List <double> estimates = linearRegression(x, y); TestStatistics ts = new TestStatistics(); ts.LoadData(x, y); double tauInit = Math.Log(estimates[2]); double[] pars = new double[] { estimates[0], estimates[1], tauInit }; int burnin = 50000; int total = 50000; // 1 parameter per auxiliary chain int numSubModels = 3; AuxiliaryChain[] submodel = new AuxiliaryChain[] { new AuxiliaryChain(), new AuxiliaryChain(), new AuxiliaryChain() }; SimpleSimultaneousGaussianProposal[] gp = new SimpleSimultaneousGaussianProposal[] { new SimpleSimultaneousGaussianProposal(new double[] { 0.01 }), new SimpleSimultaneousGaussianProposal(new double[] { 0.01 }), new SimpleSimultaneousGaussianProposal(new double[] { 0.01 }) }; ParallelHierarchicalSampler mother = new ParallelHierarchicalSampler(total, burnin, numSubModels, 3); mother.Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, pars); for (int i = 0; i < numSubModels; i++) { mother.mcmc[i].Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, gp[i].Proposal, new int[] { i }, mother.Mother); } ////// all 3 parameters in the auxiliary chains //int numSubModels = 3; //AuxiliaryChain[] submodel = new AuxiliaryChain[] { new AuxiliaryChain(), new AuxiliaryChain(), new AuxiliaryChain() }; //SimpleSimultaneousGaussianProposal[] gp = new SimpleSimultaneousGaussianProposal[] // { new SimpleSimultaneousGaussianProposal(new double[] { 0.01, 0.01, 0.01 }), // new SimpleSimultaneousGaussianProposal(new double[] { 0.01, 0.01, 0.01 }), // new SimpleSimultaneousGaussianProposal(new double[] { 0.01, 0.01, 0.01 }) }; //ParallelHierarchicalSampler mother = new ParallelHierarchicalSampler(total, burnin, numSubModels, 3); //mother.Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, pars); //for (int i = 0; i < numSubModels; i++) //{ // mother.mcmc[i].Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, gp[i].Proposal, new int[] { 0, 1, 2 }, mother.state); //} //// single chain //int numSubModels = 1; //AuxiliaryChain[] submodel = new AuxiliaryChain[] { new AuxiliaryChain() }; //SimpleSimultaneousGaussianProposal[] gp = new SimpleSimultaneousGaussianProposal[] { new SimpleSimultaneousGaussianProposal(new double[] { 0.01 }) }; //ParallelHierarchicalSampler mother = new ParallelHierarchicalSampler(total, burnin, numSubModels, 3); //mother.Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, pars); //mother.mcmc[0].Initialize(ts.LogLikelihoodLR, ts.LogPriorLR, gp[0].Proposal, new int[] { 0, 1, 2 }, mother.state); mother.Run(); }