using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UserDefinedDataEXP
{
public class InterestRateCalculator
{
//Member variables
private double r; // Interest rate
private int nPeriods; // Number of periods
//1.构造器
public InterestRateCalculator(int numberPeriods, double interest)
{
this.nPeriods = numberPeriods;
this.r = interest;
}
//2.1 FutureValue函数:1年1次
public double FutureValue(double P0)
{
double factor = 1.0 + r;
return P0 * Math.Pow(factor, nPeriods);
}
//2.2 FutureValue函数:1年m次
public double FutureValue(double P0, int m)
{
double R = r / m;
int newPeriods = m * nPeriods;
// We create a temporary object to do the job
InterestRateCalculator myBond = new InterestRateCalculator(newPeriods, R);
return myBond.FutureValue(P0);
}
// 3. 年金未来值(Future value of an ordinary annuity.)
public double OrdinaryAnnuity(double A)
{
double factor = 1.0 + r;
return A * ((Math.Pow(factor, nPeriods) - 1.0) / r);
}
// 4. 现值(Pn贴现到现在)
public double PresentValue(double Pn)
{
double factor = 1.0 + r;
return Pn * (1.0 / Math.Pow(factor, nPeriods));
}
// 5. Present Value of a series of future values.
public double PresentValue(double[] futureValues)
{
double factor = 1.0 + r;
double PV = 0.0;
for (int t = 0; t < nPeriods; t++)
{
PV += futureValues[t] / Math.Pow(factor, t + 1);
}
return PV;
}
// 6. Present Value of a series of future values with constant coupon.
public double PresentValueConstant(double Coupon)
{ // !! Maturity not in this formula.
double factor = 1.0 + r;
double PV = 0.0;
for (int t = 0; t < nPeriods; t++)
{
PV += 1.0 / Math.Pow(factor, t + 1);
}
return PV * Coupon;
}
// 7. Present Value of an ordinary annuity.
public double PresentValueOrdinaryAnnuity(double A)
{
double factor = 1.0 + r;
double numerator =1.0 - (1.0 / Math.Pow(factor, nPeriods));
return (A * numerator) / r;
}
// 8. 属性NumberOfPeriods
public int NumberOfPeriods
{
get { return nPeriods; }
set { nPeriods = value; }
}
// 9. 属性Interest
public double Interest
{
get { return r; }
set { r = value; }
}
//10. 连续复利FutureValueContinuous
public double FutureValueContinuous(double P0)
{
double factor = Math.E;
return P0 * Math.Pow(factor, r*nPeriods);
}
};
public class Bond
{
// Bond delegates to interest rate algorithms
InterestRateCalculator eng;
// Use redundant data to save delegating to InterestRateCalculator
private double r; // Interest rate
private int nPeriods; // Number of periods
private double c; // Cash coupon payment
//1.1 构造器
public Bond(int numberPeriods, double interest, double Coupon, int paymentPerYear)
{
nPeriods = numberPeriods;
r = interest / (double)paymentPerYear;
c = Coupon;
eng = new InterestRateCalculator(nPeriods, r);
}
//1.2 构造器
public Bond(InterestRateCalculator irCalculator, double Coupon, int paymentPerYear)
{
eng = irCalculator;
c = Coupon;
nPeriods = eng.NumberOfPeriods;
r = eng.Interest / (double)paymentPerYear;
}
//2. Price as function of Redemption value
public double price(double redemptionValue)
{
// present value of coupon payments
double pvCoupon = eng.PresentValueConstant(c);
// present value of redemption value
double pvPar = eng.PresentValue(redemptionValue);
return pvCoupon + pvPar;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;
namespace UserDefinedDataEXP
{
class test
{
static void Main(string[] args)
{
// Future value of a sum of money invested today
int nPeriods = 6; // 6 years
double P = Math.Pow(10.0, 7); // Amount invested now
double r = 0.092; // 9.2% interest per year
InterestRateCalculator interestEngine =new InterestRateCalculator(nPeriods, r);
double fv = interestEngine.FutureValue(P);
Console.WriteLine("Future Value: {0} ", fv.ToString("N", CultureInfo.InvariantCulture));
int m = 2; // Compounding per year
double fv2 = interestEngine.FutureValue(P, m);
Console.WriteLine("Future Value with {0} compoundings per year {1}", m, fv2);
// Future value of an ordinary annuity
double A = 2.0 * Math.Pow(10.0, 6);
interestEngine.Interest = 0.08;
interestEngine.NumberOfPeriods = 15; // 15 years
Console.WriteLine("Ordinary Annuity: {0} ",
interestEngine.OrdinaryAnnuity(A));
// Present Value
double Pn = 5.0 * Math.Pow(10.0, 6);
interestEngine.Interest = 0.10;
interestEngine.NumberOfPeriods = 7;
Console.WriteLine("**Present value: {0} ", interestEngine.PresentValue(Pn));
// Present Value of a series of future values
interestEngine.Interest = 0.0625;
interestEngine.NumberOfPeriods = 5;
int nPeriods2 = interestEngine.NumberOfPeriods;
double[] futureValues = new double[nPeriods2]; // For five years
for (long j = 0; j < nPeriods2 - 1; j++)
{ // The first 4 years
futureValues[j] = 100.0;
}
futureValues[nPeriods2 - 1] = 1100.0;
Console.WriteLine("**Present value, series: {0} ",
interestEngine.PresentValue(futureValues));
// Present Value of an ordinary annuity
A = 100.0;
interestEngine.Interest = 0.09;
interestEngine.NumberOfPeriods = 8;
Console.WriteLine("**PV, ordinary annuity: {0}",
interestEngine.PresentValueOrdinaryAnnuity(A));
// Now test periodic testing with continuous compounding
double P0 = Math.Pow(10.0, 8);
r = 0.092;
nPeriods2 = 6;
for (int mm = 1; mm <= 100000000; mm *= 12)
{
Console.WriteLine("Periodic: {0},, {1}", mm, interestEngine.FutureValue(P0, mm));
}
Console.WriteLine("Continuous Compounding: {0}", interestEngine.FutureValueContinuous(P0));
// Bond pricing
double coupon = 50; // Cash coupon, i.e. 10.0% rate semiannual
// on parValue
int n = 20 * 2; // Number of payments
double annualInterest = 11.0; // Interest rate annualized
double parValue = 1000.0;
int paymentPerYear = 2; // Number of payments per year
Bond myBond = new Bond(n, annualInterest, coupon, paymentPerYear);
double bondPrice = myBond.price(parValue);
Console.WriteLine("Bond price: {0}", bondPrice);
Console.ReadLine();
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容