-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.js
executable file
·58 lines (47 loc) · 3.26 KB
/
examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { calculateForwardRateDays, calculateForwardRateYears } from './forwardRate.js'
import { calculateDailyCompoundedReturn, calculateAnnuallyCompoundedReturn } from './compoundReturn.js'
import {
calculateAnnualRepayment,
calculateMonthlyRepayment,
calculateFortnightlyRepayment,
calculateWeeklyRepayment
} from './loanRepayment.js'
// Examples of usage of functions in forwardRate.js
const longerTermSpotRate = 5
const longerTermDays = 730
const shorterTermSpotRate = 4
const shorterTermDays = 365
const forwardRateDays = calculateForwardRateDays(longerTermSpotRate, longerTermDays, shorterTermSpotRate, shorterTermDays)
console.log('\nExample of Forward Rate calculation in days:\n')
console.log('You could invest at %d% for %d days, compounding daily.', longerTermSpotRate, longerTermDays)
console.log('If you instead invested at %d% for %d days, to earn the same return as the first investment, you\'d need to reinvest the principal and interest from the first investment at %d% for %d days.', shorterTermSpotRate, shorterTermDays, forwardRateDays, longerTermDays - shorterTermDays)
const longerTermYears = 2
const shorterTermYears = 1
const forwardRateYears = calculateForwardRateYears(longerTermSpotRate, longerTermYears, shorterTermSpotRate, shorterTermYears)
console.log('\nExample of Forward Rate calculation in years:\n')
console.log('You could invest at %d% for %d years, compounding annually.', longerTermSpotRate, longerTermYears)
console.log('If you instead invested at %d% for %d years, to earn the same return as the first investment, you\'d need to reinvest the principal and interest from the first investment at %d% for %d years.', shorterTermSpotRate, shorterTermYears, forwardRateYears, longerTermYears - shorterTermYears)
// Example of usage of functions in compoundReturn.js
const principal = 1000
const rate = 5
const years = 3
const days = 365 * 3
const annualReturn = calculateAnnuallyCompoundedReturn(principal, rate, years)
const dailyReturn = calculateDailyCompoundedReturn(principal, rate, days)
console.log('\nExample of calculating annually and daily compounding returns:\n')
console.log('If you invested $%d at a rate of %f% p.a. compounding annually for %d years, you\'d earn a return of $%f before tax. If it was compounding daily, you\'d earn $%f before tax.', principal, rate, years, annualReturn, dailyReturn)
// examples of usage of functions in loanRepayment.js
const loanPrincipal = 10000
const loanRate = 5
const loanYears = 10
const annualLoanPayment = calculateAnnualRepayment(loanPrincipal, loanRate, loanYears)
const monthlyLoanPayment = calculateMonthlyRepayment(loanPrincipal, loanRate, loanYears)
const fortnightlyLoanPayment = calculateFortnightlyRepayment(loanPrincipal, loanRate, loanYears)
const weeklyLoanPayment = calculateWeeklyRepayment(loanPrincipal, loanRate, loanYears)
console.log('\nExample of calculating loan repayments for various payment frequencies:\n')
console.log('If you borrow $%d at a rate of %d% for %d years, your', loanPrincipal, loanRate, loanYears)
console.log('\tannual repayment will be $%f', annualLoanPayment)
console.log('\tmonthly repayment will be $%f', monthlyLoanPayment)
console.log('\tfortnightly repayment will be $%f', fortnightlyLoanPayment)
console.log('\tweekly repayment will be $%f', weeklyLoanPayment)
console.log()