def test_multivariate():
    from scipy.stats import multivariate_normal as mvn
    from numpy.random import rand

    mean = 3
    var = 1.5

    assert near_equal(mvn(mean,var).pdf(0.5),
                      multivariate_gaussian(0.5, mean, var))

    mean = np.array([2.,17.])
    var = np.array([[10., 1.2], [1.2, 4.]])

    x = np.array([1,16])
    assert near_equal(mvn(mean,var).pdf(x),
                      multivariate_gaussian(x, mean, var))

    for i in range(100):
        x = np.array([rand(), rand()])
        assert near_equal(mvn(mean,var).pdf(x),
                          multivariate_gaussian(x, mean, var))

        assert near_equal(mvn(mean,var).pdf(x),
                          norm_pdf_multivariate(x, mean, var))


    mean = np.array([1,2,3,4])
    var = np.eye(4)*rand()

    x = np.array([2,3,4,5])

    assert near_equal(mvn(mean,var).pdf(x),
                      norm_pdf_multivariate(x, mean, var))
def test_multivariate():
    from scipy.stats import multivariate_normal as mvn
    from numpy.random import rand

    mean = 3
    var = 1.5

    assert near_equal(mvn(mean,var).pdf(0.5),
                      multivariate_gaussian(0.5, mean, var))

    mean = np.array([2.,17.])
    var = np.array([[10., 1.2], [1.2, 4.]])

    x = np.array([1,16])
    assert near_equal(mvn(mean,var).pdf(x),
                      multivariate_gaussian(x, mean, var))

    for i in range(100):
        x = np.array([rand(), rand()])
        assert near_equal(mvn(mean,var).pdf(x),
                          multivariate_gaussian(x, mean, var))

        assert near_equal(mvn(mean,var).pdf(x),
                          norm_pdf_multivariate(x, mean, var))


    mean = np.array([1,2,3,4])
    var = np.eye(4)*rand()

    x = np.array([2,3,4,5])

    assert near_equal(mvn(mean,var).pdf(x),
                      norm_pdf_multivariate(x, mean, var))
def plot_3d_sampled_covariance(mean, cov):
    """ plots a 2x2 covariance matrix positioned at mean. mean will be plotted
    in x and y, and the probability in the z axis.

    Parameters
    ----------
    mean :  2x1 tuple-like object
        mean for x and y coordinates. For example (2.3, 7.5)

    cov : 2x2 nd.array
       the covariance matrix

    """

    # compute width and height of covariance ellipse so we can choose
    # appropriate ranges for x and y
    o,w,h = stats.covariance_ellipse(cov,3)
    # rotate width and height to x,y axis
    wx = abs(w*np.cos(o) + h*np.sin(o))*1.2
    wy = abs(h*np.cos(o) - w*np.sin(o))*1.2


    # ensure axis are of the same size so everything is plotted with the same
    # scale
    if wx > wy:
        w = wx
    else:
        w = wy

    minx = mean[0] - w
    maxx = mean[0] + w
    miny = mean[1] - w
    maxy = mean[1] + w

    count = 1000
    x,y = multivariate_normal(mean=mean, cov=cov, size=count).T

    xs = np.arange(minx, maxx, (maxx-minx)/40.)
    ys = np.arange(miny, maxy, (maxy-miny)/40.)
    xv, yv = np.meshgrid (xs, ys)

    zs = np.array([100.* stats.multivariate_gaussian(np.array([xx,yy]),mean,cov) \
                   for xx,yy in zip(np.ravel(xv), np.ravel(yv))])
    zv = zs.reshape(xv.shape)

    ax = plt.figure().add_subplot(111, projection='3d')
    ax.scatter(x,y, [0]*count, marker='.')

    ax.set_xlabel('X')
    ax.set_ylabel('Y')

    ax.contour(xv, yv, zv, zdir='x', offset=minx-1, cmap=cm.autumn)
    ax.contour(xv, yv, zv, zdir='y', offset=maxy, cmap=cm.BuGn)
Ejemplo n.º 4
0
def plot_3d_sampled_covariance(mean, cov):
    """ plots a 2x2 covariance matrix positioned at mean. mean will be plotted
    in x and y, and the probability in the z axis.

    Parameters
    ----------
    mean :  2x1 tuple-like object
        mean for x and y coordinates. For example (2.3, 7.5)

    cov : 2x2 nd.array
       the covariance matrix

    """

    # compute width and height of covariance ellipse so we can choose
    # appropriate ranges for x and y
    o, w, h = stats.covariance_ellipse(cov, 3)
    # rotate width and height to x,y axis
    wx = abs(w * np.cos(o) + h * np.sin(o)) * 1.2
    wy = abs(h * np.cos(o) - w * np.sin(o)) * 1.2

    # ensure axis are of the same size so everything is plotted with the same
    # scale
    if wx > wy:
        w = wx
    else:
        w = wy

    minx = mean[0] - w
    maxx = mean[0] + w
    miny = mean[1] - w
    maxy = mean[1] + w

    count = 1000
    x, y = multivariate_normal(mean=mean, cov=cov, size=count).T

    xs = np.arange(minx, maxx, (maxx - minx) / 40.)
    ys = np.arange(miny, maxy, (maxy - miny) / 40.)
    xv, yv = np.meshgrid(xs, ys)

    zs = np.array([100.* stats.multivariate_gaussian(np.array([xx,yy]),mean,cov) \
                   for xx,yy in zip(np.ravel(xv), np.ravel(yv))])
    zv = zs.reshape(xv.shape)

    ax = plt.figure().add_subplot(111, projection='3d')
    ax.scatter(x, y, [0] * count, marker='.')

    ax.set_xlabel('X')
    ax.set_ylabel('Y')

    ax.contour(xv, yv, zv, zdir='x', offset=minx - 1, cmap=cm.autumn)
    ax.contour(xv, yv, zv, zdir='y', offset=maxy, cmap=cm.BuGn)