fslaborg / FSharp.Stats

statistical testing, linear algebra, machine learning, fitting and signal processing in F#
https://fslab.org/FSharp.Stats/
Other
205 stars 54 forks source link

[Feature Request] Surface fitting for simple data, and example that shows the surface in plotly #309

Open 7sharp9 opened 9 months ago

7sharp9 commented 9 months ago

I want to produce a surface from a combination of data currently in 3 axis, I am using matlab to produce a thin plate spline model and also poly44 and wondered if there are any options to do this with this lib?

Describe the solution you'd like I would like a sample that generates a surface fit for data in 3 axis and produces a Plotly ploy showing the surface and the original data scattered on top.

Describe alternatives you've considered Matlab/Python examples are easy to google but I can include one if needed.

bvenn commented 9 months ago

Do you just want to have some kind of regression or just a smoothed interpolation of each point? Currently FSharp.Stats is capable of smoothing two-dimensional data only, but if you just need and interpolation and the visualization to be smooth, you can just use a Plotly surface plot. The smooting is performed automatically and may even be adjustable (https://plotly.net/reference/plotly-net-chart3d-chart.html#Surface).

An example can be seen here or here.

7sharp9 commented 8 months ago

Hi, thanks for the reply, to be honest just looking for any particular solutions, I have a certain amount of data which I want tto plot a surface for, the surface points should not stray away from the data set I have to interpolation like the thin plate spline seem to match my data the best but Ive also managed to replicate in 2d each dimension using tanh or arctan curves. So a F# solution of any sort would suffice for me. F# is th eeasy bits its just figuring out how from samples really.

7sharp9 commented 8 months ago

If regression is a really close match to my input data I could use that too, but only if the surface is only ever higher than my plotted points, and not by very much.

kMutagene commented 8 months ago

@7sharp9 if you just want to visualize your data and do not care about the fit function that creates the surface, you can just use Chart.Surface from Plotly.NET as @bvenn suggested. However i think if you are interested in the coefficients that fit the surface, we'd need an equivalent of numpy.polyfit which to my knowledge is not implemented in FSharp.Stats.

As i do not know how your data looks like, here is a example that contains some boilerplate to generate some 2D data that also visualizes the points from the input across the surface:

//---------------------- Generate linearly spaced vector ----------------------
let linspace (min, max, n) =
    if n <= 2 then
        failwithf "n needs to be larger then 2"

    let bw = float (max - min) / (float n - 1.)
    Array.init n (fun i -> min + (bw * float i))

//---------------------- Create example data ----------------------
let size = 100

let x = linspace (-2. * Math.PI, 2. * Math.PI, size) // 100 evenly spaced samples between -2π and 2π
let y = linspace (-2. * Math.PI, 2. * Math.PI, size) // 100 evenly spaced samples between -2π and 2π

let f x y = -(5. * x / (x ** 2. + y ** 2. + 1.)) // function to generate z based on x and y: f(x,y) = -(5 * x / (x^2 + y^2 + 1))

let xyz = Array.init size (fun i -> Array.init size (fun j -> x.[j], y.[i], f x.[j] y.[i])) |> Array.concat // generate x,y,z triplets for point plot
let zArray = Array.init size (fun i -> Array.init size (fun j -> f x.[j] y.[i])) // generate z values for surface plot

[
    Chart.Point3D(xyz, Marker = Marker.init(Size = 1, Color = Color.fromString "blue"))
    Chart.Surface(zData = z, X = x, Y = y)
]
|> Chart.combine

image

7sharp9 commented 8 months ago

Thanks @kMutagene I'm a little slow to respond but thanks for that, I've ended up using Matlab for a lot of my stuff as it has been quick to generate. I'm mainly using a scatter plot of data and then using thin plate interpolation to produce a surface. The surface is then used to create increments over the scatter data, which will be used in C++. I'm getting Matlab to produce C++ code of the interpolated data points. If I had enough time I would try to implement this in F# but its tricky finding the time!