XIRR计算 [英] XIRR Calculation

查看:353
本文介绍了XIRR计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何计算Excel的 XIRR 使用功能的C#?

How do I calculate Excel's XIRR function using C#?

推荐答案

根据 XIRR函数的OpenOffice文档(公式相同EXCEL)你需要解决的XIRR变量在下面的 F(XIRR)的等式:

可以通过计算XIRR值:

According to XIRR function openoffice documentation (formula is same as in excel) you need to solve for XIRR variable in the following f(xirr) equation:

You can calculate xirr value by:

  1. 在计算上述函数的导数 - > F'(XIRR)
  2. F(XIRR) F'(XIRR)后您可以通过使用迭代求解XIRR值牛顿法 - 著名式 - >
  1. calculating derivative of above function -> f '(xirr)
  2. after having f(xirr) and f'(xirr) you can solve for xirr value by using iterative Newton's method - famous formula->

修改
我有一点时间,所以,在这里它是 - 完整的C#$ C $下XIRR计算:

EDIT
I've got a bit of time so, here it is - complete C# code for XIRR calculation:

class xirr
    {
        public const double tol = 0.001;
        public delegate double fx(double x);

        public static fx composeFunctions(fx f1, fx f2) {
            return (double x) => f1(x) + f2(x);
        }

        public static fx f_xirr(double p, double dt, double dt0) {
            return (double x) => p*Math.Pow((1.0+x),((dt0-dt)/365.0));
        }

        public static fx df_xirr(double p, double dt, double dt0) {
            return (double x) => (1.0/365.0)*(dt0-dt)*p*Math.Pow((x+1.0),(((dt0-dt)/365.0)-1.0));
        }

        public static fx total_f_xirr(double[] payments, double[] days) {
            fx resf = (double x) => 0.0;

            for (int i = 0; i < payments.Length; i++) {
                resf = composeFunctions(resf,f_xirr(payments[i],days[i],days[0]));
            }

            return resf;
        }

        public static fx total_df_xirr(double[] payments, double[] days) {
            fx resf = (double x) => 0.0;

            for (int i = 0; i < payments.Length; i++) {
                resf = composeFunctions(resf,df_xirr(payments[i],days[i],days[0]));
            }

            return resf;
        }

        public static double Newtons_method(double guess, fx f, fx df) {
            double x0 = guess;
            double x1 = 0.0;
            double err = 1e+100;

            while (err > tol) {
                x1 = x0 - f(x0)/df(x0);
                err = Math.Abs(x1-x0);
                x0 = x1;
            }

            return x0;
        }

        public static void Main (string[] args)
        {
            double[] payments = {-6800,1000,2000,4000}; // payments
            double[] days = {01,08,16,25}; // days of payment (as day of year)
            double xirr = Newtons_method(0.1,
                                         total_f_xirr(payments,days),
                                         total_df_xirr(payments,days));

            Console.WriteLine("XIRR value is {0}", xirr);
        }
    }

顺便说一句,请记住,不是所有的款项都将导致有效的,因为公式和/或牛顿法的限制XIRR!

BTW, keep in mind that not all payments will result in valid XIRR because of restrictions of formula and/or Newton method!

干杯!

这篇关于XIRR计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆