Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Multi Scale Causality
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FROGE Ewen
Multi Scale Causality
Commits
8ffee955
Commit
8ffee955
authored
3 months ago
by
FROGE Ewen
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
85d8348c
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Analog Based Forecasting and Innovation/Exemple_Lorenz.py
+63
-0
63 additions, 0 deletions
Analog Based Forecasting and Innovation/Exemple_Lorenz.py
with
63 additions
and
0 deletions
Analog Based Forecasting and Innovation/Exemple_Lorenz.py
0 → 100644
+
63
−
0
View file @
8ffee955
#%%
# This script generates a Lorenz system time series, splits it into training and testing datasets,
# and applies an analog forecasting method using nearest neighbors. The training data (bib) is used
# to build a database of past states, while the testing data (signal) is predicted based on analogs
# found in the training set. The script then visualizes the innovation (prediction error) and its PDF.
# Modules
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
scipy.integrate
import
solve_ivp
from
Compute_Innovation
import
create_database
,
prediction_neighbors
# Parameters
p
=
3
# Size of analogs
k
=
70
# Number of analogs
N
=
2
**
16
# Signal size
overlapping
=
False
# Whether to allow overlapping analogs in database
weighting
=
True
# Whether to weight analogs based on distance
normalizing
=
True
# Whether to remove analogs mean
def
lorenz_system
(
t
,
state
,
sigma
=
10.0
,
beta
=
8
/
3
,
rho
=
28.0
):
"""
Defines the Lorenz system equations.
"""
x
,
y
,
z
=
state
dx
=
sigma
*
(
y
-
x
)
dy
=
x
*
(
rho
-
z
)
-
y
dz
=
x
*
y
-
beta
*
z
return
[
dx
,
dy
,
dz
]
def
generate_lorenz_signal
(
n_steps
,
dt
=
0.01
):
"""
Generates a Lorenz system time series (x component).
"""
t_span
=
(
0
,
n_steps
*
dt
)
initial_state
=
[
1.0
,
1.0
,
1.0
]
t_eval
=
np
.
linspace
(
t_span
[
0
],
t_span
[
1
],
n_steps
)
sol
=
solve_ivp
(
lorenz_system
,
t_span
,
initial_state
,
t_eval
=
t_eval
,
method
=
'
RK45
'
)
return
sol
.
y
[
0
]
# Extract x-component
data
=
generate_lorenz_signal
(
2
*
N
)
bib
=
(
data
[:
N
]
-
np
.
mean
(
data
[:
N
]))
/
np
.
std
(
data
[:
N
])
signal
=
(
data
[
N
:]
-
np
.
mean
(
data
[
N
:]))
/
np
.
std
(
data
[
N
:])
db
=
create_database
(
bib
,
p
,
overlapping
=
overlapping
)
vals
,
variances
=
prediction_neighbors
(
signal
,
db
,
k
,
weighting
=
weighting
,
normalize
=
normalizing
)
innovation
=
signal
-
vals
# First p values are NaN due to alignment
# Plot results
fig
,
axs
=
plt
.
subplots
(
2
,
2
,
figsize
=
(
10
,
8
))
axs
[
0
,
0
].
plot
(
innovation
)
axs
[
0
,
0
].
set_title
(
"
Innovation
"
)
axs
[
0
,
1
].
plot
(
signal
)
axs
[
0
,
1
].
set_title
(
"
Signal
"
)
axs
[
1
,
0
].
hist
(
innovation
[
~
np
.
isnan
(
innovation
)],
bins
=
50
,
density
=
True
)
axs
[
1
,
0
].
set_title
(
"
PDF of Innovation
"
)
axs
[
1
,
1
].
hist
(
signal
,
bins
=
50
,
density
=
True
)
axs
[
1
,
1
].
set_title
(
"
PDF of Signal
"
)
plt
.
tight_layout
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment