#Exploration of the Kelly Wealth Process for AR(1) ####################################################### # Illustrates sequential code development. # This code is just to get you started. # It "runs" but it definitely has some weird features that # I will look at during the day today. ###################################################### # # Version 1: Just to get a sense of the problem. ######################################################### error.model=function(n){rnorm(n, sd=.01)} # Note that errror.model is of class function. This is the class that is # required in the assignment to the rand.gen argument of the arima.sim function. #More about this shortly. # First, just the usual AR(1) code. arreturns=arima.sim(model=list(ar=0.3), n=100, n.start=200, start.innov=rnorm(200, sd=.01), rand.gen=error.model ) #Now the wealth process w=rep(1, 100) for(j in 2:100){ w[j]=w[j-1]*( 1 + 0.3 *arreturns[j-1]*arreturns[j]/(0.09*arreturns[j-1]^2 +.0001) ) } #Looking at the result. w # This is a wild ride. You can think of some issues. It would be useful to keep track # of the bet sizes. If the bet fraction is more than 1, indicating leverage, then # serious code would either cap the max bet at 100% or else do the computation where # we pay for our margin borrowing. tsplot(w) ######################################################### #Version 2. # Let's wrap this stuff up in a function with FEWER embedded constants. # We'll still keep the standard deviation of the model as an embedded constant. KW=function(rho, num){ stderr=.01 #Embedded constant error.model=function(n){rnorm(n, sd=.01)} #Not a great design, but it is the best I can do right now. arreturns=arima.sim(model=list(ar=rho), n=num, n.start=200, start.innov=rnorm(200, sd=stderr), rand.gen=error.model ) w=rep(1, num) for(j in 2:num){ w[j]=w[j-1]*( 1 + rho*arreturns[j-1]*arreturns[j]/(rho^2*arreturns[j-1]^2 +stderr^2) ) } return(w) } #Testing... w=KW(rho=.3, num=200) tsplot(w) #This works but the science is wild. We still have the issues of bet sizes bigger than one #and no accounting for margin. #It's also irritating that we needed to hard code the stadard deviation for the error model. #Perhaps you will find out what the issue is here. ############################################################ # These two versions should be enough to get you going. # Here are three issues that are worth resolving. # 1. Understanding the sizes of the bets. # It would be useful to print out the Kelly factions # to help us understant the wildness of the wealth function. # If bet factors are larger than one or smaller than -1 we should deal with the margin cost --- # or do something. # Working our the margin version of Kelly is really more than I expect people to do in # this assigmenent but it should be # understood as an issue. # 2. Here it would be easy and appropriate to cap bet fractions to be in [-1,1]. # Why the minus? If the previous period return was negative, then we actually go short in this # model. # 3. S-Plus Mystery. Why can't we pass the model standard error as a value to the function KW? # I'll bet we can, but when I just followed my nose I got error messages. Perhaps someone in # the class can give us a hint.