public decimal YTM(List <PayAction> actions) { decimal x = 0, eps = (decimal)0.01; FCalc fCalc = (_actions, _x) => { decimal sum = 0; foreach (PayAction i in actions) { sum += i.Sum / (decimal)Math.Pow(1 + (double)x, (double)(((decimal)(i.Date - actions[0].Date).Days) / 365)); } return(sum); }; DerCalc derCalc = (__actions, __x) => { decimal sum = 0; foreach (PayAction i in actions) { sum -= (decimal)((i.Date - actions[0].Date).Days) / 365 * i.Sum / (decimal)(Math.Pow((double)(1 + x), (double)((decimal)((i.Date - actions[0].Date).Days) + 365) / 365)); } return(sum); }; decimal f = fCalc(actions, x); while (Math.Abs(f) >= eps) { x -= f / derCalc(actions, x); f = fCalc(actions, x); } return(x); }
public decimal YTM(List <PayAction> actions) { decimal x = 0, eps = (decimal)0.01, a = x, b = x; FCalc fCalc = (_actions, _x) => { decimal sum = 0; foreach (PayAction i in actions) { sum += i.Sum / (decimal)Math.Pow(1 + (double)x, (double)((decimal)((i.Date - actions[0].Date).Days) / 365)); } return(sum); }; decimal f = fCalc(actions, x); if (f > 0) { while (f > 0 && Math.Abs(f) >= eps) { a = b++; x = b; f = fCalc(actions, x); } } else { while (f < 0 && Math.Abs(f) >= eps) { b = a--; x = a; f = fCalc(actions, x); } } while (Math.Abs(f) >= eps) { x = (a + b) / 2; f = fCalc(actions, x); if (f > 0) { a = x; } else { b = x; } } return(x); }