2014-10-06 44 views
5

我有一组N观测值在一个二维空间中以(x[i], y[i]), i=0..N分布。每个点在两个坐标(e_x[i], e_y[i], i=0..N)中都有相关的错误,并且还有一个与其相关的权重(w[i], i=0..N)。创建直方图时记录错误

我想生成这些N点的2D直方图,占比不仅为权重,但也为错误,这将导致每个点是传播可能在许多垃圾箱如果误差值大足够的(假设标准Gaussian distribution为错误,尽管也许可以考虑其他分布)。

我看到numpy.histogram2d有一个weights参数,所以这是照顾。问题是如何解释每个观察点的错误。

有没有让我这样做的功能?我愿意接受numpyscipy的任何内容。

+0

什么这些错误值代表什么?这些标准偏差沿着主轴? – 2014-11-19 10:09:52

+0

@Dabrion恰好。 – Gabriel 2014-11-19 11:48:24

+0

好吧,那组参数构成了一个多变量GMM,给定权重(\ pi_i),样本作为平均值(\ mu_i)和协方差矩阵(\ Sigma_i)由[[e_x [i] ** 2,0] [ 0,E_Y [I] ** 2]]。与您假设的标准正态情况(对应于所有e_x和e_y等于1.0)不同,您可以使用协方差矩阵,其中对角线可以具有不同的值。这对应于主轴沿主轴的椭圆,而不是圆。这有助于你向前迈进吗? – 2014-11-19 18:35:24

回答

1

建立在user1415946的评论,你可以假设每个点代表bi-variate normal distribution[[e_x[i]**2,0][0,e_y[i]**2]]给出的协方差矩阵。但是,由此产生的分布不是一个正态分布 - 在运行该示例之后,您会看到直方图根本不像高斯分布,而是其中的一组分布。

要从这组分布中创建直方图,我看到的一种方法是使用numpy.random.multivariate_normal从每个点中生成随机样本。使用一些人造数据查看下面的示例代码。

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 


# This is a function I like to use for plotting histograms 
def plotHistogram3d(hist, xedges, yedges): 
    fig = plt.figure() 
    ax = fig.add_subplot(111, projection='3d') 
    hist = hist.transpose() 
    # Transposing is done so that bar3d x and y match hist shape correctly 
    dx = np.mean(np.diff(xedges)) 
    dy = np.mean(np.diff(yedges)) 

    # Computing the number of elements 
    elements = (len(xedges) - 1) * (len(yedges) - 1) 
    # Generating mesh grids. 
    xpos, ypos = np.meshgrid(xedges[:-1]+dx/2.0, yedges[:-1]+dy/2.0) 

    # Vectorizing matrices 
    xpos = xpos.flatten() 
    ypos = ypos.flatten() 
    zpos = np.zeros(elements) 
    dx = dx * np.ones_like(zpos) * 0.5 # 0.5 factor to give room between bars. 
# Use 1.0 if you want all bars 'glued' to each other 
    dy = dy * np.ones_like(zpos) * 0.5 
    dz = hist.flatten() 

    ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b') 
    ax.set_xlabel('x') 
    ax.set_ylabel('y') 
    ax.set_zlabel('Count') 
    return 

""" 
INPUT DATA 
""" 
#     x y ex ey w 
data = np.array([[1, 2, 1, 1, 1], 
       [3, 0, 1, 1, 2], 
       [0, 1, 2, 1, 5], 
       [7, 7, 1, 3, 1]]) 

""" 
Generate samples 
""" 
# Sample size (100 samples will be generated for each data point) 
SAMPLE_SIZE = 100 
# I want to fill in a table with columns [x, y, w]. Each data point generates SAMPLE_SIZE 
# samples, so we have SAMPLE_SIZE * (number of data points) generated points 
points = np.zeros((SAMPLE_SIZE * data.shape[0], 3)) # Initializing this matrix 

for i, element in enumerate(data): # For each row in the data set 
    meanVector = element[:2] 
    covarianceMatrix = np.diag(element[2:4]**2) # Diagonal matrix with elements equal to error^2 
    # For columns 0 and 1, add generated x and y samples 
    points[SAMPLE_SIZE*i:SAMPLE_SIZE*(i+1), :2] = \ 
     np.random.multivariate_normal(meanVector, covarianceMatrix, SAMPLE_SIZE) 
    # For column 2, simply copy original weight 
    points[SAMPLE_SIZE*i:SAMPLE_SIZE*(i+1), 2] = element[4] # weights 

hist, xedges, yedges = np.histogram2d(points[:, 0], points[:, 1], weights=points[:, 2]) 
plotHistogram3d(hist, xedges, yedges) 
plt.show() 

结果下面的曲线:

enter image description here

+0

Gabriel,您能否添加一些注释来描述您的示例中每行代码的作用?另外,你正在运行哪个版本的'matplotlib'?我有版本1.3.1并试图运行你的例子给了我一个'ValueError:Unknown projection'3d'';这很奇怪,因为这里给出的例子http://stackoverflow.com/q/3810865/1391441没有问题。 – Gabriel 2014-11-29 23:18:36

+1

我使用与您的版本相同的版本,但在回答之前我错误地删除了导入行。这一个应该工作。谢谢 – 2014-11-30 02:24:23