Skip to content

Commit e5c5cd0

Browse files
authored
Export fill_surrogate_test! (#161)
* Export fill_surrogate_test! I hope the title is self-explanatory! * put the inbounds elsewhere * correct typo * fix incorrect plot method * replace resolution with size for figure * fix columns typo * hopefully fix the surrogate tests * retrigger tests * finally find and fix the doc bug
1 parent 6a1cec8 commit e5c5cd0

9 files changed

+42
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
*Changelog is kept with respect to version 1.0. This software follows SymVer2.0*
22

3+
# 2.7
4+
5+
The function `fill_surrogate_test!` that was already in the source code and used by `pvalue` is now exported, so that if the users just want a parallelized estimation of surrogate discriminatory statistic they can just use this function.
6+
37
# 2.6
48
- Added surrogate methods: `RelativePartialRandomization`, `SpectralPartialRandomization`, `RelativePartialRandomizationAAFT`, and `SpectralPartialRandomizationAAFT`.
59

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "TimeseriesSurrogates"
22
uuid = "c804724b-8c18-5caa-8579-6025a0767c70"
33
authors = ["Kristian Agasøster Haaga <kahaaga@gmail.com>", "George Datseris"]
44
repo = "https://github.com/JuliaDynamics/TimeseriesSurrogates.jl.git"
5-
version = "2.6.4"
5+
version = "2.7.0"
66

77
[deps]
88
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

docs/src/collections/irregular_surrogates.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ This function uses the simulated annealing algorithm[^SchmitzSchreiber1999] to m
1010
```@example MAIN
1111
using TimeseriesSurrogates, CairoMakie, Random
1212
13-
# Example data: random AR1 process with a time axis with unevenly
13+
# Example data: random AR1 process with a time axis with unevenly
1414
# spaced time steps
1515
rng = Random.MersenneTwister(1234)
1616
x = AR1(n_steps = 300)
1717
N = length(x)
18-
t = (1:N) - rand(N)
18+
t = (1:N) - rand(N)
1919
2020
# Use simulated annealing based on convergence of Lomb-Scargle periodograms
2121
# The time series is relatively long, so set tolerance a bit higher than default.

docs/src/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ surrogenerator
3333

3434
```@docs
3535
SurrogateTest
36+
fill_surrogate_test!
3637
pvalue(::SurrogateTest)
3738
```
3839

@@ -96,7 +97,7 @@ RandomCascade
9697

9798
### Other
9899

99-
```@doc
100+
```@docs
100101
AutoRegressive
101102
ShuffleDimensions
102103
IrregularLombScargle

docs/src/methods/fourier_surrogates.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ surroplot(ts, s)
3030

3131
### Without rescaling
3232

33-
[`PartialRandomization`](@ref) surrogates are similar to random phase surrogates, but allow for tuning the "degree" of phase randomization.
34-
[`PartialRandomization`](@ref) use an algorithm introduced by [^Ortega1998], which draws random phases as:
33+
[`PartialRandomization`](@ref) surrogates are similar to random phase surrogates, but allow for tuning the "degree" of phase randomization.
34+
[`PartialRandomization`](@ref) use an algorithm introduced by Ortega et al., which draws random phases as:
3535

3636
$$\phi \to \alpha \xi , \quad \xi \sim \mathcal{U}(0, 2\pi),$$
3737

3838
where $\phi$ is a Fourier phase and $\mathcal{U}(0, 2\pi)$ is a uniform distribution.
39-
Tuning the randomization parameter, $\alpha$, produces a set of time series with varying degrees of randomness in their Fourier phases.
39+
Tuning the randomization parameter, $\alpha$, produces a set of time series with varying degrees of randomness in their Fourier phases.
4040

4141
```@example MAIN
4242
using TimeseriesSurrogates, CairoMakie

docs/src/methods/ppts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ method = PseudoPeriodicTwin(d, τ, δ, ρ)
1818
surr_orbit = surrogate(x, method)
1919
2020
# Get scalar surrogate time series from first and second column.
21-
s1, s2 = columns(surr_orbit)
21+
s1, s2 = surr_orbit[:, 1], surr_orbit[:, 2]
2222
2323
# Scalar time series versus surrogate time series
2424
fig = Figure()

ext/TimeseriesSurrogatesVisualizations.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function TimeseriesSurrogates.surroplot!(fig, x, a;
77
)
88

99
t = 1:length(x)
10-
# make surrogate timeseries
11-
s = a isa Surrogate ? surrogate(x, method) : a
10+
# make surrogate timeseries (allow possibility for `a` to be a timeseries or method)
11+
s = a isa Surrogate ? surrogate(x, a) : a
1212

1313
# Time series
1414
ax1, _ = Makie.lines(fig[1, 1], t, x; color=cx, linewidth=2)
@@ -37,7 +37,7 @@ end
3737

3838
function TimeseriesSurrogates.surroplot(x, s;
3939
cx="#191E44", cs=("#7143E0", 0.9), nbins=50, kwargs...)
40-
fig = Makie.Figure(resolution=(500, 500), kwargs...)
40+
fig = Makie.Figure(size=(500, 500), kwargs...)
4141
surroplot!(fig, x, s; cx, cs, nbins)
4242
end
4343

src/core/surrogate_test.jl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Random: AbstractRNG
22
import StatsAPI: HypothesisTest, pvalue
3-
export SurrogateTest, pvalue
3+
export SurrogateTest, pvalue, fill_surrogate_test!
44
using Base.Threads
55

66
"""
@@ -11,8 +11,11 @@ The tests requires as input a function `f` that given a timeseries (like `x`) it
1111
outputs a real number, and a method of how to generate surrogates.
1212
`f` is the function that computes the discriminatory statistic.
1313
14-
Once called with [`pvalue`](@ref), the `test` stores the real value `rval` and surrogate
14+
Once called with [`pvalue`](@ref), the `test` estimates and then
15+
stores the real value `rval` and surrogate
1516
values `vals` of the discriminatory statistic in the fields `rval, vals` respectively.
17+
Alternatively, you can use [`fill_surrogate_test!`](@ref) directly if you don't care about
18+
the p-value.
1619
1720
`SurrogateTest` automates the process described in the documentation page
1821
[Performing surrogate hypothesis tests](@ref).
@@ -24,7 +27,7 @@ values `vals` of the discriminatory statistic in the fields `rval, vals` respect
2427
- `rng = Random.default_rng()`: a random number generator.
2528
- `n::Int = 10_000`: how many surrogates to generate and compute `f` on.
2629
- `threaded = true`: Whether to parallelize looping over surrogate computations in
27-
[`pvalue`](@ref) to the available threads (`Threads.nthreads()`).
30+
to the available threads (`Threads.nthreads()`).
2831
"""
2932
struct SurrogateTest{F<:Function, S<:SurrogateGenerator, X<:Real} <: HypothesisTest
3033
f::F
@@ -71,19 +74,28 @@ function Base.show(io::IO, ::MIME"text/plain", test::SurrogateTest)
7174
return
7275
end
7376

74-
@inbounds function fill_surrogate_test!(test::SurrogateTest)
77+
"""
78+
fill_surrogate_test!(test::SurrgateTest) → rval, vals
79+
80+
Perform the computations foreseen by `test` and return
81+
the value of the discriminatory statistic for the real data `rval`
82+
and the distribution of values for the surrogates `vals`.
83+
84+
This function is called by `pvalue`.
85+
"""
86+
function fill_surrogate_test!(test::SurrogateTest)
7587
if test.threaded
76-
Threads.@threads for i in eachindex(test.vals)
88+
@inbounds Threads.@threads for i in eachindex(test.vals)
7789
sgen = test.sgens[Threads.threadid()]
7890
test.vals[i] = test.f(sgen())
7991
end
8092
else
8193
sgen = first(sgens)
82-
for i in eachindex(test.vals)
94+
@inbounds for i in eachindex(test.vals)
8395
test.vals[i] = test.f(sgen())
8496
end
8597
end
86-
return
98+
return test.rval, test.vals
8799
end
88100

89101
"""
@@ -102,8 +114,7 @@ discriminatory statistic values. This is the case for statistics that quantify e
102114
For statistics that quantify autocorrelation, use `tail = :right` instead.
103115
"""
104116
function pvalue(test::SurrogateTest; tail = :left)
105-
fill_surrogate_test!(test)
106-
(; rval, vals) = test
117+
rval, vals = fill_surrogate_test!(test)
107118
if tail == :right
108119
p = count(v -> v rval, vals)
109120
elseif tail == :left

test/test_test.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ test = SurrogateTest(q, x, RandomFourier(); n = 1000, rng)
1313

1414
# the AR1 process is much more correlated than its surrogates!
1515
p = pvalue(test)
16-
p > 0.9
16+
@test p > 0.9
1717
p = pvalue(test; tail = :right)
18-
@test p < 0.1
18+
@test p < 0.1
19+
20+
test = SurrogateTest(q, x, RandomFourier(); n = 1000, rng)
21+
rval, vals = fill_surrogate_test!(test)
22+
@test minimum(vals) rval maximum(vals)

0 commit comments

Comments
 (0)