如何获得 scipy.special 函数以将 pymc3 RV 作为输入

How to get scipy.special functions to take pymc3 RV's as input

我正在尝试使用 pymc3 来拟合涉及 voigt 函数的模型(来自 scipy.special)。 voigt 函数的输入应该是数组,而 a,b 是 pymc3 类。如何获得 scipy.special 函数以将 pymc3 RV 作为输入? 运行 下面附加的代码产生错误:

import pymc3 as pm
from scipy.special import voigt_profile
import numpy as np

with pm.Model() as linear_model:
    a = pm.Lognormal('a',mu=0, sigma=2.)
    b = pm.Lognormal('b',mu=0, sigma=2.)
    x = np.linspace(-1,1)
    c = voigt_profile(x,a,b)

TypeError: ufunc 'voigt_profile' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

无论好坏,您都需要使用 theano(重新)实现该功能。这是一个有效的简单版本:注意你不能使用 erfc,因为 theano 出错了。

import theano.tensor as tt

def faddeeva(z):
    m = tt.exp(-tt.square(z))
    return (m - m * tt.erf(1.j * z))

def voigt_profile(x, sigma, gamma):
    z = (x + 1.j * gamma) / (tt.sqrt(2.) * sigma)
    return faddeeva(z).real / (sigma * tt.sqrt(2 * np.pi))