def transfer_same_dist(test_list, train_list, com_comp, test_rem): if len(test_rem) == 0: return test_list, train_list, com_comp sizes = [len(line) for line in test_rem] mean_test_size = np_mean(sizes) sd = sqrt(np_var(sizes)) if sd != 0: test_rem_dist = norm_dist(mean_test_size, sd) p_dist = [test_rem_dist.pdf(len(line)) for line in train_list] norm_ct = sum(p_dist) if norm_ct != 0: p_dist = [val / norm_ct for val in p_dist] train_rem = rand_choice(train_list, size=com_comp, replace=False, p=p_dist) else: train_rem = [ line for line in train_list if len(line) == mean_test_size ][:com_comp] test_list = test_list + train_rem for line in train_rem: train_list.remove(line) return test_list, train_list
def central_limit(rvs, n, length): rv_mean = np.zeros(length) for i in range(n): rv = rvs(size=length) rv_mean = rv_mean + rv rv_mean = rv_mean / n gaussian_params = norm_dist.fit(rv_mean) gaussian = norm_dist(gaussian_params[0], gaussian_params[1]) return rv_mean, gaussian
def run_case(dummy, fuel, P_0, T_0, P_C, mfuel, T_a, mix): # Set the Cantera Solution with the thermo data from the xml file. # Get the molecular weight of the fuel and set the unit basis for # the Solution to molar basis. gas = ct.Solution('therm-data.xml') fuel_mw = gas.molecular_weights[gas.species_index(fuel)] gas.basis = 'molar' # Set the ambient temperature and distribution. Convert the ambient # temperature to °C to match the spec but use absolute temperature # for the distribution. sigma_Ta = max(2.2, (T_a - 273.15) * 0.0075) / 2 Ta_dist = norm_dist(loc=T_a, scale=sigma_Ta) # Ta_dist = uniform(loc=T_a-sigma_Ta, scale=sigma_Ta*2) # Ta_dist = triang(loc=T_a-sigma_Ta, scale=sigma_Ta*2, c=0.5) # Set the tank volume and distribution. nom_tank_volume = 0.01660 sigma_volume = 0.00001 vol_dist = norm_dist(loc=nom_tank_volume, scale=sigma_volume) # Create the normal distributions for the initial temperature, # initial pressure, and compressed pressure. Convert the initial # temperature to °C to match the spec. Use the appropriate # distribution for the desired analysis (normal, uniform, # triangular). sigma_T0 = max(2.2, (T_0 - 273) * 0.0075) / 2 T0_dist = norm_dist(loc=T_0, scale=sigma_T0) # T0_dist = uniform(loc=T_0-sigma_T0, scale=sigma_T0*2) # T0_dist = triang(loc=T_0-sigma_T0, scale=sigma_T0*2, c=0.5) sigma_P0 = 346.6 / 2 P0_dist = norm_dist(loc=P_0, scale=sigma_P0) sigma_PC = 5000 / 2 PC_dist = norm_dist(loc=P_C, scale=sigma_PC) # Set the nominal injected mass of the fuel. Compute the nominal # moles of fuel and corresponding nominal required number of moles # of the gases. nom_mass_fuel = mfuel nom_mole_fuel = nom_mass_fuel / fuel_mw nom_mole_o2 = nom_mole_fuel * mix[0] nom_mole_n2 = nom_mole_fuel * mix[1] nom_mole_ar = nom_mole_fuel * mix[2] # Create the normal distribution for the fuel mass. sigma_mass = 0.03 / 2 fuel_mass_dist = norm_dist(loc=nom_mass_fuel, scale=sigma_mass) # Calculate the nominal pressure required for each gas to match the # desired molar proportions. Note that the gas constant from # Cantera is given in units of J/kmol-K, hence the factor of 1000. nom_o2_pres = nom_mole_o2 * ct.gas_constant * T_a / (1000 * nom_tank_volume) nom_n2_pres = nom_mole_n2 * ct.gas_constant * T_a / (1000 * nom_tank_volume) nom_ar_pres = nom_mole_ar * ct.gas_constant * T_a / (1000 * nom_tank_volume) # Compute the pressures of each component as they are filled into # the mixing tank. The mean of the distribution of the pressure of # each subsequent gas is the sum of the sampled value of the # pressure of the previous gas plus the nominal value of the # current gas. Note that these are thus not partial pressures, but # the total pressure in the tank after filling each component. sigma_pressure = 346.6 / 2 o2_dist = norm_dist(loc=nom_o2_pres, scale=sigma_pressure) o2_pres_rand = o2_dist.ppf(np.random.random_sample()) n2_pressure = o2_pres_rand + nom_n2_pres n2_dist = norm_dist(loc=n2_pressure, scale=sigma_pressure) n2_pres_rand = n2_dist.ppf(np.random.random_sample()) ar_pressure = n2_pres_rand + nom_ar_pres ar_dist = norm_dist(loc=ar_pressure, scale=sigma_pressure) ar_pres_rand = ar_dist.ppf(np.random.random_sample()) # Sample random values of the ambient temperature, tank volume, and # fuel mass from their distributions. Ta_rand = Ta_dist.ppf(np.random.random_sample()) tank_volume_rand = vol_dist.ppf(np.random.random_sample()) mole_fuel_rand = fuel_mass_dist.ppf(np.random.random_sample()) / fuel_mw # Compute the number of moles of each gaseous component based on # the sampling from the various distributions. Note that the gas # constant from Cantera is given in units of J/kmol-K, hence the # factor of 1000. mole_o2_rand = o2_pres_rand * tank_volume_rand * 1000 / (ct.gas_constant * Ta_rand) mole_n2_rand = (n2_pres_rand - o2_pres_rand) * tank_volume_rand * 1000 / ( ct.gas_constant * Ta_rand) mole_ar_rand = (ar_pres_rand - n2_pres_rand) * tank_volume_rand * 1000 / ( ct.gas_constant * Ta_rand) # Compute the mole fractions of each component and set the state of # the Cantera solution. total_moles = sum( [mole_fuel_rand, mole_o2_rand, mole_n2_rand, mole_ar_rand]) mole_fractions = '{fuel_name}:{fuel_mole},o2:{o2},n2:{n2},ar:{ar}'.format( fuel_name=fuel, fuel_mole=mole_fuel_rand / total_moles, o2=mole_o2_rand / total_moles, n2=mole_n2_rand / total_moles, ar=mole_ar_rand / total_moles) gas.TPX = None, None, mole_fractions # Initialize the array of temperatures over which the C_p should be fit. # The range is [first input, second input) with increment set by the third # input. Loop through the temperatures and compute the non-dimensional # specific heats. temperatures = np.arange(300, 1105, 5) gas_cp = np.zeros(len(temperatures)) for j, temp in enumerate(temperatures): gas.TP = temp, None gas_cp[j] = gas.cp / ct.gas_constant # Compute the linear fit to the specific heat. (gas_b, gas_a) = np.polyfit(temperatures, gas_cp, 1) # Sample the values for the initial temperature, initial pressure, and # compressed pressure. T0_rand = T0_dist.ppf(np.random.random_sample()) P0_rand = P0_dist.ppf(np.random.random_sample()) PC_rand = PC_dist.ppf(np.random.random_sample()) # Compute the compressed temperature and return it. lam_rand = gas_b / gas_a * np.exp( gas_b * T0_rand / gas_a) * T0_rand * (PC_rand / P0_rand)**(1 / gas_a) TC_rand = np.real(gas_a * lambertw(lam_rand) / gas_b) return TC_rand
def run_case(dummy, fuel, P_0, T_0, P_C, mfuel, T_a, mix): # Set the Cantera Solution with the thermo data from the xml file. # Get the molecular weight of the fuel and set the unit basis for # the Solution to molar basis. gas = ct.Solution('therm-data.xml') fuel_mw = gas.molecular_weights[gas.species_index(fuel)] gas.basis = 'molar' # Set the ambient temperature and distribution. Convert the ambient # temperature to °C to match the spec but use absolute temperature # for the distribution. sigma_Ta = max(2.2, (T_a - 273.15)*0.0075)/2 Ta_dist = norm_dist(loc=T_a, scale=sigma_Ta) # Ta_dist = uniform(loc=T_a-sigma_Ta, scale=sigma_Ta*2) # Ta_dist = triang(loc=T_a-sigma_Ta, scale=sigma_Ta*2, c=0.5) # Set the tank volume and distribution. nom_tank_volume = 0.01660 sigma_volume = 0.00001 vol_dist = norm_dist(loc=nom_tank_volume, scale=sigma_volume) # Create the normal distributions for the initial temperature, # initial pressure, and compressed pressure. Convert the initial # temperature to °C to match the spec. Use the appropriate # distribution for the desired analysis (normal, uniform, # triangular). sigma_T0 = max(2.2, (T_0 - 273)*0.0075)/2 T0_dist = norm_dist(loc=T_0, scale=sigma_T0) # T0_dist = uniform(loc=T_0-sigma_T0, scale=sigma_T0*2) # T0_dist = triang(loc=T_0-sigma_T0, scale=sigma_T0*2, c=0.5) sigma_P0 = 346.6/2 P0_dist = norm_dist(loc=P_0, scale=sigma_P0) sigma_PC = 5000/2 PC_dist = norm_dist(loc=P_C, scale=sigma_PC) # Set the nominal injected mass of the fuel. Compute the nominal # moles of fuel and corresponding nominal required number of moles # of the gases. nom_mass_fuel = mfuel nom_mole_fuel = nom_mass_fuel/fuel_mw nom_mole_o2 = nom_mole_fuel*mix[0] nom_mole_n2 = nom_mole_fuel*mix[1] nom_mole_ar = nom_mole_fuel*mix[2] # Create the normal distribution for the fuel mass. sigma_mass = 0.03/2 fuel_mass_dist = norm_dist(loc=nom_mass_fuel, scale=sigma_mass) # Calculate the nominal pressure required for each gas to match the # desired molar proportions. Note that the gas constant from # Cantera is given in units of J/kmol-K, hence the factor of 1000. nom_o2_pres = nom_mole_o2*ct.gas_constant*T_a/(1000*nom_tank_volume) nom_n2_pres = nom_mole_n2*ct.gas_constant*T_a/(1000*nom_tank_volume) nom_ar_pres = nom_mole_ar*ct.gas_constant*T_a/(1000*nom_tank_volume) # Compute the pressures of each component as they are filled into # the mixing tank. The mean of the distribution of the pressure of # each subsequent gas is the sum of the sampled value of the # pressure of the previous gas plus the nominal value of the # current gas. Note that these are thus not partial pressures, but # the total pressure in the tank after filling each component. sigma_pressure = 346.6/2 o2_dist = norm_dist(loc=nom_o2_pres, scale=sigma_pressure) o2_pres_rand = o2_dist.ppf(np.random.random_sample()) n2_pressure = o2_pres_rand + nom_n2_pres n2_dist = norm_dist(loc=n2_pressure, scale=sigma_pressure) n2_pres_rand = n2_dist.ppf(np.random.random_sample()) ar_pressure = n2_pres_rand + nom_ar_pres ar_dist = norm_dist(loc=ar_pressure, scale=sigma_pressure) ar_pres_rand = ar_dist.ppf(np.random.random_sample()) # Sample random values of the ambient temperature, tank volume, and # fuel mass from their distributions. Ta_rand = Ta_dist.ppf(np.random.random_sample()) tank_volume_rand = vol_dist.ppf(np.random.random_sample()) mole_fuel_rand = fuel_mass_dist.ppf(np.random.random_sample())/fuel_mw # Compute the number of moles of each gaseous component based on # the sampling from the various distributions. Note that the gas # constant from Cantera is given in units of J/kmol-K, hence the # factor of 1000. mole_o2_rand = o2_pres_rand*tank_volume_rand*1000/(ct.gas_constant*Ta_rand) mole_n2_rand = (n2_pres_rand - o2_pres_rand)*tank_volume_rand*1000/(ct.gas_constant*Ta_rand) mole_ar_rand = (ar_pres_rand - n2_pres_rand)*tank_volume_rand*1000/(ct.gas_constant*Ta_rand) # Compute the mole fractions of each component and set the state of # the Cantera solution. total_moles = sum([mole_fuel_rand, mole_o2_rand, mole_n2_rand, mole_ar_rand]) mole_fractions = '{fuel_name}:{fuel_mole},o2:{o2},n2:{n2},ar:{ar}'.format( fuel_name=fuel, fuel_mole=mole_fuel_rand/total_moles, o2=mole_o2_rand/total_moles, n2=mole_n2_rand/total_moles, ar=mole_ar_rand/total_moles) gas.TPX = None, None, mole_fractions # Initialize the array of temperatures over which the C_p should be fit. # The range is [first input, second input) with increment set by the third # input. Loop through the temperatures and compute the non-dimensional # specific heats. temperatures = np.arange(300, 1105, 5) gas_cp = np.zeros(len(temperatures)) for j, temp in enumerate(temperatures): gas.TP = temp, None gas_cp[j] = gas.cp/ct.gas_constant # Compute the linear fit to the specific heat. (gas_b, gas_a) = np.polyfit(temperatures, gas_cp, 1) # Sample the values for the initial temperature, initial pressure, and # compressed pressure. T0_rand = T0_dist.ppf(np.random.random_sample()) P0_rand = P0_dist.ppf(np.random.random_sample()) PC_rand = PC_dist.ppf(np.random.random_sample()) # Compute the compressed temperature and return it. lam_rand = gas_b/gas_a * np.exp(gas_b*T0_rand/gas_a) * T0_rand * (PC_rand/P0_rand)**(1/gas_a) TC_rand = np.real(gas_a * lambertw(lam_rand)/gas_b) return TC_rand
learning_rate=args.lr, natural_gradient=args.natural, minibatch_frac=args.minibatch_frac, verbose=args.verbose, ) ngb.fit(X_trainall, y_trainall) # the final prediction for this fold forecast = ngb.pred_dist(X_test, max_iter=best_itr) forecast_val = ngb.pred_dist(X_val, max_iter=best_itr) # set the appropriate scale if using a homoskedastic Normal if args.distn == "NormalFixedVar": scale = (forecast.var * ((forecast_val.loc - y_val.flatten())**2).mean()**0.5) forecast = norm_dist(loc=forecast.loc, scale=scale) ngb_rmse += [np.sqrt(mean_squared_error(forecast.mean(), y_test))] ngb_nll += [-forecast.logpdf(y_test.flatten()).mean()] print("[%d/%d] BestIter=%d RMSE: Val=%.4f Test=%.4f NLL: Test=%.4f" % ( itr + 1, args.n_splits, best_itr, np.sqrt(val_rmse[best_itr - 1]), np.sqrt(mean_squared_error(forecast.mean(), y_test)), ngb_nll[-1], )) print( "== RMSE GBM=%.4f +/- %.4f, NGB=%.4f +/- %.4f, NLL NGB=%.4f +/ %.4f" %
def run_case(dummy, fuel, P_0, T_0, P_C, mix): # Set the Cantera Solution with the thermo data from the xml file. # Set the unit basis for the Solution to molar basis. gas = ct.Solution('therm-data.xml') gas.basis = 'molar' # Create the normal distributions for the initial temperature, # initial pressure, and compressed pressure. Convert the initial # temperature to °C to match the spec. Use the appropriate # distribution for the desired analysis (normal, uniform, # triangular). sigma_T0 = max(2.2, (T_0 - 273)*0.0075)/2 T0_dist = norm_dist(loc=T_0, scale=sigma_T0) # T0_dist = triang(loc=T_0-sigma_T0, scale=2*sigma_T0, c=0.5) # T0_dist = uniform(loc=T_0-sigma_T0, scale=2*sigma_T0) sigma_P0 = 346.6/2 P0_dist = norm_dist(loc=P_0, scale=sigma_P0) sigma_PC = 5000/2 PC_dist = norm_dist(loc=P_C, scale=sigma_PC) sigma_pressure = 346.6/2 # For the experiments studied here, if there was no argon added, # the nitrogen was added first. Otherwise, the order was o2, n2, # ar, fuel. Compute the pressures of each component as they are # filled into the mixing tank. if mix['ar'] > 0: nom_o2_pres = mix['o2']*ct.one_atm/760 nom_n2_pres = (mix['n2'] - mix['o2'])*ct.one_atm/760 nom_ar_pres = (mix['ar'] - mix['n2'])*ct.one_atm/760 nom_fuel_pres = (mix['fuel'] - mix['ar'])*ct.one_atm/760 o2_dist = norm_dist(loc=nom_o2_pres, scale=sigma_pressure) o2_pres_rand = o2_dist.ppf(np.random.random_sample()) n2_pressure = o2_pres_rand + nom_n2_pres n2_dist = norm_dist(loc=n2_pressure, scale=sigma_pressure) n2_pres_rand = n2_dist.ppf(np.random.random_sample()) ar_pressure = n2_pres_rand + nom_ar_pres ar_dist = norm_dist(loc=ar_pressure, scale=sigma_pressure) ar_pres_rand = ar_dist.ppf(np.random.random_sample()) fuel_pressure = ar_pres_rand + nom_fuel_pres fuel_dist = norm_dist(loc=fuel_pressure, scale=sigma_pressure) fuel_pres_rand = fuel_dist.ppf(np.random.random_sample()) o2_par_pres = o2_pres_rand n2_par_pres = n2_pres_rand - o2_pres_rand ar_par_pres = ar_pres_rand - n2_pres_rand fuel_par_pres = fuel_pres_rand - ar_pres_rand else: nom_n2_pres = mix['n2']*ct.one_atm/760 nom_o2_pres = (mix['o2'] - mix['n2'])*ct.one_atm/760 nom_fuel_pres = (mix['fuel'] - mix['o2'])*ct.one_atm/760 nom_ar_pres = 0 n2_dist = norm_dist(loc=nom_n2_pres, scale=sigma_pressure) n2_pres_rand = n2_dist.ppf(np.random.random_sample()) o2_pressure = n2_pres_rand + nom_o2_pres o2_dist = norm_dist(loc=o2_pressure, scale=sigma_pressure) o2_pres_rand = o2_dist.ppf(np.random.random_sample()) fuel_pressure = o2_pres_rand + nom_fuel_pres fuel_dist = norm_dist(loc=fuel_pressure, scale=sigma_pressure) fuel_pres_rand = fuel_dist.ppf(np.random.random_sample()) n2_par_pres = n2_pres_rand o2_par_pres = o2_pres_rand - n2_pres_rand fuel_par_pres = fuel_pres_rand - o2_pres_rand ar_par_pres = 0 # Compute the mole fractions of each component and set the state of # the Cantera solution. total_pres = sum([o2_par_pres, n2_par_pres, ar_par_pres, fuel_par_pres]) mole_fractions = '{fuel_name}:{fuel_mole},o2:{o2},n2:{n2},ar:{ar}'.format( fuel_name=fuel, fuel_mole=fuel_par_pres/total_pres, o2=o2_par_pres/total_pres, n2=n2_par_pres/total_pres, ar=ar_par_pres/total_pres) gas.TPX = None, None, mole_fractions # Initialize the array of temperatures over which the C_p should be # fit. The range is [first input, second input) with increment set # by the third input. Loop through the temperatures and compute the # non-dimensional specific heats. temperatures = np.arange(300, 1105, 5) gas_cp = np.zeros(len(temperatures)) for j, temp in enumerate(temperatures): gas.TP = temp, None gas_cp[j] = gas.cp/ct.gas_constant # Compute the linear fit to the specific heat. (gas_b, gas_a) = np.polyfit(temperatures, gas_cp, 1) # Sample the values for the initial temperature, initial pressure, and # compressed pressure. T0_rand = T0_dist.ppf(np.random.random_sample()) P0_rand = P0_dist.ppf(np.random.random_sample()) PC_rand = PC_dist.ppf(np.random.random_sample()) # Compute the compressed temperature and return it. lam_rand = gas_b/gas_a * np.exp(gas_b*T0_rand/gas_a) * T0_rand * (PC_rand/P0_rand)**(1/gas_a) TC_rand = np.real(gas_a * lambertw(lam_rand)/gas_b) return TC_rand