Fumagalli_Motta_Tarantino_2020.Configurations.FindConfig
1import random 2from numpy import arange 3from typing import Callable 4 5import Fumagalli_Motta_Tarantino_2020.Configurations.StoreConfig as StoreConfig 6import Fumagalli_Motta_Tarantino_2020.Models.Base as Base 7import Fumagalli_Motta_Tarantino_2020.Models.Types as Types 8import Fumagalli_Motta_Tarantino_2020.Models.Distributions as Distributions 9 10 11class ParameterModelGenerator: 12 """ 13 Generates a random set of parameters for Fumagalli_Motta_Tarantino_2020.Models.Base.CoreModel. 14 """ 15 16 def __init__( 17 self, 18 lower=0.01, 19 upper=0.99, 20 step=0.01, 21 ): 22 """ 23 Sets the list of choices to draw the set of parameters from. 24 25 Parameters 26 ---------- 27 lower: float 28 Lower thresholds of the range of choices. 29 upper: float 30 Upper thresholds of the range of choices. 31 step: float 32 Step between to choices in the range. 33 """ 34 self.lower = lower 35 self.upper = upper 36 self.step = step 37 38 def get_parameter_model(self, **kwargs) -> StoreConfig.ParameterModel: 39 """ 40 Draws a random set of parameters. 41 42 Parameters 43 ---------- 44 **kwargs 45 Constant inputs for the parameter model. 46 47 Returns 48 ------- 49 StoreConfig.ParameterModel 50 ParameterModel containing the set of parameters. 51 """ 52 choices = list(arange(self.lower, self.upper, self.step)) 53 choice = random.choices(choices, k=11) 54 choice = [round(i, ndigits=2) for i in choice] 55 return StoreConfig.ParameterModel( 56 merger_policy=kwargs.get("merger_policy", Types.MergerPolicies.Strict), 57 development_costs=kwargs.get("development_costs", choice[0]), 58 startup_assets=kwargs.get("startup_assets", choice[1]), 59 success_probability=kwargs.get("success_probability", choice[2]), 60 development_success=kwargs.get("development_success", True), 61 private_benefit=kwargs.get("private_benefit", choice[3]), 62 consumer_surplus_without_innovation=kwargs.get( 63 "consumer_surplus_without_innovation", choice[4] 64 ), 65 incumbent_profit_without_innovation=kwargs.get( 66 "incumbent_profit_without_innovation", choice[5] 67 ), 68 consumer_surplus_duopoly=kwargs.get("consumer_surplus_duopoly", choice[6]), 69 incumbent_profit_duopoly=kwargs.get("incumbent_profit_duopoly", choice[7]), 70 startup_profit_duopoly=kwargs.get("startup_profit_duopoly", choice[8]), 71 consumer_surplus_with_innovation=kwargs.get( 72 "consumer_surplus_with_innovation", choice[9] 73 ), 74 incumbent_profit_with_innovation=kwargs.get( 75 "incumbent_profit_with_innovation", choice[10] 76 ), 77 ) 78 79 80class RandomConfig: 81 """ 82 Finds a random configuration satisfying specific requirements. 83 84 Example 85 -------- 86 ``` 87 import Fumagalli_Motta_Tarantino_2020 as FMT20 88 89 # search for a random configuration -> this can take a while 90 config = FMT20.RandomConfig(laissez_faire_optimal=True).find_config() 91 model = FMT20.OptimalMergerPolicy(**config()) 92 print(model.is_laissez_faire_optimal()) 93 ``` 94 """ 95 96 def __init__( 97 self, 98 parameter_generator=ParameterModelGenerator(), 99 model_type: Callable = Base.OptimalMergerPolicy, 100 callable_condition: Callable = None, 101 strict_optimal=None, 102 intermediate_optimal=None, 103 laissez_faire_optimal=None, 104 is_killer_acquisition=None, 105 **kwargs 106 ): 107 """ 108 Specify the requirements to satisfy in the model to find. 109 110 Parameters 111 ---------- 112 parameter_generator: ParameterModelGenerator 113 Generator for random sets of parameters 114 model_type: Callable 115 Type of the model to find the parameters for (conforms to Base.OptimalMergerPolicy). 116 callable_condition: Callable 117 Lambda with model as input and boolean as output (True equals satisfied requirement). 118 strict_optimal: Optional[bool] 119 If True, the model has a strict policy as optimal policy. 120 intermediate_optimal: Optional[bool] 121 If True, the model has an intermediate policy as optimal policy. 122 laissez_faire_optimal: Optional[bool] 123 If True, the model has a laissez-faire policy as optimal policy. 124 is_killer_acquisition: Optional[bool] 125 If True, the model facilitates a killer acquisition. 126 **kwargs 127 Constant inputs for the parameter model. 128 """ 129 self.parameter_generator = parameter_generator 130 self._model_type = model_type 131 self.strict_optimal = strict_optimal 132 self.intermediate_optimal = intermediate_optimal 133 self.laissez_faire_optimal = laissez_faire_optimal 134 self.is_killer_acquisition = is_killer_acquisition 135 self.callable_condition = callable_condition 136 self._kwargs = kwargs 137 138 def _check_conditions(self, model: Base.OptimalMergerPolicy) -> bool: 139 if not self._check_condition(model.is_strict_optimal(), self.strict_optimal): 140 return False 141 if not self._check_condition( 142 model.is_intermediate_optimal(), self.intermediate_optimal 143 ): 144 return False 145 if not self._check_condition( 146 model.is_laissez_faire_optimal(), self.laissez_faire_optimal 147 ): 148 return False 149 if not self._check_condition( 150 model.is_killer_acquisition(), self.is_killer_acquisition 151 ): 152 return False 153 if not self._check_callable_condition(self.callable_condition, model): 154 return False 155 return True 156 157 @staticmethod 158 def _check_callable_condition(condition: Callable, model: Base.OptimalMergerPolicy): 159 if condition is not None: 160 return condition(model) 161 return True 162 163 @staticmethod 164 def _check_condition(value: bool, check: bool) -> bool: 165 if check is not None: 166 if value == check: 167 return True 168 else: 169 return False 170 return True 171 172 def find_config(self) -> StoreConfig.ParameterModel: 173 """ 174 Triggers the process of finding a set of parameters satisfying the requirements. 175 176 Note: This can take a while, depending on the given conditions (or sometimes it is not possible to find a set 177 of parameters). 178 179 Returns 180 ------- 181 StoreConfig.ParameterModel 182 ParameterModel containing the set of parameters. 183 """ 184 while True: 185 try: 186 parameter_model: StoreConfig.ParameterModel = ( 187 self.parameter_generator.get_parameter_model(**self._kwargs) 188 ) 189 model = self._get_model(parameter_model) 190 if self._check_conditions(model): 191 return parameter_model 192 else: 193 pass 194 except AssertionError: 195 pass 196 197 def _get_model(self, parameter_model): 198 model = self._model_type( 199 **parameter_model(), 200 asset_distribution=self._kwargs.get( 201 "asset_distribution", Distributions.UniformDistribution 202 ), 203 ) 204 return model
class
ParameterModelGenerator:
12class ParameterModelGenerator: 13 """ 14 Generates a random set of parameters for Fumagalli_Motta_Tarantino_2020.Models.Base.CoreModel. 15 """ 16 17 def __init__( 18 self, 19 lower=0.01, 20 upper=0.99, 21 step=0.01, 22 ): 23 """ 24 Sets the list of choices to draw the set of parameters from. 25 26 Parameters 27 ---------- 28 lower: float 29 Lower thresholds of the range of choices. 30 upper: float 31 Upper thresholds of the range of choices. 32 step: float 33 Step between to choices in the range. 34 """ 35 self.lower = lower 36 self.upper = upper 37 self.step = step 38 39 def get_parameter_model(self, **kwargs) -> StoreConfig.ParameterModel: 40 """ 41 Draws a random set of parameters. 42 43 Parameters 44 ---------- 45 **kwargs 46 Constant inputs for the parameter model. 47 48 Returns 49 ------- 50 StoreConfig.ParameterModel 51 ParameterModel containing the set of parameters. 52 """ 53 choices = list(arange(self.lower, self.upper, self.step)) 54 choice = random.choices(choices, k=11) 55 choice = [round(i, ndigits=2) for i in choice] 56 return StoreConfig.ParameterModel( 57 merger_policy=kwargs.get("merger_policy", Types.MergerPolicies.Strict), 58 development_costs=kwargs.get("development_costs", choice[0]), 59 startup_assets=kwargs.get("startup_assets", choice[1]), 60 success_probability=kwargs.get("success_probability", choice[2]), 61 development_success=kwargs.get("development_success", True), 62 private_benefit=kwargs.get("private_benefit", choice[3]), 63 consumer_surplus_without_innovation=kwargs.get( 64 "consumer_surplus_without_innovation", choice[4] 65 ), 66 incumbent_profit_without_innovation=kwargs.get( 67 "incumbent_profit_without_innovation", choice[5] 68 ), 69 consumer_surplus_duopoly=kwargs.get("consumer_surplus_duopoly", choice[6]), 70 incumbent_profit_duopoly=kwargs.get("incumbent_profit_duopoly", choice[7]), 71 startup_profit_duopoly=kwargs.get("startup_profit_duopoly", choice[8]), 72 consumer_surplus_with_innovation=kwargs.get( 73 "consumer_surplus_with_innovation", choice[9] 74 ), 75 incumbent_profit_with_innovation=kwargs.get( 76 "incumbent_profit_with_innovation", choice[10] 77 ), 78 )
Generates a random set of parameters for Fumagalli_Motta_Tarantino_2020.Models.Base.CoreModel.
ParameterModelGenerator(lower=0.01, upper=0.99, step=0.01)
17 def __init__( 18 self, 19 lower=0.01, 20 upper=0.99, 21 step=0.01, 22 ): 23 """ 24 Sets the list of choices to draw the set of parameters from. 25 26 Parameters 27 ---------- 28 lower: float 29 Lower thresholds of the range of choices. 30 upper: float 31 Upper thresholds of the range of choices. 32 step: float 33 Step between to choices in the range. 34 """ 35 self.lower = lower 36 self.upper = upper 37 self.step = step
Sets the list of choices to draw the set of parameters from.
Parameters
- lower (float): Lower thresholds of the range of choices.
- upper (float): Upper thresholds of the range of choices.
- step (float): Step between to choices in the range.
def
get_parameter_model( self, **kwargs) -> Fumagalli_Motta_Tarantino_2020.Configurations.StoreConfig.ParameterModel:
39 def get_parameter_model(self, **kwargs) -> StoreConfig.ParameterModel: 40 """ 41 Draws a random set of parameters. 42 43 Parameters 44 ---------- 45 **kwargs 46 Constant inputs for the parameter model. 47 48 Returns 49 ------- 50 StoreConfig.ParameterModel 51 ParameterModel containing the set of parameters. 52 """ 53 choices = list(arange(self.lower, self.upper, self.step)) 54 choice = random.choices(choices, k=11) 55 choice = [round(i, ndigits=2) for i in choice] 56 return StoreConfig.ParameterModel( 57 merger_policy=kwargs.get("merger_policy", Types.MergerPolicies.Strict), 58 development_costs=kwargs.get("development_costs", choice[0]), 59 startup_assets=kwargs.get("startup_assets", choice[1]), 60 success_probability=kwargs.get("success_probability", choice[2]), 61 development_success=kwargs.get("development_success", True), 62 private_benefit=kwargs.get("private_benefit", choice[3]), 63 consumer_surplus_without_innovation=kwargs.get( 64 "consumer_surplus_without_innovation", choice[4] 65 ), 66 incumbent_profit_without_innovation=kwargs.get( 67 "incumbent_profit_without_innovation", choice[5] 68 ), 69 consumer_surplus_duopoly=kwargs.get("consumer_surplus_duopoly", choice[6]), 70 incumbent_profit_duopoly=kwargs.get("incumbent_profit_duopoly", choice[7]), 71 startup_profit_duopoly=kwargs.get("startup_profit_duopoly", choice[8]), 72 consumer_surplus_with_innovation=kwargs.get( 73 "consumer_surplus_with_innovation", choice[9] 74 ), 75 incumbent_profit_with_innovation=kwargs.get( 76 "incumbent_profit_with_innovation", choice[10] 77 ), 78 )
Draws a random set of parameters.
Parameters
- **kwargs: Constant inputs for the parameter model.
Returns
- StoreConfig.ParameterModel: ParameterModel containing the set of parameters.
class
RandomConfig:
81class RandomConfig: 82 """ 83 Finds a random configuration satisfying specific requirements. 84 85 Example 86 -------- 87 ``` 88 import Fumagalli_Motta_Tarantino_2020 as FMT20 89 90 # search for a random configuration -> this can take a while 91 config = FMT20.RandomConfig(laissez_faire_optimal=True).find_config() 92 model = FMT20.OptimalMergerPolicy(**config()) 93 print(model.is_laissez_faire_optimal()) 94 ``` 95 """ 96 97 def __init__( 98 self, 99 parameter_generator=ParameterModelGenerator(), 100 model_type: Callable = Base.OptimalMergerPolicy, 101 callable_condition: Callable = None, 102 strict_optimal=None, 103 intermediate_optimal=None, 104 laissez_faire_optimal=None, 105 is_killer_acquisition=None, 106 **kwargs 107 ): 108 """ 109 Specify the requirements to satisfy in the model to find. 110 111 Parameters 112 ---------- 113 parameter_generator: ParameterModelGenerator 114 Generator for random sets of parameters 115 model_type: Callable 116 Type of the model to find the parameters for (conforms to Base.OptimalMergerPolicy). 117 callable_condition: Callable 118 Lambda with model as input and boolean as output (True equals satisfied requirement). 119 strict_optimal: Optional[bool] 120 If True, the model has a strict policy as optimal policy. 121 intermediate_optimal: Optional[bool] 122 If True, the model has an intermediate policy as optimal policy. 123 laissez_faire_optimal: Optional[bool] 124 If True, the model has a laissez-faire policy as optimal policy. 125 is_killer_acquisition: Optional[bool] 126 If True, the model facilitates a killer acquisition. 127 **kwargs 128 Constant inputs for the parameter model. 129 """ 130 self.parameter_generator = parameter_generator 131 self._model_type = model_type 132 self.strict_optimal = strict_optimal 133 self.intermediate_optimal = intermediate_optimal 134 self.laissez_faire_optimal = laissez_faire_optimal 135 self.is_killer_acquisition = is_killer_acquisition 136 self.callable_condition = callable_condition 137 self._kwargs = kwargs 138 139 def _check_conditions(self, model: Base.OptimalMergerPolicy) -> bool: 140 if not self._check_condition(model.is_strict_optimal(), self.strict_optimal): 141 return False 142 if not self._check_condition( 143 model.is_intermediate_optimal(), self.intermediate_optimal 144 ): 145 return False 146 if not self._check_condition( 147 model.is_laissez_faire_optimal(), self.laissez_faire_optimal 148 ): 149 return False 150 if not self._check_condition( 151 model.is_killer_acquisition(), self.is_killer_acquisition 152 ): 153 return False 154 if not self._check_callable_condition(self.callable_condition, model): 155 return False 156 return True 157 158 @staticmethod 159 def _check_callable_condition(condition: Callable, model: Base.OptimalMergerPolicy): 160 if condition is not None: 161 return condition(model) 162 return True 163 164 @staticmethod 165 def _check_condition(value: bool, check: bool) -> bool: 166 if check is not None: 167 if value == check: 168 return True 169 else: 170 return False 171 return True 172 173 def find_config(self) -> StoreConfig.ParameterModel: 174 """ 175 Triggers the process of finding a set of parameters satisfying the requirements. 176 177 Note: This can take a while, depending on the given conditions (or sometimes it is not possible to find a set 178 of parameters). 179 180 Returns 181 ------- 182 StoreConfig.ParameterModel 183 ParameterModel containing the set of parameters. 184 """ 185 while True: 186 try: 187 parameter_model: StoreConfig.ParameterModel = ( 188 self.parameter_generator.get_parameter_model(**self._kwargs) 189 ) 190 model = self._get_model(parameter_model) 191 if self._check_conditions(model): 192 return parameter_model 193 else: 194 pass 195 except AssertionError: 196 pass 197 198 def _get_model(self, parameter_model): 199 model = self._model_type( 200 **parameter_model(), 201 asset_distribution=self._kwargs.get( 202 "asset_distribution", Distributions.UniformDistribution 203 ), 204 ) 205 return model
Finds a random configuration satisfying specific requirements.
Example
import Fumagalli_Motta_Tarantino_2020 as FMT20
# search for a random configuration -> this can take a while
config = FMT20.RandomConfig(laissez_faire_optimal=True).find_config()
model = FMT20.OptimalMergerPolicy(**config())
print(model.is_laissez_faire_optimal())
RandomConfig( parameter_generator=<ParameterModelGenerator object>, model_type: Callable = <class 'Fumagalli_Motta_Tarantino_2020.Models.Base.OptimalMergerPolicy'>, callable_condition: Callable = None, strict_optimal=None, intermediate_optimal=None, laissez_faire_optimal=None, is_killer_acquisition=None, **kwargs)
97 def __init__( 98 self, 99 parameter_generator=ParameterModelGenerator(), 100 model_type: Callable = Base.OptimalMergerPolicy, 101 callable_condition: Callable = None, 102 strict_optimal=None, 103 intermediate_optimal=None, 104 laissez_faire_optimal=None, 105 is_killer_acquisition=None, 106 **kwargs 107 ): 108 """ 109 Specify the requirements to satisfy in the model to find. 110 111 Parameters 112 ---------- 113 parameter_generator: ParameterModelGenerator 114 Generator for random sets of parameters 115 model_type: Callable 116 Type of the model to find the parameters for (conforms to Base.OptimalMergerPolicy). 117 callable_condition: Callable 118 Lambda with model as input and boolean as output (True equals satisfied requirement). 119 strict_optimal: Optional[bool] 120 If True, the model has a strict policy as optimal policy. 121 intermediate_optimal: Optional[bool] 122 If True, the model has an intermediate policy as optimal policy. 123 laissez_faire_optimal: Optional[bool] 124 If True, the model has a laissez-faire policy as optimal policy. 125 is_killer_acquisition: Optional[bool] 126 If True, the model facilitates a killer acquisition. 127 **kwargs 128 Constant inputs for the parameter model. 129 """ 130 self.parameter_generator = parameter_generator 131 self._model_type = model_type 132 self.strict_optimal = strict_optimal 133 self.intermediate_optimal = intermediate_optimal 134 self.laissez_faire_optimal = laissez_faire_optimal 135 self.is_killer_acquisition = is_killer_acquisition 136 self.callable_condition = callable_condition 137 self._kwargs = kwargs
Specify the requirements to satisfy in the model to find.
Parameters
- parameter_generator (ParameterModelGenerator): Generator for random sets of parameters
- model_type (Callable): Type of the model to find the parameters for (conforms to Base.OptimalMergerPolicy).
- callable_condition (Callable): Lambda with model as input and boolean as output (True equals satisfied requirement).
- strict_optimal (Optional[bool]): If True, the model has a strict policy as optimal policy.
- intermediate_optimal (Optional[bool]): If True, the model has an intermediate policy as optimal policy.
- laissez_faire_optimal (Optional[bool]): If True, the model has a laissez-faire policy as optimal policy.
- is_killer_acquisition (Optional[bool]): If True, the model facilitates a killer acquisition.
- **kwargs: Constant inputs for the parameter model.
173 def find_config(self) -> StoreConfig.ParameterModel: 174 """ 175 Triggers the process of finding a set of parameters satisfying the requirements. 176 177 Note: This can take a while, depending on the given conditions (or sometimes it is not possible to find a set 178 of parameters). 179 180 Returns 181 ------- 182 StoreConfig.ParameterModel 183 ParameterModel containing the set of parameters. 184 """ 185 while True: 186 try: 187 parameter_model: StoreConfig.ParameterModel = ( 188 self.parameter_generator.get_parameter_model(**self._kwargs) 189 ) 190 model = self._get_model(parameter_model) 191 if self._check_conditions(model): 192 return parameter_model 193 else: 194 pass 195 except AssertionError: 196 pass
Triggers the process of finding a set of parameters satisfying the requirements.
Note: This can take a while, depending on the given conditions (or sometimes it is not possible to find a set of parameters).
Returns
- StoreConfig.ParameterModel: ParameterModel containing the set of parameters.