def test_target_timeseries_pred(): np.random.seed(123) goal = np.array([-10.549816, -0.657204, -4.163044, -12.341982, -6.353174]) proxy_ts = np.array([0.2831, 0.2856, 0.2832, 0.2854, 0.3081]) alpha_now = 0.3584 beta_now = 0.0054 tau2_now = 0.0016 prior_pars = {'mu': np.array([0.0535] * 5), 'inv_cov': np.eye(5) * 0.0278} victim = target_timeseries_pred(alpha_now, beta_now, tau2_now, proxy_ts, prior_pars) np.testing.assert_allclose(victim, goal, atol=1e-4)
def predict_seatemp_analog(tex, prior_std, temptype, search_tol, prior_mean=None, nens=5000, progressbar=True): """Predict sea temperature with TEX86, using the analog method Parameters ---------- tex : ndarray n-length array of TEX86 observations from a single location. prior_std : float Prior standard deviation for sea temperature (°C). temptype : str Type of sea temperature desired. Either 'sst' for sea-surface or 'subt'. search_tol: float Tolerance for finding analog locations. Comparison is between the mean of dats and the mean tex value within each large gridcell. prior_mean : float Prior mean for sea temperature (°C). nens : int Size of MCMC ensemble draws to use for calculation. progressbar: bool Whether or not to display a progress bar on the command line. The bar shows how many analogs have been completed. Returns ------- output : Prediction Raises ------ EnsembleSizeError """ draws = get_draws(temptype) tex_obs = get_tex(temptype) nd = len(tex) ntk = draws.alpha_samples_comp.shape[1] if ntk < nens: raise EnsembleSizeError(ntk, nens) latlon_match, val_match = tex_obs.find_within_tolerance(x=tex.mean(), tolerance=search_tol) n_locs_g = len(latlon_match) prior_par = {'mu': np.ones(nd) * prior_mean, 'inv_cov': np.eye(nd) * prior_std ** -2} n_latlon_matches = len(latlon_match) indices = range(n_latlon_matches) if progressbar: indices = tqdm(indices, total=n_latlon_matches) preds = np.empty((nd, n_locs_g, nens)) for kk in indices: latlon = latlon_match[kk] alpha_samples, beta_samples = draws.find_alphabeta_near(*latlon) for jj in range(nens): a_now = alpha_samples[jj] b_now = beta_samples[jj] t2_now = draws.tau2_samples[jj] preds[:, kk, jj] = target_timeseries_pred(alpha_now=a_now, beta_now=b_now, tau2_now=t2_now, proxy_ts=tex, prior_pars=prior_par) output = Prediction(ensemble=preds, temptype=temptype, prior_mean=prior_mean, prior_std=prior_std, analog_gridpoints=latlon_match) return output
def predict_seatemp(tex, lat, lon, prior_std, temptype, prior_mean=None, nens=5000): """Predict sea temperature with TEX86 Parameters ---------- tex : ndarray n-length array of TEX86 observations from a single location. lat : float Site latitude from -90 to 90. lon : float Site longitude from -180 to 180. prior_std : float Prior standard deviation for sea temperature (°C). temptype : str Type of sea temperature desired. Either 'sst' for sea-surface or 'subt'. prior_mean : float or None, optional Prior mean for sea temperature (°C). If 'None', the prior mean is found by searching for a "close" value in observed sea temperature records. nens : int Size of MCMC ensemble draws to use for calculation. Returns ------- output : Prediction Raises ------ EnsembleSizeError """ draws = get_draws(temptype) obs = get_seatemp(temptype) nd = len(tex) ntk = draws.alpha_samples_comp.shape[1] if ntk < nens: raise EnsembleSizeError(ntk, nens) if prior_mean is None: close_obs, close_dist = obs.get_close_obs(lat=lat, lon=lon) prior_mean = close_obs.mean() alpha_samples_comp, beta_samples_comp = draws.find_alphabeta_near(lat=lat, lon=lon) tau2_samples = draws.tau2_samples grid_latlon = draws.find_nearest_latlon(lat=lat, lon=lon) prior_par = {'mu': np.ones(nd) * prior_mean, 'inv_cov': np.eye(nd) * prior_std ** -2} preds = np.empty((nd, nens)) for jj in range(nens): preds[:, jj] = target_timeseries_pred(alpha_now=alpha_samples_comp[jj], beta_now=beta_samples_comp[jj], tau2_now=tau2_samples[jj], proxy_ts=tex, prior_pars=prior_par) # TODO(brews): Consider a progress bar for this loop. output = Prediction(ensemble=preds, temptype=temptype, latlon=(lat, lon), prior_mean=prior_mean, prior_std=prior_std, modelparam_gridpoints=[tuple(grid_latlon)]) return output