8000 GitHub - LeoKavanagh/mc-scala: Monte Carlo Integration in Scala
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

LeoKavanagh/mc-scala

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Monte Carlo Integration in Scala

The simplest, really. Probably not very Scala-ish, but it's a start.

Given a continuous function in one variable and lower and upper bounds, calculate the area under the curve by Monte Carlo integration.

All of this is hard coded, because it's an exercise in numerical methods rather than non-standard evaluation or processing user input.

Monte Carlo Integration, or How To Get The Right Answer By Guessing

... kind of.

Start with the defintion of the (arithmetic) mean of a function over a given interval:

\bar{f(x)}=\frac{1}{N}\sum_{i=1}^N{f(x_{i})};a\leq{x}\leq{b}

Now define the same thing in the continuous case rather than the discrete:

\bar{f(x)}=\frac{1}{b - a}\int\limits_{a}^b{f(x)}dx

Now, we can rearrange this expression to get a definition of the integral in terms of the mean and the width of the interval:

\int_{a}^b{f(x)}dx=(b - a) * \bar{f(x)}

So, this implies that we can calculate the integral of f(x) over the interval f(x) by first getting the average of the function over the interval f(x) , and then multiplying by f(x) .

This is great, because very often we find ourselves in the situation where evaluating f(x) is easy for any given f(x) , but integrating f(x) over any given integral is very difficult indeed.

The simplest, most naive, approach is to Uniformly sample from the interval (b - a), and then evaluate f(x) for each x \sim Unif(a,b).

Now we have a sequence

We can take the mean of this sequence, multiply by the width of the interval, and we get our discrete approximation of the integral:

\int_{a}^{b} f(x) dx\approx(b - a) * \bar{f(x)};\  x\sim Unif(a,b)

Setup

I used Darren Wilkinson's Giter8 template to get started. It gives you the Breeze package and a main function. This is all I need, and is much handier than having to write a build.sbt file myself.

sbt new darrenjw/breeze.g8

Run with SBT

sbt run

Making This Less Useless

Redefine the function f inside the Main object, and you're good to go with your own continuous function in one variable. Extending this to accepting user-inputted functions is left as an exercise to the reader. Please do tell me if you achieve this, because I haven't tried yet.

Making This Less Mathematically Naive

That's a pretty big ask. Ultimately it leads to things like Markov Chain Monte Carlo, via things like Importance Sampling.

Alternatively, there are sophisticated non-stochastic numerical integration methods like Simpson's Rule or Gaussian Quadrature.

About

Monte Carlo Integration in Scala

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0