## polyfit (var* coeff, vars Data, int TimePeriod, int order, var weight) : var

Polynomial regression. Generates a polynomial that is the best fit to a section of a price series or any other data series. This polynomial can be used for extrapolating the **Data** series into the future, and thus predicting future prices.
### Parameters:

**coeff** |
A **var[8]** array for storing the calculated polynomial coefficients, or **0** for storing the coefficients internally. |

**Data** |
Data series to be approximated by the polynomial. |

**TimePeriod** |
Number of elements (**1..1000**) in the **Data** series to be approximated by the polynomial. |

**order** |
Order of the polynom (**1..7**). Use **1** for linear regression, **2** for parabolic regression, and higher numbers for *n*th-order regression. |

**weight** |
Ratio of the weight of the last data value to the weight of the first value, for "fading-memory" polynomials. Use **1** for equal weights. |

### Returns

Correlation coefficient, normally in the **0..1** range. Gives the similarity of the price curve and the polynomial.

### Modifies

**coeff** - set to the coefficients of the polynomial, in the order of their index, starting with **coeff[0]**.

## polynom (var* coeff, int num) : var

Returns the value of the polynom with the given coefficients at a given bar number.
### Parameters:

**coeff** |
A **var[8]** array that contains the polynomial coefficients, or **0** for using the last coefficients generated by **polyfit**. |

**num** |
The bar offset of the returned polynomial value (**0** = current bar). Use negative bar numbers for predicting the future. |

### Returns

The predicted data at the given bar number, based on the polynomial.

### Remarks:

- Polynomials of order
**1** (straight line), **2** (parabola), or **3** are useful for price change predictions. Higher order polynomials are unlikely to give good predictions.
- The weight ratio can be used for giving recent data more weight; however the best predictions are usually generated with
**weight** at **1**.
- For better accuracy, don't fit the polynomial to a price series, but to a series of price differences. Price differences vary more than prices and thus give more accurate correlation coefficients.
- As data series are stored in reverse order, a rising data series generates a polynomial with a falling slope, and vice versa.
- For predicting curve events with polynomial regression, such as crossovers, peaks, or valleys, use the predict function.
- For logistic linear regression with multiple variables, use the advise(PERCEPTRON,...) function.

### Example:

function run()

{

vars Diff = series(price(0)-price(1));
var Correlation = polyfit(0,Diff,15,2,1);

*// sum the differences for predicting the price change over the next 3 bars*

var Change3 = polynom(0,-1)+polynom(0,-2)+polynom(0,-3);

plot("Prediction",price(0)+Change3,MAIN,BLUE);

plot("Correlation",Correlation,NEW,GREEN);

}

### See also:

detect, advise, predict
► latest
version online