Package jslx.statistics
Class Bootstrap
- java.lang.Object
-
- jslx.statistics.Bootstrap
-
- All Implemented Interfaces:
GetNameIfc,IdentityIfc,GetRandomNumberStreamIfc,RNStreamControlIfc,SetRandomNumberStreamIfc
public class Bootstrap extends java.lang.Object implements IdentityIfc, RNStreamControlIfc, SetRandomNumberStreamIfc, GetRandomNumberStreamIfc
A class to do statistical bootstrapping. The calculations occur via the method generateSamples(). Until generateSamples() is called the results are meaningless. It is possible to save the individual bootstrap samples from which the bootstrap samples can be retrieved. Recognize that this could be a lot of data. The class implements three classic bootstrap confidence intervals normal, basic, and percentile. To estimate the quantiles it uses algorithm 8 from Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, American Statistician 50, 361–365 as the default. This can be changed by the user.
-
-
Field Summary
Fields Modifier and Type Field Description protected StatisticmyAcrossBSStatprotected java.util.List<Statistic>myBSStatListprotected doublemyDefaultLevelprotected intmyIdThe id of this objectprotected java.lang.StringmyNameprotected intmyNumBSSamplesprotected doublemyOrgEstimateprotected double[]myOrginalDataprotected DPopulationmyOriginalPopprotected StatisticmyOriginalPopStatprotected org.apache.commons.math3.stat.descriptive.rank.PercentilemyPercentileCalcprotected org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationTypemyQuantileType
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvanceToNextSubstream()Positions the RNG at the beginning of its next substreamjava.lang.StringasString()static Bootstrapcreate(int sampleSize, SampleIfc sampler)static Bootstrapcreate(java.lang.String name, int sampleSize, SampleIfc sampler)voidgenerateSamples(int numBootstrapSamples)The individual bootstrapped samples are not saved.voidgenerateSamples(int numBootstrapSamples, boolean saveBootstrapSamples)The estimator is EstimatorIfc.Average()voidgenerateSamples(int numBootstrapSamples, EstimatorIfc estimator)The individual bootstrapped samples are not saved.voidgenerateSamples(int numBootstrapSamples, EstimatorIfc estimator, boolean saveBootstrapSamples)This method changes the underlying state of the Bootstrap instance by performing the bootstrap sampling.doublegetAcrossBootstrapAverage()StatisticgetAcrossBootstrapStatistics()booleangetAntitheticOption()IntervalgetBasicBootstrapCI()The "basic" method, but with no bias correction.IntervalgetBasicBootstrapCI(double level)The "basic" method, but with no bias correction.doublegetBootstrapBiasEstimate()This is getAcrossBootstrapAverage() - getOriginalDataEstimate()double[]getBootstrapDifferences()Each element is the bootstrap estimate for sample i minus getOriginalDataEstimate()double[]getBootstrapEstimates()double[]getBootstrapSampleAverages()If the bootstrap samples were saved, this returns the generated averages for each of the samplesdouble[]getBootstrapSampleVariances()If the bootstrap samples were saved, this returns the generated variance for each of the samplesdoublegetBootstrapStdErrEstimate()This is the standard deviation of the across bootstrap observations of the estimator for each bootstrap generatedouble[]getDataForBootstrapSample(int b)java.util.List<double[]>getDataForEachBootstrapSample()If the save bootstrap data option was not turned on during the sampling then the list returned is empty.doublegetDefaultCILevel()java.util.List<RVariableIfc>getEmpiricalRVForEachBootstrapSample()Creates a random variable to represent the data in each bootstrap generate for which the data was saved.java.util.List<RVariableIfc>getEmpiricalRVForEachBootstrapSample(boolean useCRN)Creates a random variable to represent the data in each bootstrap generate for which the data was saved.intgetId()java.lang.StringgetName()intgetNumBootstrapSamples()double[]getOriginalData()doublegetOriginalDataAverage()doublegetOriginalDataEstimate()StatisticgetOriginalDataStatistics()IntervalgetPercentileBootstrapCI()The "percentile" method, but with no bias correction.IntervalgetPercentileBootstrapCI(double level)The "percentile" method, but with no bias correction.RNStreamIfcgetRandomNumberStream()double[]getStandardizedBootstrapDifferences()Each element is the bootstrap estimate for sample i minus getOriginalDataEstimate() divided by getBootstrapStdErrEstimate()java.util.List<Statistic>getStatisticForEachBootstrapSample()Each returned statistic has the data saved.IntervalgetStdNormalBootstrapCI()Gets the standard normal based bootstrap confidence interval.IntervalgetStdNormalBootstrapCI(double level)Gets the standard normal based bootstrap confidence interval.voidresetStartStream()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 substreamvoidsetAntitheticOption(boolean flag)Tells the stream to start producing antithetic variatesvoidsetDefaultCILevel(double level)voidsetDefaultQuantileEstimationType(org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationType type)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
-
myId
protected final int myId
The id of this object
-
myName
protected final java.lang.String myName
-
myOriginalPop
protected final DPopulation myOriginalPop
-
myAcrossBSStat
protected final Statistic myAcrossBSStat
-
myBSStatList
protected final java.util.List<Statistic> myBSStatList
-
myOriginalPopStat
protected final Statistic myOriginalPopStat
-
myPercentileCalc
protected final org.apache.commons.math3.stat.descriptive.rank.Percentile myPercentileCalc
-
myOrginalData
protected final double[] myOrginalData
-
myNumBSSamples
protected int myNumBSSamples
-
myOrgEstimate
protected double myOrgEstimate
-
myQuantileType
protected org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationType myQuantileType
-
myDefaultLevel
protected double myDefaultLevel
-
-
Constructor Detail
-
Bootstrap
public Bootstrap(double[] originalData)
Creates a Bootstrap instance with name Bootstrap:getId()- Parameters:
originalData- the original data
-
Bootstrap
public Bootstrap(java.lang.String name, double[] originalData)- Parameters:
name- the name of bootstrap instanceoriginalData- the original data
-
-
Method Detail
-
create
public static Bootstrap create(int sampleSize, SampleIfc sampler)
- Parameters:
sampleSize- the size of the original generatesampler- something to generate the original generate of the provided size- Returns:
- an instance of Bootstrap based on the generate
-
create
public static Bootstrap create(java.lang.String name, int sampleSize, SampleIfc sampler)
- Parameters:
name- the name of bootstrap instancesampleSize- the size of the original generate, must be greater than 1sampler- something to generate the original generate of the provided size- Returns:
- an instance of Bootstrap based on the generate
-
generateSamples
public final void generateSamples(int numBootstrapSamples)
The individual bootstrapped samples are not saved. The estimator is EstimatorIfc.Average()- Parameters:
numBootstrapSamples- the number of bootstrap samples to generate, must be greater than 1
-
generateSamples
public final void generateSamples(int numBootstrapSamples, boolean saveBootstrapSamples)The estimator is EstimatorIfc.Average()- Parameters:
numBootstrapSamples- the number of bootstrap samples to generatesaveBootstrapSamples- indicates that the statistics and data of each bootstrap generate should be saved
-
generateSamples
public final void generateSamples(int numBootstrapSamples, EstimatorIfc estimator)The individual bootstrapped samples are not saved.- Parameters:
numBootstrapSamples- the number of bootstrap samples to generateestimator- a function of the data
-
generateSamples
public void generateSamples(int numBootstrapSamples, EstimatorIfc estimator, boolean saveBootstrapSamples)This method changes the underlying state of the Bootstrap instance by performing the bootstrap sampling.- Parameters:
numBootstrapSamples- the number of bootstrap samples to generateestimator- a function of the datasaveBootstrapSamples- indicates that the statistics and data of each bootstrap generate should be saved
-
getId
public final int getId()
- Specified by:
getIdin interfaceIdentityIfc- Returns:
- the identity is unique to this execution/construction
-
getName
public final java.lang.String getName()
- Specified by:
getNamein interfaceGetNameIfc- Returns:
- the name of the bootstrap
-
resetStartStream
public void resetStartStream()
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:
resetStartStreamin interfaceRNStreamControlIfc
-
resetStartSubstream
public void resetStartSubstream()
Resets the position of the RNG at the start of the current substream- Specified by:
resetStartSubstreamin interfaceRNStreamControlIfc
-
advanceToNextSubstream
public void advanceToNextSubstream()
Positions the RNG at the beginning of its next substream- Specified by:
advanceToNextSubstreamin interfaceRNStreamControlIfc
-
setAntitheticOption
public void setAntitheticOption(boolean flag)
Tells 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
-
setRandomNumberStream
public 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
-
getRandomNumberStream
public RNStreamIfc getRandomNumberStream()
- Specified by:
getRandomNumberStreamin interfaceGetRandomNumberStreamIfc- Returns:
- the random number stream reference
-
getDefaultCILevel
public final double getDefaultCILevel()
- Returns:
- the default confidence interval level
-
setDefaultCILevel
public final void setDefaultCILevel(double level)
- Parameters:
level- the level to set must be (0,1)
-
setDefaultQuantileEstimationType
public final void setDefaultQuantileEstimationType(org.apache.commons.math3.stat.descriptive.rank.Percentile.EstimationType type)
- Parameters:
type- the type to set, must not be null.
-
getNumBootstrapSamples
public final int getNumBootstrapSamples()
- Returns:
- the number of requested bootstrap samples
-
getOriginalData
public final double[] getOriginalData()
- Returns:
- a copy of the original data
-
getStatisticForEachBootstrapSample
public final java.util.List<Statistic> getStatisticForEachBootstrapSample()
Each returned statistic has the data saved. The list itself is unmodifiable. The underlying statistic objects can be modified, but have no effect on the bootstrap generate statistics. The statistical values will be changed the next time generateSamples() is executed. Users are advised to copy the statistics in the list (via Statistic newInstance()) before executing generateSamples if persistence is required. If the save bootstrap data option was not turned on during the sampling then the list returned is empty.- Returns:
- a list of size getNumBootstrapSamples() holding statistics and data from every bootstrap generate
-
getDataForEachBootstrapSample
public final java.util.List<double[]> getDataForEachBootstrapSample()
If the save bootstrap data option was not turned on during the sampling then the list returned is empty.- Returns:
- a list of size getNumBootstrapSamples() holding a copy of the data from every bootstrap generate
-
getEmpiricalRVForEachBootstrapSample
public final java.util.List<RVariableIfc> getEmpiricalRVForEachBootstrapSample()
Creates a random variable to represent the data in each bootstrap generate for which the data was saved. By default, the stream for every random variable is the same across the bootstraps to facilitate common random number generation (CRN)- Returns:
- a list of the random variables
-
getEmpiricalRVForEachBootstrapSample
public final java.util.List<RVariableIfc> getEmpiricalRVForEachBootstrapSample(boolean useCRN)
Creates a random variable to represent the data in each bootstrap generate for which the data was saved.- Parameters:
useCRN- , if true the stream for every random variable is the same across the bootstraps to facilitate common random number generation (CRN). If false different streams are used for each created random variable- Returns:
- a list of the random variables
-
getDataForBootstrapSample
public final double[] getDataForBootstrapSample(int b)
- Parameters:
b- the bootstrap generate number, b = 1, 2, ... to getNumBootstrapSamples()- Returns:
- the generated values for the bth bootstrap, if no samples are saved then the array returned is of zero length
-
getBootstrapSampleAverages
public final double[] getBootstrapSampleAverages()
If the bootstrap samples were saved, this returns the generated averages for each of the samples- Returns:
- an array of the bootstrap generate averages, will be zero length if no bootstrap samples were saved
-
getBootstrapSampleVariances
public final double[] getBootstrapSampleVariances()
If the bootstrap samples were saved, this returns the generated variance for each of the samples- Returns:
- an array of the bootstrap generated variances, will be zero length if no bootstrap samples were saved
-
getAcrossBootstrapStatistics
public final Statistic getAcrossBootstrapStatistics()
- Returns:
- a Statistic observed over estimates from the bootstrap samples
-
getAcrossBootstrapAverage
public final double getAcrossBootstrapAverage()
- Returns:
- the generated average of the estimates from the bootstrap samples
-
getBootstrapEstimates
public final double[] getBootstrapEstimates()
- Returns:
- the observations of the estimator for each bootstrap generate, may be zero length if no samples have been generated
-
getBootstrapDifferences
public final double[] getBootstrapDifferences()
Each element is the bootstrap estimate for sample i minus getOriginalDataEstimate()- Returns:
- the array of bootstrap differences
-
getStandardizedBootstrapDifferences
public final double[] getStandardizedBootstrapDifferences()
Each element is the bootstrap estimate for sample i minus getOriginalDataEstimate() divided by getBootstrapStdErrEstimate()- Returns:
- the array of bootstrap differences
-
getOriginalDataAverage
public final double getOriginalDataAverage()
- Returns:
- the generate average for the original data
-
getOriginalDataEstimate
public final double getOriginalDataEstimate()
- Returns:
- the estimate from the supplied EstimatorIfc based on the original data
-
getBootstrapBiasEstimate
public final double getBootstrapBiasEstimate()
This is getAcrossBootstrapAverage() - getOriginalDataEstimate()- Returns:
- an estimate the bias based on bootstrapping
-
getBootstrapStdErrEstimate
public final double getBootstrapStdErrEstimate()
This is the standard deviation of the across bootstrap observations of the estimator for each bootstrap generate- Returns:
- the standard error of the estimate based on bootstrapping
-
getOriginalDataStatistics
public final Statistic getOriginalDataStatistics()
- Returns:
- summary statistics for the original data
-
getStdNormalBootstrapCI
public final Interval getStdNormalBootstrapCI()
Gets the standard normal based bootstrap confidence interval. Not recommended.- Returns:
- the confidence interval
-
getStdNormalBootstrapCI
public final Interval getStdNormalBootstrapCI(double level)
Gets the standard normal based bootstrap confidence interval. Not recommended.- Parameters:
level- the confidence level, must be between 0 and 1- Returns:
- the confidence interval
-
getBasicBootstrapCI
public final Interval getBasicBootstrapCI()
The "basic" method, but with no bias correction. This is the so called centered percentile method (2θ − Bu , 2θ − Bl ) where θ is the bootstrap estimator and Bu is the 1 - alpha/2 percentile and Bl is the lower (alpha/2) percentile, where level = 1-alpha of the bootstrap replicates.- Returns:
- the confidence interval
-
getBasicBootstrapCI
public final Interval getBasicBootstrapCI(double level)
The "basic" method, but with no bias correction. This is the so called centered percentile method (2θ − Bu , 2θ − Bl ) where θ is the bootstrap estimator and Bu is the 1 - alpha/2 percentile and Bl is the lower (alpha/2) percentile, where level = 1-alpha of the bootstrap replicates.- Parameters:
level- the confidence level, must be between 0 and 1- Returns:
- the confidence interval
-
getPercentileBootstrapCI
public final Interval getPercentileBootstrapCI()
The "percentile" method, but with no bias correction. This is the percentile method (θ − Bl , θ + Bu ) where θ is the bootstrap estimator and Bu is the 1 - alpha/2 percentile and Bl is the lower (alpha/2) percentile, where level = 1-alpha of the bootstrap replicates- Returns:
- the confidence interval
-
getPercentileBootstrapCI
public final Interval getPercentileBootstrapCI(double level)
The "percentile" method, but with no bias correction. This is the percentile method (Bl , Bu ) where Bu is the 1 - alpha/2 percentile and Bl is the lower (alpha/2) percentile, where level = 1-alpha of the bootstrap replicates- Parameters:
level- the confidence level, must be between 0 and 1- Returns:
- the confidence interval
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
asString
public java.lang.String asString()
-
-