Skip to content
Snippets Groups Projects
Commit 7b932226 authored by Robert Lanzafame's avatar Robert Lanzafame
Browse files

Merge branch 'main' into release

parents cbbafbad 77c9d07e
No related branches found
No related tags found
No related merge requests found
Pipeline #251329 passed
%% Cell type:markdown id: tags:
# PA 1.8: Equations Done Symply
<h1 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; top: 60px;right: 30px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png" style="width:100px" />
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png" style="width:100px" />
</h1>
<h2 style="height: 10px">
</h2>
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.8. Due: complete this PA prior to class on Friday, Oct 25, 2024.*
%% Cell type:markdown id: tags:
This notebook should be completed _after_ you read the [book chapter on Sympy](https://mude.citg.tudelft.nl/2024/book/programming/week_1_8.html).
The exercises in this notebook are directly from WS 1.7 and PA 1.7, to give you a very practical application for Sympy. See the [WS 1.7 solution here](https://mude.citg.tudelft.nl/2024/files/Week_1_7/WS_1_7_solution/) for reference.
%% Cell type:code id: tags:
``` python
import sympy as sym
```
%% Cell type:markdown id: tags:
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1:</b>
Fill in the missing pieces of code to get Sympy to reproduce the equations and analyses required from WS 1.7. The comments in the code provide explicit instructions.
</p>
</div>
%% Cell type:code id: tags:
``` python
# Define the Gumbel distribution using SymPy
x, beta, mu = sym.symbols('x beta mu')
F_gumbel = YOUR CODE HERE
```
%% Cell type:code id: tags:
``` python
# Invert the Gumbel distribution using SymPy to find x ~ non-exceedence probability, beta, mu as done in the workshop by hand
Prob_non_exc = sym.symbols('Prob_non_exc')
YOUR CODE HERE
x_sol = YOUR CODE HERE
display(x_sol)
```
%% Cell type:code id: tags:
``` python
# Evaluate your inverted Gumbel distribution using SymPy for the min, 0.25, 0.5, 0.75 and max non-exceedence probabilities as done in the workshop by hand
# You should find:
#3.353
#23.89
#32.97
#44.48
#115.3
Prob_non_exc_list = [1/773, 0.25, 0.5, 0.75, 772/773]
for i in range(len(Prob_non_exc_list)):
display(x_sol.subs({beta:13.097, mu:28.167, Prob_non_exc:Prob_non_exc_list[i]}))
```
%% Cell type:code id: tags:
``` python
# Use SymPy to find the probability density function of the Gumbel distribution, is it equal to the function you found in the book?
f_gumbel = YOUR CODE HERE
display(f_gumbel)
```
%% Cell type:markdown id: tags:
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2:</b>
Fill in the missing pieces of code to get Sympy to reproduce the equations and analyses required from PA 1.7.
</p>
</div>
%% Cell type:markdown id: tags:
Define the Piecewise equation from PA 1.7 in SymPy (hint, see https://docs.sympy.org/latest/modules/functions/elementary.html#piecewise)
$$\begin{equation}
f(x)=
\begin{cases}
0.1 & \text{if } 0 < x < 3.6 \\
2(x-5) & 5 < x < 5.8 \\
0 & \text{elsewhere}
\end{cases}
\end{equation}$$
Use the provided function to plot it for $-1<x<6$
%% Cell type:code id: tags:
``` python
f = YOUR CODE HERE
sym.plot(f,(x,-1,6));
```
%% Cell type:code id: tags:
``` python
# Integrate the piecewise probability density function to find the cumulative distribution function as done numerically in PA1.7
x = sym.symbols('x')
F = YOUR CODE HERE
sym.plot(F, (x,-1,6));
```
%% Cell type:markdown id: tags:
This cell will check your work to make sure you have completed the assignment properly.
%% Cell type:markdown id: tags:
<div style="background-color:#AABAB2; color: black; width:95%; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3:</b>
Run the cell below to check your work, but don't change anything. If the cell runs without error, you will pass the assignment once you commit it and push it to GitHub.
</p>
</div>
%% Cell type:code id: tags:
``` python
import numpy as np
assert sym.simplify(F_gumbel - sym.exp(-sym.exp((mu - x)/beta))) == 0 , 'Error: Gumbel distribution is not correct'
assert sym.simplify(x_sol + beta*sym.log(sym.exp(-mu/beta)*sym.log(1/Prob_non_exc))) == 0, 'Error: inverted Gumbel distribution is not correct'
assert sym.simplify(f_gumbel - sym.exp(-((x-mu)/beta+sym.exp(-(x-mu)/beta)))/beta) == 0, 'Error: probability densily function is not correct'
assert np.allclose(np.array([0. , 0. , 0.05555556, 0.13333333, 0.21111111, 0.28888889, 0.36 , 0.36 , 0.40938272, 1. ]), sym.lambdify(x, F.rewrite(sym.Piecewise).simplify())(np.linspace(-1,6,10))), 'Error: Piecewise cumulative distribution function is not correct'
```
%% Cell type:markdown id: tags:
**End of notebook.**
<h2 style="height: 60px">
</h2>
<h3 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; bottom: 60px; right: 50px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<img alt="Creative Commons License" style="border-width:; width:88px; height:auto; padding-top:10px" src="https://i.creativecommons.org/l/by/4.0/88x31.png" />
</a>
<a rel="TU Delft" href="https://www.tudelft.nl/en/ceg">
<img alt="TU Delft" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png" />
</a>
<a rel="MUDE" href="http://mude.citg.tudelft.nl/">
<img alt="MUDE" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png" />
</a>
</h3>
<span style="font-size: 75%">
&copy; Copyright 2024 <a rel="MUDE" href="http://mude.citg.tudelft.nl/">MUDE</a> TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0 License</a>.
This diff is collapsed.
# PA 1.8: Sympy---your new best friend?
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 1.8. Due: before Friday, October 25th, 2024.*
_You can access this assignment with the following link: [classroom.github.com/a/SK4BFppe](https://classroom.github.com/a/SK4BFppe)._
- install environment
This PA consists of 3 parts:
sympy page
link to env page
1. Creating a new `conda` environment `mude-week-8` to use for the PA and Week 1.8 activities using the file `environment.yml` (instructions below).
2. Reading a [page in the textbook about the Python package Sympy](https://mude.citg.tudelft.nl/2024/book/external/learn-python/book/08/sympy.html).
3. `PA_1_8_Equations_Done_Symply.ipynb`: a notebook to practice using Sympy by repeating calculations from WS 1.7.
## Grading Criteria
You will pass this PA if:
1. Your notebook `PA_1_8_Equations_Done_Symply.ipynb` runs without errors.
You can verify that you passed by looking for the green circle in this repository (the last workflow run).
If your check is failing, view the Python traceback by going to the Actions tab, click the most recent workflow run, click the job (the box diagram) and expand and read the command line interface output.
## Creating a (new) `conda` environment
For the past weeks you have been using the `mude-base` environment that we made in Week 1.1...remember that?! However, now we need _new_ packages for next week. In addition,
If you forget what an environment is, [**read this page in the book from week 1!**](https://mude.citg.tudelft.nl/2024/book/external/learn-programming/book/environments.html).
_Why create a new environment and not just add the necessary packages to `mude-base`?_
This is not a good practice, especially if we are using the environments for specific purposes. In this case, the probability libraries we need next week rely on the C programming language to run efficiently, and when this happens it sometimes conflicts with other exiting packages in the environment. In addition, it is usually better to create an environment all at once, rather than adding packages sequentially over time.
Convinced? Let's make the environment, it's quite simple. Complete these steps and then you can test out the environment by using importing the Sympy package in the PA notebook for this week.
Step 1.
- open up a CLI on your computer (it is easiest to just do this in VS Code)
- execute the following command and read the output to make sure the process completes.
```
conda env create -f environment.yml
```
If you get an error, you may need to specify the location of `environment.yml`.
Step 2.
Once the environment is installed, check that it was completed by running this command:
```
conda env list
```
You should see `mude-week-8` in the list.
Step 3.
Use it! Select this environment when you open the notebook for PA 1.8.
The first cell should execute without error if you correctly installed the environment:
```
import sympy as sym
```
**End of file.**
<span style="font-size: 75%">
&copy; Copyright 2024 <a rel="MUDE" href="http://mude.citg.tudelft.nl/">MUDE</a>, TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0 License</a>.
\ No newline at end of file
name: mude-week-8
channels:
- conda-forge
- defaults
dependencies:
- python=3.12
- pyvinecopulib
- numpy
- scipy
- pandas
- matplotlib
- sympy
- seaborn
- scikit-learn
- threadpoolctl
- joblib
- jupyter
- ipykernel
- pip
- pip:
- bivariate
\ No newline at end of file
This diff is collapsed.
<!DOCTYPE html>
<html>
<head>
<title>README.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
body {
font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
font-size: var(--vscode-markdown-font-size, 14px);
padding: 0 26px;
line-height: var(--vscode-markdown-line-height, 22px);
word-wrap: break-word;
}
#code-csp-warning {
position: fixed;
top: 0;
right: 0;
color: white;
margin: 16px;
text-align: center;
font-size: 12px;
font-family: sans-serif;
background-color:#444444;
cursor: pointer;
padding: 6px;
box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}
#code-csp-warning:hover {
text-decoration: none;
background-color:#007acc;
box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}
body.scrollBeyondLastLine {
margin-bottom: calc(100vh - 22px);
}
body.showEditorSelection .code-line {
position: relative;
}
body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
content: "";
display: block;
position: absolute;
top: 0;
left: -12px;
height: 100%;
}
body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
left: -30px;
}
.vscode-light.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(0, 0, 0, 0.15);
}
.vscode-light.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(0, 0, 0, 0.40);
}
.vscode-light.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 255, 255, 0.4);
}
.vscode-dark.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 255, 255, 0.60);
}
.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-high-contrast.showEditorSelection .code-active-line:before {
border-left: 3px solid rgba(255, 160, 0, 0.7);
}
.vscode-high-contrast.showEditorSelection .code-line:hover:before {
border-left: 3px solid rgba(255, 160, 0, 1);
}
.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
img {
max-width: 100%;
max-height: 100%;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:focus,
input:focus,
select:focus,
textarea:focus {
outline: 1px solid -webkit-focus-ring-color;
outline-offset: -1px;
}
hr {
border: 0;
height: 2px;
border-bottom: 2px solid;
}
h1 {
padding-bottom: 0.3em;
line-height: 1.2;
border-bottom-width: 1px;
border-bottom-style: solid;
}
h1, h2, h3 {
font-weight: normal;
}
table {
border-collapse: collapse;
}
table > thead > tr > th {
text-align: left;
border-bottom: 1px solid;
}
table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
padding: 5px 10px;
}
table > tbody > tr + tr > td {
border-top: 1px solid;
}
blockquote {
margin: 0 7px 0 5px;
padding: 0 16px 0 10px;
border-left-width: 5px;
border-left-style: solid;
}
code {
font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
font-size: 1em;
line-height: 1.357em;
}
body.wordWrap pre {
white-space: pre-wrap;
}
pre:not(.hljs),
pre.hljs code > div {
padding: 16px;
border-radius: 3px;
overflow: auto;
}
pre code {
color: var(--vscode-editor-foreground);
tab-size: 4;
}
/** Theming */
.vscode-light pre {
background-color: rgba(220, 220, 220, 0.4);
}
.vscode-dark pre {
background-color: rgba(10, 10, 10, 0.4);
}
.vscode-high-contrast pre {
background-color: rgb(0, 0, 0);
}
.vscode-high-contrast h1 {
border-color: rgb(0, 0, 0);
}
.vscode-light table > thead > tr > th {
border-color: rgba(0, 0, 0, 0.69);
}
.vscode-dark table > thead > tr > th {
border-color: rgba(255, 255, 255, 0.69);
}
.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
border-color: rgba(0, 0, 0, 0.18);
}
.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
border-color: rgba(255, 255, 255, 0.18);
}
</style>
<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
color: #8e908c;
}
/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
color: #c82829;
}
/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
color: #f5871f;
}
/* Tomorrow Yellow */
.hljs-attribute {
color: #eab700;
}
/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
color: #718c00;
}
/* Tomorrow Blue */
.hljs-title,
.hljs-section {
color: #4271ae;
}
/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #8959a8;
}
.hljs {
display: block;
overflow-x: auto;
color: #4d4d4c;
padding: 0.5em;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>
<style>
/*
* Markdown PDF CSS
*/
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
padding: 0 12px;
}
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
border-radius: 3px;
overflow-x: auto;
white-space: pre-wrap;
overflow-wrap: break-word;
}
pre:not(.hljs) {
padding: 23px;
line-height: 19px;
}
blockquote {
background: rgba(127, 127, 127, 0.1);
border-color: rgba(0, 122, 204, 0.5);
}
.emoji {
height: 1.4em;
}
code {
font-size: 14px;
line-height: 19px;
}
/* for inline code */
:not(pre):not(.hljs) > code {
color: #C9AE75; /* Change the old color so it seems less like an error */
font-size: inherit;
}
/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
page-break-after: always;
}
</style>
<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
<script>
mermaid.initialize({
startOnLoad: true,
theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
? 'dark'
: 'default'
});
</script>
<h1 id="pa-18-sympy---your-new-best-friend">PA 1.8: Sympy---your new best friend?</h1>
<p><em><a href="http://mude.citg.tudelft.nl/">CEGM1000 MUDE</a>: Week 1.8. Due: before Friday, October 25th, 2024.</em></p>
<p><em>You can access this assignment with the following link: <a href="https://classroom.github.com/a/SK4BFppe">classroom.github.com/a/SK4BFppe</a>.</em></p>
<p>This PA consists of 3 parts:</p>
<ol>
<li>Creating a new <code>conda</code> environment <code>mude-week-8</code> to use for the PA and Week 1.8 activities using the file <code>environment.yml</code> (instructions below).</li>
<li>Reading a <a href="https://mude.citg.tudelft.nl/2024/book/external/learn-python/book/08/sympy.html">page in the textbook about the Python package Sympy</a>.</li>
<li><code>PA_1_8_Equations_Done_Symply.ipynb</code>: a notebook to practice using Sympy by repeating calculations from WS 1.7.</li>
</ol>
<h2 id="grading-criteria">Grading Criteria</h2>
<p>You will pass this PA if:</p>
<ol>
<li>Your notebook <code>PA_1_8_Equations_Done_Symply.ipynb</code> runs without errors.</li>
</ol>
<p>You can verify that you passed by looking for the green circle in this repository (the last workflow run).</p>
<p>If your check is failing, view the Python traceback by going to the Actions tab, click the most recent workflow run, click the job (the box diagram) and expand and read the command line interface output.</p>
<h2 id="creating-a-new-conda-environment">Creating a (new) <code>conda</code> environment</h2>
<p>For the past weeks you have been using the <code>mude-base</code> environment that we made in Week 1.1...remember that?! However, now we need <em>new</em> packages for next week. In addition,</p>
<p>If you forget what an environment is, <a href="https://mude.citg.tudelft.nl/2024/book/external/learn-programming/book/environments.html"><strong>read this page in the book from week 1!</strong></a>.</p>
<p><em>Why create a new environment and not just add the necessary packages to <code>mude-base</code>?</em></p>
<p>This is not a good practice, especially if we are using the environments for specific purposes. In this case, the probability libraries we need next week rely on the C programming language to run efficiently, and when this happens it sometimes conflicts with other exiting packages in the environment. In addition, it is usually better to create an environment all at once, rather than adding packages sequentially over time.</p>
<p>Convinced? Let's make the environment, it's quite simple. Complete these steps and then you can test out the environment by using importing the Sympy package in the PA notebook for this week.</p>
<p>Step 1.</p>
<ul>
<li>open up a CLI on your computer (it is easiest to just do this in VS Code)</li>
<li>execute the following command and read the output to make sure the process completes.</li>
</ul>
<pre class="hljs"><code><div>conda env create -f environment.yml
</div></code></pre>
<p>If you get an error, you may need to specify the location of <code>environment.yml</code>.</p>
<p>Step 2.</p>
<p>Once the environment is installed, check that it was completed by running this command:</p>
<pre class="hljs"><code><div>conda env list
</div></code></pre>
<p>You should see <code>mude-week-8</code> in the list.</p>
<p>Step 3.</p>
<p>Use it! Select this environment when you open the notebook for PA 1.8.</p>
<p>The first cell should execute without error if you correctly installed the environment:</p>
<pre class="hljs"><code><div>import sympy as sym
</div></code></pre>
<p><strong>End of file.</strong></p>
<span style="font-size: 75%">
&copy; Copyright 2024 <a rel="MUDE" href="http://mude.citg.tudelft.nl/">MUDE</a>, TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0 License</a>.
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment