def _xval_model_error(self, fit_spectra, reject_outliers, fit_lb, fit_ub, fitter, func): """ Helper function for calculation of split-half cross-validation model error and signal reliability. """ set1 = fit_spectra[::2] set2 = fit_spectra[1::2] errs = [] signal_select = [] models = [] signals = [] for this_set in [set1, set2]: choose_transients, model, signal, params, this_idx =\ self._fit_helper(this_set, reject_outliers, fit_lb, fit_ub, fitter) models.append(np.nanmean(model[choose_transients], 0)) signals.append(np.nanmean(signal[choose_transients], 0)) signal_select.append(signal[choose_transients]) #Cross-validation error estimation: model_err = np.mean( [ut.rmse(models[0], signals[1]), ut.rmse(models[1], signals[0])]) # Also for the signal: signal_err = ut.rmse(np.nanmean(signal_select[0], 0), np.nanmean(signal_select[1], 0)) # Based on the errors, choose a function. Also report errors: return model_err, signal_err
def _xval_choose_funcs(self, fit_spectra, reject_outliers, fit_lb, fit_ub, fitters=[ana.fit_gaussian,ana.fit_two_gaussian], funcs = [ut.gaussian, ut.two_gaussian]): """ Helper function used to do split-half xvalidation to select among alternative models""" set1 = fit_spectra[::2] set2 = fit_spectra[1::2] errs = [] signal_select = [] # We can loop over functions and try each one out, checking the # error in each: for fitter in fitters: models = [] signals = [] for this_set in [set1, set2]: choose_transients, model, signal, params, this_idx =\ self._fit_helper(this_set, reject_outliers, fit_lb, fit_ub, fitter) models.append(np.nanmean(model[choose_transients], 0)) signals.append(np.nanmean(signal[choose_transients], 0)) signal_select.append(signal[choose_transients]) #Cross-validate! errs.append(np.mean([ut.rmse(models[0], signals[1]), ut.rmse(models[1], signals[0])])) # We really only need to look at the first two: signal_err = ut.rmse(np.nanmean(signal_select[0], 0), np.nanmean(signal_select[1], 0)) # Based on the errors, choose a function. Also report errors: return (fitters[np.argmin(errs)], funcs[np.argmin(errs)], np.min(errs), signal_err)
def _xval_model_error(self, fit_spectra, reject_outliers, fit_lb, fit_ub, fitter, func): """ Helper function for calculation of split-half cross-validation model error and signal reliability. """ set1 = fit_spectra[::2] set2 = fit_spectra[1::2] errs = [] signal_select = [] models = [] signals = [] for this_set in [set1, set2]: choose_transients, model, signal, params, this_idx =\ self._fit_helper(this_set, reject_outliers, fit_lb, fit_ub, fitter) models.append(np.nanmean(model[choose_transients], 0)) signals.append(np.nanmean(signal[choose_transients], 0)) signal_select.append(signal[choose_transients]) #Cross-validation error estimation: model_err = np.mean([ut.rmse(models[0], signals[1]), ut.rmse(models[1], signals[0])]) # Also for the signal: signal_err = ut.rmse(np.nanmean(signal_select[0], 0), np.nanmean(signal_select[1], 0)) # Based on the errors, choose a function. Also report errors: return model_err, signal_err