<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/price-volatility-basic-brownian-motion/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://stoltzmaniac.com/price-volatility-basic-brownian-motion/"> R-Projects - Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h6>The Situation</h6>
<p>You are a consultant who has been hired by a business that sells one commodity product. On December 31st the price is $100 per unit. The business owner wants to know what to expect by the end of January.</p>
<p>Your client gave you the message: </p>
<ul>
<li>Prices are based off the the sales the previous day </li>
<li>Roughly 95% of the time, the price will be +/- $10 compared to the day before</li>
</ul>
<p>With only a few minutes to make the call, how would you decide on what to expect for the end of January?</p>
<p>Simple: <br>
<strong>Expected Value</strong> = $100</p>
<p>Wouldn’t that make sense? You could give yourself a pat on the back and walk away.</p>
<p>However, because you want to be hired again, you want to provide your client options and a range of what to expect.</p>
<p>The inner nerd in you screams out, “I want to simulate price changes for the month!” </p>
<p><strong><em>You make the rather large assumption</em></strong> that the daily changes are normally distributed.</p>
<p>So you write some code based off of your assumptions and create a basic simulation based off of Brownian Motion.</p>
<p><img alt="plot" src="https://i2.wp.com/www.stoltzmaniac.com/content/images/2016/12/sales-volatility-basic-brownian-motion/unnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>set.seed(5)

initialPrice = 100  
dailyPlusMinus = 10  
dailyDeviation = dailyPlusMinus/2  
# assumes 2*sigma roughly approximates 95% range on normal dist.

#Note: This isn't the proper way to calculate a standard deviation. 
We really have no information on how the +/- was calculated,  
so it's the best you can do.

n = 31 #number of days in January  
N = 500 #number of simulations

prices = matrix(ncol=N,nrow=n)  
for(i in 1:N){  
  prices[,i] = initialPrice + 
    cumsum(rnorm(n = n,mean = 0,sd = dailyDeviation))
}

steps = 1:nrow(prices)  
yLimits = c(initialPrice-dailyDeviation*n/1.5,  
          initialPrice+dailyDeviation*n/1.5)
plot(steps,prices[,1],type='l',  
     ylim=yLimits,xlab='Days',
     ylab='Daily Price ($)',
     main='Simulation of Daily Prices for the Month')
for(i in 2:ncol(prices)){  
  lines(prices[,i])
}
</code></pre>
<p>After plotting the data, you quickly notice that you still don’t quite have the answer you’re looking for, but you do have a great visualization.</p>
<p>You quickly plot a histogram of the simulated end of month prices.</p>
<p><img alt="plot" src="https://i0.wp.com/www.stoltzmaniac.com/content/images/2016/12/sales-volatility-basic-brownian-motion/unnamed-chunk-2-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>endOfMonthPrices = prices[n,]  
hist(endOfMonthPrices,main='Histogram End of Month Price',  
     xlab='Price ($)',30)
</code></pre>
<p>You also visualize your data by looking at a CDF plot. <br>
<img alt="plot" src="https://i2.wp.com/www.stoltzmaniac.com/content/images/2016/12/sales-volatility-basic-brownian-motion/unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>plot(ecdf(endOfMonthPrices),  
     main='CDF of End of Month Price',
     xlab='Price ($)')
</code></pre>
<p>Eyeballing the charts, it appears as if the majority of your data suggests the price will wind up between $50 and $150. You finally decide to pull the hard numbers.</p>
<pre><code>##  1%  5% 10% 25% 33% 50% 66% 75% 90% 95% 99% 
##  41  57  66  82  88  99 111 119 133 141 162
</code></pre>
<pre><code>decis = c(0.01,0.05,0.10,0.25,0.33,  
        0.5,0.66,0.75,0.90,0.95,0.99)
deciPrices = round(quantile(endOfMonthPrices,  
                  decis),0)
print(deciPrices)  
</code></pre>
<p>You check to see if the results make sense: <br>
<strong>YES</strong> – The 50% decile value is right around $100 <br>
<strong>YES</strong> – The deciles surrounding 50% are roughly equal (i.e. <strong>1% = $41</strong> and <strong>99% = $162</strong> are <strong>each roughly $60 away from $99</strong>)</p>
<p>You understand what you have here, but it may not be as intuitive for your client. Someone could look at it and say, “there’s a 1% chance it will be $41 and a 99% chance it will be $162” which is incorrect.</p>
<p>With your level of experience, you understand it’s wise to say:</p>
<ul>
<li>The most likely price is going to be near $100</li>
<li>The end of month price almost certainly going to fall between $160 and $40</li>
<li>Confidence intervals are available upon request</li>
</ul>
<p>The messaging and communication will 100% be based upon the client. In this case, it’s assuming someone doesn’t have any sort of stats background and just wants a high-level view.</p>
<p>Obviously, the majority of this analysis was completely unnecessary. However, that wouldn’t have been as much fun and you wouldn’t have learned as much! It’s nice to have something like this in your back pocket when you begin to analyze an increasing the number of variables, variance, and outcomes.</p>
<p>Code used in this post is on my <a target="_blank" rel="nofollow" href="https://github.com/stoltzmaniac/priceVolatilityBrownianMotion">GitHub</a></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://stoltzmaniac.com/price-volatility-basic-brownian-motion/"> R-Projects - Stoltzmaniac</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_Jnj5Vx5Mbw"></a></div>