Package microsim.alignment.outcome
Class ResamplingAlignment<T>
java.lang.Object
microsim.alignment.outcome.ResamplingAlignment<T>
- All Implemented Interfaces:
AlignmentUtils<T>
Aligns the population by resampling of data with (or without) corresponding weights. This involves picking an agent
from the relevant collection at random with a probability that depends on an associated weight in the case of
weighted agents or uniformly otherwise. The chosen agent then undergoes resampling of its relevant attribute (as
specified by the
AlignmentOutcomeClosure
). This process is continued until either the alignment target is
reached, or the maximum number of attempts to resample has been reached.- See Also:
-
- Matteo Richiardi, Ambra Poggi, 2014. "Imputing Individual Effects in Dynamic Microsimulation Models. An application to household formation and labour market participation in Italy," International Journal of Microsimulation, International Microsimulation Association, vol. 7(2), pages 3-39.
- Leombruni, R., Richiardi, M. LABORsim: An Agent-Based Microsimulation of Labour Supply – An Application to Italy. Comput Econ 27, 63–88 (2006).
- Implementation Requirements:
- This method is for events with binary outcomes
0, 1
only.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
align
(@NonNull Collection<T> agents, @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull AlignmentOutcomeClosure<T> closure, double targetShare) maxResamplingAttempts
defaults to-1
.void
align
(@NonNull Collection<T> agents, @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull AlignmentOutcomeClosure<T> closure, double targetShare, int maxResamplingAttempts) Align share of population by resampling.void
align
(@NonNull Collection<T> agents, @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull AlignmentOutcomeClosure<T> closure, int targetNumber) maxResamplingAttempts
defaults to-1
.void
align
(@NonNull Collection<T> agents, @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull AlignmentOutcomeClosure<T> closure, int targetNumber, int maxResamplingAttempts) Align absolute number of population by weighted resampling.void
doAlignment
(@NonNull List<T> list, @NonNull AlignmentOutcomeClosure<T> closure, double targetNumber, int maxResamplingAttempts) The main alignment method.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface microsim.alignment.AlignmentUtils
extractAgentList, validateProbabilityValue
-
Field Details
-
avgResampleAttempts
public static final int avgResampleAttemptsConstant: 20.- See Also:
-
-
Constructor Details
-
ResamplingAlignment
public ResamplingAlignment()
-
-
Method Details
-
align
public void align(@NonNull @NonNull Collection<T> agents, @Nullable @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull @NonNull AlignmentOutcomeClosure<T> closure, double targetShare) maxResamplingAttempts
defaults to-1
. -
align
public void align(@NonNull @NonNull Collection<T> agents, @Nullable @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull @NonNull AlignmentOutcomeClosure<T> closure, int targetNumber) maxResamplingAttempts
defaults to-1
. -
align
public void align(@NonNull @NonNull Collection<T> agents, @Nullable @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull @NonNull AlignmentOutcomeClosure<T> closure, double targetShare, int maxResamplingAttempts) Align share of population by resampling. This involves picking an agent from the relevant collection of agents at random with a probability that depends on an associated weight or uniformly if no weights exists. The chosen agent then undergoes resampling of its relevant attribute (as specified by theAlignmentOutcomeClosure
). This process is continued until either the alignment target is reached, or the maximum number of attempts to resample has been reached, as specified by themaxResamplingAttempts
parameter, or - if the input data is weighted - the pool of agents has been exhausted.- Parameters:
agents
- A list of agents to potentially be resampled; the agent class may implement theWeight
interface if needed by providing aWeight.getWeight()
method. In the case of the alignment algorithm,Weight.getWeight()
must return a non-negative value.filter
- Filters theagents
parameter so that only the relevant sub-population of agents is sampled, can benull
.closure
-AlignmentOutcomeClosure
that specifies how to define the outcome of the agent and how to resample it.targetShare
- The target share of the relevant subpopulation (specified as a proportion of the filtered population) for which the outcome (defined by theAlignmentOutcomeClosure
) must be true.maxResamplingAttempts
- The maximum number of attempts to resample before terminating the alignment. Introduced for situations when the resampling (as defined by theAlignmentOutcomeClosure
) is unable to alter the outcomes of enough agents, due to the nature of the subpopulation and the definition of the outcome (i.e. if agents' attributes are so far away from a binary outcome threshold boundary, that the probability of enough of them switching to the desired outcome is vanishingly small). If lower than the size of the subset of the population whose outcomes need changing (or negative) the number is automatically enlarged to the default value of 20 times the size of the subset of the population to be resampled in order to move the delta between the simulation and the target towards 0. Therefore, in order to improve this delta, a member of the population undergoing alignment will be resampled up to a maximum of 20 times on average in order to change their outcome, before the alignment algorithm will give up and terminate.- Throws:
IllegalArgumentException
- whentargetShare
is out of[0,1]
range.NullPointerException
- whenagents
, orclosure
, or both arenull
.- Implementation Requirements:
targetNumber
is rounded down for the sake of consistency.
-
align
public void align(@NonNull @NonNull Collection<T> agents, @Nullable @Nullable org.apache.commons.collections4.Predicate<T> filter, @NonNull @NonNull AlignmentOutcomeClosure<T> closure, int targetNumber, int maxResamplingAttempts) Align absolute number of population by weighted resampling. This involves picking an agent from the relevant collection of agents at random with a probability that depends on an associated weight. The chosen agent then undergoes resampling of its relevant attribute (as specified by theAlignmentOutcomeClosure
). This process is continued until either the alignment target is reached, or the maximum number of attempts to resample has been reached, as specified by themaxResamplingAttempts
parameter.- Parameters:
agents
- A list of agents to potentially be resampled; the agent class must implement theWeight
interface by providing a getWeight() method. In the case of the alignment algorithm, getWeight() must return a positive value.filter
- Filters theagents
so that only the relevant subpopulation of agents is sampled, can be null.closure
-AlignmentOutcomeClosure
that specifies how to define the outcome of the agent and how to resample it.targetNumber
- The target number of the filtered population for which the outcome (defined by theAlignmentOutcomeClosure
) must be true.maxResamplingAttempts
- The maximum number of attempts to resample before terminating the alignment. Introduced for situations when the resampling (as defined by theAlignmentOutcomeClosure
) is unable to alter the outcomes of enough agents, due to the nature of the subpopulation and the definition of the outcome (i.e. if agents' attributes are so far away from a binary outcome threshold boundary, that the probability of enough of them switching to the desired outcome is vanishingly small). If lower than the size of the subset of the population whose outcomes need changing (or negative) the number is automatically enlarged to the default value of 20 times the size of the subset of the population to be resampled in order to move the delta between the simulation and the target towards 0. Therefore, in order to improve this delta, a member of the population undergoing alignment will be resampled up to a maximum of 20 times on average in order to change their outcome, before the alignment algorithm will give up and terminate.- Throws:
IllegalArgumentException
- when targetNumber is negative or exceeds the population size.NullPointerException
- whenagents
, orclosure
, or both arenull
.
-
doAlignment
public void doAlignment(@NonNull @NonNull List<T> list, @NonNull @NonNull AlignmentOutcomeClosure<T> closure, double targetNumber, int maxResamplingAttempts) The main alignment method. Calculates the difference between the target value and the actual sum of all positive (weighted) outcomes. Depending on the sign of this delta, resamples the agent (tries to change the outcome value).- Parameters:
list
- A list of agents.closure
- A closure object to update the state of an agent.targetNumber
- The target sum of positive outcomes.maxResamplingAttempts
- The total number of resampling attempts, if reached, the method picks randomly another agent.- Throws:
NullPointerException
- whenlist
, orclosure
. or both arenull
.- Implementation Requirements:
- The list argument here is already filtered for the relevant agents in a corresponding
align
method., Consistent delta comparison for all cases: compare to zero every time., Consistent values of initial delta for given target numbers and equivalent target shares. Say, when there are 12 positive outcomes and the target number is 9, the equivalent target share must be 0.75 and - provided the rng seed stays the same - results must be identical., Resampling number adjustment is different for different signs of delta.
-