python - 多维置信区间

标签 python matplotlib scipy

我有许多元组 (par1,par2),即通过多次重复实验获得的二维参数空间中的点。

我正在寻找一种计算和可视化置信椭圆的可能性(不确定这是否是正确的术语)。这是我在网上找到的一个示例图来说明我的意思:

enter image description here

来源:blogspot.ch/2011/07/classification-and-discrimination-with.html

所以原则上,我猜必须将多元正态分布拟合到数据点的二维直方图。有人可以帮我解决这个问题吗?

最佳答案

听起来你只想要点散点的 2-sigma 椭圆?

如果是这样,考虑这样的事情(来自本文的一些代码:https://github.com/joferkington/oost_paper_code/blob/master/error_ellipse.py):

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

def plot_point_cov(points, nstd=2, ax=None, **kwargs):
    """
    Plots an `nstd` sigma ellipse based on the mean and covariance of a point
    "cloud" (points, an Nx2 array).

    Parameters
    ----------
        points : An Nx2 array of the data points.
        nstd : The radius of the ellipse in numbers of standard deviations.
            Defaults to 2 standard deviations.
        ax : The axis that the ellipse will be plotted on. Defaults to the 
            current axis.
        Additional keyword arguments are pass on to the ellipse patch.

    Returns
    -------
        A matplotlib ellipse artist
    """
    pos = points.mean(axis=0)
    cov = np.cov(points, rowvar=False)
    return plot_cov_ellipse(cov, pos, nstd, ax, **kwargs)

def plot_cov_ellipse(cov, pos, nstd=2, ax=None, **kwargs):
    """
    Plots an `nstd` sigma error ellipse based on the specified covariance
    matrix (`cov`). Additional keyword arguments are passed on to the 
    ellipse patch artist.

    Parameters
    ----------
        cov : The 2x2 covariance matrix to base the ellipse on
        pos : The location of the center of the ellipse. Expects a 2-element
            sequence of [x0, y0].
        nstd : The radius of the ellipse in numbers of standard deviations.
            Defaults to 2 standard deviations.
        ax : The axis that the ellipse will be plotted on. Defaults to the 
            current axis.
        Additional keyword arguments are pass on to the ellipse patch.

    Returns
    -------
        A matplotlib ellipse artist
    """
    def eigsorted(cov):
        vals, vecs = np.linalg.eigh(cov)
        order = vals.argsort()[::-1]
        return vals[order], vecs[:,order]

    if ax is None:
        ax = plt.gca()

    vals, vecs = eigsorted(cov)
    theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))

    # Width and height are "full" widths, not radius
    width, height = 2 * nstd * np.sqrt(vals)
    ellip = Ellipse(xy=pos, width=width, height=height, angle=theta, **kwargs)

    ax.add_artist(ellip)
    return ellip

if __name__ == '__main__':
    #-- Example usage -----------------------
    # Generate some random, correlated data
    points = np.random.multivariate_normal(
            mean=(1,1), cov=[[0.4, 9],[9, 10]], size=1000
            )
    # Plot the raw points...
    x, y = points.T
    plt.plot(x, y, 'ro')

    # Plot a transparent 3 standard deviation covariance ellipse
    plot_point_cov(points, nstd=3, alpha=0.5, color='green')

    plt.show()

enter image description here

关于python - 多维置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12301071/

相关文章:

python - numpy recfromcsv 和 genfromtxt 跳过数据文件的第一行

python - 如何在Python中将字典转换为数据框

python - 即使启动它的 lambda 函数超时,如何使复制命令继续在 Redshift 中运行?

python - 如何在 Python 中并行化列表理解计算?

matplotlib - 基于色调/调色板的条形图中的边缘颜色

scipy - 使用 -fPIC 与 -frecursive 进行 gfortran 编译

python - 通过将值分配为 pandas 数据框中的列来对数据框中的行进行分组

python - matplotlib 图例中的项目顺序是如何确定的?

python - 制作一个线框多边形

python - 属性错误: module 'statsmodels' has no attribute 'datasets'