Content Tagged With "statistics"

Using the Kelly Criterion to Manage a Bankroll in Stock Markets
why the hell not

I stumbled upon an interesting sentence while working my way through "An Introduction to Statistical Learning with Applications in R." By the way, the book is available for free here. You should check it out! I don't have any strong opinions on how useful it is, yet, for I only just started reading. But, so far so good.

About that sentence. It says:

Nevertheless, in Chapter 4, we explore these data using several different statistical learning methods. Interestingly, there are hints of some weak trends in the data that suggest that, at least for this 5-year period, it is possible to correctly predict the direction of movement in the market approximately 60% of the time

I immediately thought that this might be an excellent exercise to learn how the Kelly Criterion could be used to optimize returns. The Kelly Criterion is a method to determine the optimal size of a series of bets each of which is given by (directly from wikipedia )

where:

  • f* is the fraction of the current bankroll to wager, i.e. how much to bet;
  • b is the net odds received on the wager ("b to 1"); that is, you could win $b (on top of getting back your $1 wagered) for a $1 bet
  • p is the probability of winning;
  • q is the probability of losing, which is 1 − p.

The idea is if you invest (bet) according to the formula, you will do better in the long run than had you used any different betting strategy. When I read the sentence about being able to reliably predict the market's direction I thought it might be fun to apply the Kelly Criterion to see what kind of returns you could get.

If you're paying attention, you might have noticed the formula that requires you to know the net odds received on the bet. That is, you not only have to know the direction, but also the magnitude. Doh! Let's not let that stop us from seeing if we can get some results.

The book uses Quadratic Discriminant Analysis (QDA) to fit an QDA model to stock market data to predict the direction of market movement over a period of time. The dataset consists of percentage returns for the S&P 500 over 1, 250 days from 2001 to the end of 2005. The idea is to use the percent return for each of the previous five days and the volume traded on teh previous day to predict direction. Once the model is trained, you can run the predict() function to get the probability of the market direction given five days of previous percent changes and the previous day's volume. In other words, feed the predict function the right data and make a trade based on what the function says to do. You have a 60% chance of being correct.

Wasn't I talking about the Kelly Criterion a minute ago? Yes! I was. If you have an edge (that is, a greater than 50% chance of winning) there is an optimal betting strategy that will maximize your returns while guaranteeing your bank roll won't go to zero.

Let's pretend we are investing in an S&P 500 ETF. Ignoring taxes and transaction costs (of course) your strategy is to buy at market open and sell at market close. If the market is predicted to go up, buy long. If the market is predicted to go down, short the ETF. How much you buy is defined by the Kelly Criterion function defined above. p = 60% but what is the net odds? Let's be naive and compute it as the volatility for the previous five days.

 install.packages("ISLR")
 library(ISLR)
 library(MASS)

summary(Smarket) attach(Smarket) plot(Volume)

train=(Year <2005) Smarket.2005= Smarket [! train ,] dim(Smarket.2005) Direction.2005=Direction[!train]

qda.fit=qda(Direction~Lag1+Lag2,data=Smarket ,subset=train)

qda.fit

Call: qda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train) Prior probabilities of groups: Down Up 0.491984 0.508016 Group means: Lag1 Lag2 Down 0.04279022 0.03389409 Up -0.03954635 -0.03132544

qda.class=predict(qda.fit,Smarket.2005)$class table(qda.class ,Direction.2005) mean(qda.class==Direction.2005)

export it to csv

write.csv(Smarket.2005, file="/tmp/Smarket.2005.csv") write.csv(qda.class, file="/tmp/qda.class.csv")

I exported it to csv so I could work with it in python and pandas because I'm more comfortable in that environment and I'm not sure there are any examples using pandas on this site, yet. I use it way more than I use r, believe it or not. Ok, so, Smarket.2005.csv has rows numbered 999-1250 and qda.class.csv has rows numbered 1-252 which means the rows will line up. Let's pull them into ipython notebook.

Aaaand, it is bed time. BUT, there isn't much more to do other than show gain/loss for each day using the Kelly Criterion starting with a $10,000 bank roll.


2014-12-07

Real Analysis and Measure Theory
Not as scary as you might think

Do you know anything about real analysis and measure theory? I was thinking about a gambling problem I couldn't figure out and dug in to find out why. I still don't have an answer, but I learned that measure theory isn't as scary as I thought. If you don't know anything about it, these are two, very accessible introductions:

why use measure theory for probability part 1 - https://www.youtube.com/watch?v=RjPXfUT7Odo&feature=player_embedded

intro to brownian motion and the ito integral part 1 - https://www.youtube.com/watch?v=fEsnMyi5-8k&list=PLC7A868A384F6EFF0

here is the guy from the first video http://www.math.missouri.edu/~evanslc/

Terrance Tao's boodk http://terrytao.files.wordpress.com/2012/12/gsm-126-tao5-measure-book.pdf

A really simple intro to financial math from a cs teacher https://www.youtube.com/playlist?list=PL1BaGV1cIH4UXkZgrX2TtXhvUQW6_4hZF

he also has a site for these videos, might want to check out the probablility refreshers for FE's: http://byrnetube.com/mathematicalFinance.php

this all started from some guy's comments about measure theory on this "how to quant" HN post https://news.ycombinator.com/item?id=8698986

I want to do a calc refresher. Here are some resources for doing that:

calc refresher (See notes) https://news.ycombinator.com/item?id=2258794 http://ocw.mit.edu/resources/res-18-006-calculus-revisited-single-variable-calculus-fall-2010/ http://ocw.mit.edu/resources/res-18-005-highlights-of-calculus-spring-2010/ http://ocw.mit.edu/ans7870/resources/Strang/Edited/Calculus/Calculus.pdf

Mathematics: Its Content, Methods and Meaning - preview on books.google.com available at Olin Level B Stacks QA36 A453 in all three parts. And it is only $25 on amazon http://www.amazon.com/Mathematics-Content-Methods-Meaning-Dover/dp/0486409163/

And also i should read Sheldon Axler's "Linear Algebra Done Right"