-
-
Notifications
You must be signed in to change notification settings - Fork 224
DE Transformation (Change of Variables) #3695
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
Open
fchen121
wants to merge
16
commits into
SciML:master
Choose a base branch
from
fchen121:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−1
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6b0f03c
Created change of variable for SDE
24ebd99
Merge branch 'iss141'
e0bb5d2
Implement and fix change of variable for ODE
86411c2
Merge branch 'SciML:master' into master
fchen121 22ab624
Implement change of variables for sde
8ace5e7
Merge branch 'SciML:master' into master
fchen121 50a4fc3
Fix Linear transformation to diagonal system test
a1e9944
Update Project.toml
7b3ea99
Merge branch 'master' of https://github.com/fchen121/ModelingToolkit.jl
e33bcc1
Change backward subs from observed to equations
192872c
Change of variables for multiple Brownian SDE
9a53121
Improve change of variables test
8140b62
Combined change of variable function for ODE and SDE
c0453f4
Change of variable for non-simplified SDE
166b0f8
Update test/changeofvariables.jl
ChrisRackauckas 0884e59
Update src/systems/diffeqs/basic_transformations.jl
ChrisRackauckas 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
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,150 @@ | ||
using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq | ||
using Test, LinearAlgebra | ||
|
||
|
||
# Change of variables: z = log(x) | ||
# (this implies that x = exp(z) is automatically non-negative) | ||
@independent_variables t | ||
@variables z(t)[1:2, 1:2] | ||
D = Differential(t) | ||
eqs = [D(D(z)) ~ ones(2, 2)] | ||
@mtkcompile sys = System(eqs, t) | ||
@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0)) | ||
|
||
@parameters α | ||
@variables x(t) | ||
D = Differential(t) | ||
eqs = [D(x) ~ α*x] | ||
|
||
tspan = (0., 1.) | ||
def = [x => 1.0, α => -0.5] | ||
|
||
@mtkcompile sys = System(eqs, t;defaults=def) | ||
prob = ODEProblem(sys, [], tspan) | ||
sol = solve(prob, Tsit5()) | ||
|
||
@variables z(t) | ||
forward_subs = [log(x) => z] | ||
backward_subs = [x => exp(z)] | ||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs) | ||
@test equations(new_sys)[1] == (D(z) ~ α) | ||
|
||
new_prob = ODEProblem(new_sys, [], tspan) | ||
new_sol = solve(new_prob, Tsit5()) | ||
|
||
@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4) | ||
|
||
|
||
|
||
# Riccati equation | ||
@parameters α | ||
@variables x(t) | ||
D = Differential(t) | ||
eqs = [D(x) ~ t^2 + α - x^2] | ||
def = [x=>1., α => 1.] | ||
@mtkcompile sys = System(eqs, t; defaults=def) | ||
|
||
@variables z(t) | ||
forward_subs = [t + α/(x+t) => z ] | ||
backward_subs = [ x => α/(z-t) - t] | ||
|
||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.) | ||
# output should be equivalent to | ||
# t^2 + α - z^2 + 2 (but this simplification is not found automatically) | ||
|
||
tspan = (0., 1.) | ||
prob = ODEProblem(sys,[],tspan) | ||
new_prob = ODEProblem(new_sys,[],tspan) | ||
|
||
sol = solve(prob, Tsit5()) | ||
new_sol = solve(new_prob, Tsit5()) | ||
|
||
@test isapprox(sol[x][end], new_sol[x][end], rtol=1e-4) | ||
|
||
|
||
# Linear transformation to diagonal system | ||
@independent_variables t | ||
@variables x(t)[1:3] | ||
x = reshape(x, 3, 1) | ||
D = Differential(t) | ||
A = [0. -1. 0.; -0.5 0.5 0.; 0. 0. -1.] | ||
right = A*x | ||
eqs = vec(D.(x) .~ right) | ||
|
||
tspan = (0., 10.) | ||
u0 = [x[1] => 1.0, x[2] => 2.0, x[3] => -1.0] | ||
|
||
@mtkcompile sys = System(eqs, t; defaults=u0) | ||
prob = ODEProblem(sys,[],tspan) | ||
sol = solve(prob, Tsit5()) | ||
|
||
T = eigen(A).vectors | ||
T_inv = inv(T) | ||
|
||
@variables z(t)[1:3] | ||
z = reshape(z, 3, 1) | ||
forward_subs = vec(T_inv*x .=> z) | ||
backward_subs = vec(x .=> T*z) | ||
|
||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true) | ||
|
||
new_prob = ODEProblem(new_sys, [], tspan) | ||
new_sol = solve(new_prob, Tsit5()) | ||
|
||
# test RHS | ||
new_rhs = [eq.rhs for eq in equations(new_sys)] | ||
new_A = Symbolics.value.(Symbolics.jacobian(new_rhs, z)) | ||
A = diagm(eigen(A).values) | ||
A = sortslices(A, dims=1) | ||
new_A = sortslices(new_A, dims=1) | ||
@test isapprox(A, new_A, rtol = 1e-10) | ||
@test isapprox( new_sol[x[1],end], sol[x[1],end], rtol=1e-4) | ||
|
||
# Change of variables for sde | ||
noise_eqs = ModelingToolkit.get_noise_eqs | ||
value = ModelingToolkit.value | ||
|
||
@independent_variables t | ||
@brownians B | ||
@parameters μ σ | ||
@variables x(t) y(t) | ||
D = Differential(t) | ||
eqs = [D(x) ~ μ*x + σ*x*B] | ||
|
||
def = [x=>0., μ => 2., σ=>1.] | ||
@mtkcompile sys = System(eqs, t; defaults=def) | ||
forward_subs = [log(x) => y] | ||
backward_subs = [x => exp(y)] | ||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs) | ||
@test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2) | ||
@test noise_eqs(new_sys)[1] === value(σ) | ||
|
||
#Multiple Brownian and equations | ||
@independent_variables t | ||
@brownians Bx By | ||
@parameters μ σ α | ||
@variables x(t) y(t) z(t) w(t) u(t) v(t) | ||
D = Differential(t) | ||
eqs = [D(x) ~ μ*x + σ*x*Bx, D(y) ~ α*By, D(u) ~ μ*u + σ*u*Bx + α*u*By] | ||
def = [x=>0., y=> 0., u=>0., μ => 2., σ=>1., α=>3.] | ||
forward_subs = [log(x) => z, y^2 => w, log(u) => v] | ||
backward_subs = [x => exp(z), y => w^.5, u => exp(v)] | ||
|
||
@mtkcompile sys = System(eqs, t; defaults=def) | ||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs) | ||
@test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2) | ||
@test equations(new_sys)[2] == (D(w) ~ α^2) | ||
@test equations(new_sys)[3] == (D(v) ~ μ - 1/2*(α^2 + σ^2)) | ||
@test noise_eqs(new_sys)[1,1] === value(σ) | ||
@test noise_eqs(new_sys)[1,2] === value(0) | ||
@test noise_eqs(new_sys)[2,1] === value(0) | ||
@test noise_eqs(new_sys)[2,2] === value(substitute(2*α*y, backward_subs[2])) | ||
@test noise_eqs(new_sys)[3,1] === value(σ) | ||
@test noise_eqs(new_sys)[3,2] === value(α) | ||
|
||
# Test for Brownian instead of noise | ||
@named sys = System(eqs, t; defaults=def) | ||
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=false) | ||
@test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx)) | ||
@test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By)) | ||
@test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*(α^2 + σ^2) + σ*Bx + α*By)) |
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.