Package jsl.utilities.random.mcmc
Class MetropolisHastingsMV
- java.lang.Object
-
- jsl.utilities.random.mcmc.MetropolisHastingsMV
-
- All Implemented Interfaces:
ObservableIfc,GetRandomNumberStreamIfc,RNStreamControlIfc,SetRandomNumberStreamIfc
public class MetropolisHastingsMV extends java.lang.Object implements RNStreamControlIfc, SetRandomNumberStreamIfc, GetRandomNumberStreamIfc, ObservableIfc
An implementation for a multi-Dimensional Metropolis Hasting process. The process is observable at each step
-
-
Field Summary
Fields Modifier and Type Field Description protected StatisticmyAcceptanceStatprotected booleanmyBurnInFlagprotected double[]myCurrentXprotected doublemyFofCurrentXprotected doublemyFofProposedYprotected booleanmyInitializedFlagprotected double[]myInitialXprotected doublemyLastAcceptanceProbabilityprotected ObservableComponentmyObservableComponentprotected java.util.List<Statistic>myObservedStatListprotected double[]myPrevXprotected ProposalFunctionMVIfcmyProposalFunprotected double[]myProposedYprotected RNStreamIfcmyStreammyRNStream provides a reference to the underlying stream of random numbersprotected FunctionMVIfcmyTargetFun
-
Constructor Summary
Constructors Constructor Description MetropolisHastingsMV(double[] initialX, FunctionMVIfc targetFun, ProposalFunctionMVIfc proposalFun)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected doubleacceptanceFunction(double[] currentX, double[] proposedY)Computes the acceptance function for each stepvoidaddObserver(ObserverIfc observer)Allows the adding (attaching) of an observer to the observablevoidadvanceToNextSubstream()Positions the RNG at the beginning of its next substreamjava.lang.StringasString()booleancontains(ObserverIfc observer)Returns true if the observer is already attachedintcountObservers()Returns how many observers are currently observing the observablestatic MetropolisHastingsMVcreate(double[] initialX, int burnInAmount, FunctionMVIfc targetFun, ProposalFunctionMVIfc proposalFun)voiddeleteObserver(ObserverIfc observer)Allows the deletion (removing) of an observer from the observablevoiddeleteObservers()Deletes all the observers from the observableStatisticgetAcceptanceStat()booleangetAntitheticOption()double[]getCurrentX()doublegetFofCurrentX()doublegetFofProposedY()protected doublegetFunctionRatio(double[] currentX, double[] proposedY)double[]getInitialX()doublegetLastAcceptanceProbability()java.util.List<Statistic>getObservedStat()double[]getPrevX()double[]getProposedY()RNStreamIfcgetRandomNumberStream()voidinitialize()Resets statistics and sets the initial state the the initial value or to the value found via the burn in period (if the burn in period was run).booleanisInitialized()booleanisWarmedUp()double[]next()Moves the process one stepvoidresetStartStream()The resetStartStream method will position the RNG at the beginning of its stream.voidresetStartSubstream()Resets the position of the RNG at the start of the current substreamvoidresetStatistics()Resets the automatically collected statisticsdouble[]runAll(int n)voidrunBurnInPeriod(int burnInAmount)Runs a burn in period and assigns the initial value of the process to the last value from the burn in process.voidsetAntitheticOption(boolean flag)Tells the stream to start producing antithetic variatesvoidsetInitialX(double[] initialX)voidsetRandomNumberStream(RNStreamIfc stream)Sets the underlying random number streamjava.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jsl.utilities.random.rng.GetRandomNumberStreamIfc
getStreamNumber
-
Methods inherited from interface jsl.utilities.random.rng.SetRandomNumberStreamIfc
setRandomNumberStream
-
-
-
-
Field Detail
-
myCurrentX
protected double[] myCurrentX
-
myProposedY
protected double[] myProposedY
-
myPrevX
protected double[] myPrevX
-
myLastAcceptanceProbability
protected double myLastAcceptanceProbability
-
myFofProposedY
protected double myFofProposedY
-
myFofCurrentX
protected double myFofCurrentX
-
myInitialX
protected double[] myInitialX
-
myTargetFun
protected final FunctionMVIfc myTargetFun
-
myProposalFun
protected final ProposalFunctionMVIfc myProposalFun
-
myAcceptanceStat
protected final Statistic myAcceptanceStat
-
myObservedStatList
protected final java.util.List<Statistic> myObservedStatList
-
myObservableComponent
protected final ObservableComponent myObservableComponent
-
myInitializedFlag
protected boolean myInitializedFlag
-
myBurnInFlag
protected boolean myBurnInFlag
-
myStream
protected RNStreamIfc myStream
myRNStream provides a reference to the underlying stream of random numbers
-
-
Constructor Detail
-
MetropolisHastingsMV
public MetropolisHastingsMV(double[] initialX, FunctionMVIfc targetFun, ProposalFunctionMVIfc proposalFun)- Parameters:
initialX- the initial value to start generation processtargetFun- the target functionproposalFun- the proposal function
-
-
Method Detail
-
create
public static MetropolisHastingsMV create(double[] initialX, int burnInAmount, FunctionMVIfc targetFun, ProposalFunctionMVIfc proposalFun)
- Parameters:
initialX- the initial value to start the burn in periodburnInAmount- the number of samples in the burn in periodtargetFun- the target functionproposalFun- the proposal function
-
runBurnInPeriod
public void runBurnInPeriod(int burnInAmount)
Runs a burn in period and assigns the initial value of the process to the last value from the burn in process.- Parameters:
burnInAmount- the amount to burn in
-
initialize
public void initialize()
Resets statistics and sets the initial state the the initial value or to the value found via the burn in period (if the burn in period was run).
-
resetStatistics
public void resetStatistics()
Resets the automatically collected statistics
-
isInitialized
public final boolean isInitialized()
- Returns:
- true if the process has been initialized
-
isWarmedUp
public final boolean isWarmedUp()
- Returns:
- true if the process has been warmed up
-
runAll
public final double[] runAll(int n)
- Parameters:
n- runs the process for n steps- Returns:
- the value of the process after n steps
-
getCurrentX
public final double[] getCurrentX()
- Returns:
- the current state (x) of the process
-
getProposedY
public final double[] getProposedY()
- Returns:
- the last proposed state (y)
-
getPrevX
public final double[] getPrevX()
- Returns:
- the previous state (x) of the process
-
getLastAcceptanceProbability
public final double getLastAcceptanceProbability()
- Returns:
- the last value of the computed probability of acceptance
-
getFofProposedY
public final double getFofProposedY()
- Returns:
- the last value of the target function evaluated at the proposed state (y)
-
getFofCurrentX
public final double getFofCurrentX()
- Returns:
- the last value of the target function evaluated at the current state (x)
-
getAcceptanceStat
public Statistic getAcceptanceStat()
- Returns:
- statistics for the proportion of the proposed state (y) that are accepted
-
getObservedStat
public java.util.List<Statistic> getObservedStat()
- Returns:
- statistics on the observed (generated) values of the process
-
next
public double[] next()
Moves the process one step- Returns:
- the next value of the process after proposing the next state (y)
-
acceptanceFunction
protected double acceptanceFunction(double[] currentX, double[] proposedY)Computes the acceptance function for each step- Parameters:
currentX- the current stateproposedY- the proposed state- Returns:
- the evaluated acceptance function
-
getFunctionRatio
protected double getFunctionRatio(double[] currentX, double[] proposedY)- Parameters:
currentX- the current stateproposedY- the proposed state- Returns:
- the ratio of f(y)/f(x) for the generation step
-
getInitialX
public final double[] getInitialX()
- Returns:
- the specified initial state or the state after the burn in period (if run)
-
setInitialX
public final void setInitialX(double[] initialX)
- Parameters:
initialX- the value to use for the initial state
-
getRandomNumberStream
public final RNStreamIfc getRandomNumberStream()
- Specified by:
getRandomNumberStreamin interfaceGetRandomNumberStreamIfc- Returns:
- the random number stream reference
-
setRandomNumberStream
public final void setRandomNumberStream(RNStreamIfc stream)
Description copied from interface:SetRandomNumberStreamIfcSets the underlying random number stream- Specified by:
setRandomNumberStreamin interfaceSetRandomNumberStreamIfc- Parameters:
stream- the reference to the random number stream, must not be null
-
resetStartStream
public void resetStartStream()
Description copied from interface:RNStreamControlIfcThe resetStartStream method will position the RNG at the beginning of its stream. This is the same location in the stream as assigned when the RNG was created and initialized.- Specified by:
resetStartStreamin interfaceRNStreamControlIfc
-
resetStartSubstream
public void resetStartSubstream()
Description copied from interface:RNStreamControlIfcResets the position of the RNG at the start of the current substream- Specified by:
resetStartSubstreamin interfaceRNStreamControlIfc
-
advanceToNextSubstream
public void advanceToNextSubstream()
Description copied from interface:RNStreamControlIfcPositions the RNG at the beginning of its next substream- Specified by:
advanceToNextSubstreamin interfaceRNStreamControlIfc
-
setAntitheticOption
public void setAntitheticOption(boolean flag)
Description copied from interface:RNStreamControlIfcTells the stream to start producing antithetic variates- Specified by:
setAntitheticOptionin interfaceRNStreamControlIfc- Parameters:
flag- true means that it produces antithetic variates.
-
getAntitheticOption
public boolean getAntitheticOption()
- Specified by:
getAntitheticOptionin interfaceRNStreamControlIfc- Returns:
- true means on
-
addObserver
public void addObserver(ObserverIfc observer)
Description copied from interface:ObservableIfcAllows the adding (attaching) of an observer to the observable- Specified by:
addObserverin interfaceObservableIfc- Parameters:
observer- the observer to attach
-
deleteObserver
public void deleteObserver(ObserverIfc observer)
Description copied from interface:ObservableIfcAllows the deletion (removing) of an observer from the observable- Specified by:
deleteObserverin interfaceObservableIfc- Parameters:
observer- the observer to delete
-
deleteObservers
public void deleteObservers()
Description copied from interface:ObservableIfcDeletes all the observers from the observable- Specified by:
deleteObserversin interfaceObservableIfc
-
contains
public boolean contains(ObserverIfc observer)
Description copied from interface:ObservableIfcReturns true if the observer is already attached- Specified by:
containsin interfaceObservableIfc- Parameters:
observer- the observer to check- Returns:
- true if attached
-
countObservers
public int countObservers()
Description copied from interface:ObservableIfcReturns how many observers are currently observing the observable- Specified by:
countObserversin interfaceObservableIfc- Returns:
- number of observers
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
asString
public java.lang.String asString()
-
-