-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimator_error.R
148 lines (132 loc) · 4.57 KB
/
estimator_error.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
library(kdensity)
source("bivariate_normal.R")
get_KL_divergence <- function(rho_1, rho_2) {
return(-1/2 * log((1-rho_1^2)/(1-rho_2^2)) + (1-rho_1*rho_2)/(1-rho_2^2) - 1)
}
get_Fisher_information_metric <- function(rho_1, rho_2) {
return(abs(sqrt(2)*atanh(sqrt(2)*rho_1/sqrt(1+rho_1^2)) - asinh(rho_1) - sqrt(2)*atanh(sqrt(2)*rho_2/sqrt(1+rho_2^2)) + asinh(rho_2)))
}
calculate_estimates <- function(estimateFunc, rho, numData, numIter) {
n_rho <- length(rho)
list <- matrix(NA, nrow=n_rho, ncol=numIter)
for (i in 1:n_rho) {
print(rho[i])
list[i, ] <- estimateFunc(rho[i], numData, numIter)
}
return(list)
}
get_mean_squared_error <- function(rho, list) {
n_rho <- length(rho)
numIter <- length(list[1, ])
MSE <- rep(NA, n_rho)
for (i in 1:n_rho) {
MSE[i] <- as.numeric(sum((list[i, ] - rho[i])^2) / numIter)
}
return(MSE)
}
get_KL_divergence_error <- function(rho, list) {
n_rho <- length(rho)
numIter <- length(list[1, ])
KL_div <- rep(NA, n_rho)
for (i in 1:n_rho) {
KL_div[i] <- 0
for (j in 1:numIter) {
KL_div[i] <- KL_div[i] + get_KL_divergence(rho[i], list[i, j])
}
KL_div[i] <- KL_div[i] / numIter
}
return(KL_div)
}
get_Fisher_information_error <- function(rho, list) {
n_rho <- length(rho)
numIter <- length(list[1, ])
fisher_information <- rep(NA, n_rho)
for (i in 1:n_rho) {
fisher_information[i] <- 0
for (j in 1:numIter) {
fisher_information[i] <- fisher_information[i] + get_Fisher_information_metric(rho[i], list[i, j])
}
fisher_information[i] <- fisher_information[i] / numIter
}
return(fisher_information)
}
get_MSE_results <- function(rho, numData, numIter) {
df <- rbind(
as.data.frame(
cbind(rho,
mse=get_mean_squared_error(function(x1, x2, x3)
get_Bayes_estimate_distribution(posterior_J, x1, x2, x3), rho, numData, numIter),
name="MSE w/Jeffreys prior"
)
),
as.data.frame(
cbind(rho,
mse=get_mean_squared_error(function(x1, x2, x3)
get_Bayes_estimate_distribution(posterior_flat, x1, x2, x3), rho, numData, numIter),
name="MSE w/Flat prior"
)
),
as.data.frame(
cbind(rho,
mse=get_mean_squared_error(function(x1, x2, x3)
get_Bayes_estimate_distribution(function(t1, t2, t3, t4) posterior_PC(t1, t2, t3, t4, lambda=0.0818), x1, x2, x3),
rho, numData, numIter),
name="MSE w/PC prior (lambda = 0.0818)"
)
),
as.data.frame(
cbind(rho,
mse=get_mean_squared_error(function(x1, x2, x3)
get_Bayes_estimate_distribution(function(t1, t2, t3, t4) posterior_PC(t1, t2, t3, t4, lambda=1.249), x1, x2, x3),
rho, numData, numIter),
name="MSE w/PC prior (lambda = 1.249)"
)
),
as.data.frame(
cbind(rho,
mse=get_mean_squared_error(function(x1, x2, x3)
get_Bayes_estimate_distribution(function(t1, t2, t3, t4) posterior_PC(t1, t2, t3, t4, lambda=5.360), x1, x2, x3),
rho, numData, numIter),
name="MSE w/PC prior (lambda = 5.360)"
)
),
as.data.frame(
cbind(
rho,
value=get_mean_squared_error()
)
)
)
df$rho <- sapply(df$rho, as.numeric)
df$mse <- sapply(df$mse, as.numeric)
return(df)
}
plot_results <- function(df, y_label="") {
gg <- ggplot(data=df, aes(rho, value)) +
theme_grey(base_size = 22) +
geom_line(size=1.2, group=1) +
labs(
y=y_label,
x="Correlation"
) +
facet_wrap(~ name)
return(gg)
}
#posteriorFunc <- function(x1, x2, x3, x4) posterior_PC(x1, x2, x3, x4, lambda=5.360)
#posteriorFunc <- posterior_J
#estimateFunc <- function(x1, x2, x3) get_Bayes_estimate_distribution(posteriorFunc, x1, x2, x3)
#estimateFunc <- function(x1, x2, x3) get_Bayes_estimate_with_KL_divergence_distribution(posteriorFunc, x1, x2, x3)
#estimateFunc <- function(x1, x2, x3) get_Bayes_estimate_with_Fisher_information_distribution(posteriorFunc, x1, x2, x3)
#estimateFunc <- get_MLE_distribution
#estimateFunc <- get_empirical_distribution
#estimateFunc <- get_empirical_with_known_means_distribution
knownMean <- TRUE
numSamples <- 1000
estimateFunc <- function(x1, x2, x3) get_Bayes_estimate_fiducial_with_Fisher_information_distribution(x1, x2, x3, numSamples=numSamples, knownMean=knownMean)
rho <- seq(0.1, 0.9, by=0.2)
numData <- 3
numIter <- 10000
estimates <- calculate_estimates(estimateFunc, rho, numData, numIter)
#get_mean_squared_error(rho, estimates)
get_KL_divergence_error(rho, estimates)
get_Fisher_information_error(rho, estimates)