-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.R
172 lines (141 loc) · 5.58 KB
/
Functions.R
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
### STILL in PROGRESS ###
# Libraries
library(stats)
library(dplyr)
# Computes the Sharpe Ratio from a vector of returns
sharpe_ratio <- function(returns, risk_free_rate = 0) {
mean_return <- mean(returns) - risk_free_rate
std_dev <- sd(returns)
sharpe_ratio <- mean_return / std_dev
return(sharpe_ratio)
}
# Expected maximum Sharpe ratio
expected_max_sharpe_ratio <- function(mean_sharpe, var_sharpe, M) {
gamma <- 0.5772156649015328606 # Euler-Mascheroni constant
result <- mean_sharpe + sqrt(var_sharpe) * ((1 - gamma) * qnorm(1 - 1 / M) + gamma * qnorm(1 - 1 / (M * exp(1))))
return(result)
}
# Computes the t-Statistic from a vector of returns
t_statistic <- function(returns, risk_free_rate = 0) {
N <- length(returns)
sr <- sharpe_ratio(returns, risk_free_rate)
t_stat <- sr * sqrt(N)
return(t_stat)
}
# Computes the multiple testing adjusted critical t-values by the Bonferroni method
bonferroni_t_statistic <- function(t_statistics, significance_level = 0.05) {
num_tests <- length(t_statistics)
adjusted_alpha <- rep(significance_level / num_tests, num_tests)
z_critical <- qnorm(1 - adjusted_alpha / 2)
results <- data.frame(
Test_Number = 1:num_tests,
t_Statistic = t_statistics,
Necessary_t_Statistic = z_critical,
Necessary_p_Value = adjusted_alpha,
Success = as.integer(t_statistics > z_critical)
)
return(results)
}
# Computes the multiple testing adjusted critical t-values by the Holm method
holm_t_statistics <- function(t_statistics, significance_level = 0.05) {
num_tests <- length(t_statistics)
sorted_indices <- order(t_statistics, decreasing = TRUE)
sorted_t_statistics <- t_statistics[sorted_indices]
adjusted_alpha <- significance_level / (num_tests + 1 - 1:num_tests)
z_critical <- qnorm(1 - adjusted_alpha / 2)
results <- data.frame(
Test_Number = sorted_indices,
t_Statistic = sorted_t_statistics,
Necessary_t_Statistic = z_critical,
Necessary_p_Value = adjusted_alpha,
Success = as.integer(sorted_t_statistics > z_critical)
)
results <- results[order(results$Test_Number), ]
rownames(results) <- NULL
return(results)
}
# Computes the multiple testing adjusted critical t-values by the BHY method
bhy_t_statistics <- function(t_statistics, significance_level = 0.05) {
num_tests <- length(t_statistics)
c_m <- sum(1 / 1:num_tests)
sorted_indices <- order(t_statistics, decreasing = TRUE)
sorted_t_statistics <- t_statistics[sorted_indices]
adjusted_alpha <- (1:num_tests) * significance_level / (num_tests * c_m)
z_critical <- qnorm(1 - adjusted_alpha / 2)
results <- data.frame(
Test_Number = sorted_indices,
t_Statistic = sorted_t_statistics,
Necessary_t_Statistic = z_critical,
Necessary_p_Value = adjusted_alpha,
Success = as.integer(sorted_t_statistics > z_critical)
)
results <- results[order(results$Test_Number), ]
rownames(results) <- NULL
return(results)
}
# Bundles all functions to compute the adjusted critical t-values
necessary_t_statistics <- function(t_statistics, significance_level, method = 'bonferroni') {
if (method == 'bonferroni') {
return(bonferroni_t_statistic(t_statistics, significance_level))
} else if (method == 'holm') {
return(holm_t_statistics(t_statistics, significance_level))
} else if (method == 'bhy') {
return(bhy_t_statistics(t_statistics, significance_level))
} else {
stop("Method must be 'bonferroni', 'holm', or 'bhy'")
}
}
# Sharpe Ratio Haircut
haircut_sharpe_ratio <- function(returns, risk_free_rate, num_tests, k = 1, method = 'bonferroni') {
N <- length(returns)
t <- t_statistic(returns, risk_free_rate)
p <- 2 * pnorm(-abs(t))
min_p_value <- 1e-10
p <- max(p, min_p_value)
if (method == 'bonferroni') {
p_adj <- min(p * num_tests, 1)
} else if (method == 'holm') {
p_adj <- min(p * (num_tests + 1 - k), 1)
} else if (method == 'bhy') {
c_m <- sum(1 / 1:num_tests)
p_adj <- min(p * num_tests * c_m / k, 1)
} else {
stop("Method must be 'bonferroni', 'holm', or 'bhy'")
}
t_adj <- qnorm(1 - p_adj / 2)
SR_adj <- t_adj / sqrt(N)
return(SR_adj)
}
# Evaluate all strategies
evaluate_strategies <- function(returns_matrix, risk_free_rate = 0) {
num_strategies <- ncol(returns_matrix)
N <- nrow(returns_matrix)
original_sharpe_ratios <- c()
t_statistics <- c()
for (i in 1:num_strategies) {
returns <- returns_matrix[, i]
sr <- sharpe_ratio(returns, risk_free_rate)
t_stat <- t_statistic(returns, risk_free_rate)
original_sharpe_ratios <- c(original_sharpe_ratios, sr)
t_statistics <- c(t_statistics, t_stat)
}
sorted_indices <- order(t_statistics, decreasing = TRUE)
haircut_sharpe_ratios_bonferroni <- c()
haircut_sharpe_ratios_holm <- c()
haircut_sharpe_ratios_bhy <- c()
for (k in 1:length(sorted_indices)) {
idx <- sorted_indices[k]
returns <- returns_matrix[, idx]
haircut_sharpe_ratios_bonferroni <- c(haircut_sharpe_ratios_bonferroni, haircut_sharpe_ratio(returns, risk_free_rate, num_strategies, k = k, method = 'bonferroni'))
haircut_sharpe_ratios_holm <- c(haircut_sharpe_ratios_holm, haircut_sharpe_ratio(returns, risk_free_rate, num_strategies, k = k, method = 'holm'))
haircut_sharpe_ratios_bhy <- c(haircut_sharpe_ratios_bhy, haircut_sharpe_ratio(returns, risk_free_rate, num_strategies, k = k, method = 'bhy'))
}
results <- data.frame(
Strategy = sorted_indices,
Original = original_sharpe_ratios[sorted_indices],
Bonferroni = haircut_sharpe_ratios_bonferroni,
Holm = haircut_sharpe_ratios_holm,
BHY = haircut_sharpe_ratios_bhy
)
return(results)
}