-
Notifications
You must be signed in to change notification settings - Fork 15
/
Script.R
249 lines (193 loc) · 6.52 KB
/
Script.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
# Librerías ---------------------------------------------------------------
library(dplyr)
library(scales)
# Lectura de datos --------------------------------------------------------
setwd("~/Actuaría/Docencia/AMAT/Varios/UDLAP 2022/R")
# Leemos las posibles sumas aseguradas
datos <- read.csv(file = "Sumas aseguradas.csv")
(nrow(datos)) # = M
datos %>% View()
SA = datos$Suma.Asegurada
summary(SA)
# Gráficos sencillos
MASS::truehist(SA)
plot(SA,type="b")
boxplot(SA)
# Sin LMR -----------------------------------------------------------------
# _Simulación de un escenario ---------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Probailidad de siniestro
theta <- 0.25
# Simulando los siniestros materializados
set.seed(123)
D <- rbinom(n = nrow(datos),size = 1,prob = theta)
# Total agregado de los siniestros materializados
(S=sum(SA*D*EF)) %>% dollar
# _Simulación de muchos escenarios ----------------------------------------
# Reserva
Reserva <- 35000000
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Probailidad de siniestro
theta <- 0.25
# Simulando los siniestros materializados
n = 10000 # Número de simulaciones
set.seed(2012)
replicate(n = n,
expr = {
# Simulando los siniestros materializados
D <- rbinom(n = nrow(datos),size = 1,prob = theta)
# Total agregado de los siniestros materializados
sum(D*SA*EF)
}) -> S
# Histograma de S
MASS::truehist(S,col="gray")
abline(v = Reserva,col="blue",lwd=2)
# Probabilidad de ruina
(sum(S>Reserva)/n) %>% percent(1/100)
barra <- table(S>Reserva)/n
names(barra) <- c("Solvencia","Ruina")
barplot(barra,col=c("green","red"),ylim = c(0,1))
abline(h = 0.95,col="blue",lwd=2)
# Convergencia a la media teórica
plot(cummean(S),type = "l")
abline(h = EF*theta*sum(SA),col="red",lwd=2)
# Con LMR -----------------------------------------------------------------
# _Simulación de un escenario ---------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Parámetros del modelo
theta <- 0.25
LMR <- 500000
# Simulando los siniestros materializados
set.seed(123)
D <- rbinom(n = nrow(datos),size = 1,prob = theta)
# Total agregado de los siniestros materializados
(S = sum(pmin(EF*SA, LMR) * D)) %>% dollar
# _Simulación de muchos escenarios ----------------------------------------
# __ 1 --------------------------------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Parámetros del modelo
theta <- 0.25
LMR <- 500000
# Simulando los siniestros materializados
n = 10000# Número de simulaciones
set.seed(1104)
replicate(n = n,
expr = {
# Simulando los siniestros materializados
D <- rbinom(n = nrow(datos), size = 1, prob = theta)
# Total agregado de los siniestros materializados
sum(pmin(EF*SA, LMR) * D)
}) -> S_LMR
# Histograma de S
MASS::truehist(S_LMR,col="gray")
abline(v = Reserva,col="blue",lwd=2)
# Probabilidad de ruina
(sum(S_LMR>Reserva)/n) %>% percent(1/100)
barra <- table(S_LMR>Reserva)/n
names(barra) <- c("Solvencia","Ruina")
barplot(barra,col=c("green","red"),ylim = c(0,1))
abline(h = 0.95,col="blue",lwd=2)
# Convergencia a la media teórica
plot(cummean(S_LMR),type = "l")
abline(h = theta*sum(pmin(EF*SA, LMR)),col="red",lwd=2)
# __ 2 --------------------------------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Parámetros del modelo
theta <- 0.25
LMR <- 450000
# Simulando los siniestros materializados
n = 10000# Número de simulaciones
set.seed(1104)
replicate(n = n,
expr = {
# Simulando los siniestros materializados
D <- rbinom(n = nrow(datos), size = 1, prob = theta)
# Total agregado de los siniestros materializados
sum(pmin(EF*SA, LMR) * D)
}) -> S_LMR
# Histograma de S
MASS::truehist(S_LMR,col="gray")
abline(v = Reserva,col="blue",lwd=2)
# Probabilidad de ruina
(sum(S_LMR>Reserva)/n) %>% percent(1/100)
barra <- table(S_LMR>Reserva)/n
names(barra) <- c("Solvencia","Ruina")
barplot(barra,col=c("green","red"),ylim = c(0,1))
abline(h = 0.95,col="blue",lwd=2)
# Convergencia a la media teórica
plot(cummean(S_LMR),type = "l")
abline(h = theta*sum(pmin(EF*SA, LMR)),col="red",lwd=2)
# __ 3 --------------------------------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Parámetros del modelo
theta <- 0.25
LMR <- 200000
# Simulando los siniestros materializados
n = 10000# Número de simulaciones
set.seed(1104)
replicate(n = n,
expr = {
# Simulando los siniestros materializados
D <- rbinom(n = nrow(datos), size = 1, prob = theta)
# Total agregado de los siniestros materializados
sum(pmin(EF*SA, LMR) * D)
}) -> S_LMR
# Histograma de S
MASS::truehist(S_LMR,col="gray")
abline(v = Reserva,col="blue",lwd=2)
# Probabilidad de ruina
(sum(S_LMR>Reserva)/n) %>% percentage(1/100)
barra <- table(S_LMR>Reserva)/n
names(barra) <- c("Solvencia","Ruina")
barplot(barra,col=c("green","red"),ylim = c(0,1))
abline(h = 0.95,col="blue",lwd=2)
# Convergencia a la media teórica
plot(cummean(S_LMR),type = "l")
abline(h = theta*sum(pmin(EF*SA, LMR)),col="red",lwd=2)
# __ Solución -------------------------------------------------------------
# Frecuencia relativa (Porcentaje de la SA)
EF <- 0.35 #= E[F]
# Parámetros del modelo
theta <- 0.25
# Número de simulaciones
n = 10000
# Reserva
Reserva = 35000000
# Probabilidad de ruina y simulaciones dado unl LMR
S.LMR <- function(LMR){
set.seed(1104)
replicate(n = n,
expr = {
# Simulando los siniestros materializados
D <- rbinom(n = nrow(datos), size = 1, prob = theta)
# Total agregado de los siniestros materializados
sum(pmin(EF*SA, LMR) * D)
}) -> S_LMR
# Probabilidad de Solvencia
FS <- ecdf(x = S_LMR)
p.ruina = 1 - FS(Reserva)
return(list(p.ruina=p.ruina,S_LMR=S_LMR))
}
# p.ruina(x) < 0.05
x = 400000
while(S.LMR(x)$p.ruina>0.05){
print((x %>% dollar()))
x <<- x - 10000
}
x %>% dollar()
S.LMR(x)$p.ruina
S_LMR <- S.LMR(x)$S_LMR
MASS::truehist(S_LMR)
abline(v=Reserva,col="red",lwd=2)
# Probabilidad de ruina
(sum(S_LMR>Reserva)/n) %>% percent(1/100)
barra <- table(S_LMR>Reserva)/n
names(barra) <- c("Solvencia","Ruina")
barplot(barra,col=c("green","red"),ylim = c(0,1))
abline(h = 0.95,col="blue",lwd=2)