-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permuation_influence_on_metrices.R
265 lines (243 loc) · 7.67 KB
/
Permuation_influence_on_metrices.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
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
install.packages('ggplot2')
library(ggplot2)
# sampling distribution
# --> all numbers are evenly drawn
chosen = c()
x = c(1,2,3,4,5,6,7,8,9,10)
for (i in seq(0, 100000)) {
chosen = c(chosen, sample(x,1))
}
hist(chosen)
y = c(1,2,3,4,5,6,7,8)
for (i in seq(1,length(y))){
x = c(1,2,3,4,5,6,7,8)
sampled = sample(x, 2)
x[sampled[1]] = sampled[2]
x[sampled[2]] = sampled[1]
}
# Random tries on how the cRMSE is influenced by alterations to its series
x = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)
# x = c(1,2,3,4,5,6,7,9,8,10)
y = c(1.0,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.1)
# y = c(1,2,3,4,5,6,7,8,9,10)
nvariables = length(x)
x_series = x - mean(x)
y_series = y - mean(y)
sqrt(sum((y_series-x_series)**2)/length(x))
ntries = 10000
nvariables = 10
exp_err = 0.2
eucl = c()
man = c()
cram = c()
crmse = c()
pear = c()
# How does the cRMSE progress with one growing series?
### No, the series are centered to the mean. Therefore,
### as long as a vector with same values is added to the
### initial series, no effect (other than the rounding error)
### can be observed/expected.
crmse = c()
ntries = 100
x = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)
y = c(1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0)
y_series = y - mean(y)
j = 0
for (i in seq(1,ntries)) {
x = x + c(0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1) * j
x_series = x - mean(x)
print(x_series)
crmse = c(crmse, sqrt(sum((y_series - x_series)**2)/length(x)))
j = j + 1
}
plot(crmse)
# Wich influence does the alteration of one variable have?
# Not the exchange but the actual alteratio of one value with
# a value of the normal distribution
ntries = 10000
nvariables = 10
exp_err = 0.2
crmse = c()
pear = c()
j = 10
for (i in seq(1,ntries)){
x = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)
y = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)
sampled = sample(x, 1)
subtracted = sampled - rnorm(1,sd=exp_err)
x[sampled[1]] = subtracted[1]
#x[sampled[2]] = subtracted[2]
x_series = x - mean(x)
y_series = y - mean(y)
crmse = c(crmse,sqrt(sum((y_series-x_series)**2)/nvariables))
pear = c(pear, cor(y_series,x_series, method='pearson'))
}
crmse
plot(crmse)
hist(crmse)
plot(pear)
# Check what influence the permutation of two positions has on the crmse
ntries = 100000
crmse = c()
dist_x = c()
dist_y = c()
pos = c(1,2,3,4,5,6,7,8,9,10)
for (i in seq(1,ntries)){
x = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)
# x = c(1,2,3,4,5,6,7,8,9,10)
y = c(1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0)
# y = c(1,2,3,4,5,6,7,8,9,10)
nvariables = length(x)
sampled = sample(pos, 2)
x_sampled = x[sampled[1]]
x[sampled[1]] = x[sampled[2]]
x[sampled[2]] = x_sampled
dist_x = c(dist_x, abs(sampled[1] - sampled[2]))
# sampled = sample(pos, 2)
# y_sampled = y[sampled[1]]
# y[sampled[1]] = y[sampled[2]]
# y[sampled[2]] = y_sampled
x_series = x - mean(x)
y_series = y - mean(y)
# dist_y = c(dist_y, abs(sampled[1] - sampled[2]))
crmse = c(crmse,sqrt(sum((y_series-x_series)**2)/nvariables))
}
plot(crmse)
hist(crmse)
plot(dist_x, crmse)
ggplot(data.frame(crmse), aes(x=crmse)) +
geom_histogram(fill = "white", colour = "black") +
labs(title = "Influence on the cRMSE of permutating one series", x = "cRMSE", y = "Count") +
theme_bw() + #has to be placed before the other theme definitions
theme(plot.title = element_text(size = rel(2)),
axis.title = element_text(size = rel(2)),
axis.text = element_text(size = rel(2)))
# how is the cRMSE influenced by the uncertainty?
ntries = 10000
exp_err = 0.2
crmse = c()
for (i in seq(1,ntries)){
# a = c(1,2,3,4,5,6,7,8,9,10)
a = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.10)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
crmse = c(crmse,sqrt(sum((b_series-a_series)**2)/length(a)))
}
plot(crmse)
hist(crmse)
ggplot(data.frame(crmse), aes(x=crmse)) +
geom_histogram(fill = "white", colour = "black") +
labs(title = "Influence of uncertainty on cRMSE", x = "cRMSE", y = "Count") +
theme_bw() + #has to be placed before the other theme definitions
theme(plot.title = element_text(size = rel(2)),
axis.title = element_text(size = rel(2)),
axis.text = element_text(size = rel(2)))
#### Is the influence of the uncertainty independent of the value range? ####
## Write the loop for the crmse_2 values new!!
### These plots are already finished in another script!
cRMSE_broad = c()
cRMSE_2 = c()
cRMSE_3 = c()
cRMSE_4 = c()
cRMSE_narrow = c()
pear_broad = c()
pear_2 = c()
pear_3 = c()
pear_4 = c()
pear_narrow = c()
ntries = 1000
for (i in seq(1,ntries)){
a = c(5,6,7,8,9)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
cRMSE_broad = c(cRMSE_broad,sqrt(sum((b_series-a_series)**2)/length(a)))
pear_broad = c(pear_broad, cor(a_series,b_series, method='pearson'))
}
for (i in seq(1,ntries)){
a = c(5.75,6.5,7.25,8.0,8.75)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
cRMSE_2 = c(cRMSE_2,sqrt(sum((b_series-a_series)**2)/length(a)))
pear_2 = c(pear_2, cor(a_series,b_series, method='pearson'))
}
for (i in seq(1,ntries)){
a = c(5.5,6.0,6.5,7.0,7.5)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
cRMSE_3 = c(cRMSE_3,sqrt(sum((b_series-a_series)**2)/length(a)))
pear_3 = c(pear_3, cor(a_series,b_series, method='pearson'))
}
for (i in seq(1,ntries)){
a = c(5.2,5.4,5.6,5.8,6.0)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
cRMSE_4 = c(cRMSE_4,sqrt(sum((b_series-a_series)**2)/length(a)))
pear_4 = c(pear_4, cor(a_series,b_series, method='pearson'))
}
for (i in seq(1,ntries)){
a = c(5.1,5.2,5.3,5.4,5.6)
a_n = rnorm(length(a),sd=exp_err)
b_n = rnorm(length(a),sd=exp_err)
a = a + a_n
b = a + b_n
ab = list(a,b)
a_series = a - mean(a)
b_series = b - mean(b)
cRMSE_narrow = c(cRMSE_narrow,sqrt(sum((b_series-a_series)**2)/length(a)))
pear_narrow = c(pear_narrow, cor(a_series,b_series, method='pearson'))
}
corrs = data.frame(pear_broad,
pear_2,
pear_3,
pear_4,
pear_narrow,
cRMSE_narrow,
cRMSE_2,
cRMSE_3,
cRMSE_4,
cRMSE_broad)
ggplot(corrs) + geom_density(aes(pear_narrow),size=1.1) +
geom_density(aes(pear_broad),colour="red",size=1.1) +
geom_density(aes(pear_2),colour="green",size=1.1) +
geom_density(aes(pear_3),colour="yellow",size=1.1) +
geom_density(aes(pear_4),colour="orange",size=1.1) +
labs(title = "Influence of value range and uncertainty Pearson", x = "Series", y = "Density") +
theme_bw() +
theme(plot.title = element_text(size = rel(2)),
axis.title = element_text(size = rel(2)),
axis.text = element_text(size = rel(2)))
ggplot(corrs) + geom_density(aes(cRMSE_narrow),size=1.1) +
geom_density(aes(cRMSE_narrow),colour="blue",size=1.1) +
geom_density(aes(cRMSE_2),colour="green",size=1.1) +
geom_density(aes(cRMSE_3),colour="yellow",size=1.1) +
geom_density(aes(cRMSE_4),colour="orange",size=1.1) +
theme_bw() +
labs(title = "Influence of value range and uncertainty cRMSE", x = "Series", y = "Density") +
theme(plot.title = element_text(size = rel(2)),
axis.title = element_text(size = rel(2)),
axis.text = element_text(size = rel(2)))