Use the polyfit to predict the future data tendency

I was using the polyfit function to find out the polynomial coefficients from a set of given points. Domain is [0 300]. After that, I was trying to predict the future basing on the given data [0 400] using polyval, but the matlab just didn't give me any value after [0 300]
stocks = hist_stock_data('25012008','25042009','SF','PAG');
Data = [1:1:300];
Date_value = stocks(1).Close(1:1:300)
p = polyfit(Data,Date_value,18);
DD = [300:1:400];
y = polyval(p,DD);
figure
hold on
plot(DD,y,'rp')
axis([0 400 25 60]);

답변 (2개)

Star Strider
Star Strider 2017년 3월 17일
You are extrapolating an 18-degree polynomial. I will say no more.
The polyval function is ‘predicting’ values far outside the range of your plot, that being:
axis([0 400 25 60])

댓글 수: 2

what would be a proper order for the polyfit
That depends on what your data are.
I would limit it to 3 in most ‘real world’ situations, and not more than 7.

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

Image Analyst
Image Analyst 2017년 3월 17일
18th order? Have you ever heard of overfitting? Or extrapolating using such a high order? Non-training points in between the training points will oscillate wildly, and outside the training range the fit will very rapidly go to -inf or +inf.
Anyway, as a learning experience, try this:
fittedDates = [0:1:400];
fittedY = polyval(p, fittedDates);
% Plot fitted y along with extrapolated y:
plot(fittedDates, fittedY, 'b-', 'LineWidth', 2);
I'm pretty sure, even without looking at the plot or running the code, that the data from 300 to 400 will skyrocket towards infinity. When you run the code, you'll see what I mean.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2017년 3월 17일

답변:

2017년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by