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."
Beispiel #2
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)