6

我无法计算 IRR。我使用 Microsoft.VisualBasic 来计算 IRR。这是一个案例:

using Microsoft.VisualBasic;

...

static void Main(string[] args)
        {
            double[] tmpCashflows = new double[] {
                -480117.0,
                4471.5158140594149,
                6841.5950239895265,
                6550.383550359461,
                6295.8611873818609,
                6074.6070899770129,
                5883.532880960287,
                6006.9907860976427,
                6133.1633945923877
                ,6262.1156759885489
                //,6393.9143799520116            
            };

            decimal irr = 0;
            try
            {
                double tmpIrr = Financial.IRR(ref tmpCashflows);                
                ...
            }
            catch (Exception ex)
            {
                irr = 0;
            }


        }

它给出了“参数无效”类型的异常(在 Microsoft.VisualBasic.Financial.IRR(Double[]& ValueArray, Double Guess))。但是,如果我在 Excel 中进行计算,这不会显示任何错误。

4

2 回答 2

7

您需要提供一个好的 Guess 值作为此 API 的第二个参数。您的输入默认值 0.1 失败。

试试这个:

double tmpIrr = Financial.IRR(ref tmpCashflows, 0.3);

你应该取回一个IRR of -0.2987

看起来 API 只处理特定的输入情况,而在其他情况下因任何猜测而失败。这是 Microsoft 在此处确认的 IRR API 错误。

如果 Guess 成为问题,您最好使用 3rd 方可靠库计算 IRR。

http://connect.microsoft.com/VisualStudio/feedback/details/781299/microsoft-visualbasic-financial-irr-doesnt-solve-for-irr-correctly-in-certain-cases-with-any-guess

出现Argument is not valid异常的原因是无法计算默认Guess 值 0.1的 IRR 。请注意,给定值满足一个负值(支付)和一个正值的条件。(收据)

在内部,**Microsoft.VisualBasic.Financial.IRR**遍历输入值并尝试计算 40 次迭代的 IRR。它永远不会遇到有效 IRR 的中断条件,因此最终会引发此错误。

请注意,Excel 给出的值为 -30%。

同时,尝试像这样的数组的 IRR

tmpCashflows = new double[] { -100, 200, 300 };
double tmpIrr = Microsoft.VisualBasic.Financial.IRR(ref tmpCashflows);

给出 2.0 或 200% 的内部收益率。(通过代码和 Excel)

于 2014-04-04T09:16:27.403 回答
1
public static class Interpolation
{
    public static double IRR(this Dictionary<double, double> cashFlow)  //term in years, amount
    {
        int count = 0;
        double r1=0.1d, r2=0.05d, v1, v2, r = 0, v;
        v1 = cashFlow.DiscountedValue(r1);
        v2 = cashFlow.DiscountedValue(r2); 
        while (Math.Abs(v2 - v1) > .001 && count < 1000)
        {
            count++;
            r = (0 - v1) / (v1-v2) * (r1 - r2) + r1;
            v = cashFlow.DiscountedValue(r);
            v1 = v2;
            r1 = r2;
            v2 = v;
            r2 = r;
        }
        if (count == 1000) return -1;
        return r;
    }



    public static double DiscountedValue(this Dictionary<double, double> cashFlow, double rate)
    {
        double dv = 0d;
        for (int i = 0; i < cashFlow.Count; i++)
        {
            var element = cashFlow.ElementAt(i);
            dv += element.Value * Math.Pow(1 + rate, -(element.Key));
        }
        return dv;   
    }
}
于 2019-06-11T13:23:50.633 回答