viewof y1 = Inputs.range([20, 150], {value: 80, step: 5, label: html`income today ${math`y_1`}`})
viewof y2 = Inputs.range([20, 150], {value: 120, step: 5, label: html`income tomorrow ${math`y_2`}`})
viewof r = Inputs.range([0, 0.5], {value: 0.10, step: 0.01, label: html`interest rate ${math`r`}`})
viewof beta = Inputs.range([0.5, 1.0], {value: 1.0, step: 0.01, label: html`discount factor ${math`\beta`}`})
viewof pinA = Inputs.button("Pin this run for comparison")
import {pinned, ghost, math, mathSvg} from "../theme/im-sim.js"
W = y1 + y2 / (1 + r)
c1star = W / (1 + beta)
c2star = beta * (1 + r) * c1star
Ustar = Math.log(c1star) + beta * Math.log(c2star)
budget = {
const pts = [];
const n = 80;
for (let i = 0; i <= n; i++) {
const c1 = (W * i) / n;
pts.push({c1, c2: (1 + r) * (W - c1)});
}
return pts;
}
indiff = {
const pts = [];
const n = 120;
for (let i = 1; i <= n; i++) {
const c1 = (1.8 * W * i) / n;
const c2 = Math.exp((Ustar - Math.log(c1)) / beta);
if (c2 > 0 && c2 < 2.2 * W) pts.push({c1, c2});
}
return pts;
}
{
const base = pinned("two_period_consumption-1", {budget, c1star, c2star, W}, pinA);
const Wmax = Math.max(W, base.W);
// Euler line c2 = β(1+r)·c1, drawn from the origin to the edge of the frame
const eulerEnd = Math.min(1.6 * Wmax, (1.8 * Wmax) / (beta * (1 + r)));
return mathSvg(Plot.plot({
marginLeft: 55,
x: {label: "consumption today $c_1$ →", domain: [0, 1.6 * Wmax]},
y: {label: "consumption tomorrow $c_2$", grid: true, domain: [0, 1.8 * Wmax]},
marks: [
Plot.line(base.budget, {x: "c1", y: "c2", ...ghost}),
Plot.dot([{c1: base.c1star, c2: base.c2star}], {x: "c1", y: "c2", r: 5, fill: "var(--im-fg-muted)", fillOpacity: 0.5}),
Plot.line([{c1: 0, c2: 0}, {c1: eulerEnd, c2: beta * (1 + r) * eulerEnd}], {x: "c1", y: "c2", stroke: "var(--im-fg-muted)", strokeDasharray: "2 3", strokeWidth: 1.25}),
Plot.text([{c1: eulerEnd, c2: beta * (1 + r) * eulerEnd}], {x: "c1", y: "c2", text: () => "Euler line", dx: -6, dy: 12, textAnchor: "end", fill: "var(--im-fg-muted)"}),
Plot.line(budget, {x: "c1", y: "c2", stroke: "var(--im-accent)", strokeWidth: 2}),
Plot.line(indiff, {x: "c1", y: "c2", stroke: "var(--im-sim)", strokeWidth: 2}),
Plot.dot([{c1: y1, c2: y2}], {x: "c1", y: "c2", r: 5, fill: "var(--im-warn)", symbol: "square"}),
Plot.text([{c1: y1, c2: y2}], {x: "c1", y: "c2", text: () => "endowment $(y_1, y_2)$", dx: -16, dy: -10, textAnchor: "end", fill: "var(--im-warn)"}),
Plot.dot([{c1: c1star, c2: c2star}], {x: "c1", y: "c2", r: 5, fill: "var(--im-accent)"}),
Plot.text([{c1: c1star, c2: c2star}], {x: "c1", y: "c2", text: () => `optimum (${c1star.toFixed(1)}, ${c2star.toFixed(1)})`, dx: 12, dy: 14, textAnchor: "start", fill: "var(--im-accent)"})
]
}));
}