What’s the average distance that someone can jump?
Let’s estimate it!
I have a silly function that gives me
the distance that someone can jump,
based on their height h in centimeters:
functionjumpDistance(h: number): number {return h <0|| h >360?0:300* Math.sin(h *(Math.PI/400));}
Next we’ll describe the heights of people in the population.
To start, let’s say the population is just two people:
Jane is 160 cm tall, and Peter is 180 cm tall.
Then we can estimate the average jump distance:
For an even larger population,
we can describe the population by a probability distribution,
like $\text{Normal}(\mu=170, \sigma=20)$.
Then the precise average jump distance is:
Yuck!
Suddenly we can’t solve the problem by just running the code,
because there are infinitely many heights to consider.
Our nice finite loop turned into an infinite integral.
And it’s horrible to solve analytically,
especially because jumpDistance is a piecewise function.
What can we do instead?
If we have access to a function that gives us the population count at each height,
we can do what we were doing before,
but with some chosen heights:
functionpopCountForHeight(h:number){return1000* Math.exp(-0.5*((h -170)/20)**2);}let totalJump =0;let totalPeople =0;for(let h =0; h <360; h +=1){const count =popCountForHeight(h);
totalJump += count *jumpDistance(h);
totalPeople += count;}const averageJump = totalJump / totalPeople;
This approach could be called a Riemann sum.
One problem with this approach is that
we need to know a range of heights to consider
that will cover most of the population,
but not too much that we’re wasting time on heights that are very unlikely.
Above, we chose to consider heights from 0 to 360 cm.
Another approach is to sample from the distribution:
const populationHeights =newNormalDistribution({mean:170,stdDev:20});let totalJump =0;let totalPeople =0;for(let i =0; i <1000; i++){const h = populationHeights.sample();
totalJump +=jumpDistance(h);
totalPeople++;}const averageJump = totalJump / totalPeople;
Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!