-
Notifications
You must be signed in to change notification settings - Fork 30
add coupledSDEs type #212
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
Merged
Merged
add coupledSDEs type #212
Changes from 19 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
30d0970
add coupledSDEs type
oameye a11ba2c
add CoupledSDEs docs
oameye 62c5e7e
apply suggested format changes
oameye 89225a4
add idfunc
oameye f169386
Fix docs?
oameye 43ad5e9
change coupled sde example to use trajectory
oameye 8de87b8
specify which CoupledODEs docstring
oameye 023ff0c
change to SciML default tolerances
oameye f6a400e
classify noise
oameye 5c5e6dc
correct spelling of invertible
oameye ce7a03e
make noisetype field a namedtuple
oameye 573f18f
Merge branch 'main' into CoupledSDEs
Datseris fba7327
make coupledSDEs an extention
oameye e01bce6
Merge branch 'CoupledSDEs' of https://github.com/oameye/DynamicalSyst…
oameye c875b5e
export pretty print
oameye b3ccb26
export CoupledODEs
oameye b08fd7c
add CoupledODEs <-> CoupledSDEs tests
oameye dfc3053
add extention to modules in makedocs
oameye daca4a7
define dynamics rule sde specific
oameye 73af990
port docs into the docstring
Datseris 92e3369
delete ported documentation
Datseris 548130d
fix small typos in docstring
oameye 1a0c884
add CoupledSDEs examples to docs
oameye 413a25a
remove noise strength
oameye 381820f
update docs with no noise strength
oameye cb956e8
add covariance kwarg and change g to kwarg
oameye 17cf5cb
update docs
oameye ba148d9
diffusion <-> covariance matrix
oameye eb8544b
add noise_strength kwarg
oameye e28b4dd
estimate invertiability with a tolerance
oameye 39057fb
move to SOSRA with non-adaptive timesteps
oameye 400ac12
Merge branch 'main' into CoupledSDEs
oameye 41c900b
Reyk's comments
oameye e5bc61a
implemented review comments - part 1
reykboerner f907929
revised CoupledSDEs docstring
reykboerner 75996be
remove broken docs link
reykboerner 7189029
implemented review comments - part 2
reykboerner 109e7b4
proofread and revised CoupledSDEs docs
reykboerner 5df534a
fixed bug that made tests fail
reykboerner 78f25ee
Update CHANGELOG.md
Datseris 9368e7d
Merge branch 'main' into CoupledSDEs
Datseris de8258e
Update Project.toml
Datseris 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 hidden or 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 |
---|---|---|
|
@@ -6,4 +6,5 @@ deps/build.log | |
Manifest.toml | ||
*style.jl | ||
*.scss | ||
*.css | ||
*.css | ||
.vscode |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
# Define a CoupledSDEs system | ||
|
||
A `CoupledSDEs` defines a stochastic dynamical system of the form | ||
|
||
```math | ||
\text{d}\vec x = f(\vec x(t); \ p) \text{d}t + \sigma g(\vec x(t); \ p) \, \text{d}\mathcal{W} \ , | ||
``` | ||
where $\vec x \in \mathbb{R}^\text{D}$, $\sigma > 0$ is the noise strength, $\text{d}\mathcal{W}=\Gamma \cdot \text{d}\mathcal{N}$, and $\mathcal N$ denotes a stochastic process. The (positive definite) noise covariance matrix is $\Sigma = \Gamma \Gamma^\top \in \mathbb R^{N\times N}$. | ||
|
||
The function $f$ is the deterministic part of the system and follows the syntax of a `ContinuousTimeDynamicalSystem` in [DynamicalSystems.jl](https://juliadynamics.github.io/DynamicalSystems.jl/latest/tutorial/), i.e., `f(u, p, t)` for out-of-place (oop) and `f!(du, u, p, t)` for in-place (iip). The function $g$ allows to specify the stochastic dynamics of the system along with the [noise process](#noise-process) $\mathcal{W}$. It should be of the same type (iip or oop) as $f$. | ||
|
||
By combining $\sigma$, $g$ and $\mathcal{W}$, you can define different type of stochastic systems. Examples of different types of stochastic systems can be found on the [StochasticDiffEq.jl tutorial page](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/sde_example/). A quick overview of common types of stochastic systems can be found [below](#Defining-stochastic-dynamics). | ||
|
||
!!! info | ||
Note that nonlinear mixings of the Noise Process $\mathcal{W}$ fall into the class of random ordinary differential equations (RODEs) and is not supported in DynamicalSystems.jl at the moment. See [DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/rode_example/) for an implementation. | ||
|
||
```@docs | ||
CoupledSDEs | ||
``` | ||
|
||
## Converting between `CoupledSDEs` and `CoupledODEs` | ||
|
||
!!! tip "Analyzing deterministic dynamics with DynamicalSystems.jl" | ||
The deterministic part of a [`CoupledSDEs`](@ref) system can easily be extracted as a | ||
[`CoupledODEs`](https://juliadynamics.github.io/DynamicalSystems.jl/dev/tutorial/#DynamicalSystemsBase.CoupledODEs), a common subtype of a `ContinuousTimeDynamicalSystem` in DynamicalSystems.jl. | ||
|
||
- `CoupledODEs(sde::CoupledSDEs)` extracts the deterministic part of `sde` as a `CoupledODEs` | ||
- `CoupledSDEs(ode::CoupledODEs, g)`, with `g` the noise function, turns `ode` into a `CoupledSDEs` | ||
|
||
```@docs | ||
CoupledODEs(::CoupledSDEs; kwargs...) | ||
``` | ||
For example, the | ||
Lyapunov spectrum of a `CoupledSDEs` in the absence of noise, here exemplified by the | ||
FitzHugh-Nagumo model, can be computed by typing: | ||
|
||
```julia | ||
using DynamicalSystemsBase, StaticArrays | ||
using DynamicalSystemsBase: idfunc | ||
|
||
function fitzhugh_nagumo(u, p, t) | ||
x, y = u | ||
ϵ, β, α, γ, κ, I = p | ||
|
||
dx = (-α * x^3 + γ * x - κ * y + I) / ϵ | ||
dy = -β * y + x | ||
|
||
return SA[dx, dy] | ||
end | ||
|
||
sde = CoupledSDEs(fitzhugh_nagumo, idfunc, zeros(2), [1.,3.,1.,1.,1.,0.], 0.1) | ||
tr = trajectory(sde, 10) | ||
``` | ||
|
||
## Available noise processes | ||
We provide the noise processes $\mathcal{W}$ that can be used in the stochastic simulations through the [DiffEqNoiseProcess.jl](https://docs.sciml.ai/DiffEqNoiseProcess/stable) package. A complete list of the available processes can be found [here](https://docs.sciml.ai/DiffEqNoiseProcess/stable/noise_processes/). | ||
``` | ||
|
||
|
This file contains hidden or 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,17 @@ | ||
module StochasticSystemsBase | ||
|
||
using DynamicalSystemsBase: DynamicalSystemsBase, SciMLBase, correct_state, CoupledODEs, | ||
CoupledSDEs, StateSpaceSets, isinplace, _delete, set_parameter!, | ||
set_state!, dynamic_rule, isdeterministic, current_state, | ||
DynamicalSystemsBase, _set_parameter!, u_modified!, | ||
additional_details, referrenced_sciml_prob, DEFAULT_DIFFEQ, SVector | ||
using SciMLBase: SDEProblem, AbstractSDEIntegrator, __init, SDEFunction, step! | ||
using StochasticDiffEq: SOSRI | ||
using LinearAlgebra | ||
|
||
include("src/CoupledSDEs.jl") | ||
include("src/classification.jl") | ||
|
||
export CoupledSDEs, CoupledODEs | ||
|
||
end |
This file contains hidden or 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,206 @@ | ||
########################################################################################### | ||
# DiffEq options | ||
########################################################################################### | ||
|
||
const DEFAULT_SDE_SOLVER = SOSRI() # default sciml solver | ||
const DEFAULT_STOCH_DIFFEQ_KWARGS = (abstol = 1e-2, reltol = 1e-2) # default sciml tol | ||
const DEFAULT_STOCH_DIFFEQ = (alg = DEFAULT_SDE_SOLVER, DEFAULT_STOCH_DIFFEQ_KWARGS...) | ||
|
||
# Function from user `@xlxs4`, see | ||
# https://github.com/JuliaDynamics/jl/pull/153 | ||
function _decompose_into_sde_solver_and_remaining(diffeq) | ||
if haskey(diffeq, :alg) | ||
return (diffeq[:alg], _delete(diffeq, :alg)) | ||
else | ||
return (DEFAULT_SDE_SOLVER, diffeq) | ||
end | ||
end | ||
|
||
########################################################################################### | ||
# Constructor functions | ||
########################################################################################### | ||
|
||
function DynamicalSystemsBase.CoupledSDEs( | ||
f, | ||
g, | ||
u0, | ||
p = SciMLBase.NullParameters(), | ||
noise_strength = 1.0; | ||
t0 = 0.0, | ||
diffeq = DEFAULT_STOCH_DIFFEQ, | ||
noise_rate_prototype = nothing, | ||
noise = nothing, | ||
seed = UInt64(0) | ||
) | ||
IIP = isinplace(f, 4) # from SciMLBase | ||
@assert IIP==isinplace(g, 4) "f and g must both be in-place or out-of-place" | ||
|
||
s = correct_state(Val{IIP}(), u0) | ||
T = eltype(s) | ||
prob = SDEProblem{IIP}( | ||
f, | ||
g, | ||
s, | ||
(T(t0), T(Inf)), | ||
p; | ||
noise_rate_prototype = noise_rate_prototype, | ||
noise = noise, | ||
seed = seed | ||
) | ||
return CoupledSDEs(prob, diffeq, noise_strength) | ||
end | ||
|
||
function DynamicalSystemsBase.CoupledSDEs( | ||
prob::SDEProblem, diffeq = DEFAULT_STOCH_DIFFEQ, noise_strength = 1.0; special_kwargs... | ||
) | ||
if haskey(special_kwargs, :diffeq) | ||
throw( | ||
ArgumentError( | ||
"`diffeq` is given as positional argument when an ODEProblem is provided." | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
) | ||
end | ||
IIP = isinplace(prob) # from SciMLBase | ||
D = length(prob.u0) | ||
P = typeof(prob.p) | ||
if prob.tspan === (nothing, nothing) | ||
# If the problem was made via MTK, it is possible to not have a default timespan. | ||
U = eltype(prob.u0) | ||
prob = SciMLBase.remake(prob; tspan = (U(0), U(Inf))) | ||
end | ||
sde_function = SDEFunction(prob.f, add_noise_strength(noise_strength, prob.g, IIP)) | ||
prob = SciMLBase.remake(prob; f = sde_function) | ||
noise_type = find_noise_type(prob, IIP) | ||
|
||
solver, remaining = _decompose_into_sde_solver_and_remaining(diffeq) | ||
integ = __init( | ||
prob, | ||
solver; | ||
remaining..., | ||
# Integrators are used exclusively iteratively. There is no reason to save anything. | ||
save_start = false, | ||
save_end = false, | ||
save_everystep = false, | ||
# DynamicalSystems.jl operates on integrators and `step!` exclusively, | ||
# so there is no reason to limit the maximum time evolution | ||
maxiters = Inf | ||
) | ||
return CoupledSDEs{IIP, D, typeof(integ), P, true}( | ||
integ, deepcopy(prob.p), noise_strength, diffeq, noise_type | ||
) | ||
end | ||
|
||
""" | ||
CoupledSDEs(ds::CoupledODEs, g, p [, σ]; kwargs...) | ||
|
||
Converts a [`CoupledODEs` | ||
](https://juliadynamics.github.io/DynamicalSystems.jl/stable/tutorial/#CoupledODEs) | ||
system into a [`CoupledSDEs`](@ref). | ||
""" | ||
function DynamicalSystemsBase.CoupledSDEs( | ||
ds::CoupledODEs, | ||
g, | ||
p, # the parameter is likely changed as the diffusion function g is added. | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
noise_strength = 1.0; | ||
diffeq = DEFAULT_STOCH_DIFFEQ, | ||
noise_rate_prototype = nothing, | ||
noise = nothing, | ||
seed = UInt64(0) | ||
) | ||
return CoupledSDEs( | ||
dynamic_rule(ds), | ||
g, | ||
current_state(ds), | ||
p, | ||
noise_strength; | ||
diffeq = diffeq, | ||
noise_rate_prototype = noise_rate_prototype, | ||
noise = noise, | ||
seed = seed | ||
) | ||
end | ||
|
||
""" | ||
CoupledODEs(ds::CoupledSDEs; kwargs...) | ||
|
||
Converts a [`CoupledSDEs`](@ref) into [`CoupledODEs`](@ref). | ||
""" | ||
function DynamicalSystemsBase.CoupledODEs( | ||
sys::CoupledSDEs; diffeq = DEFAULT_DIFFEQ, t0 = 0.0) | ||
return CoupledODEs( | ||
dynamic_rule(sys), SVector{length(sys.integ.u)}(sys.integ.u), sys.p0; | ||
diffeq = diffeq, t0 = t0 | ||
) | ||
end | ||
|
||
# Pretty print | ||
function DynamicalSystemsBase.additional_details(ds::CoupledSDEs) | ||
solver, remaining = _decompose_into_sde_solver_and_remaining(ds.diffeq) | ||
return [ | ||
"Noise strength" => ds.noise_strength, | ||
"SDE solver" => string(nameof(typeof(solver))), | ||
"SDE kwargs" => remaining, | ||
"Noise type" => ds.noise_type | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
end | ||
|
||
########################################################################################### | ||
# API - obtaining information from the system | ||
########################################################################################### | ||
|
||
SciMLBase.isinplace(::CoupledSDEs{IIP}) where {IIP} = IIP | ||
StateSpaceSets.dimension(::CoupledSDEs{IIP, D}) where {IIP, D} = D | ||
DynamicalSystemsBase.current_state(ds::CoupledSDEs) = current_state(ds.integ) | ||
DynamicalSystemsBase.isdeterministic(ds::CoupledSDEs) = false | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function DynamicalSystemsBase.dynamic_rule(ds::CoupledSDEs) | ||
# with remake it can happen that we have nested SDEFunctions | ||
# sciml integrator deals with this internally well | ||
f = ds.integ.f | ||
while hasfield(typeof(f), :f) | ||
f = f.f | ||
end | ||
return f | ||
end | ||
|
||
# so that `ds` is printed | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function DynamicalSystemsBase.set_state!(ds::CoupledSDEs, u::AbstractArray) | ||
(set_state!(ds.integ, u); ds) | ||
end | ||
SciMLBase.step!(ds::CoupledSDEs, args...) = (step!(ds.integ, args...); ds) | ||
|
||
# TODO We have to check if for SDEIntegrators a different step interruption is possible. | ||
function DynamicalSystemsBase.successful_step(integ::AbstractSDEIntegrator) | ||
rcode = integ.sol.retcode | ||
return rcode == SciMLBase.ReturnCode.Default || rcode == SciMLBase.ReturnCode.Success | ||
end | ||
|
||
# This is here to ensure that `u_modified!` is called | ||
function DynamicalSystemsBase.set_parameter!(ds::CoupledSDEs, args...) | ||
_set_parameter!(ds, args...) | ||
u_modified!(ds.integ, true) | ||
return nothing | ||
end | ||
|
||
DynamicalSystemsBase.referrenced_sciml_prob(ds::CoupledSDEs) = ds.integ.sol.prob | ||
|
||
########################################################################################### | ||
# Utilities | ||
########################################################################################### | ||
|
||
function σg(σ, g) | ||
return (u, p, t) -> σ .* g(u, p, t) | ||
end | ||
|
||
function σg!(σ, g!) | ||
function (du, u, p, t) | ||
g!(du, u, p, t) | ||
du .*= σ | ||
return nothing | ||
end | ||
end | ||
|
||
function add_noise_strength(σ, g, IIP) | ||
oameye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newg = IIP ? σg!(σ, g) : σg(σ, g) | ||
return newg | ||
end |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.