Skip to content

Multivariate Kernel Density Estimation #83

Open
@AnnaCraig

Description

@AnnaCraig

Hello,

I am trying to use Chaospy to perform advanced sampling of a multivariate KDE generated via sm.nonparametric.KDEMultivariate. Unfortunately, I am not able to defined the KDE as a custom distribution in such a way that I am then able to call the sample operation. Could someone help me understand how I might be able to accomplish this, if it can be done at all?

Any help would be deeply appreciated!

import numpy as np
import statsmodels.api as sm
import chaospy as cp

data = np.array([[ 0.64206175,  0.49193947, -1.47253426],
       [-0.94203536, -1.16952506,  0.99483489],
       [-0.53587827, -0.29164457,  0.6460487 ],
       [ 0.43096139, -0.59982553, -0.01857683],
       [ 0.52384554, -0.54059551,  0.11118488],
       [-0.65789427,  0.15139668,  0.09203062],
       [ 0.2663031 , -0.4434035 , -0.16632009],
       [ 0.08475679, -0.0661517 , -0.34676888],
       [-0.79848711, -1.63556875,  0.52249545],
       [-1.18427302, -1.33508375,  1.12194074],
       [ 0.18819597, -0.84600592, -0.03214548]])

kde = sm.nonparametric.KDEMultivariate(data=data, var_type='ccc', bw='cv_ml')

cp_kde = cp.construct(cdf=lambda self, q: mode1_kde.cdf(q), 
                     bnd=lambda self: [(-1.82392181, 9.81120074),
                                         (-8.63429993, 3.11334478),
                                         ( -4.74632693, 2.08811487)])()

cp_kde.sample(4, 'L')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-9984df9b1b05> in <module>()
     22                                          ( -4.74632693, 2.08811487)])()
     23 
---> 24 cp_kde.sample(4, 'L')

/home/<>/lib/python2.7/site-packages/chaospy/distributions/baseclass.pyc in sample(self, size, rule, antithetic, verbose, **kws)
    287         from . import sampler
    288         out = sampler.generator.generate_samples(
--> 289             order=size_, domain=self, rule=rule, antithetic=antithetic)
    290         try:
    291             out = out.reshape(shape)

/home/<>/lib/python2.7/site-packages/chaospy/distributions/sampler/generator.pyc in generate_samples(order, domain, rule, antithetic)
    137     assert rule in SAMPLERS, "rule not recognised"
    138     sampler = SAMPLERS[rule]
--> 139     x_data = trans(sampler(order=order, dim=dim))
    140 
    141     logger.debug("order: %d, dim: %d -> shape: %s", order, dim, x_data.shape)

/home/<>/lib/python2.7/site-packages/chaospy/distributions/baseclass.pyc in inv(self, q, maxiter, tol, verbose, **kws)
    207         """
    208         from . import rosenblatt
--> 209         return rosenblatt.inv(self, q, maxiter, tol, **kws)
    210 
    211     def pdf(self, x, step=1e-7, verbose=0):

/home/<>/lib/python2.7/site-packages/chaospy/distributions/rosenblatt.pyc in inv(dist, q, maxiter, tol, **kws)
     36 
     37     try:
---> 38         out, graph = dist.graph.run(q, "inv", maxiter=maxiter, tol=tol)
     39 
     40     except NotImplementedError:

/home/<>/lib/python2.7/site-packages/chaospy/distributions/graph/baseclass.pyc in run(self, x, mode, **meta)
    196     def run(self, x, mode, **meta):
    197         """Run through network to perform an operator."""
--> 198         return main.call(self, x, mode, **meta)
    199 
    200     def counting(self, dist, mode):

/home/<>/lib/python2.7/site-packages/chaospy/distributions/graph/main.pyc in call(self, x_data, mode, **meta)
     59         out = self(x_data)
     60     else:
---> 61         out = self(x_data, self.root)
     62 
     63     # if mode == "ttr":

/home/<>/lib/python2.7/site-packages/chaospy/distributions/graph/baseclass.pyc in __call__(self, *args, **kwargs)
    158 
    159     def __call__(self, *args, **kwargs):
--> 160         return self._call(self, *args, **kwargs)
    161 
    162     def __str__(self):

/home/<>/lib/python2.7/site-packages/chaospy/distributions/graph/calling/inv.pyc in inv_call(self, q_data, dist)
     33     else:
     34         out, _, _ = approx.ppf(
---> 35             dist, q_data, self, retall=1, **self.meta)
     36     graph.add_node(dist, key=out)
     37 

/home/<>/lib/python2.7/site-packages/chaospy/distributions/approx.pyc in ppf(dist, q, G, maxiter, tol, retall, verbose)
    249 
    250     if not dist.advance:
--> 251         dist.prm, prm = G.K.build(), dist.prm
    252         out = inv(dist, q, maxiter, tol, retall, verbose)
    253         dist.prm = prm

AttributeError: Graph instance has no attribute 'K'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions