Num Py Random Walk

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Random walks get you nowhere

Well, you might bump into a wall repeatedly or get hit by a car. The best you can do is get pretty close to your desired destination. So I made some random walk simulations using a trinomial model and a pentanomial model. I also devised a strategy based on a high band, but that has nothing to do with the random walks.

Trinomial model
According to the trinomial tree model the stock price can go up, down or stay the same. The logic is undeniable. For each node in the tree we multiply the stock price at that node with a certain factor. This factor is equal to 1, of course if the price is supposed to stay the same. The probabilities for each price change possibility should be calculated using all kinds of parameters. Instead I estimated the probabilities empirically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 ... def pPos( arr ): pos = where( array( arr ) > 0 ) return len( pos[ 0 ] ) / float( len( arr ) ) def pNeg( arr ): neg = where( array( arr ) < 0 ) return len( neg[ 0 ] ) / float( len( arr ) ) ... actual = pPos( numbers ) self.assertAlmostEqual( 3/6.0, actual ) actual ) ... c,v = loadtxt(path( argv[ 1 ] + '.csv' ), delimiter=',', usecols=(6,7), unpack=True) returns = get_returns( c ) prices = [] madC = mad( returns ) pincrease = pPos( returns ) pdecrease = pNeg( returns ) n = 2 ** int( argv[ 2 ] ) madSqrt = madC * sqrt(3) for j in range(0, n): curve = [] curve.append( c[ 0 ] ) rands = uniform(0, 1, len(c)) for i in range(1, len( c ) ): factor = 1 if rands[i] < pincrease: factor = exp( madSqrt ) elif rands[i] < (pincrease + pdecrease): factor = exp( -1 * madSqrt ) curve.append( curve[i - 1] * factor ) prices.append( array( curve ) ) actual = pNeg( numbers ) self.assertAlmostEqual( 2/6.0,

43 44 45 46 47 48 49 50 51 52

prices = array( prices ) prices = prices.T aggregated = [] for curve in prices: aggregated.append( midhinge(curve) ) ...

Notice that I average the values for each point in time with midhinge formula.
1 2 3 4 5 6 7 8 9 10 ... def q1(arr): return stats.scoreatpercentile(arr, 25) def q3(arr): return stats.scoreatpercentile(arr, 75) def midhinge(arr): return (q1(arr) + q3(arr)) / 2 ...

The resulting plot for AAPL looks a bit boring, but at least we did not end up in a river or something.

Pentanomial tree
The pentanomial tree is an enhancement of the trinomial tree, with two extra levels added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ... def pBigUp( arr, limit ): pos = where( array( arr ) > limit ) return len( pos[ 0 ] ) / float( len( arr ) ) def pSmallUp( arr, limit ): pos = where( array( arr ) < limit ) pos = where( array( take( arr, pos ) ) > 0) return len( pos[ 0 ] ) / float( len( arr ) ) def pBigDown( arr, limit ): neg = where( array( arr ) < -1 * limit ) return len( neg[ 0 ] ) / float( len( arr ) ) def pSmallDown( arr, limit ): neg = where( array( arr ) > -1 * limit) neg = where( array( take( arr, neg ) ) < 0) return len( neg[ 0 ] ) / float( len( arr ) )

c,v = loadtxt(path( argv[ 1 ] + '.csv' ), delimiter=',', usecols=(6,7), unpack=True) returns = get_returns( c )

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

prices = [] madC = mad( returns ) ev = rgeomean( returns ) limit = ev + 2 * madC pbu = pBigUp( returns, limit) pbd = pBigDown( returns, limit ) psu = pSmallUp( returns, limit ) psd = pSmallDown( returns, limit ) n = 2 ** int( argv[ 2 ] ) smallPower = madC S = stats.skew( returns ) K = stats.kurtosis( returns ) bigPower = smallPower + abs( S ) / (K * 6 ) for j in range(0, n): curve = [] curve.append( c[ 0 ] ) rands = uniform(0, 1, len(c)) for i in range(1, len( c ) ): factor = 1 if rands[i] < psu: factor = exp( smallPower ) elif rands[i] < (psu + psd): factor = exp( -1 * smallPower ) elif rands[i] < (psu + psd + pbu): factor = exp( bigPower ) elif rands[i] < (psu + psd + pbu + pbd): factor = exp( -1 * bigPower ) curve.append( curve[i - 1] * factor ) prices.append( array( curve ) ) prices = array( prices ) prices = prices.T aggregated = [] aggregated2 = [] for curve in prices: aggregated.append( (max( curve ) + min( curve )) / 2 ) aggregated2.append( midhinge( curve ) ) ...

There seems to be a little bit more action in this plot.

If you liked this post and are interested in NumPy check out NumPy Beginners Guide by yours truly.

You might also like