-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighting_function_analysis.R
More file actions
76 lines (59 loc) · 2.01 KB
/
weighting_function_analysis.R
File metadata and controls
76 lines (59 loc) · 2.01 KB
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
library(ggplot2)
library(dplyr)
library(scales)
# --- Parameters ---
Tval <- 30000
Cval <- 100000
k <- 0.5
# --- Sqrt-affine reward (continuous at T, finite slope)
w_sqrtaffine <- function(s, T, C, k) {
s <- pmax(0, s)
s_c <- pmin(s, C) # clamp at cap C
out <- sqrt(pmin(s_c, T)) # branch 1: 0..T
idx <- s_c > T # branch 2: T..C
out[idx] <- sqrt(T + k^2 * (s_c[idx] - T))
out # branch 3: s > C stays flat due to clamp
}
# --- Derivative (piecewise)
# Note: derivative undefined at s=0 (infinite); we return NA there for plotting.
dw_sqrtaffine <- function(s, T, C, k) {
s <- pmax(0, s)
out <- rep(NA_real_, length(s))
# 1) 0 < s < T : d/ds sqrt(s) = 1/(2*sqrt(s))
idx1 <- s > 0 & s < T
out[idx1] <- 1 / (2 * sqrt(s[idx1]))
# 2) T < s < C : d/ds sqrt(T + k^2 (s - T)) = k^2 / (2 * sqrt(T + k^2 (s - T)))
idx2 <- s > T & s < C
out[idx2] <- k^2 / (2 * sqrt(T + k^2 * (s[idx2] - T)))
# 3) s > C : flat (cap), derivative 0
idx3 <- s > C
out[idx3] <- 0
# At exactly s = T and s = C there are kinks (left/right derivatives differ)
# We'll leave them as NA to show the kink in the plot.
out[s == T | s == C] <- NA_real_
out
}
# --- Grid and evaluations
s_grid <- seq(0, 1.1 * C, length.out = 2000)
w_val <- w_sqrtaffine(s_grid, T, C, k)
dw_val <- dw_sqrtaffine(s_grid, T, C, k)
# --- Plots
df <- data.frame(s = s_grid, w = w_val, dw = dw_val)
p_levels <- ggplot(df, aes(s, w)) +
geom_line(linewidth = 1) +
geom_vline(xintercept = T, linetype = "dashed") +
geom_vline(xintercept = C, linetype = "dashed") +
labs(title = "Levels w(s)",
x = "Self-stake s",
y = "w(s)") +
theme_minimal()
p_deriv <- ggplot(df, aes(s, dw)) +
geom_line(linewidth = 1) +
geom_vline(xintercept = T, linetype = "dashed") +
geom_vline(xintercept = C, linetype = "dashed") +
labs(title = "Marginal Reward dw/ds",
x = "Self-stake s",
y = "dw/ds") +
theme_minimal()
p_levels
p_deriv