Esempio n. 1
0
def test_dist():
    a, b = randn(100, 3), randn(200, 3)
    print("Testing distance computation in input space, i.e. standard arrays",
          file=sys.stderr)
    for (name, pow) in [("sqeuclidean", 2), ("euclidean", 1)]:
        for dist_args in [(a, b), (a, a)]:
            ground_truth = cdist(*dist_args, name)
            d = dist(*dist_args, power=pow)
            print("*", name, pow, np.abs(ground_truth - d).max())
            assert (allclose(ground_truth, d, atol=1e-05))

    print("Testing distance computation between vectors of RKHS elements",
          file=sys.stderr)
    k = LinearKernel()
    va, vb = FiniteVec(k, a), FiniteVec(k, b)
    for pow in [2, 1]:
        for insp_args, dist_args in [[(a, b), (va, vb)], [(a, a), (va, va)]]:
            lin_rkhsdist_sq = (
                np.diag(insp_args[0] @ insp_args[0].T)[:, np.newaxis] -
                2 * insp_args[0] @ insp_args[1].T +
                np.diag(insp_args[1] @ insp_args[1].T)[np.newaxis, :])
            ground_truth = np.power(lin_rkhsdist_sq, pow / 2.)
            d = dist(*dist_args, power=pow)
            print("*", pow, np.abs(ground_truth - d).max())
            assert (allclose(ground_truth, d, atol=1e-05))
Esempio n. 2
0
def test_multiply():
    x = np.linspace(-2.5, 15, 5)[:, np.newaxis].astype(np.float32)
    y = randn(x.size)[:, np.newaxis].astype(np.float32)

    gk_x = GaussianKernel(0.1)

    x_e1 = FiniteVec.construct_RKHS_Elem(gk_x, x)
    x_e2 = FiniteVec.construct_RKHS_Elem(gk_x, y)
    x_fv = FiniteVec(gk_x,
                     np.vstack([x, y]),
                     prefactors=np.hstack([x_e1.prefactors] * 2),
                     points_per_split=x.size)

    oper_feat_vec = FiniteVec(gk_x, x)

    oper = FiniteOp(oper_feat_vec, oper_feat_vec, np.eye(len(x)))
    res_e1 = multiply(oper, x_e1)
    res_e2 = multiply(oper, x_e2)
    res_v = multiply(oper, x_fv)
    assert np.allclose(
        res_e1.prefactors,
        (oper.matr @ oper.inp_feat.inner(x_e1)
         ).flatten()), "Application of operator to RKHS element failed."
    assert np.allclose(
        res_v.inspace_points,
        np.vstack([res_e1.inspace_points, res_e2.inspace_points])
    ), "Application of operator to all vectors in RKHS vector failed at inspace points."
    assert np.allclose(
        res_v.prefactors, np.hstack([
            res_e1.prefactors, res_e2.prefactors
        ])), "Application of operator to all vectors in RKHS vector failed."
    assert np.allclose(
        multiply(oper, oper).matr, oper.inp_feat.inner(
            oper.outp_feat)), "Application of operator to operator failed."
Esempio n. 3
0
def frank_wolfe_fx(element: FiniteVec, num_samples: np.int32 = 100):
    assert (len(element) == 1)
    #key = PRNGKey(np.int32(time()))
    solution = FiniteVec(element.k,
                         element.inspace_points[:1, :],
                         np.zeros(1),
                         points_per_split=1)
    for k in range(num_samples):

        def cost(x):
            x = x.reshape((1, -1))
            return (solution(x) - element(x)).sum()

        g_cost = grad(cost)
        idx = randint(0, element.points_per_split - 1)
        cand = element.inspace_points[idx:idx + 1, :]
        #print(cand)
        #print(cost(cand), grad(cost)(cand))
        res = minimize(__casted_output(cost),
                       cand,
                       jac=__casted_output(g_cost))
        solution.inspace_points = np.vstack(
            [solution.inspace_points, res["x"]])
        gamma_k = 1. / (k + 1)
        solution.prefactors = np.hstack([(1 - gamma_k) * solution.prefactors,
                                         gamma_k])
        solution.points_per_split = solution.points_per_split + 1
    return solution
Esempio n. 4
0
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)
Esempio n. 5
0
def test_pos_proj(kernel):
    vecs = [
        FiniteVec(kernel, np.array([(1., ), (-1., ), (-1., ), (1., )]), [
            LinearReduce(
                np.array([-1. / 3, -0.7, 1, 2. / 3]).reshape((1, -1)))
        ]),
        FiniteVec(kernel, np.array([(2., ), (2., ), (5., ), (5, )]),
                  [LinearReduce(np.array([-5, 10, -1, 1.5]).reshape(
                      (1, -1)))]),
    ]
    x = np.linspace(-10, 10, 1000)[:, None]
    for v in vecs:
        assert np.allclose(
            v(x), v.pos_proj()(x), atol=5e-5
        ), "Error tolerance not met for positive projection of RKHS vector, maximum absolute error was " + str(
            np.abs(v(x) - v.pos_proj()(x)).max())
Esempio n. 6
0
def test_FiniteMap():
    gk_x = GaussianKernel(0.1)
    x = np.linspace(-2.5, 15, 20)[:, np.newaxis].astype(np.float32)
    #x = np.random.randn(20, 1).astype(np.float)
    ref_fvec = FiniteVec(gk_x, x, np.ones(len(x)))
    ref_elem = FiniteVec.construct_RKHS_Elem(gk_x, x, np.ones(len(x)))

    C1 = FiniteMap(ref_fvec, ref_fvec, np.linalg.inv(inner(ref_fvec)))
    assert(np.allclose((C1 @ ref_elem).prefactors, 1.))

    C2 = FiniteMap(ref_fvec, ref_fvec, [email protected])
    assert(np.allclose((C2 @ ref_elem).prefactors, np.sum(C1.matr, 0)))

    n_rvs = 50
    rv_fvec = FiniteVec(gk_x, random.normal(rng, (n_rvs, 1)) * 5, np.ones(n_rvs))
    C3 = FiniteMap(rv_fvec, rv_fvec, np.eye(n_rvs))
    assert np.allclose((C3 @ C1).matr, gk_x(rv_fvec.insp_pts, ref_fvec.insp_pts) @ C1.matr, 0.001, 0.001)
Esempio n. 7
0
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))
Esempio n. 8
0
def test_point_representant(kernel):
    vec = FiniteVec(kernel, np.array([
        (0., ), (1., ), (0., ), (1., )
    ]), [LinearReduce(np.array([0, 0, 1. / 3, 2. / 3]).reshape((1, -1)))])
    assert vec.point_representant("inspace_point") == 1.
    assert vec.point_representant("mean") == 2. / 3
    vec = FiniteVec(kernel, np.array([(0., ), (1., ), (0., ), (1., )]), [
        LinearReduce(
            np.array([0.4, 0.6, 0, 0, 0, 0, 1. / 3, 2. / 3]).reshape((2, -1)))
    ])
    assert np.allclose(
        vec.point_representant("inspace_point").squeeze(), np.array([1., 1.]))
    assert np.allclose(
        vec.point_representant("mean").squeeze(), np.array([.6, 2. / 3]))
Esempio n. 9
0
def test_Mean_var(D = 1, kernel = kernel_setups[0]):
    N = 4

   
    el = FiniteVec.construct_RKHS_Elem(kernel, np.array([(0.,), (1.,)]), prefactors=np.ones(2)/2)
    for pref in [el.prefactors, 2*el.prefactors]:
        el.prefactors = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 0.5)
        assert np.allclose(v, kernel.var + 0.5 - m**2)
    
    el = FiniteVec.construct_RKHS_Elem(kernel, np.array([(0.,), (1.,)]), prefactors=np.array([1./3, 2./3]))
    for pref in [el.prefactors, 2*el.prefactors]:
        el.prefactors = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 2./3)
        assert np.allclose(v, kernel.var + 2./3 - m**2)
    
    el = FiniteVec.construct_RKHS_Elem(kernel, np.array([(0.,), (1.,), (2., )]), prefactors=np.array([0.2, 0.5, 0.3]))
    for pref in [el.prefactors, 2*el.prefactors]:
        el.prefactors = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 1.1)
        assert np.allclose(v, kernel.var + 0.5 + 0.3*4 - m**2)
    
    vec = FiniteVec(kernel, np.array([(0.,), (1.,), (0.,), (1.,)]), prefactors=np.array([0.5, 0.5, 1./3, 2./3]), points_per_split=2)
    m, v = vec.normalized().get_mean_var()
    assert np.allclose(m.flatten(), np.array([0.5, 2./3]))
    assert np.allclose(v.flatten(), kernel.var + np.array([0.5, 2./3]) - m.flatten()**2)
Esempio n. 10
0
def test_FiniteOp():
    gk_x = GaussianKernel(0.1)
    x = np.linspace(-2.5, 15, 20)[:, np.newaxis].astype(np.float)
    #x = np.random.randn(20, 1).astype(np.float)
    ref_fvec = FiniteVec(gk_x, x, np.ones(len(x)))
    ref_elem = FiniteVec.construct_RKHS_Elem(gk_x, x, np.ones(len(x)))

    C1 = FiniteOp(ref_fvec, ref_fvec, np.linalg.inv(inner(ref_fvec)))
    assert (np.allclose(multiply(C1, ref_elem).prefactors, 1.))

    C2 = FiniteOp(ref_fvec, ref_fvec, C1.matr @ C1.matr)
    assert (np.allclose(multiply(C2, ref_elem).prefactors, np.sum(C1.matr, 0)))

    n_rvs = 50
    rv_fvec = FiniteVec(gk_x,
                        np.random.randn(n_rvs).reshape((-1, 1)) * 5,
                        np.ones(n_rvs))
    C3 = FiniteOp(rv_fvec, rv_fvec, np.eye(n_rvs))
    assert (np.allclose(
        multiply(C3, C1).matr,
        gk_x(rv_fvec.inspace_points, ref_fvec.inspace_points) @ C1.matr),
            0.001, 0.001)
Esempio n. 11
0
def test_CovOp(plot = False, center = False):   
    from scipy.stats import multivariate_normal

    nsamps = 1000
    samps_unif = None
    regul_C_ref=0.0001
    D = 1
    import pylab as pl
    if samps_unif is None:
        samps_unif = nsamps
    gk_x = GaussianKernel(0.2)

    targ = mixt(D, [multivariate_normal(3*np.ones(D), np.eye(D)*0.7**2), multivariate_normal(7*np.ones(D), np.eye(D)*1.5**2)], [0.5, 0.5])
    out_samps = targ.rvs(nsamps).reshape([nsamps, 1]).astype(float)
    out_fvec = FiniteVec(gk_x, out_samps, np.ones(nsamps), center = center)
    out_meanemb = out_fvec.sum()
    

    x = np.linspace(-2.5, 15, samps_unif)[:, np.newaxis].astype(float)
    ref_fvec = FiniteVec(gk_x, x, np.ones(len(x)), center = center)
    ref_elem = ref_fvec.sum()

    C_ref = CovOp(ref_fvec, regul=0., center = center) # CovOp_compl(out_fvec.k, out_fvec.inspace_points, regul=0.)

    inv_Gram_ref = np.linalg.inv(inner(ref_fvec))

    C_samps = CovOp(out_fvec, regul=regul_C_ref, center = center)
    unif_obj = C_samps.solve(out_meanemb).dens_proj()
    C_ref = CovOp(ref_fvec, regul=regul_C_ref, center = center)
    dens_obj = C_ref.solve(out_meanemb).dens_proj()
    


    targp = np.exp(targ.logpdf(ref_fvec.insp_pts.squeeze())).squeeze()
    estp = np.squeeze(inner(dens_obj, ref_fvec))
    estp2 = np.squeeze(inner(dens_obj, ref_fvec))
    est_sup = unif_obj(x).squeeze()
    assert (np.abs(targp.squeeze()-estp).mean() < 0.8), "Estimated density strongly deviates from true density"
    if plot:
        pl.plot(ref_fvec.insp_pts.squeeze(), estp/np.max(estp) * np.max(targp), "b--", label="scaled estimate")
        pl.plot(ref_fvec.insp_pts.squeeze(), estp2/np.max(estp2) * np.max(targp), "g-.", label="scaled estimate (uns)")
        pl.plot(ref_fvec.insp_pts.squeeze(), targp, label = "truth")
        pl.plot(x.squeeze(), est_sup.squeeze(), label = "support")
        
        #pl.plot(ref_fvec.inspace_points.squeeze(), np.squeeze(inner(unif_obj, ref_fvec)), label="unif")
        pl.legend(loc="best")
        pl.show()
    supp = unif_obj(x).squeeze()
    assert (np.std(supp) < 0.15), "Estimated support has high variance, in data points, while it should be almost constant."
Esempio n. 12
0
def test_Mean_var(D, kernel):
    N = 4

    el = FiniteVec.construct_RKHS_Elem(kernel,
                                       np.array([(0., ), (1., )]),
                                       prefactors=np.ones(2) / 2)
    for pref in [el.reduce[0].linear_map, 2 * el.reduce[0].linear_map]:
        el.reduce[0].linear_map = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 0.5)
        assert np.allclose(v, kernel.var + 0.5 - m**2)

    el = FiniteVec.construct_RKHS_Elem(kernel,
                                       np.array([(0., ), (1., )]),
                                       prefactors=np.array([1. / 3, 2. / 3]))
    for pref in [el.reduce[0].linear_map, 2 * el.reduce[0].linear_map]:
        el.reduce[0].linear_map = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 2. / 3)
        assert np.allclose(v, kernel.var + 2. / 3 - m**2)

    el = FiniteVec.construct_RKHS_Elem(kernel,
                                       np.array([(0., ), (1., ), (2., )]),
                                       prefactors=np.array([0.2, 0.5, 0.3]))
    for pref in [el.reduce[0].linear_map, 2 * el.reduce[0].linear_map]:
        el.reduce[0].linear_map = pref
        m, v = el.normalized().get_mean_var()
        #print(m,v)
        assert np.allclose(m, 1.1)
        assert np.allclose(v, kernel.var + 0.5 + 0.3 * 4 - m**2)

    vec = FiniteVec(kernel, np.array([(0., ), (1., ), (0., ), (1., )]), [
        LinearReduce(
            np.array([0.5, 0.5, 0, 0, 0, 0, 1. / 3, 2. / 3]).reshape((2, -1)))
    ])
    m, v = vec.normalized().get_mean_var()
    assert np.allclose(m.flatten(), np.array([0.5, 2. / 3]))
    assert np.allclose(v.flatten(),
                       kernel.var + np.array([0.5, 2. / 3]) - m.flatten()**2)
Esempio n. 13
0
def test_FiniteVec(D, kernel, N):
    X = rng.randn(N, D)
    rv = FiniteVec(kernel, X, [Prefactors(np.ones(len(X)).astype(np.float32))])
    rv2 = FiniteVec.construct_RKHS_Elem(kernel, rng.randn(N + 1, D),
                                        np.ones(N + 1).astype(np.float32))
    assert np.allclose(
        inner(rv, rv),
        rv.k(rv.inspace_points, rv.inspace_points) *
        np.outer(rv.reduce[0].prefactors, rv.reduce[0].prefactors)
    ), "Simple vector computation not accurate"
    assert np.allclose(
        inner(rv, rv2),
        (rv.k(rv.inspace_points, rv2.inspace_points) *
         np.outer(rv.reduce[0].prefactors, rv2.reduce[0].linear_map)).sum(
             1, keepdims=True)), "Simple vector computation not accurate"

    N = 4
    X = rng.randn(N, D)

    rv = FiniteVec(kernel, X,
                   [Prefactors(np.ones(len(X)) / 2),
                    BalancedSum(2)])
    el = FiniteVec.construct_RKHS_Elem(kernel, X, np.ones(N))
    gram = el.k(el.inspace_points)
    assert np.allclose(inner(el, el), np.sum(gram))
    assert np.allclose(np.squeeze(inner(el, rv)),
                       np.sum(gram, 1).reshape(-1, 2).mean(1))

    rv = FiniteVec(kernel, X,
                   [Prefactors(np.ones(len(X)) / 2),
                    BalancedSum(2)])
    assert np.allclose(
        inner(rv, rv),
        np.array([[np.mean(rv.k(X[:2, :])),
                   np.mean(rv.k(X[:2, :], X[2:, :]))],
                  [np.mean(rv.k(X[:2, :], X[2:, :])),
                   np.mean(rv.k(X[2:, :]))]
                  ])), "Balanced vector computation not accurate"

    vec = FiniteVec(kernel, np.array([(0., ), (1., ), (0., ), (1., )]), [
        LinearReduce(
            np.array([0.5, 0.5, 0, 0, 0, 0, 1. / 3, 2. / 3]).reshape((2, -1)))
    ])
    m, v = vec.normalized().get_mean_var()
    assert np.allclose(m.flatten(), np.array([0.5, 2. / 3]))
    assert np.allclose(v.flatten(),
                       kernel.var + np.array([0.5, 2. / 3]) - m.flatten()**2)
Esempio n. 14
0
def test_FiniteVec(D = 1, kernel = kernel_setups[0], N = 10):
    X = rng.randn(N, D)
    rv = FiniteVec(kernel, X, np.ones(len(X)).astype(np.float32))
    rv2 = FiniteVec.construct_RKHS_Elem(kernel, rng.randn(N + 1, D), np.ones(N + 1).astype(np.float32))
    assert np.allclose(inner(rv, rv), rv.k(rv.inspace_points, rv.inspace_points)*np.outer(rv.prefactors, rv.prefactors)) , "Simple vector computation not accurate"
    assert np.allclose(inner(rv, rv2), (rv.k(rv.inspace_points, rv2.inspace_points)*np.outer(rv.prefactors, rv2.prefactors)).sum(1, keepdims=True)), "Simple vector computation not accurate"

    N = 4
    X = rng.randn(N, D)

    rv = FiniteVec(kernel, X, np.ones(len(X))/2, points_per_split = 2)
    el = FiniteVec.construct_RKHS_Elem(kernel, X, prefactors=np.ones(N))
    gram = el.k(el.inspace_points)
    assert np.allclose(inner(el, el), np.sum(gram))
    assert np.allclose(np.squeeze(inner(el, rv)), np.sum(gram, 1).reshape(-1,2).mean(1))


    rv = FiniteVec(kernel, X, np.ones(len(X))/2, points_per_split = 2)
    assert np.allclose(inner(rv, rv), np.array([[np.mean(rv.k(X[:2,:])), np.mean(rv.k(X[:2,:], X[2:,:]))],
                                               [np.mean(rv.k(X[:2,:], X[2:,:])), np.mean(rv.k(X[2:,:]))]])), "Balanced vector computation not accurate"
    
    vec = FiniteVec(kernel, np.array([(0.,), (1.,), (0.,), (1.,)]), prefactors=np.array([0.5, 0.5, 1./3, 2./3]), points_per_split=2)
    m, v = vec.normalized().get_mean_var()
    assert np.allclose(m.flatten(), np.array([0.5, 2./3]))
    assert np.allclose(v.flatten(), kernel.var + np.array([0.5, 2./3]) - m.flatten()**2)
Esempio n. 15
0
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))
Esempio n. 16
0
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))
Esempio n. 17
0
def test_CovOp(plot=False):
    from scipy.stats import multivariate_normal

    nsamps = 1000
    samps_unif = None
    regul_C_ref = 0.0001
    D = 1
    import pylab as pl
    if samps_unif is None:
        samps_unif = nsamps
    gk_x = GaussianKernel(0.2)

    targ = mixt(D, [
        multivariate_normal(3 * np.ones(D),
                            np.eye(D) * 0.7**2),
        multivariate_normal(7 * np.ones(D),
                            np.eye(D) * 1.5**2)
    ], [0.5, 0.5])
    out_samps = targ.rvs(nsamps).reshape([nsamps, 1]).astype(float)
    out_fvec = FiniteVec(gk_x, out_samps, np.ones(nsamps))

    #gk_x = LaplaceKernel(3)
    #gk_x = StudentKernel(0.7, 15)
    x = np.linspace(-2.5, 15, samps_unif)[:, np.newaxis].astype(float)
    ref_fvec = FiniteVec(gk_x, x, np.ones(len(x)))
    ref_elem = ref_fvec.sum()

    C_ref = CovOp(
        ref_fvec,
        regul=0.)  # CovOp_compl(out_fvec.k, out_fvec.inspace_points, regul=0.)

    inv_Gram_ref = np.linalg.inv(inner(ref_fvec))
    assert (np.allclose((inv_Gram_ref @ inv_Gram_ref) / C_ref.inv().matr,
                        1.,
                        atol=1e-3))
    #assert(np.allclose(multiply(C_ref.inv(), ref_elem).prefactors, np.sum(np.linalg.inv(inner(ref_fvec)), 0), rtol=1e-02))

    C_samps = CovOp(out_fvec, regul=regul_C_ref)
    unif_obj = multiply(
        C_samps.inv(),
        FiniteVec.construct_RKHS_Elem(out_fvec.k, out_fvec.inspace_points,
                                      out_fvec.prefactors).normalized())
    C_ref = CovOp(ref_fvec, regul=regul_C_ref)
    dens_obj = multiply(
        C_ref.inv(),
        FiniteVec.construct_RKHS_Elem(out_fvec.k, out_fvec.inspace_points,
                                      out_fvec.prefactors)).normalized()

    #dens_obj.prefactors = np.sum(dens_obj.prefactors, 1)
    #dens_obj.prefactors = dens_obj.prefactors / np.sum(dens_obj.prefactors)
    #print(np.sum(dens_obj.prefactors))
    #p = np.sum(inner(dens_obj, ref_fvec), 1)
    targp = np.exp(targ.logpdf(ref_fvec.inspace_points.squeeze())).squeeze()
    estp = np.squeeze(inner(dens_obj, ref_fvec))
    estp2 = np.squeeze(
        inner(dens_obj.unsigned_projection().normalized(), ref_fvec))
    assert (np.abs(targp.squeeze() - estp).mean() < 0.8)
    if plot:
        pl.plot(ref_fvec.inspace_points.squeeze(),
                estp / np.max(estp) * np.max(targp),
                "b--",
                label="scaled estimate")
        pl.plot(ref_fvec.inspace_points.squeeze(),
                estp2 / np.max(estp2) * np.max(targp),
                "g-.",
                label="scaled estimate (uns)")
        pl.plot(ref_fvec.inspace_points.squeeze(), targp, label="truth")

        #pl.plot(ref_fvec.inspace_points.squeeze(), np.squeeze(inner(unif_obj, ref_fvec)), label="unif")
        pl.legend(loc="best")
        pl.show()
    assert (np.std(np.squeeze(inner(unif_obj.normalized(), out_fvec))) < 0.15)