Package microsim.statistics.regression
Class RegressionUtils
java.lang.Object
microsim.statistics.regression.RegressionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull MultiKeyCoefficientMapbootstrap(@NonNull MultiKeyCoefficientMap map) Method to bootstrap regression covariates.static @NonNull MultiKeyCoefficientMapbootstrap(@NonNull MultiKeyCoefficientMap coefficients, @NonNull MultiKeyCoefficientMap covarianceMatrix) Method to bootstrap regression covariates.static <T> @NonNull Map<T,MultiKeyCoefficientMap> bootstrapMultinomialRegression(@NonNull Map<T, MultiKeyCoefficientMap> eventRegressionCoefficientMap, @NonNull MultiKeyCoefficientMap covarianceMatrix, @NonNull Class<T> enumType) Method to bootstrap multinomial regression covariates.static booleanevent(double prob) rnddefaults toSimulationEngine.getRnd().static booleanChecks if a randomly generated value belongs to the semi-open[0, prob)interval.static <T> Trnddefaults toSimulationEngine.getRnd(),eventsare passed aseventClass.getEnumConstants().static <T> Trnddefaults toSimulationEngine.getRnd(),eventsare passed aseventClass.getEnumConstants().static <T> Tevent(@NonNull AbstractList<T> events, @NonNull Random rnd) This allows events with types that implement the list interface.static <T> Trnddefaults toSimulationEngine.getRnd().static <T> Trnddefaults toSimulationEngine.getRnd().static <T> TcheckWeightSumdefaults to {true}.static <T> TTransforms a map into two arrays to boost code performance and passes them further.static <T> Tevent(T @NonNull [] events, double @NonNull [] prob) rnddefaults toSimulationEngine.getRnd().static <T> Tevent(T @NonNull [] events, double @NonNull [] weight, boolean checkWeightSum) rnddefaults toSimulationEngine.getRnd().static <T> TcheckWeightSumdefaults totrue.static <T> Tevent(T @NonNull [] events, double @NonNull [] weights, @NonNull Random rnd, boolean checkWeightSum) Returns an event determined randomly from a set of events and weights.static <T> TFor sampling an event where all events in the sample space have equal probability.static doubleeventPiecewiseConstant(double @NonNull [] events, double @NonNull [] weights, @NonNull Random rnd, boolean checkWeightSum) Performs a linear interpolation on the (numerical) event domain of a piecewise constant probability distribution.static doubleeventPiecewiseConstant(double @NonNull [] events, double @NonNull [] prob, Random rnd) checkSumWeightsdefaults totrue.
-
Constructor Details
-
RegressionUtils
public RegressionUtils()
-
-
Method Details
-
event
rnddefaults toSimulationEngine.getRnd(),eventsare passed aseventClass.getEnumConstants().- See Also:
-
event
@NonNull public static <T> T event(@NonNull @NonNull Class<T> eventClass, double @NonNull [] weight, boolean checkWeightSum) rnddefaults toSimulationEngine.getRnd(),eventsare passed aseventClass.getEnumConstants(). -
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] prob) rnddefaults toSimulationEngine.getRnd().- See Also:
-
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] weight, boolean checkWeightSum) rnddefaults toSimulationEngine.getRnd(). -
event
rnddefaults toSimulationEngine.getRnd().- See Also:
-
event
rnddefaults toSimulationEngine.getRnd().- See Also:
-
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] prob, @NonNull @NonNull Random rnd) checkWeightSumdefaults totrue. -
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] weights, @NonNull @NonNull Random rnd, boolean checkWeightSum) Returns an event determined randomly from a set of events and weights. Note that these weights do not necessarily have to sum to 1, as the method can convert the weights into probabilities by calculating their relative proportions. If the user desires that an exception is thrown if the weights do not sum to 1, please set checkProbSum to true.- Parameters:
events- An array of events of typeT.weights- An array of doubles from which the probabilities are calculated (by dividing the weights by the sum of weights).rnd- A random number generator.checkWeightSum- A boolean toggle, which if true means that the method will throw an exception if the weights do not add to 1. If false, the method will calculate the probabilities associated to the weights and use these in the random sampling of the event.- Returns:
- a randomly chosen event.
-
event
public static boolean event(double prob) rnddefaults toSimulationEngine.getRnd().- See Also:
-
event
Checks if a randomly generated value belongs to the semi-open[0, prob)interval.- Parameters:
prob- A probability value that must be in [0, 1] range.rnd- A random number generator.- Returns:
- a boolean that shows if a random boolean is in the [0, prob) interval.
-
event
@NonNull public static <T> T event(@NonNull @NonNull Map<T, Double> map, @NonNull @NonNull Random rnd) checkWeightSumdefaults to {true}.- See Also:
-
event
@NonNull public static <T> T event(@NonNull @NonNull Map<T, Double> map, @NonNull @NonNull Random rnd, boolean checkWeightSum) Transforms a map into two arrays to boost code performance and passes them further. -
event
For sampling an event where all events in the sample space have equal probability. The probability for each event = 1 / size of sample space.- Parameters:
events- The possible events in the sample space.rnd- A random generator.- Returns:
- the event chosen.
-
event
@NonNull public static <T> T event(@NonNull @NonNull AbstractList<T> events, @NonNull @NonNull Random rnd) This allows events with types that implement the list interface.- See Also:
-
eventPiecewiseConstant
public static double eventPiecewiseConstant(double @NonNull [] events, double @NonNull [] prob, Random rnd) checkSumWeightsdefaults totrue. -
eventPiecewiseConstant
public static double eventPiecewiseConstant(double @NonNull [] events, double @NonNull [] weights, @NonNull @NonNull Random rnd, boolean checkWeightSum) Performs a linear interpolation on the (numerical) event domain of a piecewise constant probability distribution. The effective expression is derived from(toss - (x - prob[i-1])) / (x - (x - prob[i-1])), wherexis the cumulative probability at the start ofevents[i]and(x - prob[i-1])is the cumulative probability at the start ofevents[i-1].- Parameters:
events- The discrete set of cuts characterising the domain of a piecewise constant probability distribution.weights- The discrete set of probabilities characterising a piecewise constant probability distribution.rnd- The random number generator.checkWeightSum- If true, will check weights elements sum to 1, otherwise they will be normalised (by dividing each element by the sum of the elements).- Returns:
- The value of the event drawn from a compact domain
-
bootstrap
@NonNull public static @NonNull MultiKeyCoefficientMap bootstrap(@NonNull @NonNull MultiKeyCoefficientMap map) Method to bootstrap regression covariates. This method creates a new set of regression estimates by sampling from a multivariate normal distribution with expected values (means) equal to the 'coefficients' parameter and a covariance matrix as specified.- Parameters:
map- AMultiKeyCoefficientMapthat contains both regression coefficients and a corresponding covariance matrix. This map is required to only have one key entry in theMultiKeyCoefficientMapmap'sMultiKeywith a heading 'REGRESSOR'. The map must also contain a value column with the heading 'COEFFICIENT' and an additional value column for each regressor (covariate), in order to represent the (square, symmetric, positive semi-definite) covariance matrix.- Returns:
- a
MultiKeyCoefficientMapcontaining a new set of bootstrapped regression coefficients.
-
bootstrap
@NonNull public static @NonNull MultiKeyCoefficientMap bootstrap(@NonNull @NonNull MultiKeyCoefficientMap coefficients, @NonNull @NonNull MultiKeyCoefficientMap covarianceMatrix) Method to bootstrap regression covariates. This method creates a new set of regression estimates by sampling from a multivariate normal distribution with expected values (means) equal to the 'coefficients' parameter and a covariance matrix as specified.- Parameters:
coefficients- AMultiKeyCoefficientMapthat contains a set of regression coefficients. The 'coefficients' map is required to only have one key entry in each of theMultiKeyCoefficientMapmap'sMultiKeys, with the entries being the name of the regression covariates. There must be only one values column containing the regression coefficients for each covariate (regressor).covarianceMatrix- AMultiKeyCoefficientMapthat provides the covariance matrix of a regression's coefficients. The covarianceMatrix map is required to only have one key entry in each of theMultiKeyCoefficientMapmap'sMultiKeys, with each entry corresponding to the name of a regression covariate. The values must contain a name key corresponding to each of the covariates, though the ordering of the values (columns) need not match theMultiKey(row) ordering.- Returns:
- a
MultiKeyCoefficientMapof new regression coefficients that is bootstrapped from the input estimates map.
-
bootstrapMultinomialRegression
@NonNull public static <T> @NonNull Map<T,MultiKeyCoefficientMap> bootstrapMultinomialRegression(@NonNull @NonNull Map<T, MultiKeyCoefficientMap> eventRegressionCoefficientMap, @NonNull @NonNull MultiKeyCoefficientMap covarianceMatrix, @NonNull @NonNull Class<T> enumType) Method to bootstrap multinomial regression covariates. This method creates a new map of sets of regression coefficients by sampling from a multivariate normal distribution with expected values (means) equal to the regression coefficients contained in the 'coefficientOutcomeMap' and a covariance matrix as specified.- Type Parameters:
T- The event (outcome) of a multinomial regression.- Parameters:
eventRegressionCoefficientMap- A map whose keys are the possible events (outcomes) of typeTof the multinomial regression, and whose values are MultiKeyCoefficientMaps each containing a set of regression coefficients corresponding to its event (the key). Each MultiKeyCoefficientMap of regression coefficients is used as expected value of a multivariate normal distribution, which is sampled in order to produce a new set of regression coefficients. TheMultiKeyCoefficientMapsare required to only have one key entry, with the entries being the name of the regression covariates. There must be only one values column in theMultiKeyCoefficientMaps, containing the regression coefficients for each covariate.covarianceMatrix- AMultiKeyCoefficientMapthat provides the covariance matrix of a regression's coefficients. The covarianceMatrix map is required to only have one key entry in each of theMultiKeyCoefficientMapmap'sMultiKeys, with each entry corresponding to a string with the structure [event name]_[covariate name]. So, for example, if the set of events ofTareLowEducationandHighEducation, and the regression covariates declared incoefficientOutcomeMapare 'age' and 'gender', then the followingMultiKeyentries must exist: "LowEducation_age", "LowEducation_gender", "HighEducation_age", "HighEducation_gender", such that the name of the event is a prefix, the character "_" is the 'regular expression' and the name of the regression covariate is the suffix. This prefix/suffix ordering must be preserved in order to avoid confusion between the name of events and covariates, however the order in which theMultiKeysare specified does not matter. The values of theMultiKeyCoefficientMapmust contain a name key that corresponds to each MultiKey key entry (to ensure labelling of rows and columns match), though the ordering of the values (columns) need not match theMultiKey(row) ordering.- Returns:
- a Map whose keys are the possible events (outcomes) of type
Tof the multinomial regression and whose values areMultiKeyCoefficientMapwith new regression coefficients (one set of coefficients for each event).
-