def calc_model(h_sd, alphaG): rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) return rh2_model
def odr_func(Z, h_sd): return s14.calc_rh2( h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, )
def calc_residual(params, h_sd, rh2): alphaG = params['alphaG'].value phi_g = params['phi_g'].value Z = params['Z'].value rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False) residual = rh2 - rh2_model return residual
def odr_func(alphaG, h_sd,): if 0: plt.scatter(h_sd, rh2) plt.yscale('log') plt.ylim([1e-3, 1e2]) plt.annotate(alphaG, xy=(0.8, 0.1), xycoords='axes fraction') plt.show() rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) return rh2_model
def calc_residual(params, h_sd, rh2, rh2_error=None): alphaG = params['alphaG'].value rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) if rh2_error is not None: residual = (rh2 - rh2_model) / rh2_error else: residual = rh2 - rh2_model return residual
def calc_model(alphaG): if 0: plt.scatter(h_sd, rh2) #plt.yscale('log') #plt.ylim([1e-3, 1e2]) plt.annotate(alphaG, xy=(0.8, 0.1), xycoords='axes fraction') plt.show() rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) #mask = rh2_model < 0 #print np.sum(rh2_model[~mask] < 0) mask = np.zeros(np.size(rh2), dtype=bool) chisq = np.nansum((rh2[~mask] - rh2_model[~mask])**2 / \ rh2_error[~mask]**2) return chisq
def odr_func(Z, h_sd): return s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, )
def fit_sternberg(h_sd, rh2, guesses=[10,], rh2_error=None, h_sd_error=None, radiation_type='beamed', phi_g=2.0, Z=1.0,): ''' Parameters ---------- h_sd : array-like Hydrogen surface density in units of solar mass per parsec**2 rh2 : array-like Ratio between molecular and atomic hydrogen masses. guesses : None, scalar, or M-length sequence. Initial guess for the parameters. See scipy.optimize.curve_fit. rh2_error : bool Error in rh2 parameter. Calculates a more accurate chi^2 statistic Returns ------- rh2_fit_params : array-like, optional Model parameter fits. Residual bootstrapping: http://stats.stackexchange.com/questions/67519/bootstrapping-residuals-am-i-doing-it-right ''' import sys sys.path.insert(0, '/usr/users/ezbc/.local/lib64/python2.7/site-packages/') from scipy.optimize import curve_fit, minimize from scipy import stats import scipy #from lmfit import minimize, Parameters, report_fit, Minimizer #import lmfit from myscience import sternberg14 as s14 import mystats import scipy.odr as odr if any(np.isnan(h_sd)): raise ValueError('HSD should not have nans') mask = (rh2 < 0.0) | (np.isnan(h_sd)) mask = np.isnan(h_sd) h_sd = h_sd[~mask] rh2 = rh2[~mask] rh2_error = rh2_error[~mask] if 0: def calc_residual(params, h_sd, rh2, rh2_error=None): alphaG = params['alphaG'].value rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) if rh2_error is not None: residual = (rh2 - rh2_model) / rh2_error else: residual = rh2 - rh2_model return residual # Set parameter limits and initial guesses params = Parameters() params.add('alphaG', value=guesses[0], min=0.01, max=1000, vary=True) # Perform the fit! result = minimize(calc_residual, params, args=(h_sd, rh2, rh2_error), method='leastsq', #method='anneal', is_weighted=True, ) # best-fit parameter alphaG = params['alphaG'].value elif 0: def calc_model(h_sd, alphaG): rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) return rh2_model popt, pcov = curve_fit(calc_model, h_sd, rh2, p0=guesses, sigma=rh2_error, method='lm', ) alphaG = popt[0] elif 1: def calc_model(alphaG): if 0: plt.scatter(h_sd, rh2) #plt.yscale('log') #plt.ylim([1e-3, 1e2]) plt.annotate(alphaG, xy=(0.8, 0.1), xycoords='axes fraction') plt.show() rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) #mask = rh2_model < 0 #print np.sum(rh2_model[~mask] < 0) mask = np.zeros(np.size(rh2), dtype=bool) chisq = np.nansum((rh2[~mask] - rh2_model[~mask])**2 / \ rh2_error[~mask]**2) return chisq res = minimize(calc_model, guesses[0], #method='powell', #method='nelder-mead', method='L-BFGS-B', bounds=[[0, None],], options={'disp':False}, ) alphaG = res.x[0] else: def odr_func(alphaG, h_sd,): if 0: plt.scatter(h_sd, rh2) plt.yscale('log') plt.ylim([1e-3, 1e2]) plt.annotate(alphaG, xy=(0.8, 0.1), xycoords='axes fraction') plt.show() rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) return rh2_model model = odr.Model(odr_func) data = odr.RealData(h_sd, rh2, sy=rh2_error) odr_instance = odr.ODR(data, model, beta0=guesses) output = odr_instance.run() alphaG = output.beta[0] # calculate model for observed H rh2_model = s14.calc_rh2(h_sd, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) # calculate model for resampled H h_sd_resampled = np.logspace(-3, 3, 1000) rh2_resampled = s14.calc_rh2(h_sd_resampled, alphaG, Z, phi_g=phi_g, return_fractions=False, radiation_type=radiation_type, ) return alphaG, rh2_model, rh2_resampled, h_sd_resampled