New blog post on Kaggle competition

master
Bradlee Speice 2016-03-05 11:58:46 -05:00
parent e682eb2a35
commit f60cb5f8ef
31 changed files with 3765 additions and 210 deletions

View File

@ -27,6 +27,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -71,6 +82,8 @@
<div class="container content archive">
<h2><a href="https://bspeice.github.io/archives.html"></a></h2>
<dl class="dl-horizontal">
<dt>Sat 05 March 2016</dt>
<dd><a href="https://bspeice.github.io/predicting-santander-customer-happiness.html">Predicting Santander Customer Happiness</a></dd>
<dt>Fri 26 February 2016</dt>
<dd><a href="https://bspeice.github.io/profitability-using-the-investment-formula.html">Profitability using the Investment Formula</a></dd>
<dt>Wed 03 February 2016</dt>

View File

@ -27,6 +27,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -71,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>Sat 05 March 2016</dt>
<dd><a href="https://bspeice.github.io/predicting-santander-customer-happiness.html">Predicting Santander Customer Happiness</a></dd>
<dt>Fri 26 February 2016</dt>
<dd><a href="https://bspeice.github.io/profitability-using-the-investment-formula.html">Profitability using the Investment Formula</a></dd>
<dt>Wed 03 February 2016</dt>

View File

@ -27,6 +27,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -71,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>Sat 05 March 2016</dt>
<dd><a href="https://bspeice.github.io/predicting-santander-customer-happiness.html">Predicting Santander Customer Happiness</a></dd>
<dt>Fri 26 February 2016</dt>
<dd><a href="https://bspeice.github.io/profitability-using-the-investment-formula.html">Profitability using the Investment Formula</a></dd>
<dt>Wed 03 February 2016</dt>

View File

@ -28,6 +28,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -84,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 class="k">using</span> <span class="n">Gadfly</span>
<div class=" highlight hl-julia"><pre><span></span><span class="k">using</span> <span class="n">Gadfly</span>
</pre></div>
</div>
@ -120,7 +131,7 @@ 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 class="n">S0</span> <span class="o">=</span> <span class="mf">102.2</span>
<div class=" highlight hl-julia"><pre><span></span><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>
@ -176,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 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></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>
<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>
@ -247,7 +258,7 @@ 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 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></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>
<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>
@ -1855,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 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></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>
<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>
@ -1885,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 class="c"># Example term structure taken from:</span>
<div class=" highlight hl-julia"><pre><span></span><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">$</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>
<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>
</pre></div>
</div>
@ -1929,7 +1940,7 @@ 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 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></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>
<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>
@ -3490,7 +3501,7 @@ 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 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></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>
<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>
@ -3506,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: %.2fs&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: </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>
</pre></div>
</div>
@ -3551,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 class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span></span><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>
@ -3595,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 %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="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="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">$</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">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">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">$</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>
<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>
</pre></div>
</div>
@ -3662,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 class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span></span><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>
@ -3709,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 %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="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="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">$</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">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">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">$</span><span class="s">%.2f&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">%.2f</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
</pre></div>
</div>
@ -3762,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 class="n">call_barrier</span> <span class="o">=</span> <span class="n">S0</span>
<div class=" highlight hl-julia"><pre><span></span><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>
@ -3816,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 %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="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="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">$</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">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">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">$</span><span class="s">%.2f&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">%.2f</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">pv</span><span class="p">)</span>
</pre></div>
</div>
@ -3857,6 +3868,20 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML'></script>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'bradleespeice';
var disqus_identifier = 'autocallable-bonds.html';
var disqus_url = 'https://bspeice.github.io/autocallable-bonds.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
</div>
<!-- /Content -->

View File

@ -27,6 +27,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -71,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>Sat 05 March 2016</dt>
<dd><a href="https://bspeice.github.io/predicting-santander-customer-happiness.html">Predicting Santander Customer Happiness</a></dd>
<dt>Fri 26 February 2016</dt>
<dd><a href="https://bspeice.github.io/profitability-using-the-investment-formula.html">Profitability using the Investment Formula</a></dd>
<dt>Wed 03 February 2016</dt>

View File

@ -28,6 +28,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -72,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>Sat 05 March 2016</dt>
<dd><a href="https://bspeice.github.io/predicting-santander-customer-happiness.html">Predicting Santander Customer Happiness</a></dd>
<dt>Fri 26 February 2016</dt>
<dd><a href="https://bspeice.github.io/profitability-using-the-investment-formula.html">Profitability using the Investment Formula</a></dd>
<dt>Wed 03 February 2016</dt>

View File

@ -28,6 +28,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -81,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 class="kn">import</span> <span class="nn">pickle</span>
<div class=" highlight hl-ipython3"><pre><span></span><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>
@ -348,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 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></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>
<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>
@ -362,7 +373,7 @@
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><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></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>
<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>
@ -393,7 +404,7 @@
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<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>
<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>
<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>
@ -407,7 +418,7 @@
<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;{} 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">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="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">:</span>
@ -598,7 +609,7 @@
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<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>
<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>
<span class="k">else</span><span class="p">:</span>
@ -620,7 +631,7 @@
<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;{} 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">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="k">for</span> <span class="n">city</span> <span class="ow">in</span> <span class="n">cities</span><span class="p">:</span>
@ -814,6 +825,20 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML'></script>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'bradleespeice';
var disqus_identifier = 'cloudy-in-seattle.html';
var disqus_url = 'https://bspeice.github.io/cloudy-in-seattle.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
</div>
<!-- /Content -->

View File

@ -28,6 +28,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -81,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 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></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>
<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>
@ -341,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 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></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>
<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>
@ -355,7 +366,7 @@
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<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="s1">&#39;cary&#39;</span><span class="p">]</span>
<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>
<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>
@ -443,7 +454,7 @@
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="k">def</span> <span class="nf">monthly_cloudy_days</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">monthly_cloudy_days</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>
@ -559,7 +570,7 @@
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<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>
<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>
<span class="k">else</span><span class="p">:</span>
@ -640,7 +651,7 @@
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span class="k">def</span> <span class="nf">monthly_rainy_days</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">month</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">monthly_rainy_days</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>
@ -759,7 +770,7 @@
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Generating-the-Forecast-file">Generating the Forecast file<a class="anchor-link" href="#Generating-the-Forecast-file">&#182;</a></h1><p>The following code was generates the file that was used throughout the blog post. Please note that I'm retrieving data for other cities to use in a future blog post, only Cary data was used for this post.</p>
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="nb">reduce</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
@ -834,6 +845,20 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML'></script>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'bradleespeice';
var disqus_identifier = 'complaining-about-the-weather.html';
var disqus_url = 'https://bspeice.github.io/complaining-about-the-weather.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
</div>
<!-- /Content -->

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -28,6 +28,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-74711362-1', 'auto');
ga('send', 'pageview');
</script>
<!-- /Google Analytics -->
</head>
@ -82,7 +93,7 @@
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="If-you-can-see-into-the-future,-that-is.">If you can see into the future, that is.<a class="anchor-link" href="#If-you-can-see-into-the-future,-that-is.">&#182;</a></h3><p>My previous class in Stochastic Calculus covered a lot of interesting topics, and the important one for today
is the <a href="https://en.wikipedia.org/wiki/Gambler's_ruin">Gambler's Ruin</a> problem. If you're interested in some of the theory behind it, also make sure to check out
is the <a href="https://en.wikipedia.org/wiki/Gambler&#39;s_ruin">Gambler's Ruin</a> problem. If you're interested in some of the theory behind it, also make sure to check out
<a href="https://en.wikipedia.org/wiki/Random_walk">random walks</a>. The important bit is that we studied the <a href="https://en.wikipedia.org/wiki/Martingale_%28betting_system%29">Martingale Betting Strategy</a>, which describes for us
a <strong>guaranteed way</strong> to <span style='font-size: x-small'>eventually</span> make money.</p>
<p>The strategy goes like this: You are going to toss a fair coin with a friend. If you guess heads or tails correctly, you get back double the money you bet. If you guess incorrectly, you lose money. How should you bet?</p>
@ -142,7 +153,7 @@ d_n &> \frac{o_n}{c_n - o_n} \sum_{i=1}^{n-1} d_i(1 - \frac{1}{o_i})
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span class="k">using</span> <span class="n">Quandl</span>
<div class=" highlight hl-julia"><pre><span></span><span class="k">using</span> <span class="n">Quandl</span>
<span class="n">api_key</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
<span class="n">daily_investment</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">current_open</span><span class="p">,</span> <span class="n">current_close</span><span class="p">,</span> <span class="n">purchase_history</span><span class="p">,</span> <span class="n">open_history</span><span class="p">)</span>
<span class="c"># We&#39;re not going to safeguard against divide by 0 - that&#39;s the user&#39;s responsibility</span>
@ -172,7 +183,7 @@ d_n &> \frac{o_n}{c_n - o_n} \sum_{i=1}^{n-1} d_i(1 - \frac{1}{o_i})
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span class="n">is_profitable</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">current_price</span><span class="p">,</span> <span class="n">purchase_history</span><span class="p">,</span> <span class="n">open_history</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span></span><span class="n">is_profitable</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">current_price</span><span class="p">,</span> <span class="n">purchase_history</span><span class="p">,</span> <span class="n">open_history</span><span class="p">)</span>
<span class="n">shares</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="n">purchase_history</span> <span class="o">./</span> <span class="n">open_history</span><span class="p">)</span>
<span class="k">return</span> <span class="n">current_price</span><span class="o">*</span><span class="n">shares</span> <span class="o">&gt;</span> <span class="n">sum</span><span class="p">(</span><span class="n">purchase_history</span><span class="p">)</span>
<span class="k">end</span>
@ -207,8 +218,8 @@ d_n &> \frac{o_n}{c_n - o_n} \sum_{i=1}^{n-1} d_i(1 - \frac{1}{o_i})
<span class="n">leverages</span> <span class="o">=</span> <span class="p">[</span><span class="n">sum</span><span class="p">(</span><span class="n">investments</span><span class="p">[</span><span class="mi">1</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="o">=</span><span class="mi">1</span><span class="p">:</span><span class="n">length</span><span class="p">(</span><span class="n">investments</span><span class="p">)]</span>
<span class="n">max_leverage</span> <span class="o">=</span> <span class="n">maximum</span><span class="p">(</span><span class="n">leverages</span><span class="p">)</span> <span class="o">/</span> <span class="n">investments</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;Max leverage: </span><span class="si">$(max_leverage)</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Days invested: </span><span class="si">$</span><span class="s">(length(investments))&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Profit: </span><span class="si">$</span><span class="s">profit&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Days invested: </span><span class="si">$(length(investments))</span><span class="s">&quot;</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="s">&quot;Profit: </span><span class="si">$profit</span><span class="s">&quot;</span><span class="p">)</span>
<span class="k">end</span><span class="p">;</span>
</pre></div>
@ -235,7 +246,7 @@ d_n &> \frac{o_n}{c_n - o_n} \sum_{i=1}^{n-1} d_i(1 - \frac{1}{o_i})
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">&quot;YAHOO/LMT&quot;</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.01</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span></span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">&quot;YAHOO/LMT&quot;</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.01</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">sim_summary</span><span class="p">(</span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
</pre></div>
@ -278,7 +289,7 @@ Profit: 0.6894803101560001
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">&quot;YAHOO/LMT&quot;</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.02</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<div class=" highlight hl-julia"><pre><span></span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span> <span class="o">=</span> <span class="n">simulate</span><span class="p">(</span><span class="s">&quot;YAHOO/LMT&quot;</span><span class="p">,</span> <span class="n">Date</span><span class="p">(</span><span class="mi">2015</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">29</span><span class="p">),</span> <span class="mi">100</span><span class="p">,</span> <span class="mf">1.02</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">sim_summary</span><span class="p">(</span><span class="n">investments</span><span class="p">,</span> <span class="n">profit</span><span class="p">)</span>
</pre></div>
@ -341,6 +352,20 @@ MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\(','\)']]}});
<script async src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML'></script>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'bradleespeice';
var disqus_identifier = 'guaranteed-money-maker.html';
var disqus_url = 'https://bspeice.github.io/guaranteed-money-maker.html';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the comments.</noscript>
</div>
</div>
<!-- /Content -->

View File

@ -27,6 +27,17 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
<