-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopulasaux.jl
More file actions
323 lines (278 loc) · 8.7 KB
/
copulasaux.jl
File metadata and controls
323 lines (278 loc) · 8.7 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
### Funciones auxiliares para `copulas.jl`
### Autor: Arturo Erdely
### Última actualización: 2026-05-04
"""
Fn(x::Real, xobs::Vector{<:Real})
Función de distribución empírica evaluada en el punto `x`
con base en una muestra aleatoria observada `xobs`.
## Ejemplo
```
Fn(0.0, randn(1_000)) # valor cercano a 0.5
```
"""
function Fn(x::Real, xobs::Vector{<:Real})
return count(xobs .≤ x) / length(xobs)
end
"""
Fn(x::Vector{<:Real}, xobs::Vector{<:Real})
Función de distribución empírica evaluada en cada punto del vector `x`
con base en una muestra aleatoria observada `xobs`.
## Ejemplo
```
Fn([-1.96, 0.0, 1.96], randn(10_000)) # valores cercanos a [0.025, 0.5, 0.975]
```
"""
function Fn(x::Vector{<:Real}, xobs::Vector{<:Real})
m = length(x)
n = length(xobs)
v = zeros(m)
for k ∈ 1:m
v[k] = count(xobs .≤ x[k]) / n
end
return v
end
"""
poligonal(x::Vector{<:Real}, xobs::Vector{<:Real}; mínimo = minimum(xobs), máximo = maximum(xobs))
Aproximación poligonal de una función de distribución continua en un vector dado de valores `x`
con base en una muestra aleatoria observada dada por el vector `xobs`. El parámetro opcional
`mínimo` puede escogerse como un valor menor que el mínimo de la muestra, y `máximo` como
un valor mayor que el máximo de la muestra, si así se requiere.
greater value than the sample maximum, if required.
## Ejemplo
```
xobs = randn(10_000)
x = [-1.96, 0, 1.96]
poligonal(x, xobs) # valores cercanos a [0.025, 0.5, 0.975]
```
"""
function poligonal(x::Vector{<:Real}, xobs::Vector{<:Real}; mínimo = minimum(xobs), máximo = maximum(xobs))
n = length(xobs)
xord = sort(xobs)
xp = (xord[1:(n-1)] .+ xord[2:n]) ./ 2
xp = vcat(mínimo, xp, máximo)
function g(z, xp)
a = 0.0
if z > xp[n+1]
a = 1.0
elseif z > xp[1]
k = findmax(z .≤ xp)[2]
a = (1/n) * ((k-1) - (xp[k] - z)/(xp[k] - xp[k-1]))
end
return a
end
m = length(x)
polivalores = zeros(m)
for i ∈ 1:m
polivalores[i] = g(x[i], xp)
end
return polivalores
end
# repetidos (1 method)
"""
repetidos(x::Vector)
Returns a type `Int` vector with the positions in vector `x` of repeated values.
## Examples
```
repetidos([1, 2, 3, 4, 1, 2, 5, 2])
repetidos(['A', 'B', 'C', 'A', 'B', 'A'])
repetidos(rand(10_000)) # no repeated values
```
"""
function repetidos(x::Vector)
if allunique(x)
return Int[]
else
u = unique(x)
n = length(u)
xpos = Int[]
for k ∈ 1:n
ix = findall(x .== u[k])
if length(ix) > 1
append!(xpos, ix[2:end])
end
end
sort!(xpos)
end
return xpos
end
# resumen (4 methods)
"""
resumen(v::Vector{<:Real}, desplegar = false)
Calculates a tuple of summary statistics for the values in vector `v`.
If `desplegar = true` it also displays a summary table. The calculated
statistics are: sample size, total number of repeated values, mean,
minimum, first quartile, median, third quartile and maximum.
*Dependencies*:
- function `repetidos`
- package `Statistics` (Julia standard library)
## Example
```
v = rand(rand(50), 55);
r = resumen(v, true)
r.median
```
"""
function resumen(v::Vector{<:Real}, desplegar = false)
# dependencies: `repetidos` `Statistics`
n = length(v)
rep = length(repetidos(v))
μ = mean(v)
m = minimum(v)
Q1 = quantile(v, 0.25)
Q2 = median(v)
Q3 = quantile(v, 0.75)
M = maximum(v)
r = (samplesize = n, repeated = rep, mean = μ, min = m,
quartile_1 = Q1, median = Q2, quartile_3 = Q3, max = M)
if desplegar
display(hcat(collect(keys(r)), collect(values(r))))
end
return r
end
"""
resumen(matriz::Matrix{<:Real}, desplegar = false)
Calculates a tuple of summary statistics for each column in `matriz`.
If `desplegar = true` it also displays a summary table. The calculated
statistics are: sample size, total number of repeated values, mean,
minimum, first quartile, median, third quartile and maximum.
*Dependencies*:
- function `resumen(v::Vector{<:Real},...)`
- package `Statistics` (Julia standard library)
## Example
```
M = [[.1,.3,.2,.1,.2,.4] [.6,.4,.2,.5,.3,.1] [.4,.5,.4,.6,.7,.8]]
r = resumen(M, true)
r.repeated
r.table
```
"""
function resumen(matriz::Matrix{<:Real}, desplegar = false)
# dependencies: `Statistics` `resumen(v::Vector{<:Real},...)`
nfilas, ncols = size(matriz)
v = collect(1:ncols)
n = fill(nfilas, ncols); rep = fill(0, ncols)
μ = fill(0.0, ncols); m = fill(0.0, ncols)
Q1 = fill(0.0, ncols); Q2 = fill(0.0, ncols)
Q3 = fill(0.0, ncols); M = fill(0.0, ncols)
D = Matrix{Any}(undef, nfilas, 0)
for j ∈ 1:ncols
r = resumen(matriz[:, j])
n[j] = r.samplesize; rep[j] = r.repeated
μ[j] = r.mean; m[j] = r.min
Q1[j] = r.quartile_1; Q2[j] = r.median
Q3[j] = r.quartile_3; M[j] = r.max
if j == 1
D = vcat([:variable j], hcat(collect(keys(r)), collect(values(r))))
else
D = hcat(D, pushfirst!(collect(values(r)), j))
end
end
rr = (variable = v, samplesize = n, repeated = rep, mean = μ, min = m,
quartile_1 = Q1, median = Q2, quartile_3 = Q3, max = M, table = D)
if desplegar
display(D)
end
return rr
end
# desempate! (4 methods) desempate (4 methods)
"""
desempate!(v::Vector{<:AbstractFloat}, ε = minimum(abs.(v)) / 1_000_000.0)
Adds (in place) a uniform random noise ±`ε` to the repeated values in `v` (if any).
If `ε` is not specified then it defaults to `ε = minimum(abs.(v)) / 1_000_000`.
*Dependencies*:
- function `repetidos`
## Example
```
a = [.1, .2, .1, .1, .3, .2]
desempate!(a)
println(a)
```
"""
function desempate!(v::Vector{<:AbstractFloat}, ε = minimum(abs.(v)) / 1_000_000.0)
# dependencies: `repetidos`
ipos = repetidos(v)
npos = length(ipos)
v[ipos] .+= ε .* (2 .* rand(npos) .- 1)
return v
end
"""
desempate!(matriz::Matrix{<:AbstractFloat}, ε = minimum(abs.(matriz)) / 1_000_000.0)
Adds (in place) a uniform random noise ±`ε` to the repeated values in each column of `matriz`
(if any). If `ε` is not specified then it defaults to `ε = minimum(abs.(matriz)) / 1_000_000`.
*Dependencies*:
- function `repetidos`
- function `desempate!(v::Vector{<:AbstractFloat},...)`
## Example
```
A = [[.1,.2,.1,.3] [.1,.2,.3,.4] [.1,.1,.2,.1]]
println(A)
desempate!(A)
println(A)
```
"""
function desempate!(matriz::Matrix{<:AbstractFloat}, ε = minimum(abs.(matriz)) / 1_000_000.0)
# dependencies: `repetidos` `desempate!(v::Vector{Float64},...)`
ncol = size(matriz)[2]
for j ∈ 1:ncol
matriz[:, j] = desempate!(matriz[:, j], ε)
end
return(matriz)
end
"""
desempate(v::Vector{<:AbstractFloat}, ε = minimum(abs.(v)) / 1_000_000.0)
Adds a uniform random noise ±`ε` to the repeated values in a copy of `v` (if any).
If `ε` is not specified then it defaults to `ε = minimum(abs.(v)) / 1_000_000`.
Returns a tuple with such copy but with no repeated values, a vector with the
positions where repeated values were found, and the total number of repeated values.
*Dependencies*:
- function `repetidos`
## Example
```
a = [.1, .2, .1, .1, .3, .2]
b = desempate(a)
a ≠ b[1]
```
"""
function desempate(v::Vector{<:AbstractFloat}, ε = minimum(abs.(v)) / 1_000_000.0)
# dependencies: `repetidos`
vv = copy(v)
ipos = repetidos(vv)
npos = length(ipos)
vv[ipos] .+= ε .* (2 .* rand(npos) .- 1)
return (vv, ipos, npos)
end
"""
desempate(matriz::Matrix{<:AbstractFloat}, ε = minimum(abs.(matriz)) / 1_000_000.0)
Adds a uniform random noise ±`ε` to the repeated values in each column of
a copy of `matriz` (if any). If `ε` is not specified then it defaults to
`ε = minimum(abs.(v)) / 1_000_000`. Returns a tuple with such copy but with no repeated values,
a vector with the positions where repeated values were found in each column,
and the total number of repeated values in each column.
*Dependencies*:
- function `repetidos`
- function `desempate(v::Vector{<:AbstractFloat},...)`
## Example
```
A = [[.1,.2,.1,.3] [.1,.2,.3,.4] [.1,.1,.2,.1]]
R = desempate(A)
R[1]
A .≠ R[1]
R[2]
R[3]
```
"""
function desempate(matriz::Matrix{<:AbstractFloat}, ε = minimum(abs.(matriz)) / 1_000_000.0)
# dependencies: `repetidos` `desempate(v::Vector{<:AbstractFloat},...)`
d = size(matriz)
M = zeros(d)
ipos = Vector{Vector{Int}}(undef, d[2])
npos = Vector{Int}(undef, d[2])
for j ∈ 1:d[2]
des = desempate(matriz[:, j], ε)
M[:, j] = des[1]
ipos[j] = des[2]
npos[j] = des[3]
end
return (M, ipos, npos)
end
@info "Fn poligonal repetidos resumen desempate desempate!"