def covariance_stuff(self, cost_function): ls_zero_step = scipy_ls(cost_function, self.fit_coefficients, args=(self.data_frame.temp, self.data_frame.experiment)) temporary_parameters = ls_zero_step.x jacobian = ls_zero_step.jac covariance_matrix = np.linalg.inv(jacobian.T.dot(jacobian)) temporary_fit = cost_function(temporary_parameters, self.data_frame.temp, self.data_frame.experiment) temporary_fit *= temporary_fit # todo read https://stackoverflow.com/questions/28702631/scipy-curve-fit-returns-negative-variance sigma_squared = sum(temporary_fit) / (len(self.data_frame.temp) - len(self.fit_coefficients) / 2) covariance_matrix *= sigma_squared diagonal = np.sqrt(np.diagonal(covariance_matrix)) self.confidence_params = np.sqrt(diagonal) self.confidence_params *= np.abs(self.level_t()) print('Confidence something: ', self.confidence_params) need_new = False for a, b in zip(self.fit_coefficients, self.confidence_params): if a < b: need_new = True if need_new: self.fit_coefficients = np.append(self.fit_coefficients, 0.1) self.fit_coefficients = np.append(self.fit_coefficients, 10)
def j_mode_approximation(self): t_ref = self.data_frame.reference_temperature t_ref_vector = t_ref * np.ones(len(self.enthalpy_temperature)) c_0 = self.data_frame.reference_enthalpy_value c_1 = self.data_frame.reference_heat_capacity_value updated_enthalpy = self.enthalpy_data - c_0 * np.ones( len(self.enthalpy_temperature)) - c_1 * ( self.enthalpy_temperature - t_ref_vector) updated_heat_capacity = self.data_frame.cp_e - c_1 * np.ones( len(self.heat_capacity_temperature)) initial_fit = scipy_ls(self.joint_cost_function, self.params, args=(self.enthalpy_temperature, updated_enthalpy, self.data_frame.cp_t, updated_heat_capacity)) self.fit_coefficients = self.stationary_coefficients( initial_fit.x.tolist(), t_ref, c_0, c_1) self.fit_enthalpy = self.delta_enthalpy(self.fit_coefficients, self.data_frame.dh_t) self.fit_heat_capacity = self.heat_capacity(self.fit_coefficients, self.data_frame.cp_t)
def approx(self): if self.mode == 'j': res_lsq = scipy_ls(self.joint_cost_function, self.params, # bounds=self.bounds, args=(self.data_frame.dh_t, self.data_frame.dh_e, self.data_frame.cp_t, self.data_frame.cp_e)) elif self.mode == 'h': res_lsq = scipy_ls(self.delta_enthalpy_cost, self.params, # bounds=self.bounds, args=(self.data_frame.dh_t, self.data_frame.dh_e)) elif self.mode == 'c': res_lsq = scipy_ls(self.heat_capacity_cost, self.params, # bounds=self.bounds, args=(self.data_frame.cp_t, self.data_frame.cp_e)) else: print('Wrong calculation type!') raise ValueError('Type can only be h c j.\n') # print(res_lsq.x.tolist()) self.params = res_lsq.x.tolist()
def c_mode_approximation(self): self.params = np.ones(self.max_power - self.min_power + 1) initial_fit = scipy_ls(self.heat_capacity_cost, self.params, args=(self.data_frame.cp_t, self.data_frame.cp_e)) self.fit_coefficients = initial_fit.x.tolist() # self.fit_coefficients = self.stationary_coefficients(initial_fit.x.tolist(), t_ref, c_0, c_1) self.fit_enthalpy = self.delta_enthalpy(self.fit_coefficients, self.data_frame.dh_t) self.fit_heat_capacity = self.heat_capacity(self.fit_coefficients, self.data_frame.cp_t)
def approx(self): for i in range(0, self.power - 1): self.fit_coefficients.append(0.01) self.fit_coefficients.append(1.00) self.bounds[0].append(0.0) self.bounds[0].append(0.0) self.bounds[1].append(500.0) self.bounds[1].append(1.0e5) if self.mode == 'j': res_lsq = scipy_ls(self.joint_cost_function, self.fit_coefficients, bounds=self.bounds, args=(self.data_frame.dh_t, self.data_frame.dh_e, self.data_frame.cp_t, self.data_frame.cp_e)) elif self.mode == 'h': res_lsq = scipy_ls(self.delta_enthalpy_cost, self.fit_coefficients, bounds=self.bounds, args=(self.data_frame.dh_t, self.data_frame.dh_e)) elif self.mode == 'c': res_lsq = scipy_ls(self.heat_capacity_cost, self.fit_coefficients, bounds=self.bounds, args=(self.data_frame.cp_t, self.data_frame.cp_e)) else: print('Wrong calculation type!') raise ValueError('Type can only be h c j.\n') self.fit_coefficients = res_lsq.x.tolist()
def c_mode_constrained_approximation(self): # todo change to appr, add another named unconstrained t_ref = self.data_frame.reference_temperature c_0 = self.data_frame.reference_enthalpy_value c_1 = self.data_frame.reference_heat_capacity_value updated_cp = self.data_frame.cp_e - c_1 * np.ones(len(self.heat_capacity_temperature)) initial_fit = scipy_ls(self.heat_capacity_constrained_cost, self.params, args=(self.data_frame.cp_t, updated_cp)) self.fit_coefficients = self.stationary_coefficients(initial_fit.x.tolist(), t_ref, c_0, c_1) self.fit_enthalpy = self.delta_enthalpy(self.fit_coefficients, self.data_frame.dh_t) self.fit_heat_capacity = self.heat_capacity(self.fit_coefficients, self.data_frame.cp_t)
def h_mode_approximation(self): t_ref = self.data_frame.reference_temperature t_ref_vector = t_ref * np.ones(len(self.enthalpy_temperature)) c_0 = self.data_frame.reference_enthalpy_value c_1 = self.data_frame.reference_heat_capacity_value updated_experiment = self.data_frame.dh_e - c_0 * np.ones( len(self.enthalpy_temperature)) - c_1 * ( self.enthalpy_temperature - t_ref_vector) initial_fit = scipy_ls(self.delta_enthalpy_constrained_cost, self.params, args=(self.data_frame.dh_t, updated_experiment)) print(self.name, initial_fit.x.tolist()) self.fit_coefficients = self.stationary_coefficients( initial_fit.x.tolist(), t_ref, c_0, c_1) self.fit_enthalpy = self.delta_enthalpy(self.fit_coefficients, self.data_frame.dh_t) self.fit_heat_capacity = self.heat_capacity(self.fit_coefficients, self.data_frame.cp_t)