-
-
Notifications
You must be signed in to change notification settings - Fork 50
Add SmoothedConstantInterpolation
#367
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
base: master
Are you sure you want to change the base?
Add SmoothedConstantInterpolation
#367
Conversation
Fun feature; if using DataInterpolations
using Plots
using Random
N = 4
Random.seed!(8)
u = rand(N)
t = cumsum(rand(N))
Δt = t[end] - t[1]
t_eval = range(first(t) - Δt, last(t) + Δt, length = 500)
A = SmoothedConstantInterpolation(u, t; extrapolation = ExtrapolationType.Periodic, d_max = 0.2)
plot(t_eval, A.(t_eval))
scatter!(t[1:end-1], u[1:end-1]; label = "data")
scatter!(t[1:end-1] .+ Δt, u[1:end-1]; label = "data one period forward")
scatter!(t[1:end-1] .- Δt, u[1:end-1]; label = "data one period back") |
plot!(A) | ||
``` | ||
|
||
Note that `u[end]` is ignored. |
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.
even for extrapolation?
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.
Yes, also for extrapolation
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.
This line can now be removed/edited?
Conflicts? |
@visr I added the function |
What's the reason the end is ignored and it doesn't just constant extrapolate? |
The key properties of this interpolation type are that it is differentiable everywhere and sufficiently close to constant interpolation. I achieve the diffentiability by replacing the stepwise transition in each data point by a differentiable transition in an interval around each data point. What that means for extrapolation is a bit murky. I already made it so that the extrapolation is smooth in the case of periodic extrapolation, which means that the interpolation around the edges of the domain has to account for that. I'd argue that for constant extrapolation, ignoring the last data point is also best. Maybe for extension extrapolation, the last data point can be used, but that feels a bit inconsistent. |
I'd like to hear @sathvikbhagavan 's thoughts, because at least to me constant extrapolation with the last point feels natural |
If I understand correctly, can't we do the same transition from second last to the last point after which it becomes constant? This is strictly not constant extrapolation as values would be different from the last point (for a small interval) as it needs to be C1 smooth everywhere |
I made it now so that both using DataInterpolations
using Plots
using Random
N = 4
Random.seed!(10)
u = rand(N)
t = cumsum(rand(N))
Δt = t[end] - t[1]
t_eval = range(first(t) - Δt, last(t) + Δt, length=500)
A = SmoothedConstantInterpolation(u, t; extrapolation=ExtrapolationType.Extension, d_max=0.2, cache_parameters=true)
plot(t_eval, A.(t_eval), label="interpolation with Extension extrapolation")
scatter!(t, u, label="Data") |
I don't understand why the derivative tests are failing 🤔 |
fixed it 👍 |
I think I'm happy. @sathvikbhagavan ? |
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.
A couple of clarifications
plot!(A) | ||
``` | ||
|
||
Note that `u[end]` is ignored. |
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.
This line can now be removed/edited?
|
||
It is a method for interpolating constantly with forward fill, with smoothing around the | ||
value transitions to make the curve continuously differentiable while the integral never | ||
drifts far from the integral of constant interpolation. `u[end]` is ignored for extrapolation types |
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.
comment about u[end]
- it can be removed? When is u[end]
ignored now?
Fixes #364.
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context