-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Resampling in-place - Added some example constraints.
- Loading branch information
Showing
13 changed files
with
287 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,14 @@ name = "UncertainData" | |
uuid = "dcd9ba68-c27b-5cea-ae21-829cd07325bf" | ||
authors = ["Kristian Agasøster Haaga <[email protected]>"] | ||
repo = "https://github.com/kahaaga/UncertainData.jl.git" | ||
version = "0.6.0" | ||
version = "0.7.0" | ||
|
||
[deps] | ||
Bootstrap = "e28b5b4c-05e8-5b66-bc03-6f0c0a0a06e0" | ||
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8" | ||
DynamicalSystemsBase = "6e36e845-645a-534a-86f2-f5d4aa5a06b4" | ||
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5" | ||
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" | ||
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
```@docs | ||
resample! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
example_constraints(X::AbstractUncertainIndexValueDataset, | ||
d_xinds = Uniform(0.5, 1.1), d_yinds = Uniform(0.9, 1.7)) | ||
Generate a set of random sampling constraints that can be used to constrain | ||
the indices and values of an uncertain index-value dataset `X`. These are | ||
generated as follows: | ||
- `constraints_inds = TruncateStd(rand(d_xinds))` | ||
- `constraints_vals = [TruncateQuantiles(0.5 - rand(d_xvals), 0.5 + rand(d_xvals)) for i = 1:length(X)];` | ||
Returns the tuple (constraints_inds, constraints_vals). | ||
""" | ||
function example_constraints(X::AbstractUncertainIndexValueDataset, | ||
d_xinds = Uniform(0.5, 1.1), d_yinds = Uniform(0.9, 1.7)) | ||
|
||
# Truncate indices at some fraction time their standard deviation around the man | ||
constraints_inds = TruncateStd(rand(d_xinds)) | ||
|
||
# Truncate values at some percentile range | ||
constraints_vals = [TruncateQuantiles(0.5 - rand(d_xvals), 0.5 + rand(d_xvals)) for i = 1:length(X)]; | ||
|
||
return (constraints_x_inds, constraints_vals) | ||
end | ||
|
||
""" | ||
example_constraints(X::AbstractUncertainIndexValueDataset, | ||
Y::AbstractUncertainIndexValueDataset; | ||
d_xinds = Uniform(0.5, 1.1), d_yinds = Uniform(0.9, 1.7), | ||
d_xvals = Uniform(0.05, 0.15), d_yvals = Uniform(0.3, 0.4)) | ||
Generate a set of random sampling constraints that can be used to constrain | ||
the indices and values of two uncertain index-value datasets `X` and `Y`. | ||
The constraints are generated as follows: | ||
- `constraints_inds_x = TruncateStd(rand(d_xinds))` | ||
- `constraints_inds_x = TruncateStd(rand(d_yinds))` | ||
- `constraints_vals_x = [TruncateQuantiles(0.5 - rand(d_xvals), 0.5 + rand(d_xvals)) for i = 1:length(X)]` | ||
- `constraints_vals_x = [TruncateQuantiles(0.5 - rand(d_yvals), 0.5 + rand(d_yvals)) for i = 1:length(Y)]` | ||
Returns the tuple of tuples `((constraints_inds_x, constraints_vals_x), (constraints_inds_y, constraints_vals_y))` | ||
""" | ||
function example_constraints(X::AbstractUncertainIndexValueDataset, | ||
Y::AbstractUncertainIndexValueDataset; | ||
d_xinds = Uniform(0.5, 1.1), d_yinds = Uniform(0.9, 1.7), | ||
d_xvals = Uniform(0.05, 0.15), d_yvals = Uniform(0.3, 0.4)) | ||
|
||
# Truncate indices at some fraction time their standard deviation around the man | ||
constraints_x_inds = TruncateStd(rand(d_xinds)) | ||
constraints_y_inds = TruncateStd(rand(d_yinds)) | ||
|
||
# Truncate values at some percentile range | ||
constraints_x_vals = [TruncateQuantiles(0.5 - rand(d_xvals), 0.5 + rand(d_xvals)) for i = 1:length(X)]; | ||
constraints_y_vals = [TruncateQuantiles(0.5 - rand(d_yvals), 0.5 + rand(d_yvals)) for i = 1:length(Y)]; | ||
cs_x = (constraints_x_inds, constraints_x_vals) | ||
cs_y = (constraints_y_inds, constraints_y_vals) | ||
|
||
cs_x, cs_y | ||
end | ||
|
||
export example_constraints |
40 changes: 40 additions & 0 deletions
40
src/example_datasets/example_uncertainindexvalue_dataset.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using DynamicalSystemsBase | ||
|
||
""" | ||
example_uncertain_indexvalue_datasets(system::DynamicalSystems.DiscreteDynamicalSystem, n::Int, vars; Ttr = 1000, | ||
d_xval = Uniform(0.01, 0.4), d_yval = Uniform(0.01, 0.5), | ||
d_xind = Uniform(0.5, 1.5), d_yind = Uniform(0.5, 1.5)) | ||
Generate a pair of `UncertainIndexValueDataset`s from a discrete dynamical `system`, generated | ||
by iterating the system `n` time after a transient run of `Ttr` steps, then gathering the | ||
columns at positions `vars` (should be two column indices) as separate time series. | ||
Each of the time series, call them `x` and `y`, are then converted to uncertain values. | ||
Specifically, replace `x[i]` and `y[i]` with `UncertainValue(Normal, x[i], rand(d_xval)` | ||
and `UncertainValue(Normal, y[i], rand(d_xval)`. Because the time series don't have | ||
explicit time indices associated with them, we'll create some time indices as the range | ||
`1:tstep:length(x)*tstep`, call them `x_inds` and `y_inds`. The time indices for `x` and `y` | ||
are also normally distributed, such that `x_inds[i] = UncertainValue(Normal, i, rand(d_xind)`, | ||
and the same for `y_inds`. | ||
Returns a tuple of `UncertainIndexValueDataset` instances, one for `x` and one for `y`. | ||
""" | ||
function example_uncertain_indexvalue_datasets(system::DiscreteDynamicalSystem, n::Int, vars; Ttr = 1000, | ||
tstep = 3, | ||
d_xval = Uniform(0.01, 0.4), d_yval = Uniform(0.01, 0.5), | ||
d_xind = Uniform(0.5, 1.5), d_yind = Uniform(0.5, 1.5)) | ||
|
||
orbit = trajectory(system, n - 1, Ttr = 1000); | ||
x_vals = UncertainValueDataset([UncertainValue(Normal, x, rand(d_xval)) for x in orbit[:, vars[1]]]) | ||
y_vals = UncertainValueDataset([UncertainValue(Normal, y, rand(d_yval)) for y in orbit[:, vars[2]]]) | ||
|
||
x_inds = UncertainIndexDataset([UncertainValue(Normal, i, rand(d_xind)) for i = 1:tstep:length(x_vals)*tstep]); | ||
y_inds = UncertainIndexDataset([UncertainValue(Normal, i, rand(d_yind)) for i = 1:tstep:length(y_vals)*tstep]); | ||
|
||
X = UncertainIndexValueDataset(x_inds, x_vals) | ||
Y = UncertainIndexValueDataset(y_inds, y_vals); | ||
|
||
return X, Y | ||
end | ||
|
||
export example_uncertain_indexvalue_datasets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
resample!(v::AbstractArray{T, 1}, x::AbstractUncertainValue) | ||
resample!(v::AbstractArray{T, 1}, x::UVAL_COLLECTION_TYPES) where T | ||
resample!(v::AbstractArray{T, 2}, x::UVAL_COLLECTION_TYPES) where T | ||
resample!(idxs::::AbstractArray{T, 1}, vals::AbstractArray{T, 1}, | ||
x::AbstractUncertainIndexValueDataset) where T | ||
resample!(idxs::::AbstractArray{T, 2}, vals::AbstractArray{T, 2}, | ||
x::AbstractUncertainIndexValueDataset) where T | ||
Resample a uncertain value `x`, or a collection of uncertain values `x`, into a | ||
pre-allocated container `v`. | ||
## Uncertain values | ||
- If `x` is a single uncertain value, fill `v` with `length(v)` draws of `x`. | ||
## Uncertain collections | ||
- If `x` is a collection uncertain values and `v` is a vector, fill the `i`-th | ||
element of `v` with a draw of the `i`-th uncertain value in `x`. | ||
- If `x` is a collection of uncertain values and `v` is a 2D-array, fill the | ||
`i`-th column of `v` with `length(x)` draws of the `i`-th uncertain value | ||
in `x`. | ||
## Uncertain index-value collections | ||
- If two mutable vector-like containers, `idxs` and `vals`, are provided along | ||
with an uncertain index-value dataset `x`, then fill `idxs[i]` with a | ||
random draw from `x.indices[i]` and fill `vals[i]` with a random draw | ||
from `x.values[i]`. | ||
- If two mutable matrix-like containers, `idxs` and `vals`, are provided along | ||
with an uncertain index-value dataset `x` (where the number of | ||
columns in both `idxs` and `vals` matches `length(x)`), then fill the | ||
`i`-th column of `idxs` with `size(idxs, 1)` draws from `x.indices[i]`, | ||
and fill the `i`-th column of `vals` with `size(idxs, 1)` draws | ||
from `x.values[i]`. | ||
""" | ||
function resample! end | ||
|
||
function resample!(v, x::AbstractUncertainValue) | ||
v[:] = rand(x, length(v)) | ||
|
||
return v | ||
end | ||
|
||
function resample!(v, x::UVAL_COLLECTION_TYPES) where T | ||
for i in eachindex(v) | ||
@inbounds v[i] = rand(x[i]) | ||
end | ||
|
||
return v | ||
end | ||
|
||
function resample!(v::AbstractArray{T, 2}, x::UVAL_COLLECTION_TYPES) where T | ||
# The i-th column is filled with random values from the ith uncertain | ||
# value in the collection. | ||
n_draws = size(v, 1) | ||
n_vals = length(x) | ||
for i in 1:n_vals | ||
v[:, i] = rand(x[i], n_draws) | ||
end | ||
|
||
return v | ||
end | ||
|
||
function resample!(idxs::Vector{T}, vals::Vector{T}, x::AbstractUncertainIndexValueDataset) where T | ||
if !(length(idxs) == length(vals) == length(x)) | ||
error("`length(idxs) == length(vals) == length(x)` evaluated to false") | ||
end | ||
|
||
for i in eachindex(idxs) | ||
@inbounds idxs[i] = rand(x.indices[i]) | ||
@inbounds vals[i] = rand(x.values[i]) | ||
end | ||
|
||
return idxs, vals | ||
end | ||
|
||
function resample!(idxs::AbstractArray{T, 2}, vals::AbstractArray{T, 2}, x::AbstractUncertainIndexValueDataset) where T | ||
if !(size(idxs, 2) == size(vals, 2) == length(x)) | ||
error("`length(idxs) == length(vals) == length(x)` evaluated to false") | ||
end | ||
n_draws = size(idxs, 1) | ||
n_uvals = length(x) | ||
|
||
# The i-th column in `idxs` is filled with random values from the ith uncertain | ||
# index in the collection, and vice versa for `vals`. | ||
for i in 1:n_uvals | ||
@inbounds idxs[:, i] = rand(x.indices[i], n_draws) | ||
@inbounds vals[:, i] = rand(x.values[i], n_draws) | ||
end | ||
|
||
return idxs, vals | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
val = UncertainValue([1, 2, 3], [0.3, 0.3, 0.3]) | ||
val2 = UncertainValue([1, 2, 3], [0.3, 0.3, 0.3]) | ||
vals = UncertainValue([val2, val], [1, 2]) | ||
val3 = UncertainValue(Normal, 0, 1) | ||
|
||
|
||
|
||
################################### | ||
# Single values | ||
################################### | ||
x = zeros(Float64, 10) | ||
|
||
# There should be no zero entries after resampling in place | ||
resample!(x, val) | ||
@test any(x .== 0) == false | ||
|
||
resample!(x, vals) | ||
@test any(x .== 0) == false | ||
|
||
resample!(x, val3) | ||
@test any(x .== 0) == false | ||
|
||
################################### | ||
# Collections into vectors | ||
################################### | ||
v = fill(NaN, length(example_uvals)) | ||
resample!(v, example_uvals) | ||
@test any(isnan.(v)) == false | ||
|
||
################################### | ||
# Collections into arrays | ||
################################### | ||
arr = fill(NaN, 100, length(example_uvals)) | ||
resample!(arr, example_uvals) | ||
@test any(isnan.(arr)) == false | ||
|
||
|
||
################################################# | ||
# Uncertain index-value collections into vectors | ||
################################################# | ||
ids = UncertainIndexDataset(example_uidxs) | ||
vals = UncertainValueDataset(example_uvals) | ||
U = UncertainIndexValueDataset(ids, vals) | ||
|
||
idxs = fill(NaN, length(U)) | ||
vals = fill(NaN, length(U)) | ||
|
||
resample!(idxs, vals, U) | ||
|
||
@test any(isnan.(idxs)) == false | ||
@test any(isnan.(vals)) == false | ||
|
||
################################################# | ||
# Uncertain index-value collections into arrays | ||
################################################# | ||
ids = UncertainIndexDataset(example_uidxs) | ||
vals = UncertainValueDataset(example_uvals) | ||
U = UncertainIndexValueDataset(ids, vals) | ||
|
||
n_draws = 10 | ||
idxs = fill(NaN, n_draws, length(U)) | ||
vals = fill(NaN, n_draws, length(U)) | ||
|
||
resample!(idxs, vals, U) | ||
|
||
@test any(isnan.(idxs)) == false | ||
@test any(isnan.(vals)) == false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters