def start(logger): setup = set_up() model_param = get_model_param() parameters = {'alpha': 0, 'beta': 1, 'g_dsa': 2, 'g_dsr': 3} input_parameters = {'alpha': np.linspace(1.0, 2.5, 101), 'beta': np.linspace(0.02, 0.2, 101), 'g_dsa': np.linspace(0., 1., 101), 'g_dsr': np.linspace(0., 1., 101)} for rand in range(30): for biased_parameter, biased_idx in parameters.items(): result = dict() logger.debug("Simulation: %s" % setup['expected']) mu = setup['expected'][biased_idx] input_ = input_parameters[biased_parameter] irr_symbol = IrradianceModel(model_param['x'], model_param['zenith'], model_param['AMass'], model_param['pressure'], model_param['ssa']) getIrrRatio = irr_symbol.getcompiledModel('ratio') simulation = getIrrRatio(*setup['expected']) + np.random.normal(0, 0.001, len(model_param['x'])) expected = copy.copy(setup['expected']) variables = copy.copy(setup['variables']) guess = copy.copy(setup['guess']) bounds = copy.copy(setup['bounds']) del variables[biased_idx] del expected[biased_idx] del guess[biased_idx] del bounds[biased_idx] result['output'] = _iterate(simulation, variables, expected, guess, bounds, biased_parameter, irr_symbol, input_, logger) result['input'] = input_ - mu logger.info("======= Got %s" % result['output']) result['expected'] = np.zeros(len(input_)) result['expected'][0:len(setup['expected'])] = setup['expected'] frame = pd.DataFrame(result['output'], columns=variables) frame.insert(0, '%s' % biased_parameter, result['input']) frame.insert(0, 'expected', result['expected']) frame.to_csv('results/biased_%s_%s.txt' % (rand, biased_parameter), index=False)
def example(): # possible values from get_ssa import get_ssa from Model import IrradianceModel zenith = 53.1836240528 AMass = 1.66450160404 rel_h = 0.665 pressure = 950 AM = 5 ssa = get_ssa(rel_h, AM) iteration = 20 alphas = np.zeros(len(range(1, iteration)) + 1) x = np.linspace(200, 800, 100) irr = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure) irr_symbol = IrradianceModel(x, zenith, AMass, pressure, ssa) func = irr_symbol._irradiance_ratio() y = irr.irradiance_ratio(x, 2.5, 0.06, 0.0, 1.0, 1.0) for i in range(0, iteration): ssa = get_ssa(rel_h, AM) print(ssa) irr = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure) yerror = np.random.normal(0, 0.009, len(x)) y = irr.irradiance_ratio(x, 1.5, 0.06, 0.0, 0.6, 0.9) + yerror weights = 1 / yerror gmod = Model(irr.irradiance_ratio, independent_vars=["x"], param_names=["alpha", "beta", "g_dsa", "g_dsr"]) gmod.set_param_hint("alpha", value=1.0, min=-0.2, max=2.5) gmod.set_param_hint("beta", value=0.01, min=0.0, max=2.0) gmod.set_param_hint("g_dsa", value=0.6, min=0.0, max=1.0) gmod.set_param_hint("g_dsr", value=0.9, min=0.0, max=1.0) print(gmod.param_hints) print(gmod.param_names) print(gmod.independent_vars) result = gmod.fit(y, x=x) print(result.fit_report()) alphas[i] = result.params["alpha"].value # plt.plot(x, y, label='%s' % AM) # plt.plot(x, result.best_fit, 'r-', label='fit') y = irr.irradiance_ratio(x, 1.5, 0.06, 0.0, 0.6, 0.9) y2 = irr.irradiance_ratio(x, 1.5, 0.08, 0.0, 0.6, 0.9) plt.legend() plt.show()
def start2D(logger): model_param = get_model_param() setup = set_up() r_setup = copy.copy(set_up()) noise_count = 40 global_var = {'param': 'g_dsr', 'input': np.linspace(.1,.5,101), 'idx':3} local_var = {'param': 'g_dsa', 'input': np.linspace(.1,.5,101) ,'idx':2} for key in setup.keys(): del r_setup[key][global_var['idx']] del r_setup[key][local_var['idx']] irr_symbol = IrradianceModel(model_param['x'], model_param['zenith'], model_param['AMass'], model_param['pressure'], model_param['ssa']) getIrrRatio = irr_symbol.getcompiledModel('ratio') simulation = getIrrRatio(*setup['expected']) fit_parameters = dict((key,0) for key in r_setup['variables']) map_parameters = {'alpha': 0, 'beta': 1} for key, value in fit_parameters.items(): fit_parameters[key] = ['%s_%s' %(key, i) for i in range(noise_count)] for i, input_value in enumerate(global_var['input']): irr_symbol.setVariable(global_var['param'], input_value) logger.info(">>>> Global Input for %s: %s" %(global_var['param'], input_value)) result = dict() frame = pd.DataFrame() for count in range(noise_count): noised_simulation = simulation + np.random.normal(0, 0.001, len(model_param['x'])) logger.debug("Symbols %s" % irr_symbol.get_Symbols()) result['output'] = _iterate(noised_simulation, [], r_setup['expected'], r_setup['guess'], r_setup['bounds'], local_var['param'], irr_symbol, local_var['input'], logger, 2) for key, value in fit_parameters.items(): logger.debug("Inlude for parameter %s the column %s at index %s" % (key, value[count], map_parameters[key])) frame.insert(0, value[count], result['output'][:, map_parameters[key]]) for key, value in fit_parameters.items(): mean = frame[value].iloc[:].mean(axis=1) std = frame[value].iloc[:].std(axis=1) frame.insert(0, '%s_std' % key, std) frame.insert(0, '%s_mean' % key, mean) frame.insert(0, local_var['param'], local_var['input'] - setup['expected'][local_var['idx']]) buf = np.zeros(len(local_var['input'])) buf[0] = input_value - setup['expected'][global_var['idx']] frame.insert(0, global_var['param'], buf) buf[0:len(setup['expected'])] = setup['expected'] frame.insert(0, 'expected', buf) frame.to_csv('results/two_variation_Deltaoutput_%s.txt' % i, index=False) logger.info(frame) logger.debug(r_setup)