<p>symphy中的统计模块名为<code>stats</code>。
另外,<code>Normal</code>接受3个参数。下面是一个简单的使用示例:</p>
<pre><code>>>> from sympy import symbols
>>> from sympy.stats import Normal, density, cdf
>>> x, mu, sigma = symbols("x mu sigma")
>>> N = Normal("N", mu, sigma)
>>> density(N)(x)
sqrt(2)*exp(-(-mu + x)**2/(2*sigma**2))/(2*sqrt(pi)*sigma)
>>> cdf(N)(x)
erf(sqrt(2)*(-mu + x)/(2*sigma))/2 + 1/2
</code></pre>
<p>如需进一步参考,请参阅<a href="https://docs.sympy.org/latest/modules/stats.html" rel="nofollow noreferrer">Sympy Stats docs</a>和<a href="https://docs.sympy.org/latest/modules/stats.html#sympy.stats.Normal" rel="nofollow noreferrer">Normal Distribution docs</a>。你知道吗</p>
<p>对于你的情况,让我来解释一下你应该做什么。你知道吗</p>
<pre><code>import sympy as sy
import sympy.stats as systats
def euro_put_sym(S, K, T, r, sigma):
#S: spot price
#K: strike price
#T: time to maturity
#r: interest rate
#sigma: volatility of underlying asset
N = systats.Normal('N', 0.0, 1.0)
d1 = (sy.ln(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
d2 = (sy.ln(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * sy.sqrt(T))
put = (K * sy.exp(-r * T) * systats.cdf(N)(-d2) - S * systats.cdf(N)(-d1))
return put
</code></pre>
<p>现在</p>
<pre><code>S, K, T, r, sigma = sy.symbols("S K T r sigma")
sy.pprint(euro_put_sym(S, K, T, r, sigma), use_unicode=False)
</code></pre>
<p>输出为</p>
<pre><code> / / ___ / / 2\ /S\\\\ / /
| |0.5*\/ 2 *|T*\r - 0.5*sigma / + log|-|||| | |0.5*\/
| | \ \K//|| | |
| erf| -|| | erf|
| | ___ || | |
|1 \ \/ T *sigma /| -T*r |1 \
K*|- - |*e - S*|- -
\2 2 / \2
___ / / 2\ /S\\\\
2 *|T*\r + 0.5*sigma / + log|-||||
\ \K//||
-||
___ ||
\/ T *sigma /|
|
2 /
</code></pre>
<p>这是预期产出吗?你知道吗</p>
<p>我通过您提供的链接中的示例进行了测试,结果是匹配的。你知道吗</p>
<pre><code>>>> euro_put_sym(50, 100, 1, 0.05, 0.25)
-25*erf(1.22379436111989*sqrt(2)) + 22.5614712250357 + 47.5614712250357*erf(1.34879436111989*sqrt(2))
</code></pre>