function nsYield(tau, b0, b1, b2, lam) {
const x = tau / lam;
const f1 = (1 - Math.exp(-x)) / x;
const f2 = f1 - Math.exp(-x);
return b0 + b1 * f1 + b2 * f2;
}
// Semi-annual par bond analytical modified duration & convexity (yield in %)
function parBondMetrics(tenorY, yieldPct) {
const m = 2;
const n = Math.round(tenorY * m);
const y = Math.max(0.001, yieldPct / 100);
const r = y / m;
const c = r; // par bond: semi-annual coupon rate = r
let pv = 0;
let durSum = 0;
let convSum = 0;
for (let k = 1; k <= n; k++) {
const cf = k === n ? (1 + c) : c;
const disc = Math.pow(1 + r, -k);
const pvCf = cf * disc;
pv += pvCf;
durSum += (k / m) * pvCf;
convSum += (k * (k + 1) / (m * m)) * pvCf;
}
const macDur = durSum / pv;
const modDur = macDur / (1 + r);
const conv = convSum / (pv * Math.pow(1 + r, 2));
return {modDur, conv};
}
function runCurveLab(nsRows, dailyRows, dtStr, dB0bps, dB1bps, dB2bps, mixName, notB) {
const dateKey = d => (d.date instanceof Date ? d.date.toISOString().slice(0, 10) : String(d.date).slice(0, 10));
const nsRow = nsRows.find(d => dateKey(d) === dtStr) || nsRows[nsRows.length - 1];
const cdRow = dailyRows.find(d => dateKey(d) === dtStr) || {};
const b0Base = nsRow.b0;
const b1Base = nsRow.b1;
const b2Base = nsRow.b2;
const lam = nsRow.lam || 2.0;
const b0Shock = b0Base + dB0bps / 100;
const b1Shock = b1Base + dB1bps / 100;
const b2Shock = b2Base + dB2bps / 100;
// Grid for smooth baseline vs shocked NS curves (1Y..25Y)
const curveGrid = [];
for (let t = 1.0; t <= 25.0; t += 0.5) {
curveGrid.push({
tenor: t,
base: nsYield(t, b0Base, b1Base, b2Base, lam),
shocked: nsYield(t, b0Shock, b1Shock, b2Shock, lam)
});
}
const benchSpecs = [
{tenor: 2, col: "y_2y", label: "2Y"},
{tenor: 4, col: "y_4y", label: "4Y"},
{tenor: 5, col: "y_5y", label: "5Y"},
{tenor: 10, col: "y_10y", label: "10Y"},
{tenor: 15, col: "y_15y", label: "15Y"},
{tenor: 20, col: "y_20y", label: "20Y"},
{tenor: 25, col: "y_25y", label: "25Y"}
];
const actualPts = benchSpecs
.map(s => ({tenor: s.tenor, label: s.label, actual: cdRow[s.col]}))
.filter(d => d.actual != null && !Number.isNaN(d.actual));
const weightsMap = {
"10Y Benchmark (100% 10Y)": {10: 1.0},
"2s10s Barbell (50% 2Y / 50% 10Y)": {2: 0.5, 10: 0.5},
"2s20s Barbell (40% 2Y / 60% 20Y)": {2: 0.4, 20: 0.6},
"Belly Ladder (30% 4Y / 40% 5Y / 30% 10Y)": {4: 0.3, 5: 0.4, 10: 0.3},
"Equal-Weight Curve (2Y–25Y)": {2: 1/7, 4: 1/7, 5: 1/7, 10: 1/7, 15: 1/7, 20: 1/7, 25: 1/7},
"Long-End ALM (50% 15Y / 50% 25Y)": {15: 0.5, 25: 0.5}
};
const wObj = weightsMap[mixName] || {10: 1.0};
let portYieldBase = 0;
let portYieldShock = 0;
let portModDur = 0;
let portConv = 0;
let totalDurPnlB = 0;
let totalConvPnlB = 0;
let totalDv01M = 0;
const tenorRows = benchSpecs.map(s => {
const w = wObj[s.tenor] || 0;
const baseY = nsYield(s.tenor, b0Base, b1Base, b2Base, lam);
const shockY = nsYield(s.tenor, b0Shock, b1Shock, b2Shock, lam);
const dyDec = (shockY - baseY) / 100;
const dyBps = (shockY - baseY) * 100;
const {modDur, conv} = parBondMetrics(s.tenor, baseY);
const allocB = notB * w;
const dv01M = allocB * 1000 * modDur * 0.0001; // Rp Million per 1 bp
const durPnlB = -allocB * modDur * dyDec;
const convPnlB = 0.5 * allocB * conv * dyDec * dyDec;
const totPnlB = durPnlB + convPnlB;
portYieldBase += w * baseY;
portYieldShock += w * shockY;
portModDur += w * modDur;
portConv += w * conv;
totalDv01M += dv01M;
totalDurPnlB += durPnlB;
totalConvPnlB += convPnlB;
return {
tenor: s.tenor,
label: s.label,
weightPct: w * 100,
baseY,
shockY,
dyBps,
modDur,
conv,
dv01M,
durPnlB,
convPnlB,
totPnlB
};
});
const totalPnlB = totalDurPnlB + totalConvPnlB;
const totalPnlPct = (totalPnlB / notB) * 100;
const base2y = nsYield(2, b0Base, b1Base, b2Base, lam);
const base10y = nsYield(10, b0Base, b1Base, b2Base, lam);
const shock2y = nsYield(2, b0Shock, b1Shock, b2Shock, lam);
const shock10y = nsYield(10, b0Shock, b1Shock, b2Shock, lam);
// Parallel shift sensitivity curve (-200 to +200 bps) for current portfolio
const sensGrid = [];
for (let bps = -200; bps <= 200; bps += 10) {
const dy = bps / 10000;
const linPct = -portModDur * dy * 100;
const fullPct = (-portModDur * dy + 0.5 * portConv * dy * dy) * 100;
sensGrid.push({bps, linPct, fullPct});
}
return {
nsRow,
cdRow,
curveGrid,
actualPts,
tenorRows,
sensGrid,
base2y,
base10y,
shock2y,
shock10y,
base2s10s: (base10y - base2y) * 100,
shock2s10s: (shock10y - shock2y) * 100,
portYieldBase,
portYieldShock,
portModDur,
portConv,
totalDv01M,
totalDurPnlB,
totalConvPnlB,
totalPnlB,
totalPnlPct
};
}
sim = runCurveLab(nsFactors, curveDaily, selectedDate, shockLevelBps, shockSlopeBps, shockCurvBps, portfolioMix, notionalRpB)