public void IRR6Compute() { //Arrange, var obj = new CfObject(); obj.CF0_Irr = -5000; obj.CF0_Npv = -5000; obj.I_Npv = 5; obj.CashFlows_Irr = new List <double> { 1000 }; obj.Frequency_Irr = new List <double> { 3 }; obj.CashFlows_Npv = new List <double> { 1000 }; obj.Frequency_Npv = new List <double> { 3 }; //Act, var result2 = CfNPVMethod(obj); var result = Math.Round(CfIRRMethod(obj), 4); //Assert Assert.AreEqual(-21.7627, result); }
//CashFlow Functions (NPV IRR Calculations) /// <summary> /// Computes the Net Present Value or NPV of a list of provided cash flows at a set interest rate. /// </summary> public static double CfNPVMethod(CfObject cfoObject) { var args_cf = cfoObject.CashFlows_Npv; var args_f = cfoObject.Frequency_Npv; var count = args_cf.Count; var t = 0; var t2 = 0; // Adjust the CF list for the frequency of the Cash flows for (int x = 0; x < count; x++) { if (x != 0) { t2++; } ; for (int y = 1; y < args_f[x]; y++) { double cf = cfoObject.CashFlows_Npv[t2]; args_cf.Insert(t2, cf); t2++; } } ; // Lookup rate double rate = cfoObject.I_Npv / 100; // Initialize net present value double value = 0; // Loop on all values for (int z = 0; z < count; z++) { for (int x = 0; x < args_f[z]; x++) { value += args_cf[t] / Math.Pow(1 + rate, (t + 1)); t++; } } // Return net present value cfoObject.NPV = value + cfoObject.CF0_Npv; //cfoObject.CashFlows = args_cf; return(cfoObject.NPV); }
public void IRR4Compute() { //Arrange, var obj = new CfObject(); obj.CF0_Irr = -3500; obj.CashFlows_Irr = new List <double> { 5, -10, 5, 2000 }; obj.Frequency_Irr = new List <double> { 10, 2, 3, 1 }; //Act, var result = Math.Round(CfIRRMethod(obj), 4); //Assert Assert.AreEqual(-3.3412, result); }
public void IRR2Compute() { //Arrange, var obj = new CfObject(); obj.CF0_Irr = 0; obj.CashFlows_Irr = new List <double> { 500, -100, -300 }; obj.Frequency_Irr = new List <double> { 3, 2, 1 }; //Act, var result = Math.Round(CfIRRMethod(obj), 4); //Assert Assert.AreEqual(-27.6896, result); }
public void IRRCompute() { //Arrange, var obj = new CfObject(); obj.CF0_Irr = 0; obj.CashFlows_Irr = new List <double> { 5555, -1000 }; obj.Frequency_Irr = new List <double> { 2, 15 }; //Act, var result = Math.Round(CfIRRMethod(obj), 4); //Assert Assert.AreEqual(3.7465, result); }
public void CFNPV3Compute() { //Arrange, var obj = new CfObject(); obj.CF0_Npv = 0; obj.I_Npv = 10; obj.CashFlows_Npv = new List <double> { 5, -10 }; obj.Frequency_Npv = new List <double> { 1, 1 }; //Act, var result = Math.Round(CfNPVMethod(obj), 4); //Assert Assert.AreEqual(-3.7190, result); }
public void CFNPV2Compute() { //Arrange, var obj = new CfObject(); obj.CF0_Npv = 150; obj.I_Npv = 8.04; obj.CashFlows_Npv = new List <double> { 10, 15, -5, -33, 50 }; obj.Frequency_Npv = new List <double> { 5, 2, 3, 5, 1 }; //Act, var result = Math.Round(CfNPVMethod(obj), 4); //Assert Assert.AreEqual(154.3204, result); }
public void CFNPVCompute() { //Arrange, var obj = new CfObject(); obj.CF0_Npv = -150; obj.I_Npv = 10; obj.CashFlows_Npv = new List <double> { 10, 20, 5, 3, 250 }; obj.Frequency_Npv = new List <double> { 5, 2, 3, 5, 1 }; //Act, var result = Math.Round(CfNPVMethod(obj), 4); //Assert Assert.AreEqual(-25.3669, result); }
/// <summary> /// computes the internal rate of return for a set of provided cash flows. Newton's method is utilized. /// </summary> /// <param name="cfObject"></param> /// <returns></returns> public static double CfIRRMethod(CfObject cfObject) { var args_cf = cfObject.CashFlows_Irr; var args_f = cfObject.Frequency_Irr; var count = args_cf.Count; var t2 = 0; // Adjust the CF list for the frequency of the Cash flows for (int t = 0; t < count; t++) { if (t != 0) { t2++; } ; for (int y = 1; y < args_f[t]; y++) { double cf = cfObject.CashFlows_Irr[t2]; args_cf.Insert(t2, cf); t2++; } } ; if (cfObject.CF0_Irr > 1e-10 || cfObject.CF0_Irr < -1e-10) { args_cf.Insert(0, cfObject.CF0_Irr); } int itrMax = 20; double espMax = 1e-5; double x = -0.1; //need to start with a negative to allow for negative IRR return values. int iter = 0; while (iter++ < itrMax) { double x1 = 1.0 + x; double fx = 0.0; double dfx = 0.0; for (int i = 0; i < args_cf.Count; i++) { double v = args_cf[i]; double x1_i = Math.Pow(x1, i); fx += v / x1_i; double x1_i1 = x1_i * x1; dfx += -i * v / x1_i1; } double new_x = x - fx / dfx; double esp = Math.Abs(new_x - x); if (esp <= espMax) { if (x == 0.0 && Math.Abs(new_x) <= espMax) { return(0.0); } else { return(new_x * 100); } } x = new_x; } return(x); }