j1-template 2022.5.0.rc3 → 2022.5.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (22) hide show
  1. checksums.yaml +4 -4
  2. data/assets/data/banner.html +2 -2
  3. data/assets/themes/j1/adapter/js/masterslider.js +22 -5
  4. data/lib/j1/version.rb +1 -1
  5. data/lib/starter_web/_config.yml +3 -3
  6. data/lib/starter_web/_data/blocks/banner.yml +1 -1
  7. data/lib/starter_web/_data/modules/defaults/masterslider.yml +1 -1
  8. data/lib/starter_web/_data/modules/masterslider.yml +0 -4
  9. data/lib/starter_web/package.json +1 -1
  10. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_interactive_widgets.html +512 -512
  11. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets.html +629 -629
  12. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_bar_chart.html +95 -95
  13. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_hist_chart.html +93 -93
  14. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_line_chart.html +96 -96
  15. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_question_multiple_choice.html +70 -70
  16. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_question_short_answer.html +156 -156
  17. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_scatter_chart.html +95 -95
  18. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_nbi_widgets_scatter_drag_chart.html +93 -93
  19. data/lib/starter_web/pages/public/jupyter/notebooks/textbooks/j1_odes_in_python.html +955 -955
  20. data/lib/starter_web/utilsrv/_defaults/package.json +1 -1
  21. data/lib/starter_web/utilsrv/package.json +1 -1
  22. metadata +2 -2
@@ -1,955 +1,955 @@
1
- <div class="cell text_cell">
2
- <button class="js-nbinteract-widget">
3
- Loading widgets...
4
- </button>
5
- </div>
6
-
7
-
8
-
9
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
10
- <div class="text_cell_render border-box-sizing rendered_html">
11
- <h2 id="Library-Settings">Library Settings<a class="anchor-link" href="#Library-Settings">&#182;</a></h2>
12
- </div>
13
- </div>
14
- </div>
15
-
16
-
17
-
18
- <div class="
19
- cell border-box-sizing code_cell rendered">
20
- <div class="input">
21
-
22
- <div class="inner_cell">
23
- <div class="input_area">
24
- <div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">warnings</span>
25
- <span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">)</span>
26
-
27
- <span class="c1"># General Purpose</span>
28
- <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
29
- <span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">pyplot</span> <span class="k">as</span> <span class="n">plt</span>
30
- <span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="kn">import</span> <span class="n">odeint</span>
31
-
32
- <span class="c1"># Jupyter Specifics</span>
33
- <span class="kn">from</span> <span class="nn">IPython.display</span> <span class="kn">import</span> <span class="n">HTML</span>
34
- <span class="kn">from</span> <span class="nn">ipywidgets.widgets</span> <span class="kn">import</span> <span class="n">interact</span><span class="p">,</span> <span class="n">IntSlider</span><span class="p">,</span> <span class="n">FloatSlider</span><span class="p">,</span> <span class="n">Layout</span>
35
-
36
- <span class="o">%</span><span class="k">matplotlib</span> inline
37
-
38
- <span class="n">style</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;description_width&#39;</span><span class="p">:</span> <span class="s1">&#39;150px&#39;</span><span class="p">}</span>
39
- <span class="n">slider_layout</span> <span class="o">=</span> <span class="n">Layout</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="s1">&#39;99%&#39;</span><span class="p">)</span>
40
- </pre></div>
41
-
42
- </div>
43
- </div>
44
- </div>
45
-
46
- </div>
47
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
48
- <div class="text_cell_render border-box-sizing rendered_html">
49
- <h2 id="Economy-of-a-Home">Economy of a Home<a class="anchor-link" href="#Economy-of-a-Home">&#182;</a></h2><p>A home economy, a family, has a fixed income at the beginning of some periodic interval; usually a month. There are two kind of spendings:</p>
50
- <ul>
51
- <li>Fixed costs </li>
52
- <li>Extraordinary expenses</li>
53
- </ul>
54
- <p>which are proportional to the money at the moment. Additionally, the family decides to set apart some proportion of the income just for fixed costs. This way, when the capital of the family is <strong>below</strong> this amount, the extraordinary <strong>expenses are 0</strong>.</p>
55
- <p>The model has the following variables:</p>
56
- <ul>
57
- <li><em>initial_salary</em>, the amount of money for the income</li>
58
- <li><em>savings_ratio</em>, Proportion of the income that will be the keep just for fixed costs</li>
59
- <li><em>fixed_costs</em>, The amount of money of fixed costs distributed per day</li>
60
- <li><em>extraordinary_expenses</em>, Proportion of money that will be spend in extraordinary expenses.</li>
61
- <li><em>days</em>, The time interval until the next income</li>
62
- </ul>
63
- <p>Notation for the equation:</p>
64
- <ul>
65
- <li>$x(t)$: Capital over time</li>
66
- <li>$x(0)$ = <em>initial_salary</em></li>
67
- <li>s= savings_ratio * initial_salary</li>
68
- <li>a = <em>fixed_costs</em></li>
69
- <li>b = <em>extraordinary_expenses</em></li>
70
- </ul>
71
-
72
- </div>
73
- </div>
74
- </div>
75
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
76
- <div class="text_cell_render border-box-sizing rendered_html">
77
- <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
78
- $$
79
- \frac{\mathrm{d} x}{\mathrm{d} t} = \left\{\begin{matrix} - a &amp; x \leq s \\ - b (x - s) &amp; x &gt; s \end{matrix}\right.
80
- $$<p>First, we defined a function called main for easier use of interact which will produce the interactive widget. After that, all the constants are calculated, in this case saving_limit which is analogous to s in the equation.</p>
81
-
82
- </div>
83
- </div>
84
- </div>
85
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
86
- <div class="text_cell_render border-box-sizing rendered_html">
87
- <p>A nested function is defined, this function is the differential equation, it should take two parameters and return the value of $\frac{\mathrm{d} x}{\mathrm{d} t}$. The first parameter can be used as the current value of x for a given t. For the numerical integration <em>scipy.integrate.odeint</em> is used and that's why this format is required. Using a nested function has the advantage that its name doesn't conflict with the outer scope of the function containing it and that it has access to all the parameters the outer function has, avoiding unnecessary indirection due to reference.</p>
88
- <p>Then the time variable is defined and initialized, this will be the t steps. We have basically two options here:</p>
89
- <ul>
90
- <li>Define the <strong>start</strong>, the <strong>end</strong> and the <strong>number of steps</strong></li>
91
- <li>Define the <strong>start</strong>, the <strong>end</strong> and the <strong>step</strong></li>
92
- </ul>
93
- <p><strong>Note</strong>: It is important to note that this will determined how smooth the resulting function will be. Although at first it might seem that the integration step (usually denoted with h) is implicitly defined here, that's not the case since, as you can see in the docs of scipy.integrate.odeint, h is determined by the solver in each iteration.</p>
94
- <p>Then the <em>scipy.integrate.odeint</em> function is called with the function defined as a first parameter, the initial conditions as the second and the window time as the third. This function returns the solution array which will be later used for the plotting.</p>
95
-
96
- </div>
97
- </div>
98
- </div>
99
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
100
- <div class="text_cell_render border-box-sizing rendered_html">
101
- <h3 id="Equation---Home-Economy">Equation - Home Economy<a class="anchor-link" href="#Equation---Home-Economy">&#182;</a></h3>
102
- </div>
103
- </div>
104
- </div>
105
-
106
-
107
-
108
- <div class="
109
- cell border-box-sizing code_cell rendered">
110
- <div class="input">
111
-
112
- <div class="inner_cell">
113
- <div class="input_area">
114
- <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">initial_salary</span><span class="p">,</span> <span class="n">savings_ration</span><span class="p">,</span>
115
- <span class="n">extraordinary_expenses</span><span class="p">,</span> <span class="n">fixed_costs</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
116
-
117
- <span class="n">saving_limit</span> <span class="o">=</span> <span class="n">savings_ration</span> <span class="o">*</span> <span class="n">initial_salary</span>
118
-
119
- <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">capital</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
120
- <span class="k">if</span> <span class="n">capital</span> <span class="o">&lt;=</span> <span class="n">saving_limit</span><span class="p">:</span>
121
- <span class="n">out_rate</span> <span class="o">=</span> <span class="mi">0</span>
122
- <span class="k">else</span><span class="p">:</span>
123
- <span class="n">out_rate</span> <span class="o">=</span> <span class="n">extraordinary_expenses</span> <span class="o">*</span> <span class="p">(</span><span class="n">capital</span> <span class="o">-</span> <span class="n">saving_limit</span><span class="p">)</span>
124
- <span class="k">return</span> <span class="o">-</span><span class="n">fixed_costs</span> <span class="o">-</span> <span class="n">out_rate</span>
125
-
126
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span>
127
-
128
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_salary</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
129
-
130
- <span class="c1">#Graphic details</span>
131
- <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
132
-
133
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">saving_limit</span><span class="p">,</span> <span class="n">saving_limit</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Saving Limit&#39;</span><span class="p">)</span>
134
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Capital(t)&#39;</span><span class="p">)</span>
135
-
136
- <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
137
- <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
138
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
139
- <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
140
- <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
141
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
142
- <span class="k">else</span><span class="p">:</span>
143
- <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
144
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
145
-
146
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
147
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
148
-
149
- <span class="n">ax</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">/</span> <span class="mi">20</span><span class="p">))</span>
150
-
151
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
152
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span>
153
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Days&#39;</span><span class="p">)</span>
154
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Capital $&#39;</span><span class="p">)</span>
155
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
156
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
157
-
158
- <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
159
- </pre></div>
160
-
161
- </div>
162
- </div>
163
- </div>
164
-
165
- </div>
166
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
167
- <div class="text_cell_render border-box-sizing rendered_html">
168
- <p>Next, the graphics part, here matplotlib is used and this snipped is specific to this library but I will explain it in order to be self-contained.</p>
169
- <p><strong>Note</strong>: I know there are plenty of plotting libraries, and many even more elegant and powerful than matplotlib but for this use case, where the complexity lies in the differential equation and not in the graphic itself, I believe it is more than enough. For complex graphics such as statistics or big data, I would recommend another library such as bqplot.</p>
170
- <p>First we define a <em>figure</em> and an <em>axes</em> object, explicitly initializing the figure size through <em>figsize</em>. Then a horizontal line with the label <em>Saving Limit</em> is plot at the heigh of the <em>saving_limit</em> and after that the solution is plot with the label <em>Capital(t)</em>.</p>
171
- <p>The if-elif-else structure defines how many sticks there will be in the x-axis and if they should be place horizontal or vertically. Then the said sticks are plotted, the same occurs with the yticks. Then the limits for each axis and the labels for each axis are set and finally, the legend position is defined and the grid is added.</p>
172
- <p>Then when <em>plt.show()</em> is called the figure is generated and showed in the screen.</p>
173
-
174
- </div>
175
- </div>
176
- </div>
177
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
178
- <div class="text_cell_render border-box-sizing rendered_html">
179
- <h3 id="Evaluation---Home-Economy">Evaluation - Home Economy<a class="anchor-link" href="#Evaluation---Home-Economy">&#182;</a></h3><p>Finally, the code for the interaction, here the <em>interact</em> function from <em>ipywidgets</em> is used. A separate slider is used for each parameter with the default parameters set for a nice default visualization, with a description and the style and layout is used from the initialization cells.</p>
180
-
181
- </div>
182
- </div>
183
- </div>
184
-
185
-
186
-
187
- <div class="
188
- cell border-box-sizing code_cell rendered">
189
- <div class="input">
190
-
191
- <div class="inner_cell">
192
- <div class="input_area">
193
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
194
- <span class="n">initial_salary</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">25000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">15000</span><span class="p">,</span>
195
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Salary&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
196
- <span class="n">savings_ration</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span>
197
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Savings Ratio&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
198
- <span class="n">extraordinary_expenses</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.005</span><span class="p">,</span>
199
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Extraordinary Expenses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
200
- <span class="n">fixed_costs</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
201
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Fixed Costs&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
202
- <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
203
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">)</span>
204
- <span class="p">);</span>
205
- </pre></div>
206
-
207
- </div>
208
- </div>
209
- </div>
210
-
211
- <div class="output_wrapper">
212
- <div class="output">
213
-
214
-
215
- <div class="output_area">
216
-
217
-
218
-
219
-
220
-
221
- <div class="output_subarea output_widget_view ">
222
- <button class="js-nbinteract-widget">
223
- Loading widgets...
224
- </button>
225
- </div>
226
-
227
- </div>
228
-
229
- </div>
230
- </div>
231
-
232
- </div>
233
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
234
- <div class="text_cell_render border-box-sizing rendered_html">
235
- <h2 id="Sales-of-Houses-and-Air-Conditionings">Sales of Houses and Air Conditionings<a class="anchor-link" href="#Sales-of-Houses-and-Air-Conditionings">&#182;</a></h2><p>In a city there are houses and air conditionings (AC) for sale and it's known that these are complementary goods, which means that if the sales of one increase the sales of the other will also increase. The number of houses sales is proportional to the number of houses that weren't sold yet and the number of AC sales is proportional to the houses sold that doesn't yet have an AC.</p>
236
- <p>The model has the following variables:</p>
237
- <ul>
238
- <li>_initial<em>houses</em>: Number of Houses initially sold, could be 0</li>
239
- <li>_initial<em>ac</em>: Number of air conditionings sold, could be 0 and
240
- should be less than initial_houses</li>
241
- <li>_avg_time<em>house</em>: Average number of days to sell a <strong>house</strong></li>
242
- <li>_avg_time<em>ac</em>: Average number of days to sell an <strong>AC</strong></li>
243
- <li>_total<em>houses</em>: Total number of <strong>Houses for sale</strong></li>
244
- <li><em>days</em>: The time interval until the <strong>next income</strong></li>
245
- </ul>
246
- <p>Notation for the equation:</p>
247
- <ul>
248
- <li>$x(t)$: Number of sold Houses</li>
249
- <li>$y(t)$: Number of sold Air Conditionings</li>
250
- <li>$x(0)$= _initial<em>houses</em></li>
251
- <li>$y(0)$= _initial<em>ac</em></li>
252
- <li>h = _total<em>houses</em></li>
253
- <li>a = _avg_time<em>house</em></li>
254
- <li>b = _avg_time<em>ac</em></li>
255
- </ul>
256
- <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
257
-
258
- </div>
259
- </div>
260
- </div>
261
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
262
- <div class="text_cell_render border-box-sizing rendered_html">
263
- $$
264
- \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =\frac{1}{a} (h - x) \\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =\frac{1}{b} (x - y) \end{matrix}\right.
265
- $$
266
- </div>
267
- </div>
268
- </div>
269
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
270
- <div class="text_cell_render border-box-sizing rendered_html">
271
- <h3 id="Equation---Homes-and-ACs">Equation - Homes and ACs<a class="anchor-link" href="#Equation---Homes-and-ACs">&#182;</a></h3>
272
- </div>
273
- </div>
274
- </div>
275
-
276
-
277
-
278
- <div class="
279
- cell border-box-sizing code_cell rendered">
280
- <div class="input">
281
-
282
- <div class="inner_cell">
283
- <div class="input_area">
284
- <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">initial_houses</span><span class="p">,</span> <span class="n">initial_ac</span><span class="p">,</span> <span class="n">avg_time_house</span><span class="p">,</span> <span class="n">avg_time_ac</span><span class="p">,</span> <span class="n">total_houses</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
285
-
286
- <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
287
- <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">s</span>
288
- <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
289
- <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">avg_time_house</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">total_houses</span> <span class="o">-</span> <span class="n">x</span><span class="p">),</span> <span class="c1"># dx/dt: Change in the House sales</span>
290
- <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">avg_time_ac</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">)</span> <span class="c1"># dx/dt: Change in the AC sales</span>
291
- <span class="p">]</span>
292
- <span class="k">return</span> <span class="n">dydt</span>
293
-
294
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
295
- <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_houses</span><span class="p">,</span> <span class="n">initial_ac</span><span class="p">]</span>
296
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
297
-
298
- <span class="c1">#Graphic details</span>
299
- <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
300
-
301
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Houses(t)&#39;</span><span class="p">)</span>
302
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Air Conditionings(t)&#39;</span><span class="p">)</span>
303
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">total_houses</span><span class="p">,</span> <span class="n">total_houses</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Total Houses&#39;</span><span class="p">)</span>
304
-
305
- <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
306
- <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
307
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
308
- <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
309
- <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
310
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
311
- <span class="k">else</span><span class="p">:</span>
312
- <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
313
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
314
-
315
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
316
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
317
-
318
- <span class="n">ax</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">/</span> <span class="mi">20</span><span class="p">))</span>
319
-
320
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
321
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span>
322
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Months&#39;</span><span class="p">)</span>
323
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Units&#39;</span><span class="p">)</span>
324
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
325
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
326
-
327
- <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
328
- </pre></div>
329
-
330
- </div>
331
- </div>
332
- </div>
333
-
334
- </div>
335
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
336
- <div class="text_cell_render border-box-sizing rendered_html">
337
- <p>In this scenario and the followings the code will be explain where it differs from the first example to avoid unnecesary repetition.</p>
338
- <p>In this case the constants <em>avg_time_house</em> and <em>avg_time_ac</em> are used directly inside the function since it is much clearer this way. To define a system of ODEs, first the initial condition should be a list, each element of the list represents the initial condition for each of the variables. The nested function also changes a bit, in order to work, the first parameter would also be a list so it is necessary to unpack it into the variables and then a variable <em>dydt</em> is used to represent the system where each element is the left side of each equation in canonical form.</p>
339
- <p>Finally the <em>dydt</em> variable is returned. The definition of <em>time</em> is the same as previously seen and the solution variable is now a nested array containing the solution for both variables. This is specially useful when plotting.</p>
340
-
341
- </div>
342
- </div>
343
- </div>
344
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
345
- <div class="text_cell_render border-box-sizing rendered_html">
346
- <h3 id="Evaluation---Homes-and-ACs">Evaluation - Homes and ACs<a class="anchor-link" href="#Evaluation---Homes-and-ACs">&#182;</a></h3>
347
- </div>
348
- </div>
349
- </div>
350
-
351
-
352
-
353
- <div class="
354
- cell border-box-sizing code_cell rendered">
355
- <div class="input">
356
-
357
- <div class="inner_cell">
358
- <div class="input_area">
359
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
360
- <span class="n">initial_houses</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
361
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial sold Houses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
362
- <span class="n">initial_ac</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
363
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial sold AC&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
364
- <span class="n">total_houses</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
365
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Houses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
366
- <span class="n">avg_time_house</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
367
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Time for House&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
368
- <span class="n">avg_time_ac</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
369
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Time for AC&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
370
- <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">360</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
371
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
372
- <span class="p">);</span>
373
- </pre></div>
374
-
375
- </div>
376
- </div>
377
- </div>
378
-
379
- <div class="output_wrapper">
380
- <div class="output">
381
-
382
-
383
- <div class="output_area">
384
-
385
-
386
-
387
-
388
-
389
- <div class="output_subarea output_widget_view ">
390
- <button class="js-nbinteract-widget">
391
- Loading widgets...
392
- </button>
393
- </div>
394
-
395
- </div>
396
-
397
- </div>
398
- </div>
399
-
400
- </div>
401
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
402
- <div class="text_cell_render border-box-sizing rendered_html">
403
- <h2 id="Stock-Control">Stock Control<a class="anchor-link" href="#Stock-Control">&#182;</a></h2><p>A company wants a desired stock, they started with an initial stock and they work with a provider which sends deliveries goods through sales orders. The company asks the provider each day a sales order proportional to the missing or surplus quantity to the desired stock and the market demand.</p>
404
- <p>The provider deliver the goods according to the sales order with a constant delay. Finally, the market demand is consider constant. The company wants to know how their stock and their request to the provider will behave and they will try to avoid alternating behavior, a oscillation in stock.</p>
405
- <p>The model has the following variables:</p>
406
- <ul>
407
- <li>_desired<em>stock</em>: Amount of stock the company wants to have.</li>
408
- <li>_initial<em>stock</em>: The initial amount of stock of the company.</li>
409
- <li>_initial<em>orders</em>: initial amount of goods asked in sales orders.</li>
410
- <li>_stock<em>control</em>: Proportion of the missing/surplus stock to ask to the provider.</li>
411
- <li>_market<em>demand</em>: The daily demand of the market</li>
412
- <li>_provider<em>delay</em>: The number of days of the provider delay.</li>
413
- <li><em>days</em>: The total number of days the model will analyse.</li>
414
- </ul>
415
- <p>Notation for the equation:</p>
416
- <ul>
417
- <li>$x(t)$: Number of goods in stock</li>
418
- <li>$y(t)$: Number of pending sales order sent to the provider</li>
419
- <li>$x(0)$ = _initial<em>stock</em></li>
420
- <li>$y(0)$ = _initial<em>orders</em></li>
421
- <li>s = _desired<em>stock</em></li>
422
- <li>d = _market<em>demand</em></li>
423
- <li>k = _stock<em>control</em></li>
424
- <li>p = _provider<em>delay</em></li>
425
- </ul>
426
-
427
- </div>
428
- </div>
429
- </div>
430
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
431
- <div class="text_cell_render border-box-sizing rendered_html">
432
- <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
433
- $$
434
- \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; \frac{1}{p} y - d \\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; - \frac{1}{p} y + d + k ( s - x ) \end{matrix}\right.
435
- $$<p>This time the code is only changed in the variables names and the equations but it is very similar to the previous example, the particularity of this model doesn't lie in the code but rather in the behavior, since it can show the differences between an <strong>oscillation</strong> behavior and a damped one. Going through all kinds of possibilities such as underdamped, overdamped, critically damped and undamped. I encourage you to experiment with the parameters in the widget.</p>
436
-
437
- </div>
438
- </div>
439
- </div>
440
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
441
- <div class="text_cell_render border-box-sizing rendered_html">
442
- <h3 id="Equation---Stock-Control">Equation - Stock Control<a class="anchor-link" href="#Equation---Stock-Control">&#182;</a></h3>
443
- </div>
444
- </div>
445
- </div>
446
-
447
-
448
-
449
- <div class="
450
- cell border-box-sizing code_cell rendered">
451
- <div class="input">
452
-
453
- <div class="inner_cell">
454
- <div class="input_area">
455
- <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">desired_stock</span><span class="p">,</span> <span class="n">initial_stock</span><span class="p">,</span>
456
- <span class="n">initial_request</span><span class="p">,</span> <span class="n">stock_control</span><span class="p">,</span> <span class="n">market_demand</span><span class="p">,</span>
457
- <span class="n">provider_delay</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
458
-
459
- <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">v0</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
460
- <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">v0</span>
461
-
462
- <span class="c1"># dx/dt -&gt; Change in Stock</span>
463
- <span class="c1"># dy/dt -&gt; Change in Requests</span>
464
- <span class="c1">#</span>
465
- <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
466
- <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">provider_delay</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="o">-</span> <span class="n">market_demand</span><span class="p">,</span>
467
- <span class="o">-</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">provider_delay</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="o">+</span> <span class="n">market_demand</span> <span class="o">+</span> <span class="n">stock_control</span> <span class="o">*</span> <span class="p">(</span><span class="n">desired_stock</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
468
- <span class="p">]</span>
469
- <span class="k">return</span> <span class="n">dydt</span>
470
-
471
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
472
- <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_stock</span><span class="p">,</span> <span class="n">initial_request</span><span class="p">]</span>
473
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
474
-
475
- <span class="c1"># Graphic details</span>
476
- <span class="c1">#</span>
477
- <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
478
-
479
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Stock(t)&#39;</span><span class="p">)</span>
480
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Requests(t)&#39;</span><span class="p">)</span>
481
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">desired_stock</span><span class="p">,</span> <span class="n">desired_stock</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Desired Stock&#39;</span><span class="p">)</span>
482
-
483
- <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
484
- <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
485
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
486
- <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
487
- <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
488
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
489
- <span class="k">else</span><span class="p">:</span>
490
- <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
491
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
492
-
493
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
494
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
495
-
496
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
497
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]),</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]))</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
498
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Days&#39;</span><span class="p">)</span>
499
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Units&#39;</span><span class="p">)</span>
500
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
501
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
502
-
503
- <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
504
-
505
- </pre></div>
506
-
507
- </div>
508
- </div>
509
- </div>
510
-
511
- </div>
512
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
513
- <div class="text_cell_render border-box-sizing rendered_html">
514
- <h3 id="Evaluation---Stock-Control">Evaluation - Stock Control<a class="anchor-link" href="#Evaluation---Stock-Control">&#182;</a></h3>
515
- </div>
516
- </div>
517
- </div>
518
-
519
-
520
-
521
- <div class="
522
- cell border-box-sizing code_cell rendered">
523
- <div class="input">
524
-
525
- <div class="inner_cell">
526
- <div class="input_area">
527
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
528
- <span class="n">desired_stock</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
529
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Desired Stock&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
530
- <span class="n">initial_stock</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span>
531
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Stock&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
532
- <span class="n">initial_request</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">14</span><span class="p">,</span>
533
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Requests&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
534
- <span class="n">stock_control</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">1.5</span><span class="p">,</span>
535
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Stock Control&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
536
- <span class="n">market_demand</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
537
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Market Demand&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
538
- <span class="n">provider_delay</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
539
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Provider Delay&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
540
- <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">360</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
541
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
542
- <span class="p">);</span>
543
- </pre></div>
544
-
545
- </div>
546
- </div>
547
- </div>
548
-
549
- <div class="output_wrapper">
550
- <div class="output">
551
-
552
-
553
- <div class="output_area">
554
-
555
-
556
-
557
-
558
-
559
- <div class="output_subarea output_widget_view ">
560
- <button class="js-nbinteract-widget">
561
- Loading widgets...
562
- </button>
563
- </div>
564
-
565
- </div>
566
-
567
- </div>
568
- </div>
569
-
570
- </div>
571
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
572
- <div class="text_cell_render border-box-sizing rendered_html">
573
- <h2 id="Useful-Life">Useful Life<a class="anchor-link" href="#Useful-Life">&#182;</a></h2><p>A patient takes a certain drug with a recipe, the recipe says how many milligrams the patient should have per intake, how often the intakes should be and how many. Additionally the drug itself provides information about its useful life which helps to determine the optimal interval between intakes. It's also known that the useful life of the drug follows a natural decay.</p>
574
- <p>The model has the following variables:</p>
575
- <ul>
576
- <li>_useful<em>life</em>: Duration of the useful life of the drug in hours</li>
577
- <li>_intake<em>mg</em>: Amount of milligrams per intake.</li>
578
- <li>_intake<em>interval</em>: Time interval in hours between intakes.</li>
579
- <li>_intake<em>number</em>: Number of intakes.</li>
580
- <li><em>hours</em>: The total number of hours the model will analyse.</li>
581
- </ul>
582
- <p>Notation for the equation:</p>
583
- <ul>
584
- <li>$x(t)$: Milligrams of drug in the body</li>
585
- <li>$x(0)$: _intake<em>mg</em></li>
586
- <li>a = _useful<em>life</em></li>
587
- </ul>
588
- <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
589
- $$
590
- \frac{\mathrm{d} x}{\mathrm{d} t} = - \frac{1}{a} ln(2) y
591
- $$
592
- </div>
593
- </div>
594
- </div>
595
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
596
- <div class="text_cell_render border-box-sizing rendered_html">
597
- <p><strong>Note</strong>: the expression $ln(2)$ is part of the <strong>natural decay formula</strong>.</p>
598
-
599
- </div>
600
- </div>
601
- </div>
602
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
603
- <div class="text_cell_render border-box-sizing rendered_html">
604
- <h3 id="Equation---Useful-Life">Equation - Useful Life<a class="anchor-link" href="#Equation---Useful-Life">&#182;</a></h3>
605
- </div>
606
- </div>
607
- </div>
608
-
609
-
610
-
611
- <div class="
612
- cell border-box-sizing code_cell rendered">
613
- <div class="input">
614
-
615
- <div class="inner_cell">
616
- <div class="input_area">
617
- <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">useful_life</span><span class="p">,</span> <span class="n">intake_mg</span><span class="p">,</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="n">intake_number</span><span class="p">,</span> <span class="n">hours</span><span class="p">):</span>
618
-
619
- <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
620
- <span class="k">return</span> <span class="o">-</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">useful_life</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="c1"># dy/dt -&gt; Change of mg </span>
621
-
622
- <span class="n">intake_hours</span> <span class="o">=</span> <span class="p">[</span><span class="n">intake_interval</span> <span class="o">*</span> <span class="n">i</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="n">intake_number</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>
623
- <span class="n">initial_condition</span> <span class="o">=</span> <span class="n">intake_mg</span>
624
- <span class="n">times</span> <span class="o">=</span> <span class="p">[]</span>
625
- <span class="n">solutions</span> <span class="o">=</span> <span class="p">[]</span>
626
-
627
- <span class="k">for</span> <span class="n">intake_time</span> <span class="ow">in</span> <span class="n">intake_hours</span><span class="p">:</span>
628
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">intake_time</span><span class="p">,</span> <span class="n">intake_time</span> <span class="o">+</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
629
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_condition</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
630
-
631
- <span class="n">initial_condition</span> <span class="o">=</span> <span class="n">solution</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">intake_mg</span>
632
-
633
- <span class="n">times</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
634
- <span class="n">solutions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span>
635
-
636
-
637
- <span class="n">intake_time</span> <span class="o">=</span> <span class="n">intake_hours</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">intake_interval</span>
638
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">intake_time</span><span class="p">,</span> <span class="n">intake_time</span> <span class="o">+</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
639
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_condition</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
640
- <span class="n">times</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
641
- <span class="n">solutions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span>
642
-
643
- <span class="c1">#Graphic details</span>
644
- <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
645
-
646
- <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">times</span><span class="p">,</span> <span class="n">solutions</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Concentration in the Body(t)&#39;</span><span class="p">)</span>
647
-
648
- <span class="k">if</span> <span class="n">hours</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
649
- <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
650
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
651
- <span class="k">elif</span> <span class="n">hours</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
652
- <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
653
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
654
- <span class="k">else</span><span class="p">:</span>
655
- <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
656
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
657
-
658
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
659
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
660
-
661
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span><span class="p">])</span>
662
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solutions</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
663
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Hours&#39;</span><span class="p">)</span>
664
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Concentration&#39;</span><span class="p">)</span>
665
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
666
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
667
-
668
- <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
669
- </pre></div>
670
-
671
- </div>
672
- </div>
673
- </div>
674
-
675
- </div>
676
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
677
- <div class="text_cell_render border-box-sizing rendered_html">
678
- <p>This model doesn't introduces complexity in the ODE but rather in the way one can model the successive intakes. It is usually helpful to keep this 'iteration logic' independent from the ODE itself, that's why no particular changes are needed in the equations.</p>
679
- <p>There are many ways for achieving this but the one I used is the following:</p>
680
- <ol>
681
- <li>Create a list with all the intake hours</li>
682
- <li>Create an empty list, times, for the times</li>
683
- <li>Create an empty list, solutions for the solutions</li>
684
- <li>Iterate through that list<ul>
685
- <li>Create a time array starting in the time iterated and ended in the time
686
- iterated + the intake_interval</li>
687
- <li>Solve the ODE with the initial_condition and the previously created time</li>
688
- <li>Update the value of the initial_conditions with the final solution</li>
689
- <li>Append the solution list to the end of the solutions list</li>
690
- <li>Append the time list to the end of the times list</li>
691
- </ul>
692
- </li>
693
- <li>Create a time array starting from the last the last time of intakes
694
- to 10 times the intake_interval</li>
695
- <li>Solve the ODE again with a longer interval to see how it behaves
696
- after the iterations</li>
697
- <li>Append the solution list to the end of the solutions list</li>
698
- <li>Append the time list to the end of the times list</li>
699
- <li>Proceed to Plotting</li>
700
- </ol>
701
- <p>This approach consist in solving several ODEs, each of which receives the final solution of the previous as its initial conditions.</p>
702
-
703
- </div>
704
- </div>
705
- </div>
706
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
707
- <div class="text_cell_render border-box-sizing rendered_html">
708
- <h3 id="Evaluation---Useful-Life">Evaluation - Useful Life<a class="anchor-link" href="#Evaluation---Useful-Life">&#182;</a></h3>
709
- </div>
710
- </div>
711
- </div>
712
-
713
-
714
-
715
- <div class="
716
- cell border-box-sizing code_cell rendered">
717
- <div class="input">
718
-
719
- <div class="inner_cell">
720
- <div class="input_area">
721
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
722
- <span class="n">useful_life</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">3.8</span><span class="p">,</span>
723
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Useful Life&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
724
- <span class="n">intake_mg</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
725
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Miligrams per Intake&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
726
- <span class="n">intake_interval</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
727
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Hours between Intakes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
728
- <span class="n">intake_number</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
729
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Number of Intakes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
730
- <span class="n">hours</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">240</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span>
731
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total number of Hours&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
732
- <span class="p">);</span>
733
- </pre></div>
734
-
735
- </div>
736
- </div>
737
- </div>
738
-
739
- <div class="output_wrapper">
740
- <div class="output">
741
-
742
-
743
- <div class="output_area">
744
-
745
-
746
-
747
-
748
-
749
- <div class="output_subarea output_widget_view ">
750
- <button class="js-nbinteract-widget">
751
- Loading widgets...
752
- </button>
753
- </div>
754
-
755
- </div>
756
-
757
- </div>
758
- </div>
759
-
760
- </div>
761
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
762
- <div class="text_cell_render border-box-sizing rendered_html">
763
- <h2 id="Foxes-and-Rabbits">Foxes and Rabbits<a class="anchor-link" href="#Foxes-and-Rabbits">&#182;</a></h2><p>This is the model known as <em>predator–prey</em> or <em>Lotka–Volterra</em> equations. There are two species, a predator and a prey, like foxes and rabbits. Each has a certain birthrate and a deathrate. The birthrate of the prey is proportional to its current population and the birthrate of the predator is proportional to both its population and the prey population. The deathrate of the prey is proportional to both its population and the predator population and the deathrate of the predator is proportional to its population.</p>
764
- <p>The model has the following variables:</p>
765
- <ul>
766
- <li><em>rabbits_birthrate</em>: Rabbits birthrate</li>
767
- <li><em>rabbits_deathrate</em>: Rabbits deathrate</li>
768
- <li><em>foxes_birthrate</em>: Foxes birthrate</li>
769
- <li><em>initial_rabbits</em>: Initial rabbits</li>
770
- <li><em>initial_foxes</em>: Initial foxes</li>
771
- <li><em>days</em>: The total number of days the model will analyse</li>
772
- </ul>
773
- <p>Notation for the equation:</p>
774
- <ul>
775
- <li>$x(t)$: Population of Rabbits over time</li>
776
- <li>$y(t)$: Population of Foxes over time</li>
777
- <li>$x(0)$ = <em>initial_rabbits</em></li>
778
- <li>$y(0)$ = <em>initial_foxes</em></li>
779
- <li>a = <em>rabbits_birthrate</em></li>
780
- <li>b = <em>rabbits_deathrate</em></li>
781
- <li>c = <em>foxes_birthrate</em></li>
782
- <li>d = <em>foxes_deathrate</em></li>
783
- </ul>
784
-
785
- </div>
786
- </div>
787
- </div>
788
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
789
- <div class="text_cell_render border-box-sizing rendered_html">
790
- <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
791
- $$
792
- \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; a x &amp;-&amp; bxy\\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; cxy &amp;-&amp; dy \end{matrix}\right.
793
- $$
794
- </div>
795
- </div>
796
- </div>
797
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
798
- <div class="text_cell_render border-box-sizing rendered_html">
799
- <p>Or more concisely:</p>
800
- $$
801
- \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; x (a - by)\\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; y (cx - d) \end{matrix}\right.
802
- $$
803
- </div>
804
- </div>
805
- </div>
806
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
807
- <div class="text_cell_render border-box-sizing rendered_html">
808
- <p><strong>Note</strong>: The <em>Lotka-Volterra equations</em> are a pair of first-order nonlinear differential
809
- equations, frequently used to describe the dynamics of biological systems in which
810
- two species interact, one as a predator and the other as prey.</p>
811
-
812
- </div>
813
- </div>
814
- </div>
815
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
816
- <div class="text_cell_render border-box-sizing rendered_html">
817
- <h3 id="Equation---Foxes-and-Rabbits">Equation - Foxes and Rabbits<a class="anchor-link" href="#Equation---Foxes-and-Rabbits">&#182;</a></h3>
818
- </div>
819
- </div>
820
- </div>
821
-
822
-
823
-
824
- <div class="
825
- cell border-box-sizing code_cell rendered">
826
- <div class="input">
827
-
828
- <div class="inner_cell">
829
- <div class="input_area">
830
- <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">rabbits_birthrate</span><span class="p">,</span> <span class="n">rabbits_deathrate</span><span class="p">,</span>
831
- <span class="n">foxes_birthrate</span><span class="p">,</span> <span class="n">foxes_deathrate</span><span class="p">,</span>
832
- <span class="n">initial_rabbits</span><span class="p">,</span> <span class="n">initial_foxes</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
833
-
834
- <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
835
- <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">s</span>
836
- <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
837
- <span class="n">rabbits_birthrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="n">rabbits_deathrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">,</span> <span class="c1"># dx/dy -&gt; Change in Rabbits</span>
838
- <span class="n">foxes_birthrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span> <span class="o">-</span> <span class="n">foxes_deathrate</span> <span class="o">*</span> <span class="n">y</span> <span class="c1"># dy/dt -&gt; Change in Foxes</span>
839
- <span class="p">]</span>
840
-
841
- <span class="k">return</span> <span class="n">dydt</span>
842
-
843
- <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
844
- <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_rabbits</span><span class="p">,</span> <span class="n">initial_foxes</span><span class="p">]</span>
845
- <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
846
-
847
- <span class="c1"># Graphic details</span>
848
- <span class="c1">#</span>
849
- <span class="n">fig</span><span class="p">,</span> <span class="n">axes</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
850
-
851
- <span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
852
-
853
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Rabbits(t)&#39;</span><span class="p">)</span>
854
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Foxes(t)&#39;</span><span class="p">)</span>
855
-
856
- <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">30</span><span class="p">:</span>
857
- <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
858
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
859
- <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">150</span><span class="p">:</span>
860
- <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
861
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
862
- <span class="k">else</span><span class="p">:</span>
863
- <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
864
- <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
865
-
866
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
867
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
868
-
869
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
870
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]),</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]))</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
871
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Time&#39;</span><span class="p">)</span>
872
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Population&#39;</span><span class="p">)</span>
873
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
874
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
875
-
876
- <span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
877
-
878
- <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Foxes vs Rabbits&#39;</span><span class="p">)</span>
879
-
880
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
881
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
882
- <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Rabbits&#39;</span><span class="p">)</span>
883
- <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Foxes&#39;</span><span class="p">)</span>
884
- <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
885
- <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
886
-
887
- <span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span>
888
- <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
889
- </pre></div>
890
-
891
- </div>
892
- </div>
893
- </div>
894
-
895
- </div>
896
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
897
- <div class="text_cell_render border-box-sizing rendered_html">
898
- <h3 id="Evaluation---Foxes-and-Rabbits">Evaluation - Foxes and Rabbits<a class="anchor-link" href="#Evaluation---Foxes-and-Rabbits">&#182;</a></h3>
899
- </div>
900
- </div>
901
- </div>
902
-
903
-
904
-
905
- <div class="
906
- cell border-box-sizing code_cell rendered">
907
- <div class="input">
908
-
909
- <div class="inner_cell">
910
- <div class="input_area">
911
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
912
- <span class="n">rabbits_birthrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
913
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Birth Rate of Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
914
- <span class="n">rabbits_deathrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
915
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Death Rate of Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
916
- <span class="n">foxes_birthrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
917
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Birth Rate of Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
918
- <span class="n">foxes_deathrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
919
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Death Rate of Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
920
- <span class="n">initial_rabbits</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
921
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
922
- <span class="n">initial_foxes</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
923
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
924
- <span class="n">days</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span><span class="nb">max</span><span class="o">=</span><span class="mi">365</span> <span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span>
925
- <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
926
- <span class="p">);</span>
927
- </pre></div>
928
-
929
- </div>
930
- </div>
931
- </div>
932
-
933
- <div class="output_wrapper">
934
- <div class="output">
935
-
936
-
937
- <div class="output_area">
938
-
939
-
940
-
941
-
942
-
943
- <div class="output_subarea output_widget_view ">
944
- <button class="js-nbinteract-widget">
945
- Loading widgets...
946
- </button>
947
- </div>
948
-
949
- </div>
950
-
951
- </div>
952
- </div>
953
-
954
- </div>
955
-
1
+ <div class="cell text_cell">
2
+ <button class="js-nbinteract-widget">
3
+ Loading widgets...
4
+ </button>
5
+ </div>
6
+
7
+
8
+
9
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
10
+ <div class="text_cell_render border-box-sizing rendered_html">
11
+ <h2 id="Library-Settings">Library Settings<a class="anchor-link" href="#Library-Settings">&#182;</a></h2>
12
+ </div>
13
+ </div>
14
+ </div>
15
+
16
+
17
+
18
+ <div class="
19
+ cell border-box-sizing code_cell rendered">
20
+ <div class="input">
21
+
22
+ <div class="inner_cell">
23
+ <div class="input_area">
24
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">warnings</span>
25
+ <span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">)</span>
26
+
27
+ <span class="c1"># General Purpose</span>
28
+ <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
29
+ <span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">pyplot</span> <span class="k">as</span> <span class="n">plt</span>
30
+ <span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="kn">import</span> <span class="n">odeint</span>
31
+
32
+ <span class="c1"># Jupyter Specifics</span>
33
+ <span class="kn">from</span> <span class="nn">IPython.display</span> <span class="kn">import</span> <span class="n">HTML</span>
34
+ <span class="kn">from</span> <span class="nn">ipywidgets.widgets</span> <span class="kn">import</span> <span class="n">interact</span><span class="p">,</span> <span class="n">IntSlider</span><span class="p">,</span> <span class="n">FloatSlider</span><span class="p">,</span> <span class="n">Layout</span>
35
+
36
+ <span class="o">%</span><span class="k">matplotlib</span> inline
37
+
38
+ <span class="n">style</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;description_width&#39;</span><span class="p">:</span> <span class="s1">&#39;150px&#39;</span><span class="p">}</span>
39
+ <span class="n">slider_layout</span> <span class="o">=</span> <span class="n">Layout</span><span class="p">(</span><span class="n">width</span><span class="o">=</span><span class="s1">&#39;99%&#39;</span><span class="p">)</span>
40
+ </pre></div>
41
+
42
+ </div>
43
+ </div>
44
+ </div>
45
+
46
+ </div>
47
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
48
+ <div class="text_cell_render border-box-sizing rendered_html">
49
+ <h2 id="Economy-of-a-Home">Economy of a Home<a class="anchor-link" href="#Economy-of-a-Home">&#182;</a></h2><p>A home economy, a family, has a fixed income at the beginning of some periodic interval; usually a month. There are two kind of spendings:</p>
50
+ <ul>
51
+ <li>Fixed costs </li>
52
+ <li>Extraordinary expenses</li>
53
+ </ul>
54
+ <p>which are proportional to the money at the moment. Additionally, the family decides to set apart some proportion of the income just for fixed costs. This way, when the capital of the family is <strong>below</strong> this amount, the extraordinary <strong>expenses are 0</strong>.</p>
55
+ <p>The model has the following variables:</p>
56
+ <ul>
57
+ <li><em>initial_salary</em>, the amount of money for the income</li>
58
+ <li><em>savings_ratio</em>, Proportion of the income that will be the keep just for fixed costs</li>
59
+ <li><em>fixed_costs</em>, The amount of money of fixed costs distributed per day</li>
60
+ <li><em>extraordinary_expenses</em>, Proportion of money that will be spend in extraordinary expenses.</li>
61
+ <li><em>days</em>, The time interval until the next income</li>
62
+ </ul>
63
+ <p>Notation for the equation:</p>
64
+ <ul>
65
+ <li>$x(t)$: Capital over time</li>
66
+ <li>$x(0)$ = <em>initial_salary</em></li>
67
+ <li>s= savings_ratio * initial_salary</li>
68
+ <li>a = <em>fixed_costs</em></li>
69
+ <li>b = <em>extraordinary_expenses</em></li>
70
+ </ul>
71
+
72
+ </div>
73
+ </div>
74
+ </div>
75
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
76
+ <div class="text_cell_render border-box-sizing rendered_html">
77
+ <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
78
+ $$
79
+ \frac{\mathrm{d} x}{\mathrm{d} t} = \left\{\begin{matrix} - a &amp; x \leq s \\ - b (x - s) &amp; x &gt; s \end{matrix}\right.
80
+ $$<p>First, we defined a function called main for easier use of interact which will produce the interactive widget. After that, all the constants are calculated, in this case saving_limit which is analogous to s in the equation.</p>
81
+
82
+ </div>
83
+ </div>
84
+ </div>
85
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
86
+ <div class="text_cell_render border-box-sizing rendered_html">
87
+ <p>A nested function is defined, this function is the differential equation, it should take two parameters and return the value of $\frac{\mathrm{d} x}{\mathrm{d} t}$. The first parameter can be used as the current value of x for a given t. For the numerical integration <em>scipy.integrate.odeint</em> is used and that's why this format is required. Using a nested function has the advantage that its name doesn't conflict with the outer scope of the function containing it and that it has access to all the parameters the outer function has, avoiding unnecessary indirection due to reference.</p>
88
+ <p>Then the time variable is defined and initialized, this will be the t steps. We have basically two options here:</p>
89
+ <ul>
90
+ <li>Define the <strong>start</strong>, the <strong>end</strong> and the <strong>number of steps</strong></li>
91
+ <li>Define the <strong>start</strong>, the <strong>end</strong> and the <strong>step</strong></li>
92
+ </ul>
93
+ <p><strong>Note</strong>: It is important to note that this will determined how smooth the resulting function will be. Although at first it might seem that the integration step (usually denoted with h) is implicitly defined here, that's not the case since, as you can see in the docs of scipy.integrate.odeint, h is determined by the solver in each iteration.</p>
94
+ <p>Then the <em>scipy.integrate.odeint</em> function is called with the function defined as a first parameter, the initial conditions as the second and the window time as the third. This function returns the solution array which will be later used for the plotting.</p>
95
+
96
+ </div>
97
+ </div>
98
+ </div>
99
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
100
+ <div class="text_cell_render border-box-sizing rendered_html">
101
+ <h3 id="Equation---Home-Economy">Equation - Home Economy<a class="anchor-link" href="#Equation---Home-Economy">&#182;</a></h3>
102
+ </div>
103
+ </div>
104
+ </div>
105
+
106
+
107
+
108
+ <div class="
109
+ cell border-box-sizing code_cell rendered">
110
+ <div class="input">
111
+
112
+ <div class="inner_cell">
113
+ <div class="input_area">
114
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">initial_salary</span><span class="p">,</span> <span class="n">savings_ration</span><span class="p">,</span>
115
+ <span class="n">extraordinary_expenses</span><span class="p">,</span> <span class="n">fixed_costs</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
116
+
117
+ <span class="n">saving_limit</span> <span class="o">=</span> <span class="n">savings_ration</span> <span class="o">*</span> <span class="n">initial_salary</span>
118
+
119
+ <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">capital</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
120
+ <span class="k">if</span> <span class="n">capital</span> <span class="o">&lt;=</span> <span class="n">saving_limit</span><span class="p">:</span>
121
+ <span class="n">out_rate</span> <span class="o">=</span> <span class="mi">0</span>
122
+ <span class="k">else</span><span class="p">:</span>
123
+ <span class="n">out_rate</span> <span class="o">=</span> <span class="n">extraordinary_expenses</span> <span class="o">*</span> <span class="p">(</span><span class="n">capital</span> <span class="o">-</span> <span class="n">saving_limit</span><span class="p">)</span>
124
+ <span class="k">return</span> <span class="o">-</span><span class="n">fixed_costs</span> <span class="o">-</span> <span class="n">out_rate</span>
125
+
126
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span><span class="o">*</span><span class="mi">10</span><span class="p">)</span>
127
+
128
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_salary</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
129
+
130
+ <span class="c1">#Graphic details</span>
131
+ <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
132
+
133
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">saving_limit</span><span class="p">,</span> <span class="n">saving_limit</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Saving Limit&#39;</span><span class="p">)</span>
134
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Capital(t)&#39;</span><span class="p">)</span>
135
+
136
+ <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
137
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
138
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
139
+ <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
140
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
141
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
142
+ <span class="k">else</span><span class="p">:</span>
143
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
144
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
145
+
146
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
147
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
148
+
149
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">/</span> <span class="mi">20</span><span class="p">))</span>
150
+
151
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
152
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">initial_salary</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span>
153
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Days&#39;</span><span class="p">)</span>
154
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Capital $&#39;</span><span class="p">)</span>
155
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
156
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
157
+
158
+ <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
159
+ </pre></div>
160
+
161
+ </div>
162
+ </div>
163
+ </div>
164
+
165
+ </div>
166
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
167
+ <div class="text_cell_render border-box-sizing rendered_html">
168
+ <p>Next, the graphics part, here matplotlib is used and this snipped is specific to this library but I will explain it in order to be self-contained.</p>
169
+ <p><strong>Note</strong>: I know there are plenty of plotting libraries, and many even more elegant and powerful than matplotlib but for this use case, where the complexity lies in the differential equation and not in the graphic itself, I believe it is more than enough. For complex graphics such as statistics or big data, I would recommend another library such as bqplot.</p>
170
+ <p>First we define a <em>figure</em> and an <em>axes</em> object, explicitly initializing the figure size through <em>figsize</em>. Then a horizontal line with the label <em>Saving Limit</em> is plot at the heigh of the <em>saving_limit</em> and after that the solution is plot with the label <em>Capital(t)</em>.</p>
171
+ <p>The if-elif-else structure defines how many sticks there will be in the x-axis and if they should be place horizontal or vertically. Then the said sticks are plotted, the same occurs with the yticks. Then the limits for each axis and the labels for each axis are set and finally, the legend position is defined and the grid is added.</p>
172
+ <p>Then when <em>plt.show()</em> is called the figure is generated and showed in the screen.</p>
173
+
174
+ </div>
175
+ </div>
176
+ </div>
177
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
178
+ <div class="text_cell_render border-box-sizing rendered_html">
179
+ <h3 id="Evaluation---Home-Economy">Evaluation - Home Economy<a class="anchor-link" href="#Evaluation---Home-Economy">&#182;</a></h3><p>Finally, the code for the interaction, here the <em>interact</em> function from <em>ipywidgets</em> is used. A separate slider is used for each parameter with the default parameters set for a nice default visualization, with a description and the style and layout is used from the initialization cells.</p>
180
+
181
+ </div>
182
+ </div>
183
+ </div>
184
+
185
+
186
+
187
+ <div class="
188
+ cell border-box-sizing code_cell rendered">
189
+ <div class="input">
190
+
191
+ <div class="inner_cell">
192
+ <div class="input_area">
193
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
194
+ <span class="n">initial_salary</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">25000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">15000</span><span class="p">,</span>
195
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Salary&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
196
+ <span class="n">savings_ration</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span>
197
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Savings Ratio&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
198
+ <span class="n">extraordinary_expenses</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.005</span><span class="p">,</span>
199
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Extraordinary Expenses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
200
+ <span class="n">fixed_costs</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
201
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Fixed Costs&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
202
+ <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
203
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">)</span>
204
+ <span class="p">);</span>
205
+ </pre></div>
206
+
207
+ </div>
208
+ </div>
209
+ </div>
210
+
211
+ <div class="output_wrapper">
212
+ <div class="output">
213
+
214
+
215
+ <div class="output_area">
216
+
217
+
218
+
219
+
220
+
221
+ <div class="output_subarea output_widget_view ">
222
+ <button class="js-nbinteract-widget">
223
+ Loading widgets...
224
+ </button>
225
+ </div>
226
+
227
+ </div>
228
+
229
+ </div>
230
+ </div>
231
+
232
+ </div>
233
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
234
+ <div class="text_cell_render border-box-sizing rendered_html">
235
+ <h2 id="Sales-of-Houses-and-Air-Conditionings">Sales of Houses and Air Conditionings<a class="anchor-link" href="#Sales-of-Houses-and-Air-Conditionings">&#182;</a></h2><p>In a city there are houses and air conditionings (AC) for sale and it's known that these are complementary goods, which means that if the sales of one increase the sales of the other will also increase. The number of houses sales is proportional to the number of houses that weren't sold yet and the number of AC sales is proportional to the houses sold that doesn't yet have an AC.</p>
236
+ <p>The model has the following variables:</p>
237
+ <ul>
238
+ <li>_initial<em>houses</em>: Number of Houses initially sold, could be 0</li>
239
+ <li>_initial<em>ac</em>: Number of air conditionings sold, could be 0 and
240
+ should be less than initial_houses</li>
241
+ <li>_avg_time<em>house</em>: Average number of days to sell a <strong>house</strong></li>
242
+ <li>_avg_time<em>ac</em>: Average number of days to sell an <strong>AC</strong></li>
243
+ <li>_total<em>houses</em>: Total number of <strong>Houses for sale</strong></li>
244
+ <li><em>days</em>: The time interval until the <strong>next income</strong></li>
245
+ </ul>
246
+ <p>Notation for the equation:</p>
247
+ <ul>
248
+ <li>$x(t)$: Number of sold Houses</li>
249
+ <li>$y(t)$: Number of sold Air Conditionings</li>
250
+ <li>$x(0)$= _initial<em>houses</em></li>
251
+ <li>$y(0)$= _initial<em>ac</em></li>
252
+ <li>h = _total<em>houses</em></li>
253
+ <li>a = _avg_time<em>house</em></li>
254
+ <li>b = _avg_time<em>ac</em></li>
255
+ </ul>
256
+ <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
257
+
258
+ </div>
259
+ </div>
260
+ </div>
261
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
262
+ <div class="text_cell_render border-box-sizing rendered_html">
263
+ $$
264
+ \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =\frac{1}{a} (h - x) \\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =\frac{1}{b} (x - y) \end{matrix}\right.
265
+ $$
266
+ </div>
267
+ </div>
268
+ </div>
269
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
270
+ <div class="text_cell_render border-box-sizing rendered_html">
271
+ <h3 id="Equation---Homes-and-ACs">Equation - Homes and ACs<a class="anchor-link" href="#Equation---Homes-and-ACs">&#182;</a></h3>
272
+ </div>
273
+ </div>
274
+ </div>
275
+
276
+
277
+
278
+ <div class="
279
+ cell border-box-sizing code_cell rendered">
280
+ <div class="input">
281
+
282
+ <div class="inner_cell">
283
+ <div class="input_area">
284
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">initial_houses</span><span class="p">,</span> <span class="n">initial_ac</span><span class="p">,</span> <span class="n">avg_time_house</span><span class="p">,</span> <span class="n">avg_time_ac</span><span class="p">,</span> <span class="n">total_houses</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
285
+
286
+ <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
287
+ <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">s</span>
288
+ <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
289
+ <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">avg_time_house</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">total_houses</span> <span class="o">-</span> <span class="n">x</span><span class="p">),</span> <span class="c1"># dx/dt: Change in the House sales</span>
290
+ <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">avg_time_ac</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">)</span> <span class="c1"># dx/dt: Change in the AC sales</span>
291
+ <span class="p">]</span>
292
+ <span class="k">return</span> <span class="n">dydt</span>
293
+
294
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
295
+ <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_houses</span><span class="p">,</span> <span class="n">initial_ac</span><span class="p">]</span>
296
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
297
+
298
+ <span class="c1">#Graphic details</span>
299
+ <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
300
+
301
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Houses(t)&#39;</span><span class="p">)</span>
302
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Air Conditionings(t)&#39;</span><span class="p">)</span>
303
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">total_houses</span><span class="p">,</span> <span class="n">total_houses</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Total Houses&#39;</span><span class="p">)</span>
304
+
305
+ <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
306
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
307
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
308
+ <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
309
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
310
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
311
+ <span class="k">else</span><span class="p">:</span>
312
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
313
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
314
+
315
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
316
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
317
+
318
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_yticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">/</span> <span class="mi">20</span><span class="p">))</span>
319
+
320
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
321
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">total_houses</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span>
322
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Months&#39;</span><span class="p">)</span>
323
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Units&#39;</span><span class="p">)</span>
324
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
325
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
326
+
327
+ <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
328
+ </pre></div>
329
+
330
+ </div>
331
+ </div>
332
+ </div>
333
+
334
+ </div>
335
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
336
+ <div class="text_cell_render border-box-sizing rendered_html">
337
+ <p>In this scenario and the followings the code will be explain where it differs from the first example to avoid unnecesary repetition.</p>
338
+ <p>In this case the constants <em>avg_time_house</em> and <em>avg_time_ac</em> are used directly inside the function since it is much clearer this way. To define a system of ODEs, first the initial condition should be a list, each element of the list represents the initial condition for each of the variables. The nested function also changes a bit, in order to work, the first parameter would also be a list so it is necessary to unpack it into the variables and then a variable <em>dydt</em> is used to represent the system where each element is the left side of each equation in canonical form.</p>
339
+ <p>Finally the <em>dydt</em> variable is returned. The definition of <em>time</em> is the same as previously seen and the solution variable is now a nested array containing the solution for both variables. This is specially useful when plotting.</p>
340
+
341
+ </div>
342
+ </div>
343
+ </div>
344
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
345
+ <div class="text_cell_render border-box-sizing rendered_html">
346
+ <h3 id="Evaluation---Homes-and-ACs">Evaluation - Homes and ACs<a class="anchor-link" href="#Evaluation---Homes-and-ACs">&#182;</a></h3>
347
+ </div>
348
+ </div>
349
+ </div>
350
+
351
+
352
+
353
+ <div class="
354
+ cell border-box-sizing code_cell rendered">
355
+ <div class="input">
356
+
357
+ <div class="inner_cell">
358
+ <div class="input_area">
359
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
360
+ <span class="n">initial_houses</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
361
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial sold Houses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
362
+ <span class="n">initial_ac</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
363
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial sold AC&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
364
+ <span class="n">total_houses</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2000</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
365
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Houses&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
366
+ <span class="n">avg_time_house</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
367
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Time for House&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
368
+ <span class="n">avg_time_ac</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
369
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Time for AC&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
370
+ <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">360</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
371
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
372
+ <span class="p">);</span>
373
+ </pre></div>
374
+
375
+ </div>
376
+ </div>
377
+ </div>
378
+
379
+ <div class="output_wrapper">
380
+ <div class="output">
381
+
382
+
383
+ <div class="output_area">
384
+
385
+
386
+
387
+
388
+
389
+ <div class="output_subarea output_widget_view ">
390
+ <button class="js-nbinteract-widget">
391
+ Loading widgets...
392
+ </button>
393
+ </div>
394
+
395
+ </div>
396
+
397
+ </div>
398
+ </div>
399
+
400
+ </div>
401
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
402
+ <div class="text_cell_render border-box-sizing rendered_html">
403
+ <h2 id="Stock-Control">Stock Control<a class="anchor-link" href="#Stock-Control">&#182;</a></h2><p>A company wants a desired stock, they started with an initial stock and they work with a provider which sends deliveries goods through sales orders. The company asks the provider each day a sales order proportional to the missing or surplus quantity to the desired stock and the market demand.</p>
404
+ <p>The provider deliver the goods according to the sales order with a constant delay. Finally, the market demand is consider constant. The company wants to know how their stock and their request to the provider will behave and they will try to avoid alternating behavior, a oscillation in stock.</p>
405
+ <p>The model has the following variables:</p>
406
+ <ul>
407
+ <li>_desired<em>stock</em>: Amount of stock the company wants to have.</li>
408
+ <li>_initial<em>stock</em>: The initial amount of stock of the company.</li>
409
+ <li>_initial<em>orders</em>: initial amount of goods asked in sales orders.</li>
410
+ <li>_stock<em>control</em>: Proportion of the missing/surplus stock to ask to the provider.</li>
411
+ <li>_market<em>demand</em>: The daily demand of the market</li>
412
+ <li>_provider<em>delay</em>: The number of days of the provider delay.</li>
413
+ <li><em>days</em>: The total number of days the model will analyse.</li>
414
+ </ul>
415
+ <p>Notation for the equation:</p>
416
+ <ul>
417
+ <li>$x(t)$: Number of goods in stock</li>
418
+ <li>$y(t)$: Number of pending sales order sent to the provider</li>
419
+ <li>$x(0)$ = _initial<em>stock</em></li>
420
+ <li>$y(0)$ = _initial<em>orders</em></li>
421
+ <li>s = _desired<em>stock</em></li>
422
+ <li>d = _market<em>demand</em></li>
423
+ <li>k = _stock<em>control</em></li>
424
+ <li>p = _provider<em>delay</em></li>
425
+ </ul>
426
+
427
+ </div>
428
+ </div>
429
+ </div>
430
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
431
+ <div class="text_cell_render border-box-sizing rendered_html">
432
+ <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
433
+ $$
434
+ \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; \frac{1}{p} y - d \\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; - \frac{1}{p} y + d + k ( s - x ) \end{matrix}\right.
435
+ $$<p>This time the code is only changed in the variables names and the equations but it is very similar to the previous example, the particularity of this model doesn't lie in the code but rather in the behavior, since it can show the differences between an <strong>oscillation</strong> behavior and a damped one. Going through all kinds of possibilities such as underdamped, overdamped, critically damped and undamped. I encourage you to experiment with the parameters in the widget.</p>
436
+
437
+ </div>
438
+ </div>
439
+ </div>
440
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
441
+ <div class="text_cell_render border-box-sizing rendered_html">
442
+ <h3 id="Equation---Stock-Control">Equation - Stock Control<a class="anchor-link" href="#Equation---Stock-Control">&#182;</a></h3>
443
+ </div>
444
+ </div>
445
+ </div>
446
+
447
+
448
+
449
+ <div class="
450
+ cell border-box-sizing code_cell rendered">
451
+ <div class="input">
452
+
453
+ <div class="inner_cell">
454
+ <div class="input_area">
455
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">desired_stock</span><span class="p">,</span> <span class="n">initial_stock</span><span class="p">,</span>
456
+ <span class="n">initial_request</span><span class="p">,</span> <span class="n">stock_control</span><span class="p">,</span> <span class="n">market_demand</span><span class="p">,</span>
457
+ <span class="n">provider_delay</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
458
+
459
+ <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">v0</span><span class="p">,</span> <span class="n">time</span><span class="p">):</span>
460
+ <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">v0</span>
461
+
462
+ <span class="c1"># dx/dt -&gt; Change in Stock</span>
463
+ <span class="c1"># dy/dt -&gt; Change in Requests</span>
464
+ <span class="c1">#</span>
465
+ <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
466
+ <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">provider_delay</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="o">-</span> <span class="n">market_demand</span><span class="p">,</span>
467
+ <span class="o">-</span> <span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="n">provider_delay</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="o">+</span> <span class="n">market_demand</span> <span class="o">+</span> <span class="n">stock_control</span> <span class="o">*</span> <span class="p">(</span><span class="n">desired_stock</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
468
+ <span class="p">]</span>
469
+ <span class="k">return</span> <span class="n">dydt</span>
470
+
471
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="n">days</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
472
+ <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_stock</span><span class="p">,</span> <span class="n">initial_request</span><span class="p">]</span>
473
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
474
+
475
+ <span class="c1"># Graphic details</span>
476
+ <span class="c1">#</span>
477
+ <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
478
+
479
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Stock(t)&#39;</span><span class="p">)</span>
480
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Requests(t)&#39;</span><span class="p">)</span>
481
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">),</span> <span class="p">(</span><span class="n">desired_stock</span><span class="p">,</span> <span class="n">desired_stock</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Desired Stock&#39;</span><span class="p">)</span>
482
+
483
+ <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
484
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
485
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
486
+ <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
487
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
488
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
489
+ <span class="k">else</span><span class="p">:</span>
490
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
491
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
492
+
493
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
494
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
495
+
496
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
497
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]),</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]))</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
498
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Days&#39;</span><span class="p">)</span>
499
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Units&#39;</span><span class="p">)</span>
500
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
501
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
502
+
503
+ <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
504
+
505
+ </pre></div>
506
+
507
+ </div>
508
+ </div>
509
+ </div>
510
+
511
+ </div>
512
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
513
+ <div class="text_cell_render border-box-sizing rendered_html">
514
+ <h3 id="Evaluation---Stock-Control">Evaluation - Stock Control<a class="anchor-link" href="#Evaluation---Stock-Control">&#182;</a></h3>
515
+ </div>
516
+ </div>
517
+ </div>
518
+
519
+
520
+
521
+ <div class="
522
+ cell border-box-sizing code_cell rendered">
523
+ <div class="input">
524
+
525
+ <div class="inner_cell">
526
+ <div class="input_area">
527
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
528
+ <span class="n">desired_stock</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
529
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Desired Stock&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
530
+ <span class="n">initial_stock</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span>
531
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Stock&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
532
+ <span class="n">initial_request</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">14</span><span class="p">,</span>
533
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Requests&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
534
+ <span class="n">stock_control</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">1.5</span><span class="p">,</span>
535
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Stock Control&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
536
+ <span class="n">market_demand</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
537
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Market Demand&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
538
+ <span class="n">provider_delay</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
539
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Provider Delay&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
540
+ <span class="n">days</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">360</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
541
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total Number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
542
+ <span class="p">);</span>
543
+ </pre></div>
544
+
545
+ </div>
546
+ </div>
547
+ </div>
548
+
549
+ <div class="output_wrapper">
550
+ <div class="output">
551
+
552
+
553
+ <div class="output_area">
554
+
555
+
556
+
557
+
558
+
559
+ <div class="output_subarea output_widget_view ">
560
+ <button class="js-nbinteract-widget">
561
+ Loading widgets...
562
+ </button>
563
+ </div>
564
+
565
+ </div>
566
+
567
+ </div>
568
+ </div>
569
+
570
+ </div>
571
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
572
+ <div class="text_cell_render border-box-sizing rendered_html">
573
+ <h2 id="Useful-Life">Useful Life<a class="anchor-link" href="#Useful-Life">&#182;</a></h2><p>A patient takes a certain drug with a recipe, the recipe says how many milligrams the patient should have per intake, how often the intakes should be and how many. Additionally the drug itself provides information about its useful life which helps to determine the optimal interval between intakes. It's also known that the useful life of the drug follows a natural decay.</p>
574
+ <p>The model has the following variables:</p>
575
+ <ul>
576
+ <li>_useful<em>life</em>: Duration of the useful life of the drug in hours</li>
577
+ <li>_intake<em>mg</em>: Amount of milligrams per intake.</li>
578
+ <li>_intake<em>interval</em>: Time interval in hours between intakes.</li>
579
+ <li>_intake<em>number</em>: Number of intakes.</li>
580
+ <li><em>hours</em>: The total number of hours the model will analyse.</li>
581
+ </ul>
582
+ <p>Notation for the equation:</p>
583
+ <ul>
584
+ <li>$x(t)$: Milligrams of drug in the body</li>
585
+ <li>$x(0)$: _intake<em>mg</em></li>
586
+ <li>a = _useful<em>life</em></li>
587
+ </ul>
588
+ <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
589
+ $$
590
+ \frac{\mathrm{d} x}{\mathrm{d} t} = - \frac{1}{a} ln(2) y
591
+ $$
592
+ </div>
593
+ </div>
594
+ </div>
595
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
596
+ <div class="text_cell_render border-box-sizing rendered_html">
597
+ <p><strong>Note</strong>: the expression $ln(2)$ is part of the <strong>natural decay formula</strong>.</p>
598
+
599
+ </div>
600
+ </div>
601
+ </div>
602
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
603
+ <div class="text_cell_render border-box-sizing rendered_html">
604
+ <h3 id="Equation---Useful-Life">Equation - Useful Life<a class="anchor-link" href="#Equation---Useful-Life">&#182;</a></h3>
605
+ </div>
606
+ </div>
607
+ </div>
608
+
609
+
610
+
611
+ <div class="
612
+ cell border-box-sizing code_cell rendered">
613
+ <div class="input">
614
+
615
+ <div class="inner_cell">
616
+ <div class="input_area">
617
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">useful_life</span><span class="p">,</span> <span class="n">intake_mg</span><span class="p">,</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="n">intake_number</span><span class="p">,</span> <span class="n">hours</span><span class="p">):</span>
618
+
619
+ <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
620
+ <span class="k">return</span> <span class="o">-</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">useful_life</span><span class="p">)</span> <span class="o">*</span> <span class="n">y</span> <span class="c1"># dy/dt -&gt; Change of mg </span>
621
+
622
+ <span class="n">intake_hours</span> <span class="o">=</span> <span class="p">[</span><span class="n">intake_interval</span> <span class="o">*</span> <span class="n">i</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="n">intake_number</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)]</span>
623
+ <span class="n">initial_condition</span> <span class="o">=</span> <span class="n">intake_mg</span>
624
+ <span class="n">times</span> <span class="o">=</span> <span class="p">[]</span>
625
+ <span class="n">solutions</span> <span class="o">=</span> <span class="p">[]</span>
626
+
627
+ <span class="k">for</span> <span class="n">intake_time</span> <span class="ow">in</span> <span class="n">intake_hours</span><span class="p">:</span>
628
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">intake_time</span><span class="p">,</span> <span class="n">intake_time</span> <span class="o">+</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
629
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_condition</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
630
+
631
+ <span class="n">initial_condition</span> <span class="o">=</span> <span class="n">solution</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">intake_mg</span>
632
+
633
+ <span class="n">times</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
634
+ <span class="n">solutions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span>
635
+
636
+
637
+ <span class="n">intake_time</span> <span class="o">=</span> <span class="n">intake_hours</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">intake_interval</span>
638
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">intake_time</span><span class="p">,</span> <span class="n">intake_time</span> <span class="o">+</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">intake_interval</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
639
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_condition</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
640
+ <span class="n">times</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
641
+ <span class="n">solutions</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span>
642
+
643
+ <span class="c1">#Graphic details</span>
644
+ <span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
645
+
646
+ <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">times</span><span class="p">,</span> <span class="n">solutions</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Concentration in the Body(t)&#39;</span><span class="p">)</span>
647
+
648
+ <span class="k">if</span> <span class="n">hours</span> <span class="o">&lt;=</span> <span class="mi">60</span><span class="p">:</span>
649
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
650
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
651
+ <span class="k">elif</span> <span class="n">hours</span> <span class="o">&lt;=</span> <span class="mi">300</span><span class="p">:</span>
652
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
653
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
654
+ <span class="k">else</span><span class="p">:</span>
655
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
656
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
657
+
658
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
659
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
660
+
661
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">hours</span><span class="p">])</span>
662
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solutions</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
663
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Hours&#39;</span><span class="p">)</span>
664
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Concentration&#39;</span><span class="p">)</span>
665
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
666
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
667
+
668
+ <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
669
+ </pre></div>
670
+
671
+ </div>
672
+ </div>
673
+ </div>
674
+
675
+ </div>
676
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
677
+ <div class="text_cell_render border-box-sizing rendered_html">
678
+ <p>This model doesn't introduces complexity in the ODE but rather in the way one can model the successive intakes. It is usually helpful to keep this 'iteration logic' independent from the ODE itself, that's why no particular changes are needed in the equations.</p>
679
+ <p>There are many ways for achieving this but the one I used is the following:</p>
680
+ <ol>
681
+ <li>Create a list with all the intake hours</li>
682
+ <li>Create an empty list, times, for the times</li>
683
+ <li>Create an empty list, solutions for the solutions</li>
684
+ <li>Iterate through that list<ul>
685
+ <li>Create a time array starting in the time iterated and ended in the time
686
+ iterated + the intake_interval</li>
687
+ <li>Solve the ODE with the initial_condition and the previously created time</li>
688
+ <li>Update the value of the initial_conditions with the final solution</li>
689
+ <li>Append the solution list to the end of the solutions list</li>
690
+ <li>Append the time list to the end of the times list</li>
691
+ </ul>
692
+ </li>
693
+ <li>Create a time array starting from the last the last time of intakes
694
+ to 10 times the intake_interval</li>
695
+ <li>Solve the ODE again with a longer interval to see how it behaves
696
+ after the iterations</li>
697
+ <li>Append the solution list to the end of the solutions list</li>
698
+ <li>Append the time list to the end of the times list</li>
699
+ <li>Proceed to Plotting</li>
700
+ </ol>
701
+ <p>This approach consist in solving several ODEs, each of which receives the final solution of the previous as its initial conditions.</p>
702
+
703
+ </div>
704
+ </div>
705
+ </div>
706
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
707
+ <div class="text_cell_render border-box-sizing rendered_html">
708
+ <h3 id="Evaluation---Useful-Life">Evaluation - Useful Life<a class="anchor-link" href="#Evaluation---Useful-Life">&#182;</a></h3>
709
+ </div>
710
+ </div>
711
+ </div>
712
+
713
+
714
+
715
+ <div class="
716
+ cell border-box-sizing code_cell rendered">
717
+ <div class="input">
718
+
719
+ <div class="inner_cell">
720
+ <div class="input_area">
721
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
722
+ <span class="n">useful_life</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">3.8</span><span class="p">,</span>
723
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Useful Life&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
724
+ <span class="n">intake_mg</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
725
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Miligrams per Intake&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
726
+ <span class="n">intake_interval</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
727
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Hours between Intakes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
728
+ <span class="n">intake_number</span><span class="o">=</span><span class="n">IntSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
729
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Number of Intakes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
730
+ <span class="n">hours</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">240</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span>
731
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total number of Hours&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
732
+ <span class="p">);</span>
733
+ </pre></div>
734
+
735
+ </div>
736
+ </div>
737
+ </div>
738
+
739
+ <div class="output_wrapper">
740
+ <div class="output">
741
+
742
+
743
+ <div class="output_area">
744
+
745
+
746
+
747
+
748
+
749
+ <div class="output_subarea output_widget_view ">
750
+ <button class="js-nbinteract-widget">
751
+ Loading widgets...
752
+ </button>
753
+ </div>
754
+
755
+ </div>
756
+
757
+ </div>
758
+ </div>
759
+
760
+ </div>
761
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
762
+ <div class="text_cell_render border-box-sizing rendered_html">
763
+ <h2 id="Foxes-and-Rabbits">Foxes and Rabbits<a class="anchor-link" href="#Foxes-and-Rabbits">&#182;</a></h2><p>This is the model known as <em>predator–prey</em> or <em>Lotka–Volterra</em> equations. There are two species, a predator and a prey, like foxes and rabbits. Each has a certain birthrate and a deathrate. The birthrate of the prey is proportional to its current population and the birthrate of the predator is proportional to both its population and the prey population. The deathrate of the prey is proportional to both its population and the predator population and the deathrate of the predator is proportional to its population.</p>
764
+ <p>The model has the following variables:</p>
765
+ <ul>
766
+ <li><em>rabbits_birthrate</em>: Rabbits birthrate</li>
767
+ <li><em>rabbits_deathrate</em>: Rabbits deathrate</li>
768
+ <li><em>foxes_birthrate</em>: Foxes birthrate</li>
769
+ <li><em>initial_rabbits</em>: Initial rabbits</li>
770
+ <li><em>initial_foxes</em>: Initial foxes</li>
771
+ <li><em>days</em>: The total number of days the model will analyse</li>
772
+ </ul>
773
+ <p>Notation for the equation:</p>
774
+ <ul>
775
+ <li>$x(t)$: Population of Rabbits over time</li>
776
+ <li>$y(t)$: Population of Foxes over time</li>
777
+ <li>$x(0)$ = <em>initial_rabbits</em></li>
778
+ <li>$y(0)$ = <em>initial_foxes</em></li>
779
+ <li>a = <em>rabbits_birthrate</em></li>
780
+ <li>b = <em>rabbits_deathrate</em></li>
781
+ <li>c = <em>foxes_birthrate</em></li>
782
+ <li>d = <em>foxes_deathrate</em></li>
783
+ </ul>
784
+
785
+ </div>
786
+ </div>
787
+ </div>
788
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
789
+ <div class="text_cell_render border-box-sizing rendered_html">
790
+ <p>With the previously defined notation, the ordinary differential equation is as follows:</p>
791
+ $$
792
+ \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; a x &amp;-&amp; bxy\\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; cxy &amp;-&amp; dy \end{matrix}\right.
793
+ $$
794
+ </div>
795
+ </div>
796
+ </div>
797
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
798
+ <div class="text_cell_render border-box-sizing rendered_html">
799
+ <p>Or more concisely:</p>
800
+ $$
801
+ \left\{\begin{matrix} \frac{\mathrm{d} x}{\mathrm{d} t} =&amp; x (a - by)\\\\ \frac{\mathrm{d} y}{\mathrm{d} t} =&amp; y (cx - d) \end{matrix}\right.
802
+ $$
803
+ </div>
804
+ </div>
805
+ </div>
806
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
807
+ <div class="text_cell_render border-box-sizing rendered_html">
808
+ <p><strong>Note</strong>: The <em>Lotka-Volterra equations</em> are a pair of first-order nonlinear differential
809
+ equations, frequently used to describe the dynamics of biological systems in which
810
+ two species interact, one as a predator and the other as prey.</p>
811
+
812
+ </div>
813
+ </div>
814
+ </div>
815
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
816
+ <div class="text_cell_render border-box-sizing rendered_html">
817
+ <h3 id="Equation---Foxes-and-Rabbits">Equation - Foxes and Rabbits<a class="anchor-link" href="#Equation---Foxes-and-Rabbits">&#182;</a></h3>
818
+ </div>
819
+ </div>
820
+ </div>
821
+
822
+
823
+
824
+ <div class="
825
+ cell border-box-sizing code_cell rendered">
826
+ <div class="input">
827
+
828
+ <div class="inner_cell">
829
+ <div class="input_area">
830
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">rabbits_birthrate</span><span class="p">,</span> <span class="n">rabbits_deathrate</span><span class="p">,</span>
831
+ <span class="n">foxes_birthrate</span><span class="p">,</span> <span class="n">foxes_deathrate</span><span class="p">,</span>
832
+ <span class="n">initial_rabbits</span><span class="p">,</span> <span class="n">initial_foxes</span><span class="p">,</span> <span class="n">days</span><span class="p">):</span>
833
+
834
+ <span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
835
+ <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">s</span>
836
+ <span class="n">dydt</span> <span class="o">=</span> <span class="p">[</span>
837
+ <span class="n">rabbits_birthrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="n">rabbits_deathrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span><span class="p">,</span> <span class="c1"># dx/dy -&gt; Change in Rabbits</span>
838
+ <span class="n">foxes_birthrate</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span> <span class="o">-</span> <span class="n">foxes_deathrate</span> <span class="o">*</span> <span class="n">y</span> <span class="c1"># dy/dt -&gt; Change in Foxes</span>
839
+ <span class="p">]</span>
840
+
841
+ <span class="k">return</span> <span class="n">dydt</span>
842
+
843
+ <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
844
+ <span class="n">initial_conditions</span> <span class="o">=</span> <span class="p">[</span><span class="n">initial_rabbits</span><span class="p">,</span> <span class="n">initial_foxes</span><span class="p">]</span>
845
+ <span class="n">solution</span> <span class="o">=</span> <span class="n">odeint</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">initial_conditions</span><span class="p">,</span> <span class="n">time</span><span class="p">)</span>
846
+
847
+ <span class="c1"># Graphic details</span>
848
+ <span class="c1">#</span>
849
+ <span class="n">fig</span><span class="p">,</span> <span class="n">axes</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
850
+
851
+ <span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
852
+
853
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Rabbits(t)&#39;</span><span class="p">)</span>
854
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Foxes(t)&#39;</span><span class="p">)</span>
855
+
856
+ <span class="k">if</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">30</span><span class="p">:</span>
857
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
858
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;horizontal&quot;</span>
859
+ <span class="k">elif</span> <span class="n">days</span> <span class="o">&lt;=</span> <span class="mi">150</span><span class="p">:</span>
860
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">5</span>
861
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
862
+ <span class="k">else</span><span class="p">:</span>
863
+ <span class="n">step</span> <span class="o">=</span> <span class="mi">10</span>
864
+ <span class="n">rotation</span> <span class="o">=</span> <span class="s2">&quot;vertical&quot;</span>
865
+
866
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">),</span> <span class="n">rotation</span><span class="o">=</span><span class="n">rotation</span><span class="p">)</span>
867
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">step</span><span class="p">))</span>
868
+
869
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="n">days</span><span class="p">])</span>
870
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]),</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]))</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
871
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Time&#39;</span><span class="p">)</span>
872
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Population&#39;</span><span class="p">)</span>
873
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
874
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
875
+
876
+ <span class="n">ax</span> <span class="o">=</span> <span class="n">axes</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
877
+
878
+ <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;Foxes vs Rabbits&#39;</span><span class="p">)</span>
879
+
880
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">])</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
881
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">solution</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">])</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">])</span>
882
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s1">&#39;Rabbits&#39;</span><span class="p">)</span>
883
+ <span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s1">&#39;Foxes&#39;</span><span class="p">)</span>
884
+ <span class="n">ax</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="s1">&#39;best&#39;</span><span class="p">)</span>
885
+ <span class="n">ax</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
886
+
887
+ <span class="n">plt</span><span class="o">.</span><span class="n">tight_layout</span><span class="p">()</span>
888
+ <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
889
+ </pre></div>
890
+
891
+ </div>
892
+ </div>
893
+ </div>
894
+
895
+ </div>
896
+ <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
897
+ <div class="text_cell_render border-box-sizing rendered_html">
898
+ <h3 id="Evaluation---Foxes-and-Rabbits">Evaluation - Foxes and Rabbits<a class="anchor-link" href="#Evaluation---Foxes-and-Rabbits">&#182;</a></h3>
899
+ </div>
900
+ </div>
901
+ </div>
902
+
903
+
904
+
905
+ <div class="
906
+ cell border-box-sizing code_cell rendered">
907
+ <div class="input">
908
+
909
+ <div class="inner_cell">
910
+ <div class="input_area">
911
+ <div class=" highlight hl-ipython3"><pre><span></span><span class="n">interact</span><span class="p">(</span><span class="n">main</span><span class="p">,</span>
912
+ <span class="n">rabbits_birthrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
913
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Birth Rate of Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
914
+ <span class="n">rabbits_deathrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
915
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Death Rate of Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
916
+ <span class="n">foxes_birthrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
917
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Birth Rate of Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
918
+ <span class="n">foxes_deathrate</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">24</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
919
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Death Rate of Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
920
+ <span class="n">initial_rabbits</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
921
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Rabbits&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
922
+ <span class="n">initial_foxes</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
923
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Initial Foxes&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
924
+ <span class="n">days</span><span class="o">=</span><span class="n">FloatSlider</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span> <span class="p">,</span><span class="nb">max</span><span class="o">=</span><span class="mi">365</span> <span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">15</span><span class="p">,</span>
925
+ <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Total number of Days&#39;</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="n">style</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="n">slider_layout</span><span class="p">),</span>
926
+ <span class="p">);</span>
927
+ </pre></div>
928
+
929
+ </div>
930
+ </div>
931
+ </div>
932
+
933
+ <div class="output_wrapper">
934
+ <div class="output">
935
+
936
+
937
+ <div class="output_area">
938
+
939
+
940
+
941
+
942
+
943
+ <div class="output_subarea output_widget_view ">
944
+ <button class="js-nbinteract-widget">
945
+ Loading widgets...
946
+ </button>
947
+ </div>
948
+
949
+ </div>
950
+
951
+ </div>
952
+ </div>
953
+
954
+ </div>
955
+