private void clusterButton_Click(object sender, RoutedEventArgs e) { int nElements = 100; List <double> elements = new List <double>(); Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution cud = new Troschuetz.Random.Distributions.Continuous.ContinuousUniformDistribution(0, 1); for (int i = 0; i < nElements; i++) { elements.Add(cud.NextDouble()); } Ball <double> .Metric metric = (double x, double y) => Math.Abs(x - y); Tuple <List <Ball <double> >, bool, int> tuple = Ball <double> .Cluster2FixedNumberOfBalls(elements, metric, 10, 10000); List <Ball <double> > balls = tuple.Item1; using (System.IO.StreamWriter writer = System.IO.File.CreateText("testClusterer.txt")) { foreach (Ball <double> ball in balls) { foreach (double element in ball.Elements) { writer.WriteLine(element + "\t" + ball.Center + "\t" + ball.Radius + "\t" + Math.Abs(element - ball.Center)); } } } MessageBox.Text += "Cluster done.\n"; MessageBox.Text += "Converged = " + tuple.Item2 + "\n"; MessageBox.Text += "n Steps taken = " + tuple.Item3 + "\n"; }
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 T Next() { double u = uniGen.NextDouble(); for (int i = 0; i < cumulatives.Length; i++) { if (u <= cumulatives[i]) { return(items[i]); } } return(items.Last()); }
/// <summary> /// generates a single step in an MCMC algorithm /// </summary> public void Step() { lastAccepted = false; state.lastAccepted = false; // the argument is the likelihood value at the current position if (LD == null) { return; } // generate a new point in the auxiliary chain working parameter space MCMCProposal mcmcp = PD(Theta); // Create a new full state that is up-to-date with the proposed parameters MCMCState newState = new MCMCState(); newState.Theta = updateCopyFullParams(mcmcp.Theta); // compute likelihood and prior at new point newState.LogLikelihood = LD(newState.Theta); newState.LogPrior = LPD(newState.Theta); // decide whether to accept the new point if ((newState.LogLikelihood + newState.LogPrior > state.LogLikelihood + state.LogPrior) | AcceptAll) { lastAccepted = true; accepted++; } else { double U = uRand.NextDouble(); if (Math.Log(U) < newState.LogLikelihood + newState.LogPrior - (state.LogLikelihood + state.LogPrior)) { lastAccepted = true; accepted++; } else { state.lastAccepted = false; } } if (lastAccepted) { newState.lastAccepted = true; // update the working parameters Theta = mcmcp.Theta; // update the full set of parameters state = newState; } }
private double MCMCStep(double LLV, double LPV, ref double[] theta) { // the argument is the likelihood value at the current position if (LD == null) { return(Double.NaN); } // generates a single step in an MCMC algorithm lastAccepted = false; // generate new point in parameter space MCMCProposal mcmcp = PD(theta); double[] thetaNew = mcmcp.Theta; // compute likelihood and prior at new point double LLVNew = LD(thetaNew); double LPVNew = LPD(thetaNew); double logAccept = LLVNew + LPVNew - (LLV + LPV) + mcmcp.LogRatio; // decide whether to accept new point if ((LLVNew + LPVNew > LLV + LPV) | AcceptAll) { lastAccepted = true; accepted++; } else { // debug: double U = uRand.NextDouble(); ////////////////////////////// if (Math.Log(U) < LLVNew + LPVNew - (LLV + LPV)) { lastAccepted = true; accepted++; } else { lastAccepted = false; } } if (lastAccepted) { theta = thetaNew; return(LLVNew); } return(LLV); }