def test_rkhs_dens_and_operators(D=1, nsamps=200): targ = dist.mixt(D, [ dist.mvnorm(3 * np.ones(D), np.eye(D) * 0.7**2), dist.mvnorm(7 * np.ones(D), np.eye(D) * 1.5**2) ], [0.5, 0.5]) out_samps = targ.rvs(nsamps) gk_x = GaussianKernel(0.7) de = RKHSDensityEstimator(out_samps, gk_x, 0.1) x = np.linspace(-1, 12, 200) pl.figure() pl.plot(x, exp(targ.logpdf(x)), 'k-', label='truth') pl.plot(x, de.eval_rkhs_density_approx(x[:, None]), 'b--', label='Density estimate') pl.plot(x, de.eval_kme(x[:, None]), 'r:', label='KDE/Kernel mean embedding') pl.legend(loc='best') pl.savefig('Density_estimation_(preimage_of_KDE).pdf') inp_samps = (out_samps - 5)**2 + np.random.randn(*out_samps.shape) gk_y = GaussianKernel(1) cme = ConditionMeanEmbedding(inp_samps, out_samps, gk_y, gk_x, 5) cdo = ConditionDensityOperator(inp_samps, out_samps, gk_y, gk_x, 5, 5) (fig, ax) = pl.subplots(3, 1, True, False, figsize=(10, 10)) ax[2].scatter(out_samps, inp_samps, alpha=0.3) ax[2].axhline(0, 0, 8, color='r', linestyle='--') ax[2].axhline(5, 0, 8, color='r', linestyle='--') ax[2].set_title("Input: y, output: x, %d pairs" % nsamps) ax[2].set_yticks((0, 5)) d = cdo.lhood(np.array([[0.], [5.]]), x[:, None]).T e = cme.lhood(np.array([[0.], [5.]]), x[:, None]).T assert (d.shape[0] == 2) assert (np.allclose(d[0], cdo.lhood(0, x[:, None]))) assert (np.allclose(d[1], cdo.lhood(5, x[:, None]))) # assert() ax[1].plot(x, d[1], '-', label='cond. density') ax[1].plot(x, e[1], '--', label='cond. mean emb.') ax[1].set_title("p(x|y=5)") ax[0].plot(x, d[0], '-', label='cond. density') ax[0].plot(x, e[0], '--', label='cond. mean emb.') ax[0].set_title("p(x|y=0)") ax[0].legend(loc='best') fig.show() fig.savefig("conditional_density_operator.pdf")
from numpy import exp, log, sqrt from scipy.misc import logsumexp from autograd import grad, hessian import distributions as dist #import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt x = np.linspace(-2, 8, 1000) sp.random.seed(5) targd = dist.mixt(1, [ dist.mvnorm(np.ones(1) + 1, np.ones(1)), dist.mvnorm(np.ones(1) + 3.8, np.ones(1)) ], [0.8, 0.2]) qrw = dist.mvnorm(np.zeros(1), np.ones(1) * 0.4) qind = dist.mvnorm(np.ones(1) * 2, np.ones(1) * 0.2) fig, ax = plt.subplots(figsize=(4, 2)) ax.plot(x, exp(targd.logpdf(x)) / exp(targd.logpdf(x)).max(), label=r'$\pi$', linewidth=2) for i in range(3): current = targd.rvs().flatten() ax.plot(x, exp(qrw.logpdf(x - current)) / exp(qrw.logpdf(x - current)).max() / 2,
import scipy.stats as stats from numpy import exp, log, sqrt from scipy.misc import logsumexp from autograd import grad, hessian import distributions as dist #import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt x = np.linspace(-2, 8, 1000) targd = dist.mixt(1, [ dist.mvnorm(np.ones(1) + 2, np.ones(1)), dist.mvnorm(np.ones(1) + 3.8, np.ones(1)) ], [0.7, 0.3]) g = grad(targd.logpdf) h = hessian(targd.logpdf) res = sp.optimize.minimize_scalar(lambda x: -targd.logpdf(x)) #maximum = 3.44515 maximum = res['x'] print("Gradient at Maximum logpdf ", g(maximum)) #mpl.style.use('seaborn') fig, ax = plt.subplots(figsize=(5, 3)) ax.plot(x, exp(targd.logpdf(x)), label='target density', linewidth=2) ax.plot(x, exp(dist.mvnorm(maximum, 1. / -h(maximum)).logpdf(x)), '--', label='Gaussian approx',
from numpy import exp, log, sqrt from scipy.misc import logsumexp from autograd import grad, hessian import distributions as dist #import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt x = np.linspace(-2, 8, 1000) sp.random.seed(2) targd = dist.mixt(1, [ dist.mvnorm(np.ones(1), np.ones(1)), dist.mvnorm(np.ones(1) + 3.8, np.ones(1)) ], [0.7, 0.3]) q0 = dist.mvnorm(np.ones(1) + 3, np.ones(1) * 2) samps = q0.rvs(20) lw = targd.logpdf(samps).flatten() - q0.logpdf(samps) lw = lw - logsumexp(lw) q1 = dist.mixt(1, [dist.mvnorm(mu, np.ones(1)) for mu in samps], lw.flatten(), comp_w_in_logspace=True) fig, ax = plt.subplots(figsize=(6, 3)) ax.plot(x, exp(targd.logpdf(x)), label='target density', linewidth=2) ax.plot(x, exp(q0.logpdf(x)), '-.', label='q0', linewidth=2) ax.plot(x, exp(q1.logpdf(x)), '--', label='q1', linewidth=2) ax.legend(loc='best') ax.set_xticks([]) ax.set_yticks([])
from numpy import exp, log, sqrt from scipy.misc import logsumexp from autograd import grad, hessian import distributions as dist #import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt x = np.linspace(-2, 8, 1000) sp.random.seed(2) targd = dist.mixt(1, [ dist.mvnorm(np.ones(1) + 1, np.ones(1)), dist.mvnorm(np.ones(1) + 3.8, np.ones(1)) ], [0.8, 0.2]) q0 = dist.mvnorm(np.ones(1), np.ones(1) * 3) q1 = dist.mvnorm(np.ones(1), np.ones(1) * 0.1) fig, ax = plt.subplots(figsize=(6, 3)) ax.plot(x, exp(targd.logpdf(x)) / exp(targd.logpdf(x)).max(), label='target density', linewidth=2) ax.plot(x, exp(q0.logpdf(x)) / exp(q0.logpdf(x)).max() / 2, '-.', label='q_0(.|1)', linewidth=2) ax.plot(x,