Laziness is a Virtue

You have the coding chops to automate your job; savvy to communicate a new easier solution to your client; or come up with that smart equation which neatly cuts through the bullshit.

You're smart enough not to have to work needlessly hard.

There's an idea in computer science called 'lazy evaluation' which allows your program to put its feet up until calculations are actually needed.

Why is that important?

We often refer to series of numbers which never end.

For example an infinite list of random numbers using the Lazy.js library looks like this:

var rndmNrs = Lazy.generate(Math.random);

All the code for these examples is here.

You could also define Fibonacci and Prime numbers pretty easily.

This blog is pretending to be about finance, so let's look into stochastic processes.

var bMP = Lazy.generate( function() { return jStat.normal.sample( 0, 1 ); } );

A Brownian motion process.

The geometric variety looks like:

var gBMP = Lazy.generate( function() { return s0 * Math.exp( (r - 0.5 * Math.pow(sigma, 2) ) * T + sigma * Math.sqrt(T) * bMP.take(1) ) ; } );

Now how about we do something with these stochastic processes?

Let's define call options in the same lazy manner.

var call = Lazy.generate( function() { return Math.max(gBMP.take(1)-K,0); } );

and then generate 10,000 prices.

call.take(10000)

As simple as that.

No loops or other control structures are needed when you use lazy evaluation and infinite data structures - which makes for clean and intuitive code.