MISO2 Monte Carlo
Created on Wed Mar 9 19:33:13 2022
Monte Carlo module to return a queue of randomised values for a ODYM parameter dictionary.
@author: bgrammer
- class model.config.MISO2_monte_carlo.MISO2MonteCarlo
An object of this class is injected into a MISO2Config object for MC simulation.
The object transforms each individual ODYM Parameter values to a randomized version depending on the information given in the Uncert attribute. For this to work it needs first to transform the uncertainty information parsed by ODYM (a list of stats-array-like semicolon separated strings) into a list of distribution dictionaries for easier transformation.
Warning: Random state is at the moment meant for testing purposes only, not for production use.
- mc_state
Dictionary with parameter names as key and their Monte Carlo state as bool.
- Type:
dict
- index
Can probably be removed safely.
- mc_deque
Deque containing randomized version of parameter dicts. Will be filled automatically when needed.
- Type:
deque
- random_state
Set this value if you want reproducible values. Is repeated for all batches of mc parameter queue. Defaults to None.
- Type:
int
- get_bias(mode)
Return bias config, defaults to “none”.
- get_parameter_randomization()
Returns true if at least one parameter is set to be randomized, false otherwise.
- randomize_model_input(parameter_dict, uncertainty_settings, sample_size)
Returns a randomized version of the Parameter dictionary.
Randomization variables are taken from the Uncert variable of the Parameter object to create a Deque of sample_size randomized parameter_dicts. If the Deque already exists and contains data, a parameter_dict will be drawn from it and returned.
- Parameters:
parameter_dict (dict) –
ODYM Parameter objects
with name as keys.uncertainty_settings (dict) – Types and limits (min,max) of Parameters.
sample_size (int) – Number of sampled values that will be added to the randomization deque if it is empty.
- Returns:
Modified dict of ODYM Parameters.
- Return type:
parameter_dict_mc(dict)
- set_parameter_names(parameter_names)
Initialise Parameter names that can be randomised.
All parameters MC state is initially set to False.
- Parameters:
parameter_names (dict) – Name of parameters for regulating Monte Carlo state.
- set_random_state(random_state)
Set the random state of the Monte Carlo configuration.
Note that this will reset the existing Deque of randomized variables.
- Parameters:
random_state (int) – The seed of the random state.
- update_parameter_randomization(new_state, parameter_list=None)
Updates the randomization state for all or specified list of parameters.
This will reset the MISO2MonteCarlo objects queue of randomized dicts (if there was one).
- Parameters:
new_state (bool) – New state.
parameter_list (list) – List of parameter names.
- model.config.MISO2_monte_carlo.stats_array_to_dict(uncertainty_list, parameter_limit)
Transform a single stat array to a parametrized distribution.
The Dirichlet distribution is not parts of stat-array definition and assigned custom code 13. Distributions Min and Max value are capped by the parameter_limits for Normal and Uniform distribution to avoid potential negative values and rates in excess of 1.
- Parameters:
uncertainty_list (list) – Stats array split into list.
parameter_limit (dict) – Dictionary with Min and Max hard limit.
- Returns:
Stats array as a dict.
- Return type:
distribution(dict)
- Raises:
ValueError – if an uncertainty string with an unknown distribution is received.
- model.config.MISO2_monte_carlo.transform_stats_array_to_dicts(parameter_dict, uncertainty_settings)
The function transforms stats arrays, as parsed by the ODYM module, into dictionaries readable by the Monte Carlo object. This replaces the values from the parameters Uncert entry. If all values in the Uncert variable are None, the Uncert variable itself will be set to None.
- Parameters:
parameter_dict (dict) – Dictionary of
ODYM Parameter objects
.uncertainty_settings (dict) – Nested dict with settings of uncertainty values
Created on Wed Mar 9 19:33:13 2022
Monte Carlo object for systematic bias, in which all values are increased / decreased systematically across samples so that stocks are maximised / minimised.
@author: bgrammer
- class model.config.MISO2_monte_carlo_systematic.MISO2MonteCarloSystematic
This implementation of the MISO2MonteCarlo class will introduce a systematic bias in the randomized variables. Samples of all values of all parameters will be drawn either from a value below or above the mean, so the entirety of the input data will either be systematically over- or underestimated to maximise overall stock returns.
- mc_state
Dictionary with parameter names as key and their Monte Carlo state as bool.
- Type:
dict
- mc_deque_lower
Deque containing randomized version of parameter dicts with below mean values.
- Type:
deque
- mc_deque_higher
Deque containing randomized version of parameter dicts with higher than mean values.
- Type:
deque
- random_state
Set this value if you want reproducible values. Is repeated for all batches of mc parameter queue. Defaults to None.
- Type:
int
- get_bias(mode)
Return bias of next or last sample handed out by config.
- Parameters:
mode (string) – One of “next” or “last”
- Raises:
ValueError – If mode is unknown
- randomize_model_input(parameter_dict, uncertainty_settings, sample_size)
Returns a randomized version of the Parameter dictionary.
Randomization variables are taken from the Uncert variable of the Parameter object to create two deque of half sample_size randomized parameter_dicts, one for lower and one for higher bias. If the Deque already exists and contains data, a parameter_dict will be drawn from it and returned.
- Parameters:
parameter_dict (dict) –
ODYM Parameter objects
with name as keys.uncertainty_settings (dict) – Types and limits (min,max) of Parameters.
sample_size (int) – Number of sampled values that will be added to the randomization deque if it is empty.
- Returns:
Modified dict of ODYM Parameters.
- Return type:
parameter_dict_mc(dict)
- set_random_state(random_state)
Set the random state of the Monte Carlo configuration.
Note that this will reset the existing Deque of randomized variables.
- Parameters:
random_state (int) – The seed of the random state.
- update_parameter_randomization(new_state, parameter_list=None)
Updates the randomization state for all or specified list of parameters.
This will reset the MISO2MonteCarlo objects queue of randomized dicts (if there was one).
- Parameters:
new_state (bool) – New state.
parameter_list (list) – List of parameter names.
Created on Wed Mar 9 19:33:13 2022
Monte Carlo object for systematic bias, in which only production values are increased / decreased systematically across samples. All other parameters are randomised normally.
@author: bgrammer
- class model.config.MISO2_monte_carlo_systematic_v2.MISO2MonteCarloSystematic_v2
This implementation of the MISO2MonteCarlo class will introduce a systematic bias v2 in the randomized variables. Samples of production will be drawn exclusively form higher / lower half of distribution. All other parameters are randomised normally.
- mc_state
Dictionary with parameter names as key and their Monte Carlo state as bool.
- Type:
dict
- mc_deque_lower
Deque containing randomized version of parameter dicts with below mean values.
- Type:
deque
- mc_deque_higher
Deque containing randomized version of parameter dicts with higher than mean values.
- Type:
deque
- random_state
Set this value if you want reproducible values. Is repeated for all batches of mc parameter queue. Defaults to None.
- Type:
int
- get_bias(mode)
Return bias of stated sample handed out by config.
- Parameters:
mode (str) – Choose one of “next” or “last”.
- Raises:
ValueError – If mode is not “next” or “last”
- randomize_model_input(parameter_dict, uncertainty_settings, sample_size)
Returns a randomized version of the Parameter dictionary.
Randomization variables are taken from the Uncert variable of the Parameter object to create a Deque of sample_size randomized parameter_dicts. If the Deque already exists and contains data, a parameter_dict will be drawn from it and returned.
- Parameters:
parameter_dict (dict) –
ODYM Parameter objects
with name as keys.uncertainty_settings (dict) – Types and limits (min,max) of Parameters.
sample_size (int) – Number of sampled values that will be added to the randomization deque if it is empty.
- Returns:
Modified dict of ODYM Parameters.
- Return type:
parameter_dict_mc(dict)
- set_random_state(random_state)
Set the random state of the Monte Carlo configuration.
Note that this will reset the existing Deque of randomized variables.
- Parameters:
random_state (int) – The seed of the random state.
- update_parameter_randomization(new_state, parameter_list=None)
Updates the randomization state for all or specified list of parameters.
This will reset the MISO2MonteCarlo objects queue of randomized dicts (if there was one).
- Parameters:
new_state (bool) – New state.
parameter_list (list) – List of parameter names.
- class model.config.MISO2_sensitivity_by_parameter.MISO2SensitivityByParameter(sensitivity_settings, cv_factor=1.0)
Initializes the MISO2SensitivityByParameter class with the given sensitivity settings and coefficient of variation factor. This class is injected into a MISO2 config similar to the Monte Carlo object to randomise a dataset. It has the same interface (via duck-typing).
- Parameters:
sensitivity_settings (pandas.DataFrame) – DataFrame containing sensitivity settings.
cv_factor (float) – Coefficient of variation factor, default is 1.0.
- available_parameter_groups()
Returns the available parameter groups.
- Returns:
Keys of the uncertainty options dictionary.
- Return type:
dict_keys
- get_bias(mode)
Returns the bias of the next sample handed out by config.
- Parameters:
mode (str) – Mode for bias retrieval. Not used.
- Returns:
Returns “sensitivity_by_parameter”
- Return type:
str
- get_parameter_randomization()
Checks if parameter randomization is enabled.
- Returns:
Always returns True indicating parameter randomization is enabled.
- Return type:
bool
- randomize_model_input(parameter_dict, uncertainty_settings, sample_size)
Randomizes model input parameters based on the active randomization settings.
- Parameters:
parameter_dict (dict) – Dictionary of parameters to be randomized.
uncertainty_settings (dict) – Dictionary containing uncertainty settings.
sample_size (int) – Not used, but part of interface for other monte carlo methods.
- Returns:
A new parameter dictionary with randomized values.
- Return type:
dict
- transform_uncert_array(parameter_dict, uncertainty_settings)
The function transforms stats arrays, as parsed by the ODYM module, into dictionaries readable by the Monte Carlo object. This replaces the values from the parameters Uncert entry. If all values in the Uncert variable are None, the Uncert variable itself will be set to None.
- Parameters:
parameter_dict (dict) – Dictionary of
ODYM Parameter objects
.uncertainty_settings (dict) – Nested dict with settings of uncertainty values
- update_parameter_randomization(new_parameter, new_scenario)
Sets the next parameter group to test. The Monte Carlo object will return a modified config of the specified type.
- Parameters:
new_parameter (str) – Name of the parameter group to test next.
new_scenario (str) – One of “Low_scenario” or “High_scenario”.
- Raises:
ValueError – If new_scenario is not one of the allowed scenarios.
- model.config.MISO2_sensitivity_by_parameter.modify_parameter(parameter_name, parameter_group, test_options, parameter_values, parameter_cvs, scenario, uncertainty_settings, cv_factor)
Modifies parameter values based on the provided settings. Either the parameters are adjusted by global cv, by cv per value, or not at all if no cv is present.
- Parameters:
parameter_name (str) – Name of the parameter to modify.
parameter_group (str) – Group of the parameter.
test_options (dict) – Test options for the parameter.
parameter_values (np.ndarray) – Array of parameter values.
parameter_cvs (np.ndarray) – Coefficient of variation values.
scenario (str) – Scenario to apply (“Low_scenario” or “High_scenario”).
uncertainty_settings (dict) – Dictionary with uncertainty settings.
cv_factor (float) – Coefficient of variation factor.
- Returns:
Modified parameter values.
- Return type:
np.ndarray