Friday, October 17, 2008

PI approximation with Monte Carlo Simulation

/**
*
* PI approximation using Monte Carlo Simulation. Draw a square of
* unit area on the ground, then inscribe a circle within it. Now, scatter some
* small objects (for example, grains of rice or sand) throughout the square. If
* the objects are scattered uniformly, then the proportion of objects within
* the circle vs objects within the square should be approximately PI/4, which
* is the ratio of the circle's area to the square's area. Thus, if we count the
* number of objects in the circle, multiply by four, and divide by the total
* number of objects in the square (including those in the circle), we get an
* approximation to PI. (
http://en.wikipedia.org/wiki/Monte_Carlo_method)
*
*/
public class PiEstimationWithMonteCarlo {

public double estimatePI(long numberSample) {
long numberInCircle = 0;
double pi = 0.0;

for (int i = 0; i < numberSample; i++) {
double x = generateRandomNumber();
double y = generateRandomNumber();
if (insideCircle(x, y))
numberInCircle++;
}
pi = (numberInCircle * 4) / ((double) numberSample);
return pi;
}

private boolean insideCircle(double x, double y) {
double distance = x * x + y * y;
if (distance > 1) {
// out of circle
return false;
}
return true;
}

private double generateRandomNumber() {
// between -1 and 1
return Math.random() * 2 - 1;
}

public static void main(String args[]) {
PiEstimationWithMonteCarlo piEstimator = new PiEstimationWithMonteCarlo();
double pi = piEstimator.estimatePI(5000000);
System.out.println("PI=" + pi);
}
}

Tuesday, October 14, 2008

Bets and the City: Sally Nicoll's Spread Betting Diary

One of my friend started to work in a leading spread betting company recently. It was the first time i heard spread betting. It came cross to me as gambling at first but my friend tried to convince me that it is trading more than betting and many trader uses it to hedge their investment.

With these are in my mind, i bought a spread betting and a stock market books to get some beginner information about this sector. Let me be clear first, i don't like gambling, i never heard or seen anybody gains from gambling, apart from casino or gambling saloon owners. Besides, i saw from some of people around me how gambling dramatically effects people's life.

Spread betting book i purchased is
Bets and the City: Sally Nicoll's Spread Betting Diary. It has good review rate on Amazon and i think it is a good book to get some starting information on learning phrase of a spread better or gambler.

Nothing can explain this book more than "Bridget Jones meets Wall Street", i think. As a middle age, single lady with tendency to gambling, Sally decides to play on spread betting after reading an article on a magazine. She is also a full-time writer, struggling to finish her first novel. While she learns and gamble rather than trade on spread betting, she writes her spread betting experience or dairies on a spread betting company's website.

She puts together a genuine dairy. In her blog/dairy, she writes all her mistakes, losses, gains and lessons with a hilarious way with her daily life. Like many beginner, she is not much successful, in many trades she loses, but each time she strikes back with new methodologies. And she explains in a clean way her mistakes, as much as she can.

When she published her spread betting dairy as book, after a year, she was still a learner and not millionaire yet. She does not reveal her final account figures in the end of the book, but i think, she lost big chunk of her initial money. But she still claims that spread betting (especially binary betting) is a trade rather than a gambling, which i doubt very much, especially in this financial crisis (even in normal conditions, in the medium and long term). Maybe Sally should try also arbitrage sport betting which a friend of mine claims that he earned decent amount with his pocket money when he was in university. When i heard these, i can not help myself thinking motto of BBC3's The Real Hustle TV program: "These bets are very tempting to take part in but you can guess which way the bet always goes - the hustler's way!"


Wednesday, October 01, 2008

Sample Address Book

Here is a sample address book application based on 3-tier architecture and with various frameworks and toolkits such as :

Presentation layer
  • GWT
  • EXT GXT
Logic layer
  • Spring
  • Hibernate
Database layer
  • HSQLDB (or MySQL)

Source code as eclipse project is available here . Please note, in order to run project in eclipse, a maven 2 plugin must be installed. I suggest m2eclipse (http://m2eclipse.codehaus.org/).

Requirements for the address books are as follows:

  • A simple address book with three separate pages.
  • The first page should allow the user to input up to 50 names and phone numbers at a time. The user can input between one and fifty name/numbers at a time.Each name must be unique and have only one phone number.Both the name and the phone number must not be blank.The names and phone numbers should be stored in memory. Phone numbers should be validated to contain only numbers, with an optional + prefix and possibly one pair of brackets with at least one number in them. The phone number must start either with a + or a 0 - if it starts with a +, it cannot be followed by a 0.

  • The second page should list all stored numbers and names, sorted alphabetically.

  • The third page should allow a user to search the address book by phone number (exact number, not substrings) and also by full name or part of a name (case insensitive). It should display all matching names and related phone numbers for the search criteria.