Numpy采样功能

时间:2017-04-09 18:50:44

标签: python numpy

在numpy或scipy中是否有任何函数对于给定的pdf,将返回从该分布中采样的点?

例如,我有高斯分布的混合:

means = 0., 8.
stdevs = 1.0,1.0
weights = 0.75,0.25

pdfs = [p * norm.pdf(x, mu, sd) for mu, sd, p in zip(means, stdevs, weights)]

pdf的图表和从此分布中采样的点的直方图应如下所示: enter image description here

感谢

3 个答案:

答案 0 :(得分:0)

你可以用numpy解决这个问题

import numpy as np
x_vals = np.random.normal(0., 1.0, 100)
y_vals = np.random.normal(8., 1.0, 100)
x_norm = [*x_vals, *y_vals]

答案 1 :(得分:0)

如果您希望以一种仍然可以传递x的方式组合多个pdf,则需要创建一个新函数。一种方法是创建一个函数,返回一个带有高斯加权和的函数。

from scipy.stats import norm
import numpy as np

def create_mixed_pdf(means, stdevs, weights):
    # this uses a lambda function to return the weighted sum of the pdfs at x
    return lambda x: sum([p*norm(mu, sd).pdf(x) for mu, sd, p in zip(means, stdevs, weights)])

# this sets `my_pdf` to be the combined pdf distributions
my_pdf = create_mixed_pdf(means, stdevs, weights)

# testing:
import matplotlib.pyplot as plt
x = np.arange(-4, 12, .05)

plt.plot(x, my_pdf(x))
plt.show()

enter image description here

答案 2 :(得分:0)

从随机pdf采样是一个有趣的问题!!! 而且非常容易。

# LETS CREATE SOME CUSTOM PDF AND CDF
x = np.linspace(-10,10,1000)
mu=1
s=0.5
pdf = (1/np.sqrt(2*np.pi*s**2))*np.exp(-(x-mu)**2/(2*s**2))/0.8 + (1/np.sqrt(2*np.pi*4*s**2))*np.exp(-(x-mu-3)**2/(2*4*s**2))/0.8
cdf = np.cumsum(pdf)
cdf = cdf/ cdf[-1]
plt.figure()
plt.subplot(121)
plt.plot(x,pdf)
plt.subplot(122)
plt.plot(x,cdf)

Images:PDF and CDF

# SAMPLE FROM CDF USING UNIFORM DISTRIBUTION
uniform_samples = np.random.uniform(0,1,100000)
index = []
for sample in uniform_samples:
  index.append(np.argmin(np.abs(cdf - sample)))
#Project theses samples from cdf to pdf 
pdf_samples = x[index]
plt.hist(pdf_samples, bins=100);

Image: samples from custom pdf