Skip to content

Commit

Permalink
Release 0.7.0 (#111)
Browse files Browse the repository at this point in the history
- Resampling in-place
- Added some example constraints.
  • Loading branch information
kahaaga authored Oct 19, 2019
1 parent 59ba7f6 commit e0a80dd
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ julia 1.1
StaticArrays
Bootstrap
Distributions
DynamicalSystemsBase
IntervalArithmetic 0.16
StatsBase
KernelDensity
Expand Down
3 changes: 2 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ PAGES = [
"resampling/resampling_schemes/resampling_schemes_uncertain_value_collections.md",
"resampling/resampling_schemes/resampling_schemes_uncertain_indexvalue_collections.md",
"resampling/resampling_schemes/resampling_with_schemes_uncertain_value_collections.md",
"resampling/resampling_schemes/resampling_with_schemes_uncertain_indexvalue_collections.md"
"resampling/resampling_schemes/resampling_with_schemes_uncertain_indexvalue_collections.md",
"resampling/resampling_inplace.md"
#"resampling/models/resampling_with_models.md"
],

Expand Down
2 changes: 1 addition & 1 deletion docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pages:
- Interpolation:
- Overview: resampling/interpolation/interpolation.md
- Gridded interpolation: resampling/interpolation/gridded.md

- Resampling in-place: resampling/resampling_inplace.md
- Mathematics:
- Elementary operations: mathematics/elementary_operations.md
- Trigonometric functions: mathematics/trig_functions.md
Expand Down
6 changes: 6 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

# Changelog

## Uncertaindata.jl v0.7.0

### New functionality

- Added `resample!` for in-place resampling into pre-allocated containers.

## UncertainData.jl v0.5.1

### Bug fixes
Expand Down
4 changes: 4 additions & 0 deletions docs/src/resampling/resampling_inplace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

```@docs
resample!
```
2 changes: 2 additions & 0 deletions src/UncertainData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ include("uncertain_values/operations/merging.jl")

# Example datasets
include("example_datasets/example_uvals.jl")
include("example_datasets/example_constraints.jl")
include("example_datasets/example_uncertainindexvalue_dataset.jl")

export UncertainScalarBinomialDistributed

Expand Down
61 changes: 61 additions & 0 deletions src/example_datasets/example_constraints.jl
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 src/example_datasets/example_uncertainindexvalue_dataset.jl
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
4 changes: 3 additions & 1 deletion src/resampling/Resampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ using Reexport
################################
include("resampling_with_schemes/methods_resamplings_schemes.jl")

export resample, resample_elwise
include("resampling_inplace.jl")

export resample, resample!, resample_elwise
end # module


Expand Down
94 changes: 94 additions & 0 deletions src/resampling/resampling_inplace.jl
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
67 changes: 67 additions & 0 deletions test/resampling/test_resampling_inplace.jl
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
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ include("resampling/uncertain_vectors/test_resampling_vectors.jl")
include("resampling/uncertain_datasets/sequential/test_resampling_sequential_increasing.jl")
include("resampling/uncertain_datasets/sequential/test_resampling_sequential_decreasing.jl")

# Resampling inplace.
#-----------------------------
include("resampling/test_resampling_inplace.jl")

#############################################
# Resampling uncertain datasets element-wise
#############################################
Expand Down

0 comments on commit e0a80dd

Please sign in to comment.