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]);

Réponses (2)

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 commentaires

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.

Connectez-vous pour commenter.

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.

Catégories

En savoir plus sur Descriptive Statistics 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!

Translated by