-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_report.rmd
executable file
·322 lines (267 loc) · 11 KB
/
gen_report.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
320
321
322
---
title: "Expected IL for Uniswap v3 positions"
author:
- 0xAlbert.eth
- folch.eth
- 0xOli
date: \today
abstract: ""
pdf_document: null
geometry: "margin=45mm"
output:
pdf_document:
toc: true
number_sections: true
toc_depth: 2
keep_md: false
keep_tex: false
template: NULL
#pandoc_args: [
# --template=extras/haptic_template.tex
#]
html_document: default
tables: yes
latex_engine: pdflatex
header-includes:
- \usepackage{float}
- \usepackage{pdfpages}
- \usepackage{tabu}
- \usepackage{lipsum}
- \usepackage{booktabs}
- \usepackage[justification=raggedright,labelfont=bf,singlelinecheck=false]{caption}
- \usepackage{array}
- \usepackage{xcolor}
- \usepackage{color, colortbl}
- \usepackage{amsmath}
- \usepackage{mathtools,mathptmx}
- \usepackage{tabularx}
- \usepackage{background}
- \usepackage[english]{babel}
- \usepackage{csquotes}
- \usepackage[style=alphabetic, backend=bibtex]{biblatex}
- \bibliography{bibliography/haptic.bib}
- \usepackage{tikz}
- \usepackage[font=large,labelfont=bf]{caption}
- \usetikzlibrary{shapes,positioning}
- \usepackage{wrapfig}
- \usepackage{eso-pic,graphicx,transparent}
- \DeclareUnicodeCharacter{2212}{-}
- \backgroundsetup{pages={some},contents={}, opacity={0.3}, color={gray}}
- \usepackage{multirow}
- \usepackage{caption}
- \newcommand{\Tau}{\textstyle{\mathcal{T}}}
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_dir = "pdf") })
---
\captionsetup{font=small}
\captionsetup[table]{labelformat=empty}
\captionsetup[table]{labelfont=bf}
```{r results="asis", warning=FALSE, echo=FALSE}
options(knitr.kable.NA = '')
options(scipen=999) # avoid scientific notation
options(width = 60)
path = getwd()
source(paste(path, "/config.r", sep=""))
source(paste(path, "/uil.r", sep=""))
source(paste(path, "/eil.r", sep=""))
source(paste(path, "/static.r", sep=""))
T = 168
P = S0
chunk_one <- run_calc(mu, sigma, T)
chunk_two <- run_calc(mu, sigma, T * 2)
df_uil_one <- run_uil_calc(ranges, sigma, T)
```
\newpage
\section{Synopsis}
Impermament loss (IL) or divergence-loss, is a phenomena experienced by LP providers on almost all types of AMM platforms, especially when providing liquidity for non-mean reverting assets. The effects of IL have been studied extensively because of their negative impact on LP providers' returns \cite{LOE}. When modelling an asset's price with a geometric brownian motion (GBM) the expected value of IL (EIL) can be estimated numerically by using techniques such as the Monte Carlo method. To our knowledge, at least one previous fellow scholar deemed the computation of EIL for Uniswap v3 positions analytically intractable due to the complexity of the math expressions involving the GBM \cite{GSL}, however, a recent paper provided a framework for the analytical computation of EIL for Uniswap v3 positions \cite{CV3}. Previous work \cite{DZ} introduced the "unit impermanent loss per liquidity" (UIL) and efficient static replication formulas. We implement these ideas and present the results obtained with Monte Carlo simulations.
\subsection{Simulation parameters}
\label{simpar}
- Number of simulations: `r n_sim`
- Initial price: $`r S0`
\newpage
\section{Implementation details}
\subsection{EIL}
\label{eilint}
The integrals in the analytical computation of EIL and UIL are calculated using the "_integrate_" R function, based on QUADPACK routines dqags and dqagi by R. Piessens et al. \cite{PIE}. The code shown below is simplified for displaying purposes. Checkout the Github repository for the full version.
```{r a, eval=FALSE}
EIL <- function(f, lb, ub, P, Pa, Pb, mu, sigma, T) {
integral <- integrate(
f,
# Lower integration bound
lb,
# Upper integration bound
ub,
subdivisions = 5000,
abs.tol = 1e-5,
rel.tol = 1e-7,
stop.on.error = F,
T,
P,
Pa,
Pb,
mu,
sigma
)$value;
coefficient <- (1 / (sigma * sqrt(2 * pi * T)))
return(coefficient * integral)
}
```
\newpage
\subsection{UIL}
\label{uilint}
```{r x, eval=FALSE}
UIL <- function(P0, Pa, Pb, sigma, t, flag) {
psi <- function(x, T, sigma, flag) {
option_price <- bs_option(P0, x, sigma, 0, T, flag)
return(x^(-(3/2)) * option_price)
}
integral <- integrate(
psi,
# Lower integration bound
lower = Pa,
# Upper integration bound
upper = Pb,
subdivisions = 5000,
abs.tol = 1e-5,
rel.tol = 1e-7,
stop.on.error = F,
T = t ,
sigma = sigma,
flag = flag
)$value;
coefficient <- (-0.5)
return(coefficient * integral)
}
```
\newpage
\subsection{Time in-the-money (TITM)}
\label{itimint}
The two-dimensional integrals for the computation of "_time in-the-money_" ($\Tau$) are calculated using the dblquad function from the pracma library.
```{r b, eval=FALSE}
TITM <- function(P, Pa, Pb, mu, sigma, T) {
lower_bound <- log(Pa / P)
upper_bound <- log(Pb / P)
f <- function(x, y, mu, sigma) {
t <- y
first_term <- -(x - (mu - (sigma^2 / 2)) * t)^2
second_term <- (2 * t * (sigma^2))
exp(first_term / second_term) / sqrt(t)
}
integral <- dblquad(
f,
# Lower integration bound
log(Pa / P),
# Upper integration bound
log(Pb / P),
0,
T,
tol=1e-5,
subdivs=5000,
mu=mu,
sigma=sigma
)
result <- (1 / (sigma * (sqrt(2 * pi)))) * integral[1]
return(result)
}
```
\newpage
\section{Monte Carlo simulation results}
\label{simres}
\subsection{EIL for 168 hours (7 days)}
```{r, echo=FALSE, warning=FALSE, results='asis'}
collapse_rows_dt <- data.frame(
C1 = chunk_one[1:nrow(chunk_one), 1],
C2 = chunk_one[1:nrow(chunk_one), 2],
C3 = chunk_one[1:nrow(chunk_one), 3],
C4 = chunk_one[1:nrow(chunk_one), 4],
C5 = chunk_one[1:nrow(chunk_one), 5],
C6 = chunk_one[1:nrow(chunk_one), 6],
C7 = chunk_one[1:nrow(chunk_one), 7],
C8 = chunk_one[1:nrow(chunk_one), 8],
C9 = chunk_one[1:nrow(chunk_one), 9]
)
sigmaMsg <- paste("$\\\\sigma = $ ", sigma, sep="")
sigmaMsg2 <- paste("$\\\\sigma = $ ", sigma * 2, sep="")
collapse_rows_dt %>%
kbl(booktabs = T,
align = "c",
longtable = T,
escape = FALSE,
col.names = c( "$\\mu$", "r", "CE", "$\\Tau$", "EIL", "IL","$\\Tau$", "EIL", "IL"),
caption = "Table 1: Results for setting $\\mu$ = 7.5e−5, 1.5e−4 and $\\sigma$ = 0.001 and $\\sigma$ = 0.002 for 7 days (T = 168 hours) of providing liquidity with initial price \\$1000.\\label{tab:table1}"
) %>%
kable_styling(font_size = 9) %>%
column_spec(1, bold=F) %>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
add_header_above(c(" ", " ", " ", "Formula" = 2, "Simulation" = 1, "Formula" = 2, "Simulation" = 1), escape = FALSE) %>%
add_header_above(escape =FALSE, c(" ", " ", " ", setNames(3,sigmaMsg), setNames(3,sigmaMsg2))) %>%
kable_styling(font_size = 9, latex_options = c("hold_position", "repeat_header")) %>%
print()
```
\subsection{EIL for 720 hours (30 days)}
```{r, echo=FALSE, warning=FALSE, results='asis'}
collapse_rows_dt <- data.frame(
C1 = chunk_two[1:nrow(chunk_two), 1],
C2 = chunk_two[1:nrow(chunk_two), 2],
C3 = chunk_two[1:nrow(chunk_two), 3],
C4 = chunk_two[1:nrow(chunk_two), 4],
C5 = chunk_two[1:nrow(chunk_two), 5],
C6 = chunk_two[1:nrow(chunk_two), 6],
C7 = chunk_two[1:nrow(chunk_two), 7],
C8 = chunk_two[1:nrow(chunk_two), 8],
C9 = chunk_two[1:nrow(chunk_two), 9]
)
sigmaMsg <- paste("$\\\\sigma = $ ", sigma, sep="")
sigmaMsg2 <- paste("$\\\\sigma = $ ", sigma * 2, sep="")
collapse_rows_dt %>%
kbl(booktabs = T,
align = "c",
longtable = T,
escape = FALSE,
col.names = c( "$\\mu$", "r", "CE", "$\\Tau$", "EIL", "IL","$\\Tau$", "EIL", "IL"),
caption = "Table 2: Results for setting $\\mu$ = 7.5e−5, 1.5e−4 and $\\sigma$ = 0.001 and $\\sigma$ = 0.002 for 30 days (T = 720 hours) of providing liquidity with initial price \\$1000.\\label{tab:table2}"
) %>%
kable_styling(font_size = 9) %>%
column_spec(1, bold=F) %>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle") %>%
add_header_above(escape =FALSE, c(" ", " ", " ", setNames(3,sigmaMsg), setNames(3,sigmaMsg2))) %>%
add_header_above(escape =FALSE, c(" ", " ", setNames(3,sigmaMsg), setNames(4,sigmaMsg2))) %>%
kable_styling(font_size = 9, latex_options = c("hold_position", "repeat_header")) %>%
print()
```
\subsection{Expected prices}
```{r, echo=FALSE, warning=FALSE, results='asis'}
static_expected_prices <- get_expected_prices(P, mu)
data <- static_expected_prices$data
df_expected_prices <- data.frame(data)
colnames(df_expected_prices) <- c("$\\mu$", "$\\Tau$ = 168", "$\\Tau$ = 720")
df_expected_prices <- setNames(df_expected_prices, c("$\\mu$", "$\\Tau$ = 168", "$\\Tau$ = 720"))
row.names(df_expected_prices) <- NULL
df_expected_prices %>%
kbl( escape = F, caption = paste("Table 3: Expected price of the asset, for 7 ($\\Tau$ = 168 hours) and 30 days ($\\Tau$ = 720 hours).\\label{tab:table3}"), booktabs = T) %>%
kable_styling(latex_options = "HOLD_position", position="left") %>%
kable_classic(full_width = F, html_font = "Cambria") %>%
print()
```
\subsection{Unit impermanent loss per liquidity (UIL)}
```{r, echo=FALSE, warning=FALSE, results='asis'}
df_uil_one %>%
kbl(
booktabs = T,
align = "c",
longtable = T,
escape = FALSE,
caption = "Table 4 - Unit IL per liquidity (UIL).\\label{tab:table4} for $\\sigma$ = 0.001 and $\\sigma$ = 0.005"
) %>%
kable_styling(font_size = 9) %>%
collapse_rows(columns = c(1), latex_hline = "major", valign = "middle") %>%
add_header_above(escape = FALSE, c(" ", "Formula " = 2, "Static replication " = 2)) %>%
kable_styling(latex_options = "HOLD_position", position="left") %>%
kable_classic(full_width = F, html_font = "Cambria") %>%
print()
```
\section{Remarks}
\label{remarks}
The method introduced by \cite{CV3} provides a reliable way to determine the expected value of impermanent loss under a geometric brownian motion assumption. The model is sensitive to parameters such as drift and volatility as seen in Table \ref{tab:table1} and \ref{tab:table2}. The expected value of impermanent loss computed analytically converges with the result of the Monte Carlo simulation with low error rate. \cite{DZ} characterizes analytically the option-like payoff structure of impermanent loss for concentrated liquidity positions and provides formulas for static replication of the impermanent loss with a combination of European options. Table \ref{tab:table4} reports the result of the computation obtained using the analytical approach and highlights the high accuracy of the replication formulas.
\newpage
\printbibliography