/
fs_rescale.Rmd
319 lines (287 loc) · 9.52 KB
/
fs_rescale.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
---
title: "Quadratic and Ratio Rescaling of Parameters with Fixed Min and Max"
titleshort: "Quadratic and Ratio Rescaling of Parameters with Fixed Min and Max"
description: |
For 0<theta<1, generate 0 < thetaHat(theta, lambda) < 1, where lambda is between positive and negative infinity, used to rescale theta.
Fit a quadratic function for three points, where the starting and ending points are along the 45 degree line.
core:
- package: r
code: |
sort(unique())
sapply(ar, func, param=val)
- package: ggplot2
code: |
geom_line()
geom_vline()
labs(title, subtitle, x, y, caption)
scale_y_continuous(breaks, limits)
date: 2022-07-25
date_start: 2021-03-08
output:
pdf_document:
pandoc_args: '../../_output_kniti_pdf.yaml'
includes:
in_header: '../../preamble.tex'
html_document:
pandoc_args: '../../_output_kniti_html.yaml'
includes:
in_header: "../../hdga.html"
always_allow_html: true
urlcolor: blue
---
### Rescale a Parameter with Fixed Min and Max
```{r global_options, include = FALSE}
try(source("../../.Rprofile"))
```
`r text_shared_preamble_one`
`r text_shared_preamble_two`
`r text_shared_preamble_thr`
#### Rescale a Fraction Between $0$ and $1$
We have ratio $0<\theta<1$, we want to multiply $\theta$ to get closer to 0 or to 1, but to not exceed the bounds of $0$ and $1$. This is achieved by the following $\widehat{\theta}\left(\theta, \lambda\right)$ function, where we adjust $\lambda$ between negative and positive infinities to move $\theta$ closer to $0$ (as $\lambda\rightarrow-\infty$) or $\theta$ closer to $1$ (as $\lambda\rightarrow\infty$).
$$
\widehat{\theta}\left(\theta, \lambda\right)=
\theta \cdot
\left(
\frac{
\exp\left(\lambda\right)
}{
1 + \theta\cdot\left(\exp\left(\lambda\right) - 1\right)
}
\right)
$$
Given this function form, when $\lambda=0$, we are back to $\theta$, and $0 \le \widehat{\theta}\left(\lambda\right) \le 1$, which allows $\widehat{\theta}$ to be used as the "chance of success" parameter as we freely vary $\lambda$.
$$
\begin{aligned}
\widehat{\theta}\left(\theta, \lambda=0\right) &=
\theta \cdot \left(\frac{1}{1 + \theta\cdot(1-1)}\right) = \theta \\
\lim_{\lambda \rightarrow -\infty} \widehat{\theta}\left(\theta, \lambda\right) &=
\theta \cdot \left(\frac{0}{1 + \theta\cdot(0-1)}\right) = 0 \\
\lim_{\lambda \rightarrow \infty} \widehat{\theta}\left(\theta, \lambda\right) &=
\theta \cdot \left(\frac{\exp(\lambda)}{\theta \cdot \exp(\lambda)}\right) = 1 \\
\end{aligned}
$$
To test this, first, we write out the rescaling function.
```{r}
# Construct the formula
ffi_theta_lambda_0t1 <- function(theta, lambda) {
if (is.finite(exp(lambda))) {
theta * (exp(lambda) / (1 + theta * (exp(lambda) - 1)))
} else {
# If lambda is large, exp(lambda)=inf, ratio above becomes 1
1
}
}
# Test the function
print(ffi_theta_lambda_0t1(0.5, 1e1))
print(ffi_theta_lambda_0t1(0.5, 1e2))
print(ffi_theta_lambda_0t1(0.5, -1e3))
```
Second, given theta, we evaluate the function with differing lambda values.
```{r}
# Create Function
ffi_fixtheta_varylambda_0t1 <-
function(ar_lambda_pos =
1e1^(seq(-0.1, 1, length.out = 4)),
theta = 0.5) {
# Construct lambda vector
ar_lambda <- sort(unique((c(-ar_lambda_pos, 0, ar_lambda_pos))))
# sapply
ar_theta_hat <- sapply(ar_lambda, ffi_theta_lambda_0t1, theta = theta)
# Create table
ar_st_varnames <- c("theta_hat", "lambda")
tb_theta_hat_lambda <- as_tibble(
cbind(round(ar_theta_hat, 5), ar_lambda)
) %>%
rename_all(~ c(ar_st_varnames)) %>%
mutate(theta = theta)
# return
return(tb_theta_hat_lambda)
}
# Test function
tb_theta_hat_lambda <- ffi_fixtheta_varylambda_0t1()
# Print
kable(tb_theta_hat_lambda,
caption = paste(
"Theta-hat rescaling",
", given different lambda rescalers.",
separator = " "
)
) %>% kable_styling_fc()
```
Third, we run the function we just created for two three different $\theta$ levels, and we stack the results together.
```{r}
# Evaluate at differing thetas
ar_lambda_pos <- 1e1^(seq(-0.1, 1, length.out = 2))
tb_theta_hat_lambda_low <- ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.1)
tb_theta_hat_lambda_mid <- ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.5)
tb_theta_hat_lambda_hgh <- ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.9)
# Combine
tb_theta_hat_lambda_combo <- bind_rows(
tb_theta_hat_lambda_low,
tb_theta_hat_lambda_mid,
tb_theta_hat_lambda_hgh
)
# Print
kable(tb_theta_hat_lambda_combo,
caption = paste(
"Theta-hat rescaling",
", with multiple theta values",
", given different lambda rescalers.",
separator = " "
)
) %>% kable_styling_fc()
```
Fourth, we visualize the results from above. We generate a denser x-grid for this purpose, and we evaluate at 9 different theta values from $\theta=0.01$ to $\theta=0.99$. We can see in the graph that all $0<\hat{\theta}<1$.
```{r}
# Generate a denser result
ar_lambda_pos <- 1e1^(seq(-0.1, 1, length.out = 100))
tb_theta_hat_lambda_combo <- bind_rows(
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.01),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.10),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.25),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.50),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.75),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.90),
ffi_fixtheta_varylambda_0t1(ar_lambda_pos, 0.99)
)
# Labeling
st_title <- paste0("Rescale a Fraction (theta), Constrained Between 0 and 1")
st_subtitle <- paste0(
"Note that when LAMBDA = 0, THETA-HAT = theta."
)
st_caption <- paste0(
"https://fanwangecon.github.io/",
"R4Econ/math/solutions/htmlpdfr/fs_rescale.html"
)
st_x_label <- "Different rescaling LAMBDA values"
st_y_label <- "Rescaled THETA-HAT values between 0 and 1"
ar_y_breaks <- c(0.01, 0.10, 0.25, 0.50, 0.75, 0.90, 0.99)
# Graph
tb_theta_hat_lambda_combo %>%
mutate(theta = as.factor(theta)) %>%
ggplot(aes(
x = lambda, y = theta_hat,
color = theta, linetype = theta
)) +
geom_line(size = 2) +
geom_vline(
xintercept = 0,
linetype = "dashed", color = "black", size = 1
) +
labs(
title = st_title,
subtitle = st_subtitle,
x = st_x_label,
y = st_y_label,
caption = st_caption
) +
scale_y_continuous(
breaks = ar_y_breaks,
limits = c(0, 1)
) +
theme(
panel.grid.major.y = element_line(
color = "black",
size = 0.5,
linetype = 1
),
legend.key.width = unit(3, "line")
)
```
#### Fit Three Points with Ends Along 45 Degree Line
Given e < x < f, use f(x) to rescale x, such that f(e)=e, f(f)=f, but f(z)=$\alpha \cdot z$ for one particular z between e and f, with $\alpha$ > 1. And in this case, assume that $\alpha \cdot z < f$. Note that this is case where we have three points, and the starting and the ending points are along the 45 degree line.
We can fit these three points using the Quadratic function exactly. In another word, there is a unique quadratic function that crosses these three points. Note the quadratic function is either concave or convex through the entire domain.
First, as an example, suppose that $e=0$, $f=10$, $z=2$, and $\alpha=1.5$. Using a quadratic to fit:
$$
y(x) = a \cdot x^2 + b \cdot x + c
$$
We have three equations:
$$
0 = a \cdot 0 + b \cdot 0 + c\\
2 \cdot 1.5 = a \cdot 2^2 + b \cdot 2 + c\\
10 = a \cdot 10^2 + b \cdot 10 + c
$$
Given these, we have, $c = 0$, and subsequently, 2 equations and 2 unknowns:
$$
3 = a \cdot 4 + b \cdot 2\\
10 = a \cdot 100 + b \cdot 10
$$
Hence:
$$
a = \frac{3-2b}{4}\\
10 = \frac{3-2b}{4}\cdot 100 + b \cdot 10\\
10 = 75 - 50b + 10b\\
$$
And finally:
$$
a = \frac{3-2*1.625}{4}=-0.0625\\
b = \frac{65}{40}=1.625\\
c = 0
$$
Generate the $a$, $b$ and $c$ points above for the quadratic function:
```{r}
# set values
e <- 0
f <- 10
z <- 2
alpha <- 1.5
# apply formulas from above
a <- -0.0625
b <- 1.625
c <- 0
# grid of values beween a and b, 11 points covering z = 2
ar_x <- seq(e, f, length.out = 11)
# rescale
ar_grid_quad <- a * ar_x^2 + b * ar_x + c
# show values
kable(print(as_tibble(cbind(ar_x, ar_grid_quad))),
caption = paste0(
"Quadratic Fit of Three Equations and Three Unknowns\n",
"Satisfies: f(0)=0, f(10)=10, f(2)=3"
)
) %>%
kable_styling_fc()
```
Second, as another example, suppose that $e=0$, $f=3.5$, $z=0.5$, and $\alpha=1.5$. Using a quadratic to fit these, we have three equations:
$$
0 = a \cdot 0 + b \cdot 0 + c\\
0.75 = a \cdot 0.5^2 + b \cdot 0.5 + c\\
3.5 = a \cdot 3.5^2 + b \cdot 3.5 + c
$$
Given these, we have, $c = 0$, and subsequently, 2 equations and 2 unknowns:
$$
0.75 = a \cdot 0.25 + b \cdot 0.5\\
3.5 = a \cdot 12.25 + b \cdot 3.5
$$
Hence:
$$
a = \frac{0.75-0.5b}{0.25}\\
3.5 = \frac{0.75-0.5b}{0.25}\cdot 12.25 + b \cdot 3.5\\
3.5 = 36.75 - 24.5b + 3.5b\\
$$
And finally:
$$
a = \frac{0.75-0.5*1.58333}{0.25}=-0.1666\\
b = \frac{36.75-3.5}{24.5-3.5}=1.58333\\
c = 0
$$
Generate the $a$, $b$ and $c$ points above for the quadratic function:
```{r}
# set values
e <- 0
f <- 3.5
z <- 0.5
alpha <- 1.5
# apply formulas from above
a <- -0.16666666
b <- 1.583333333
c <- 0
# grid of values beween a and b, 11 points covering z = 2
ar_x <- seq(e, f, length.out = 100000)
# rescale
ar_grid_quad <- a * ar_x^2 + b * ar_x + c
# show values
# cbind(ar_x, ar_grid_quad)
ar_x[which.min(abs(ar_grid_quad - 0.75))]
```
The exercises above are special cases of the formula we derive on this page: [Formulas for Quadratic Parameters and Three Points](https://fanwangecon.github.io/R4Econ/math/solutions/htmlpdfr/fs_analytical_poly_fit.html#11_Formulas_for_Quadratic_Parameters_and_Three_Points).