I am trying to fit my data in figure 1
to be like figure 2 (exponential fit)
but my code does not do that
using LsqFit
#days = [1,2,3,4,5,6,7,8,9 ,...........]
#y = some code to update y value
@. model(days, p) = p[1] * days.^(-p[2])
fit = curve_fit(model, days, y, [0.5,0.5]) #
@show fit.param
plot(days, model(days, fit.param))
Fitting exponential data is a bit tricky.
If you want a quick fit try fitting the logarithm of the data. To get a correct uncertainty estimation, however, one has to use nonuniform weights.
This fit you can then use to get a better initial value.
Also your data does not look like to follow an exponential function. Maybe one could fit a sigmoid function or restrict the data range.
The function you use is also not an exponential.I would have expected something like this
@. model(days,p) = p[1] + exp(days*p[2])
For restricting the data range you can introduce a mask like this.
mask=y.<=50
curve_fit(model,days[mask],y[mask],[0.5,0.5])
In addition, I noticed that in your example there is a missing ]
for the initial value array.
One thing I wanted to do for some time was figuring out how to use GLM.jl for such applications. I think by selecting the correct link function this might work.
I hope my comments help somewhat.
3 Likes
This seems much easier to do by using blackbox optimization (via Optim) with the actual functional form you want that provides a closure over your data. Itβs true that you can make this into a LsqFit problem, but the more general approach of blackbox optimization is easier once youβre used to it.
1 Like
Here is a function that might work.
julia> using UnicodePlots
julia> scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid (generic function with 1 method)
julia> lineplot((x)->scaled_sigmoid(x,0,2,3,-1))
ββββββββββββββββββββββββββββββββββββββββββ
2 ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β β β β β β£β €β β β β β β β β #9(x)
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β β β β’β‘β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β β β’β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β β β‘β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β β‘Έβ β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β’β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β β‘β β β β β β β β β β β β β β
f(x) ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β’ β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β β‘β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β’°β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β β‘β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β β‘Έβ β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β β‘°β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘β‘β β β β β β β β β β β β β β β β β β β
0 ββ£β£β£β£β£β£β£β£β£β£β£β£β£β£β£β£β£β£β €β β‘β β β β β β β β β β β β β β β β β β β β
ββββββββββββββββββββββββββββββββββββββββββ
β -10β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 10β
At the high and low ends this can also be approximated by an exponential function. (See Fermi-Dirac statistics)
1 Like
feanor12:
lineplot((x)->scaled_sigmoid(x,0,2,3,-1))
How to use it with vectors like
x=[1,2,3,4,5,6,7,β¦]
y=[2,3,3,4,6,7,8,β¦]
feanor12:
scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
using LsqFit
using Plots
x=[1,2,3,4,5,6,7]
y=[2,3,3,4,6,7,8]
@. scaled_sigmoid(x,low,high,a,b) = low+(high-low)*(1/(1+exp(a+b*x)))
scaled_sigmoid(x,p) = scaled_sigmoid(x,p...)
p0 = [0,100,0.1,-1]
fit = curve_fit(scaled_sigmoid,x,y,p0)
scatter(x,y,label="data",xlabel="x",ylabel="y")
plot!((x)->scaled_sigmoid(x,p0),label="initial")
plot!((x)->scaled_sigmoid(x,coef(fit)),label="fit")
1 Like
Easy, like this
using UnicodePlots
x = [1, 2, 3, 4, 5, 6, 7]
y = [2, 3, 3, 4, 6, 7, 8]
lineplot(x, y)
# result will be
ββββββββββββββββββββββββββββββββββββββββββ
8 ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β’β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β’β‘ β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β£β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β’β €β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β β β‘ β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β β’ β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β β β‘°β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β β’β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β β’ β β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β β β β‘°β β β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β β β β’β β β β β β β β β β β β β β β β β β β β β β β
ββ β β β β β β β β β β β β β β’β‘ β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β β β β β’β‘ β €β €β €β €β €β €β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββ β β β‘ β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
2 ββ£β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
ββββββββββββββββββββββββββββββββββββββββββ
β 1β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 7β
I recommend you check the docs, they are nice:
https://github.com/JuliaPlots/UnicodePlots.jl
Or have a look at methods(lineplot)
.
Good luck with Julia)
2 Likes