Exemple #1
0
        /// <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;
            }
        }
Exemple #2
0
        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);
        }