stochastic processes and advanced mathematical financesdunbar1/mathematicalfinance/... · ... one...
TRANSCRIPT
Steven R. DunbarDepartment of Mathematics203 Avery HallUniversity of Nebraska-LincolnLincoln, NE 68588-0130http://www.math.unl.edu
Voice: 402-472-3731Fax: 402-472-8466
Stochastic Processes and
Advanced Mathematical Finance
A Coin Tossing Experiment
Rating
Student: contains scenes of mild algebra or calculus that may require guid-ance.
1
Section Starter Question
Suppose you start with a fortune of $10, and you want to gamble to achieve$20 before you go broke. Your gambling game is to flip a fair coin successively,and gain $1 if the coin comes up “Heads” and lose $1 if the coin comes up“Tails”. What do you estimate is the probability of achieving $20 before goingbroke? How long do you estimate it takes before one of the two outcomesoccurs? How do you estimate each of these quantities?
Key Concepts
1. Performing an experiment to gain intuition about coin-tossing games.
Vocabulary
1. We call victory the outcome of reaching a fortune goal by chancebefore going broke.
2. We call ruin the outcome of going broke by chance before reaching afortune goal.
2
Mathematical Ideas
Pure Risk Modeling
We need a better understanding of the paths that risky securities take. Weshall make and investigate a greatly simplified model of randomness and risk.For our model, we assume:
1. Time is discrete, occurring at t0 = 0, t1, t2, . . ..
2. No risk-free investments are available, (i.e. no interest-bearing bonds).
3. No options, and no financial derivatives are available.
4. The only investments are risk-only, that is, our net fortune at the nthtime is a random variable:
Tn+1 = Tn + Yn+1
where T0 = 0 is our given initial fortune, and for simplicity,
Yn =
{+1 probability 1/2
−1 probability 1/2.
Our model is commonly called “gambling” and we will investigate the prob-abilities of making a fortune by gambling.
Some Humor
3
Figure 1: Welcome to my casino!
Figure 2: Where not to lose at the casino!
An Experiment
The reader should perform the following experiment as a “gambler” to gainintuition about the coin-tossing game. We call victory the outcome of reach-ing a fortune goal by chance before going broke. We call ruin the outcome
4
of going broke by chance before reaching a fortune goal.
1. Each “gambler” has a chart for recording the outcomes of each game(see below) and a sheet of graph paper.
2. Each “gambler” has a fair coin to flip, say a penny.
3. Each “gambler” flips the coin, and records a +1 (gains $1) if the coincomes up “Heads” and records −1 (loses $1) if the coin comes up“Tails”. On the chart, the player records the outcome of each flipwith the flip number, the outcome as “H” or “T” and keeps track ofthe cumulative fortune of the gambler so far. Keep these records in aneat chart, since some problems refer to them later.
4. Each “gambler” should record 100 flips, which takes about 10 to 20minutes.
Toss n 1 2 3 4 5 6 7 8 9 10H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 11 12 13 14 15 16 17 18 19 20H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 21 22 23 24 25 26 27 28 29 30H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 31 32 33 34 35 36 37 38 39 40H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 41 42 43 44 45 46 47 48 49 50H or TYn = +1,−1Tn =
∑ni=1 Yi
5
Toss n 51 52 53 54 55 56 57 58 59 60H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 61 62 63 64 65 66 67 68 69 70H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 71 72 73 74 75 76 77 78 79 80H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 81 82 83 84 85 86 87 88 89 90H or TYn = +1,−1Tn =
∑ni=1 Yi
Toss n 91 92 93 94 95 96 97 98 99 100H or TYn = +1,−1Tn =
∑ni=1 Yi
Some Outcomes
A simulation with 30 “gamblers” obtained the following results:
1. 14 gamblers reached a net loss of −10 before reaching a net gain of+10, that is, were “ruined”.
2. 9 gamblers reached a net gain of +10 before reaching a net loss of −10,that is, achieved “victory”.
3. 7 gamblers still had not reached a net loss of +10 or −10 yet.
This roughly matches the predicted outcomes of 1/2 the gamblers beingruined, not as close for the predicted proportion of 1/2 for victory. Themean duration until victory or ruin was 41.6. Compare this to your results.
6
Sources
This section is adapted from ideas in William Feller’s classic text, An Intro-duction to Probability Theory and Its Applications, Volume I, Third Edition.
The GeoGebra script is adapted from a demonstration and discussionamong “carlosgomes” of Amarante, Portugal; Noel Lambert of Vosges, France;and Juan Vicente Snchez Gaitero of Cadiz, Spain on geogebra.org
Algorithms, Scripts, Simulations
Algorithm
The probability of Heads is p. In matrix-oriented languages, use k columnsof the matrix for the iterations of an experiment each with n trials. This ismore efficient than using for-loops or the equivalent.
Then each n× 1 column vector contains the outcomes of the experiment.Use the random number generator to create a random number in (0, 1). Tosimulate the flip of a coin use logical functions to score a 1, also counted asa Head, if the value is less than p; a 0, also counted as a Tail, if the valueis greater than p. Column sums then give a 1 × k vector of total numbersof Heads. Statistical measures can then be applied to this vector. For winsand losses, linearly transform the coin flips to 1 if Heads, a −1 if Tails.Cumulatively sum the entries to create the running total. Apply statisticalmeasures to the last row of the totals.
Finally, set values for victory and ruin. Check when each column firsthits victory or ruin.
Experiment Scripts
Geogebra GeoGebra script for coin flipping experiment
R R script for coin flipping experiment.
7
1 p <- 0.5
2 n <- 100
3 k <- 30
4 coinFlips <- array( 0+( runif(n*k) <= p), dim=c(n,k))
5 # 0+ coerces Boolean to numeric
6 headsTotal <- colSums(coinFlips) # 0..n binomial rv
sample , size k
7 muHeads <- mean(headsTotal) # Expected value is n
/2
8 sigmaSquaredHeads <- var(headsTotal) # Theoretical
value is np(1-p)
9 cat(sprintf("Empirical Mean of Heads: %f \n", muHeads ))
10 cat(sprintf("Empirical Variance of Heads: %f \n",
sigmaSquaredHeads ))
11
12 winLose <- 2*coinFlips - 1 # -1 for Tails , 1 for
Heads
13 totals <- apply( winLose , 2, cumsum)
14 # -n..n (every other integer) binomial rv sample
15 # the second argument ‘‘2’’ means column -wise
16 muWinLose <- mean( totals[n,]) # Expected value is 0
17 sigmaSquaredWinLose <- var( totals[n,]) # Theoretical
value is 4np(1-p)
18 cat(sprintf("Empirical Mean of Wins minus Losses: %f \n",
muWinLose ))
19 cat(sprintf("Empirical Variance of Wins minus Losses: %f
\n", sigmaSquaredWinLose ))
20
Octave Octave script for coin flipping experiment
1 p = 0.5;
2 n = 100;
3 k = 30;
4
5 coinFlips = rand(n,k) <= p;
6 headsTotal = sum(coinFlips); # 0..n binomial rv sample ,
size k
7 muHeads = mean(headsTotal); # Expected value is n/2
8 sigmaSquaredHeads = var(headsTotal); # Theoretical value
is np(1-p)
9 disp("Empirical Mean of Heads:"), disp(muHeads)
8
10 disp("Empirical Variance of Heads:"), disp(
sigmaSquaredHeads)
11
12 winLose = 2* coinFlips - 1; # -1 for Tails , 1 for Heads
13 totals = cumsum(winLose); # -n..n (every other integer)
binomial rv sample
14 muWinLose = mean( totals(n,:) ); # Expected value is 0
15 sigmaSquaredWinLose = var( totals(n,:) ); # Theoretical
value is 4np(1-p)
16 disp("Empirical Mean of Wins minus Losses:"), disp(
muWinLose)
17 disp("Empirical Variance of Wins minus Losses:"), \
18 disp(sigmaSquaredWinLose)
19
Perl Perl PDL script for coin flipping experiment
1 $p = 0.5;
2 $n = 100;
3 $k = 30;
4
5 $coinFlips = random( $k, $n ) <= $p; #note order of
dims!!
6 $headsTotal =
7 $coinFlips ->transpose ->sumover; # 0..n binomial r
.v. sample , size k
8
9 #note transpose , PDL likes x (row) direction for
implicitly threaded operations
10
11 ( $muHeads , $prms , $median , $min , $max , $adev , $rmsHeads
) =
12 $headsTotal ->stats;
13
14 # easiest way to get the descriptive statistics
15
16 printf "Empirical Mean of Heads: %6.3f \n", $muHeads;
17
18 # Theoretical value is np
19 $varHeads = $rmsHeads * $rmsHeads;
20 printf "Emprical Variance of Heads: %6.3f \n", $varHeads;
21
22 # Theoretical value is np(1-p)
23
9
24 $winLose = 2 * $coinFlips - 1; # note use of threading
25 $totals = ( $winLose ->xchg( 0, 1 )->cumusumover )->
transpose;
26
27 # note xchg transpose , PDL likes x (row) direction for
implicity threaded operations
28
29 ( $muWinLose , $prms , $median , $min , $max , $adev ,
$rmsWinLose ) =
30 $totals ( 0 : $k - 1, $n - 1 )->stats; # Expected
value is 0
31
32 # pdl are indexed from 0 like C and Perl , so that’s why
$k -1, $n -1
33
34 printf "Empirical Mean number of Wins over Losses: %6.3f
\n", $muWinLose;
35
36 # Theoretical value is 0
37 $varWinLose = $rmsWinLose * $rmsWinLose; #
Theoretical value is 4np(1-p)
38 printf "Empirical Variance of Wins over Losses: %6.3f \n
", $varWinLose;
39
SciPy Scientific Python script for coin flipping experiment
1 import scipy
2
3 p = 0.5
4 n = 100
5 k = 30
6
7 coinFlips = scipy.random.random ((n,k)) <= p
8 # Note Booleans True for Heads and False for Tails
9 headsTotal = scipy.sum(coinFlips , axis = 0)
10 # Note how Booleans act as 0 (False) and 1 (True)
11 muHeads = scipy.mean(headsTotal)
12 stddevHeads = scipy.std(headsTotal)
13 sigmaSquaredHeads = stddevHeads * stddevHeads
14 print "Empirical Mean of Heads:", muHeads
15 print "Empirical Variance of Heads:", sigmaSquaredHeads
16
17 winLose = 2* coinFlips - 1
10
18 totals = scipy.cumsum(winLose , axis = 0)
19 muWinLose = scipy.mean(totals[n-1,range(k)])
20 stddevWinLose = scipy.std(totals[n-1, range(k)])
21 sigmaSquaredWinLose = stddevWinLose * stddevWinLose
22 print "Empirical Mean of Wins minus Losses:", muWinLose
23 print "Empirical Variance of Wins minus Losses:",
sigmaSquaredWinLose
Victory or Ruin Scripts
R R script for victory or ruin.
1 victory <- 10 # top boundary for random walk
2 ruin <- -10 # bottom boundary for random walk
3
4 victoryOrRuin <- array(0, dim = c(k))
5
6 hitVictory <- apply( (totals >= victory), 2, which)
7 hitRuin <- apply( (totals <= ruin), 2, which)
8
9 for (j in 1:k) {
10 if ( length(hitVictory [[j]]) == 0 && length(
hitRuin [[j]]) == 0 ) {
11 # no victory , no ruin
12 # do nothing
13 }
14 else if ( length(hitVictory [[j]]) > 0 && length(
hitRuin [[j]]) == 0 ) {
15 # victory , no ruin
16 victoryOrRuin[j] <- min(hitVictory [[j]])
17 }
18 else if ( length(hitVictory [[j]]) == 0 && length(
hitRuin [[j]]) > 0 ) {
19 # no victory , ruin
20 victoryOrRuin[j] <- -min(hitRuin [[j]])
21 }
22 else # ( length(hitVictory [[j]]) > 0 && length(
hitRuin [[j]]) > 0 )
23 # victory and ruin
24 if ( min(hitVictory [[j]]) < min(hitRuin [[j]]) ) { #
victory first
25 victoryOrRuin[j] <- min(hitVictory [[j]]) #
code hitting victory
11
26 }
27 else { # ruin first
28 victoryOrRuin[j] <- -min(hitRuin [[j]]) # code
hitting ruin as negative
29 }
30 }
31
32 victoryBeforeRuin <- sum (0+( victoryOrRuin > 0)) # count
exits through top
33 ruinBeforeVictory <- sum (0+( victoryOrRuin < 0)) # count
exits through bottom
34 noRuinOrVictory <- sum (0+( victoryOrRuin == 0))
35
36 cat(sprintf("Victories: %i Ruins: %i No Ruin or Victory:
%i \n",
37 victoryBeforeRuin , ruinBeforeVictory , noRuinOrVictory
))
38
39 avgTimeVictoryOrRuin <- mean( abs(victoryOrRuin) )
40 varTimeVictoryOrRuin <- var( abs(victoryOrRuin) )
41
42 cat(sprintf("Average Time to Victory or Ruin: %f \n",
43 avgTimeVictoryOrRuin))
44 cat(sprintf("Variance of Time to Victory or Ruin: %f \n",
45 varTimeVictoryOrRuin))
46
47 hist( victoryOrRuin , nclass = 2*max(abs(victoryOrRuin))+1
) # sample exit time distribution
48
Octave Octave script for victory or ruin,
1 victory = 10; # top boundary for random walk
2 ruin = -10; # bottom boundary for random walk
3
4 # Octave find treats a whole matrix as a single column ,
5 # but I need to treat experiment column by column , so
here is a place
6 # where for clarity , better to loop over columns , using
find on each
7 victoryOrRuin = zeros(1,k);
8 for j = 1:k
9 hitVictory = find(totals(:,j) >= victory);
10 hitRuin = find(totals(:,j) <= ruin);
12
11 if ( !rows(hitVictory) && !rows(hitRuin) )
12 # no victory , no ruin
13 # do nothing
14 elseif ( rows(hitVictory) && !rows(hitRuin) )
15 # victory , no ruin
16 victoryOrRuin(j) = hitVictory (1);
17 elseif ( !rows(hitVictory) && rows(hitRuin) )
18 # no victory , but hit ruin
19 victoryOrRuin(j) = -hitRuin (1);
20 else # ( rows(hitvictory) && rows(hitruin) )
21 # victory and ruin
22 if ( hitVictory (1) < hitRuin (1) )
23 victoryOrRuin(j) = hitVictory (1); # code hitting
victory
24 else
25 victoryOrRuin(j) = -hitRuin (1); # code hitting ruin as
negative
26 endif
27 endif
28 endfor
29
30 victoryOrRuin # display vector of k hitting times
31
32 victoryBeforeRuin = sum( victoryOrRuin > 0 ) # count
exits through top
33 ruinBeforeVictory = sum( victoryOrRuin < 0 ) # count
exits through bottom
34 noRuinOrVictory = sum( victoryOrRuin == 0 )
35
36 avgTimeVictoryOrRuin = mean( abs(victoryOrRuin) )
37 varTimeVictoryOrRuin = var( abs(victoryOrRuin) )
38
39 max(totals)
40 hist( victoryOrRuin , max(max(victoryOrRuin))) # sample
exit time distribution
41
Perl Perl PDL script for victory or ruin
1 use PDL:: NiceSlice;
2
3 $victory = 10; # top boundary for random walk
4 $ruin = -10; # bottom boundary for random walk
5
13
6 $victoryOrRuin = zeros($k);
7
8 $hitVictory = ( $totals >= $victory );
9 $hitRuin = ( $totals <= $ruin );
10
11 $whenVictory = $hitVictory ( 0 : $k - 1, : )->xchg( 0, 1
)->maximum_ind;
12 $whenRuin = $hitRuin ( 0 : $k - 1, : )->xchg( 0, 1
)->maximum_ind;
13 foreach $i ( 0 .. $k - 1 ) {
14 if ( $whenVictory ($i) == 0 and $whenRuin ($i) == 0 )
{
15
16 # no victory , no ruin , do nothing
17 $victoryOrRuin ($i) = 0;
18 }
19 elsif ( $whenVictory ($i) > 0 and $whenRuin ($i) == 0
) {
20 $victoryOrRuin ($i) .= $whenVictory ($i);
21 } # victory , no ruin
22 elsif ( $whenVictory ($i) == 0 and $whenRuin ($i) > 0
) {
23 $victoryOrRuin ($i) .= -$whenRuin ($i);
24 } # no victory , ruin
25 else {
26 if ( $whenVictory ($i) < $whenRuin ($i) ) { #
victory first
27 $victoryOrRuin ($i) .= $whenVictory ($i); #
code hitting victory
28 }
29 else {
30 $victoryOrRuin ($i)
31 .= -$whenRuin ($i); # code hitting
ruin as negative
32 }
33 }
34 }
35
36 $victoryBeforeRuin =
37 ( $victoryOrRuin > 0 )->sumover; # count exits
through top
38 $ruinBeforeVictory =
39 ( $victoryOrRuin < 0 )->sumover; # count exits
through bottom
40
14
41 ( $muTimeVictoryOrRuin , $prms , $median , $min , $max ,
$adev ,
42 $rmsTimeVictoryOrRuin
43 ) = $victoryOrRuin ->abs ->stats;
44
45 print "Mean time to victory or ruin",
$muTimeVictoryOrRuin , "\n";
46 print "Standard deviation of time to victory or ruin",
$rmsTimeVictoryOrRuin ,
47 "\n";
48
49 $histLimit = $victoryOrRuin ->abs ->max;
50 print hist( $victoryOrRuin , -$histLimit , $histLimit , 1 ),
"\n";
51
SciPy Scientific Python script for victory or ruin.
1 victory = 10; # top boundary for random
walk
2 ruin = -10; # bottom boundary of
random walk
3
4 victoryOrRuin = scipy.zeros(k,int);
5 hitVictory = scipy.argmax( (totals >= victory), axis =0)
6 hitRuin = scipy.argmax( (totals <= ruin), axis =0)
7 for i in range(k):
8 if hitVictory[i] == 0 and hitRuin[i] == 0:
9 # no victory , no ruin , do nothing
10 victoryOrRuin[i] = 0
11 elif hitVictory[i] > 0 and hitRuin[i] == 0:
12 victoryOrRuin[i] = hitVictory[i]
13 elif hitVictory[i] == 0 and hitRuin[i] > 0:
14 victoryOrRuin[i] = -hitRuin[i]
15 else:
16 if hitVictory[i] < hitRuin[i]:
17 victoryOrRuin[i] = hitVictory[i]
18 else:
19 victoryOrRuin[i] = -hitRuin[i]
20
21 victoryBeforeRuin = sum( victoryOrRuin > 0 ) # count
exits through top
22 ruinBeforeVictory = sum( victoryOrRuin < 0 ) # count
exits through bottom
15
23 noRuinOrVictory = sum( victoryOrRuin == 0 )
24
25 print "Victories:", victoryBeforeRuin , "Ruins:",
ruinBeforeVictory ,
26 print "No Ruin or Victory:", noRuinOrVictory
27
28 avgTimeVictoryOrRuin = scipy.mean( abs(victoryOrRuin) )
29 stdTimeVictoryOrRuin = scipy.std( abs(victoryOrRuin) )
30 varTimeVictoryOrRuin = stdTimeVictoryOrRuin *
stdTimeVictoryOrRuin
31
32 print "Average Time to Victory or Ruin:",
avgTimeVictoryOrRuin
33 print "Variance of Time to Victory or Ruin:",
varTimeVictoryOrRuin
Problems to Work for Understanding
1. How many heads were obtained in your sequence of 100 flips? Whatis the class average of the number of heads in 100 flips? What is thevariance of the number of heads obtained by the class members?
2. What is the net win-loss total obtained in your sequence of 100 flips?What is the class average of the net win-loss total in 100 flips? What isthe variance of the net win-loss total obtained by the class members?
3. How many of the class reached a net gain of +10 (call it victory) beforereaching a net loss of −10 (call it ruin)?
4. How many flips did it take before you reached a net gain of +10 (vic-tory) or a net loss of −10 (ruin)? What is the class average of thenumber of flips before reaching victory before ruin at +10 or a net lossof −10?
16
5. What is the maximum net value achieved in your sequence of flips?What is the class distribution of maximum values achieved in the se-quence of flips?
6. Perform some simulations of the coin-flipping game, varying p. Howdoes the value of p affect the experimental probability of victory andruin?
Reading Suggestion:
References
[1] William Feller. An Introduction to Probability Theory and Its Applica-tions, Volume I, volume I. John Wiley and Sons, third edition, 1973. QA273 F3712.
[2] Emmanuel Lesigne. Heads or Tails: An Introduction to Limit Theoremsin Probability, volume 28 of Student Mathematical Library. AmericanMathematical Society, 2005.
Outside Readings and Links:
1. Virtual Laboratories in Probability and Statistics Red and Black GameSearch for Red and Black Game, both the explanation and the simula-tion.
17
2. University of California, San Diego, Department of Mathematics, A.M.Garsia A java applet that simulates how long it takes for a gambler togo broke. You can control how much money you and the casino startwith, the house odds, and the maximum number of games. Results area graph and a summary table. Submitted by Matt Odell, September8, 2003.
I check all the information on each page for correctness and typographicalerrors. Nevertheless, some errors may occur and I would be grateful if you wouldalert me to such errors. I make every reasonable effort to present current andaccurate information for public use, however I do not guarantee the accuracy ortimeliness of information on this website. Your use of the information from thiswebsite is strictly voluntary and at your risk.
I have checked the links to external sites for usefulness. Links to externalwebsites are provided as a convenience. I do not endorse, control, monitor, orguarantee the information contained in any external website. I don’t guaranteethat the links are active at all times. Use the links here with the same caution asyou would all information on the Internet. This website reflects the thoughts, in-terests and opinions of its author. They do not explicitly represent official positionsor policies of my employer.
Information on this website is subject to change without notice.
Steve Dunbar’s Home Page, http://www.math.unl.edu/~sdunbar1Email to Steve Dunbar, sdunbar1 at unl dot edu
Last modified: Processed from LATEX source on July 18, 2016
18