36 of 40 focal problems use RatMath for a calculation or a substantive step.
Direct calculation
36 · 90%
Assisted
0 · 0%
Worked by hand
4 · 10%
Using the packages
Finance methods make cash-flow and valuation conventions explicit in code. The financial meaning still depends on payment timing, risk assumptions and whether rates are nominal, effective or continuous.
Calculator interface
Display an editable timeline with dates, signed flows and a rate selector. Show price sensitivity under parallel yield shifts and distinguish a model value from a tradable quote.
Each of the 40 focal problems has equal weight. Direct means the displayed package calculation answers the computational question after the inputs are set up. Assisted means it contributes a step and the worked solution supplies essential reasoning, interpretation or selection. Worked by hand means the guide relies on a construction, argument or judgment for that task. The helpful share combines direct and assisted days. Courses and instructors assign different mixes of problems. The percentages describe this syllabus.
Classroom inputs and practical limits
36 of 40 days include a checked executable example. On 11 days the measured size sweep exercises a named API used in the lesson. Each day identifies its tested inputs and the mathematical property checked. Hand-worked days give the input scale and the argument to verify.
Ranges across the five units
Days 1–8 · Time value of money. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
Unit benchmark · finance.compound, annual periods. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family.
Days 9–16 · Loans and investment decisions. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
Unit benchmark · finance.annuity, payments. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family.
Days 17–24 · Fixed income. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
Unit benchmark · finance.annuity, payments. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family.
Days 25–32 · Risk-neutral pricing. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
Unit benchmark · finance.binomialTree, time steps. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms).
Days 33–40 · Planning and model risk. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
Unit benchmark · finance.compound, annual periods. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family.
Accuracy notes (1)
Open · Two distinct categorical tuples can merge. A uniform table on the two arrays [‘a,sb’,‘c’] and [‘a’,‘b,sc’] should give each probability 1/2. The table reports one outcome with probability 1 because the composite string keys collide. Use unambiguous scalar IDs for such labels. Ordinary numeric dice faces have distinct keys.
What effective annual rate corresponds to 6% compounded monthly?
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(finance.effectiveRate("6%", 12));
Checked output
0.061677811864499568789707…
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
Focal example. The code above passed its displayed check in 0.290 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.compound · annual periods. Baseline test points across courses 1, 5, 10, 30. At 5%, the balance on 100 is exactly 100·21^n/20^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family. Fixed-period exact compounding raises rational factors to a power, increasing numerator and denominator lengths. Match rate units to periods.
Measured times, memory and outcomes
annual periods · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.433
75.2
5
Baseline
Passed
1.14
76
10
Baseline
Passed
0.565
75.8
30
Baseline
Passed
0.419
75.3
100
Stretch
Passed
0.468
75.6
500
Stretch
Passed
0.905
75.9
2,000
Stretch
Passed
5.59
78.5
10,000
Stretch
Passed
207
89.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.effectiveRate · 1 passing test case, 2 distinct argument descriptions
What does $1000 grow to in 10 years at 5% compounded annually?
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.compound(1000, "5%", 10)));
Checked output
16679880978201/10240000000
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.125 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Same API, measured input family.finance.compound · annual periods. Baseline test points across courses 1, 5, 10, 30. At 5%, the balance on 100 is exactly 100·21^n/20^n.
Practical limit. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family. Fixed-period exact compounding raises rational factors to a power, increasing numerator and denominator lengths. Match rate units to periods.
Measured times, memory and outcomes
annual periods · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.433
75.2
5
Baseline
Passed
1.14
76
10
Baseline
Passed
0.565
75.8
30
Baseline
Passed
0.419
75.3
100
Stretch
Passed
0.468
75.6
500
Stretch
Passed
0.905
75.9
2,000
Stretch
Passed
5.59
78.5
10,000
Stretch
Passed
207
89.3
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.compound · 1 passing test case, 3 distinct argument descriptions
1000, "10%", 2 or 1000, "12%", 1, {periodsPerYear=12} · interest and rates
Day 03 / 40 · Time value of money
Continuous compounding
Direct calculation@ratmath/finance · @ratmath/core
Problem
What does $1000 grow to in 10 years at 5% compounded continuously?
Worked solution
.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import * as finance from "@ratmath/finance";
import { R } from "@ratmath/core";
console.log(R`1000`.multiply(finance.exp(R`5/100 * 10`)));
Checked output
1648.7212707001281468486[5:6]
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
Focal example. The code above passed its displayed check in 0.224 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.compound · annual periods. Baseline test points across courses 1, 5, 10, 30. At 5%, the balance on 100 is exactly 100·21^n/20^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family. Fixed-period exact compounding raises rational factors to a power, increasing numerator and denominator lengths. Match rate units to periods.
Measured times, memory and outcomes
annual periods · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.433
75.2
5
Baseline
Passed
1.14
76
10
Baseline
Passed
0.565
75.8
30
Baseline
Passed
0.419
75.3
100
Stretch
Passed
0.468
75.6
500
Stretch
Passed
0.905
75.9
2,000
Stretch
Passed
5.59
78.5
10,000
Stretch
Passed
207
89.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
Find the present value of $100 paid at the end of each year for 10 years at 5%.
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.annuity({ payment: 100, rate: "5%", periods: 10 }).presentValue));
Checked output
12879761956402000/16679880978201
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.241 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Same API, measured input family.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.annuity · 2 passing test cases, 6 distinct argument descriptions
Find the present value of ten payments of $100 each, at the start of each year, at an annual effective rate of 5%.
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(
String(finance.annuity({ payment: 100, rate: "5%", periods: 10, due: true }).presentValue)
);
Checked output
643988097820100/794280046581
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.121 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Same API, measured input family.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.annuity · 2 passing test cases, 6 distinct argument descriptions
A perpetuity pays $100 next year, growing by 2% a year after that. At 6%, what is it worth?
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.perpetuity({ payment: 100, rate: "6%", growth: "2%" })));
Checked output
2500
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
Focal example. The code above passed its displayed check in 0.123 ms on Node.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.perpetuity · 1 passing test case, 2 distinct argument descriptions
An asset earns 8% while prices rise 5%. Find the exact real one-period return.
Worked solution
Divide the nominal accumulation factor by the inflation factor and subtract one.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`108/105-1`));
Checked output
1/35
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
Focal example. The code above passed its displayed check in 0.071 ms on Node.
Same arithmetic expression. Replaced its first numeral 108 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.add · denominator. Baseline test points across courses 1, 2, 3, 12, 100, 1,000. Cross-multiply 1/n + 1/(n+1) against (2n+1)/(n(n+1)).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000,007 (0.038 ms, 76.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact work depends on the bit lengths of both numerator and denominator. A long sum of relatively prime fractions can build a large common denominator.
Measured times, memory and outcomes
denominator · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.034
74.9
2
Baseline
Passed
0.036
75.6
3
Baseline
Passed
0.040
75.9
12
Baseline
Passed
0.039
75.2
100
Baseline
Passed
0.041
75
1,000
Baseline
Passed
0.035
75.8
1,000,003
Stretch
Passed
0.044
75
1,000,000,007
Stretch
Passed
0.038
76.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
How much simple interest does $1200 earn at 3.5% a year for 4 years?
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.simpleInterest(1200, "0.035", 4).interest));
Checked output
168
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Ordinary principal amounts, annual rates roughly 0–20%, and about 1–40 annual or up to 360 monthly periods.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.256 ms on Node.
Same API, measured input family.finance.simpleInterest · years at 5% per year. Baseline test points across courses 0, 1, 5, 10, 30. Interest on 100 at 5% per year is 5n, giving balance 100+5n.
Practical limit. Largest passing sample 100,000 (0.378 ms, 74.2 MiB peak process RSS). The runs did not locate a ceiling for this family. The time and rate must use the same unit. Large decimal inputs first parsed as JavaScript Numbers can already have lost information.
Measured times, memory and outcomes
years at 5% per year · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
0.291
76.1
1
Baseline
Passed
0.365
74.3
5
Baseline
Passed
0.446
75.1
10
Baseline
Passed
0.565
74.9
30
Baseline
Passed
0.280
75
100
Stretch
Passed
0.345
75.6
1,000
Stretch
Passed
0.289
76.3
100,000
Stretch
Passed
0.378
74.2
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.simpleInterest · 1 passing test case, 1 distinct argument description
Find the monthly payment on a $250,000, 30-year loan at 6% a year compounded monthly.
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.loan({ principal: 250000, rate: "6%", periods: 360 }).payment));
Checked output
37472/25
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 5.28 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Same API, measured input family.finance.loan · monthly payments. Baseline test points across courses 1, 12, 60, 360. At 6% nominal annual interest with monthly payments, the unrounded level payment on 100 equals (1/2)·201^n/(201^n−200^n).
Practical limit. Largest passing sample 600 (48.69 ms, 88.1 MiB peak process RSS). Library refusal at 1,200, 2,400. A schedule may retain one row per payment and exact balances can grow rapidly. Loan rounding conventions and the compounding frequency affect the result.
Measured times, memory and outcomes
monthly payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.963
74.7
12
Baseline
Passed
1.58
75.5
60
Baseline
Passed
3.64
76.7
360
Baseline
Passed
10.66
81.7
600
Stretch
Passed
48.69
88.1
1,200
Stretch
Library refusal · FinanceError: in period 1 the payment 0.50 does not cover the interest 0.50
13.09
81.6
2,400
Stretch
Library refusal · FinanceError: in period 1 the payment 0.50 does not cover the interest 0.50
87.10
89
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.loan · 5 passing test cases, 8 distinct argument descriptions
On a $250,000, 30-year amortizing loan at nominal 6% compounded monthly, add $200 to every scheduled monthly payment. How many payments are needed?
Worked solution
Running the balance forward, , it reaches zero at the 267th payment.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(finance.loan({ principal: 250000, rate: "6%", periods: 360, extra: 200 }).periods);
Checked output
267
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 2.65 ms on Node.
Same API, measured input family.finance.loan · monthly payments. Baseline test points across courses 1, 12, 60, 360. At 6% nominal annual interest with monthly payments, the unrounded level payment on 100 equals (1/2)·201^n/(201^n−200^n).
Practical limit. Largest passing sample 600 (48.69 ms, 88.1 MiB peak process RSS). Library refusal at 1,200, 2,400. A schedule may retain one row per payment and exact balances can grow rapidly. Loan rounding conventions and the compounding frequency affect the result.
Measured times, memory and outcomes
monthly payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.963
74.7
12
Baseline
Passed
1.58
75.5
60
Baseline
Passed
3.64
76.7
360
Baseline
Passed
10.66
81.7
600
Stretch
Passed
48.69
88.1
1,200
Stretch
Library refusal · FinanceError: in period 1 the payment 0.50 does not cover the interest 0.50
13.09
81.6
2,400
Stretch
Library refusal · FinanceError: in period 1 the payment 0.50 does not cover the interest 0.50
87.10
89
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.loan · 5 passing test cases, 8 distinct argument descriptions
A project costs $1000 now and returns $300, $400 and $500 at the ends of the next three years. Find its NPV at 10%.
Worked solution
: not worth it at 10%.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.npv("10%", [-1000, 300, 400, 500])));
Checked output
-28000/1331
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.280 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.npv · 2 passing test cases, 7 distinct argument descriptions
"10%", [-1000, 300, 400, 500, …] (n=5) or 0, [-1000, 300, 400, 500, …] (n=5) · net present value
21 decimal characters/22 decimal characters, [-1000, 300, 400, 500, …] (n=5) or 22 decimal characters/22 decimal characters, [-1000, 300, 400, 500, …] (n=5) · internal rates of return: all of them
Day 12 / 40 · Loans and investment decisions
Internal rate of return
Direct calculation@ratmath/finance
Problem
A project costs $1000 now and pays $300, $400 and $500 at the ends of years one through three. Find its annual internal rate of return.
Worked solution
The root of with : .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(finance.irr([-1000, 300, 400, 500]).rate);
Checked output
0.0889633946933499353[1:2]
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 1.05 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.irr · 1 passing test case, 6 distinct argument descriptions
$2000 is invested at 6% a year compounded monthly. What is it worth after 5 years, to the cent?
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(finance.compound(2000, "0.06", 5, { periodsPerYear: 12 }));
Checked output
2697.700305098632138692018344…
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.444 ms on Node.
Same API, measured input family.finance.compound · annual periods. Baseline test points across courses 1, 5, 10, 30. At 5%, the balance on 100 is exactly 100·21^n/20^n.
Practical limit. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family. Fixed-period exact compounding raises rational factors to a power, increasing numerator and denominator lengths. Match rate units to periods.
Measured times, memory and outcomes
annual periods · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.433
75.2
5
Baseline
Passed
1.14
76
10
Baseline
Passed
0.565
75.8
30
Baseline
Passed
0.419
75.3
100
Stretch
Passed
0.468
75.6
500
Stretch
Passed
0.905
75.9
2,000
Stretch
Passed
5.59
78.5
10,000
Stretch
Passed
207
89.3
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.compound · 1 passing test case, 3 distinct argument descriptions
1000, "10%", 2 or 1000, "12%", 1, {periodsPerYear=12} · interest and rates
Day 14 / 40 · Loans and investment decisions
Infinite geometric series
Direct calculation@ratmath/symbolic
Problem
Find .
Worked solution
: .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as symbolic from "@ratmath/symbolic";
console.log(String(symbolic.sum("12*(1/3)^k", "k", 0, "inf").value));
Checked output
18
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
Focal example. The code above passed its displayed check in 15.56 ms on Node.
Related workload in this unit.core.range · terms. Baseline test points across courses 1, 10, 100, 1,000. Check length and every consecutive integer from 1 through n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000 (141 ms, 277.5 MiB peak process RSS). The first sampled passing call above 100 ms used 1,000,000 (141 ms). The runs did not locate a ceiling for this family. Keeping the entire result requires storage proportional to its length. Shortening the displayed list after construction still pays that cost.
Measured times, memory and outcomes
terms · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.173
75.5
10
Baseline
Passed
0.192
74.9
100
Baseline
Passed
0.269
74.7
1,000
Baseline
Passed
1.52
75.6
10,000
Stretch
Passed
5.29
80.9
100,000
Stretch
Passed
11.57
109.6
1,000,000
Stretch
Passed
141
277.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
symbolic.sum · 4 passing test cases, 14 distinct argument descriptions
For cash flows −100, +230, −132, can two positive annual IRRs exist?
Worked solution
Multiplying the NPV equation by (1+r)² yields 100(1+r)²−230(1+r)+132=0. Its roots in 1+r are 1.1 and 1.2, giving 10% and 20%.
Using RatMath
Write the equation and name the variable to solve for. Check returned candidates against the original domain and any restrictions in the question.
JavaScript module
import * as symbolic from "@ratmath/symbolic";
console.log(String(symbolic.solve("100*(1+r)^2-230*(1+r)+132=0","r")));
Checked output
r = 1/10, 1/5
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
For this problem type. Start with one unknown, small signed coefficients and degree one or two. Higher-degree or parameter-dependent equations need separate cases.
Focal example. The code above passed its displayed check in 1.22 ms on Node.
Same API, measured input family.symbolic.solve · absolute coefficient. Baseline test points across courses 1, 2, 10, 100. Solve n·x+1=3n+1; the unique root is 3.
Practical limit. Largest passing sample 1,000,000,000 (6.48 ms, 75.9 MiB peak process RSS). The runs did not locate a ceiling for this family. One linear unknown has a simple root here. A vanishing coefficient may produce no solution or infinitely many. Multiple parameters require case analysis.
Measured times, memory and outcomes
absolute coefficient · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
16.76
75.8
2
Baseline
Passed
7.72
74.9
10
Baseline
Passed
29.10
75.2
100
Baseline
Passed
12.79
74.6
10,000
Stretch
Passed
28.50
76.3
1,000,000,000
Stretch
Passed
6.48
75.9
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
symbolic.solve · 21 passing test cases, 71 distinct argument descriptions
At zero interest what equal end-of-year deposit builds 12,000 in five years?
Worked solution
Divide the target by five. This is also the zero-rate limit of the annuity formula.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`12000/5`));
Checked output
2400
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Cash flows over a few years or up to about 360 loan payments, with positive principal and a stated compounding convention.
Focal example. The code above passed its displayed check in 0.129 ms on Node.
Same arithmetic expression. Replaced its first numeral 12000 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.add · denominator. Baseline test points across courses 1, 2, 3, 12, 100, 1,000. Cross-multiply 1/n + 1/(n+1) against (2n+1)/(n(n+1)).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000,007 (0.038 ms, 76.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact work depends on the bit lengths of both numerator and denominator. A long sum of relatively prime fractions can build a large common denominator.
Measured times, memory and outcomes
denominator · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.034
74.9
2
Baseline
Passed
0.036
75.6
3
Baseline
Passed
0.040
75.9
12
Baseline
Passed
0.039
75.2
100
Baseline
Passed
0.041
75
1,000
Baseline
Passed
0.035
75.8
1,000,003
Stretch
Passed
0.044
75
1,000,000,007
Stretch
Passed
0.038
76.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
A 10-year bond with face 100 pays a 5% coupon semi-annually. Price it at a yield of 6% (compounded semi-annually).
Worked solution
.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(
finance.bond({ face: 100, coupon: "5%", frequency: 2, periods: 20 }).price("6%").clean
);
Checked output
92.561262569772246586820875…
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.338 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.bond · 3 passing test cases, 4 distinct argument descriptions
A 10-year bond with face value 100 pays a 5% nominal annual coupon semiannually. At what nominal yield compounded semiannually does it sell at par?
Worked solution
At par the yield equals the coupon rate: 5%.
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(
String(finance.bond({ face: 100, coupon: "5%", frequency: 2, periods: 20 }).yield(100))
);
Checked output
1/20
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 4.62 ms on Node.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.bond · 3 passing test cases, 4 distinct argument descriptions
{coupon="4.5%", settlement="2027-03-15", maturity="2032-11-15"} or {coupon="5%", settlement="2027-05-15", maturity="2037-05-15"} · between coupon dates
Day 19 / 40 · Fixed income
Duration
Direct calculation@ratmath/finance
Problem
Find the Macaulay and modified durations of the bond at a 6% yield.
Worked solution
Macaulay years; modified .
Using RatMath
Match the interest convention, cash-flow dates and number of periods to the question. Keep intermediate values at full precision and round money at the requested stage.
JavaScript module
import * as finance from "@ratmath/finance";
const d = finance.bond({ face: 100, coupon: "5%", frequency: 2, periods: 20 }).duration("6%");
console.log([d.macaulay, d.modified]);
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.570 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.bond · 3 passing test cases, 4 distinct argument descriptions
{coupon="4.5%", settlement="2027-03-15", maturity="2032-11-15"} or {coupon="5%", settlement="2027-05-15", maturity="2037-05-15"} · between coupon dates
Day 20 / 40 · Fixed income
Forward rates
Direct calculation@ratmath/finance
Problem
The one-year spot rate is 4% and the two-year spot rate is 5%. Find the one-year forward rate a year from now.
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.yieldCurve({ spots: ["4%", "5%"] }).forward(1, 2)));
Checked output
25/416
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
For this problem type. Use a few annual cash flows or 12–360 monthly periods, usually with moderate annual rates. Round monetary payments under the contract’s stated convention.
Focal example. The code above passed its displayed check in 0.197 ms on Node.
Related workload in this unit.finance.annuity · payments. Baseline test points across courses 1, 12, 60, 360. At 5% per period a unit annuity-immediate has PV 20·(1−(20/21)^n).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 20,000 (2,553 ms, 89.5 MiB peak process RSS). The first sampled passing call above one second used 20,000 (2,553 ms). The runs did not locate a ceiling for this family. Exact sums can contain very large fractions even when the monetary answer needs only cents. Distinguish payment timing and the zero-rate case.
Measured times, memory and outcomes
payments · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.461
75.3
12
Baseline
Passed
0.521
75.2
60
Baseline
Passed
0.538
75.1
360
Baseline
Passed
1.08
75.3
1,000
Stretch
Passed
4.23
77.9
5,000
Stretch
Passed
117
89.3
20,000
Stretch
Passed
2,553
89.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.yieldCurve · 1 passing test case, 2 distinct argument descriptions
{spots=["3%", "3.5%", "4%"] (n=3)} or {spots=["4%", "4%"] (n=2), frequency=2} · spots and forwards
Day 21 / 40 · Fixed income
Linear approximation
Direct calculation@ratmath/symbolic
Problem
Use a linear approximation to estimate .
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as symbolic from "@ratmath/symbolic";
console.log(String(symbolic.taylor("sqrt(x)", "x", 4, 1)));
Checked output
2 + 1/4*(x - 4) + …
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
For this problem type. Typical hand calculations keep 2–10 nonzero terms. Specify the expansion center and distance of the evaluation point from it.
Focal example. The code above passed its displayed check in 0.658 ms on Node.
Related workload in this unit.symbolic.sym.diff · degree. Baseline test points across courses 1, 2, 4, 8. Derivative of x^n at 2 is n·2^(n−1).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,024 (0.459 ms, 74.4 MiB peak process RSS). The runs did not locate a ceiling for this family. A monomial differentiates to one term. Products, repeated derivatives and expression expansion can make intermediate expressions much larger.
Measured times, memory and outcomes
degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.157
75.7
2
Baseline
Passed
0.192
75.7
4
Baseline
Passed
0.276
75.2
8
Baseline
Passed
0.642
75.5
16
Stretch
Passed
18.83
74.7
64
Stretch
Passed
0.406
75
256
Stretch
Passed
0.241
75.3
1,024
Stretch
Passed
0.459
74.4
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
symbolic.taylor · 2 passing test cases, 7 distinct argument descriptions
A bond pays 121 in two years. Price it at annual effective yield 10%.
Worked solution
Discount the only payment by two annual accumulation factors.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`121/(11/10)^2`));
Checked output
100
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
Focal example. The code above passed its displayed check in 0.151 ms on Node.
Same arithmetic expression. Replaced its first numeral 121 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.multiply · operand bits. Baseline test points across courses 8, 32, 64, 256. Cross-multiply the unreduced integer products; operands have unlike denominators.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 131,072 (1.53 ms, 75.5 MiB peak process RSS). The runs did not locate a ceiling for this family. Intermediate products can have twice the bit length of their factors. Repeated squaring can exhaust the engine’s BigInt allocation limit in a small number of steps.
Measured times, memory and outcomes
operand bits · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
8
Baseline
Passed
0.317
74.6
32
Baseline
Passed
0.035
75.5
64
Baseline
Passed
0.045
75.1
256
Baseline
Passed
0.106
74
1,024
Stretch
Passed
0.035
75.3
8,192
Stretch
Passed
0.075
75.8
32,768
Stretch
Passed
0.245
75
131,072
Stretch
Passed
1.53
75.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
A position has market value 500 and modified duration 4. Estimate its change for a 0.01 rise in yield.
Worked solution
The first-order approximation is −D times value times the yield change, giving a decrease of 20 in currency units.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`-4*500/100`));
Checked output
-20
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
Focal example. The code above passed its displayed check in 0.064 ms on Node.
Same arithmetic expression. Replaced its first numeral 4 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.add · denominator. Baseline test points across courses 1, 2, 3, 12, 100, 1,000. Cross-multiply 1/n + 1/(n+1) against (2n+1)/(n(n+1)).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000,007 (0.038 ms, 76.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact work depends on the bit lengths of both numerator and denominator. A long sum of relatively prime fractions can build a large common denominator.
Measured times, memory and outcomes
denominator · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.034
74.9
2
Baseline
Passed
0.036
75.6
3
Baseline
Passed
0.040
75.9
12
Baseline
Passed
0.039
75.2
100
Baseline
Passed
0.041
75
1,000
Baseline
Passed
0.035
75.8
1,000,003
Stretch
Passed
0.044
75
1,000,000,007
Stretch
Passed
0.038
76.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
Why can a coupon bond’s realized compounded return differ from its initial yield to maturity?
Worked solution
Coupons may have to be reinvested at changing rates. Selling early can also change the terminal value.
Working without a calculator
Use the argument or construction in the worked solution. This lesson assigns the reasoning to the student.
Input range and limits
Typical inputs for this unit. Bonds with tens of coupon dates and yields usually quoted to basis points; align compounding and coupon frequency.
Scope of verification. This day asks for a hand argument, construction or interpretation. The worked instance gives the input. General claims need the reasoning shown above. A measured time, memory ceiling or finite input sweep would not verify that claim.
Day 25 / 40 · Risk-neutral pricing
Black–Scholes
Direct calculation@ratmath/finance
Problem
Price a one-year European call with , , .
Worked solution
, ; .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(
finance.blackScholes({ spot: 100, strike: 100, rate: "5%", volatility: "20%", time: 1 }).call
);
Checked output
10.4505835721855667816[5:6]
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Hand trees usually use 1–3 steps, classroom numerical trees about 5–25. Use consistent per-step factors and volatility/time units.
Focal example. The code above passed its displayed check in 4.31 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.binomialTree · time steps. Baseline test points across courses 1, 2, 5, 10, 25. A zero-strike European call on a non-dividend-paying stock is worth the spot. Exact factors satisfy d<g<u.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms). Backward induction works through a triangular collection of nodes. Exact fractions grow with depth. The fast floating mode changes the precision contract.
Measured times, memory and outcomes
time steps · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.675
75.5
2
Baseline
Passed
0.879
75.8
5
Baseline
Passed
0.656
76.8
10
Baseline
Passed
1.51
75.6
25
Baseline
Passed
3.30
78.6
50
Stretch
Passed
11.26
83.3
100
Stretch
Passed
56.40
90.2
250
Stretch
Passed
703
90.7
2,001
Stretch
Library refusal · FinanceError: an exact tree is limited to 2000 steps (its fractions grow with every step); use float: true for a quick preview
0.330
75.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.blackScholes · 3 passing test cases, 9 distinct argument descriptions
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Hand trees usually use 1–3 steps, classroom numerical trees about 5–25. Use consistent per-step factors and volatility/time units.
Focal example. The code above passed its displayed check in 3.35 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.finance.binomialTree · time steps. Baseline test points across courses 1, 2, 5, 10, 25. A zero-strike European call on a non-dividend-paying stock is worth the spot. Exact factors satisfy d<g<u.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms). Backward induction works through a triangular collection of nodes. Exact fractions grow with depth. The fast floating mode changes the precision contract.
Measured times, memory and outcomes
time steps · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.675
75.5
2
Baseline
Passed
0.879
75.8
5
Baseline
Passed
0.656
76.8
10
Baseline
Passed
1.51
75.6
25
Baseline
Passed
3.30
78.6
50
Stretch
Passed
11.26
83.3
100
Stretch
Passed
56.40
90.2
250
Stretch
Passed
703
90.7
2,001
Stretch
Library refusal · FinanceError: an exact tree is limited to 2000 steps (its fractions grow with every step); use float: true for a quick preview
0.330
75.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.blackScholes · 3 passing test cases, 9 distinct argument descriptions
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Hand trees usually use 1–3 steps, classroom numerical trees about 5–25. Use consistent per-step factors and volatility/time units.
Focal example. The code above passed its displayed check in 0.388 ms on Node.
Same API, measured input family.finance.binomialTree · time steps. Baseline test points across courses 1, 2, 5, 10, 25. A zero-strike European call on a non-dividend-paying stock is worth the spot. Exact factors satisfy d<g<u.
Practical limit. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms). Backward induction works through a triangular collection of nodes. Exact fractions grow with depth. The fast floating mode changes the precision contract.
Measured times, memory and outcomes
time steps · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.675
75.5
2
Baseline
Passed
0.879
75.8
5
Baseline
Passed
0.656
76.8
10
Baseline
Passed
1.51
75.6
25
Baseline
Passed
3.30
78.6
50
Stretch
Passed
11.26
83.3
100
Stretch
Passed
56.40
90.2
250
Stretch
Passed
703
90.7
2,001
Stretch
Library refusal · FinanceError: an exact tree is limited to 2000 steps (its fractions grow with every step); use float: true for a quick preview
0.330
75.5
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.binomialTree · 1 passing test case, 3 distinct argument descriptions
Implementation limit. Exact binomial trees are capped at 2,000 steps. Floating trees share the overall 100,000-step input guard and use approximate arithmetic. Source at the tested revision
Day 28 / 40 · Risk-neutral pricing
Option strategies
Direct calculation@ratmath/finance
Problem
A straddle buys a call (premium 5) and a put (premium 4), both struck at 100. Where does it break even?
Worked solution
It costs 9, so it breaks even when the stock is away from 100: at 91 and 109.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Hand trees usually use 1–3 steps, classroom numerical trees about 5–25. Use consistent per-step factors and volatility/time units.
Focal example. The code above passed its displayed check in 0.371 ms on Node.
Related workload in this unit.finance.binomialTree · time steps. Baseline test points across courses 1, 2, 5, 10, 25. A zero-strike European call on a non-dividend-paying stock is worth the spot. Exact factors satisfy d<g<u.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms). Backward induction works through a triangular collection of nodes. Exact fractions grow with depth. The fast floating mode changes the precision contract.
Measured times, memory and outcomes
time steps · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.675
75.5
2
Baseline
Passed
0.879
75.8
5
Baseline
Passed
0.656
76.8
10
Baseline
Passed
1.51
75.6
25
Baseline
Passed
3.30
78.6
50
Stretch
Passed
11.26
83.3
100
Stretch
Passed
56.40
90.2
250
Stretch
Passed
703
90.7
2,001
Stretch
Library refusal · FinanceError: an exact tree is limited to 2000 steps (its fractions grow with every step); use float: true for a quick preview
0.330
75.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.strategy · 1 passing test case, 3 distinct argument descriptions
[{type="call", strike=100, premium=5}, {type="put", strike=100, premium=4}] (n=2) or [{type="call", strike=90, premium=12}, {type="call", strike=110, premium=3, quantity=-1}] (n=2) · strategies: break-even prices make the profit exactly zero
Day 29 / 40 · Risk-neutral pricing
Conditioning
Direct calculation@ratmath/stats · @ratmath/core
Problem
Roll a die. If it is even, flip two coins; otherwise flip one. What is the expected number of heads?
Worked solution
.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import * as stats from "@ratmath/stats";
import { R } from "@ratmath/core";
console.log(String(stats.die().expect((d) => (d % 2 === 0 ? R`1` : R`1/2`))));
Checked output
3/4
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Use a few events or a table of roughly 2–36 elementary outcomes. Two to four independent dice are common; verify whether the question needs ordered tuples or a sum.
Focal example. The code above passed its displayed check in 0.249 ms on Node.
Related workload in this unit.stats.die.power · six-sided dice in the ordered sample space. Baseline test points across courses 1, 2, 3, 4. The implicit product has 6^n ordered tuples. Check the exact count and the probability of the all-sixes tuple without listing it.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (2.08 ms, 76.7 MiB peak process RSS). The runs did not locate a ceiling for this family. An independent product stores its factors. Point lookup multiplies one weight per die. An arbitrary predicate may visit 6ⁿ tuples; asking for all entries also consumes exponential space.
Measured times, memory and outcomes
six-sided dice in the ordered sample space · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.650
74.9
2
Baseline
Passed
0.654
75.1
3
Baseline
Passed
0.953
75.2
4
Baseline
Passed
0.755
75.3
7
Stretch
Passed
0.751
75.6
9
Stretch
Passed
0.788
75.6
32
Stretch
Passed
1.68
75.5
100
Stretch
Passed
2.08
76.7
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
stats.die · 4 passing test cases, 2 distinct argument descriptions
Seven ordered dice. The current product represents 279,936 outcomes in 0.34 ms and streams the order-sensitive probability check in 33.46 ms. Accessing .entries materializes all tuples. For the sum, stats.die().sumOf(7) needs only 36 totals. Compare the representations and their measured costs
Accuracy notes (1)
Open · Two distinct categorical tuples can merge. A uniform table on the two arrays [‘a,sb’,‘c’] and [‘a’,‘b,sc’] should give each probability 1/2. The table reports one outcome with probability 1 because the composite string keys collide. Use unambiguous scalar IDs for such labels. Ordinary numeric dice faces have distinct keys.
Day 30 / 40 · Risk-neutral pricing
Bayes with urns
Direct calculation@ratmath/stats
Problem
Urn 1 has 3 red and 2 blue balls, urn 2 has 1 red and 4 blue. An urn is chosen at random and a red ball drawn. What is the probability it came from urn 1?
Worked solution
.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as stats from "@ratmath/stats";
console.log(
String(stats.bayes({ one: "1/2", two: "1/2" }, { one: "3/5", two: "1/5" }).posterior.p("one"))
);
Checked output
3/4
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
For this problem type. Use a few events or a table of roughly 2–36 elementary outcomes. Two to four independent dice are common; verify whether the question needs ordered tuples or a sum.
Focal example. The code above passed its displayed check in 0.139 ms on Node.
Related workload in this unit.finance.binomialTree · time steps. Baseline test points across courses 1, 2, 5, 10, 25. A zero-strike European call on a non-dividend-paying stock is worth the spot. Exact factors satisfy d<g<u.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 250 (703 ms, 90.7 MiB peak process RSS). Library refusal at 2,001. The first sampled passing call above 100 ms used 250 (703 ms). Backward induction works through a triangular collection of nodes. Exact fractions grow with depth. The fast floating mode changes the precision contract.
Measured times, memory and outcomes
time steps · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.675
75.5
2
Baseline
Passed
0.879
75.8
5
Baseline
Passed
0.656
76.8
10
Baseline
Passed
1.51
75.6
25
Baseline
Passed
3.30
78.6
50
Stretch
Passed
11.26
83.3
100
Stretch
Passed
56.40
90.2
250
Stretch
Passed
703
90.7
2,001
Stretch
Library refusal · FinanceError: an exact tree is limited to 2000 steps (its fractions grow with every step); use float: true for a quick preview
0.330
75.5
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
stats.bayes · 1 passing test case, 3 distinct argument descriptions
{sick="1/100", well="99/100"}, {sick="95/100", well="5/100"} or Finite, function · Bayes' rule
Day 31 / 40 · Risk-neutral pricing
Risk-neutral weight
Direct calculation@ratmath/core
Problem
A stock can grow by 1.2 or 0.9 in one step. The risk-free growth factor is 1.05. Find the risk-neutral up weight.
Worked solution
Match expected growth to 1.05, giving (1.05−0.9)/(1.2−0.9)=1/2.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`(105-90)/(120-90)`));
Checked output
1/2
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
Focal example. The code above passed its displayed check in 0.107 ms on Node.
Same arithmetic expression. Replaced its first numeral 105 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.add · denominator. Baseline test points across courses 1, 2, 3, 12, 100, 1,000. Cross-multiply 1/n + 1/(n+1) against (2n+1)/(n(n+1)).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000,007 (0.038 ms, 76.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact work depends on the bit lengths of both numerator and denominator. A long sum of relatively prime fractions can build a large common denominator.
Measured times, memory and outcomes
denominator · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.034
74.9
2
Baseline
Passed
0.036
75.6
3
Baseline
Passed
0.040
75.9
12
Baseline
Passed
0.039
75.2
100
Baseline
Passed
0.041
75
1,000
Baseline
Passed
0.035
75.8
1,000,003
Stretch
Passed
0.044
75
1,000,000,007
Stretch
Passed
0.038
76.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
Why require d<R<u in a two-state binomial model with both states possible?
Worked solution
The strict inequalities give positive state prices. Outside them the bond or stock weakly dominates the other, strictly in at least one state, after matching initial cost.
Working without a calculator
Use the argument or construction in the worked solution. This lesson assigns the reasoning to the student.
Input range and limits
Typical inputs for this unit. One- and two-step hand trees, about 5–25 steps for classroom computation, or a few option legs. State the model, rate and volatility units.
Scope of verification. This day asks for a hand argument, construction or interpretation. The worked instance gives the input. General claims need the reasoning shown above. A measured time, memory ceiling or finite input sweep would not verify that claim.
Day 33 / 40 · Planning and model risk
Depreciation
Direct calculation@ratmath/finance
Problem
A machine costs $10,000, lasts 5 years and is sold for $1000. Find the yearly straight-line depreciation and the first year's double-declining-balance depreciation.
Worked solution
Straight line: . Double declining: of $10,000 .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
const s = finance.depreciation({ cost: 10000, salvage: 1000, life: 5 });
const d = finance.depreciation({ cost: 10000, salvage: 1000, life: 5, method: "declining-balance" });
console.log([s[0].depreciation, d[0].depreciation]);
Checked output
[1800, 4000]
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
Focal example. The code above passed its displayed check in 0.287 ms on Node.
Related workload in this unit.finance.compound · annual periods. Baseline test points across courses 1, 5, 10, 30. At 5%, the balance on 100 is exactly 100·21^n/20^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (207 ms, 89.3 MiB peak process RSS). The first sampled passing call above 100 ms used 10,000 (207 ms). The runs did not locate a ceiling for this family. Fixed-period exact compounding raises rational factors to a power, increasing numerator and denominator lengths. Match rate units to periods.
Measured times, memory and outcomes
annual periods · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.433
75.2
5
Baseline
Passed
1.14
76
10
Baseline
Passed
0.565
75.8
30
Baseline
Passed
0.419
75.3
100
Stretch
Passed
0.468
75.6
500
Stretch
Passed
0.905
75.9
2,000
Stretch
Passed
5.59
78.5
10,000
Stretch
Passed
207
89.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
finance.depreciation · 1 passing test case, 6 distinct argument descriptions
{cost=10000, salvage=1000, life=5} or {cost=10000, salvage=1000, life=5, method="declining-balance"} · depreciation
Day 34 / 40 · Planning and model risk
Life expectancy
Direct calculation@ratmath/finance
Problem
Under de Moivre's law with limiting age 100, what is the complete life expectancy at age 40?
Worked solution
The future lifetime is uniform on , so .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as finance from "@ratmath/finance";
console.log(String(finance.deMoivre(100).completeExpectation(40)));
Checked output
30
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
For this problem type. Use a limiting age around 80–120 years and a current age strictly below it when conditioning on survival.
Focal example. The code above passed its displayed check in 0.382 ms on Node.
Same API, measured input family.finance.deMoivre.completeExpectation · current age; limiting age 100. Baseline test points across courses 0, 20, 40, 65, 90. Under de Moivre’s uniform lifetime law, remaining life at age x is uniform on [0,100−x] and has mean (100−x)/2.
Practical limit. Largest passing sample 99 (0.647 ms, 75.4 MiB peak process RSS). The runs did not locate a ceiling for this family. An age in this finite-lifetime model must lie in the model’s support. An expectation describes a population and depends on its survival law.
Measured times, memory and outcomes
current age; limiting age 100 · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
1.49
74.9
20
Baseline
Passed
0.833
75.3
40
Baseline
Passed
1.85
75.1
65
Baseline
Passed
0.828
75.7
90
Baseline
Passed
0.582
75.4
98
Stretch
Passed
0.523
76
99
Stretch
Passed
0.647
75.4
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Day 35 / 40 · Planning and model risk
Expected value
Direct calculation@ratmath/stats
Problem
A game costs $1. Roll a die: on a 6 you get $6 back (a $5 gain), otherwise nothing. What is the expected gain?
Worked solution
. A fair game.
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
JavaScript module
import * as stats from "@ratmath/stats";
console.log(String(stats.die().mean((x) => (x === 6 ? 5 : -1))));
Checked output
0
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
For this problem type. Use a few events or a table of roughly 2–36 elementary outcomes. Two to four independent dice are common; verify whether the question needs ordered tuples or a sum.
Focal example. The code above passed its displayed check in 0.132 ms on Node.
Related workload in this unit.stats.die.power · six-sided dice in the ordered sample space. Baseline test points across courses 1, 2, 3, 4. The implicit product has 6^n ordered tuples. Check the exact count and the probability of the all-sixes tuple without listing it.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (2.08 ms, 76.7 MiB peak process RSS). The runs did not locate a ceiling for this family. An independent product stores its factors. Point lookup multiplies one weight per die. An arbitrary predicate may visit 6ⁿ tuples; asking for all entries also consumes exponential space.
Measured times, memory and outcomes
six-sided dice in the ordered sample space · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.650
74.9
2
Baseline
Passed
0.654
75.1
3
Baseline
Passed
0.953
75.2
4
Baseline
Passed
0.755
75.3
7
Stretch
Passed
0.751
75.6
9
Stretch
Passed
0.788
75.6
32
Stretch
Passed
1.68
75.5
100
Stretch
Passed
2.08
76.7
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
stats.die · 4 passing test cases, 2 distinct argument descriptions
Seven ordered dice. The current product represents 279,936 outcomes in 0.34 ms and streams the order-sensitive probability check in 33.46 ms. Accessing .entries materializes all tuples. For the sum, stats.die().sumOf(7) needs only 36 totals. Compare the representations and their measured costs
Accuracy notes (1)
Open · Two distinct categorical tuples can merge. A uniform table on the two arrays [‘a,sb’,‘c’] and [‘a’,‘b,sc’] should give each probability 1/2. The table reports one outcome with probability 1 because the composite string keys collide. Use unambiguous scalar IDs for such labels. Ordinary numeric dice faces have distinct keys.
Day 36 / 40 · Planning and model risk
Covariance
Direct calculation@ratmath/stats
Problem
takes with probabilities 0.4, 0.1, 0.2, 0.3. Find .
Worked solution
, , : .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
For this problem type. Use a few events or a table of roughly 2–36 elementary outcomes. Two to four independent dice are common; verify whether the question needs ordered tuples or a sum.
Focal example. The code above passed its displayed check in 0.320 ms on Node.
Related workload in this unit.stats.die.power · six-sided dice in the ordered sample space. Baseline test points across courses 1, 2, 3, 4. The implicit product has 6^n ordered tuples. Check the exact count and the probability of the all-sixes tuple without listing it.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (2.08 ms, 76.7 MiB peak process RSS). The runs did not locate a ceiling for this family. An independent product stores its factors. Point lookup multiplies one weight per die. An arbitrary predicate may visit 6ⁿ tuples; asking for all entries also consumes exponential space.
Measured times, memory and outcomes
six-sided dice in the ordered sample space · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.650
74.9
2
Baseline
Passed
0.654
75.1
3
Baseline
Passed
0.953
75.2
4
Baseline
Passed
0.755
75.3
7
Stretch
Passed
0.751
75.6
9
Stretch
Passed
0.788
75.6
32
Stretch
Passed
1.68
75.5
100
Stretch
Passed
2.08
76.7
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
stats.finite · 7 passing test cases, 14 distinct argument descriptions
Seven ordered dice. The current product represents 279,936 outcomes in 0.34 ms and streams the order-sensitive probability check in 33.46 ms. Accessing .entries materializes all tuples. For the sum, stats.die().sumOf(7) needs only 36 totals. Compare the representations and their measured costs
Accuracy notes (1)
Open · Two distinct categorical tuples can merge. A uniform table on the two arrays [‘a,sb’,‘c’] and [‘a’,‘b,sc’] should give each probability 1/2. The table reports one outcome with probability 1 because the composite string keys collide. Use unambiguous scalar IDs for such labels. Ordinary numeric dice faces have distinct keys.
Day 37 / 40 · Planning and model risk
Multiple regression
Direct calculation@ratmath/stats
Problem
Fit to , , .
Worked solution
Solving the normal equations gives .
Using RatMath
Use the data and parameter order shown below. Compare the returned value with the intermediate quantities in the worked solution.
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
For this problem type. Hand fits use about 4–12 paired observations. A classroom dataset can have tens to hundreds of rows and one to five predictors. Include a nonconstant predictor.
Focal example. The code above passed its displayed check in 0.366 ms on Node.
Same API, measured input family.stats.linearModel · observations; one predictor. Baseline test points across courses 4, 5, 10, 50. Check recovery of intercept 3 and slope 2 from exact data y=3+2x.
Practical limit. Largest passing sample 5,000 (106 ms, 124.9 MiB peak process RSS). The first sampled passing call above 100 ms used 5,000 (106 ms). The runs did not locate a ceiling for this family. The number of predictors and matrix rank matter independently of row count. A perfect fit can make standard-error or inference formulas degenerate.
Measured times, memory and outcomes
observations; one predictor · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
4
Baseline
Passed
1.01
76.4
5
Baseline
Passed
0.941
74.6
10
Baseline
Passed
5.18
74.7
50
Baseline
Passed
20.91
75.7
200
Stretch
Passed
10.07
80.1
1,000
Stretch
Passed
33.39
89.1
5,000
Stretch
Passed
106
124.9
Each row measures the stated fixture. Other inputs to this API can take different paths. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
stats.linearModel · 6 passing test cases, 13 distinct argument descriptions
A liability and an asset portfolio have equal present value and equal modified duration. What interest-rate move is hedged to first order?
Worked solution
Their first derivatives agree for a small parallel yield shift under the same yield convention. Convexity, nonparallel shifts and cash-flow changes can leave residual exposure.
Working without a calculator
Use the argument or construction in the worked solution. This lesson assigns the reasoning to the student.
Input range and limits
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
Scope of verification. This day asks for a hand argument, construction or interpretation. The worked instance gives the input. General claims need the reasoning shown above. A measured time, memory ceiling or finite input sweep would not verify that claim.
Day 39 / 40 · Planning and model risk
Survival benefit
Direct calculation@ratmath/core
Problem
Find the present value of 1,000 payable in one year conditional on survival probability 0.98 at a 5% annual effective rate.
Worked solution
Discount the expected payment. The actuarial present value is 2800/3, or about 933.33 currency units.
Using RatMath
Put the arithmetic inside the R tag so RatMath parses the literals. Keep fractions as ratios throughout the calculation and attach the units from the problem to the result.
JavaScript module
import { R } from "@ratmath/core";
console.log(String(R`1000*(98/100)/(105/100)`));
Checked output
2800/3
Run this example locally
Use the integrated RatMath workspace with the subject packages linked. Save this example as lesson.mjs in its root and run node lesson.mjs. The guide formats library objects as mathematical values. Your console may also print their fields or JavaScript type suffixes.
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
Focal example. The code above passed its displayed check in 0.098 ms on Node.
Same arithmetic expression. Replaced its first numeral 1000 with 0, 1, 2, 7, 12, 100, 1000, -3, 1/2. All 9 defined variants agreed exactly with Python’s rational arithmetic. This varies the arithmetic operation while holding the rest of the expression fixed.
Related workload in this unit.core.Rational.add · denominator. Baseline test points across courses 1, 2, 3, 12, 100, 1,000. Cross-multiply 1/n + 1/(n+1) against (2n+1)/(n(n+1)).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 1,000,000,007 (0.038 ms, 76.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact work depends on the bit lengths of both numerator and denominator. A long sum of relatively prime fractions can build a large common denominator.
Measured times, memory and outcomes
denominator · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.034
74.9
2
Baseline
Passed
0.036
75.6
3
Baseline
Passed
0.040
75.9
12
Baseline
Passed
0.039
75.2
100
Baseline
Passed
0.041
75
1,000
Baseline
Passed
0.035
75.8
1,000,003
Stretch
Passed
0.044
75
1,000,000,007
Stretch
Passed
0.038
76.3
Each row measures the stated fixture. This workload uses a different operation or expression from the focal example. Its thresholds apply only to the named fixture. Course intervals are estimates. Finite samples leave intermediate inputs and other problem families unverified.
Additional inputs from package tests
These arguments were observed in passing test cases. The linked assertions show what the tests actually checked. Structured inputs are abbreviated.
core.R · 2 passing test cases, 10 distinct argument descriptions
Why can a single historical volatility be inadequate for a long-dated option?
Worked solution
Volatility can vary across maturities, strikes and regimes. A scenario table exposes sensitivity to this modeling input.
Working without a calculator
Use the argument or construction in the worked solution. This lesson assigns the reasoning to the student.
Input range and limits
Typical inputs for this unit. Lifetimes or depreciation horizons of a few to about 100 years, modest cash-flow tables and explicit scenario assumptions.
Scope of verification. This day asks for a hand argument, construction or interpretation. The worked instance gives the input. General claims need the reasoning shown above. A measured time, memory ceiling or finite input sweep would not verify that claim.