Assignment Submitted to Prof. Hlynka - University of Windsor

This is a simulation project to study the stochastic behavior of stock prices based on a simple probability model. The transaction is aggregated for 70 days and the profit or loss is computed on 71st day after selling all the stocks using that day’s price as the sale price.

#####################STOCHASTIC SIMULATION OF STOCK PRICES###############

##this vector stores individual profit from transactions aggregated across 70 days. There are 
### 10,000 simulations altogether.
library(ggplot2)
allprofits <- 0  

###############################EXPERIMENATL BLOCK###############
for (j in 1:10000){
  
  profit <- 0     ## Initialization
  prices <- 9     ##First day stock price
  x <- 9          ## Variable to hold the changing stock prices
  numStocks <- 0  
  salesAmount <- 0
  
############# DAILY STOCK PRICES SIMULATION BLOCK ####################
for (i in 1:70){
  numStocks <- numStocks + (1000/x)
  simProb <- sample(1:100, 1)/100
  if (x >=9){
    upProb <- (14-x)/10
    if (simProb <= upProb) {
      x <- x+1
      prices <- c(prices, x)
      
    }
    else {
      x <- x-1
      prices <- c(prices, x)
    }
    
  }
  else {
    upProb <- 1-(x-4)/10
    if (simProb <= upProb) {
      x <- x+1
      prices <- c(prices, x)
    }
    else {
      x <- x-1
      prices <- c(prices, x)
      
    }
    
  }
  
}
  
########## Transaction on 71st day #############
salesAmount <- numStocks*prices[71]
profit <- salesAmount - 70000
allprofits <- c(allprofits, profit)
####################################

}

Output and Plots

summary(allprofits)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -34369   -6996    1727    1944   10434   53976
hist(allprofits, freq = TRUE)
par(new = TRUE)
plot(density(allprofits), xlab = "", ylab = "", main = "", col = "red", lwd = 3)

par(mfrow = c(1,1))
indexVal <- 1:10001
isProfit <- allprofits > 0
df <- data.frame(allprofits, indexVal, isProfit )

ggplot(df, aes(x = isProfit, y = allprofits, colour = isProfit)) + geom_boxplot()

ggplot(df, aes(x=indexVal, y=allprofits, colour=isProfit)) + geom_point()

Conclusion

It is concluded that the dollar cost averaging strategy minimizes downside risk and paired with the symmetric probability distribution the expected profit was positive. In fact, when looking at a sample size of 10 000, the median and mean profit came out to be $2000. Considering the model on the surface looks to be unbiased by every measure, this was a very unintuitive, yet interesting conclusion. Overall, the project showed a good perspective of why computer aided simulation is a powerful tool for any statistician to learn, as it can help model and predict scenarios that initially may be too complicated to understand, and a simulation might even further our understandings of said scenario.