一千萬個為什麽

搜索

C# - 使用Black Scholes Newton偶爾返回NaN

第一個警告:我是一個為客戶做這個的程序員,我對選項的了解可能有漏洞。所以在這裏要寬容一點。 =)

問題:當我針對鏈中的所有選項運行Black Scholes Newton時,我偶爾會得到NaN的返回值。我對準確性的比較是針對ThinkorSwim的數字。

Here are screencaps of the issue: http://imgur.com/a/cItIj

spot, strike, time, etc, are all there. And most of the time the numbers are accurate. But once I hit certain ranges with Calls and Puts, I get those NaN's, and I can't seem to figure out the pattern as to why

我使用garagebandhedgefund的代碼作為基礎,調整了看跌/看漲期權。

這是牛頓代碼:

 public static double OptionPriceImpliedVolatilityCallBlackScholesNewton(double S, double K, double r, double time, double optionPrice,bool iscall, bool isput,out double price,out double diff)
    {
        price = 0;
        diff = 0;
        int MAX_ITERATIONS = 100;
        double ACCURACY = 1.0e-5;
        double t_sqrt = Math.Sqrt(time);
        double sigma = (optionPrice/S)/(0.398 * t_sqrt);   //find initial value  
         price = 0;
        for (int i = 0; i < MAX_ITERATIONS; i++)
        {
            if (iscall)
            {
                 price = OptionPriceCallBlackScholes(S, K, r, sigma, time);
            }
            if (isput)
            {
                 price = OptionPricePutBlackScholes(S, K, r, sigma, time);
            }
            diff = optionPrice - price;
            if (Math.Abs(diff) < ACCURACY)
                return sigma;
            double d1 = (Math.Log(S/K) + r * time)/(sigma * t_sqrt) + 0.5 * sigma * t_sqrt;
            double vega = (S * t_sqrt * NormDist(d1));
            sigma = sigma + diff/vega;
        }
        return sigma; //something screwy happened, should throw exception //<--- original code  
        //throw new Exception("An error occurred");//Comment this line if you uncomment the line above  
    }

我的問題:這是我的代碼嗎?它是否具有一定的價值範圍?是否存在牛頓無法返回有效值的情況?我有點難過。

為了記錄,當我將這些數據用於garageband的Bisection代碼時,我得到了全面的錯誤。我不知道那是否相關。我很確定輸入數據的準確性,因為我確實得到了一些有效的牛頓返回。

最佳答案

某些期權價格無法轉換為波動率。例如。對低於其內在價值的價內看漲期權的出價。所以有時NaN是一個有效的答案。 處理它的最佳方法是在進入搜索循環之前進行前期檢查。

轉載註明原文: C# - 使用Black Scholes Newton偶爾返回NaN