def test_Cdo_timeseries(plot = False): raise NotImplementedError() if plot: import pylab as pl x = np.linspace(0, 40, 400).reshape((-1, 1)) y = np.sin(x) + randn(len(x)).reshape((-1, 1)) * 0.2 proc_data = np.hstack([x,y]) if plot: pl.plot(x.flatten(), y.flatten()) invec = FiniteVec(GaussianKernel(0.5), np.array([y.squeeze()[i:i+10] for i in range(190)])) outvec = FiniteVec(GaussianKernel(0.5), y[10:200]) refervec = FiniteVec(outvec.k, np.linspace(y[:-201].min() - 2, y[:-201].max() + 2, 5000)[:, None]) cd = Cdo(invec, outvec, refervec, 0.1) cd = Cmo(invec, outvec, 0.1) sol2 = np.array([(cd @ FiniteVec(invec.k, y[end-10:end].T)).normalized().get_mean_var() for end in range(200,400) ]) if plot: pl.plot(x[200:].flatten(), sol2.T[0].flatten()) invec = CombVec(FiniteVec(PeriodicKernel(np.pi, 5), x[:200,:]), SpVec(SplitDimsKernel([0,1,2],[PeriodicKernel(np.pi, 5), GaussianKernel(0.1)]), proc_data[:200,:], np.array([200]), use_subtrajectories=True), np.multiply) outvec = FiniteVec(GaussianKernel(0.5), y[1:-199]) #cd = Cdo(invec, outvec, refervec, 0.1) cd = Cmo(invec, outvec, 0.1) #sol = (cd.inp_feat.inner(SpVec(invec.k, proc_data[:230], np.array([230]), use_subtrajectories=True))) #sol = [(cd.inp_feat.inner(SiEdSpVec(invec.k_obs, y[:end], np.array([end]), invec.k_idx, use_subtrajectories=False ))) for end in range(200,400) ] #pl.plot(np.array([sol[i][-1] for i in range(len(sol))])) #sol = np.array([multiply (cd, SpVec(invec.k, proc_data[:end], np.array([end]), use_subtrajectories=False)).normalized().get_mean_var() for end in range(200,400) ]) sol = (cd @ CombVec(FiniteVec(invec.v1.k, x), SpVec(invec.v2.k, proc_data[:400], np.array([400]), use_subtrajectories=True), np.multiply)).normalized().get_mean_var() print(sol) return sol2.T[0], sol[0][200:], y[200:] (true_x1, est_x1, este_x1, true_x2, est_x2, este_x2) = [lambda samps: true_dens(np.hstack([np.repeat(x1, len(samps), 0), samps])), lambda samps: np.squeeze(inner((cd@ FiniteVec.construct_RKHS_Elem(invec.k, x1)).normalized().pos_proj().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: np.squeeze(inner((cm@ FiniteVec.construct_RKHS_Elem(invec.k, x1)).normalized().pos_proj().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: true_dens(np.hstack([np.repeat(x2, len(samps), 0), samps])), lambda samps: np.squeeze(inner((cd@ FiniteVec.construct_RKHS_Elem(invec.k, x2)).normalized().pos_proj().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: np.squeeze(inner((cm@ FiniteVec.construct_RKHS_Elem(invec.k, x2)).normalized().pos_proj().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps)))))] t = np.array((true_x1(refervec.insp_pts), true_x2(refervec.insp_pts))) e = np.array((est_x1(refervec.insp_pts), est_x2(refervec.insp_pts))) if plot: import pylab as pl (fig, ax) = pl.subplots(1, 3, False, False) ax[0].plot(refervec.insp_pts, t[0]) ax[0].plot(refervec.insp_pts, e[0], "--", label = "dens") ax[0].plot(refervec.insp_pts, este_x1(refervec.insp_pts), "-.", label = "emb") ax[1].plot(refervec.insp_pts, t[1]) ax[1].plot(refervec.insp_pts, e[1], "--", label = "dens") ax[1].plot(refervec.insp_pts, este_x2(refervec.insp_pts),"-.", label = "emb") ax[2].scatter(*rvs.T) fig.legend() fig.show() assert(np.allclose(e,t, atol=0.5))
def test_SpVecTraffic(): in_kern = PeriodicKernel(5, 1) out_kern = GaussianKernel(0.5) resh_traf = np.random.random_integers(0, 12, 20 * 20 * 2).reshape( (20, 20, 2)) resh_traf[:, :, 0] = np.arange(20) regr_inp = resh_traf[:15, :-1, :].reshape((-1, 2)) regr_out = resh_traf[:15, 1:, :].reshape((-1, 2))[:, 1:2] un, sr = SparseReduce.sum_from_unique(regr_out.flatten()) num_points_per_obs = resh_traf.shape[1] - 1 inp_vec = SpVec(in_kern, regr_inp, np.arange(1, regr_inp.shape[0] // num_points_per_obs + 1) * num_points_per_obs, gram_reduce=sr) out_vec = FiniteVec( out_kern, un[:, np.newaxis], ) O = Cmo(inp_vec, out_vec) test_inp = resh_traf[15:, :10, :].reshape((-1, 2)) invec_inf = SpVec( in_kern, test_inp, np.array([test_inp.shape[0]]), use_subtrajectories=False) #initial observed trajectory/warmup print(len(O @ invec_inf)) cur_ro = RolloutSpVec(O, invec_inf, 1) rval = [] for i in tqdm(range(1, 10)): new_point = cur_ro.current_outp_emb.point_representant() rval.append(new_point) cur_ro.update(new_point)
def test_Cdmo(plot = False): cent_vals = [True, False] site_vals = [0., 1.] def generate_donut(nmeans = 10, nsamps_per_mean = 50): from scipy.stats import multivariate_normal from numpy import exp def pol2cart(theta, rho): x = (rho * np.cos(theta)).reshape(-1,1) y = (rho * np.sin(theta)).reshape(-1,1) return np.concatenate([x, y], axis = 1) comp_distribution = multivariate_normal(np.zeros(2), np.eye(2)/100) means = pol2cart(np.linspace(0,2*3.141, nmeans + 1)[:-1], 1) rvs = comp_distribution.rvs(nmeans * nsamps_per_mean) + np.repeat(means, nsamps_per_mean, 0) true_dens = lambda samps: exp(location_mixture_logpdf(samps, means, np.ones(nmeans) / nmeans, comp_distribution)) return rvs, means, true_dens x_vals = [np.zeros((1,1)) + i for i in site_vals] (rvs, means, true_dens) = generate_donut(500, 10) regul = CovOp.regul(1, len(rvs)) # we will look at 1 point inputs invec = FiniteVec(GenGaussKernel(0.3, 1.7), rvs[:, :1]) outvec = FiniteVec(GenGaussKernel(0.3, 1.7), rvs[:, 1:]) refervec = FiniteVec(outvec.k, np.linspace(-4, 4, 10000)[:, None]) C_ref = CovOp(refervec) maps = {} for center in cent_vals: cm = Cmo(invec, outvec, regul, center = center) maps[center] = {"emb":cm, "dens":C_ref.solve(cm)} print(np.abs(cm.const_cent_term - maps[center]["dens"].const_cent_term).max()) ests = {map_type: {cent: np.array([maps[cent][map_type](x).dens_proj()(refervec.insp_pts).squeeze() for x in x_vals]) for cent in cent_vals} for map_type in ["emb", "dens"]} t = np.array([true_dens(np.hstack([np.repeat(x, len(refervec.insp_pts), 0), refervec.insp_pts])) for x in x_vals]) if plot: import matplotlib.pyplot as plt (fig, ax) = plt.subplots(len(site_vals) + 1, 1, False, False) for i, site in enumerate(site_vals): ax[i].plot(refervec.insp_pts, t[i], linewidth=2, color="b", label = "true dens", alpha = 0.5) for map_type in ["emb", "dens"]: for cent in cent_vals: if map_type == "emb": color = "r" else: color = "g" if cent == True: style = ":" else: style= "--" ax[i].plot(refervec.insp_pts, ests[map_type][cent][i], style, color = color, label = map_type+" "+("cent" if cent else "unc"), alpha = 0.5) ax[-1].scatter(*rvs.T) fig.legend() fig.show() for cent in cent_vals: assert(np.allclose(ests["dens"][cent],t, atol=0.5))
def test_Cdmo(plot=False): def generate_donut(nmeans=10, nsamps_per_mean=50): from scipy.stats import multivariate_normal from numpy import exp def pol2cart(theta, rho): x = (rho * np.cos(theta)).reshape(-1, 1) y = (rho * np.sin(theta)).reshape(-1, 1) return np.concatenate([x, y], axis=1) comp_distribution = multivariate_normal(np.zeros(2), np.eye(2) / 100) means = pol2cart(np.linspace(0, 2 * 3.141, nmeans + 1)[:-1], 1) rvs = comp_distribution.rvs(nmeans * nsamps_per_mean) + np.repeat( means, nsamps_per_mean, 0) true_dens = lambda samps: exp( location_mixture_logpdf(samps, means, np.ones(nmeans) / nmeans, comp_distribution )) return rvs, means, true_dens x1 = np.ones((1, 1)) x2 = np.zeros((1, 1)) (rvs, means, true_dens) = generate_donut(50, 10) invec = FiniteVec(GaussianKernel(0.5), rvs[:, :1]) outvec = FiniteVec(GaussianKernel(0.5), rvs[:, 1:]) refervec = FiniteVec(outvec.k, np.linspace(-4, 4, 5000)[:, None]) cd = Cdo(invec, outvec, refervec, 0.1) cm = Cmo(invec, outvec, 0.1) (true_x1, est_x1, este_x1, true_x2, est_x2, este_x2) = [ lambda samps: true_dens( np.hstack([np.repeat(x1, len(samps), 0), samps])), lambda samps: np.squeeze( inner( multiply(cd, FiniteVec.construct_RKHS_Elem(invec.k, x1)). normalized().unsigned_projection().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: np.squeeze( inner( multiply(cm, FiniteVec.construct_RKHS_Elem(invec.k, x1)). normalized().unsigned_projection().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: true_dens( np.hstack([np.repeat(x2, len(samps), 0), samps])), lambda samps: np.squeeze( inner( multiply(cd, FiniteVec.construct_RKHS_Elem(invec.k, x2)). normalized().unsigned_projection().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))), lambda samps: np.squeeze( inner( multiply(cm, FiniteVec.construct_RKHS_Elem(invec.k, x2)). normalized().unsigned_projection().normalized(), FiniteVec(refervec.k, samps, prefactors=np.ones(len(samps))))) ] t = np.array( (true_x1(refervec.inspace_points), true_x2(refervec.inspace_points))) e = np.array( (est_x1(refervec.inspace_points), est_x2(refervec.inspace_points))) if plot: import pylab as pl (fig, ax) = pl.subplots(1, 3, False, False) ax[0].plot(refervec.inspace_points, t[0]) ax[0].plot(refervec.inspace_points, e[0], "--", label="dens") ax[0].plot(refervec.inspace_points, este_x1(refervec.inspace_points), "-.", label="emb") ax[1].plot(refervec.inspace_points, t[1]) ax[1].plot(refervec.inspace_points, e[1], "--", label="dens") ax[1].plot(refervec.inspace_points, este_x2(refervec.inspace_points), "-.", label="emb") ax[2].scatter(*rvs.T) fig.legend() fig.show() assert (np.allclose(e, t, atol=0.5))