我得到了这两个方程,其中给出了 a (alpha) 和 v 的值。 我知道这些不能用经典方法解决,只能通过迭代来解决,所以我想使用 sympy 和 scipy 来计算 phi(分布)。
我的主要目标是计算 k1 和 k2(用代码中的 x 和 y 代替)。
(我用 x 代替 k1,用 y 代替 k2,用 a 代替 alpha。)
from sympy import symbols, Eq, solve, exp
from scipy.stats import norm
x, y = symbols('x y')
v = 2.2526 # given
a = 0.1 # given
eq1 = (x + (1/v)) * exp((-(1/2)) * (x**2)) - (-y + (1/v)) * exp((-(1/2)) * (y**2))
eq2 = (((1 - norm.cdf(-1/v))**-1) * (norm.cdf(y) - norm.cdf(-x)) + a - 1)
sol = solve((eq1, eq2), (x, y))
print(sol)
如果我运行这个我会得到
TypeError("cannot determine truth value of Relational")
这个错误是什么意思?
编辑1:
x, y = symbols('x y', real=True)
导致
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
最佳答案
使用您的代码和 this comment 提供的提示,以下代码可以提供数值解:
from sympy import symbols, exp, nsolve, solve
from sympy.stats import Normal, cdf
x, y = symbols('x y')
T = Normal("t", 0, 1)
v = 2.2526 # given
a = 0.1 # given
eq1 = (x + (1 / v)) * exp((-(1 / 2)) * (x ** 2)) - (-y + (1 / v)) * exp((-(1 / 2)) * (y ** 2))
eq2 = (((1 - cdf(T)(-1 / v)) ** -1) * (cdf(T)(y) - cdf(T)(-x)) + a - 1)
sol = nsolve((eq1, eq2), (x, y), (1, 1))
# sol = solve((eq1, eq2), (x, y), (1, 1))
print(sol)
# output is: Matrix([[2.42082817559350], [0.284669014405097]])
均值为 0、方差为 1 的正态分布连续随机变量由 T = Normal("t", 0, 1)
指定,CDF 指定为 cdf(T )
。 nsolve
要求调用者提供对答案的估计作为起点,所以我猜是 (1,1) 并且它起作用了。
关于python - 迭代求解 2 个方程结果为 "cannot determine truth value of Relational",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74863253/