 Traders' Roundtable A forum for mechanical system traders.

View previous topic :: View next topic 
Author 
Message 
Trading Leech Senior Member
Joined: 29 Nov 2010 Posts: 48

Posted: Thu Nov 03, 2011 8:46 pm Post subject: Question from Vince's "The Mathematics of Money Managem 


Please take a look at page 3 in Vince's "The Mathematics of Money Management" (1992).
How does he obtain this graph? I have tried to reproduce it using the following C code, but I get some erroneous and totally different results. What am I doing wrong?
Code:  #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main (int argc, const char * argv[])
{
const int fractionFineness = 20; //f interval fineness
printf("f TWR\n");
printf("\n");
for (int i=0; i<=fractionFineness; i++)
{
double f = 1.0/fractionFineness*i;
const int numberOfBets = 40; //as in the book
double startingCapital = 1.0; //doesn't matter for final result
int numberOfSimulations = 100000; //Monte Carlo
double expectedFinalCapital = 0;
for (int _c=0; _c<numberOfSimulations; _c++)
{
double currentCapital = startingCapital;
for (int _c2=0; _c2<numberOfBets; _c2++)
{
double betSize = currentCapital*f;
double r = ((double)rand())/RAND_MAX;
assert(r>=0 && r<=1);
if (r<0.5) //tails
currentCapital = 1.0*betSize;
else //heads
currentCapital += 2.0*betSize;
}
expectedFinalCapital += currentCapital/numberOfSimulations;
}
printf("%f %f\n", f, expectedFinalCapital/startingCapital);
}
return 0;
} 
These are my results. Totally out of whack, as you can see:
Code:  f TWR

0.000000 1.000000
0.050000 2.681272
0.100000 7.033180
0.150000 18.041265
0.200000 45.286080
0.250000 116.339518
0.300000 254.900325
0.350000 619.910976
0.400000 1315.125641
0.450000 4145.293687
0.500000 6106.482120
0.550000 17338.963085
0.600000 33052.989488
0.650000 50256.643405
0.700000 515449.227614
0.750000 13816.685338
0.800000 34357.379275
0.850000 54704.916534
0.900000 464443.893644
0.950000 47729.574970
1.000000 0.000000 


Back to top 


stopsareforwimps Roundtable Knight
Joined: 09 Oct 2010 Posts: 199 Location: Melbourne Australia

Posted: Thu Nov 03, 2011 10:36 pm Post subject: Re: Question from Vince's "The Mathematics of Money Man 


Trading Leech wrote:  Please take a look at page 3 in Vince's "The Mathematics of Money Management" (1992).
How does he obtain this graph? I have tried to reproduce it using the following C code, but I get some erroneous and totally different results. What am I doing wrong?... 
I don't have that book but...
You need to be clear what you are trying to calculate. You seem to be computing something like the arithmetic average of the various returns over the 40 bets.
You probably should be taking the geometric means if you want to know expected compound return rate (multiply the end results together and take the 40th root of the result). Another way to get an 'expected' return is to compute the median return. This is more likely to be typical than the arithmetic average, which will be polluted by a few unrepresentative and massive returns.
I did the geometric mean and the 0.25 best bet is confirmed in that case. You need to be careful of floating underflow and overflow.
Median worked OK too.


Back to top 


sluggo Roundtable Knight
Joined: 11 Jun 2004 Posts: 2959

Posted: Fri Nov 04, 2011 6:02 am Post subject: 


This line of code has got a bug
Code:  expectedFinalCapital += currentCapital/numberOfSimulations; 
Make three or four copies of your program and give them different names. Then edit each program, to make it calculate the average of your 10000 MC sim results, a different way. Run all four of these programs. The one which tells you that max(TWR) occurs at f=0.25, is the one to study further.
Tim Arnold's allpurpose debugging advice applies here too: PRINT your intermediate calculations, so you can see which one is producing an unexpected ("wrong") result


Back to top 


Trading Leech Senior Member
Joined: 29 Nov 2010 Posts: 48

Posted: Fri Nov 04, 2011 8:15 am Post subject: 


sluggo wrote:  This line of code has got a bug 
I don't understand. This just look like regular MC to me. But I tried changing it to only
Code:  expectedFinalCapital += currentCapital; 
and then dividing by "numberOfSimulations" at the end, but that (of course) yields the same result. I don't see why I'm averaging this incorrectly. It looks like the regular MC average to me...


Back to top 


sluggo Roundtable Knight
Joined: 11 Jun 2004 Posts: 2959

Posted: Fri Nov 04, 2011 8:49 am Post subject: 


PRINT your intermediate results. Study them and find "anomalies"
Code and run four (not two) different ways of averaging the 10,000 monte carlo results.
I will put a note on my calendar to check back here in 6 weeks' time, to learn whether you've fixed the problem(s). That ought to be plenty of time to perform LOTS of experiments, lots of doovers (recode starting from a blank sheet of paper), and lots of intermediateresults checking.
If you still haven't reported success, then sometime after Dec 16, 2011, I'll post a splash of (nonC++) code which computes the correct answer. Might be Blox source code, might be APL, might be a bash script, I'll decide later.


Back to top 


stopsareforwimps Roundtable Knight
Joined: 09 Oct 2010 Posts: 199 Location: Melbourne Australia

Posted: Sat Nov 05, 2011 3:52 am Post subject: 


Trading Leech wrote:  sluggo wrote:  This line of code has got a bug 
I don't understand. This just look like regular MC to me. But I tried changing it to only
Code:  expectedFinalCapital += currentCapital; 
and then dividing by "numberOfSimulations" at the end, but that (of course) yields the same result. I don't see why I'm averaging this incorrectly. It looks like the regular MC average to me... 
You have all the information that you need. Are you actually calculating expected compound return by averaging the total returns and averaging those? No, you are calculating something else. You are calculating the average total return. But surely they are the same thing...?
Here is my output (for copying into a csv file)
Code: 
Median return by fraction
0.0,1.0,
0.05,2.411714024837409,
0.1,4.660957143849302,
0.15000000000000002,7.366234841925617,
0.2,9.646293093274938,
0.25,10.5450938424492,
0.30000000000000004,9.646293093274934,
0.35000000000000003,7.366234841925614,
0.4,4.6609571438493,
0.45,2.4117140248374085,
0.5,1.0,
0.55,0.3225790586242929,
0.6000000000000001,0.07756279363818937,
0.65,0.013059231100277797,
0.7000000000000001,0.001401683395356256,
0.75,0.00008271806125530277,
0.8,0.0000020896178655943026,
0.8500000000000001,0.00000001409614843505329,
0.9,0.000000000008773252460082304,
0.9500000000000001,0.00000000000000001687951814185192,
1.0,0.0,
Average return by fraction
0.0,1.0,
0.05,2.689620828845793,
0.1,7.019904173179042,
0.15000000000000002,18.17310600582705,
0.2,45.1735701438515,
0.25,109.00395427810811,
0.30000000000000004,256.62123614633043,
0.35000000000000003,678.1590027337628,
0.4,1438.9059784666745,
0.45,4023.174355354776,
0.5,7441.689295331302,
0.55,6280.506453882783,
0.6000000000000001,20068.718869842516,
0.65,66440.0894274343,
0.7000000000000001,85263.83546308047,
0.75,107592.8607763377,
0.8,28827.51391077118,
0.8500000000000001,7456.689783143323,
0.9,1198.6683959993893,
0.9500000000000001,853.8264523099347,
1.0,0.0,
Average compound return by fraction
0.0,1.0,
0.05,2.407877356842422,
0.1,4.679322978041792,
0.15000000000000002,7.353413875873102,
0.2,9.708735780823204,
0.25,10.452021040214635,
0.30000000000000004,9.538685406501083,
0.35000000000000003,7.425107657110994,
0.4,4.6450082686534335,
0.45,2.363674796718191,
0.5,0.9664051178746541,
0.55,0.32337510373011835,
0.6000000000000001,0.07656175486532302,
0.65,0.012500384462452531,
0.7000000000000001,0.001435696217457491,
0.75,0.00008464286342168247,
0.8,0.0000020984263921793466,
0.8500000000000001,0.000000014665951698520266,
0.9,0.000000000008849296898355004,
0.9500000000000001,0.000000000000000016774979568055827,
1.0,0.0,

Here is the code in lisp.
Code: 
(defun csvhelper (outputstream formatargument colonmodifierp atmodifierp)
(declare (ignore colonmodifierp atmodifierp))
(typecase formatargument
(float (format outputstream "~F" formatargument))
(t (format outputstream "~S" formatargument))))
(defun csvline (outputstream formatargument colonmodifierp atmodifierp)
(declare (ignore colonmodifierp atmodifierp))
(if (consp formatargument)
(format outputstream "~{~/csvhelper/,~}" formatargument)
(format outputstream "~/csvhelper/," formatargument)))
(defun csv (listofitems)
"Output a list of items in csv format. Each item can be one thing or a list and is printed on one line.
Print floating point double precision without the trailing d0"
(format t "~&~{~/csvline/~%~}" listofitems))
(defun median (l)
(let ((ll (length l)))
(nth (truncate (/ ll 2)) (sort (copylist l) #'<))))
(defun sum (list)
(reduce #'+ list))
(defun average (list)
(assert (> (length list) 0))
(/ (sum list) (length list)))
(defun safelog (x)
(if (not (plusp x))
9999999
(log x)))
(defun avglog (list)
(average (mapcar #'safelog list)))
(defun invincible (functiontoapplytoreturns)
(let* ((finenesses 20)
(finenessfraction (/ 1.0d0 finenesses))
(startcap 1.0d0)
(numberofbets 40)
(numberofsimulations 100000))
(loop for i from 0 to finenesses by 1 collect
(let ((f (* finenessfraction i)))
(list f (funcall functiontoapplytoreturns (loop for c from 1 by 1 to numberofsimulations collect
(let ((capital startcap))
(loop for i from 1 to numberofbets do
(let ((betsize (* capital f)))
(if (< (random 1.0d0) 0.5d0)
(decf capital (* betsize 1.0d0))
(incf capital (* betsize 2.0d0)))))
capital))))))))
(defun rr ()
(format t "~&Median return by fraction~%")
(csv (invincible #'median))
(format t "~&Average return by fraction~%")
(csv (invincible #'average))
(format t "~&Average compound return by fraction~%")
(csv (mapcar (lambda (el) (list (first el) (exp (second el)))) (invincible #'avglog))))



Back to top 


Ted Annemann Roundtable Knight
Joined: 15 Apr 2003 Posts: 119 Location: Arizona

Posted: Sat Nov 05, 2011 8:24 am Post subject: 


US schools teach a course called "Algebra1" to fifteenyearold kids. This course covers the mathematics that helps you and other >15 year olds to recognize that in Ralph Vince's coin toss game,
TWR = { (1 + 2f)^Nheads } x { (1  f)^Ntails }
where: TWR is Vince's Terminal Wealth Relative
f is Vince's "divisor of the biggest perceived loss", i.e., the abscissa of his Figure 11
Nheads is the number of profitable coinflips
Ntails is the number of unprofitable coinflips
^ is the "raised to the power" operator, e.g., 2^3 = 8, 3^2 = 9, 4^3 = 64, etc
You can plot this expression and look for the maximum. You will find that it occurs at f = 0.25, just as Ralph Vince says. Example plots, and the Excel spreadsheets which generated them, are attached. I used Nheads=20 and Ntails=20, same as Vince's Fig 11.
However  can you use mathematics to solve for the exact position of the maximum, without needing to make a graph? Yes you can. In the US they teach this to finalyear secondary school students (~17 years old) and/or firstyear university students: Work out dTWR/df, set it equal to zero (since you seek the maximum), then solve for f. That's the location of the maximum.
Description: 

Filesize: 
23.28 KB 
Viewed: 
2679 Time(s) 

Description: 
Excel spreadsheet used to generate the plots above 

Download 
Filename: 
rvince_plotz.xls 
Filesize: 
40 KB 
Downloaded: 
157 Time(s) 


Back to top 


stopsareforwimps Roundtable Knight
Joined: 09 Oct 2010 Posts: 199 Location: Melbourne Australia

Posted: Sat Nov 05, 2011 7:30 pm Post subject: 


Ted Annemann wrote:  US schools teach a course called "Algebra1" to fifteenyearold kids... 
The coin toss is solvable via basic calculus because it is very simple. Other more complex distributions are not so easily solvable.
My reading of RV's use of this example is that he does it to give people an intuitive feel for the relationship between risk and expected compound returns. The key takeout is that while more risk increases average returns without bounds, after a while it costs you in expected compound returns due to volatility drag and risk of ruin.
Monte Carlo simulation is a good way to go in general because it is easy to apply to pretty well any distribution.
Some pitfalls:
The optimum bet size depends on the entire distribution. It is particularly sensitive to the most extreme tails of the distribution. If a given bet size could make you go bust, then in the very long term it will, and the expected compound return from such a bet size is 100%. The coin flip example does not have fat tails and does not demonstrate this problem and so it is somewhat misleading.
The optimum bet size depends on the distribution. This may seem to be an obvious point, but I have had at least two people tell me that "optimum bet size is 25%", period. One guy told me he had 25% of his net worth in each of 4 stocks, because 25% was the optimum bet size. Really.
Our information about the distribution of returns is incomplete. As Nassim Taleb has shown in the papers on his web site, we know very little or nothing about how fat the tails are, even assuming a stationary distribution of returns. We strongly suspect that the distributions are not stationary. But even if they were stationary, our ignorance of the fatness of the tails declines exponentially as we move further out. That is, as we move out into the domain of moves that could wipe us out the profundity of our ignorance explodes.
If you assume a normal or lognormal distribution of returns, then the problem is semicontainable. But with a nonnormal levy distribution or other fat tailed distribution  such as those that exist in the real world of trading  our ignorance may well be fatal.

Vince's formulas (later on in his books) require input of a maximum loss as input to the optimum bet size calculation. We really don't know this number. Perhaps we can limit the maximum loss by using put options. Vince has some interesting arguments that this can allow you to achieve higher riskweighted returns. My personal experience with using options for hedging is that the results may be underwhelming in terms of the precision of the hedge achieved.


Back to top 


sluggo Roundtable Knight
Joined: 11 Jun 2004 Posts: 2959

Posted: Sun Nov 06, 2011 12:41 pm Post subject: 


The usealgebraandmakeaplot approach, which requires neither calculus nor Monte Carlo simulation, can be applied to a wide range of distributions. Vince's cointoss example employs a particularly simplistic distribution (the "binomial distribution"), which is a histogram having exactly two bars. Other distributions have histograms with more than two bars. (For example: a throw of two standard 6sided dice has a distribution whose histogram includes eleven bars). Any histogram with a finite number of bars is amenable to the usealgebraandmakeaplot approach.
This is comforting because a large number of realworld distributions can be very well approximated by histograms having "only" 2000 bars. You construct the histogram starting at 10 standard deviations, and build it upwards to +10 standard deviations, with a stepsize (histogram bin width) of 0.01 standard deviations. 20/0.01 = 2000 bins. So you can analyze a large number of realworld distributions without calculus. Also without Monte Carlo simulation. It's messy but far from impossible.
Here's a fun little homework problem whose distribution histogram has got more than 2 bars. See whether you can find the optimal f (which maximizes TWR) without using Monte Carlo and without using calculus. Just algebra and a plot:A game is played with two standard decks of 52 cards each: the "Green Deck" and the "Yellow Deck".
On each play, the bettor chooses a betsize for this play (call it "B dollars"), and each deck is thoroughly shuffled. The bettor draws one card from each deck.
The bettor loses if neither of the drawn cards is a Spade, and he wins if one or both of the drawn cards is a Spade. Wins are paid off as follows.
 If both of the drawn cards are the Ace of Spades, he is paid off at 100to1.
 If one or both of the drawn cards is a face card (Jack, King, or Queen), he is paid off at 4to1.
 If neither of the drawn cards is a face card, he is paid off at 2to1.
Summary: Zero spades means: lose. More than zero spades means: win. If win and no facecard, payoff 2x. If win and facecard, payoff 4x. If win and two Aces of Spades, payoff 100x.
To illustrate, here are some example draws, and their payoffs
1. (7club, 9heart) lose B dollars
2. (7club, 9spade) win 2B dollars
3. (7spade, 9spade) win 2B dollars
4. (Jspade, Qspade) win 4B dollars
5. (Kheart, 3spade) win 4B dollars
6. (Aspade, Aspade) win 100B dollars
7. (Aspade, Aclub) win 2B dollars


Back to top 


Ted Annemann Roundtable Knight
Joined: 15 Apr 2003 Posts: 119 Location: Arizona

Posted: Mon Nov 07, 2011 10:02 am Post subject: 


Try the "A2345 game": Bet B dollars, shuffle a deck of cards, draw 1 card. Look only at the card's value (disregard suit). Payoffs are: Ace wins B dollars
 2 wins 2B dollars
 3 wins 3B dollars
 4 wins 4B dollars
 5 wins 5B dollars
 any other card (610, J, Q, K) loses B dollars
Then return the card to the deck and shuffle again.
The distribution is triangular shaped, which is delightful, and the optimal f is not even close to 0.25, which will annoy the people who tell stopsareforwimps that optimalf is always equal to 0.25, in every situation, with no exceptions.


Back to top 




You cannot post new topics in this forum You cannot reply to topics in this forum You can edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum

