本文介绍了创建多元偏斜正态分布python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建多元偏斜法线函数,然后通过输入x和y点,我们可以在3d(x,y和z坐标)中创建表面图

解决方案

我写了

How can I create a multivariate skew normal function, where then by inputting x and y points we can create a surface diagram in 3d (x,y and z coordinates)

解决方案

I wrote a blog post about this, but here is complete working code:

from   matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
from   scipy.stats import (multivariate_normal as mvn,
                           norm)


class multivariate_skewnorm:

    def __init__(self, a, cov=None):
        self.dim  = len(a)
        self.a    = np.asarray(a)
        self.mean = np.zeros(self.dim)
        self.cov  = np.eye(self.dim) if cov is None else np.asarray(cov)

    def pdf(self, x):
        return np.exp(self.logpdf(x))

    def logpdf(self, x):
        x    = mvn._process_quantiles(x, self.dim)
        pdf  = mvn(self.mean, self.cov).logpdf(x)
        cdf  = norm(0, 1).logcdf(np.dot(x, self.a))
        return np.log(2) + pdf + cdf


xx   = np.linspace(-2, 2, 100)
yy   = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(xx, yy)
pos  = np.dstack((X, Y))

fig  = plt.figure(figsize=(10, 10), dpi=150)
axes = [
    fig.add_subplot(1, 3, 1, projection='3d'),
    fig.add_subplot(1, 3, 2, projection='3d'),
    fig.add_subplot(1, 3, 3, projection='3d')
]

for a, ax in zip([[0, 0], [5, 1], [1, 5]], axes):
    Z = multivariate_skewnorm(a=a).pdf(pos)
    ax.plot_surface(X, Y, Z, cmap=cm.viridis)
    ax.set_title(r'$\alpha$ = %s, cov = $\mathbf{I}$' % str(a), fontsize=18)

That code will generate this figure:

这篇关于创建多元偏斜正态分布python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:56