-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Gaussian Noise Process submodule #62
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b53cfd8
Add Gaussian Noise Process
kdbarajas 81bcb1f
Add submodule for Gaussian Noise Process
kdbarajas 5d5c29b
Add dependencies
marwahaha 5c4ad4c
Update tests
marwahaha 88d7366
add dep
marwahaha 6095e75
Move StochasticDiffEq
marwahaha b25068c
Prepare python
marwahaha 1ab7ead
Use python3
marwahaha e9cc3fb
Use python3
marwahaha c672b5f
Set python
marwahaha 144cc69
Update with master
marwahaha e5a3c0d
Try to add noise code
marwahaha 5429fa7
Add dep
marwahaha 443215a
Format and get test working
marwahaha 82df58c
Add better type-checking
marwahaha bdbf855
Add test
marwahaha 3811af2
Remove comments
marwahaha 1cbb0d6
Reduce tolerance
marwahaha 08a2b3a
merge with master
marwahaha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,14 +4,21 @@ authors = ["Joseph Broz <[email protected]>"] | |
version = "0.4.2" | ||
|
||
[deps] | ||
DiffEqNoiseProcess = "77a26b50-5914-5dd7-bc55-306e6241c503" | ||
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" | ||
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e" | ||
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56" | ||
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" | ||
PolynomialRoots = "3a141323-8675-5d76-9d11-e1df1406c778" | ||
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" | ||
QuantumOptics = "6e0679c1-51ea-5a7c-ac74-d61b76210b0c" | ||
QuantumOpticsBase = "4f57444f-1401-5e15-980d-4471b28d5678" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" | ||
WignerSymbols = "9f57e263-0b3d-5e2e-b1be-24f2bb48858b" | ||
|
||
[compat] | ||
|
@@ -27,7 +34,6 @@ julia = "1.3" | |
[extras] | ||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2" | ||
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0" | ||
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
|
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,186 @@ | ||
using DiffEqNoiseProcess | ||
using LinearAlgebra: dot | ||
using Random | ||
using PyCall | ||
|
||
@inline wiener_randn(rng::AbstractRNG, ::Type{T}) where {T} = randn(rng, T) | ||
function arma_step!(x, mean, sigma, rng, T) | ||
return x[1] = mean + sigma * wiener_randn(rng, T) | ||
end | ||
|
||
# == Generators of Discrete AR/MA time series == # | ||
mutable struct AR_Process{T1, T2, T3, T4, T5} | ||
phi::T1 | ||
p::T2 | ||
sigma::T3 | ||
mean::T3 | ||
c::T3 | ||
dt::T3 | ||
t::T3 | ||
filter::T4 | ||
past::T5 | ||
end | ||
|
||
mutable struct MA_Process{T1, T2, T3, T4, T5} | ||
psi::T1 | ||
q::T2 | ||
sigma::T3 | ||
mean::T3 | ||
dt::T3 | ||
t::T3 | ||
et::T4 | ||
filter::T5 | ||
end | ||
|
||
@inline function (X::AR_Process)(dW, W, dt, u, p, t, rng) | ||
T = (typeof(dW) <: AbstractArray) ? dW : typeof(dW) | ||
|
||
# == derivative of step function == # | ||
ϵ = t isa Union{Rational, Integer} ? 0 : 100eps(typeof(t)) | ||
t += dt | ||
if t < X.t + ϵ | ||
return 0 #dW = 0 between steps | ||
end | ||
|
||
# == AR(p) Step == # | ||
while t >= X.t + ϵ | ||
mean = X.c + dot(X.phi, X.past) | ||
pushfirst!(X.past, 0.0) | ||
pop!(X.past) | ||
|
||
arma_step!(X.past, mean, X.sigma, rng, T) | ||
while X.filter(X.past[1] - X.mean) #Filter Function | ||
arma_step!(X.past, mean, X.sigma, rng, T) | ||
end | ||
|
||
X.t += X.dt | ||
end | ||
|
||
return X.past[1] * max(dt, X.dt) | ||
end | ||
|
||
@inline function (X::MA_Process)(dW, W, dt, u, p, t, rng) | ||
T = (typeof(dW) <: AbstractArray) ? dW : typeof(dW) | ||
|
||
# == derivative of step function == # | ||
ϵ = t isa Union{Rational, Integer} ? 0 : 100eps(typeof(t)) | ||
t += dt | ||
if t < X.t + ϵ | ||
return 0 #dW = 0 between steps | ||
end | ||
|
||
# == MA(∞) Step == # | ||
Xt = 0 | ||
while t >= X.t + ϵ | ||
pushfirst!(X.et, 0.0) #add new first value | ||
pop!(X.et) #remove last value | ||
arma_step!(X.et, 0.0, X.sigma, rng, T) | ||
Xt = X.mean + dot(X.psi, X.et) | ||
|
||
while X.filter(Xt - X.mean) #Filter Function | ||
arma_step!(X.et, 0, X.sigma, rng, T) | ||
Xt = X.mean + dot(X.psi, X.et) | ||
end | ||
|
||
X.t += X.dt | ||
end | ||
return Xt * max(dt, X.dt) | ||
end | ||
|
||
# == Bridge Function == # | ||
@inline function AR_STEP_BRIDGE(X, dW, W, W0, Wh, q, h, u, p, t, rng) | ||
# setup_next_step! correction | ||
if isapprox(W0, W.curW) # line 97 | ||
return AR_STEP_BRIDGE(X, dW, W, 1, Wh, q, h, u, p, t, rng) | ||
end | ||
|
||
if t isa Union{Rational, Integer} | ||
i = searchsortedlast(W.t, h * floor(t / h)) | ||
else | ||
i = searchsortedlast(W.t, h * floor(t / h) + eps(typeof(t))) | ||
end | ||
return W[i] - (1 - q) * W0 | ||
end | ||
|
||
# reject_step! correction | ||
# https://github.com/SciML/DiffEqNoiseProcess.jl/blob/master/src/noise_interfaces/noise_process_interface.jl | ||
@inline function AR_STEP_BRIDGE(X, dW, W, W0::Int, Wh, q, h, u, p, t, rng) | ||
|
||
# line 203 correction | ||
# q = dtnew / W.dt -> h = W.dt (not dtnew) | ||
if isempty(W.S₂) && isapprox(h, q * W.dt) # if h=dtnew | ||
h = W.dt | ||
end | ||
|
||
tnew = t + q * h | ||
tfloor = ( | ||
X.dt * floor(round(t / X.dt, digits = 8)), | ||
X.dt * floor(round(tnew / X.dt, digits = 8)) | ||
) | ||
ϵ = t isa Union{Rational, Integer} ? 0 : sqrt(eps(typeof(t))) | ||
|
||
while tnew < (X.t - X.dt) + ϵ | ||
X.t -= X.dt | ||
popfirst!(X.past) | ||
push!(X.past, 0.0) | ||
# X.past .= [X.past[2:end]; 0.0] | ||
end | ||
|
||
# both in same step | ||
if isapprox(tfloor[1], tfloor[2]) | ||
dWnew = 0.0 | ||
# both in different steps | ||
else | ||
dWnew = X.past[1] * min(q * h, X.dt) | ||
end | ||
|
||
return dWnew | ||
end | ||
|
||
@inline function MA_STEP_BRIDGE(X, dW, W, W0, Wh, q, h, u, p, t, rng) | ||
# setup_next_step! correction | ||
if isapprox(W0, W.curW) # line 97 | ||
return MA_STEP_BRIDGE(X, dW, W, 1, Wh, q, h, u, p, t, rng) | ||
end | ||
|
||
if t isa Union{Rational, Integer} | ||
i = searchsortedlast(W.t, h * floor(t / h)) | ||
else | ||
i = searchsortedlast(W.t, h * floor(t / h) + eps(typeof(t))) | ||
end | ||
return W[i] - (1 - q) * W0 | ||
end | ||
|
||
# reject_step! correction | ||
# https://github.com/SciML/DiffEqNoiseProcess.jl/blob/master/src/noise_interfaces/noise_process_interface.jl | ||
@inline function MA_STEP_BRIDGE(X, dW, W, W0::Int, Wh, q, h, u, p, t, rng) | ||
|
||
# line 203 correction | ||
# q = dtnew / W.dt -> h = W.dt (not dtnew) | ||
if isempty(W.S₂) && isapprox(h, q * W.dt) # if h=dtnew | ||
h = W.dt | ||
end | ||
|
||
tnew = t + q * h | ||
tfloor = ( | ||
X.dt * floor(round(t / X.dt, digits = 8)), | ||
X.dt * floor(round(tnew / X.dt, digits = 8)) | ||
) | ||
ϵ = t isa Union{Rational, Integer} ? 0 : sqrt(eps(typeof(t))) | ||
|
||
while tnew < (X.t - X.dt) + ϵ | ||
X.t -= X.dt | ||
popfirst!(X.et) # remove last innovation | ||
push!(X.et, 0.0) # add zero to end | ||
end | ||
|
||
# both in same step | ||
if isapprox(tfloor[1], tfloor[2]) | ||
dWnew = 0.0 | ||
# both in different steps | ||
else | ||
dWnew = (X.mean + dot(X.psi, X.et)) * min(q * h, X.dt) | ||
end | ||
|
||
return dWnew | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be docstrings, correct?