尝试在Python中插入直方图函数的输出

尝试在Python中插入直方图函数的输出,python,numpy,scipy,interpolation,cdf,Python,Numpy,Scipy,Interpolation,Cdf,我想做的是玩一些随机分布。我不希望这是正常的。但就目前而言,恢复正常比较容易 import matplotlib.pyplot as plt from scipy.stats import norm ws=norm.rvs(4.0, 1.5, size=100) density, bins = np.histogram(ws, 50,normed=True, density=True) unity_density = density / density.sum() fig, ((ax1, a

我想做的是玩一些随机分布。我不希望这是正常的。但就目前而言,恢复正常比较容易

import matplotlib.pyplot as plt
from scipy.stats import norm

ws=norm.rvs(4.0, 1.5, size=100)
density, bins = np.histogram(ws, 50,normed=True, density=True)
unity_density = density / density.sum()

fig, ((ax1, ax2)) = plt.subplots(nrows=1, ncols=2, sharex=True, figsize=(12,6))
widths = bins[:-1] - bins[1:]

ax1.bar(bins[1:], unity_density, width=widths)
ax2.bar(bins[1:], unity_density.cumsum(), width=widths)

fig.tight_layout()
然后我能做的就是用点来可视化CDF

density1=unity_density.cumsum()
x=bins[:-1]
y=density1

plt.plot(x, density1, 'o')

所以我一直在尝试在np.histogram的输出上使用np.interp函数,以获得表示CDF的平滑曲线,并提取百分比点来绘制它们。理想情况下,我需要尝试手动和使用scipy的ppf函数来完成这一切。 作为一名大学生,我一直在努力学习统计学。我现在在读研究生,我会尽可能多地做这样的练习,以便更深入地了解正在发生的事情。我对这项任务已到了绝望的地步。
谢谢大家!

获得更平滑结果的一种可能性是使用更多样本,通过使用
10^5个样本
100个箱子
我得到以下图像:

ws=norm.rvs(loc=4.0,比例=1.5,尺寸=100000)
密度,料仓=np。直方图(ws,料仓=100,标准化=真,密度=真)

通常,您可以使用平滑CDF。 对于
100个样本
s=0.01的平滑因子,我得到:

将numpy导入为np
将matplotlib.pyplot作为plt导入
从scipy.interpolate导入splev,splrep
密度1=单位密度
x=垃圾箱[:-1]
y=密度1
#插值
spl=splrep(x,y,s=0.01,per=False)
x2=np.linspace(x[0],x[-1],200)
y2=splev(x2,spl)
#策划
图,ax=plt.子批次()
plt.绘图(x,密度1,'o')
plt.图(x2,y2,'r-')

第三种可能性是通过分析计算CDF。如果您自己在大多数情况下使用
numpy
/
scipy
函数生成噪声,那么已经有了
CDF
的实现,否则您应该在维基百科上找到它。如果你的样品来自测量,那当然是另一回事了

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = np.linspace(-2, 10)
y = norm(loc=4.0, scale=1.5).cdf(x)
ax.plot(x, y, 'bo-')