from variational_sampler import VariationalSampler
from variational_sampler.gaussian import Gaussian
from variational_sampler.toy_dist import ExponentialPowerLaw
from variational_sampler.display import display_fit

DIM = 1
NPTS = 100
DM = 2

target = ExponentialPowerLaw(beta=1, dim=DIM)
vs = VariationalSampler(target, (DM + target.m, target.V), NPTS)
f = vs.fit().fit
fl = vs.fit('l').fit

context = Gaussian(DM + target.m, 2 * target.V)
target2 = lambda x: target(x) + context.log(x)
vs2 = VariationalSampler(target2, context, NPTS)
f2 = vs2.fit().fit / context
fl2 = vs2.fit('l').fit / context

if DIM == 1:
    display_fit(vs.x, target, (f, f2, fl, fl2),
                ('blue', 'green', 'orange', 'red'), 
                ('VS', 'VSc', 'IS', 'ISc'))

gopt = Gaussian(target.m, target.V, Z=target.Z)
print('Error for VS: %f' % gopt.kl_div(f))
print('Error for VSc: %f' % gopt.kl_div(f2))
print('Error for IS: %f' % gopt.kl_div(fl))
print('Error for ISc: %f' % gopt.kl_div(fl2))
# Random sampling approach
vs = VariationalSampler(target, (mk, vk), NPTS)
f_kl = vs.fit()
f_l = vs.fit('l')
f_gp = vs.fit('gp', var=v)

# Deterministic sampling approch (tweak a vs object)
x, w = gauss_hermite_rule(NPTS, mk, vk)
vsd = VariationalSampler(target, (mk, vk), NPTS, x=x, w=w)
fd_kl = vsd.fit()
fd_l = vsd.fit('l')
fd_gp = vsd.fit('gp', var=v)


print('Error for VS: %f (expected: %f)'\
          % (gs_fit.kl_div(f_kl.fit), f_kl.kl_error))
print('Error for IS: %f (expected: %f)'\
           % (gs_fit.kl_div(f_l.fit), f_l.kl_error))
print('Error for BMC: %f' % gs_fit.kl_div(f_gp.fit))
print('Error for GH: %f' % gs_fit.kl_div(fd_l.fit))
print('Error for VSd: %f' % gs_fit.kl_div(fd_kl.fit))
print('Error for GP: %f' % gs_fit.kl_div(fd_gp.fit))

acronyms = ('VS', 'IS', 'BMC')
colors = ('blue', 'red', 'green')
legend = ('VS', 'direct', 'spline')
plt.figure()
display_fit(vs.x, target, (f_kl, f_l, f_gp), colors, legend, xmax=XMAX)
plt.figure()
display_fit(vsd.x, target, (fd_kl, fd_l, fd_gp), colors, legend, xmax=XMAX)
# Random sampling approach
vs = VariationalSampler(target, (mk, vk), NPTS)
f_kl = vs.fit()
f_l = vs.fit('l')
f_gp = vs.fit('gp', var=v)

# Deterministic sampling approch (tweak a vs object)
x, w = gauss_hermite_rule(NPTS, mk, vk)
vsd = VariationalSampler(target, (mk, vk), NPTS, x=x, w=w)
fd_kl = vsd.fit()
fd_l = vsd.fit('l')
fd_gp = vsd.fit('gp', var=v)


print('Error for VS: %f (expected: %f)'\
          % (gs_fit.kl_div(f_kl.fit), f_kl.kl_error))
print('Error for IS: %f (expected: %f)'\
           % (gs_fit.kl_div(f_l.fit), f_l.kl_error))
print('Error for BMC: %f' % gs_fit.kl_div(f_gp.fit))
print('Error for GH: %f' % gs_fit.kl_div(fd_l.fit))
print('Error for VSd: %f' % gs_fit.kl_div(fd_kl.fit))
print('Error for GP: %f' % gs_fit.kl_div(fd_gp.fit))

acronyms = ('VS', 'IS', 'BMC')
colors = ('blue', 'red', 'green')
legend = ('VS', 'direct', 'spline')
plt.figure()
display_fit(vs.x, target, (f_kl, f_l, f_gp), colors, legend, xmax=XMAX)
plt.figure()
display_fit(vsd.x, target, (fd_kl, fd_l, fd_gp), colors, legend, xmax=XMAX)
Beispiel #4
0
from variational_sampler import VariationalSampler
from variational_sampler.gaussian import Gaussian
from variational_sampler.toy_dist import ExponentialPowerLaw
from variational_sampler.display import display_fit

DIM = 1
NPTS = 100
DM = 2

target = ExponentialPowerLaw(beta=1, dim=DIM)
vs = VariationalSampler(target, (DM + target.m, target.V), NPTS)
f = vs.fit().fit
fl = vs.fit('l').fit

context = Gaussian(DM + target.m, 2 * target.V)
target2 = lambda x: target(x) + context.log(x)
vs2 = VariationalSampler(target2, context, NPTS)
f2 = vs2.fit().fit / context
fl2 = vs2.fit('l').fit / context

if DIM == 1:
    display_fit(vs.x, target, (f, f2, fl, fl2),
                ('blue', 'green', 'orange', 'red'), ('VS', 'VSc', 'IS', 'ISc'))

gopt = Gaussian(target.m, target.V, Z=target.Z)
print('Error for VS: %f' % gopt.kl_div(f))
print('Error for VSc: %f' % gopt.kl_div(f2))
print('Error for IS: %f' % gopt.kl_div(fl))
print('Error for ISc: %f' % gopt.kl_div(fl2))