Skip to content
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
15 changes: 11 additions & 4 deletions lectures/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ Try to use lambda expressions to define the function `f`.
:class: dropdown
```

- Here's one solution for part 1
Here's one solution for part 1

```{code-cell} python3
def factorial(n):
Expand All @@ -525,7 +525,7 @@ def factorial(n):
factorial(4)
```

- Adding the lambda expression
Adding the lambda expression

```{code-cell} python3
def factorial(n,f = lambda x: x):
Expand Down Expand Up @@ -561,14 +561,21 @@ The [binomial random variable](https://en.wikipedia.org/wiki/Binomial_distributi
Without any import besides `from numpy.random import uniform`, write a function
`binomial_rv` such that `binomial_rv(n, p)` generates one draw of $Y$.

Hint: If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` evaluates to `True` with probability $p$.
```{hint}
:class: dropdown

If $U$ is uniform on $(0, 1)$ and $p \in (0,1)$, then the expression `U < p` evaluates to `True` with probability $p$.
```

```{exercise-end}
```


```{solution-start} func_ex2
:class: dropdown
````
```

Here is one solution:

```{code-cell} python3
from numpy.random import uniform
Expand Down
4 changes: 3 additions & 1 deletion lectures/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ When you're ready to execute the code in a cell, hit `Shift-Enter` instead of th
:figclass: auto
```

(Note: There are also menu and button options for running code in a cell that you can find by exploring)
```{note}
There are also menu and button options for running code in a cell that you can find by exploring.
```

#### Modal Editing

Expand Down
8 changes: 6 additions & 2 deletions lectures/numba.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ When Numba compiles machine code for functions, it treats global variables as co
```{exercise}
:label: speed_ex1

{ref}`Previously <pbe_ex3>` we considered how to approximate $\pi$ by
{ref}`Previously <pbe_ex5>` we considered how to approximate $\pi$ by
Monte Carlo.

Use the same idea here, but make the code efficient using Numba.
Expand Down Expand Up @@ -560,11 +560,15 @@ To test your code, evaluate the fraction of time that the chain spends in the lo

If your code is correct, it should be about 2/3.

Hints:

```{hint}
:class: dropdown

* Represent the low state as 0 and the high state as 1.
* If you want to store integers in a NumPy array and then apply JIT compilation, use `x = np.empty(n, dtype=np.int_)`.

```

```{exercise-end}
```

Expand Down
13 changes: 10 additions & 3 deletions lectures/numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,10 @@ Now write a new function that does the same job, but uses NumPy arrays and array

(Such functionality is already implemented as `np.poly1d`, but for the sake of the exercise don't use this class)

* Hint: Use `np.cumprod()`

```{hint}
:class: dropdown
Use `np.cumprod()`
```
```{exercise-end}
```

Expand Down Expand Up @@ -1262,7 +1264,12 @@ It helps to sketch the intervals on paper.

Your exercise is to speed it up using NumPy, avoiding explicit loops

* Hint: Use `np.searchsorted` and `np.cumsum`
```{hint}
:class: dropdown

Use `np.searchsorted` and `np.cumsum`

```

If you can, implement the functionality as a class called `DiscreteRV`, where

Expand Down
4 changes: 3 additions & 1 deletion lectures/pandas.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,12 @@ A trivial example is to return itself for each row in the dataframe
df.apply(lambda row: row, axis=1)
```

Note: for the `.apply()` method
```{note}
For the `.apply()` method
- axis = 0 -- apply function to each column (variables)
- axis = 1 -- apply function to each row (observations)
- axis = 0 is the default parameter
```

We can use it together with `.loc[]` to do some more advanced selection.

Expand Down
33 changes: 24 additions & 9 deletions lectures/python_essentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,10 @@ all([1 <= 2 <= 3, "a" in "letter"])
any([1 <= 2 <= 3, "a" in "letter"])
```

Note:

```{note}
* `all()` returns `True` when *all* boolean values/expressions in the sequence are `True`
* `any()` returns `True` when *any* boolean values/expressions in the sequence are `True`

```

## Coding Style and Documentation

Expand Down Expand Up @@ -692,20 +691,28 @@ Solve the following exercises.

(For some, the built-in function `sum()` comes in handy).

```{exercise}
```{exercise-start}
:label: pyess_ex1

```
Part 1: Given two numeric lists or tuples `x_vals` and `y_vals` of equal length, compute
their inner product using `zip()`.

Part 2: In one line, count the number of even numbers in 0,...,99.

* Hint: `x % 2` returns 0 if `x` is even, 1 otherwise.

Part 3: Given `pairs = ((2, 5), (4, 2), (9, 8), (12, 10))`, count the number of pairs `(a, b)`
such that both `a` and `b` are even.

```{hint}
:class: dropdown

`x % 2` returns 0 if `x` is even, 1 otherwise.

```

```{exercise-end}
```


```{solution-start} pyess_ex1
:class: dropdown
```
Expand Down Expand Up @@ -804,12 +811,20 @@ p(1, (2, 4))
```


```{exercise}
```{exercise-start}
:label: pyess_ex3
```

Write a function that takes a string as an argument and returns the number of capital letters in the string.

Hint: `'foo'.upper()` returns `'FOO'`.
```{hint}
:class: dropdown

`'foo'.upper()` returns `'FOO'`.

```

```{exercise-end}
```

```{solution-start} pyess_ex3
Expand Down