def __init__(self, copula_spec): """Init class.""" version = distribute_copula_spec(copula_spec, 'version') self.attr = dict() self.attr['version'] = version # Assign correct monitoring and attribute checks function. if version in ['scaled_archimedean']: self._logging = partial(log_scaled_archimedean, self) self._check_attributes = partial(check_attributes_scaled_archimedean, self) elif version in ['nonstationary']: self._logging = partial(log_nonstationary, self) self._check_attributes = partial(check_attributes_nonstationary, self) elif version in ['warmglow']: self._logging = partial(log_warmglow, self) self._check_attributes = partial(check_attributes_warmglow, self) else: raise NotImplementedError # Handle other attributes if version in ['scaled_archimedean']: args = ['r', 'bounds', 'delta', 'u', 'generating_function', 'a', 'b', 'marginals'] r, bounds, delta, u, generating_function, a, b, marginals = \ distribute_copula_spec(copula_spec, *args) marginal_utility = [] for i, marginal in enumerate(marginals): if marginal == 'power': marginal_utility += [PowerCls(r[i], a, b, bounds[i])] elif marginal == 'exponential': marginal_utility += [ExponentialCls(r[i], a, b, bounds[i])] self.attr['x_uniattribute_utility'] = marginal_utility[0] self.attr['y_uniattribute_utility'] = marginal_utility[1] self.attr['bounds'] = bounds self.attr['delta'] = delta self.attr['u_1'] = u[0] self.attr['u_2'] = u[1] copula = ScaledArchimedeanCls(generating_function, u[0], u[1], delta) elif version in ['nonstationary', 'warmglow']: args = ['alpha', 'beta', 'gamma', 'discount_factors', 'y_scale', 'unrestricted_weights', 'discounting'] alpha, beta, gamma, discount_factors, y_scale, unrestricted_weights, discounting = \ distribute_copula_spec(copula_spec, *args) self.attr['unrestricted_weights'] = unrestricted_weights self.attr['discount_factors'] = discount_factors self.attr['discounting'] = discounting self.attr['y_scale'] = y_scale self.attr['alpha'] = alpha self.attr['gamma'] = gamma self.attr['beta'] = beta if version in ['nonstationary']: copula = NonstationaryUtilCls( alpha, beta, gamma, discount_factors, y_scale, unrestricted_weights, discounting) elif version in ['warmglow']: # Handle version-specifc parameters args = ['warmglow_type'] warmglow_model = distribute_copula_spec(copula_spec, *args) copula = WarmglowUtilCls( alpha, beta, gamma, discount_factors, y_scale, unrestricted_weights, discounting, warmglow_model) else: raise NotImplementedError else: raise NotImplementedError self.attr['copula'] = copula self._check_attributes() self._logging()
np.random.seed(123) for _ in range(10): x, y, is_normalized, copula_spec = generate_random_request( {'version': 'nonstationary'}) # copula_spec['marginals'] = ['exponential', 'exponential'] # copula_spec['r'] = [-5, -5] # copula_spec['bounds'] = [500, 500] # print(is_normalized, 'out') copula = UtilityCopulaCls(copula_spec) util = copula.evaluate(x, y, is_normalized) alpha, beta, gamma, y_scale, version = \ distribute_copula_spec(copula_spec, 'alpha', 'beta', 'gamma', 'y_scale', 'version') print('version: {}'.format(version)) print('alpha: {0:.2f}, beta: {1:.2f}, gamma: {2:.2f}, y_scale: {3:.2f}'. format(alpha, beta, gamma, y_scale)) print('x: {0:.2f}, y: {1:.2f}, utility: {2:.2f}.'.format(x, y, util)) # Don't expect monotonicity here print(copula.evaluate(x, y)) print(copula.evaluate(x, y, t=0)) print(copula.evaluate(x, y, t=1)) print(copula.evaluate(x, y, t=3)) print(copula.evaluate(x, y, t=6)) print(copula.evaluate(x, y, t=12)) print(copula.evaluate(x, y, t=24))