GitHub

@@ -19,14 +19,14 @@ kernelspec:

19192020

# Kesten Processes and Firm Dynamics

212122-

```{admonition} GPU in use

22+

```{admonition} GPU

2323

:class: warning

242425-

This lecture is accelerated via [hardware](status:machine-details) that has access to a GPU and JAX for GPU programming.

25+

This lecture is accelerated via [hardware](status:machine-details) that has access to a GPU and JAX for GPU programming.

26262727

Free GPUs are available on Google Colab. To use this option, please click on the play icon top right, select Colab, and set the runtime environment to include a GPU.

282829-

Alternatively, if you have your own GPU, you can follow the [instructions](https://github.com/google/jax) for installing JAX with GPU support.

29+

Alternatively, if you have your own GPU, you can follow the [instructions](https://github.com/google/jax) for installing JAX with GPU support. If you would like to install jax running on the `cpu` only you can use `pip install jax[cpu]`

3030

```

31313232

```{index} single: Linear State Space Models

@@ -44,9 +44,6 @@ tags: [hide-output]

4444

---

4545

!pip install quantecon

4646

!pip install --upgrade yfinance

47-

# If your machine has CUDA support, please follow the guide in GPU Warning.

48-

# Otherwise, run the line below:

49-

!pip install --upgrade "jax[CPU]"

5047

```

51485249

## Overview

@@ -686,7 +683,7 @@ s_init = 1.0 # initial condition for each firm

686683

:class: dropdown

687684

```

688685689-

Here's one solution in [JAX](https://python-programming.quantecon.org/jax_intro.html).

686+

Here's one solution in [JAX](https://python-programming.quantecon.org/jax_intro.html).

690687691688

First let's import the necessary modules and check the backend for JAX

692689

@@ -731,10 +728,10 @@ def generate_draws(μ_a=-0.5,

731728

exp_a = jnp.exp(a_random[t, :])

732729

exp_b = jnp.exp(b_random[t, :])

733730

exp_e = jnp.exp(e_random[t, :])

734-

s = s.at[:, t+1].set(jnp.where(s[:, t] < s_bar,

731+

s = s.at[:, t+1].set(jnp.where(s[:, t] < s_bar,

735732

exp_e,

736733

exp_a * s[:, t] + exp_b))

737-734+738735

return s[:, -1]

739736740737

%time data = generate_draws().block_until_ready()

@@ -761,7 +758,7 @@ plt.show()

761758762759

The plot produces a straight line, consistent with a Pareto tail.

763760764-

It is possible to further speed up our code by replacing the `for` loop with [`lax.scan`](https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.scan.html)

761+

It is possible to further speed up our code by replacing the `for` loop with [`lax.scan`](https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.scan.html)

765762

to reduce the loop overhead in the compilation of the jitted function

766763767764

```{code-cell} ipython3

@@ -779,10 +776,10 @@ def generate_draws_lax(μ_a=-0.5,

779776

M=1_000_000,

780777

s_init=1.0,

781778

seed=123):

782-779+783780

key = random.PRNGKey(seed)

784781

keys = random.split(key, 3)

785-782+786783

# Generate random draws and initial values

787784

a_random = μ_a + σ_a * random.normal(keys[0], (T, M))

788785

b_random = μ_b + σ_b * random.normal(keys[1], (T, M))

@@ -792,11 +789,11 @@ def generate_draws_lax(μ_a=-0.5,

792789

# Define the function for each update

793790

def update_s(s, a_b_e_draws):

794791

a, b, e = a_b_e_draws

795-

res = jnp.where(s < s_bar,

796-

jnp.exp(e),

792+

res = jnp.where(s < s_bar,

793+

jnp.exp(e),

797794

jnp.exp(a) * s + jnp.exp(b))

798795

return res, res

799-796+800797

# Use lax.scan to perform the calculations on all states

801798

s_final, _ = lax.scan(update_s, s, (a_random, b_random, e_random))

802799

return s_final

@@ -877,4 +874,4 @@ plt.show()

877874

```

878875879876

```{solution-end}

880-

```

877+

```

Read the original on github.com ↗