New earnings release post

master
Bradlee Speice 2016-06-09 19:27:36 -04:00
parent 0ba3723cd2
commit f9e8c08491
26 changed files with 17371 additions and 1256 deletions

View File

@ -82,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/archives.html"></a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -82,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/author/bradlee-speice.html"></a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -82,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/author/bradlee-speice.html"></a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -82,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/author/bradlee-speice.html">Bradlee Speice</a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -95,7 +95,7 @@ Because these are all very similar, we decided to demonstrate all 3 products at
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="k">using</span> <span class="n">Gadfly</span>
<div class=" highlight hl-julia"><pre><span class="k">using</span> <span class="n">Gadfly</span>
</pre></div>
</div>
@ -131,10 +131,10 @@ Because these are all very similar, we decided to demonstrate all 3 products at
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">S0</span> <span class="o">=</span> <span class="mf">102.2</span>
<div class=" highlight hl-julia"><pre><span class="n">S0</span> <span class="o">=</span> <span class="mf">102.2</span>
<span class="n">nominal</span> <span class="o">=</span> <span class="mi">100</span>
<span class="n">q</span> <span class="o">=</span> <span class="mf">2.84</span> <span class="o">/</span> <span class="mi">100</span>
<span class="n">σ</span> <span class="o">=</span> <span class="mf">15.37</span> <span class="o">/</span> <span class="mi">100</span>
<span class="err">σ</span> <span class="o">=</span> <span class="mf">15.37</span> <span class="o">/</span> <span class="mi">100</span>
<span class="n">term</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">49</span><span class="p">,</span> <span class="o">.</span><span class="mi">9</span><span class="p">,</span> <span class="mf">1.21</span><span class="p">,</span> <span class="mf">1.45</span><span class="p">,</span> <span class="mf">1.69</span><span class="p">]</span> <span class="o">/</span> <span class="mi">100</span> <span class="o">+</span> <span class="mi">1</span>
<span class="c">###</span>
@ -187,7 +187,7 @@ Because these are all very similar, we decided to demonstrate all 3 products at
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">simulate_gbm</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">μ</span><span class="p">,</span> <span class="n">σ</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span class="n">simulate_gbm</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="err">μ</span><span class="p">,</span> <span class="err">σ</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="c"># Set the initial state</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">length</span><span class="p">(</span><span class="n">S0</span><span class="p">)</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">T</span> <span class="o">/</span> <span class="n">n</span>
@ -196,7 +196,7 @@ Because these are all very similar, we decided to demonstrate all 3 products at
<span class="c"># Build out all states</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="n">motion</span><span class="p">[:,</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">motion</span><span class="p">[:,</span><span class="n">i</span><span class="p">]</span> <span class="o">.*</span> <span class="n">exp</span><span class="p">((</span><span class="n">μ</span> <span class="o">-</span> <span class="n">σ</span><span class="o">^</span><span class="mi">2</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">t</span><span class="p">)</span> <span class="o">.*</span> <span class="n">exp</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">σ</span> <span class="o">.*</span> <span class="n">randn</span><span class="p">(</span><span class="n">m</span><span class="p">))</span>
<span class="n">motion</span><span class="p">[:,</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">motion</span><span class="p">[:,</span><span class="n">i</span><span class="p">]</span> <span class="o">.*</span> <span class="n">exp</span><span class="p">((</span><span class="err">μ</span> <span class="o">-</span> <span class="err">σ</span><span class="o">^</span><span class="mi">2</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">t</span><span class="p">)</span> <span class="o">.*</span> <span class="n">exp</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="err">σ</span> <span class="o">.*</span> <span class="n">randn</span><span class="p">(</span><span class="n">m</span><span class="p">))</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">motion</span>
@ -258,9 +258,9 @@ Because these are all very similar, we decided to demonstrate all 3 products at
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">initial</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span class="n">initial</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="n">S0</span>
<span class="c"># Using μ=0, T=.25 for now, we&#39;ll use the proper values later</span>
<span class="n">motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">initial</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">σ</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
<span class="n">motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">initial</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="err">σ</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
<span class="n">display_motion</span><span class="p">(</span><span class="n">motion</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">)</span>
</pre></div>
@ -1866,7 +1866,7 @@ fig.select("#fig-3a6dd25ad25c4037a166889ee51bb151-element-20")
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">forward_term</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">yearly_term</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span class="n">forward_term</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">yearly_term</span><span class="p">)</span>
<span class="c"># It is assumed that we have a yearly term structure passed in, and starts at year 0</span>
<span class="c"># This implies a nominal rate above 0 for the first year!</span>
<span class="n">years</span> <span class="o">=</span> <span class="n">length</span><span class="p">(</span><span class="n">term</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span> <span class="c"># because we start at 0</span>
@ -1896,14 +1896,14 @@ fig.select("#fig-3a6dd25ad25c4037a166889ee51bb151-element-20")
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="c"># Example term structure taken from:</span>
<div class=" highlight hl-julia"><pre><span class="c"># Example term structure taken from:</span>
<span class="c"># http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield</span>
<span class="c"># Linear interpolation used years in-between periods, assuming real-dollar</span>
<span class="c"># interest rates</span>
<span class="n">forward_yield</span> <span class="o">=</span> <span class="n">forward_term</span><span class="p">(</span><span class="n">term</span><span class="p">)</span>
<span class="n">calculated_term2</span> <span class="o">=</span> <span class="n">term</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">forward_yield</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Actual term[2]: </span><span class="si">$(term[2])</span><span class="s">; Calculated term[2]: </span><span class="si">$(calculated_term2)</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Actual term[2]: </span><span class="si">$</span><span class="s">(term[2]); Calculated term[2]: </span><span class="si">$(calculated_term2)</span><span class="s">&quot;</span><span class="p">)</span>
</pre></div>
</div>
@ -1940,11 +1940,11 @@ fig.select("#fig-3a6dd25ad25c4037a166889ee51bb151-element-20")
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">full_motion</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span class="n">full_motion</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="n">S0</span>
<span class="n">full_term</span> <span class="o">=</span> <span class="n">vcat</span><span class="p">(</span><span class="n">term</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">forward_yield</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">T</span>
<span class="n">μ</span> <span class="o">=</span> <span class="p">(</span><span class="n">full_term</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">q</span><span class="p">)</span>
<span class="n">year_motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">full_motion</span><span class="p">[:,</span><span class="k">end</span><span class="p">],</span> <span class="n">μ</span><span class="p">,</span> <span class="n">σ</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="err">μ</span> <span class="o">=</span> <span class="p">(</span><span class="n">full_term</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">q</span><span class="p">)</span>
<span class="n">year_motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">full_motion</span><span class="p">[:,</span><span class="k">end</span><span class="p">],</span> <span class="err">μ</span><span class="p">,</span> <span class="err">σ</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="n">full_motion</span> <span class="o">=</span> <span class="n">hcat</span><span class="p">(</span><span class="n">full_motion</span><span class="p">,</span> <span class="n">year_motion</span><span class="p">)</span>
<span class="k">end</span>
@ -3501,14 +3501,14 @@ fig.select("#fig-0378e04b897742b597befd2e8e1c169e-element-20")
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">full_simulation</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">term</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span class="n">full_simulation</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">term</span><span class="p">)</span>
<span class="n">forward</span> <span class="o">=</span> <span class="n">vcat</span><span class="p">(</span><span class="n">term</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">forward_term</span><span class="p">(</span><span class="n">term</span><span class="p">))</span>
<span class="c"># And an S0 to kick things off.</span>
<span class="n">final_motion</span> <span class="o">=</span> <span class="n">ones</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="o">*</span> <span class="n">S0</span>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">T</span>
<span class="n">μ</span> <span class="o">=</span> <span class="p">(</span><span class="n">forward</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">q</span><span class="p">)</span>
<span class="n">year_motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">final_motion</span><span class="p">[:,</span><span class="k">end</span><span class="p">],</span> <span class="n">μ</span><span class="p">,</span> <span class="n">σ</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="err">μ</span> <span class="o">=</span> <span class="p">(</span><span class="n">forward</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">q</span><span class="p">)</span>
<span class="n">year_motion</span> <span class="o">=</span> <span class="n">simulate_gbm</span><span class="p">(</span><span class="n">final_motion</span><span class="p">[:,</span><span class="k">end</span><span class="p">],</span> <span class="err">μ</span><span class="p">,</span> <span class="err">σ</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="n">final_motion</span> <span class="o">=</span> <span class="n">hcat</span><span class="p">(</span><span class="n">final_motion</span><span class="p">,</span> <span class="n">year_motion</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">return</span> <span class="n">final_motion</span>
@ -3517,7 +3517,7 @@ fig.select("#fig-0378e04b897742b597befd2e8e1c169e-element-20")
<span class="n">tic</span><span class="p">()</span>
<span class="n">full_simulation</span><span class="p">(</span><span class="n">S0</span><span class="p">,</span> <span class="n">T</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">term</span><span class="p">)</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">toq</span><span class="p">()</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Time to run simulation: </span><span class="si">%.2f</span><span class="s">s&quot;</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Time to run simulation: %.2fs&quot;</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
</pre></div>
</div>
@ -3562,7 +3562,7 @@ fig.select("#fig-0378e04b897742b597befd2e8e1c169e-element-20")
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<span class="n">strike</span> <span class="o">=</span> <span class="n">S0</span>
<span class="n">protection_barrier</span> <span class="o">=</span> <span class="n">S0</span> <span class="o">*</span> <span class="o">.</span><span class="mi">6</span>
<span class="n">coupon</span> <span class="o">=</span> <span class="n">nominal</span> <span class="o">*</span> <span class="o">.</span><span class="mi">07</span>
@ -3576,7 +3576,7 @@ fig.select("#fig-0378e04b897742b597befd2e8e1c169e-element-20")
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">t</span>
<span class="n">price</span> <span class="o">=</span> <span class="n">year_prices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">if</span> <span class="n">price</span> <span class="n"></span> <span class="n">call_barrier</span>
<span class="k">if</span> <span class="n">price</span> <span class="err"></span> <span class="n">call_barrier</span>
<span class="k">return</span> <span class="p">(</span><span class="n">nominal</span> <span class="o">+</span> <span class="n">coupon</span><span class="o">*</span><span class="n">i</span><span class="p">)</span> <span class="o">*</span> <span class="n">exp</span><span class="p">((</span><span class="n">prod</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="k">end</span><span class="p">])</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">t</span><span class="o">-</span><span class="n">i</span><span class="p">))</span>
<span class="k">end</span>
<span class="k">end</span>
@ -3606,13 +3606,13 @@ fig.select("#fig-0378e04b897742b597befd2e8e1c169e-element-20")
<span class="n">tic</span><span class="p">()</span>
<span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">athena</span><span class="p">()</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">toq</span><span class="p">()</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation </span><span class="si">%i</span><span class="s">: \$</span><span class="si">%.4f</span><span class="s">; Simulation time: </span><span class="si">%.2f</span><span class="s">s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">time</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation %i: \</span><span class="si">$</span><span class="s">%.4f; Simulation time: %.2fs</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">time</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">final_mean</span> <span class="o">=</span> <span class="n">mean</span><span class="p">(</span><span class="n">mean_payoffs</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$num_simulations</span><span class="s"> simulations: </span><span class="si">$(mean(mean_payoffs))</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$</span><span class="s">num_simulations simulations: </span><span class="si">$</span><span class="s">(mean(mean_payoffs))&quot;</span><span class="p">)</span>
<span class="n">pv</span> <span class="o">=</span> <span class="n">final_mean</span> <span class="o">*</span> <span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">T</span><span class="p">))</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Athena note: \$</span><span class="si">%.2f</span><span class="s">, notional: \$</span><span class="si">%.2f</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">,</span> <span class="n">nominal</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Athena note: \</span><span class="si">$</span><span class="s">%.2f, notional: \</span><span class="si">$</span><span class="s">%.2f&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">,</span> <span class="n">nominal</span><span class="p">)</span>
</pre></div>
</div>
@ -3673,7 +3673,7 @@ Present value of Athena note: $95.00, notional: $100.00</pre>
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<span class="n">coupon_barrier</span> <span class="o">=</span> <span class="n">S0</span> <span class="o">*</span> <span class="o">.</span><span class="mi">8</span>
<span class="n">protection_barrier</span> <span class="o">=</span> <span class="n">S0</span> <span class="o">*</span> <span class="o">.</span><span class="mi">6</span>
<span class="n">coupon</span> <span class="o">=</span> <span class="n">nominal</span> <span class="o">*</span> <span class="o">.</span><span class="mi">06</span>
@ -3686,9 +3686,9 @@ Present value of Athena note: $95.00, notional: $100.00</pre>
<span class="k">for</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">t</span>
<span class="n">price</span> <span class="o">=</span> <span class="n">year_prices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">if</span> <span class="n">price</span> <span class="n"></span> <span class="n">call_barrier</span>
<span class="k">if</span> <span class="n">price</span> <span class="err"></span> <span class="n">call_barrier</span>
<span class="k">return</span> <span class="p">(</span><span class="n">nominal</span> <span class="o">+</span> <span class="n">coupon</span> <span class="o">+</span> <span class="n">total_coupons</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">((</span><span class="n">prod</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="k">end</span><span class="p">])</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">t</span><span class="o">-</span><span class="n">i</span><span class="p">))</span>
<span class="k">elseif</span> <span class="n">price</span> <span class="n"></span> <span class="n">coupon_barrier</span>
<span class="k">elseif</span> <span class="n">price</span> <span class="err"></span> <span class="n">coupon_barrier</span>
<span class="n">total_coupons</span> <span class="o">=</span> <span class="n">total_coupons</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">coupon</span>
<span class="k">else</span>
<span class="n">total_coupons</span> <span class="o">*=</span> <span class="n">exp</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
@ -3720,13 +3720,13 @@ Present value of Athena note: $95.00, notional: $100.00</pre>
<span class="n">tic</span><span class="p">()</span>
<span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">phoenix_no_memory</span><span class="p">()</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">toq</span><span class="p">()</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation </span><span class="si">%i</span><span class="s">: \$</span><span class="si">%.4f</span><span class="s">; Simulation time: </span><span class="si">%.2f</span><span class="s">s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">time</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation %i: \</span><span class="si">$</span><span class="s">%.4f; Simulation time: %.2fs</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">time</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">final_mean</span> <span class="o">=</span> <span class="n">mean</span><span class="p">(</span><span class="n">mean_payoffs</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$num_simulations</span><span class="s"> simulations: </span><span class="si">$(mean(mean_payoffs))</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$</span><span class="s">num_simulations simulations: </span><span class="si">$</span><span class="s">(mean(mean_payoffs))&quot;</span><span class="p">)</span>
<span class="n">pv</span> <span class="o">=</span> <span class="n">final_mean</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Phoenix without memory note: \$</span><span class="si">%.2f</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Phoenix without memory note: \</span><span class="si">$</span><span class="s">%.2f&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
</pre></div>
</div>
@ -3773,7 +3773,7 @@ Present value of Phoenix without memory note: $97.44</pre>
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<span class="n">coupon_barrier</span> <span class="o">=</span> <span class="n">S0</span> <span class="o">*</span> <span class="o">.</span><span class="mi">8</span>
<span class="n">protection_barrier</span> <span class="o">=</span> <span class="n">S0</span> <span class="o">*</span> <span class="o">.</span><span class="mi">6</span>
<span class="n">coupon</span> <span class="o">=</span> <span class="n">nominal</span> <span class="o">*</span> <span class="o">.</span><span class="mi">07</span>
@ -3827,14 +3827,14 @@ Present value of Phoenix without memory note: $97.44</pre>
<span class="n">tic</span><span class="p">()</span>
<span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">phoenix_with_memory</span><span class="p">()</span>
<span class="n">time</span> <span class="o">=</span> <span class="n">toq</span><span class="p">()</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation </span><span class="si">%i</span><span class="s">: \$</span><span class="si">%.4f</span><span class="s">; Simulation time: </span><span class="si">%.2f</span><span class="s">s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Mean of simulation %i: \</span><span class="si">$</span><span class="s">%.4f; Simulation time: %.2fs</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="n">i</span><span class="p">,</span> <span class="n">mean_payoffs</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">time</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">final_mean</span> <span class="o">=</span> <span class="n">mean</span><span class="p">(</span><span class="n">mean_payoffs</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$num_simulations</span><span class="s"> simulations: </span><span class="si">$(mean(mean_payoffs))</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Mean over </span><span class="si">$</span><span class="s">num_simulations simulations: </span><span class="si">$</span><span class="s">(mean(mean_payoffs))&quot;</span><span class="p">)</span>
<span class="n">pv</span> <span class="o">=</span> <span class="n">final_mean</span> <span class="o">*</span> <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">forward_structure</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">T</span><span class="p">))</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Phoenix with memory note: \$</span><span class="si">%.2f</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
<span class="p">@</span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;Present value of Phoenix with memory note: \</span><span class="si">$</span><span class="s">%.2f&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
</pre></div>
</div>

View File

@ -82,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/category/blog.html">Blog</a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -83,6 +83,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/category/blog.html">Blog</a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -83,6 +83,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/category/blog.html">Blog</a></h2>
<dl class="dl-horizontal">
<dt>Wed 08 June 2016</dt>
<dd><a href="https://bspeice.github.io/event-studies-and-earnings-releases.html">Event Studies and Earnings Releases</a></dd>
<dt>Sun 15 May 2016</dt>
<dd><a href="https://bspeice.github.io/the-unfair-casino.html">The Unfair Casino</a></dd>
<dt>Wed 06 April 2016</dt>

View File

@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="In [1]: import pickle import pandas as pd import numpy as np from bokeh.plotting import output_notebook, figure, show from bokeh.palettes import RdBu4 as Palette from datetime import datetime ...">
<meta name="description" content="In [1]: import pickle import pandas as pd import numpy as np from bokeh.plotting import output_notebook, figure, show from bokeh.palettes import RdBu4 as Palette from datetime import datetime import ...">
<meta name="keywords" content="data science, weather">
<link rel="icon" href="https://bspeice.github.io/favicon.ico">
@ -92,7 +92,7 @@
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">pickle</span>
<div class=" highlight hl-ipython3"><pre><span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">bokeh.plotting</span> <span class="k">import</span> <span class="n">output_notebook</span><span class="p">,</span> <span class="n">figure</span><span class="p">,</span> <span class="n">show</span>
@ -359,7 +359,7 @@
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">city_forecasts</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&#39;city_forecasts.p&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">))</span>
<div class=" highlight hl-ipython3"><pre><span class="n">city_forecasts</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;city_forecasts.p&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">))</span>
<span class="n">forecasts_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">city_forecasts</span><span class="p">)</span>
</pre></div>
@ -373,24 +373,24 @@
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;binghamton&#39;</span><span class="p">,</span> <span class="s1">&#39;cary&#39;</span><span class="p">,</span> <span class="s1">&#39;nyc&#39;</span><span class="p">,</span> <span class="s1">&#39;seattle&#39;</span><span class="p">]</span>
<div class=" highlight hl-ipython3"><pre><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;binghamton&#39;</span><span class="p">,</span> <span class="s">&#39;cary&#39;</span><span class="p">,</span> <span class="s">&#39;nyc&#39;</span><span class="p">,</span> <span class="s">&#39;seattle&#39;</span><span class="p">]</span>
<span class="n">city_colors</span> <span class="o">=</span> <span class="p">{</span><span class="n">cities</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">Palette</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">)}</span>
<span class="k">def</span> <span class="nf">safe_cover</span><span class="p">(</span><span class="n">frame</span><span class="p">):</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s1">&#39;cloudCover&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s1">&#39;cloudCover&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s">&#39;cloudCover&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s">&#39;cloudCover&#39;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">NaN</span>
<span class="k">def</span> <span class="nf">monthly_avg_cloudcover</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<span class="n">dates</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="n">end</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="n">freq</span><span class="o">=</span><span class="s1">&#39;D&#39;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">)</span>
<span class="n">cloud_cover_vals</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">safe_cover</span><span class="p">(</span><span class="n">forecasts_df</span><span class="p">[</span><span class="n">city</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;currently&#39;</span><span class="p">]),</span> <span class="n">dates</span><span class="p">))</span>
<span class="n">freq</span><span class="o">=</span><span class="s">&#39;D&#39;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s">&#39;left&#39;</span><span class="p">)</span>
<span class="n">cloud_cover_vals</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">safe_cover</span><span class="p">(</span><span class="n">forecasts_df</span><span class="p">[</span><span class="n">city</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s">&#39;currently&#39;</span><span class="p">]),</span> <span class="n">dates</span><span class="p">))</span>
<span class="n">cloud_cover_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">NaN</span><span class="p">,</span> <span class="n">cloud_cover_vals</span><span class="p">)))</span>
<span class="c1"># Ignore an issue with nanmean having all NaN values. We&#39;ll discuss the data issues below.</span>
<span class="c"># Ignore an issue with nanmean having all NaN values. We&#39;ll discuss the data issues below.</span>
<span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;ignore&#39;</span><span class="p">)</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&#39;ignore&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">cloud_cover_vals</span><span class="p">),</span> <span class="n">cloud_cover_samples</span>
</pre></div>
@ -404,23 +404,23 @@
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">years</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">2016</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span class="n">years</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">2016</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">city_avg_cc</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">monthly_avg_cloudcover</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">month</span><span class="p">)</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">years</span><span class="p">]</span>
<span class="n">months</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="s1">&#39;July&#39;</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span>
<span class="p">(</span><span class="s1">&#39;August&#39;</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span>
<span class="p">(</span><span class="s1">&#39;September&#39;</span><span class="p">,</span> <span class="mi">9</span><span class="p">),</span>
<span class="p">(</span><span class="s1">&#39;October&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s1">&#39;November&#39;</span><span class="p">,</span> <span class="mi">11</span><span class="p">)</span>
<span class="p">(</span><span class="s">&#39;July&#39;</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span>
<span class="p">(</span><span class="s">&#39;August&#39;</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span>
<span class="p">(</span><span class="s">&#39;September&#39;</span><span class="p">,</span> <span class="mi">9</span><span class="p">),</span>
<span class="p">(</span><span class="s">&#39;October&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s">&#39;November&#39;</span><span class="p">,</span> <span class="mi">11</span><span class="p">)</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">month</span><span class="p">,</span> <span class="n">month_id</span> <span class="ow">in</span> <span class="n">months</span><span class="p">:</span>
<span class="n">month_averages</span> <span class="o">=</span> <span class="p">{</span><span class="n">city</span><span class="p">:</span> <span class="n">city_avg_cc</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">month_id</span><span class="p">)</span> <span class="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">}</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> Average Cloud Cover&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">month</span><span class="p">),</span>
<span class="n">x_axis_label</span><span class="o">=</span><span class="s1">&#39;Year&#39;</span><span class="p">,</span>
<span class="n">y_axis_label</span><span class="o">=</span><span class="s1">&#39;Cloud Cover Percentage&#39;</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">&quot;{} Average Cloud Cover&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">month</span><span class="p">),</span>
<span class="n">x_axis_label</span><span class="o">=</span><span class="s">&#39;Year&#39;</span><span class="p">,</span>
<span class="n">y_axis_label</span><span class="o">=</span><span class="s">&#39;Cloud Cover Percentage&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">line</span><span class="p">(</span><span class="n">years</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">month_averages</span><span class="p">[</span><span class="n">city</span><span class="p">]],</span>
<span class="n">legend</span><span class="o">=</span><span class="n">city</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">city_colors</span><span class="p">[</span><span class="n">city</span><span class="p">])</span>
@ -609,21 +609,21 @@
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">safe_precip</span><span class="p">(</span><span class="n">frame</span><span class="p">):</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s1">&#39;precipProbability&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s1">&#39;precipProbability&#39;</span><span class="p">]</span>
<div class=" highlight hl-ipython3"><pre><span class="k">def</span> <span class="nf">safe_precip</span><span class="p">(</span><span class="n">frame</span><span class="p">):</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s">&#39;precipProbability&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s">&#39;precipProbability&#39;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">NaN</span>
<span class="k">def</span> <span class="nf">monthly_avg_precip</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<span class="n">dates</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="n">end</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="n">freq</span><span class="o">=</span><span class="s1">&#39;D&#39;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">)</span>
<span class="n">precip_vals</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">safe_precip</span><span class="p">(</span><span class="n">forecasts_df</span><span class="p">[</span><span class="n">city</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;currently&#39;</span><span class="p">]),</span> <span class="n">dates</span><span class="p">))</span>
<span class="n">freq</span><span class="o">=</span><span class="s">&#39;D&#39;</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="s">&#39;left&#39;</span><span class="p">)</span>
<span class="n">precip_vals</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">safe_precip</span><span class="p">(</span><span class="n">forecasts_df</span><span class="p">[</span><span class="n">city</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s">&#39;currently&#39;</span><span class="p">]),</span> <span class="n">dates</span><span class="p">))</span>
<span class="n">precip_samples</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">NaN</span><span class="p">,</span> <span class="n">precip_vals</span><span class="p">)))</span>
<span class="c1"># Ignore an issue with nanmean having all NaN values. We&#39;ll discuss the data issues below.</span>
<span class="c"># Ignore an issue with nanmean having all NaN values. We&#39;ll discuss the data issues below.</span>
<span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;ignore&#39;</span><span class="p">)</span>
<span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&#39;ignore&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">precip_vals</span><span class="p">),</span> <span class="n">precip_samples</span>
<span class="k">def</span> <span class="nf">city_avg_precip</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
@ -631,9 +631,9 @@
<span class="k">for</span> <span class="n">month</span><span class="p">,</span> <span class="n">month_id</span> <span class="ow">in</span> <span class="n">months</span><span class="p">:</span>
<span class="n">month_averages</span> <span class="o">=</span> <span class="p">{</span><span class="n">city</span><span class="p">:</span> <span class="n">city_avg_cc</span><span class="p">(</span><span class="n">city</span><span class="p">,</span> <span class="n">month_id</span><span class="p">)</span> <span class="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">}</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> Average Precipitation Chance&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">month</span><span class="p">),</span>
<span class="n">x_axis_label</span><span class="o">=</span><span class="s1">&#39;Year&#39;</span><span class="p">,</span>
<span class="n">y_axis_label</span><span class="o">=</span><span class="s1">&#39;Precipitation Chance Percentage&#39;</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s">&quot;{} Average Precipitation Chance&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">month</span><span class="p">),</span>
<span class="n">x_axis_label</span><span class="o">=</span><span class="s">&#39;Year&#39;</span><span class="p">,</span>
<span class="n">y_axis_label</span><span class="o">=</span><span class="s">&#39;Precipitation Chance Percentage&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">line</span><span class="p">(</span><span class="n">years</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">month_averages</span><span class="p">[</span><span class="n">city</span><span class="p">]],</span>
<span class="n">legend</span><span class="o">=</span><span class="n">city</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">city_colors</span><span class="p">[</span><span class="n">city</span><span class="p">])</span>

View File

@ -92,7 +92,7 @@
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">bokeh.plotting</span> <span class="k">import</span> <span class="n">figure</span><span class="p">,</span> <span class="n">output_notebook</span><span class="p">,</span> <span class="n">show</span>
<div class=" highlight hl-ipython3"><pre><span class="kn">from</span> <span class="nn">bokeh.plotting</span> <span class="k">import</span> <span class="n">figure</span><span class="p">,</span> <span class="n">output_notebook</span><span class="p">,</span> <span class="n">show</span>
<span class="kn">from</span> <span class="nn">bokeh.palettes</span> <span class="k">import</span> <span class="n">PuBuGn9</span> <span class="k">as</span> <span class="n">Palette</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
@ -352,7 +352,7 @@
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">city_forecasts</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s1">&#39;city_forecasts.p&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">))</span>
<div class=" highlight hl-ipython3"><pre><span class="n">city_forecasts</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&#39;city_forecasts.p&#39;</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">))</span>
<span class="n">forecast_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="o">.</span><span class="n">from_dict</span><span class="p">(</span><span class="n">city_forecasts</span><span class="p">)</span>
</pre></div>
@ -366,31 +366,31 @@
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">cary_forecast</span> <span class="o">=</span> <span class="n">forecast_df</span><span class="p">[</span><span class="s1">&#39;cary&#39;</span><span class="p">]</span>
<div class=" highlight hl-ipython3"><pre><span class="n">cary_forecast</span> <span class="o">=</span> <span class="n">forecast_df</span><span class="p">[</span><span class="s">&#39;cary&#39;</span><span class="p">]</span>
<span class="n">years</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1990</span><span class="p">,</span> <span class="mi">2016</span><span class="p">)</span>
<span class="n">months</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">12</span><span class="p">)</span>
<span class="n">months_str</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;July&#39;</span><span class="p">,</span> <span class="s1">&#39;August&#39;</span><span class="p">,</span> <span class="s1">&#39;September&#39;</span><span class="p">,</span> <span class="s1">&#39;October&#39;</span><span class="p">,</span> <span class="s1">&#39;November&#39;</span><span class="p">]</span>
<span class="n">months_str</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;July&#39;</span><span class="p">,</span> <span class="s">&#39;August&#39;</span><span class="p">,</span> <span class="s">&#39;September&#39;</span><span class="p">,</span> <span class="s">&#39;October&#39;</span><span class="p">,</span> <span class="s">&#39;November&#39;</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">safe_cover</span><span class="p">(</span><span class="n">frame</span><span class="p">):</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s1">&#39;cloudCover&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s1">&#39;cloudCover&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">frame</span> <span class="ow">and</span> <span class="s">&#39;cloudCover&#39;</span> <span class="ow">in</span> <span class="n">frame</span><span class="p">:</span>
<span class="k">return</span> <span class="n">frame</span><span class="p">[</span><span class="s">&#39;cloudCover&#39;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">NaN</span>
<span class="k">def</span> <span class="nf">monthly_avg_cloudcover</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<span class="n">dates</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">(</span><span class="n">start</span><span class="o"