我将过去 19 天的股票价格存储在 pd.dataframe 中,我试图找出小于(平均值 - 2.5*标准差)的价格 P,其中平均值和标准差是根据 P 和过去 19 天的价格。
不等式如下:
P < mean - 2.5*standard deviation
我假设在SymPy中使用solve,但在调用np.std()时出现错误“ufunc的循环不支持Add类型的参数0,它没有可调用的sqrt方法”。
这是演示代码:
from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')
solve(x-np.mean([1,2,3,x])-np.std([1,2,3,x]), x)
错误信息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'Add' object has no attribute 'sqrt'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Input In [71], in <cell line: 4>()
2 from sympy import Symbol
3 x = Symbol('x')
----> 4 solve(x-np.mean([1,2,3,x])-np.std([1,2,3,x]), x)
File ~/opt/anaconda3/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3645, in std(a, axis, dtype, out, ddof, keepdims, where)
3642 else:
3643 return std(axis=axis, dtype=dtype, out=out, ddof=ddof, **kwargs)
-> 3645 return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
3646 **kwargs)
File ~/opt/anaconda3/lib/python3.9/site-packages/numpy/core/_methods.py:214, in _std(a, axis, dtype, out, ddof, keepdims, where)
212 ret = ret.dtype.type(um.sqrt(ret))
213 else:
--> 214 ret = um.sqrt(ret)
216 return ret
TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sqrt method
或者还有其他更好的方法吗?
mean
和stddev
这里有纯Python例程。有了它们(并使用 sqrt(x)
而不是 x**.5
),您可以生成方程来求解:
>>> v=[1,2,3,x]; solve(x-mean(v)-stddev(v))
[2*sqrt(3)/3 + 2]
>>> _[0].n()
3.15470053837925