2016-05-22 20 views
1

我有數據值的數組如下:如何繪製事件的到達間隔時間的概率密度函數(PDF)?

0.000000000000000000e+00 
3.617000000000000171e+01 
1.426779999999999973e+02 
2.526699999999999946e+01 
4.483190000000000168e+02 
7.413999999999999702e+00 
1.132390000000000043e+02 
8.797000000000000597e+00 
1.362599999999999945e+01 
2.080880900000000111e+04 
5.580000000000000071e+00 
3.947999999999999954e+00 
2.615000000000000213e+00 
2.458000000000000185e+00 
8.204600000000000648e+01 
1.641999999999999904e+00 
5.108999999999999986e+00 
2.388999999999999790e+00 
2.105999999999999872e+00 
5.783000000000000362e+00 
4.309999999999999609e+00 
3.685999999999999943e+00 
6.339999999999999858e+00 
2.198999999999999844e+00 
3.568999999999999950e+00 
2.883999999999999897e+00 
7.307999999999999829e+00 
2.515000000000000124e+00 
3.810000000000000053e+00 
2.829000000000000181e+00 
2.593999999999999861e+00 
3.963999999999999968e+00 
7.258000000000000007e+00 
3.543000000000000149e+00 
2.874000000000000110e+00 
................... and so on. 

我要繪製的數據值的概率密度函數。我提到(Wiki)scipy.stats.gaussian_kde。但我沒有得到這是正確的或不。 我正在使用python。簡單的數據圖代碼如下:

from matplotlib import pyplot as plt 
plt.plot(Data) 

但現在我想繪製PDF(概率密度函數)。但我沒有在Python中獲得任何庫來這樣做。

+0

由於您正在處理* discrete *數據,因此您的PDF將被分類爲'bin'。使用雙打創建這些容器是很困難的,因爲在它們之間聲明平等是非常困難的,因此您的PDF現在看起來幾乎肯定會看起來像一條扁平線(因爲它正在計算N個唯一值)。你需要介紹一些比較這些像舍入等方式 –

+0

好的。我可以將它舍入到2個小數點。那我該如何策劃? @ScottStainton – KrunalParmar

+1

四捨五入後,您需要計算每個數字的出現次數,然後除以所有數據總量,這會給出每個數值的概率。繪製這個值是你的PDF。 –

回答

4

您所提供的數據集是非常小的,允許一個可靠的內核密度估計。因此,我將會示範程序(如果我理解正確的是你正在嘗試做的),通過使用另一個數據集

import numpy as np 
import scipy.stats 

# generate data samples 
data = scipy.stats.expon.rvs(loc=0, scale=1, size=1000, random_state=123) 

內核密度估計然後可以通過簡單地調用

scipy.stats.gaussian_kde(data,bw_method=bw) 

那裏獲得bw是估算程序的(可選)參數。對於這個數據組,並考慮bw三個值擬合如下

# test values for the bw_method option ('None' is the default value) 
bw_values = [None, 0.1, 0.01] 

# generate a list of kde estimators for each bw 
kde = [scipy.stats.gaussian_kde(data,bw_method=bw) for bw in bw_values] 


# plot (normalized) histogram of the data 
import matplotlib.pyplot as plt 
plt.hist(data, 50, normed=1, facecolor='green', alpha=0.5); 

# plot density estimates 
t_range = np.linspace(-2,8,200) 
for i, bw in enumerate(bw_values): 
    plt.plot(t_range,kde[i](t_range),lw=2, label='bw = '+str(bw)) 
plt.xlim(-1,6) 
plt.legend(loc='best') 

enter image description here

注意如圖所示,大型bw值產生較平滑的PDF格式的估計,然而,隨着成本(在這個例子)暗示負值是可能的,這在這裏不是這種情況。

1

使用numpy.histogram

例子:

# a is your data array 
hist, bins = np.histogram(a, bins=100, normed=True) 
bin_centers = (bins[1:]+bins[:-1])*0.5 
plt.plot(bin_centers, hist)