Skip to content

Commit 91a35c2

Browse files
committed
updates
1 parent 434ccfe commit 91a35c2

File tree

6 files changed

+507
-85
lines changed

6 files changed

+507
-85
lines changed

lectures/divergence_measures.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -505,23 +505,6 @@ def plot_dist_diff(para_grid):
505505
return divergence_data
506506
507507
divergence_data = plot_dist_diff(param_grid)
508-
509-
from pandas.plotting import parallel_coordinates
510-
kl_gf_values = [float(result['KL(g, f)']) for result in results]
511-
512-
df_plot = pd.DataFrame({
513-
"KL(f,g)": kl_fg_values,
514-
"KL(g,f)": kl_gf_values,
515-
"JS": js_values,
516-
"Chernoff": chernoff_values
517-
})
518-
df_plot["pair"] = df_plot.index.astype(str) # just to group lines
519-
520-
plt.figure(figsize=(8,5))
521-
parallel_coordinates(df_plot, "pair", color="blue", alpha=0.3)
522-
plt.ylabel("Value")
523-
plt.title("Parallel comparison of divergence measures per pair")
524-
plt.show()
525508
```
526509
527510
## KL divergence and maximum-likelihood estimation

lectures/imp_sample.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import matplotlib.pyplot as plt
3636
from math import gamma
3737
```
3838

39-
## Mathematical Expectation of Likelihood Ratio
39+
## Mathematical expectation of likelihood ratio
4040

4141
In {doc}`this lecture <likelihood_ratio_process>`, we studied a likelihood ratio $\ell \left(\omega_t\right)$
4242

@@ -57,11 +57,10 @@ $$
5757
Our goal is to approximate the mathematical expectation $E \left[ L\left(\omega^t\right) \right]$ well.
5858

5959
In {doc}`this lecture <likelihood_ratio_process>`, we showed that $E \left[ L\left(\omega^t\right) \right]$ equals $1$ for all $t$.
60+
6061
We want to check out how well this holds if we replace $E$ by with sample averages from simulations.
6162

62-
This turns out to be easier said than done because for
63-
Beta distributions assumed above, $L\left(\omega^t\right)$ has
64-
a very skewed distribution with a very long tail as $t \rightarrow \infty$.
63+
This turns out to be easier said than done because for Beta distributions assumed above, $L\left(\omega^t\right)$ has a very skewed distribution with a very long tail as $t \rightarrow \infty$.
6564

6665
This property makes it difficult efficiently and accurately to estimate the mean by standard Monte Carlo simulation methods.
6766

@@ -156,7 +155,7 @@ $$
156155
E^g\left[\ell\left(\omega\right)\right] = \int_\Omega \ell(\omega) g(\omega) d\omega = \int_\Omega \ell(\omega) \frac{g(\omega)}{h(\omega)} h(\omega) d\omega = E^h\left[\ell\left(\omega\right) \frac{g(\omega)}{h(\omega)}\right]
157156
$$
158157

159-
## Selecting a Sampling Distribution
158+
## Selecting a sampling distribution
160159

161160
Since we must use an $h$ that has larger mass in parts of the distribution to which $g$ puts low mass, we use $h=Beta(0.5, 0.5)$ as our importance distribution.
162161

@@ -178,7 +177,7 @@ plt.ylim([0., 3.])
178177
plt.show()
179178
```
180179

181-
## Approximating a Cumulative Likelihood Ratio
180+
## Approximating a cumulative likelihood ratio
182181

183182
We now study how to use importance sampling to approximate
184183
${E} \left[L(\omega^t)\right] = \left[\prod_{i=1}^T \ell \left(\omega_i\right)\right]$.
@@ -252,13 +251,13 @@ The Monte Carlo method underestimates because the likelihood ratio $L(\omega^T)
252251

253252
Most samples from $g$ produce small likelihood ratios, while the true mean requires occasional very large values that are rarely sampled.
254253

255-
In our case, since $g(\omega) \to 0$ as $\omega \to 0$ while $f(\omega)$ remains bounded, the Monte Carlo procedure undersamples precisely where the likelihood ratio $\frac{f(\omega)}{g(\omega)}$ is largest.
254+
In our case, since $g(\omega) \to 0$ as $\omega \to 0$ while $f(\omega)$ remains constant, the Monte Carlo procedure undersamples precisely where the likelihood ratio $\frac{f(\omega)}{g(\omega)}$ is largest.
256255

257256
As $T$ increases, this problem worsens exponentially, making standard Monte Carlo increasingly unreliable.
258257

259258
Importance sampling with $q = h$ fixes this by sampling more uniformly from regions important to both $f$ and $g$.
260259

261-
## Distribution of Sample Mean
260+
## Distribution of sample mean
262261

263262
We next study the bias and efficiency of the Monte Carlo and importance sampling approaches.
264263

@@ -333,17 +332,15 @@ The simulation exercises above show that the importance sampling estimates are u
333332

334333
Evidently, the bias increases with increases in $T$.
335334

336-
## Choosing a Sampling Distribution
335+
## Choosing a sampling distribution
337336

338337
+++
339338

340339
Above, we arbitraily chose $h = Beta(0.5,0.5)$ as the importance distribution.
341340

342341
Is there an optimal importance distribution?
343342

344-
In our particular case, since we know in advance that $E_0 \left[ L\left(\omega^t\right) \right] = 1$.
345-
346-
We can use that knowledge to our advantage.
343+
In our particular case, since we know in advance that $E_0 \left[ L\left(\omega^t\right) \right] = 1$, we can use that knowledge to our advantage.
347344

348345
Thus, suppose that we simply use $h = f$.
349346

lectures/likelihood_ratio_process.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def plot_likelihood_paths(l_seq, title="Likelihood ratio paths",
194194
```
195195

196196
(nature_likeli)=
197-
## Nature Permanently Draws from Density g
197+
## Nature permanently draws from density g
198198

199199
We first simulate the likelihood ratio process when nature permanently
200200
draws from $g$.
@@ -264,7 +264,7 @@ Mathematical induction implies
264264
$E\left[L\left(w^{t}\right)\bigm|q=g\right]=1$ for all
265265
$t \geq 1$.
266266

267-
## Peculiar Property
267+
## Peculiar property
268268

269269
How can $E\left[L\left(w^{t}\right)\bigm|q=g\right]=1$ possibly be true when most probability mass of the likelihood
270270
ratio process is piling up near $0$ as
@@ -300,7 +300,7 @@ We explain the problem in more detail in {doc}`this lecture <imp_sample>`.
300300
There we describe an alternative way to compute the mean of a likelihood ratio by computing the mean of a _different_ random variable by sampling from a _different_ probability distribution.
301301

302302

303-
## Nature Permanently Draws from Density f
303+
## Nature permanently draws from density f
304304

305305
Now suppose that before time $0$ nature permanently decided to draw repeatedly from density $f$.
306306

@@ -348,7 +348,7 @@ plt.plot(range(T), np.sum(l_seq_f > 10000, axis=0) / N)
348348
plt.show()
349349
```
350350

351-
## Likelihood Ratio Test
351+
## Likelihood ratio test
352352

353353
We now describe how to employ the machinery
354354
of Neyman and Pearson {cite}`Neyman_Pearson` to test the hypothesis that history $w^t$ is generated by repeated
@@ -959,7 +959,7 @@ $$
959959
p(\textrm{wrong decision}) = {1 \over 2} (\alpha_T + \beta_T) .
960960
$$ (eq:detectionerrorprob)
961961
962-
Now let's simulate timing protocol 1 and 2 and compute the error probabilities
962+
Now let's simulate timing protocol 1 and compute the error probabilities
963963
964964
```{code-cell} ipython3
965965
@@ -1068,6 +1068,8 @@ $$ (eq:classerrorprob)
10681068
10691069
where $\tilde \alpha_t = {\rm Prob}(l_t < 1 \mid f)$ and $\tilde \beta_t = {\rm Prob}(l_t \geq 1 \mid g)$.
10701070
1071+
Now let's write some code to simulate it
1072+
10711073
```{code-cell} ipython3
10721074
def compute_protocol_2_errors(π_minus_1, T_max, N_simulations, f_func, g_func,
10731075
F_params=(1, 1), G_params=(3, 1.2)):
@@ -1712,7 +1714,7 @@ P_g = np.array([[0.5, 0.3, 0.2],
17121714
markov_results = analyze_markov_chains(P_f, P_g)
17131715
```
17141716
1715-
## Related Lectures
1717+
## Related lectures
17161718
17171719
Likelihood processes play an important role in Bayesian learning, as described in {doc}`likelihood_bayes`
17181720
and as applied in {doc}`odu`.

lectures/likelihood_ratio_process_2.md

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ from math import gamma
6565
from scipy.integrate import quad
6666
```
6767

68-
## Review: Likelihood Ratio Processes
68+
## Review: likelihood ratio processes
6969

7070
We'll begin by reminding ourselves definitions and properties of likelihood ratio processes.
7171

@@ -166,7 +166,7 @@ def simulate(a, b, T=50, N=500):
166166
return l_arr
167167
```
168168

169-
## Blume and Easley's Setting
169+
## Blume and Easley's setting
170170

171171
Let the random variable $s_t \in (0,1)$ at time $t =0, 1, 2, \ldots$ be distributed according to the same Beta distribution with parameters
172172
$\theta = \{\theta_1, \theta_2\}$.
@@ -195,7 +195,7 @@ $$c^1(s_t) = y_t^1 = s_t. $$
195195

196196
But in our model, agent 1 is not alone.
197197

198-
## Nature and Agents' Beliefs
198+
## Nature and agents' beliefs
199199

200200
Nature draws i.i.d. sequences $\{s_t\}_{t=0}^\infty$ from $\pi_t(s^t)$.
201201

@@ -239,7 +239,7 @@ $$
239239
c_t^1 + c_t^2 = 1 .
240240
$$
241241

242-
## A Socialist Risk-Sharing Arrangement
242+
## A socialist risk-sharing arrangement
243243

244244
In order to share risks, a benevolent social planner dictates a history-dependent consumption allocation that takes the form of a sequence of functions
245245

@@ -284,7 +284,7 @@ $$ (eq:objectiveagenti)
284284
where $\delta \in (0,1)$ is an intertemporal discount factor, and $u(\cdot)$ is a strictly increasing, concave one-period utility function.
285285
286286
287-
## Social Planner's Allocation Problem
287+
## Social planner's allocation problem
288288
289289
The benevolent dictator has all the information it requires to choose a consumption allocation that maximizes the social welfare criterion
290290
@@ -305,45 +305,12 @@ This means that the social planner knows and respects
305305
306306
Consequently, we anticipate that these objects will appear in the social planner's rule for allocating the aggregate endowment each period.
307307
308-
The Lagrangian for the social planner's problem is
309-
310-
$$
311-
L = \sum_{t=0}^{\infty}\sum_{s^t} \{ \lambda \delta^t u(c_t^1(s^t)) \pi_t^1(s^t) + (1-\lambda) \delta^t u(c_t^2(s^t)) \pi_t^2(s^t) + \theta_t(s^t)(1-c_t^1(s^t)-c_t^2(s^t)) \}
312-
$$
313-
314-
where $\theta_t(s^t)$ are the shadow prices.
315-
316-
The first order conditions for maximizing $L$ with respect to $c_t^i(s^t)$ are:
317-
318-
$$
319-
\lambda \delta^t u'(c_t^1(s^t)) \pi_t^1(s^t) = \theta_t(s^t), \quad (1-\lambda) \delta^t u'(c_t^2(s^t)) \pi_t^2(s^t) = \theta_t(s^t)
320-
$$
321-
322-
Substituting formula {eq}`eq:allocationrule1` for $c_t^1(s^t)$, we get
323-
324-
$$
325-
\theta_t(s^t) = \delta^t [(1-\lambda)\pi_t^2(s^t) + \lambda \pi_t^1(s^t)]
326-
$$
327-
328-
Now for the competitive equilibrium, notice that if we take $\mu_1 = \frac{1}{\lambda}$ and $\mu_2 = \frac{1}{1-\lambda}$, formula {eq}`eq:allocationce` agrees with formula {eq}`eq:allocationrule1`, and we get from {eq}`eq:priceequation1`
329-
330-
$$
331-
p_t(s^t) = \delta^t \lambda \pi_t^1(s^t) \frac{1-\lambda + \lambda l_t(s^t)}{\lambda l_t(s^t)} = \delta^t \pi_t^2(s^t)[1-\lambda + \lambda l_t(s^t)] =
332-
\delta^t \bigl[(1 - \lambda) \pi_t^2(s^t) + \lambda \pi_t^1(s^t)\bigr]
333-
$$
334-
335-
Thus, "shadow" prices $\theta_t(s^t)$ in the planning problem equal the competitive equilibrium prices $p_t(s^t)$.
336-
337-
338308
First-order necessary conditions for maximizing welfare criterion {eq}`eq:welfareW` subject to the feasibility constraint {eq}`eq:feasibility` are
339309
340310
$$\frac{\pi_t^2(s^t)}{\pi_t^1(s^t)} \frac{(1/c_t^2(s^t))}{(1/c_t^1(s^t))} = \frac{\lambda}{1-\lambda}$$
341311

342312
which can be rearranged to become
343313

344-
345-
346-
347314
$$
348315
\frac{c_t^1(s^t)}{c_t^2(s^t)} = \frac{\lambda}{1-\lambda} l_t(s^t)
349316
$$ (eq:allocationrule0)
@@ -390,7 +357,7 @@ $$
390357
391358
392359
393-
## If You're So Smart, $\ldots$
360+
## If you're so smart, $\ldots$
394361
395362
396363
Let's compute some values of limiting allocations {eq}`eq:allocationrule1` for some interesting possible limiting
@@ -429,7 +396,7 @@ Doing this will allow us to connect our analysis with an argument of {cite}`alch
429396

430397

431398

432-
## Competitive Equilibrium Prices
399+
## Competitive equilibrium prices
433400

434401
Two fundamental welfare theorems for general equilibrium models lead us to anticipate that there is a connection between the allocation that solves the social planning problem we have been studying and the allocation in a **competitive equilibrium** with complete markets in history-contingent commodities.
435402

@@ -555,6 +522,9 @@ According to formula {eq}`eq:pformulafinal`, we have the following possible limi
555522
* when $l_\infty = \infty$, $c_\infty^1 = 1 $ and tails of competitive equilibrium prices reflect agent $1$'s probability model $\pi_t^1(s^t)$ according to $p_t(s^t) \propto \delta^t \pi_t^1(s^t) $
556523
* for small $t$'s, competitive equilibrium prices reflect both agents' probability models.
557524
525+
We leave the verification of the shadow prices to the reader since it follows from
526+
the same reasoning.
527+
558528
## Simulations
559529
560530
Now let's implement some simulations when agent $1$ believes marginal density
@@ -850,7 +820,7 @@ This ties in nicely with {eq}`eq:kl_likelihood_link`.
850820

851821

852822

853-
## Related Lectures
823+
## Related lectures
854824

855825
Complete markets models with homogeneous beliefs, a kind often used in macroeconomics and finance, are studied in this quantecon lecture {doc}`ge_arrow`.
856826

lectures/likelihood_var.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ kernelspec:
2020
</div>
2121
```
2222

23-
# Likelihood Processes for VAR Models
23+
# Likelihood Processes For VAR Models
2424

2525
```{contents} Contents
2626
:depth: 2
@@ -156,11 +156,11 @@ Given the Gaussian structure, the conditional distribution $f(x_{t+1} | x_t)$ is
156156
- Mean: $A x_t$
157157
- Covariance: $CC'$
158158

159-
The log conditional density is:
159+
The log conditional density is
160160

161161
$$
162162
\log f(x_{t+1} | x_t) = -\frac{n}{2} \log(2\pi) - \frac{1}{2} \log \det(CC') - \frac{1}{2} (x_{t+1} - A x_t)' (CC')^{-1} (x_{t+1} - A x_t)
163-
$$
163+
$$ (eq:cond_den)
164164
165165
```{code-cell} ipython3
166166
def log_likelihood_transition(x_next, x_curr, model):
@@ -250,11 +250,8 @@ $$
250250
L_t = \sum_{s=1}^{t} \ell_s = \sum_{s=1}^{t} \log \frac{p_f(x_s | x_{s-1})}{p_g(x_s | x_{s-1})}
251251
$$
252252
253-
For the VAR model $x_{t+1} = A x_t + C w_{t+1}$ where $w_{t+1} \sim \mathcal{N}(0, I)$, the conditional density is
253+
where $p_f(x_t | x_{t-1})$ and $p_g(x_t | x_{t-1})$ are given by their respective conditional densities defined in {eq}`eq:cond_den`.
254254
255-
$$
256-
p(x_{t+1} | x_t) = (2\pi)^{-n/2} |CC^T|^{-1/2} \exp\left(-\frac{1}{2}(x_{t+1} - Ax_t)^T (CC^T)^{-1} (x_{t+1} - Ax_t)\right)
257-
$$
258255
259256
Let's write those equations in Python
260257
@@ -293,7 +290,7 @@ def compute_likelihood_ratio_var(paths, model_f, model_g):
293290
return log_L_ratios if N_paths > 1 else log_L_ratios[0]
294291
```
295292
296-
## Example 1: Two AR(1) processes
293+
## Example 1: two AR(1) processes
297294
298295
Let's start with a simple example comparing two univariate AR(1) processes with $A_f = 0.8$, $A_g = 0.5$, and $C_f = 0.3$, $C_g = 0.4$
299296
@@ -336,7 +333,7 @@ plt.show()
336333
337334
As we expected, the likelihood ratio processes goes to $+\infty$ as $T$ increases, indicating that model $f$ is chosen correctly by our algorithm.
338335
339-
## Example 2: Bivariate VAR models
336+
## Example 2: bivariate VAR models
340337
341338
Now let's consider an example with bivariate VAR models with
342339

0 commit comments

Comments
 (0)