37 of 40 focal problems use RatMath for a calculation or a substantive step.
Direct calculation
34 · 85%
Assisted
3 · 7.5%
Worked by hand
3 · 7.5%
Using the packages
Finite distributions make exact event arithmetic accessible. Densities, independence and conditioning require a model, and a mass interval containing one cannot certify normalization by itself.
Calculator interface
Give events names on a sample-space diagram and distinguish conditioning from intersection. For a density show the support, analytic normalization and the enclosure as separate fields.
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
37 of 40 days include a checked executable example. On 8 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 · Events and conditioning. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
Unit benchmark · stats.die.sumOf, six-sided dice. Largest passing sample 100 (1,985 ms, 215.1 MiB peak process RSS). Time cutoff at 250, 500. The first sampled passing call above one second used 100 (1,985 ms).
Days 9–16 · Discrete distributions. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
Unit benchmark · stats.binomial.pmf, trials. Largest passing sample 10,000 (0.836 ms, 74.4 MiB peak process RSS). The runs did not locate a ceiling for this family.
Days 17–24 · Continuous laws. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
Unit benchmark · stats.fromDensity, monomial degree. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family.
Days 25–32 · Expectation and dependence. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
Unit benchmark · stats.mean, observations. Largest passing sample 100,000 (129 ms, 142.2 MiB peak process RSS). The first sampled passing call above 100 ms used 100,000 (129 ms). The runs did not locate a ceiling for this family.
Days 33–40 · Limits and processes. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
Unit benchmark · stats.markovChain.stationary, states. Largest passing sample 100 (655 ms, 144.2 MiB peak process RSS). The first sampled passing call above 100 ms used 50 (112 ms). The runs did not locate a ceiling for this family.
Accuracy notes (2)
Qualified · A density can carry unproven total mass. On [0,1], (1+10⁻⁵⁰)cos(x)/sin(1) has mass 1+10⁻⁵⁰. The default result reports massProven=false and supplies its enclosure. Pass {requireExactMass:true} when a proof of unit mass is required. The strict option refuses this example.
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.
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([R`0.5 + 0.4 - 0.2`, R`0.2 / 0.4`]);
Checked output
[7/10, 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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
Focal example. The code above passed its displayed check in 0.121 ms on Node.
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
Find the mean and variance of the sum of two fair dice.
Worked solution
Mean 7; variance .
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";
const S = stats.die().sumOf(2);
console.log([S.mean(), S.variance()]);
Checked output
[7, 35/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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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.885 ms on Node.
Same API, measured input family.stats.die.sumOf · six-sided dice. Baseline test points across courses 1, 2, 5, 10, 30. For a sum of n fair dice, E[X]=7n/2 and Var[X]=35n/12.
Practical limit. Largest passing sample 100 (1,985 ms, 215.1 MiB peak process RSS). Time cutoff at 250, 500. The first sampled passing call above one second used 100 (1,985 ms). A sum of dice has only 5n+1 totals. Exact convolution still slows as support and rational weights grow. Enumerating ordered tuples grows as 6ⁿ.
Measured times, memory and outcomes
six-sided dice · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
3.94
75.1
2
Baseline
Passed
1.63
75.2
5
Baseline
Passed
3.03
77.2
10
Baseline
Passed
10.19
79.1
30
Baseline
Passed
67.18
89.7
100
Stretch
Passed
1,985
215.1
250
Stretch
Time cutoff
—
250.2
500
Stretch
Time cutoff
—
274.8
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.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 03 / 40 · Events and conditioning
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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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 04 / 40 · Events and conditioning
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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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.stats.die.sumOf · six-sided dice. Baseline test points across courses 1, 2, 5, 10, 30. For a sum of n fair dice, E[X]=7n/2 and Var[X]=35n/12.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (1,985 ms, 215.1 MiB peak process RSS). Time cutoff at 250, 500. The first sampled passing call above one second used 100 (1,985 ms). A sum of dice has only 5n+1 totals. Exact convolution still slows as support and rational weights grow. Enumerating ordered tuples grows as 6ⁿ.
Measured times, memory and outcomes
six-sided dice · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
3.94
75.1
2
Baseline
Passed
1.63
75.2
5
Baseline
Passed
3.03
77.2
10
Baseline
Passed
10.19
79.1
30
Baseline
Passed
67.18
89.7
100
Stretch
Passed
1,985
215.1
250
Stretch
Time cutoff
—
250.2
500
Stretch
Time cutoff
—
274.8
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
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
Day 05 / 40 · Events and conditioning
Conditional probability
Direct calculation@ratmath/stats
Problem
Of 50 students who studied, 45 passed; of 50 who did not, 15 passed. Find and .
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";
const table = stats.finite(new Map([[["S", "P"], 45], [["S", "F"], 5], [["N", "P"], 15], [["N", "F"], 35]]), { normalize: true });
console.log(
[table.prob(([s, r]) => r === "P", ([s]) => s === "S"), table.prob(([s]) => s === "S", ([, r]) => r === "P")]
);
Checked output
[9/10, 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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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.293 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 06 / 40 · Events and conditioning
Bayes' rule
Direct calculation@ratmath/stats
Problem
A disease affects 1% of people. A test detects it 95% of the time and gives a false positive 5% of the time. Given a positive test, what is the probability of disease?
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({ sick: "0.01", well: "0.99" }, { sick: "0.95", well: "0.05" }).posterior.p("sick"))
);
Checked output
19/118
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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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.814 ms on Node.
Related workload in this unit.stats.die.sumOf · six-sided dice. Baseline test points across courses 1, 2, 5, 10, 30. For a sum of n fair dice, E[X]=7n/2 and Var[X]=35n/12.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (1,985 ms, 215.1 MiB peak process RSS). Time cutoff at 250, 500. The first sampled passing call above one second used 100 (1,985 ms). A sum of dice has only 5n+1 totals. Exact convolution still slows as support and rational weights grow. Enumerating ordered tuples grows as 6ⁿ.
Measured times, memory and outcomes
six-sided dice · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
3.94
75.1
2
Baseline
Passed
1.63
75.2
5
Baseline
Passed
3.03
77.2
10
Baseline
Passed
10.19
79.1
30
Baseline
Passed
67.18
89.7
100
Stretch
Passed
1,985
215.1
250
Stretch
Time cutoff
—
250.2
500
Stretch
Time cutoff
—
274.8
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
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
Day 07 / 40 · Events and conditioning
Conditional denominators
Direct calculation@ratmath/core
Problem
A class has 8 left-handed students, including 3 musicians. Given left-handedness, what is the probability of musicianship under uniform sampling?
Worked solution
Restrict the denominator to the eight left-handed students.
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`3/8`));
Checked output
3/8
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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
Focal example. The code above passed its displayed check in 0.071 ms on Node.
Same arithmetic expression. Replaced its first numeral 3 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
Can disjoint events each of positive probability be independent?
Worked solution
Their intersection has probability zero while the product of the two probabilities is positive.
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. Finite probability tables often have 2–20 atoms, two to four conditional events and rational probabilities.
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 09 / 40 · Discrete distributions
Birthday problem
Direct calculation@ratmath/core
Problem
In a group of 23 people, what is the probability that two share a birthday (365 equally likely days)?
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.
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
Focal example. The code above passed its displayed check in 0.368 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
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.
core.R · 2 passing test cases, 10 distinct argument descriptions
A box has 8 red and 12 blue balls. Five are drawn without replacement. Find .
Worked solution
.
Using RatMath
Set the distribution parameters first. Distinguish a point probability, a lower cumulative probability and its complementary tail when reading the result.
JavaScript module
import * as stats from "@ratmath/stats";
console.log(String(stats.hypergeometric(20, 8, 5).pmf(2)));
Checked output
385/969
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
For this problem type. Typical examples use small integer counts, trial or population sizes from a few to about 100, and probabilities strictly between 0 and 1 unless studying an endpoint.
Focal example. The code above passed its displayed check in 0.235 ms on Node.
Related workload in this unit.stats.binomial.pmf · trials. Baseline test points across courses 1, 5, 20, 100. For fair trials, P(exactly one success)=n/2^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (0.836 ms, 74.4 MiB peak process RSS). The runs did not locate a ceiling for this family. Very large trial counts create long rational probabilities. Near the tails, converting to floating point can erase small positive probabilities.
Measured times, memory and outcomes
trials · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.527
76.3
5
Baseline
Passed
0.495
74.4
20
Baseline
Passed
0.723
75.1
100
Baseline
Passed
10.61
75.4
500
Stretch
Passed
0.559
75.8
2,000
Stretch
Passed
0.536
75.1
10,000
Stretch
Passed
0.836
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.
stats.hypergeometric · 2 passing test cases, 2 distinct argument descriptions
Calls arrive at 3 per minute (Poisson). Find and in one minute.
Worked solution
; .
Using RatMath
Set the distribution parameters first. Distinguish a point probability, a lower cumulative probability and its complementary tail when reading the result.
JavaScript module
import * as stats from "@ratmath/stats";
import { R } from "@ratmath/core";
const X = stats.poisson(3);
console.log([X.pmf(2), R`1`.subtract(X.pmf(0).low ?? X.pmf(0))]);
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
For this problem type. Typical examples use small integer counts, trial or population sizes from a few to about 100, and probabilities strictly between 0 and 1 unless studying an endpoint.
Focal example. The code above passed its displayed check in 0.370 ms on Node.
Related workload in this unit.stats.binomial.pmf · trials. Baseline test points across courses 1, 5, 20, 100. For fair trials, P(exactly one success)=n/2^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (0.836 ms, 74.4 MiB peak process RSS). The runs did not locate a ceiling for this family. Very large trial counts create long rational probabilities. Near the tails, converting to floating point can erase small positive probabilities.
Measured times, memory and outcomes
trials · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.527
76.3
5
Baseline
Passed
0.495
74.4
20
Baseline
Passed
0.723
75.1
100
Baseline
Passed
10.61
75.4
500
Stretch
Passed
0.559
75.8
2,000
Stretch
Passed
0.536
75.1
10,000
Stretch
Passed
0.836
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.
stats.poisson · 4 passing test cases, 4 distinct argument descriptions
A die is rolled until the first 6. Find the expected number of rolls and the probability the first 6 is on roll 4.
Worked solution
; .
Using RatMath
Set the distribution parameters first. Distinguish a point probability, a lower cumulative probability and its complementary tail when reading the result.
JavaScript module
import * as stats from "@ratmath/stats";
import { R } from "@ratmath/core";
const G = stats.geometric(R`1/6`);
console.log([G.mean, G.pmf(4)]);
Checked output
[6, 125/1296]
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
For this problem type. Typical examples use small integer counts, trial or population sizes from a few to about 100, and probabilities strictly between 0 and 1 unless studying an endpoint.
Focal example. The code above passed its displayed check in 0.173 ms on Node.
Related workload in this unit.stats.binomial.pmf · trials. Baseline test points across courses 1, 5, 20, 100. For fair trials, P(exactly one success)=n/2^n.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (0.836 ms, 74.4 MiB peak process RSS). The runs did not locate a ceiling for this family. Very large trial counts create long rational probabilities. Near the tails, converting to floating point can erase small positive probabilities.
Measured times, memory and outcomes
trials · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
0.527
76.3
5
Baseline
Passed
0.495
74.4
20
Baseline
Passed
0.723
75.1
100
Baseline
Passed
10.61
75.4
500
Stretch
Passed
0.559
75.8
2,000
Stretch
Passed
0.536
75.1
10,000
Stretch
Passed
0.836
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.
stats.geometric · 2 passing test cases, 4 distinct argument descriptions
How many rolls of a die are expected before every face has appeared?
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 { R, sum } from "@ratmath/core";
console.log(String(sum(1, 6, (k) => R`6/${k}`)));
Checked output
147/10
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
Focal example. The code above passed its displayed check in 0.191 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.
core.R · 2 passing test cases, 10 distinct argument descriptions
Direct calculation@ratmath/discrete · @ratmath/core
Problem
Ten letters go into ten envelopes at random. Find the expected number in the right envelope and the probability that none is.
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 discrete from "@ratmath/discrete";
import { R, Rational } from "@ratmath/core";
console.log([R`10 * 1/10`, new Rational(discrete.derangementCount(10), discrete.factorial(10))]);
Checked output
[1, 16481/44800]
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
For this problem type. Use about 3–20 labeled objects in hand examples. Counts can describe sets far too large to list. Enumeration exercises generally use fewer than ten objects.
Focal example. The code above passed its displayed check in 0.108 ms on Node.
Related workload in this unit.discrete.binomial · objects. Baseline test points across courses 2, 5, 10, 52. Check k!(n−k)!·C(n,k)=n! for k=floor(n/2).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (8.36 ms, 88 MiB peak process RSS). The runs did not locate a ceiling for this family. Coefficient bit length grows with n and k. Counting is far cheaper than actually listing all k-subsets.
Measured times, memory and outcomes
objects · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
0.144
74.7
5
Baseline
Passed
0.151
73.9
10
Baseline
Passed
0.142
75.1
52
Baseline
Passed
0.141
75.9
100
Stretch
Passed
0.128
75
500
Stretch
Passed
0.171
76
2,000
Stretch
Passed
0.526
78.6
10,000
Stretch
Passed
8.36
88
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.
discrete.derangementCount · 3 passing test cases, 7 distinct argument descriptions
How many five-card poker hands contain exactly two pairs?
Worked solution
.
Using RatMath
Decide what counts as one outcome and whether order matters. Supply the sizes to the counting function and keep the returned whole number exact.
JavaScript module
import * as discrete from "@ratmath/discrete";
console.log(discrete.binomial(13, 2) * discrete.binomial(4, 2) ** 2n * 11n * 4n);
Checked output
123552n
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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
For this problem type. Use about 3–20 labeled objects in hand examples. Counts can describe sets far too large to list. Enumeration exercises generally use fewer than ten objects.
Focal example. The code above passed its displayed check in 0.062 ms on Node.
Same API, measured input family.discrete.binomial · objects. Baseline test points across courses 2, 5, 10, 52. Check k!(n−k)!·C(n,k)=n! for k=floor(n/2).
Practical limit. Largest passing sample 10,000 (8.36 ms, 88 MiB peak process RSS). The runs did not locate a ceiling for this family. Coefficient bit length grows with n and k. Counting is far cheaper than actually listing all k-subsets.
Measured times, memory and outcomes
objects · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
0.144
74.7
5
Baseline
Passed
0.151
73.9
10
Baseline
Passed
0.142
75.1
52
Baseline
Passed
0.141
75.9
100
Stretch
Passed
0.128
75
500
Stretch
Passed
0.171
76
2,000
Stretch
Passed
0.526
78.6
10,000
Stretch
Passed
8.36
88
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.
discrete.binomial · 1 passing test case, 6 distinct argument descriptions
Among four fair dice, what is the expected number of sixes?
Worked solution
Sum the four indicator expectations. Independence is unnecessary for this expectation.
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*(1/6)`));
Checked output
2/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. Binomial or hypergeometric samples often span 1–100 trials. Dice sums and full outcome tuples have different support growth.
Focal example. The code above passed its displayed check in 0.093 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
Verify x² ≥ 0 and integrate x² on [0,2] to get 8/3. This supplies the normalization argument for this density.
JavaScript module
import * as stats from "@ratmath/stats";
const X = stats.fromDensity("x^2", [0, 2], { normalize: true });
console.log([X.constant, X.mean, X.variance]);
Checked output
[3/8, 3/2, 3/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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 1.51 ms on Node.
Same API, measured input family.stats.fromDensity · monomial degree. Baseline test points across courses 0, 1, 2, 4. For density proportional to x^k on [0,1], the normalizing constant is k+1 and mean is (k+1)/(k+2).
Practical limit. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family. Check total mass and nonnegativity on the full support. An interval containing one may leave room for an invalid density.
Measured times, memory and outcomes
monomial degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
2.85
74.6
1
Baseline
Passed
4.02
75.5
2
Baseline
Passed
5.26
75.6
4
Baseline
Passed
5.71
76.2
8
Stretch
Passed
5.37
75.4
16
Stretch
Passed
7.14
77.1
32
Stretch
Passed
12.81
77.8
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.fromDensity · 8 passing test cases, 7 distinct argument descriptions
Qualified · A density can carry unproven total mass. On [0,1], (1+10⁻⁵⁰)cos(x)/sin(1) has mass 1+10⁻⁵⁰. The default result reports massProven=false and supplies its enclosure. Pass {requireExactMass:true} when a proof of unit mass is required. The strict option refuses this example.
Day 18 / 40 · Continuous laws
Exponential and memorylessness
Direct calculation@ratmath/stats · @ratmath/core
Problem
A bulb's life is exponential with mean 10 years. Find , and .
Worked solution
for both (memorylessness).
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";
const X = stats.exponential(R`1/10`);
console.log([X.sf(15), X.sf(25).low.divide(X.sf(10).high)]);
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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 0.533 ms on Node.
Related workload in this unit.stats.fromDensity · monomial degree. Baseline test points across courses 0, 1, 2, 4. For density proportional to x^k on [0,1], the normalizing constant is k+1 and mean is (k+1)/(k+2).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family. Check total mass and nonnegativity on the full support. An interval containing one may leave room for an invalid density.
Measured times, memory and outcomes
monomial degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
2.85
74.6
1
Baseline
Passed
4.02
75.5
2
Baseline
Passed
5.26
75.6
4
Baseline
Passed
5.71
76.2
8
Stretch
Passed
5.37
75.4
16
Stretch
Passed
7.14
77.1
32
Stretch
Passed
12.81
77.8
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.exponential · 9 passing test cases, 2 distinct argument descriptions
IQ is normal with mean 100 and SD 15. What fraction score above 130?
Worked solution
.
Using RatMath
Set the distribution parameters first. Distinguish a point probability, a lower cumulative probability and its complementary tail when reading the result.
JavaScript module
import * as stats from "@ratmath/stats";
console.log(stats.normal(100, 15).sf(130));
Checked output
0.0227501319481792072:0.02275013194817920721
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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 3.68 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.stats.fromDensity · monomial degree. Baseline test points across courses 0, 1, 2, 4. For density proportional to x^k on [0,1], the normalizing constant is k+1 and mean is (k+1)/(k+2).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family. Check total mass and nonnegativity on the full support. An interval containing one may leave room for an invalid density.
Measured times, memory and outcomes
monomial degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
2.85
74.6
1
Baseline
Passed
4.02
75.5
2
Baseline
Passed
5.26
75.6
4
Baseline
Passed
5.71
76.2
8
Stretch
Passed
5.37
75.4
16
Stretch
Passed
7.14
77.1
32
Stretch
Passed
12.81
77.8
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.normal · 20 passing test cases, 9 distinct argument descriptions
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";
const Y = stats.uniform(0, 1).transform("x^2");
console.log([Y.pdf(R`1/4`), Y.cdf(R`1/4`)]);
Checked output
[1, 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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 1.31 ms on Node.
Related workload in this unit.stats.fromDensity · monomial degree. Baseline test points across courses 0, 1, 2, 4. For density proportional to x^k on [0,1], the normalizing constant is k+1 and mean is (k+1)/(k+2).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family. Check total mass and nonnegativity on the full support. An interval containing one may leave room for an invalid density.
Measured times, memory and outcomes
monomial degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
2.85
74.6
1
Baseline
Passed
4.02
75.5
2
Baseline
Passed
5.26
75.6
4
Baseline
Passed
5.71
76.2
8
Stretch
Passed
5.37
75.4
16
Stretch
Passed
7.14
77.1
32
Stretch
Passed
12.81
77.8
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.uniform · 15 passing test cases, 8 distinct argument descriptions
Find the expected maximum of three independent uniform variables.
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.uniform(0, 1).orderStatistic(3, 3).mean));
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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 0.451 ms on Node.
Related workload in this unit.stats.fromDensity · monomial degree. Baseline test points across courses 0, 1, 2, 4. For density proportional to x^k on [0,1], the normalizing constant is k+1 and mean is (k+1)/(k+2).
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 32 (12.81 ms, 77.8 MiB peak process RSS). The runs did not locate a ceiling for this family. Check total mass and nonnegativity on the full support. An interval containing one may leave room for an invalid density.
Measured times, memory and outcomes
monomial degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
2.85
74.6
1
Baseline
Passed
4.02
75.5
2
Baseline
Passed
5.26
75.6
4
Baseline
Passed
5.71
76.2
8
Stretch
Passed
5.37
75.4
16
Stretch
Passed
7.14
77.1
32
Stretch
Passed
12.81
77.8
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.uniform · 15 passing test cases, 8 distinct argument descriptions
Pass the integrand and variable separately. Finite bounds request a definite integral. For an antiderivative, differentiate the proposed expression on its domain.
JavaScript module
import * as symbolic from "@ratmath/symbolic";
console.log(String(symbolic.integrate("1/x^2", "x", { from: 1, to: "inf" }).value));
Checked output
1
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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
For this problem type. Use a low-degree or elementary integrand and one to three integration variables, with one interval per dimension or a few piecewise bounds.
Focal example. The code above passed its displayed check in 46.00 ms on Node.
Same API, measured input family.symbolic.integrate · degree. Baseline test points across courses 0, 1, 2, 5, 10. The integral of x^n over [0,1] is 1/(n+1).
Practical limit. Largest passing sample 500 (195 ms, 88.7 MiB peak process RSS). The first sampled passing call above 100 ms used 500 (195 ms). The runs did not locate a ceiling for this family. Polynomial primitives are a favorable case. Singular endpoints, non-elementary primitives and unevaluated integrals need separate treatment.
Measured times, memory and outcomes
degree · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
5.98
75.7
1
Baseline
Passed
8.66
75.9
2
Baseline
Passed
7.14
75.7
5
Baseline
Passed
9.22
75.5
10
Baseline
Passed
8.48
75.8
30
Stretch
Passed
56.11
88.9
100
Stretch
Passed
78.06
88.4
500
Stretch
Passed
195
88.7
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.integrate · 11 passing test cases, 47 distinct argument descriptions
On [0,1] let F(x)=x³, extended by 0 and 1 outside. Find P(1/2<X≤1).
Worked solution
Subtract the CDF at the lower endpoint.
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`1-(1/2)^3`));
Checked output
7/8
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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
Focal example. The code above passed its displayed check in 0.087 ms on Node.
Same arithmetic expression. Replaced its first numeral 1 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
Nonnegativity requires c≥0 and the exact integral is 2c. Solving 2c=1 gives 1/2.
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("2*c=1","c")));
Checked output
c = 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. Scalar continuous laws on intervals or the line, usually one or two parameters; distinguish exact mass from a numerical enclosure.
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 0.383 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
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
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 26 / 40 · Expectation and dependence
Moment generating function
Direct calculation@ratmath/symbolic
Problem
The MGF of a Poisson() variable is . Use it to find .
Worked solution
.
Using RatMath
Enter the expression in the form given. Expand or factor the result in reverse to check that it represents the original expression.
JavaScript module
import * as symbolic from "@ratmath/symbolic";
const M = symbolic.sym("exp(l*(exp(t) - 1))");
console.log(String(symbolic.expand(M.diff("t").diff("t").subs({ t: 0 }).simplify())));
Checked output
l^2 + l
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
For this problem type. Use one or two variables and polynomial degrees roughly 2–6. Rational expressions often have two or three denominator factors.
Focal example. The code above passed its displayed check in 0.835 ms on Node.
Same API, measured input family.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. 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. 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.sym · 14 passing test cases, 44 distinct argument descriptions
"sin(x)^2 + cos(x)^2", undefined or "tan(x)*cos(x)", undefined · trigonometry
Day 27 / 40 · Expectation and dependence
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
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 28 / 40 · Expectation and dependence
Unbiased variance
Direct calculation@ratmath/stats
Problem
Find the sample variance (divisor ) of 3, 5, 7, 9, 11.
Worked solution
Deviations ; ; .
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.variance([3, 5, 7, 9, 11])));
Checked output
10
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
For this problem type. Use lists of about 4–30 values by hand and tens to hundreds on a calculator. Include ties, fractions and even or odd sample sizes as appropriate.
Focal example. The code above passed its displayed check in 0.130 ms on Node.
Related workload in this unit.stats.mean · observations. Baseline test points across courses 2, 5, 20, 100. Mean of integers 1 through n is (n+1)/2.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100,000 (129 ms, 142.2 MiB peak process RSS). The first sampled passing call above 100 ms used 100,000 (129 ms). The runs did not locate a ceiling for this family. Exact sums take at least one visit per observation. Unrelated denominators can make the running sum much larger than in this integer sample.
Measured times, memory and outcomes
observations · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
0.796
75.5
5
Baseline
Passed
0.231
74.8
20
Baseline
Passed
0.656
75.1
100
Baseline
Passed
0.370
74.4
1,000
Stretch
Passed
2.29
75.2
10,000
Stretch
Passed
10.14
83.6
100,000
Stretch
Passed
129
142.2
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.variance · 4 passing test cases, 11 distinct argument descriptions
Within each of two equally likely groups the variance is 1. Their means are 2 and 6. Find the overall variance.
Worked solution
Add the expected within-group variance 1 to the between-group variance of the means, 4.
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`1+((2-4)^2+(6-4)^2)/2`));
Checked output
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
Focal example. The code above passed its displayed check in 0.138 ms on Node.
Same arithmetic expression. Replaced its first numeral 1 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
Two equally likely groups have conditional means 3 and 9. Find the overall mean.
Worked solution
Average the conditional means with the group probabilities.
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`(3+9)/2`));
Checked output
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
Focal example. The code above passed its displayed check in 0.063 ms on Node.
Same arithmetic expression. Replaced its first numeral 3 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
If X is uniform on {−1,0,1} and Y=X², can Cov(X,Y) vanish?
Worked solution
E[X]=E[X³]=0 by symmetry. Yet Y is determined by X, so zero covariance does not establish independence.
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
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 32 / 40 · Expectation and dependence
Variance of a sum
Direct calculation@ratmath/core
Problem
Find Var(X+Y) for variances 4 and 9 and covariance −1.
Worked solution
Include twice the covariance.
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+9+2*(-1)`));
Checked output
11
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. Finite distributions with a few to tens of atoms or one-dimensional integrals for first and second moments.
Focal example. The code above passed its displayed check in 0.062 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.R · whole-number operand. Baseline test points across courses 0, 1, 10, 20, 100, 1,000, 1,000,000. (n+7)(n−3) by independent BigInt arithmetic.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 9,007,199,254,740,991 (1.67 ms, 75.3 MiB peak process RSS). The runs did not locate a ceiling for this family. Exact integer arithmetic grows with operand bit length. Enter large integers as decimal strings or BigInts before any JavaScript Number conversion.
Measured times, memory and outcomes
whole-number operand · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
0
Baseline
Passed
1.75
75
1
Baseline
Passed
1.70
75.7
10
Baseline
Passed
1.78
75.1
20
Baseline
Passed
1.88
75.4
100
Baseline
Passed
1.55
75.4
1,000
Baseline
Passed
1.36
75.3
1,000,000
Baseline
Passed
1.39
74.8
1,000,000,000
Stretch
Passed
1.69
76.3
9,007,199,254,740,991
Stretch
Passed
1.67
75.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
100 fair dice are rolled. Estimate with the normal approximation, and give the exact value.
Worked solution
Mean 350, SD . With continuity correction . Exactly (by convolution in Python) 0.126948.
Using RatMath
The convolution gives the exact tail. For the normal estimate use mean 350, variance 100×35/12 and the continuity-corrected cutoff 369.5.
JavaScript module
import * as stats from "@ratmath/stats";
import { R } from "@ratmath/core";
const S = stats.die().sumOf(100);
console.log(R`1`.subtract(S.cdf(369)));
Checked output
0.126947761041731454742050…
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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
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 1,874 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Same API, measured input family.stats.die.sumOf · six-sided dice. Baseline test points across courses 1, 2, 5, 10, 30. For a sum of n fair dice, E[X]=7n/2 and Var[X]=35n/12.
Practical limit. Largest passing sample 100 (1,985 ms, 215.1 MiB peak process RSS). Time cutoff at 250, 500. The first sampled passing call above one second used 100 (1,985 ms). A sum of dice has only 5n+1 totals. Exact convolution still slows as support and rational weights grow. Enumerating ordered tuples grows as 6ⁿ.
Measured times, memory and outcomes
six-sided dice · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
1
Baseline
Passed
3.94
75.1
2
Baseline
Passed
1.63
75.2
5
Baseline
Passed
3.03
77.2
10
Baseline
Passed
10.19
79.1
30
Baseline
Passed
67.18
89.7
100
Stretch
Passed
1,985
215.1
250
Stretch
Time cutoff
—
250.2
500
Stretch
Time cutoff
—
274.8
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.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 34 / 40 · Limits and processes
Stationary distribution
Direct calculation@ratmath/stats
Problem
Find the stationary distribution of the chain with transition matrix .
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(stats.markovChain([["0.9", "0.1"], ["0.5", "0.5"]]).stationary().distribution);
Checked output
{ "0": 5/6, "1": 1/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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
For this problem type. Use about 2–10 states, nonnegative rational transition probabilities and rows summing to one. Vary the class structure when testing uniqueness.
Focal example. The code above passed its displayed check in 0.871 ms on Node.
Same API, measured input family.stats.markovChain.stationary · states. Baseline test points across courses 2, 3, 5, 10. A strictly positive uniform transition matrix has uniform stationary probabilities.
Practical limit. Largest passing sample 100 (655 ms, 144.2 MiB peak process RSS). The first sampled passing call above 100 ms used 50 (112 ms). The runs did not locate a ceiling for this family. An explicit transition matrix has n² entries. Check row sums, closed communicating classes and periodicity before interpreting a stationary law as a limit.
Measured times, memory and outcomes
states · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
5.03
74.6
3
Baseline
Passed
1.61
75.6
5
Baseline
Passed
2.69
75
10
Baseline
Passed
16.48
77
20
Stretch
Passed
36.57
83.9
50
Stretch
Passed
112
92.9
100
Stretch
Passed
655
144.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.
stats.markovChain · 2 passing test cases, 3 distinct argument descriptions
A gambler with $3 bets $1 at a time, winning each bet with probability 0.4, until reaching $10 or $0. What is the probability of reaching $10?
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";
const P = Array.from({ length: 11 }, (_, i) => Array.from({ length: 11 }, (_, j) => (i === 0 || i === 10 ? (i === j ? 1 : 0) : j === i + 1 ? "0.4" : j === i - 1 ? "0.6" : 0)));
console.log(String(stats.markovChain(P).hitting(10).probability[3]));
Checked output
2432/58025
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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
For this problem type. Use about 2–10 states, nonnegative rational transition probabilities and rows summing to one. Vary the class structure when testing uniqueness.
Focal example. The code above passed its displayed check in 0.926 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.stats.markovChain.stationary · states. Baseline test points across courses 2, 3, 5, 10. A strictly positive uniform transition matrix has uniform stationary probabilities.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (655 ms, 144.2 MiB peak process RSS). The first sampled passing call above 100 ms used 50 (112 ms). The runs did not locate a ceiling for this family. An explicit transition matrix has n² entries. Check row sums, closed communicating classes and periodicity before interpreting a stationary law as a limit.
Measured times, memory and outcomes
states · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
5.03
74.6
3
Baseline
Passed
1.61
75.6
5
Baseline
Passed
2.69
75
10
Baseline
Passed
16.48
77
20
Stretch
Passed
36.57
83.9
50
Stretch
Passed
112
92.9
100
Stretch
Passed
655
144.2
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.markovChain · 2 passing test cases, 3 distinct argument descriptions
A population has mean 50 and standard deviation 12. For samples of 36, find .
Worked solution
SE ; .
Using RatMath
Set the distribution parameters first. Distinguish a point probability, a lower cumulative probability and its complementary tail when reading the result.
JavaScript module
import * as stats from "@ratmath/stats";
console.log(stats.normal(50, 2).sf(53));
Checked output
0.066807201268858066:0.06680720126885806601
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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
For this problem type. Use a scalar random variable, one or two parameters, and one or two probability bounds. Normal-table exercises tend to stay within about 3 standard deviations.
Focal example. The code above passed its displayed check in 4.42 ms on Node. Both enclosure endpoints also met the lesson’s stated numerical tolerance.
Related workload in this unit.stats.markovChain.stationary · states. Baseline test points across courses 2, 3, 5, 10. A strictly positive uniform transition matrix has uniform stationary probabilities.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 100 (655 ms, 144.2 MiB peak process RSS). The first sampled passing call above 100 ms used 50 (112 ms). The runs did not locate a ceiling for this family. An explicit transition matrix has n² entries. Check row sums, closed communicating classes and periodicity before interpreting a stationary law as a limit.
Measured times, memory and outcomes
states · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
2
Baseline
Passed
5.03
74.6
3
Baseline
Passed
1.61
75.6
5
Baseline
Passed
2.69
75
10
Baseline
Passed
16.48
77
20
Stretch
Passed
36.57
83.9
50
Stretch
Passed
112
92.9
100
Stretch
Passed
655
144.2
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.normal · 20 passing test cases, 9 distinct argument descriptions
The waiting times 2, 3, 1, 4, 5 come from an exponential distribution with rate . Find the maximum likelihood estimate of .
Worked solution
, 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 stats from "@ratmath/stats";
console.log(String(stats.fit([2, 3, 1, 4, 5], "exponential").params.rate));
Checked output
1/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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
Focal example. The code above passed its displayed check in 0.339 ms on Node.
Related workload in this unit.stats.tTest · observations per sample. Baseline test points across courses 3, 8, 20, 50. A symmetric sample centered at its hypothesized mean has t=0 and a two-sided p-value of 1.
Practical limit. The cutoff for this lesson’s own code is unmeasured. Largest passing sample 10,000 (83.32 ms, 88.5 MiB peak process RSS). The runs did not locate a ceiling for this family. Tail probability, degrees of freedom, ties and variance assumptions matter. A larger sample cannot repair an inappropriate test or model.
Measured times, memory and outcomes
observations per sample · one run per input
Input
Sample
Result
Call (ms)
Peak RSS (MiB)
3
Baseline
Passed
1.62
75.7
8
Baseline
Passed
1.77
75
20
Baseline
Passed
1.68
75.4
50
Baseline
Passed
1.28
75.1
200
Stretch
Passed
3.24
75
1,000
Stretch
Passed
5.50
78.8
10,000
Stretch
Passed
83.32
88.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.fit · 3 passing test cases, 8 distinct argument descriptions
Independent coin flips have heads probability 1/2. Bound the chance that the average of 100 flips differs from 1/2 by at least 1/5.
Worked solution
The sample mean has variance (1/4)/100. Chebyshev gives an upper bound of (1/400)/(1/25)=1/16.
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`(1/4)/100/(1/5)^2`));
Checked output
1/16
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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
Focal example. The code above passed its displayed check in 0.143 ms on Node.
Same arithmetic expression. Replaced its first numeral 1 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
Input contract · An integer can lose a digit before RatMath receives it. The decimal integer 9007199254740993 supplied as a JavaScript Number has already become 9007199254740992. The Rational constructor refuses this unsafe Number. Supply a BigInt or an exact string. Converting a rounded Number back to BigInt preserves the earlier error.
Runtime limit · BigInt capacity depends on the runtime. Bun 1.4.2 and Node both accepted 2^1048575 and 2^1048576. The latter shift failed on Bun 1.4.0. Core now declares Bun ≥1.4.2. Passing these two shifts only establishes a lower bound on capacity.
Day 39 / 40 · Limits and processes
Absorbing states
Worked by hand
Problem
A process stays in state B once it arrives. Which transition row represents B in the state order A,B?
Worked solution
The row is [0,1]. Each entry is conditional on starting in B.
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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
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 40 / 40 · Limits and processes
Markov's inequality
Direct calculation@ratmath/core
Problem
For nonnegative X with E[X]=3, bound P(X≥12).
Worked solution
The contribution from X≥12 is at least 12 times its probability.
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`3/12`));
Checked output
1/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. Markov chains usually have 2–10 states; limit theorems concern a sequence of distributions and need their stated hypotheses.
Focal example. The code above passed its displayed check in 0.110 ms on Node.
Same arithmetic expression. Replaced its first numeral 3 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