Sunday, June 5, 2011

Monte Carlo Simulation to Estimate pi

Imagine a person randomly throwing darts at a square board with a unit circle inscribed on it. The proportion of darts that land in the circle multiplied by 4 gives an estimate of the value of pi. The point (x, y) hit by the dart is in the circle if x^2 + y^2 <= 1.

What follows is my implementation of this idea in Teradata sql, with the value of pi estimated to two decimal places. The resource at the end of this post has a nice discourse on this topic, with Python implementations and a video on Monte Carlo simulation.
/* Create a table with a random point (x,y) on the board */
CREATE VOLATILE TABLE tblTemp AS
(SELECT RANDOM(-100,100)/100.00 AS x
,RANDOM(-100,100)/100.00 AS y
,CASE WHEN x**2+y**2 <= 1 THEN 1
ELSE 0
END AS inCirc
)WITH DATA PRIMARY INDEX (x)
ON COMMIT PRESERVE ROWS;

/* Generate a sufficiently large (but not too large!) number of random points (x,y) on the board */
CREATE VOLATILE TABLE tblRndPts AS
(WITH RECURSIVE tblRnd (lvl, x, y, inCirc) AS
(SELECT CAST(1 AS DECIMAL(18,0)) AS lvl, x, y, inCirc
FROM tblTemp

UNION ALL

SELECT lvl+1
,RANDOM(-100,100)/100.00 AS x1
,RANDOM(-100,100)/100.00 AS y1
,CASE WHEN x1**2+y1**2 <= 1 THEN 1
ELSE 0
END AS inCirc
FROM tblRnd AS t1
WHERE t1.lvl < 10000 /* total number of points (x, y) */
)
SELECT *
FROM tblRnd
) WITH DATA PRIMARY INDEX (lvl)
ON COMMIT PRESERVE ROWS;

/* Calculate pi and the diff from the actual value */
SELECT 4.00 * SUM(inCirc)/COUNT(*) AS pi, CAST(pi() - pi AS DECIMAL(4,2)) AS diff
FROM tblRndPts;

Click here to read more about this topic.

Monday, May 2, 2011

Pacquiao Was Inspired by Bruce Lee

pacquiao

On the eve of his fight with Shane Mosley this Saturday US time, The New York Times reports that the Filipino boxing sensation Manny Pacquiao was inspired by Bruce Lee.

"The boxing genius of Manny Pacquiao includes feet that belong in Riverdance, calves the size of grapefruits and deceptive power generated from his core. His movement is unorthodox, scattered and perpetual, as if designed by a jazz musician. He creates angles unlike any other fighter, past or present, appearing, disappearing, shifting, striking; on balance, off balance, even off one foot.

"It is this style — part performance art, part technical wizardry, unique to Pacquiao— that defines perhaps the best boxer of his generation. And it started with a videotape of the martial artist who became his idol. It started with Bruce Lee.

"Last month, as Pacquiao molded his style specific to Shane Mosley, his welterweight opponent on Saturday in Las Vegas, he wrapped his hands inside the dressing room at the Wild Card boxing gym here. To explain the way he fights, he settled on three words.

“Like Bruce Lee,” he said.

Saturday, April 30, 2011

Thinking About CMM

Some things learned in the academia can have practical significance. One thing I learned in a dry, theory-laden unit was the concept of Capability Maturity Model (CMM). It is summed in the following diagram.

[caption id="attachment_804" align="aligncenter" width="480" caption="Capability Maturity Model"]Capability Maturity Model matrix[/caption]

The CMM can help locate where an organization or a unit within an organization is in relation to a particular process or functional capability.

In a sense, CMM does for organizations what Maslow's hierarchy of needs does for human beings. The latter claims that lofty human needs cannot be fulfilled before satisfying baser needs.

By helping organizations assess their capabilities or lack thereof, CMM can point out where an organisation or a team needs to pour its resources before planning grand but doomed schemes.

Thursday, April 14, 2011

Thus Spake HAL

Dissociated press is a computer program that generates text based on another text. Here is an example of dissociated press based on the US presidential inaugural addresses:
Fellow - Citizens of the world. Abroad and at the same mistake. In a great river system, purified and made self. And there is a privilege which he has shown that our Government together and laugh together and save the lives of our civilization secure. In our time, a pleasing guaranty that the actual equality of rewards or possessions so long bled and suffered, we have fought not for themselves. Mindful of these States, was out of the Ohio or of rank have been distinguished by some from a fever of words

A computer would have apparently written The Book of Genesis thus:
In the day when they were both the daughters of Lot with him, at the first born; I bare the loss of it; for a possession of flocks, and she called his name Onan. And Hamor communed with him for righteousness. And her brother and her damsels, and possession of herds, and at night he shall be fruitful and multiply thee, then the other end thereof. And Terah took Abram his son with the sword? Wherefore didst thou flee away secretly, and where thou art yet alive,

I was curious how Gandhi would come out. The following text is based on a collection of Gandhi's writings at Project Gutenberg. I did not remove the Project Gutenberg copyright notice before feeding it to the computer, and the effects are pleasing:
Project Gutenberg is a grand doctrine of life. With them it is to weld India into one, he instanced Scotland's poverty of the Government whenever it needed their help. Their resistance consisted of disobedience of the copyright holder, your use and distribution of Project Gutenberg-tm eBooks are often created from several printed editions, all of them fellow cranks--if in similar circumstances I acted towards them differently from my own family. It were better to concentrate my effort upon the subject before us. I was hard put to it that the co-operative movement. " In his opinion

 

Saturday, March 5, 2011

Bruce Lee Comes To Town

Move over, Mundine! The real Man has come to town. The Man who could stop bullets with a nunchaku. The Man who could move faster than electricity!

[caption id="attachment_768" align="alignright" width="350" caption="Bruce Lee's statue at the Kogarah Town Square."]Bruce Lee[/caption]

A statue of Bruce Lee in his familiar martial arts pose was unveiled at the Kogarah Town Square yesterday. The statue was presented by the City of Shunde, China, to the City of Kogarah to mark the first anniversary of their Friendship City Agreement.

Among the boys of a certain generation to which I myself belonged, Bruce Lee was bigger than the Beatles. Before we learnt to dream of becoming engineers and doctors, we aspired to become a Bruce Lee and kick ass.

And the legend of Bruce Lee! He could fell a bull by a single punch on its forehead thrown from an inch away. He could block machinegun bullets with his trademark weapon, nunchaku. In fact, it was de riguer to have a nunchaku, and learning to wield it was a rite of passage for many.

Bruce Lee was believed to be so quick that he could flick the switch off, dash to his bed and lie on it before the light went out.

Many an afternoon would be spent discussing the endlessly fascinating question of what would have happened if Bruce Lee and Mohammed Ali had met in a fight. The consensus was either a win for Bruce Lee or a draw as Ali also belonged to our pantheon of superheroes.

Bruce Lee's death at a young age only served to enhance his mystique.

Later on, as we grew older, we, or at least I, came to view Bruce Lee as an embodiment of the wisdom of the East.

The unveiling of Bruce Lee’s statue at the Kogarah Town Square tells  me that the man who used to break bones is now helping build bridges between peoples and cultures.

Sunday, February 27, 2011

And the Grass Grows by Itself



The movie Eat Pray Love, which I finally watched on DVD, reminded me of an old Buddhist poem:
Sitting silently
Doing nothing
The Spring comes
And the grass grows by itself

So, as the protagonist of Eat Pray Love learns, the Italians have distilled the aesthetic of doing nothing in an even more exquisite phrase: il dolce far niente or the sweetness of doing nothing.

The idea of rejuvenating the spirit and body through inaction is nothing new. The Abrahamic religions bestowed the idea of sabbath. Buddhist masters set aside time for doing nothing, except perhaps brewing tea and watching the cassia flowers fall.

Time for me to do nothing!