Stock Price Simulation

Larry Wasserman presents an interesting simulation in Problem 11, Chapter 3 of All of Statistics. The problem asks you to simulate the stock market by modeling a simple random walk. With probability 0.5 the price of the stock goes down $1 and with probability 0.5 the stock prices goes up $1. You may recognize this as the same setup in our two simple random walk examples modeling a particle on the real line.

This simulation is interesting because Wasserman notes that even with an equal probability of the stock moving up and down we’re likely to see patterns in the data. I ran some simulations that modeled the change in stock price over the course of 1,000 days and grabbed a couple of graphs to illustrate this point. For example, look at the graph below. It sure looks like this is a stock that’s tanking! However, it’s generated with the random walk I just described.

stock_plot1

Even stocks that generally hover around the origin seem to have noticeable dips and peaks that look like patterns to the human eye even though they are not.

stock_plot2

If we run the simulation multiple times it’s easy to see that if you consider any single stock it’s not so unlikely to get large variations in price (the light purple lines). However, when you consider the average price of all stocks, there is very little change over time as we would expect (the dark purple line).

stock_plot3

Here is the R code to calculate the random walk and generate the last plot:

################################################################
# R Simulation
# James McCammon
# 2/20/2017
################################################################
# This script goes through the simulation of changes in stock
# price data.

# Load plotting libraries
library(ggplot2)
library(ggthemes)
library(reshape2)

#
# Simulate stock price data with random walk
#
    n = 1000 # Walk n steps
    p = .5 # Probability of moving left
    trials = 100 # Num times to repeate sim
    # Run simulation
    rand_walk = replicate(trials, cumsum(sample(c(-1,1), size=n, replace=TRUE, prob=c(p,1-p))))

#
# Prepare data for plotting
#
    all_walks = melt(rand_walk)
    avg_walk = cbind.data.frame(
      'x' = seq(from=1, to=n, by=1),
      'y' = apply(rand_walk, 1, mean)
    )

#
# Plot data
#
    ggplot() + 
      geom_line(data=all_walks, aes(x=Var1, y=value, group=Var2), color='#BCADDC', alpha=.5) +
      geom_line(data=avg_walk, aes(x=x, y=y), size = 1.3, color='#937EBF') +
      theme_fivethirtyeight() +
      theme(axis.title = element_text()) +
      xlab('Days') +
      ylab('Change in Stock Price (in $)') +
      ggtitle("Simulated Stock Prices")


How delicate are relationships?

Recently I have been thinking of dating analytically as a stream of dates where one must build up “relationship capital” before certain pieces of information are revealed or certain negative events occur. The stream must more or less occur in a particular order. That is, if you examined a series of weekly dates over the course of a year and scrambled that order – maybe the 37th date came first, the 5th date came 9th, etc. – the outcome wouldn’t necessarily be the same.

Many of us believe that there are instances when a piece of personal information is revealed “too soon.” For example, if on a first date your companion tells you that he or she recently filed for bankruptcy it may be a “deal breaker” as you assume this reflects negatively on their level of responsibility (as well as being an “overshare”). However, if the same piece of information is revealed after you’ve been dating, say, three months you can weigh the strength of that assumption against the person you’ve come to know. Likewise, some intense external shocks can prematurely end a relationship if they occur too close to the beginning of a relationship whereas  if that same event occurred after sufficient time had passed both partners have would have built up enough “relationship capital” to weather the storm.

I decided to expand on this idea by creating a simple model (which I plan on elaborating over time) by assuming a couple goes on a date once a week for a year (or 52 dates over whatever time period you like). Three pieces of information or events must be reveled only after enough capital has accrued. Event 1, is low level and can only occur after Date 5, Event 2 must occur after Date 20, and Event 3, the most serious, must occur only after Date 40.

What if we kept the concept of “deal breakers” and randomly scrambled the order or the dates. How many relationships would still last a year or more (by chance) simply because events happened after enough capital was built up? It turns out that scrambling the order of the stream of dates results in a failed relationship about 88% of the time.

Of course, this is a theoretical exercise, not just because it’s impossible to scramble the order of dates, but because in practice it is us who decide if we want to be with a person despite difficult times or questionable personal sharing.