Skip to content

Commit

Permalink
finished exercises for intro to numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
beauvilerobed committed Jan 24, 2025
1 parent ca55ecc commit 0b3c8d6
Showing 1 changed file with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,179 @@
"# Exercises"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_3d: \n",
" [[[ 1 2 3]\n",
" [ 4 5 6]]\n",
"\n",
" [[10 20 30]\n",
" [40 50 60]]]\n",
"\n",
"x_3d[:, 1] \n",
" [[ 4 5 6]\n",
" [40 50 60]]\n",
"\n",
"x_3d[:, 0, :1]: \n",
" [[ 1]\n",
" [10]]\n",
"\n",
"x_3d[1, 1, 2:]: \n",
" [60]\n",
"\n"
]
}
],
"source": [
"# 1. Try indexing into another element of your choice from the 3-dimensional array.\n",
"# \n",
"# Building an understanding of indexing means working through this type of operation \n",
"# several times – without skipping steps!\n",
"\n",
"\n",
"x_3d_list = [[[1, 2, 3], [4, 5, 6]], \n",
" [[10, 20, 30], [40, 50, 60]]]\n",
"x_3d = np.array(x_3d_list)\n",
"\n",
"print(f\"x_3d: \\n {x_3d}\\n\")\n",
"print(f\"x_3d[:, 1] \\n {x_3d[:, 1]}\\n\")\n",
"print(f\"x_3d[:, 0, :1]: \\n {x_3d[:, 0, :1]}\\n\")\n",
"print(f\"x_3d[1, 1, 2:]: \\n {x_3d[1, 1, 2:]}\\n\") # the 0 in 0:2 is optional"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"inner is the column, example column 1 is [2 5 8]\n",
"outer is the row, example row 1 is [4 5 6]\n"
]
}
],
"source": [
"# 2. Look at the 2-dimensional array x_2d.\n",
"\n",
"x_2d = np.array([[1, 2, 3], \n",
" [4, 5, 6], \n",
" [7, 8, 9]])\n",
"print(x_2d)\n",
"\n",
"# Does the inner-most index correspond to rows or columns? \n",
"# What does the outer-most index correspond to? \n",
"\n",
"# Write your thoughts.\n",
"column_number = 1\n",
"row_number = 1\n",
"\n",
"inner = x_2d[:,column_number]\n",
"outer = x_2d[1]\n",
"\n",
"print(f'inner is the column, example column 1 is {inner}')\n",
"print(f'outer is the row, example row 1 is {outer}')\n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 5 6]\n",
" [50 60]]\n"
]
}
],
"source": [
"# 3. What would you do to extract the array [[5, 6], [50, 60]] from \n",
"# [[[1, 2, 3], [4, 5, 6]], [[10, 20, 30], [40, 50, 60]]]?\n",
"\n",
"arr = [[[1, 2, 3], [4, 5, 6]], \n",
" [[10, 20, 30], [40, 50, 60]]]\n",
"\n",
"arr = np.array(arr)\n",
"\n",
"e_arr = arr[:,1,1:]\n",
"print(e_arr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s revisit a bond pricing example we saw in Control flow.\n",
"\n",
"Recall that the equation for pricing a bond with coupon payment $C$, face value $M$, yield to maturity $i$, and periods to maturity $N$ is\n",
"\n",
"$$\n",
"\\begin{align}\n",
" P &= \\left(\\sum_{n=1}^N \\frac{C}{(i+1)^n}\\right) + \\frac{M}{(1+i)^N} \\\\\n",
" &= C \\left(\\frac{1 - (1+i)^{-N}}{i} \\right) + M(1+i)^{-N}\n",
"\\end{align}\n",
"$$\n",
"\n",
"In the code cell below, we have defined variables for `i`, `M` and `C`.\n",
"1. You have two tasks:\n",
"1. Define a numpy array N that contains all maturities between 1 and 10"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array calculation: price of our bound is $117.06\n",
"direct calculation: price of our bound is $117.06\n"
]
}
],
"source": [
"i = 0.03\n",
"M = 100\n",
"C = 5\n",
"\n",
"# Define array here\n",
"x = np.arange(1,11)\n",
"N = len(x)\n",
"# price bonds here\n",
"face_val_discount = M/(1+i)**N\n",
"discounts = C/(i+1)**x\n",
"P = discounts.sum() + face_val_discount\n",
"P = round(P,2)\n",
"\n",
"P_direct = C*(1-(1+i)**(-N))/i + face_val_discount\n",
"P_direct = round(P_direct, 2)\n",
"\n",
"print(f'array calculation: price of our bound is ${P}')\n",
"print(f'direct calculation: price of our bound is ${P_direct}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
Expand Down

0 comments on commit 0b3c8d6

Please sign in to comment.