# Parameter estimation in mathematical models of common functions (sine, cos, exp and other special functions)

**URL:** <https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325>\
**Category:** New to Julia\
**Tags:** package, physics, curve-fitting\
**Created:** [June 30, 2025, 8:36am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325 "2025-06-30T08:36:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [June 30, 2025, 8:36am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/1 "2025-06-30T08:36:27Z")

</div>

Hello all glad to have arrived here. I have recently picked up Julia, but am quite  
excited about it!

I have many data sets that follow the same model but the parameters change widely from  
dataset to dataset. In our labs we have python code that does the parameter estimation  
for many of the models (lorentzian peaks, gaussian peaks, sine, exponential decay).

This seems to me a rather general problem so I always wondered why there isn’t a package  
for this out there (at least in the python ecosphere I am not aware of one) and maybe in Julia there is also no such package.

Hopefully I just haven’t been able to find it. Nevertheless if this would be something  
people are interested in then I could implement the code we use in our labs in a Julia package.

Best

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 30, 2025, 9:25am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/2 "2025-06-30T09:25:48Z")

</div>

Hi, welcome to the community!  
Parameter estimation is a very broad topic, and the Julia ecosystem has plenty of packages to do that. Perhaps if you gave some more detail on 1) the models you’re interested in and 2) the methods that your lab already uses, we could point you in the right direction.  
For instance:

1. Are these statistical / physical / economic models? Do you have an explicit formula or just a simulator? Do they have hidden variables? How many parameters are involved?
2. Are the optimal parameters defined by an explicit formula, or an optimization problem? Do your Python procedures leverage differentiability or not? If so, first order or more? If not, do you use some kind of fitting heuristic?

etc. etc.

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [June 30, 2025, 9:45am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/3 "2025-06-30T09:45:42Z")

</div>

Thanks for the nice welcoming.

> [@gdalle](#):
>
> r

I use the them model here but in effect we talk about continuous functions  
like f(t; A, w) = A sin(wt), where we want to estimate A and w in often very noisy data.

Other functions we are interested in is lorentzian(s), exponential(s) - specifically decay, gaussian(s)

Our code leverages things like FFT, convolution, smoothing with gaussian filters and  
a lot of heuristics to get to a decent parameter estimation (far from perfect).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 30, 2025, 11:43am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/4 "2025-06-30T11:43:40Z")

</div>

> [@Geegee](#):
>
> Our code leverages things like FFT, convolution, smoothing with gaussian filters and  
> a lot of heuristics to get to a decent parameter estimation (far from perfect).

You can probably find all of these ingredients in the Julia ecosystem, but your description above should answer

> [@Geegee](#):
>
> why there isn’t a package for this out there

: because you need to make a lot of decisions like this, so there is no general _method_ for “estimation”.

That said, I am not sure why something simple like minimizing a sum of squared discrepancies would not work, but maybe your problem domain has something special.

If you want to get more specific help, post an MWE for generating the data, and the questions you have.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [June 30, 2025, 1:30pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/5 "2025-06-30T13:30:12Z")

</div>

I get were you are coming from. Indeed whatever package someone would put together it would be extremely opinionated. Nevertheless I have the feeling that basically the same things are implemented again and again all over the world in different labs. Having robust estimators for the most common functions seems like a good thing to have, but I guess I am alone with this opinion, maybe indeed our code is so specialized that only we can use it.

I will do as you suggest and implement an estimator for the sine model as a start with the existing packages in Julia and when I get stuck post a MWE.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 30, 2025, 1:43pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/6 "2025-06-30T13:43:56Z")

</div>

> [@Geegee](#):
>
> I have the feeling that basically the same things are implemented again and again all over the world in different labs

The difficult parts are packaged as generic algorithms, eg an optimimizer (see Optimization.jl). Using it for estimation is a special case.

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [June 30, 2025, 2:13pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/7 "2025-06-30T14:13:45Z")

</div>

This is where I struggle I feel. I don’t understand really what this package does. There is only the example of the Rosenbrock model where they try to find the minimum of a function if I understand correctly. I fail to make the connection to my problem. I think you mentioned this before that you could do estimation with optimizers but I have absolutely no glue how to do that. I see that the magic they do is in that they don’t get stuck in local minima and can cover huge “areas” in parameter space. I guess I need more examples with real world applications.  
Especially how do I bring in the data in here? How does Optimization.jl relate to curve fitting i.e. [LsqFit](https://julianlsolvers.github.io/LsqFit.jl)?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 30, 2025, 2:19pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/8 "2025-06-30T14:19:42Z")

</div>

> [@Geegee](#):
>
> I fail to make the connection to my problem.

A lot (but not all) of methodologies for _estimation_ minimize some discrepancy between the data and a model, parametrizing the latter by the desired parameters, functional forms, etc. You can formalize this as least squares, maximum likelihood, etc.

> [@Geegee](#):
>
> I guess I need more examples with real world applications.

Or possibly some background in statistical procedures? Perhaps there is a mentor/advisor at your lab who knows the problem domain and could get you started.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [June 30, 2025, 4:00pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/9 "2025-06-30T16:00:51Z")

</div>

> [@Geegee](#):
>
> Especially how do I bring in the data in here? How does [Optimization.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Optimization) relate to curve fitting i.e. [LsqFit](https://julianlsolvers.github.io/LsqFit.jl)?

The connection is not that difficult to find. In curve fitting you ask “For which parameters does this function describe my data _best_?”. That sounds like an optimization, does it not?  
How that works in practice is that you define some distance function that measures the distance between your model and your data. Then the process of _fitting_ is just the minimization of that distance.

A very common method is “least squares”. Here the distance function is just the L\_2 norm, i.e. d(x,y) = \sum\_i |x\_i - y\_i|^2. Then you plug in your data points and the predictions of your model g(\theta)=d(\hat y, f(\hat x;\theta)), where f is the model function, \theta the model’s parameter, \hat x the measurement points and \hat y the measured values. Now minimizing g(\theta) is “fitting f to your data”.

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [June 30, 2025, 4:06pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/10 "2025-06-30T16:06:15Z")

</div>

> [@Geegee](#):
>
> Especially how do I bring in the data in here? How does [Optimization.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Optimization) relate to curve fitting i.e. [LsqFit](https://julianlsolvers.github.io/LsqFit.jl)?

Think of the Rosenbrock example function as this “discrepancy function” which takes fixed values `_p` and optimizable parameters `x`. In your case the fixed values is your data, say `x, y`, and the discrepancy function is some measure of distance between model predictions and observed values, `f(x, y, p) = model(x, p) - y`. LsqFit.jl seems to implement its own algorithm (Levenberg–Marquardt) which perhaps works well specifically for least squares optimization of non-linear regression models.

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [June 30, 2025, 5:26pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/11 "2025-06-30T17:26:43Z")

</div>

Thanks everyone for the helpful remarks. As someone with a physics background I probably used all the wrong terms and indeed upon thinking more about it I think what people commonly mean with parameter estimation is something I know as fitting. Whereas what i meant (falsly) is finding the initial bounds so my algorithm can find the best solution and not get stuck in local minima. In essence splitting the estimation part into two steps. One function which is tailored to the model used and another one which is generally good at finding the optimum.

I guess there probably is something in Optimize.jl for me but my feeling as suggested by @Tamas_Papp is that i would need to study a lot more optimal control theory, statistics or other related subjects.

---

<div class="post-metadata">

**Author:** ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)\
**Post date:** [June 30, 2025, 9:42pm UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/12 "2025-06-30T21:42:12Z")

</div>

As a physics student and a self-taught in Julia myself, I can understand some struggles you may be encountering. Unfortunately I have not been able to understand what you mean either. If you are interested in finding the parameters `A` and `w` from a function `f(x, A, w) = A sin(wt)` that is a fitting problem. If the problem is with local minimums you can try to use a statistical optimizer, randomness can usually allow get out from local minima and go toward the global one.

If you could provide us a more specific and clear usage we might be able to help more.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 1, 2025, 7:49am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/13 "2025-07-01T07:49:35Z")

</div>

> [@Geegee](#):
>
> i would need to study a lot more optimal control theory,

Note that I have not suggested [optimal control](https://en.wikipedia.org/wiki/Optimal_control), which is probably irrelevant for your specific problem.

Whether you need more statistics or not depends, from your description of the problem you can get by with very little. Again, I think that the key difficulty here understanding what you are doing, which is not Julia-specific, and you would probably benefit from asking someone in your lab/university for help.

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [July 1, 2025, 9:33am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/14 "2025-07-01T09:33:38Z")

</div>

Thanks a lot for the compassion 🙂 .

One more thing that I probably didn’t clarify enough is our use case.  
We measure data and want to extract from it the parameters (i.e. frequency)  
but we know very very little about the parameters themselves in advance and we want  
that all this happens unsupervised (automatically)  
(like the frequency should be between 100e3 and 100e6, or the amplitude between 1 and 1e6).

I put together a small example:

```Julia
using DataFrames
using CSV
using LsqFit
using GLMakie

data = CSV.read("src/Data/estimation_mwe.csv", DataFrame)
axis = data[!, "axis"]
counts = data[!, "counts"]

function amp_cos_offset(t, p)
    p[1] * cos.(2 * π * p[2] * t) .+ p[3]
end

init_params1 = [212.0, 8.0e6, 9500.0]
init_params2 = [800.0, 1.0e6, 1000.0]

fit_res1 = curve_fit(amp_cos_offset, axis, counts, init_params1)
fit_res2 = curve_fit(amp_cos_offset, axis, counts, init_params2)

init_y1 = amp_cos_offset(axis, init_params1)
fit_y1 = amp_cos_offset(axis, fit_res1.param)
init_y2 = amp_cos_offset(axis, init_params2)
fit_y2 = amp_cos_offset(axis, fit_res2.param)
# visualize the fit results
f = Figure()
Axis(f[1, 1],
     title="Fit with differeing initial parameters",
     xlabel="Measurement duration (s)",
     ylabel="Counts on detector (a.u)",
     titlesize=25,
     xlabelsize=20,
     ylabelsize=20
     )

rd = scatter!(axis, counts);
l1 = lines!(axis, fit_y1);
l2 = lines!(axis, init_y1);
l3 = lines!(axis, fit_y2);
l4 = lines!(axis, init_y2);
Legend(f[1, 2], [rd, l1, l2, l3, l4],
    ["Measurement Data", "Optimized parameters with initial guess $init_params1",
        "Sine with initial parameters $init_params1", "Optimized parameters with initial guess $init_params2",
        "Sine with initial parameters $init_params2"])
f

```

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

I am not sure how to supply the raw data.  
Also

> [@VinceNeede](#):
>
> If the problem is with local minimums you can try to use a statistical optimizer, randomness can usually allow get out from local minima and go toward the global one

this could be very interesting! If it helps I can also link the python code, that we use  
to find the initial parameters (to start the optimization process).

---

<div class="post-metadata">

**Author:** ![VinceNeede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinceneede/32/215744_2.png) [@VinceNeede](https://discourse.julialang.org/u/VinceNeede)\
**Post date:** [July 1, 2025, 10:17am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/15 "2025-07-01T10:17:05Z")

</div>

I think the best way would be to define a loss function and try to minimize that, for example you could use a ChiSq loss function. You would then have something like:

```julia
function find_opt_init_param(init_param)
    fit_result = curve_fit(amp_cos_offset, axis, counts, init_param)
    return sum(fit_result.resid .^ 2)
end

```

that you can then minimize with [Optimization.jl](https://github.com/SciML/Optimization.jl).

If there is a lot of noise and it is very difficult to find some good starting parameters, you could bypass `curve_fit` and try some more “pure” Bayesian approach, you could then take a look at [Turing.jl](https://github.com/TuringLang/Turing.jl).

Needless to say that doing all this makes sense if you really have zero knowledge on the parameters and zero way to retrieve that (in the sinusoidal case, both amplitude and frequency can be estimated from the plot, or in physics most of the time you have some theoretical estimations, or at least some constrains)

---

<div class="post-metadata">

**Author:** ![Geegee](https://avatars.discourse-cdn.com/v4/letter/g/b19c9b/32.png) [@Geegee](https://discourse.julialang.org/u/Geegee)\
**Post date:** [August 28, 2025, 10:25am UTC](https://discourse.julialang.org/t/parameter-estimation-in-mathematical-models-of-common-functions-sine-cos-exp-and-other-special-functions/130325/16 "2025-08-28T10:25:44Z")

</div>

It is now some time since I encountered the issue and I figured out I can use PyCall  
to use most of my old code for parameter estimation and fitting. Just for people who  
also might be also stumbling over this problem.

One just needs to add the python environment to the path.
