# Using Interpolations

**URL:** <https://discourse.julialang.org/t/using-interpolations/23933>\
**Category:** New to Julia\
**Tags:** interpolations\
**Created:** [May 6, 2019, 8:44pm UTC](https://discourse.julialang.org/t/using-interpolations/23933 "2019-05-06T20:44:05Z")\
**Posts on this page:** 8\
**Page:** 3

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 8, 2019, 5:15pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/42 "2019-05-08T17:15:21Z")

</div>

This will have to be slower.  
There you couldn’t use the closed form solution.

And then you also can’t use my iterative approach either (hidden behind the “Summary” label). That one was faster because I simply only calculated the interpolations once.  
Calculating the interpolations once took about 310 microseconds (meaning, given those, it only took about 40 for my closed form solution to calculate beta). That is the slowest part of the code.

If you use an iterative approach, because the fits are now a function of the parameters, you’ll have to reevaluate them each time. This will make that code much slower.

I’d look at others’ suggestions above on how to make the interpolations faster.  
Is it possible to make your interpolation an evenly spaced grid, or some transform of such a grid?  
I’d also sort the "x"es (use `sortperm`, so you can also sort y the same way), and then use some function to evaluate the batch, taking advantage of the fact they’re sorted.

---

<div class="post-metadata">

**Author:** ![Eric\_Shain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_shain/32/8348_2.png) [@Eric\_Shain](https://discourse.julialang.org/u/Eric_Shain)\
**Post date:** [May 8, 2019, 6:00pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/43 "2019-05-08T18:00:01Z")

</div>

The data is sampled at a consistent rate, and that will always be reasonably constant. However it isn’t always the same values. Here is a small sample of the x vector of real data.

```julia
6-element Array{Float64,1}:
 29.439125518281116
 29.519035054655205
 29.598944591028953
 29.67885412740293 
 29.75876366377679 
 29.838673200150765

```

The start times of the data vary a bit run-to-run with respect to where the peaks occur which is why I need the extra two parameters. What I can say about the data is that the curve shape is reasonably smooth and without discontinuities. Also, the data is much more finely sampled than necessary with about 7000 samples. One could, for instance, use every tenth sample (perhaps even coarser), especially if you fit with a cubic spline instead of a linear approximation. I’ve done this with Python and got comparable answers with faster processing times (but not ten times faster).

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 14, 2019, 10:32am UTC](https://discourse.julialang.org/t/using-interpolations/23933/44 "2019-05-14T10:32:44Z")

</div>

I guess the main concern is that the constructor will no longer be concretely inferrable, so code that creates the object and then uses it in the same function might suffer. Due to union-splitting it won’t be terrible, but it could easily cause a slowdown if the check isn’t hoisted out of the inner loop. Would be worth exploring before submitting a PR, but if the compiler is smart enough this could be a very nice improvement.

The performance improvements in Interpolations 0.12 also seem to basically solve the problem (now you pay the price of extrapolation only when you need it):

```julia
julia> itp1 = interpolate((x,), y, Gridded(Linear()));

julia> itp2 = LinearInterpolation(x, y,extrapolation_bc=Flat());

julia> @btime $itp1($x2);
  62.032 μs (9 allocations: 31.56 KiB)

julia> @btime $itp2($x2);
  62.408 μs (1 allocation: 7.94 KiB)

```

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [May 14, 2019, 1:54pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/45 "2019-05-14T13:54:38Z")

</div>

> [@tim.holy](#):
>
> The performance improvements in Interpolations 0.12 also seem to basically solve the problem (now you pay the price of extrapolation only when you need it):

Perfect, sounds like this isn’t needed any more. Is the same true of the exception throwing extrapolation (which is the current default in the convenience constructors)?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 14, 2019, 2:01pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/46 "2019-05-14T14:01:10Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)\
**Post date:** [October 2, 2020, 12:42pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/47 "2020-10-02T12:42:32Z")

</div>

On topic; and yet slightly off topic:

Can someone help me understand why CubicSplineInterpolation does not work with collect(). Thanks in advance:

```nohighlight
using Interpolations
x = collect(1:0.1:5)
y = rand(length(x))
itp1 = LinearInterpolation(x, y; extrapolation_bc=Flat())
@show itp1(4)
@show itp1(6)
itp2 = CubicSplineInterpolation(x, y; extrapolation_bc=Flat())
@show itp2(4)
@show itp2(6)

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 5, 2020, 3:32pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/48 "2020-10-05T15:32:51Z")

</div>

It should. Post an issue about this, mention that `LinearInterpolation(...)` handles collected xs and ys properly.

---

<div class="post-metadata">

**Author:** ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)\
**Post date:** [October 5, 2020, 4:49pm UTC](https://discourse.julialang.org/t/using-interpolations/23933/50 "2020-10-05T16:49:44Z")

</div>

Thanks Jeffrey. Just [posted](https://github.com/JuliaMath/Interpolations.jl/issues/383)

[Previous page](https://discourse.julialang.org/t/using-interpolations/23933.md?page=2)
