How to find local minimum of fit polynomial

조회 수: 13 (최근 30일)
Rizwan Khan
Rizwan Khan 2022년 4월 30일
댓글: Walter Roberson 2022년 5월 1일
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
dpb
dpb 2022년 4월 30일
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
Rizwan Khan 2022년 5월 1일
Dear Sir,
Thanks for the quick response, while you're there, can i quickly ask, where are you downloading the data from, are you able to share the code?
Secondly, in relation to my query, although the model is linear 1, it actually then chagnes the log back and eventually is equivialent to poly3.
So even when i plot it, i get a polynomial, and my question is how do i find the local minimum and maximum.
So as an example, if you look at your data, and change your linear orange line to a polynomial of degree 3, then you will see that it turns at the top, now how can you find the local minimum of that, i believe this required fminsearch or fmin function.
The eventual goal is to draw a polynomial, and then find the most recent turning point, so that may not be the very extreme (global) min or max value of the chart, as the charts goes up and then down and so on, the these local peaks and trophs keep coming, and i want to find the most recent local min or local max
I think the answer is in using fmin or fminsearch function, but i'm unsure how to exactly do that.
Thanks again so much for your response

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2022년 4월 30일
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
Rizwan Khan
Rizwan Khan 2022년 5월 1일
Dear Sir,
Thanks you for your response, i hae a few questions
What is your y varirable, is that a polynomial?
When i use fit it returns me a fit object, and as you rightly say, that comes with coefficients.
So regarding your second approach, that may be quicker for me because i already have the object and therefore the co-efficients.
How would i take the derivative and set it to zero to get the "flat spots" in the curve?
Walter Roberson
Walter Roberson 2022년 5월 1일
roots(polyder(p)) like I posted earlier

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2022년 4월 30일
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
Rizwan Khan
Rizwan Khan 2022년 5월 1일
Hi Walter,
Thanks for your response, yes i log it, but then i also reverse the log, so eventually it actully charts a polynomical, for the purpose of this excercise you can assume i'm getting a polynomial curve of degree 3.
But there can be a few turns, up and down, and i want to find the most recent turning point.
I think the answer is in using Fmin and Fminsearch, but unsure how to do that.
Its an optimisation question i think they call it.
Walter Roberson
Walter Roberson 2022년 5월 1일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by