Python中累积分布函数的导数WRT不同参数
我有以下 python 代码:
stats.norm.cdf((log((H ** 2) / (S * X))) / (sigma * sqrt(T)) + (1 + mu) * sigma * sqrt(T))
我正在尝试获取 cdf 的 S、X、sigma 和 T 的导数。 换句话说,我正在努力寻找 $ d/dX (stats.norm.cdf((log((H ** 2) / (S * X))) / (sigma * sqrt(T)) + (1 + mu) * sigma * sqrt(T) ))$
(对于 d/dS、d/d sigma、d/dT 也相同)。
这可能吗?如果是这样,我如何找到衍生品?
我尝试使用链式法则来做到这一点。然而,我没有成功。谁能告诉我这是如何完成的或分享他们显示步骤的链接?
I have the following python code:
stats.norm.cdf((log((H ** 2) / (S * X))) / (sigma * sqrt(T)) + (1 + mu) * sigma * sqrt(T))
I am trying to get the derivative w.r.t. S, X, sigma, and T of the cdf.
In other words, I am trying to find
$ d/dX (stats.norm.cdf((log((H ** 2) / (S * X))) / (sigma * sqrt(T)) + (1 + mu) * sigma * sqrt(T)))$
(and the same for d/dS, d/d sigma, d/dT).
Is this possible? If so, how do I find the derivatives?
I tried to do so using the chain rule. However, I fail to succeed. Can anyone show me how it is done or share a link where they show the steps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里你已经得到了两个函数的组合,因此使用链式法则得到
stats.norm.pdf
作为外部函数F
的导数,这里是>stats.norm.cdf
。您可以通过数字或符号方式获得内部函数的导数(我们将其称为内部函数
g
)。为了速度,我猜测以符号方式计算 g 的导数然后定义函数会更快,但如果时间允许,您可以尝试这两个选项。您可以尝试 https://www.wolframalpha.com/ 在线导数计算器来获取您需要的导数,然后为
g
编写三个导数函数。编辑/更新:对于数值导数,如果函数在变量中是解析的,则可以使用的一件简单的事情是导数近似的复杂步骤方法。
这是有关该内容的一些参考资料
何时、为何以及如何运作:
https: //nhigham.com/2020/10/06/what-is-the-complex-step-approximation/
示例 python 代码:
https://mdolab.engin.umich.edu/misc/complex-step- Guide-python
这是一个有时在 scipy 内部用于导数近似的技巧。鉴于它的简单和有用,该方法也可能在其他地方使用。
Here you've got the composition of two functions so use the chain rule to get
stats.norm.pdf
as the derivative of the outer functionF
, which here isstats.norm.cdf
.You can get the derivatives of the inner function (let's call the inner one
g
) either numerically or symbolically. For speed I would guess that calculatingg
's derivatives symbolically and then defining the function is faster but you could try both options if time permits you to do so.You could try https://www.wolframalpha.com/ online derivative calculator to get the closed form of the derivatives that you need and then code the three derivative functions for
g
.Edit/Update: For numeric derivatives one easy thing you can use if the function is analytic in the variable is the complex step method for derivative approximation.
Here are some references on that one
when, why, and how it works:
https://nhigham.com/2020/10/06/what-is-the-complex-step-approximation/
example python code:
https://mdolab.engin.umich.edu/misc/complex-step-guide-python
This is a trick that is sometimes used inside scipy for derivative approximations. Given how easy and useful it is, the method is likely used in other places too.