这种新镇 - 拉夫森实施对黑人学位的效率暗示着波动?

发布于 2025-02-04 03:06:51 字数 994 浏览 2 评论 0原文

我已经实施了一种用于计算隐含波动率的算法(如下所示),并正在寻找有关如何改进代码的反馈,无论是速度,组织等。走到任何不良习惯的面前。

请假设最佳编写“ BS_OPTION_PRICE_ANALYTICE”和“ VEGA_BS”的实现。这些用于第一个导数计算。

double newton_raphson_implied_vol(char type, double mkt_price, double S, double K, double T, double sigma0, double r, double q, double tol)
{
    // Initialize variables
    double bs_price_old{bs_option_price_analytical(type, S, K, T, sigma0, r, q)};
    double bs_mkt_diff{bs_price_old - mkt_price}; // To avoid repeated calculations in While condition
    double sigma_new{-bs_price_old/vega_bs(S, K, T, sigma0, r, q) + sigma0}; // First new value (to be updated iteratively later) 

    // Iterate using Newton-Raphson method
    while (abs(bs_mkt_diff/mkt_price) > tol)
    {
        bs_price_old = bs_option_price_analytical(type, S, K, T, sigma_new, r, q);
        bs_mkt_diff = bs_price_old - mkt_price;
        sigma_new = -bs_mkt_diff/vega_bs(S, K, T, sigma_new, r, q) + sigma_new;
    }

    return sigma_new;
}

I've implemented an algorithm for calculating the implied volatility (shown below), and was looking for feedback on how I could improve my code, whether it's speed, organization, etc. I'm very new to C++ so I'm looking to get in front of any bad habits.

Please assume the implementation for "bs_option_price_analytical" and "vega_bs" are optimally written. These are used in the first derivative calculation.

double newton_raphson_implied_vol(char type, double mkt_price, double S, double K, double T, double sigma0, double r, double q, double tol)
{
    // Initialize variables
    double bs_price_old{bs_option_price_analytical(type, S, K, T, sigma0, r, q)};
    double bs_mkt_diff{bs_price_old - mkt_price}; // To avoid repeated calculations in While condition
    double sigma_new{-bs_price_old/vega_bs(S, K, T, sigma0, r, q) + sigma0}; // First new value (to be updated iteratively later) 

    // Iterate using Newton-Raphson method
    while (abs(bs_mkt_diff/mkt_price) > tol)
    {
        bs_price_old = bs_option_price_analytical(type, S, K, T, sigma_new, r, q);
        bs_mkt_diff = bs_price_old - mkt_price;
        sigma_new = -bs_mkt_diff/vega_bs(S, K, T, sigma_new, r, q) + sigma_new;
    }

    return sigma_new;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文