Giter VIP home page Giter VIP logo

kde_multiply's Introduction

KDE_multiply

A function to multiply two scipy Gaussian KDEs analytically with arbitary dimension.

Usage

from KDE_multiply import KDE_multiply
from scipy.stats import gaussian_kde

KDE1 = gaussian_kde(x1) # x1 is some generic one- or multi-dimensional samples
KDE2 = gaussian_kde(x2) # x2 is some generic one- or multi-dimensional samples

KDE3 = KDE_multiply(KDE1, KDE2)

Example

from KDE_multiply import KDE_multiply
from numpy import dot
from numpy.random import seed, uniform, multivariate_normal
from numpy.linalg import multi_dot, inv
from scipy.stats import gaussian_kde
from matplotlib import pyplot as plt
from corner import corner

# generate samples to be used for KDE
dimension = 4 # setting the dimension
seed(512) # setting the seed

# randomly generate the mean for the two Gaussians
mean1 = uniform(-3, 3, size=dimension)
mean2 = uniform(-3, 3, size=dimension)

# randomly generate the covariance matrix for the two Gaussians
cov1 = uniform(0, 2, size=(dimension,dimension))
cov1 = dot(cov1.T, cov1)
cov2 = uniform(0, 2, size=(dimension,dimension))
cov2 = dot(cov2.T, cov2)

# generate samples for the two Gaussians
x1 = multivariate_normal(mean=mean1, cov=cov1, size=6000).T
x2 = multivariate_normal(mean=mean2, cov=cov2, size=6000).T

# estimated the KDEs
KDE1 = gaussian_kde(x1)
KDE2 = gaussian_kde(x2)

# multiply the KDEs
KDE_joint = KDE_multiply(KDE1, KDE2, downsample=True,
                         random_state=42, nsamples=6000)

# resample from the joint KDE
samples_joint = KDE_joint.resample(size=6000)

# compare with exact calculation
cov_predict = multi_dot((cov1, inv(cov1 + cov2), cov2))
mean_predict = multi_dot((cov2, inv(cov1 + cov2), mean1))
mean_predict += multi_dot((cov1, inv(cov1 + cov2), mean2))
samples_joint_predict = multivariate_normal(mean=mean_predict,
                                            cov=cov_predict,
                                            size=6000).T

fig = corner(samples_joint_predict.T, color='C0')
corner(samples_joint.T, fig=fig, color='C1')
plt.show()

Methods

Details can be found at the methods.pdf

kde_multiply's People

Contributors

tsunhopang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.