It's not just tofu. Programming · Technology · Fitness

Google Earth has a built-in animation feature. The typical use of this feature is to show moving object on a GPS track or geographic data with time elements. For example, the following Google Earth animation shows a swarm of earthquakes hit Japan a few days before and after the devastating 8.9 magnitude earthquake on March 11, 2011.

Actually, it is possible to use Google Earth animation as a generic platform for visualizing objects or processes in 4D (3D + time). In this post I'll give you an example of Google Earth animation that demonstrates the process of approximating the value of π using Monte Carlo mathod, which, b.t.w. has nothing to do with geographic data, nor has any time related elements per se.

Approximating π with Monte Carlo Method

The following scenario is a classic example of the use of Monte Carlo method. Consider a quarter-circle inscribed in a square as shown in Fig. 1.

Fig. 1. A quarter-circle inscribed in a square

If we randomly cast a point in the square, the probability of it falling in the quarter-circle area is given by

where,

Ac - the area of the quarter circle

As - the area of the square

If we cast N points in the square, of which Nc points are counted falling in the quarter-circle area, then the probability P can be approximatd by Nc/N, and therefore,

So in our case the Monte Carlo method of approximating π using the above formula is simply a matter of casting N random points in the square and counting how many of them fall in the quarter-circle area. In particular, if we let the radius R = 1, the generation of a random point in the square is equivalent to the generation of two random numbers in interval [0, 1), one for x and one for y coordinates. And the condition of the point being in the quarter-circle area is x2 + y2 < 1.

In old days when I learned Monte Carlo method in college (Gee, I sound old), we used a random number table, something like this, to generate random numbers. Boy, it was truly a tedious task to do any meaningful Monte Carlo simulation at that time. B.t.w. I can't believe that book (just a bunch of numbers) sells for 81 bucks on Amazon.com! I guess a million of anything can sell for money. I highly recommand you read the customer reviews of the book, though. Funny as hell.

The accuracy of π approximated using Monte Carlo method depends on the sample size N. The general trend is that the larger the sample size, the more accurate the approximated value of π. However, for any two particular sets of samples of different sizes, the set with larger sample size is not guaranteed to produce better approximation of π than the set with smaller sample size. Fig. 2 illustrates the point. Fig. 2 also seems to indicate that Monte Carlo method is not a terribly efficient way to get very accurate value of π. It takes a lot of points to improve even one digit of accuracy.

Fig. 2. Sample size N and accuracy of π

Google Earth Animation

Monte Carlo method doesn't care how you cast the points. You can cast one at a time or you can cast them all together. But for our purpose of illustrating the converge trend of the estimated value of π as the sample size increases, this is what I want to achieve through Google Earth animation: For a given sample size N and a square area, I want to show a series of snapshots of distributions of N1, N2, ... Ni, ..., N points in the square, where, N1 < N2 < ... < Ni < ... < N. For each snapshot i, there is a screen overlay located at top-center showing current approximated value of π, Nci, and Ni. A point found inside the quarter-circle is represented by a green dot and a point outside, a red dot. Fig. 3 shows such a snapshot.

Fig. 3. A snapshot of Google Earth animation

Note that the points are semi-transparent. If two or more points are overlapped, their combined color seems brighter. Also, the screen overlay for each snapshot is actually an image. This is because there is currently no way to write text directly on map surface in KML - Google Earth's file format.

To make the series of snapshots playback or animate, we need to artificially add KML time element to each point. For the first N1 points, I add to them a <TimeSpan> with a begin time T0. For the next N2 - N1 points, I add to them a <TimeSpan> with a begin time T1 = T0 + ΔT. For the next N3 - N2 - N1 points, I add to them a <TimeSpan> with a begin time T2 = T1 + ΔT, and so on. The time increment ΔT is one minute, but can be anything, an hour or a day. It doesn't matter. Here is a snip of the KML:

Notice that a <ScreenOverlay> is inserted whenever Ni is reached and its duration is exactly ΔT. The displayable content of the <ScreenOverlay> is the text in some image format (.png), which is generated along with the KML file.

You can download the animation (in .kmz) at the end of this post. You need Google Earth to run it. If you don't bother to install Google Earth, you can watch the following YouTube video I made based on the animation. Enjoy!