/
resample_uncertaindataset_value.jl
176 lines (132 loc) · 6.07 KB
/
resample_uncertaindataset_value.jl
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
import ..UncertainDatasets:
AbstractUncertainValueDataset,
UncertainValueDataset,
UVAL_COLLECTION_TYPES
import ..SamplingConstraints:
NoConstraint,
TruncateLowerQuantile,
TruncateUpperQuantile,
TruncateQuantiles
"""
resample(x::UVAL_COLLECTION_TYPES, constraint::SamplingConstraint) -> Vector{T} where T
Resample `x` (a collection of uncertain values) once, applying the provided sampling `constraint`.
Returns a `length(x)`-element vector. The `i`-th element of this vector is generated by
truncating the `i`-th uncertain value by the sampling `constraint`, then drawing a single random
number from the truncated value.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values where the `i`-th value is given by a normal
# distribution with mean `i` and a standard deviation drawn from a uniform
# distribution on `[0, 1]`.
uvals = [UncertainValue(Normal(i, rand())) for i = 1:100]
# Truncate each distribution at +- 0.5 standard deviations, then resample.
resample(uvals, TruncateStd(0.5))
```
"""
function resample(uv::UVAL_COLLECTION_TYPES, constraint::SamplingConstraint)
[resample(uv.values[i], constraint) for i in 1:length(uv)]
end
"""
resample(x::UVAL_COLLECTION_TYPES, constraint::Vector{<:SamplingConstraint}) -> Vector{T} where T
Resample `x` (a collection of uncertain values) once, applying the provided sampling `constraint`s.
The number of constraints must match the number of elements in `x`.
Returns a `length(x)`-element vector. The `i`-th element of this vector is generated by
truncating the `i`-th uncertain value by the `i`-th sampling `constraint`, then drawing a single random
number from the truncated value.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values where the `i`-th value is given by a normal
# distribution with mean `i` and a standard deviation drawn from a uniform
# distribution on `[0, 1]`.
uvals = [UncertainValue(Normal(i, rand())) for i = 1:100]
# Truncate each distribution at +- 0.5 standard deviations, then resample.
resample(uvals, TruncateStd(0.5))
```
"""
function resample(uv::UVAL_COLLECTION_TYPES, constraint::Vector{<:SamplingConstraint})
[resample(uv.values[i], constraint[i]) for i in 1:length(uv)]
end
"""
resample(x::UVAL_COLLECTION_TYPES, constraint::SamplingConstraint, n::Int) -> Vector{Vector{T}} where T
Resample `x` (a collection of uncertain values) `n` times, applying the provided sampling `constraint`.
Returns an `n`-element vector of `length(x)`-element vectors. Each of these vectors is an independent
draw from `x`. The `i`-th element of each draw is generated by truncating the `i`-th uncertain value by
the sampling `constraint`, then drawing a single random number from the truncated value.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values where the `i`-th value is given by a normal
# distribution with mean `i` and a standard deviation drawn from a uniform
# distribution on `[0, 1]`.
uvals = [UncertainValue(Normal(i, rand())) for i = 1:100]
# Truncate the first 50 elements at the 90th percentile range, and the
# last 50 elements at the 40th percentile range.
constraints = [i <= 50 ? TruncateQuantiles(0.05, 0.95) : TruncateQuantiles(0.3, 0.7) for i = 1:100]
# Truncate the distributions, then draw ten independent realisations of the collection subject
# to the provided constraints.
resample(uvals, constraints, 10)
```
"""
function resample(uv::UVAL_COLLECTION_TYPES, constraint::SamplingConstraint, n::Int)
[[resample(uv.values[i], constraint) for i in 1:length(uv)] for k = 1:n]
end
"""
resample(x::UVAL_COLLECTION_TYPES, constraint::Vector{<:SamplingConstraint}, n::Int) -> Vector{Vector{T}} where T
Resample `x` (a collection of uncertain values) `n` times, applying the provided sampling `constraint`s.
Returns an `n`-element vector of `length(x)`-element vectors. Each of these vectors is an independent
draw from `x`. The `i`-th element of each draw is generated by truncating the `i`-th uncertain value by
the `i`-th sampling `constraint`, then drawing a single random number from the truncated value.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values where the `i`-th value is given by a normal
# distribution with mean `i` and a standard deviation drawn from a uniform
# distribution on `[0, 1]`.
uvals = [UncertainValue(Normal(i, rand())) for i = 1:100]
# Truncate the first 50 elements at `± 0.5` standard deviations, and the
# last 50 elements at `± 1.2` standar deviations.
constraints = [i <= 50 ? TruncateStd(0.5) : TruncateStd(1.2) for i = 1:100]
# Apply the constraints element-wise, then draw ten independent realisations
# of the collection subject to those constraints.
resample(uvals, constraints, 10)
```
"""
function resample(uv::UVAL_COLLECTION_TYPES, constraint::Vector{<:SamplingConstraint}, n::Int)
[[resample(uv.values[i], constraint[i]) for i in 1:length(uv)] for k = 1:n]
end
"""
resample(x::UVAL_COLLECTION_TYPES, constraint::SamplingConstraint, n::Int) -> Vector{Vector{T}} where T
Resample `x` (a collection of uncertain values) once by drawing a single random number from
each of the uncertain values in `x`.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values represented by gamma distributions
uvals = [UncertainValue(Gamma(i, rand())) for i = 1:100]
# Resample the collection once
resample(uvals)
```
"""
function resample(uvd::UVAL_COLLECTION_TYPES)
L = length(uvd)
[resample(uvd.values[i]) for i in 1:L]
end
"""
resample(uvd::UVAL_COLLECTION_TYPES, n::Int) -> Vector{Vector{T}}
Draw `n` realisations of an uncertain value dataset according to the distributions
of the uncertain values comprising it.
See also [`UVAL_COLLECTION_TYPES`](@ref).
## Example
```julia
# Generate some uncertain values represented by gamma distributions
uvals = [UncertainValue(Gamma(i, rand())) for i = 1:100]
# Resample the collection once
resample(uvals)
```
"""
function resample(uvd::UVAL_COLLECTION_TYPES, n::Int)
L = length(uvd)
[[resample(uvd.values[i]) for i in 1:L] for k in 1:n]
end