private void writeHistory(int step, StreamWriter sw, MCMCState state) { sw.Write(step + "\t" + state.LogLikelihood.ToString()); sw.Write("\t" + lastAuxChain.ToString()); sw.WriteLine("\t" + state.lastAccepted.ToString()); sw.Flush(); }
public MCMCState SwapStates(MCMCState _motherState) { // preserve the current set of full parameters for returning to the parent MCMCState auxiliaryState = state; state = _motherState; // update the working parameters updateWorkingFromFullParams(); return(auxiliaryState); }
public void Initialize(LikelihoodDelegate _LD, LogPriorDelegate _LPD, ProposalDelegate _PD, int[] _indices, MCMCState _state) { LD = _LD; LPD = _LPD; PD = _PD; accepted = 0; state = _state; dim = _indices.Length; indices = new int[dim]; indices = _indices; Theta = new double[dim]; updateWorkingFromFullParams(); }
/// <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; } }
public void Step() { lastAuxChain = discreteUniformDist.Next(); Mother = mcmc[lastAuxChain].SwapStates(Mother); for (int i = 0; i < numAuxiliary; i++) { if (i == lastAuxChain) { continue; // skip the swapped chain } mcmc[i].Step(); } }