Posts RSS Comments RSS 72 Posts and 762 Comments till now

Archive for November, 2007

Enlightened By Randomness

Weeks ago I read the book Fooled by Randomness. Besides the interesting stories on financial trading and risk management, the core message of the book is the fallibility of human knowledge. The fallacies that we often make are:

1. Overly associative. As my favorite quote in the book states, “Symbolism is the child of our inability and unwillingness to accept randomness.” We human like to assign meanings to anything, even when there are none. Religious people look at a Jesus-looking cloud and think it is a sign from God. Literature students discuss about symbolisms they find in books that the authors haven’t even thought about. In quality management, W. Edwards Deming noted that managers panic over common causes of variation which are just noises in the business process. Managers see a drop in revenue and demand explanation when in fact it is just a random event.

2. Survivorship Bias. We tend to see things that are special cases and mistake them as the norm. For example, newspapers report violences everyday. We read them and think that we are living in a horrible world. In a TED talk by Steven Pinker, he objectively analyzes historical data of violence and find that we are now living in the most peaceful time of human history. We read The Millionaire Next Door and think that education is not important to getting rich. It is true that highschool dropouts can be successful, but that does not mean they have the same chance to succeed as a college graduate.

3. Abnormal Distribution. Every process have a distribution of its outcomes. Fat tails in financial markets are common. If you have 99% chance of winning $1 and 1% chance of losing $100, the odds are against you even if you are to win 99% of the time.

4. Other Monte Carlo paths. In studying history and past events, people tend to judge a decision base on what happened instead of what could have happened. If a manager spends $1000 and wins a lottary that pays $1,000,000 when the odds of winning is 1 in 1,000,000, he is likely to get promoted for “making a smart move” or “taking some good risk” instead of getting fired for making a stupid bet.

Language Processing By the Human Brain

reading_test.jpg

Today I came across the above image. It is amazing how the human brain can process words even when the letters are misordered. What makes it more interesting is that I should not have a gene that corrects English words as my ancestors are Chinese. This ability to auto-correct English words must be caused by a more general error-tolerant communication mechanism in the brain. This reminds me of the complexity of the mathematics in signal processing, noise filtering, and error correction algorithms. Now I can only gasp at the wonder of the process of Evolution.

Robocode - Predictive Targeting Implementation

When I was building my bot, the most interesting aspect is implementing predictive targeting.   To tackle this problem, I first define two aspects I can use for prediction:  Velocity Change and Angular Change per turn.

Here is how I capture these two parameters:

angularChange = (targetHeading - lastTargetHeading) / turnsElapsed;
velocityChange = (targetVelocity - lastTargetVelocity) / turnsElapsed;

After capturing these two values, I can calculate an angle in addition to the current bearing such that the bullet shot from that new angle can arrive within 5 pixels of the target in some future turn.

Here is how I calculate future bullet and target positions at futuren turn:

double estTargetX = targetX;
double estTargetY = targetY;

for (double time = 1; time < 100; time++) {

  estTargetX = estTargetX + Math.sin(Math.toRadians((targetHeading + time * angularChange))) *(targetVelocity + time * velocityChange);
  estTargetY = estTargetY + Math.cos(Math.toRadians((targetHeading + time * angularChange))) * (targetVelocity + time * velocityChange);

  estBulletX = myX + Math.sin(Math.toRadians(myAngle)) * (time - timeTurnGun) * bulletSpeed;
  estBulletY = myY + Math.cos(Math.toRadians(myAngle)) * (time - timeTurnGun) * bulletSpeed;
}

Once the distance between the target and bullet is less than 5 pixels, I will turn the gun to myAngle and fire a shot.

I’ve attached my robocode file for reference.

My Robocode Bot

On Robocode & Strategy

Days ago I’ve stumpled upon a programming game called Robocode while I was reading up AI.   Basically it is a platform for programming intelligent tanks that fight with each other. 

http://robocode.sourceforge.net/

After playing with it for a while, I discover the following to be true for heads-up battles:

1. It is always better to move around, this avoids targets.

2. It is always better to predict the movement of the enemy and shoot accordingly.

3. It is always better to travel at unpredictable angles and speeds so your enemy cannot predict your future position.

Having this in mind, I started designing my robot to avoid bullets and predict targets.

As it turns out, my robot comes out to be pretty intelligent.

Through implementing my robocode, I realize the following:

1. Each action has a strategic value, which is:

(My Benefit - My Cost) - (Opponent Benefit - Opponent Cost).

Although looks simple, this equation entails the impacts on future actions.

2. Some actions have higher strategic values than other actions.

3. Given the rules of the game, some actions would always have higher strategic value than other actions.

4. A working strategy comprises of a sequence of high-value actions.

5. Theories are important in finding out what needs to be done. (In robocode’s case, I have used trigonometry extensively).

6. Good execution skills are important to carry out what needs to be done. (In robocode’s case, good execution is good programming in Java).

These six points can be applied to any strategic settings, be it chess or business.   Of course, the games can be much more complex than robocode.  But the principles of strategy are the same.

Money Management, Kelly Criterion and the Stock Market

It all starts with a simple game:

A coin has 80% chance of flipping a HEAD and 20% chance of flipping a tail. At each turn of the game you place a bet. If the coin flips a HEAD, you get back twice the amount you bet. If the coin flips a TAIL, you lose your bet.

You have $10 available for betting, and you can play as many turns as you can afford.

The question is: Would you play this game? If so, how would you play it?

Although you have an edge in winning each turn, without proper money management you will eventually lose.  Why?  Because when you play long enough, you will run into a streak of bad turns and lose all your money.   It is just probability.

So how can you systematically exploit this edge?  Many money management systems can solve this problem.   My personal favorite is the Kelly Criterion.  It is the percentage of your bank roll that you should bet which will give you the highest long term geometric mean of return.

To see this mathematically, let’s define some variables:

F = % of the money available for betting

W1 = Return on investment (ROI) if you win: 100%

P1 = Probability of Winning: 80%

W2 = Return on investment (ROI) if you lose: -100%

P2 = Probability of Losing: 20%

B(0) = Initial amount available for betting (bank roll): $10

B(N) = Future bank roll after N turns

M = Geometric mean of return of N turns

With the above definition, we can formulate:

B(N) = B(0) * (1 + W1 * F)^(P1 * N) * (1 + W2 * F)^(P2 * N)

M^N = B(N)/B(0) = (1 + W1 * F)^(P1 * N) * (1 + W2 * F)^(P2 * N)

M = [(1+W1*F)^(P1*N) * (1+W2*F)^(P2*N)]^(1/N)

M = (1+W1*F)^(P1) * (1+W2*F)^(P2)

To maximize M, we can maximize the natural log of M:

Ln(M) = Ln[(1+W1*F)^(P1) * (1+W2*F)^(P2)]

Ln(M) = P1*Ln(1+W1*F) + P2*Ln(1+W2*F)

The above equation is what Ed Thorp stated in chapter 7 of his paper “THE KELLY CRITERION IN BLACKJACK, SPORTS BETTING, AND THE STOCK MARKET”, in which he discusses how to apply the Kelly Criterion in the stock market.

For our example, which has only two outcomes, the solution for the maximization is: 

F = [W1*P1 - P2] / (1+W1) = 60%

This means you should bet 60% of your available bank roll each turn.

In the investment world (ie. stock market), the problem becomes more complicated as you might have more than two outcomes.   For example, you might have:

Stock X is going to launch new products.

If product A is launched, ROI = 40% (probability = 40%)

If product B is launched, ROI = 20% (probability = 20%)

If product C is launched, ROI = 30% (probability = 30%)

If no products are launched, ROI = -80% (probability = 10%)

What percentage of your money should you invest in X?

It is mathematically difficult to find a formula to apply for such problems.  However, you can use a free web Kelly calculator at: http://www.cisiova.com/betsizing.asp

To learn more about the Kelly Criterion and Money Management, I recommend you to read “Fortune’s Formula” by William Poundstone.

« Prev