尝试在 Python 中插入直方图函数的输出
Trying to interpolate the output of a histogram function in Python
我想做的是尝试一些随机分布。我不希望它是正常的。不过暂时正常还是比较容易的。
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 samples
和 100 bins
我得到以下图像:
ws = norm.rvs(loc=4.0, scale=1.5, size=100000)
density, bins = np.histogram(ws, bins=100, normed=True, density=True)
一般来说,您可以使用 scipys
interpolation module 来平滑您的 CDF。
对于 100 samples
和 s=0.01
的平滑因子,我得到:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep
density1 = unity_density.cumsum()
x = bins[:-1]
y = density1
# Interpolation
spl = splrep(x, y, s=0.01, per=False)
x2 = np.linspace(x[0], x[-1], 200)
y2 = splev(x2, spl)
# Plotting
fig, ax = plt.subplots()
plt.plot(x, density1, 'o')
plt.plot(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-')
我想做的是尝试一些随机分布。我不希望它是正常的。不过暂时正常还是比较容易的。
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()
density1=unity_density.cumsum()
x=bins[:-1]
y=density1
plt.plot(x, density1, 'o')
所以我一直在尝试做的是在 np.histogram 的输出上使用 np.interp 函数以获得表示 CDF 的平滑曲线并提取百分比点来绘制它们.理想情况下,我需要尝试手动完成所有操作并使用 scipy 中的 ppf 函数。 作为一名本科生,我一直在与统计学作斗争。我现在在读研究生,并尝试让我尽可能多地进行这样的练习,以便更深入地了解正在发生的事情。我已经对这项任务感到绝望了。 谢谢!
获得更平滑结果的一种可能性是使用更多样本,通过使用 10^5 samples
和 100 bins
我得到以下图像:
ws = norm.rvs(loc=4.0, scale=1.5, size=100000)
density, bins = np.histogram(ws, bins=100, normed=True, density=True)
一般来说,您可以使用 scipys
interpolation module 来平滑您的 CDF。
对于 100 samples
和 s=0.01
的平滑因子,我得到:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep
density1 = unity_density.cumsum()
x = bins[:-1]
y = density1
# Interpolation
spl = splrep(x, y, s=0.01, per=False)
x2 = np.linspace(x[0], x[-1], 200)
y2 = splev(x2, spl)
# Plotting
fig, ax = plt.subplots()
plt.plot(x, density1, 'o')
plt.plot(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-')