diff --git a/dev/.documenter-siteinfo.json b/dev/.documenter-siteinfo.json index 74d88e30..ba942a1a 100644 --- a/dev/.documenter-siteinfo.json +++ b/dev/.documenter-siteinfo.json @@ -1 +1 @@ -{"documenter":{"julia_version":"1.9.3","generation_timestamp":"2023-10-14T12:06:33","documenter_version":"1.1.1"}} \ No newline at end of file +{"documenter":{"julia_version":"1.9.3","generation_timestamp":"2023-10-14T12:07:27","documenter_version":"1.1.1"}} \ No newline at end of file diff --git a/dev/index.html b/dev/index.html index cd273b8a..6b37ab70 100644 --- a/dev/index.html +++ b/dev/index.html @@ -1,7 +1,7 @@ DataInterpolations.jl · DataInterpolations.jl

DataInterpolations.jl

DataInterpolations.jl is a library for performing interpolations of one-dimensional data. By "data interpolations" we mean techniques for interpolating possibly noisy data, and thus some methods are mixtures of regressions with interpolations (i.e. do not hit the data points exactly, smoothing out the lines). This library can be used to fill in intermediate data points in applications like timeseries data.

Installation

To install DataInterpolations.jl, use the Julia package manager:

using Pkg
 Pkg.add("DataInterpolations")

Available Interpolations

In all cases, u an AbstractVector of values and t is an AbstractVector of timepoints corresponding to (u,t) pairs.

  • ConstantInterpolation(u,t) - A piecewise constant interpolation.

  • LinearInterpolation(u,t) - A linear interpolation.

  • QuadraticInterpolation(u,t) - A quadratic interpolation.

  • LagrangeInterpolation(u,t,n) - A Lagrange interpolation of order n.

  • QuadraticSpline(u,t) - A quadratic spline interpolation.

  • CubicSpline(u,t) - A cubic spline interpolation.

  • AkimaInterpolation(u, t) - Akima spline interpolation provides a smoothing effect and is computationally efficient.

  • BSplineInterpolation(u,t,d,pVec,knotVec) - An interpolation B-spline. This is a B-spline which hits each of the data points. The argument choices are:

    • d - degree of B-spline
    • pVec - Symbol to Parameters Vector, pVec = :Uniform for uniform spaced parameters and pVec = :ArcLen for parameters generated by chord length method.
    • knotVec - Symbol to Knot Vector, knotVec = :Uniform for uniform knot vector, knotVec = :Average for average spaced knot vector.
  • BSplineApprox(u,t,d,h,pVec,knotVec) - A regression B-spline which smooths the fitting curve. The argument choices are the same as the BSplineInterpolation, with the additional parameter h<length(t) which is the number of control points to use, with smaller h indicating more smoothing.

Extension Methods

The follow methods require extra dependencies and will be loaded as package extensions.

  • Curvefit(u,t,m,p,alg) - An interpolation which is done by fitting a user-given functional form m(t,p) where p is the vector of parameters. The user's input p is a an initial value for a least-square fitting, alg is the algorithm choice to use for optimize the cost function (sum of squared deviations) via Optim.jl and optimal ps are used in the interpolation. Requires using Optim.
  • RegularizationSmooth(u,t,d;λ,alg) - A regularization algorithm (ridge regression) which is done by minimizing an objective function (l2 loss + derivatives of order d) integrated in the time span. It is a global method and creates a smooth curve. Requires using RegularizationTools.

Plotting

DataInterpolations.jl is tied into the Plots.jl ecosystem, by way of RecipesBase. Any interpolation can be plotted using the plot command (or any other), since they have type recipes associated with them.

For convenience, and to allow keyword arguments to propagate properly, DataInterpolations.jl also defines several series types, corresponding to different interpolations.

The series types defined are:

  • :linear_interp
  • :quadratic_interp
  • :lagrange_interp
  • :quadratic_spline
  • :cubic_spline

By and large, these accept the same keywords as their function counterparts.

Contributing

Reproducibility

The documentation of this SciML package was built using these direct dependencies,
Status `~/work/DataInterpolations.jl/DataInterpolations.jl/docs/Project.toml`
-  [82cc6244] DataInterpolations v4.3.0 `~/work/DataInterpolations.jl/DataInterpolations.jl`
+  [82cc6244] DataInterpolations v4.3.1 `~/work/DataInterpolations.jl/DataInterpolations.jl`
   [e30172f5] Documenter v1.1.1
   [429524aa] Optim v1.7.8
   [91a5bcdd] Plots v1.39.0
@@ -12,10 +12,10 @@
   Official https://julialang.org/ release
 Platform Info:
   OS: Linux (x86_64-linux-gnu)
-  CPU: 2 × Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
+  CPU: 2 × Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz
   WORD_SIZE: 64
   LIBM: libopenlibm
-  LLVM: libLLVM-14.0.6 (ORCJIT, skylake-avx512)
+  LLVM: libLLVM-14.0.6 (ORCJIT, icelake-server)
   Threads: 1 on 2 virtual cores
A more complete overview of all dependencies and their versions is also provided.
Status `~/work/DataInterpolations.jl/DataInterpolations.jl/docs/Manifest.toml`
   [a4c015fc] ANSIColoredPrinters v0.0.1
   [1520ce14] AbstractTrees v0.4.4
@@ -34,7 +34,7 @@
   [187b0558] ConstructionBase v1.5.4
   [d38c429a] Contour v0.6.2
   [9a962f9c] DataAPI v1.15.0
-  [82cc6244] DataInterpolations v4.3.0 `~/work/DataInterpolations.jl/DataInterpolations.jl`
+  [82cc6244] DataInterpolations v4.3.1 `~/work/DataInterpolations.jl/DataInterpolations.jl`
   [864edb3b] DataStructures v0.18.15
   [e2d170a0] DataValueInterfaces v1.0.0
   [8bb1440f] DelimitedFiles v1.9.1
@@ -241,4 +241,4 @@
   [8e850b90] libblastrampoline_jll v5.8.0+0
   [8e850ede] nghttp2_jll v1.48.0+0
   [3f19e933] p7zip_jll v17.4.0+0
-Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated -m`

You can also download the manifest file and the project file.

+Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated -m`

You can also download the manifest file and the project file.

diff --git a/dev/interface/index.html b/dev/interface/index.html index 28f0c00f..ce595531 100644 --- a/dev/interface/index.html +++ b/dev/interface/index.html @@ -17,4 +17,4 @@ A(100.0)
10.101397401671347
Note

The values computed beyond the range of the time points provided during interpolation will not be reliable as these methods only perform well within the range and the first/last piece polynomial fit is extrapolated on either sides which might not reflect the true nature of the data.

Derivatives

Derivatives of the interpolated curves can also be computed at any point for all the methods.

We will continue with the above example, but the API is same for all the methods.

# derivative(A, t)
 DataInterpolations.derivative(A, 1.0)
-0.051048168999699245

Integrals

Integrals of the interpolated curves can also be computed easily.

Currently, this is implemented only for a few methods - ConstantInterpolation, LinearInterpolation, QuadraticInterpolation, QuadraticSpline and CubicSpline.

To compute the integrals from the start of time points provided during interpolation to any point, we can do:

# integral(A, t)
 DataInterpolations.integral(A, 5.0)
72.86338611822583

If we want to compute integrals between two points, we can do:

# integral(A, t1, t2)
-DataInterpolations.integral(A, 1.0, 5.0)
114.9694509973317
Note

If the times provided in the integral goes beyond the range of the time points provided during interpolation, it uses extrapolation methods to compute the values and hence the integral can be misrepsentative and might not reflect the true nature of the data.

+DataInterpolations.integral(A, 1.0, 5.0)
114.9694509973317
Note

If the times provided in the integral goes beyond the range of the time points provided during interpolation, it uses extrapolation methods to compute the values and hence the integral can be misrepsentative and might not reflect the true nature of the data.

diff --git a/dev/methods/1632f60a.svg b/dev/methods/2022a3c3.svg similarity index 97% rename from dev/methods/1632f60a.svg rename to dev/methods/2022a3c3.svg index b98fa7f1..14c913db 100644 --- a/dev/methods/1632f60a.svg +++ b/dev/methods/2022a3c3.svg @@ -1,53 +1,53 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/a30c7b61.svg b/dev/methods/20390be5.svg similarity index 80% rename from dev/methods/a30c7b61.svg rename to dev/methods/20390be5.svg index 0e757b79..5d1c101e 100644 --- a/dev/methods/a30c7b61.svg +++ b/dev/methods/20390be5.svg @@ -1,248 +1,248 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/568489b6.svg b/dev/methods/297a1a76.svg similarity index 97% rename from dev/methods/568489b6.svg rename to dev/methods/297a1a76.svg index 559f7596..7af60df3 100644 --- a/dev/methods/568489b6.svg +++ b/dev/methods/297a1a76.svg @@ -1,55 +1,55 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/11cded30.svg b/dev/methods/2f6a74f8.svg similarity index 97% rename from dev/methods/11cded30.svg rename to dev/methods/2f6a74f8.svg index 45588c8c..c43ff5f4 100644 --- a/dev/methods/11cded30.svg +++ b/dev/methods/2f6a74f8.svg @@ -1,57 +1,57 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/92a0d726.svg b/dev/methods/462123ae.svg similarity index 95% rename from dev/methods/92a0d726.svg rename to dev/methods/462123ae.svg index 044141c6..db38a649 100644 --- a/dev/methods/92a0d726.svg +++ b/dev/methods/462123ae.svg @@ -1,145 +1,145 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/52a11a7a.svg b/dev/methods/479c6e24.svg similarity index 97% rename from dev/methods/52a11a7a.svg rename to dev/methods/479c6e24.svg index 98ad7e8d..0565e103 100644 --- a/dev/methods/52a11a7a.svg +++ b/dev/methods/479c6e24.svg @@ -1,55 +1,55 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/b7cd22de.svg b/dev/methods/6106d6b1.svg similarity index 97% rename from dev/methods/b7cd22de.svg rename to dev/methods/6106d6b1.svg index 40fa7aa8..91cfe603 100644 --- a/dev/methods/b7cd22de.svg +++ b/dev/methods/6106d6b1.svg @@ -1,59 +1,59 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/8354e66f.svg b/dev/methods/6e3c99a2.svg similarity index 97% rename from dev/methods/8354e66f.svg rename to dev/methods/6e3c99a2.svg index e608c3c9..dbd26033 100644 --- a/dev/methods/8354e66f.svg +++ b/dev/methods/6e3c99a2.svg @@ -1,55 +1,55 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/605a647c.svg b/dev/methods/9f2ee4db.svg similarity index 97% rename from dev/methods/605a647c.svg rename to dev/methods/9f2ee4db.svg index 6411c7b5..2dc0892b 100644 --- a/dev/methods/605a647c.svg +++ b/dev/methods/9f2ee4db.svg @@ -1,55 +1,55 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/cfda197b.svg b/dev/methods/dec33a62.svg similarity index 97% rename from dev/methods/cfda197b.svg rename to dev/methods/dec33a62.svg index ebfe37fa..fb9a2852 100644 --- a/dev/methods/cfda197b.svg +++ b/dev/methods/dec33a62.svg @@ -1,57 +1,57 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/1e7e108a.svg b/dev/methods/ecaa0f5c.svg similarity index 86% rename from dev/methods/1e7e108a.svg rename to dev/methods/ecaa0f5c.svg index 96983aa3..e31ec84a 100644 --- a/dev/methods/1e7e108a.svg +++ b/dev/methods/ecaa0f5c.svg @@ -1,62 +1,62 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/ea1f59d3.svg b/dev/methods/f130043a.svg similarity index 95% rename from dev/methods/ea1f59d3.svg rename to dev/methods/f130043a.svg index 2f3c2f89..222a80b8 100644 --- a/dev/methods/ea1f59d3.svg +++ b/dev/methods/f130043a.svg @@ -1,145 +1,145 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/d7130980.svg b/dev/methods/fed69fb6.svg similarity index 97% rename from dev/methods/d7130980.svg rename to dev/methods/fed69fb6.svg index d5c5ecc3..3555a01f 100644 --- a/dev/methods/d7130980.svg +++ b/dev/methods/fed69fb6.svg @@ -1,55 +1,55 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/methods/index.html b/dev/methods/index.html index c783f99a..48131eb7 100644 --- a/dev/methods/index.html +++ b/dev/methods/index.html @@ -13,24 +13,24 @@ 205.8 252.3

For each method, we will show how to perform the fit and use the plot recipe to show the fitting curve.

Linear Interpolation

This is a linear interpolation between ends points of interval of input data point.

A = LinearInterpolation(u, t)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Quadratic Interpolation

This function fits a parabola passing through the two nearest points from the input data point as well as the next-closest point in the right or the left, depending on whether the forward- or backward-looking mode is selected (default mode is forward-looking). It is continuous and piecewise differentiable.

A = QuadraticInterpolation(u, t) # same as QuadraticInterpolation(u,t,:Forward)
+plot!(A)
Example block output

Quadratic Interpolation

This function fits a parabola passing through the two nearest points from the input data point as well as the next-closest point in the right or the left, depending on whether the forward- or backward-looking mode is selected (default mode is forward-looking). It is continuous and piecewise differentiable.

A = QuadraticInterpolation(u, t) # same as QuadraticInterpolation(u,t,:Forward)
 # alternatively: A = QuadraticInterpolation(u,t,:Backward)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Lagrange Interpolation

It fits polynomial of degree d (=length(t)-1), and is thus a continuously differentiable function.

A = LagrangeInterpolation(u, t)
+plot!(A)
Example block output

Lagrange Interpolation

It fits polynomial of degree d (=length(t)-1), and is thus a continuously differentiable function.

A = LagrangeInterpolation(u, t)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Constant Interpolation

This function is constant between data points. By default it takes value at left end of the interval. One can change that behavior by passing the keyword argument dir = :right.

A = ConstantInterpolation(u, t)
+plot!(A)
Example block output

Constant Interpolation

This function is constant between data points. By default it takes value at left end of the interval. One can change that behavior by passing the keyword argument dir = :right.

A = ConstantInterpolation(u, t)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Or using the right endpoints:

A = ConstantInterpolation(u, t, dir = :right)
+plot!(A)
Example block output

Or using the right endpoints:

A = ConstantInterpolation(u, t, dir = :right)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Quadratic Spline

This is the quadratic spline. It is a continuously differentiable interpolation which hits each of the data points exactly. Splines are a local interpolation method, meaning that the curve in a given spot is only affected by the points nearest to it.

A = QuadraticSpline(u, t)
+plot!(A)
Example block output

Quadratic Spline

This is the quadratic spline. It is a continuously differentiable interpolation which hits each of the data points exactly. Splines are a local interpolation method, meaning that the curve in a given spot is only affected by the points nearest to it.

A = QuadraticSpline(u, t)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Cubic Spline

This is the cubic spline. It is a continuously twice differentiable interpolation which hits each of the data points exactly.

A = CubicSpline(u, t)
+plot!(A)
Example block output

Cubic Spline

This is the cubic spline. It is a continuously twice differentiable interpolation which hits each of the data points exactly.

A = CubicSpline(u, t)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

B-Splines

This is an interpolating B-spline. B-splines are a global method, meaning that every data point is taken into account for each point of the curve. The interpolating B-spline is the version which hits each of the points. This method is described in more detail here. Let's plot a cubic B-spline (3rd order). Since the data points are not close to uniformly spaced, we will use the :ArcLen and :Average choices:

A = BSplineInterpolation(u, t, 3, :ArcLen, :Average)
+plot!(A)
Example block output

B-Splines

This is an interpolating B-spline. B-splines are a global method, meaning that every data point is taken into account for each point of the curve. The interpolating B-spline is the version which hits each of the points. This method is described in more detail here. Let's plot a cubic B-spline (3rd order). Since the data points are not close to uniformly spaced, we will use the :ArcLen and :Average choices:

A = BSplineInterpolation(u, t, 3, :ArcLen, :Average)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

The approximating B-spline is a smoothed version of the B-spline. It again is a global method. In this case, we need to give a number of control points length(t)>h and this method fits a B-spline through the control points which is a least square approximation. This has a natural effect of smoothing the data. For example, if we use 4 control points, we get the result:

A = BSplineApprox(u, t, 3, 4, :ArcLen, :Average)
+plot!(A)
Example block output

The approximating B-spline is a smoothed version of the B-spline. It again is a global method. In this case, we need to give a number of control points length(t)>h and this method fits a B-spline through the control points which is a least square approximation. This has a natural effect of smoothing the data. For example, if we use 4 control points, we get the result:

A = BSplineApprox(u, t, 3, 4, :ArcLen, :Average)
 scatter(t, u, label = "input data")
-plot!(A)
Example block output

Regularization Smoothing

Smoothing by regularization (a.k.a. ridge regression) finds a function $\hat{u}$ that minimizes the objective function:

$Q(\hat{u}) = \int_{t_1}^{t_N} |\hat{u}(t) - u(t)|^2 \mathrm{d}t + \lambda \int_{\hat{t}_1}^{\hat{t}_N} |\hat{u}^{(d)}(\hat{t})|^2 \mathrm{d} \hat{t}$

where $(d)$ denotes derivative order and $\lambda$ is the regularization (smoothing) parameter. The integrals are evaluated numerically at the set of $t$ values for the first term and $\hat{t}$ values for the second term (equal to $t$ if not provided). Regularization smoothing is a global method and creates a smooth curve directly. See Stickel (2010) Comput. Chem. Eng. 34:467 for details. The implementation in this package uses cubic splines to interpolate between the smoothed points after they are determined.

using RegularizationTools
+plot!(A)
Example block output

Regularization Smoothing

Smoothing by regularization (a.k.a. ridge regression) finds a function $\hat{u}$ that minimizes the objective function:

$Q(\hat{u}) = \int_{t_1}^{t_N} |\hat{u}(t) - u(t)|^2 \mathrm{d}t + \lambda \int_{\hat{t}_1}^{\hat{t}_N} |\hat{u}^{(d)}(\hat{t})|^2 \mathrm{d} \hat{t}$

where $(d)$ denotes derivative order and $\lambda$ is the regularization (smoothing) parameter. The integrals are evaluated numerically at the set of $t$ values for the first term and $\hat{t}$ values for the second term (equal to $t$ if not provided). Regularization smoothing is a global method and creates a smooth curve directly. See Stickel (2010) Comput. Chem. Eng. 34:467 for details. The implementation in this package uses cubic splines to interpolate between the smoothed points after they are determined.

using RegularizationTools
 d = 2
 λ = 1e3
 A = RegularizationSmooth(u, t, d; λ = λ, alg = :fixed)
@@ -42,7 +42,7 @@
 lw = 1.5
 scatter(t, u, label = "data")
 scatter!(t, û, marker = :square, label = "smoothed data")
-plot!(titp, uitp, lw = lw, label = "smoothed interpolation")
Example block output

Dense Data Demonstration

Some methods are better suited for dense data. Let's generate such data to demonstrate these methods.

import StableRNGs: StableRNG
+plot!(titp, uitp, lw = lw, label = "smoothed interpolation")
Example block output

Dense Data Demonstration

Some methods are better suited for dense data. Let's generate such data to demonstrate these methods.

import StableRNGs: StableRNG
 rng = StableRNG(318)
 t = sort(10 .* rand(rng, 100))
 u = sin.(t) .+ 0.5 * randn(rng, 100);
100-element Vector{Float64}:
@@ -76,17 +76,17 @@
 scatter(t, u, label = "simulated data", legend = :top)
 scatter!(t, û, marker = (:square, 4), label = "smoothed data")
 plot!(titp, uitp, lw = lw, label = "smoothed interpolation")
-plot!(titp, ûm, lw = lw, linestyle = :dash, label = "smoothed, more points")
Example block output

Curve Fits

A curve fit works with both dense and sparse data. We will demonstrate the curve fit on the dense data since we generated it based on sin(t), so this is the curve we want to fit through it. Do do so, let's define a similar function with parameters. Let's choose the form:

m(t, p) = @. p[1] * sin(p[2] * t) + p[3] * cos(p[4] * t)
m (generic function with 1 method)

Notice that this is a function on the whole array of t and expects an array for the predicted u out. This choice of m is the assumption that our function is of the form p1*sin(p2*t)+p3*cos(p4*t). We want to find the p to match our data. Let's start with the guess of every p being zero, that is p=ones(4). Then we would fit this curve using:

using Optim
+plot!(titp, ûm, lw = lw, linestyle = :dash, label = "smoothed, more points")
Example block output

Curve Fits

A curve fit works with both dense and sparse data. We will demonstrate the curve fit on the dense data since we generated it based on sin(t), so this is the curve we want to fit through it. Do do so, let's define a similar function with parameters. Let's choose the form:

m(t, p) = @. p[1] * sin(p[2] * t) + p[3] * cos(p[4] * t)
m (generic function with 1 method)

Notice that this is a function on the whole array of t and expects an array for the predicted u out. This choice of m is the assumption that our function is of the form p1*sin(p2*t)+p3*cos(p4*t). We want to find the p to match our data. Let's start with the guess of every p being zero, that is p=ones(4). Then we would fit this curve using:

using Optim
 A = Curvefit(u, t, m, ones(4), LBFGS())
 scatter(t, u, label = "points", legend = :bottomright)
-plot!(A)
Example block output

We can check what the fitted parameters are via:

A.pmin
4-element Vector{Float64}:
+plot!(A)
Example block output

We can check what the fitted parameters are via:

A.pmin
4-element Vector{Float64}:
   1.002517318529601
   1.0396588440356678
  -0.13178842466234678
   1.0670107400182296

Notice that it essentially made p3=0 with p1=p2=1, meaning it approximately found sin(t)! But note that the ability to fit is dependent on the initial parameters. For example, with p=zeros(4) as the initial parameters the fit is not good:

A = Curvefit(u, t, m, zeros(4), LBFGS())
 scatter(t, u, label = "points", legend = :bottomright)
-plot!(A)
Example block output

And the parameters show the issue:

A.pmin
4-element Vector{Float64}:
+plot!(A)
Example block output

And the parameters show the issue:

A.pmin
4-element Vector{Float64}:
  0.0
  0.0
  0.042632088464589324
- 0.0
+ 0.0