Skip to content

Commit

Permalink
Merge pull request #26 from QuantEcon/smit_8
Browse files Browse the repository at this point in the history
Modify exercise 2
  • Loading branch information
mmcky authored Jul 4, 2023
2 parents 2597cd3 + e83d987 commit 7e0bccf
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 199 deletions.
150 changes: 83 additions & 67 deletions day-02/exercise_set_2.ipynb
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear Algebra and Probability Distributions Exercises"
"# Python and Linear Algebra Exercises"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\"><button><a href=\"https://colab.research.google.com/github/QuantEcon/workshop.africa-july2023/blob/main/day-02/exercise_set_2.ipynb\"><img src=\"\" heght=\"10px\"/><img\n",
"<div align=\"right\"><button><a href=\"https://colab.research.google.com/github/QuantEcon/workshop.africa-july2023/blob/main/day-02/exercise_set_2_with_solution.ipynb\"><img src=\"\" heght=\"10px\"/><img\n",
" src=\"https://colab.research.google.com/assets/colab-badge.svg\"\n",
" alt=\"open with Colab\" width=\"100px\"/></a></button></div>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Written for the QuantEcon Africa Workshop (July 2023)\n",
"#### Author: [John Stachurski](http://johnstachurski.net/) and [Hengcheng Zhang](https://github.com/HengchengZhang)"
"#### Author: [John Stachurski](http://johnstachurski.net/) , [Hengcheng Zhang](https://github.com/HengchengZhang), and [Smit Lunagariya](https://github.com/Smit-create)"
]
},
{
Expand All @@ -40,23 +37,87 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1\n",
"\n",
"Complete the following function that returns the number of odd elements in the given list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_odd_num_from_list(nums):\n",
" # TODO: Finish this function\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Test the solution\n",
"try:\n",
" assert get_odd_num_from_list([1, 2, 3]) == 2\n",
" assert get_odd_num_from_list(list(range(1, 20))) == 10\n",
" assert get_odd_num_from_list(list(range(0, 20, 2))) == 0\n",
" print(\"Congratulations!\")\n",
"except:\n",
" print(\"Wrong answer, please check your code again.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2\n",
"\n",
"Consider the following time-series\n",
"\n",
"$$\n",
" a_{t+1} = (0.5 + r)a_t - br^2a_t\n",
"$$\n",
"\n",
"Assume that $r=0.05$, $b=100$ and $a_0=1.5$.\n",
"\n",
"Generate and plot the sequence $a_0, a_1, ..., a_T$.\n",
"\n",
"Use NumPy arrays to store the sequence $a$.\n",
"\n",
"Use $T=25$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Write your solution here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3\n",
"\n",
"Warm up on NumPy Basics.\n",
"\n",
"Feel free to refer to the [numpy documentation](https://numpy.org/doc/stable/reference/index.html) for API."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 1.1**: Write a function that creates numpy array from the given list/tuple."
"**Exercise 3.1**: Write a function that creates numpy array from the given list/tuple."
]
},
{
Expand Down Expand Up @@ -86,11 +147,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 1.2**: Write a function to return the transpose of the given matrix."
"**Exercise 3.2**: Write a function to return the transpose of the given matrix."
]
},
{
Expand Down Expand Up @@ -119,11 +179,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 1.3**: Write a function to return the inverse of the given matrix."
"**Exercise 3.3**: Write a function to return the inverse of the given matrix."
]
},
{
Expand Down Expand Up @@ -154,11 +213,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 1.4**: Write a function to compute the norm of the vector."
"**Exercise 3.4**: Write a function to compute the norm of the vector."
]
},
{
Expand Down Expand Up @@ -187,11 +245,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 1.5**: Write a function that returns the standardized vector of the given vector such that the mean is $0$ and standard deviation is $1$. Mathematically:\n",
"**Exercise 3.5**: Write a function that returns the standardized vector of the given vector such that the mean is $0$ and standard deviation is $1$. Mathematically:\n",
"$$\n",
"\\forall x_{i}\\in x:\\hspace{1em}x_{i}^{\\prime}=\\frac{x_{i}-\\mu}{\\sigma}\n",
"$$"
Expand Down Expand Up @@ -226,19 +283,17 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2"
"### Exercise 4"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise 2.1**: Write a function that takes two numpy vectors as an argument and returns `True` if they are orthogonal else returns `False`.\n",
"**Exercise 4.1**: Write a function that takes two numpy vectors as an argument and returns `True` if they are orthogonal else returns `False`.\n",
"\n",
"*Note: Two vectors are called orthogonal if their inner product is zero.*"
]
Expand Down Expand Up @@ -270,11 +325,10 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2.2**: Write a function to check whether the given matrix is symmetric or not."
"**Exercise 4.2**: Write a function to check whether the given matrix is symmetric or not."
]
},
{
Expand All @@ -283,7 +337,7 @@
"metadata": {},
"outputs": [],
"source": [
"def is_symmetric(x, y):\n",
"def is_symmetric(x):\n",
" # TODO: Finish this function\n",
" pass"
]
Expand All @@ -305,23 +359,20 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3\n"
"### Exercise 5"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Please refer to this [quantecon lecture](https://intro.quantecon.org/time_series_with_matrices.html) before solving this exercise."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -330,14 +381,15 @@
"For $ t = 1, 2, 3, \\ldots, T $ suppose that\n",
"\n",
"\n",
"\n",
"$$\n",
"y_{t} = \\alpha_{0} + \\alpha_{1} y_{t-1}\n",
"$$\n",
"\n",
"where we assume that $ y_0 = 1$, $\\alpha_{0}=5$, and $\\alpha_{1}=1.2$.\n",
"\n",
"\n",
"**Exercise 3.1**: Use the matrix algebra to solve the above time series equation, and plot the solution.\n",
"Use the matrix algebra to solve the above time series equation, and plot the solution.\n",
"\n",
"*Hint:*\n",
"\n",
Expand Down Expand Up @@ -404,43 +456,7 @@
"metadata": {},
"outputs": [],
"source": [
"# TODO: Write your code here"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate 100000 data points from the [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution) with density\n",
"\n",
"$$\n",
"f(x; \\alpha) = \\alpha \\exp(-\\alpha x)\n",
"\\qquad\n",
"(x > 0, \\alpha > 0)\n",
"$$\n",
"\n",
"taking $\\alpha = 0.5$. Then\n",
"\n",
"1. Plot a histogram of your sample and compare it to the density of the exponential distribution.\n",
"2. After looking up the maximum likelihood estimator of $\\alpha$, compute the estimate given your data and check that it is in fact close to $\\alpha$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Put your solution here"
"# TODO: Write your solution here"
]
}
],
Expand All @@ -460,7 +476,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.10.11"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 7e0bccf

Please sign in to comment.