Share Brilliantly
term structure models
let data = [
{price:96.60, maturity:1, coupon:2},
{price:93.71, maturity:2, coupon:2.5},
{price:91.56, maturity:3, coupon:3},
{price:90.24, maturity:4, coupon:3.5},
{price:89.74, maturity:5, coupon:4},
{price:90.04, maturity:6, coupon:4.5},
{price:91.09, maturity:7, coupon:5},
{price:92.82, maturity:8, coupon:5.5},
{price:95.19, maturity:9, coupon:6},
{price:98.14, maturity:10, coupon:6.5},
{price:101.60, maturity:11, coupon:7},
{price:105.54, maturity:12, coupon:7.5},
{price:109.90, maturity:13, coupon:8},
{price:114.64, maturity:14, coupon:8.5},
{price:119.73, maturity:15, coupon:9},
];
let cs = await import('/lib/finance/fixed-income/curves/v1.0.0/cubic.mjs');
let knots = [0,7.5,15];
let test1 = cs.g(0, 2, knots)
let test2 = cs.g(0, 0, knots)
let test3 = cs.g(1, 0, knots)
let test4 = cs.g(2, 8, knots)
let test5 = cs.g(3, 8, knots)
let test6 = cs.g(2, 1, knots)
Try it!
let cashFlows = function(item){
let ans = [];
for(let i=1;i<=item.maturity;i++){
if(i==item.maturity){
ans.push({
date:i,
value:100*(1+item.coupon/100)
});
}
else ans.push({
date:i,
value:100*item.coupon
});
}
return ans;
}
Try it!
let C = data.map((p,i)=>{
let row = [];
let flows = cashFlows(p);
for(let k=0;k<=knots.length;k++){
let value = 0;
flows.forEach(q=>{
value += q.value * cs.g(k, q.date, knots);
})
row.push(value);
}
return row;
});
Try it!
let sums = [];
data.map((p,i)=>{
let flows = cashFlows(p);
let sum = 0;
flows.forEach(p=>{
sum += p.value;
});
sums.push(sum);
});
let y = data.map((p,i)=>[p.price-sums[i]]);
let regression = olsregression.regress(y, C);
let alphas = regression.Coefficients.map(p=>p[0]);