public double GenerateSample() { // generate a single deviate of Qm/Qr double[] delta = new double[K + 1]; for (int i = 0; i < K + 1; i++) { delta[i] = zeta * rnorm.NextDouble(); } double[] v = new double[K + 1]; for (int i = 0; i < K + 1; i++) { v[i] = sigma * rnorm.NextDouble(); } double[] w = new double[N - K - 1]; for (int i = 0; i < N - K - 1; i++) { w[i] = sigma * rnorm.NextDouble(); } double num = 0; for (int i = 0; i < K + 1; i++) { num += Math.Pow(v[i] + delta[i], 2); } double denom = 0; for (int i = 0; i < K + 1; i++) { denom += Math.Pow(delta[i], 2); } for (int i = 0; i < N - K - 1; i++) { denom += Math.Pow(w[i], 2); } return(num / denom); }
private double AnnealStep(double objectiveValue, ref double[] theta) { // the argument is the function value at the current position if (ofd == null) { return(Double.NaN); } // generates a single step in the optimization lastAccepted = false; // generate new point in parameter space double[] thetaNew = theta.Clone() as double[]; for (int i = 0; i < scale.Length; i++) { thetaNew[i] += scale[i] * zRand.NextDouble(); } // compute function at new point double newObjectiveValue = ofd(thetaNew); // decide whether to accept new point if (newObjectiveValue > objectiveValue) { lastAccepted = true; accepted++; } else if (Temperature > 0) { double U = uRand.NextDouble(); if (Temperature * Math.Log(U) <= newObjectiveValue - objectiveValue) { lastAccepted = true; accepted++; } else { lastAccepted = false; } } else { lastAccepted = false; } if (lastAccepted) { theta = thetaNew; return(newObjectiveValue); } return(objectiveValue); }
public override MCMCProposal Proposal(double[] theta) { if (theta.Length != dim) { throw new ArgumentException("The length of the parameter vector is inconsistent with previous lengths."); } double[] newTheta = theta.Clone() as double[]; int i = duDist.Next(); newTheta[i] = theta[i] + sigma[i] * gDist.NextDouble(); return(new MCMCProposal() { Theta = newTheta, LogRatio = 0 }); }
//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(); }