📄 yieldcurve.java
字号:
package edu.nyu.cims.hw4;
import edu.nyu.cims.hw4.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.Map;
/**
*
* @author yun xiang
*
*/
public class YieldCurve {
private static final String Iterator = null;
LinkedList<Bond> BondList;
TreeMap<Double,Double> YC = new TreeMap<Double,Double>() ;
YieldCurve(LinkedList<Bond> BondList){
this.BondList = BondList;
}
public double getInterestRate(double time) throws Exception{
double rate = this.YC.get(1.0);
return Math.exp((rate/100)*time)-1;
}
public double getForwardRate(double t0, double t1) throws Exception{
double r0 = this.YC.get(t0)/100;
double r1 = this.YC.get(t1)/100;
/*double m = Math.pow(1+Math.pow(r1,t1),t1);
double s = Math.pow(1+Math.pow(r0,t1),t0);
double f = Math.pow((m/s),(1/(t1-t0)))-1;*/
double m = r1*t1-r0*t0;
double s = t1-t0;
return m/s;
}
public double getDiscountFactor(double t) throws Exception{
double rt = this.YC.get(t)/100;
return Math.exp(rt*t*(-1));
}
public TreeMap<Double,Double> buildYieldCurve() throws Exception{
for(Iterator it = BondList.iterator();it.hasNext();){
final Bond b = (Bond)it.next();
IFunction f = new IFunction(){
public double value(double x) throws Exception {
return b.getFaceValue()- b.getPrice()* Math.exp(x*b.getMaturity());
}
};
Solver solver = new Solver();
double result = solver.findZero(f, 0, 1, 1e-6);
this.YC.put((Double)b.getMaturity(), (Double)(result * 100));
}
return this.YC;
}
public void printYieldCurve() throws Exception{
System.out.print("Year | r\n");
NumberFormat fmt = DecimalFormat.getInstance();
fmt.setMaximumFractionDigits(2);
for(Iterator it = this.YC.keySet().iterator(); it.hasNext();){
Double t = (Double)it.next();
System.out.print(t + " | " + fmt.format(this.YC.get(t)) + "%\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -