Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate Exercise to the bottom: Scientific Computing #32

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 61 additions & 35 deletions src/scientific/applied_linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,9 @@ print("Dot product with @", x @ y)

````{exercise} 1
:nonumber:
:label: dir3-3-1

Alice is a stock broker who owns two types of assets: A and B. She owns 100
units of asset A and 50 units of asset B. The current interest rate is 5%.
Each of the A assets have a remaining duration of 6 years and pay
\$1500 each year, while each of the B assets have a remaining duration
of 4 years and pay \$500 each year. Alice would like to retire if she
can sell her assets for more than \$500,000. Use vector addition, scalar
multiplication, and dot products to determine whether she can retire.

See {ref}`exercise 1 <ex3-3-1>` in the exercise list.
````

```{code-cell} python
Expand Down Expand Up @@ -241,32 +235,15 @@ print(y1 @ x1)
Despite our options, we stick to using `@` because
it is simplest to read and write.


````{exercise} 2
:nonumber:
:label: dir3-3-2

Which of the following operations will work and which will
create errors because of size issues?

Test out your intuitions in the code cell below

```{code-block} python
x1 @ x2
x2 @ x1
x2 @ x3
x3 @ x2
x1 @ x3
x4 @ y1
x4 @ y2
y1 @ x4
y2 @ x4
```

```{code-block} python
# testing area
```

See {ref}`exercise 2 <ex3-3-2>` in the exercise list.
````


### Other Linear Algebra Concepts

#### Transpose
Expand Down Expand Up @@ -760,19 +737,68 @@ print(f"The distribution of workers is given by {dist}")

````{exercise} 3
:nonumber:
:label: dir3-3-3

Compare the distribution above to the final values of a long simulation.
See {ref}`exercise 3 <ex3-3-3>` in the exercise list.
````

If you multiply the distribution by 1,000,000 (the number of workers), do you get (roughly) the same number as the simulation?

## Exercises

````{exercise} 1
:nonumber:
:label: ex3-3-1

Alice is a stock broker who owns two types of assets: A and B. She owns 100
units of asset A and 50 units of asset B. The current interest rate is 5%.
Each of the A assets have a remaining duration of 6 years and pay
\$1500 each year, while each of the B assets have a remaining duration
of 4 years and pay \$500 each year. Alice would like to retire if she
can sell her assets for more than \$500,000. Use vector addition, scalar
multiplication, and dot products to determine whether she can retire.

({ref}`back to text <dir3-3-1>`)
````

````{exercise} 2
:nonumber:
:label: ex3-3-2

Which of the following operations will work and which will
create errors because of size issues?

Test out your intuitions in the code cell below

```{code-block} python
# your code here
x1 @ x2
x2 @ x1
x2 @ x3
x3 @ x2
x1 @ x3
x4 @ y1
x4 @ y2
y1 @ x4
y2 @ x4
```

```{code-block} python
# testing area
```

({ref}`back to text <dir3-3-2>`)
````

## Exercises
````{exercise} 3
:nonumber:
:label: ex3-3-3

````{exerciselist}
````
Compare the distribution above to the final values of a long simulation.

If you multiply the distribution by 1,000,000 (the number of workers), do you get (roughly) the same number as the simulation?

```{code-block} python
# your code here
```

({ref}`back to text <dir3-3-3>`)
````
157 changes: 102 additions & 55 deletions src/scientific/numpy_arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,20 @@ print(x_3d[0, 1, 0])

Success!


````{exercise} 1
:nonumber:
:label: dir3-1-1

Try indexing into another element of your choice from the
3-dimensional array.

Building an understanding of indexing means working through this
type of operation several times -- without skipping steps!

See {ref}`exercise 1 <ex3-1-1>` in the exercise list.
````


````{exercise} 2
:nonumber:
:label: dir3-1-2

Look at the 2-dimensional array `x_2d`.

Does the inner-most index correspond to rows or columns? What does the
outer-most index correspond to?

Write your thoughts.

See {ref}`exercise 2 <ex3-1-2>` in the exercise list.
````

We can also select multiple elements at a time -- this is called slicing.
Expand All @@ -237,13 +230,15 @@ print(x_3d[:, 0, 0:2])
print(x_3d[:, 0, :2]) # the 0 in 0:2 is optional
```


````{exercise} 3
:nonumber:
:label: dir3-1-3

What would you do to extract the array `[[5, 6], [50, 60]]`?

See {ref}`exercise 3 <ex3-1-3>` in the exercise list.
````


### Array Functionality

#### Array Properties
Expand Down Expand Up @@ -333,13 +328,12 @@ print("2 * x = ", 2 * x)
print("x / 2 = ", x / 2)
```


````{exercise} 4
:nonumber:
:label: dir3-1-4

Do you recall what multiplication by an integer did for lists?

How does this differ?

See {ref}`exercise 4 <ex3-1-4>` in the exercise list.
````

Operations between two arrays of the same size, in this case `(2, 2)`, simply apply the operation
Expand Down Expand Up @@ -408,44 +402,12 @@ z = np.array([1,2,3])
np.log(z) * z
```


````{exercise} 5
:nonumber:
:label: dir3-1-5

Let's revisit a bond pricing example we saw in {doc}`Control flow <../python_fundamentals/control_flow>`.

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

$$
\begin{align*}
P &= \left(\sum_{n=1}^N \frac{C}{(i+1)^n}\right) + \frac{M}{(1+i)^N} \\
&= C \left(\frac{1 - (1+i)^{-N}}{i} \right) + M(1+i)^{-N}
\end{align*}
$$

In the code cell below, we have defined variables for `i`, `M` and `C`.

You have two tasks:

1. Define a numpy array `N` that contains all maturities between 1 and 10

```{hint}
look at the `np.arange` function.
```

1. Using the equation above, determine the bond prices of all maturity levels in your array.

```{code-block} python
i = 0.03
M = 100
C = 5

# Define array here

# price bonds here
```

See {ref}`exercise 5 <ex3-1-5>` in the exercise list.
````

### Other Useful Array Operations
Expand Down Expand Up @@ -523,6 +485,91 @@ When speed matters, directly write a `f` function to work on arrays.

## Exercises

````{exerciselist}
````{exercise} 1
:nonumber:
:label: ex3-1-1

Try indexing into another element of your choice from the
3-dimensional array.

Building an understanding of indexing means working through this
type of operation several times -- without skipping steps!

({ref}`back to text <dir3-1-1>`)
````

````{exercise} 2
:nonumber:
:label: ex3-1-2

Look at the 2-dimensional array `x_2d`.

Does the inner-most index correspond to rows or columns? What does the
outer-most index correspond to?

Write your thoughts.

({ref}`back to text <dir3-1-2>`)
````

````{exercise} 3
:nonumber:
:label: ex3-1-3

What would you do to extract the array `[[5, 6], [50, 60]]`?

({ref}`back to text <dir3-1-3>`)
````

````{exercise} 4
:nonumber:
:label: ex3-1-4

Do you recall what multiplication by an integer did for lists?

How does this differ?

({ref}`back to text <dir3-1-4>`)
````

````{exercise} 5
:nonumber:
:label: ex3-1-5

Let's revisit a bond pricing example we saw in {doc}`Control flow <../python_fundamentals/control_flow>`.

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

$$
\begin{align*}
P &= \left(\sum_{n=1}^N \frac{C}{(i+1)^n}\right) + \frac{M}{(1+i)^N} \\
&= C \left(\frac{1 - (1+i)^{-N}}{i} \right) + M(1+i)^{-N}
\end{align*}
$$

In the code cell below, we have defined variables for `i`, `M` and `C`.

You have two tasks:

1. Define a numpy array `N` that contains all maturities between 1 and 10

```{hint}
look at the `np.arange` function.
```

1. Using the equation above, determine the bond prices of all maturity levels in your array.

```{code-block} python
i = 0.03
M = 100
C = 5

# Define array here

# price bonds here
```

({ref}`back to text <dir3-1-5>`)
````
Loading