📄 bond.java
字号:
package edu.nyu.cims.hw4;
import java.util.*;
import edu.nyu.cims.hw4.*;
/**
*
* @author yun xiang
*
*/
public class Bond {
double price;
Coupon coupon;
double maturity;
double faceValue;
Map<Double, Double> cashFlow;
Bond(){
//this.price = price;
//this.coupon = coupon;
//this.maturity = maturity;
//this.faceValue = faceValue;
}
public double getPrice(){
return this.price;
}
public Coupon getCoupon(){
return this.coupon;
}
public double getMaturity(){
return this.maturity;
}
public double getFaceValue(){
return this.faceValue;
}
public Map<Double,Double> getCashFlow(){
return this.cashFlow;
}
public double getFairPrice(TreeMap<Double,Double> yc, Bond b){
Coupon coupon = b.getCoupon();
double faceValue = b.getFaceValue();
double maturity = b.getMaturity();
double i = coupon.getPayPeriod();
double price = 0;
while(i<maturity){
double rate = (yc.get(i))/100;
price += (faceValue * coupon.getRate())/Math.exp(rate * i);
i+=coupon.getPayPeriod();
}
double rate = (yc.get(maturity))/100;
price += (faceValue + faceValue * coupon.getRate())/Math.exp(rate * maturity);
return price;
}
public double getYTM(final TreeMap<Double,Double> yc, final Bond b) throws Exception{
final double price = b.getPrice();
final double faceValue = b.getFaceValue();
final Coupon coupon = b.getCoupon();
final double maturity = b.getMaturity();
IFunction f = new IFunction(){
public double value(double x) throws Exception {
double i = coupon.getPayPeriod();
double p = 0;
while(i<maturity){
p += (faceValue * coupon.getRate())/Math.exp(x * i);
i += coupon.getPayPeriod();
}
p += (faceValue * ( coupon.getRate()+1 ) )/Math.exp(x * maturity);
return price-p;
}
};
Solver solver = new Solver();
double YTM = solver.findZero(f, 0, 1, 1e-6);
return YTM;
}
public void setPrice(double price){
this.price = price;
}
public void setFaceValue(double faceValue){
this.faceValue = faceValue;
}
public void setCoupon(Coupon coupon){
this.coupon = coupon;
}
public void setMaturity(double maturity){
this.maturity = maturity;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -