Package microsim.statistics.regression
Class RegressionUtils
java.lang.Object
microsim.statistics.regression.RegressionUtils
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull MultiKeyCoefficientMap
bootstrap
(@NonNull MultiKeyCoefficientMap map) Method to bootstrap regression covariates.static @NonNull MultiKeyCoefficientMap
bootstrap
(@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 boolean
event
(double prob) rnd
defaults toSimulationEngine.getRnd()
.static boolean
Checks if a randomly generated value belongs to the semi-open[0, prob)
interval.static <T> T
rnd
defaults toSimulationEngine.getRnd()
,events
are passed aseventClass.getEnumConstants()
.static <T> T
rnd
defaults toSimulationEngine.getRnd()
,events
are passed aseventClass.getEnumConstants()
.static <T> T
event
(@NonNull AbstractList<T> events, @NonNull Random rnd) This allows events with types that implement the list interface.static <T> T
rnd
defaults toSimulationEngine.getRnd()
.static <T> T
rnd
defaults toSimulationEngine.getRnd()
.static <T> T
checkWeightSum
defaults to {true}.static <T> T
Transforms a map into two arrays to boost code performance and passes them further.static <T> T
event
(T @NonNull [] events, double @NonNull [] prob) rnd
defaults toSimulationEngine.getRnd()
.static <T> T
event
(T @NonNull [] events, double @NonNull [] weight, boolean checkWeightSum) rnd
defaults toSimulationEngine.getRnd()
.static <T> T
checkWeightSum
defaults totrue
.static <T> T
event
(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> T
For sampling an event where all events in the sample space have equal probability.static double
eventPiecewiseConstant
(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 double
eventPiecewiseConstant
(double @NonNull [] events, double @NonNull [] prob, Random rnd) checkSumWeights
defaults totrue
.
-
Constructor Details
-
RegressionUtils
public RegressionUtils()
-
-
Method Details
-
event
rnd
defaults toSimulationEngine.getRnd()
,events
are passed aseventClass.getEnumConstants()
.- See Also:
-
event
@NonNull public static <T> T event(@NonNull @NonNull Class<T> eventClass, double @NonNull [] weight, boolean checkWeightSum) rnd
defaults toSimulationEngine.getRnd()
,events
are passed aseventClass.getEnumConstants()
. -
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] prob) rnd
defaults toSimulationEngine.getRnd()
.- See Also:
-
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] weight, boolean checkWeightSum) rnd
defaults toSimulationEngine.getRnd()
. -
event
rnd
defaults toSimulationEngine.getRnd()
.- See Also:
-
event
rnd
defaults toSimulationEngine.getRnd()
.- See Also:
-
event
@NonNull public static <T> T event(T @NonNull [] events, double @NonNull [] prob, @NonNull @NonNull Random rnd) checkWeightSum
defaults 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) rnd
defaults 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) checkWeightSum
defaults 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) checkSumWeights
defaults 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]))
, wherex
is 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
- AMultiKeyCoefficientMap
that contains both regression coefficients and a corresponding covariance matrix. This map is required to only have one key entry in theMultiKeyCoefficientMap
map'sMultiKey
with 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
MultiKeyCoefficientMap
containing 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
- AMultiKeyCoefficientMap
that contains a set of regression coefficients. The 'coefficients' map is required to only have one key entry in each of theMultiKeyCoefficientMap
map'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
- AMultiKeyCoefficientMap
that provides the covariance matrix of a regression's coefficients. The covarianceMatrix map is required to only have one key entry in each of theMultiKeyCoefficientMap
map'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
MultiKeyCoefficientMap
of 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 typeT
of 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. TheMultiKeyCoefficientMaps
are 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
- AMultiKeyCoefficientMap
that provides the covariance matrix of a regression's coefficients. The covarianceMatrix map is required to only have one key entry in each of theMultiKeyCoefficientMap
map'sMultiKeys
, with each entry corresponding to a string with the structure [event name]_[covariate name]. So, for example, if the set of events ofT
areLowEducation
andHighEducation
, and the regression covariates declared incoefficientOutcomeMap
are 'age' and 'gender', then the followingMultiKey
entries 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 theMultiKeys
are specified does not matter. The values of theMultiKeyCoefficientMap
must 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
T
of the multinomial regression and whose values areMultiKeyCoefficientMap
with new regression coefficients (one set of coefficients for each event).
-