How to find local minimum of fit polynomial
Afficher commentaires plus anciens
I have some stock data (time and price).
I log the data, and then use fit function to fit a polynomial onto it
then i make a function f
This is shown in the three lines of code below
date_log10 = log10(datenum(T2.Time)); price_log10 = log10(T2.Close);
[f_mdl,gof,output] = fit(date_log10,price_log10,'poly1','Normalize','on','Robust','Bisquare');
f(j) = 10.^f_mdl(log10(datenum(T2.Time(end))));
My question is, how to i now find the local minimum of this function f?
I was thinking i would use fminsearch, but just unsure how i would make use of that.
2 commentaires
dpb
le 30 Avr 2022
What local minimum? It's a linear fit, the minimum will be at either end of the range depending upon whether the slope is positve or negative.
Seems a bizarre thing to even contemplate doing to stock price data...is there some literature in the field that uses such a model?
Also, there's essentially nothing to be gained, anyway...for something to use I downloaded some past 5-6 years for GOOGLE and tried the model over it -- first, of course, visualizing the data as one should always do before just blindly fitting a model--here are the two figures -- real data and then the log10 version -- NB, they are identically the same shape; just changing the numerical values is all that has been done.


As is obvious, the minimum value of the fitted line is the first value since the slope is positive. If we were to arbitrarily flip the data to be reversed in time, then the slope would be the same numerically, but with negative sign and the minimum would be the last point evaluated.
No idea what must be trying to do here...
Rizwan Khan
le 1 Mai 2022
Réponses (2)
Image Analyst
le 30 Avr 2022
I use polyfit() instead of fit(). Have you gotten a fitted y value vector yet? If so, just use min
miny = min(yFitted(index1 : index2))
4 commentaires
Rizwan Khan
le 1 Mai 2022
Image Analyst
le 1 Mai 2022
It gives the min between index1 and index2. I don't know how you were defining the region when you originally said "local minimum of this function f". You didn't say that you needed the last turning point originally like you did just now.
If you have your data in an array you can simply use islocalmin(y) and look at the last index
indexesOfMins = find(islocalmin(y)) % Of ALL mins in the array.
lastIndex = indexesOfMins(end) % Just the very last location ONLY.
lastValue = y(lastIndex) % Value of y there.
Alternatively you can take the coefficients of the fit, take the derivative and set it to zero, and that will tell you the "flat spots" in the curve.
Rizwan Khan
le 1 Mai 2022
Walter Roberson
le 1 Mai 2022
roots(polyder(p)) like I posted earlier
Walter Roberson
le 30 Avr 2022
0 votes
poly1 is of the form a*x+b for some model parameters a and b. The minimum of a straight line is:
- at the lower bound of x if a is positive
- at the upper bound of x if a is negative
2 commentaires
Rizwan Khan
le 1 Mai 2022
Walter Roberson
le 1 Mai 2022
If you take 10 to the power of a linear fit, then the result will still have its maximum at either the upper bound or the lower bound. It will not turn into a cubic!!
If you had the coefficients p for a cubic then use roots(polyder(p)) to find the locations of the critical points
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!