Package jsl.utilities.random.mcmc
Class MetropolisHastings1D
- java.lang.Object
-
- jsl.utilities.random.mcmc.MetropolisHastings1D
-
- All Implemented Interfaces:
ObservableIfc
,GetValueIfc
,RandomIfc
,GetRandomNumberStreamIfc
,RNStreamControlIfc
,SetRandomNumberStreamIfc
,SampleIfc
public class MetropolisHastings1D extends java.lang.Object implements RandomIfc, ObservableIfc
An implementation for a 1-Dimensional Metropolis Hasting process. The process is observable at each step
-
-
Field Summary
Fields Modifier and Type Field Description protected Statistic
myAcceptanceStat
protected boolean
myBurnInFlag
protected double
myCurrentX
protected double
myFofCurrentX
protected double
myFofProposedY
protected boolean
myInitializedFlag
protected double
myInitialX
protected double
myLastAcceptanceProbability
protected ObservableComponent
myObservableComponent
protected Statistic
myObservedStat
protected double
myPrevX
protected ProposalFunction1DIfc
myProposalFun
protected double
myProposedY
protected RNStreamIfc
myStream
myRNStream provides a reference to the underlying stream of random numbersprotected FunctionIfc
myTargetFun
-
Constructor Summary
Constructors Constructor Description MetropolisHastings1D(double initialX, FunctionIfc targetFun, ProposalFunction1DIfc proposalFun)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected double
acceptanceFunction(double currentX, double proposedY)
Computes the acceptance function for each stepvoid
addObserver(ObserverIfc observer)
Allows the adding (attaching) of an observer to the observablevoid
advanceToNextSubstream()
Positions the RNG at the beginning of its next substreamjava.lang.String
asString()
boolean
contains(ObserverIfc observer)
Returns true if the observer is already attachedint
countObservers()
Returns how many observers are currently observing the observablestatic MetropolisHastings1D
create(double initialX, int burnInAmount, FunctionIfc targetFun, ProposalFunction1DIfc proposalFun)
void
deleteObserver(ObserverIfc observer)
Allows the deletion (removing) of an observer from the observablevoid
deleteObservers()
Deletes all the observers from the observableStatistic
getAcceptanceStatistics()
boolean
getAntitheticOption()
double
getCurrentX()
double
getFofCurrentX()
double
getFofProposedY()
protected double
getFunctionRatio(double currentX, double proposedY)
double
getInitialX()
double
getLastAcceptanceProbability()
Statistic
getObservedStatistics()
double
getPrevX()
double
getProposedY()
RNStreamIfc
getRandomNumberStream()
double
getValue()
This method simply returns the value.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).boolean
isInitialized()
boolean
isWarmedUp()
double
next()
Moves the process one stepvoid
resetStartStream()
The resetStartStream method will position the RNG at the beginning of its stream.void
resetStartSubstream()
Resets the position of the RNG at the start of the current substreamvoid
resetStatistics()
Resets the automatically collected statisticsdouble
runAll(int n)
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.double
sample()
void
setAntitheticOption(boolean flag)
Tells the stream to start producing antithetic variatesvoid
setInitialX(double initialX)
void
setRandomNumberStream(RNStreamIfc stream)
Sets the underlying random number streamjava.lang.String
toString()
-
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 FunctionIfc myTargetFun
-
myProposalFun
protected final ProposalFunction1DIfc myProposalFun
-
myAcceptanceStat
protected final Statistic myAcceptanceStat
-
myObservedStat
protected final Statistic myObservedStat
-
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
-
MetropolisHastings1D
public MetropolisHastings1D(double initialX, FunctionIfc targetFun, ProposalFunction1DIfc proposalFun)
- Parameters:
initialX
- the initial value to start generation processtargetFun
- the target functionproposalFun
- the proposal function
-
-
Method Detail
-
create
public static MetropolisHastings1D create(double initialX, int burnInAmount, FunctionIfc targetFun, ProposalFunction1DIfc 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- Returns:
- the created instance
-
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 of sampling for the burn-in (warm up) period
-
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)
-
getAcceptanceStatistics
public Statistic getAcceptanceStatistics()
- Returns:
- statistics for the proportion of the proposed state (y) that are accepted
-
getObservedStatistics
public Statistic getObservedStatistics()
- 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)
-
getValue
public final double getValue()
Description copied from interface:GetValueIfc
This method simply returns the value.- Specified by:
getValue
in interfaceGetValueIfc
- Returns:
- The value.
-
sample
public final double sample()
-
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:
getRandomNumberStream
in interfaceGetRandomNumberStreamIfc
- Returns:
- the random number stream reference
-
setRandomNumberStream
public final void setRandomNumberStream(RNStreamIfc stream)
Description copied from interface:SetRandomNumberStreamIfc
Sets the underlying random number stream- Specified by:
setRandomNumberStream
in interfaceSetRandomNumberStreamIfc
- Parameters:
stream
- the reference to the random number stream, must not be null
-
resetStartStream
public void resetStartStream()
Description copied from interface:RNStreamControlIfc
The 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:
resetStartStream
in interfaceRNStreamControlIfc
-
resetStartSubstream
public void resetStartSubstream()
Description copied from interface:RNStreamControlIfc
Resets the position of the RNG at the start of the current substream- Specified by:
resetStartSubstream
in interfaceRNStreamControlIfc
-
advanceToNextSubstream
public void advanceToNextSubstream()
Description copied from interface:RNStreamControlIfc
Positions the RNG at the beginning of its next substream- Specified by:
advanceToNextSubstream
in interfaceRNStreamControlIfc
-
setAntitheticOption
public void setAntitheticOption(boolean flag)
Description copied from interface:RNStreamControlIfc
Tells the stream to start producing antithetic variates- Specified by:
setAntitheticOption
in interfaceRNStreamControlIfc
- Parameters:
flag
- true means that it produces antithetic variates.
-
getAntitheticOption
public boolean getAntitheticOption()
- Specified by:
getAntitheticOption
in interfaceRNStreamControlIfc
- Returns:
- true means on
-
addObserver
public void addObserver(ObserverIfc observer)
Description copied from interface:ObservableIfc
Allows the adding (attaching) of an observer to the observable- Specified by:
addObserver
in interfaceObservableIfc
- Parameters:
observer
- the observer to attach
-
deleteObserver
public void deleteObserver(ObserverIfc observer)
Description copied from interface:ObservableIfc
Allows the deletion (removing) of an observer from the observable- Specified by:
deleteObserver
in interfaceObservableIfc
- Parameters:
observer
- the observer to delete
-
deleteObservers
public void deleteObservers()
Description copied from interface:ObservableIfc
Deletes all the observers from the observable- Specified by:
deleteObservers
in interfaceObservableIfc
-
contains
public boolean contains(ObserverIfc observer)
Description copied from interface:ObservableIfc
Returns true if the observer is already attached- Specified by:
contains
in interfaceObservableIfc
- Parameters:
observer
- the observer to check- Returns:
- true if attached
-
countObservers
public int countObservers()
Description copied from interface:ObservableIfc
Returns how many observers are currently observing the observable- Specified by:
countObservers
in interfaceObservableIfc
- Returns:
- number of observers
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
asString
public java.lang.String asString()
-
-