This commit is contained in:
Boki 2025-06-11 10:38:05 -04:00
parent 597c6efc9b
commit 8b5e06954a
26 changed files with 532 additions and 186 deletions

View file

@ -7,7 +7,9 @@
* Calculate percentage change between two values
*/
export function percentageChange(oldValue: number, newValue: number): number {
if (oldValue === 0) {return 0;}
if (oldValue === 0) {
return 0;
}
return ((newValue - oldValue) / oldValue) * 100;
}
@ -15,7 +17,9 @@ export function percentageChange(oldValue: number, newValue: number): number {
* Calculate simple return
*/
export function simpleReturn(initialPrice: number, finalPrice: number): number {
if (initialPrice === 0) {return 0;}
if (initialPrice === 0) {
return 0;
}
return (finalPrice - initialPrice) / initialPrice;
}
@ -23,7 +27,9 @@ export function simpleReturn(initialPrice: number, finalPrice: number): number {
* Calculate logarithmic return
*/
export function logReturn(initialPrice: number, finalPrice: number): number {
if (initialPrice <= 0 || finalPrice <= 0) {return 0;}
if (initialPrice <= 0 || finalPrice <= 0) {
return 0;
}
return Math.log(finalPrice / initialPrice);
}
@ -31,7 +37,9 @@ export function logReturn(initialPrice: number, finalPrice: number): number {
* Calculate compound annual growth rate (CAGR)
*/
export function cagr(startValue: number, endValue: number, years: number): number {
if (years <= 0 || startValue <= 0 || endValue <= 0) {return 0;}
if (years <= 0 || startValue <= 0 || endValue <= 0) {
return 0;
}
return Math.pow(endValue / startValue, 1 / years) - 1;
}
@ -91,8 +99,12 @@ export function internalRateOfReturn(
dnpv += (-j * cashFlows[j]) / Math.pow(1 + rate, j + 1);
}
if (Math.abs(npv) < 1e-10) {break;}
if (Math.abs(dnpv) < 1e-10) {break;}
if (Math.abs(npv) < 1e-10) {
break;
}
if (Math.abs(dnpv) < 1e-10) {
break;
}
rate = rate - npv / dnpv;
}
@ -186,7 +198,9 @@ export function bondYield(
);
const diff = calculatedPrice - price;
if (Math.abs(diff) < tolerance) {break;}
if (Math.abs(diff) < tolerance) {
break;
}
// Numerical derivative
const delta = 0.0001;
@ -199,7 +213,9 @@ export function bondYield(
);
const derivative = (priceUp - calculatedPrice) / delta;
if (Math.abs(derivative) < tolerance) {break;}
if (Math.abs(derivative) < tolerance) {
break;
}
yield_ = yield_ - diff / derivative;
}
@ -358,7 +374,9 @@ export function dividendDiscountModel(
growthRate: number,
discountRate: number
): number {
if (discountRate <= growthRate) {return NaN;} // Indeterminate
if (discountRate <= growthRate) {
return NaN;
} // Indeterminate
return (currentDividend * (1 + growthRate)) / (discountRate - growthRate);
}