# How to Determine or Approximate a Functions from Scatter Plots?

**URL:** <https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 17, 2022, 11:13am UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769 "2022-12-17T11:13:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 17, 2022, 11:13am UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/1 "2022-12-17T11:13:47Z")

</div>

Hi all,

I am using this code to plot a lot of data and connect the scatter plot:

```julia
using Plots, LaTeXStrings, Plots.PlotMeasures
gr()

using Plots

x = [10/60, 20/60, 30/60, 40/60, 50/60, 60/60, 70/60, 80/60, 80/60, 90/60, 100/60, 
	110/60, 120/60, 130/60, 140/60, 150/60, 160/60, 170/60, 180/60, 190/60, 200/60, 210/60]
y = [0, 55, 57, 60, 70, 70, 70, 70, 19, 0, 59, 63, 65, 62, 0, 0, 0, 22, 38, 35, 25, 0]

scatter(x,y)
plot(x,y; marker=(:circle,5), legend=:topright, label=" f(x)", 
	left_margin=5mm, size=(720, 360), tickfontsize=10)

```

I want to approximate the function that is connecting the dots/scatter plots. How to do this in Julia? Which package to use?

The function could be a polynomial or perhaps combination of transcendental functions, possibly… we never know…

Thanks.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [December 17, 2022, 12:27pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/2 "2022-12-17T12:27:45Z")

</div>

There are _many_ such functions. Uncountably many. And almost as many ways to _interpolate_ a set of points. The choice of methods depends strongly on where the points come from.

A common choice is a linear or higher order spline interpolation as implemented for example here [GitHub - JuliaMath/Interpolations.jl: Fast, continuous interpolation of discrete datasets in Julia](https://github.com/JuliaMath/Interpolations.jl)  
Maybe that’s already what you’re looking for?

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [December 17, 2022, 12:31pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/3 "2022-12-17T12:31:32Z")

</div>

How about this approximation?

```julia
using Statistics
ymean = mean(y)
plot!(x,fill(ymean,length(y)), label="Approx of f(x)")

```

 ![approx](https://global.discourse-cdn.com/julialang/original/3X/a/d/ad69b79510aee64c6ea9b320c9c7a343ddda5ea3.png)

I mean, you should certainly give some more requirements that such approximation should satisfy because the approximation using a constant function (it does not even have to be computed as a mean value) presented above does indeed satisfy all that you declared so far.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 17, 2022, 2:32pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/4 "2022-12-17T14:32:59Z")

</div>

If you want to experiment on your own, the following code will create an approximation based on Least Squares:

```julia
# Insert your choice of basis functions in vector function below
ϕ(x) = [1,x,x^2,x^3,sin(x),cos(x),sin(x/2),cos(x/2)]
#
# Standard linear regression
Φ = ϕ.(x) |> x-> reduce(hcat,x) |> permutedims
b = Φ\y
# Approximating function
f(x) = sum(ϕ(x).*b)

```

To apply the code to your own data:

```julia
scatter(x,y)
plot!(x,f.(x))

```

With this vector of basis functions (i.e., `ϕ(x)`), the resulting fit is not too impressive…

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/f/ef2c055d8b7044243e68783c648d57f6b39d5f14.png)

Some comments:

1. Go ahead and experiment with your own function elements of `ϕ(x)` – I think you can use any valid Julia function – or your own function.
2. It is possible that matrix `Φ` gets ill conditioned with many elements in the vector of basis functions, so that it gets difficult to compute `b = Φ\y` precisely. In that case, you can use SVD on `Φ`.
3. The simple method above is nice for experimenting with the choice of basis functions, but a proper package will probably do the same and much more, such as giving you statistics of the approximation, etc.

---

<div class="post-metadata">

**Author:** ![braamvandyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/braamvandyk/32/5086_2.png) [@braamvandyk](https://discourse.julialang.org/u/braamvandyk)\
**Post date:** [December 17, 2022, 7:19pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/5 "2022-12-17T19:19:14Z")

</div>

Have a look at [SymbolicRegression](https://github.com/MilesCranmer/SymbolicRegression.jl).

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 17, 2022, 8:27pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/6 "2022-12-17T20:27:55Z")

</div>

What do you want or need to do?

- To interpolate the data piecewise.
- To fit a spline.
- To fit a function minimizing the residuals.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 18, 2022, 9:37am UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/7 "2022-12-18T09:37:53Z")

</div>

The third one, to fit a function minimizing the residual.

And know which function is it for the data that I have.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 18, 2022, 9:39am UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/8 "2022-12-18T09:39:13Z")

</div>

I use this, and it is great. I haven’t experimenting yet, but I will try another function for \phi (x)

For SVD I will learn it after I finish with Calculus, it should be in Linear Algebra section.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 18, 2022, 1:08pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/9 "2022-12-18T13:08:00Z")

</div>

Then you have several options:

If you know the theoretical or desired shape of your function you provide that function parametrized and optimize those parameter to minimize the residual error. (common functions they are already implemented on many packages).

If not sure about the function then you can try something like a polynomial function.  
Or use LOESS.  
Or you can try with different functions sequentially trying to find the best one, but you need to be aware that you are doing multi-testing and you need to take it into account when interpreting the results.

You can also use a bayesian approach with Beta functions.

Or you can use decision trees or XGBoost. With proper cross-validation.

Or you can try with a black box: a neural network, deep learning… with proper cross-validation.

PS: Don’t forget to include interactions.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 18, 2022, 1:23pm UTC](https://discourse.julialang.org/t/how-to-determine-or-approximate-a-functions-from-scatter-plots/91769/10 "2022-12-18T13:23:17Z")

</div>

I’ll stick with polynomial first, I am still beginner in Mathematics, later will come soon.
