Chapter 7.1 Exercise 2

The price of a stock on a given trading day changes according to the distribution

\[ p_X = \left( \matrix{-1 & 0 & 1 & 2 \\ 1/4 & 1/2 & 1/8 & 1/8} \right) \]

Find the distribution for the change in stock price after two (independent) trading days.

Mostly manual calculation…

pneg1 <- .25
p0 <- .5
p1 <- .125
p2 <- .125

Sneg2 <- (pneg1 * pneg1)
Sneg1 <- (pneg1 * p0) + (p0 * pneg1)
S0 <- (pneg1 * p1) + (p0 * p0) + (p1 * pneg1)
S1 <- (pneg1 * p2) + (p0 * p1) + (p1 * p0) + (p2 * pneg1)
S2 <- (p0 * p2) + (p1 * p1) + (p2 * p0)
S3 <- (p1 * p2) + (p2 * p1)
S4 <- (p2 * p2)

\[ p_S = \left( \matrix{-2 & -1 & 0 & 1 & 2 & 3 & 4 \\ 0.0625 & 0.25 & 0.3125 & 0.1875 & 0.140625 & 0.03125 & 0.015625} \right) \]

Writing my own function…

I got this working!!!

outcomes <- c(-1, 0, 1, 2)
probabilities <- c(.25, .5, .125, .125)
d <- matrix(c(outcomes, probabilities), nrow = 2, byrow = TRUE)

# function argument x must be a matrix with outcomes in the first row and corresponding probabilities in the second.

convolution <- function(x){
  row1 <- x[1,]
  row2 <- x[2,]
  sums <- c()
  p <- c()
  
  for(i in row1){
    temp <- i + x[1,]
    sums <- c(sums, temp)
  }
  
  for(i in row2){
    temp <- i * x[2,]
    p <- c(p, temp)
  }
  
  distr <- rbind(sums, p)
  distr <- aggregate(distr[2,], list(distr[1,]), FUN = sum)
  distr <- t(distr)
  rownames(distr) <- c('Sums', 'Probabilities')
  
  return(distr)
}

conv <- convolution(d)
conv
##                  [,1]  [,2]   [,3]   [,4]     [,5]    [,6]     [,7]
## Sums          -2.0000 -1.00 0.0000 1.0000 2.000000 3.00000 4.000000
## Probabilities  0.0625  0.25 0.3125 0.1875 0.140625 0.03125 0.015625

Using the R convolve function…

This still doesn’t work right. Can’t seem to figure out how to use this function…

X <- c(.25, .5, .125, .125)
X
## [1] 0.250 0.500 0.125 0.125
convolve(X,X,type='open')
## [1] 0.031250 0.093750 0.203125 0.343750 0.203125 0.093750 0.031250