## Fitting a Trend Line to Data using the Gauss-Newton Method

**Download**

# Description

**Overview:** This model illustrates how to fit a trend line to data using the Gauss-Newton method (https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm). This allows you to estimate trend line parameters that can be useful in forecasting. The implementation uses a Looping Container to carry out the required iterative calculations. The Looping Container is conditionally activated right at the beginning of the simulation and then deactivates after the first time step.

Note that the Gauss-Newton method requires that you have a specific equation you want to fit to data. If you have a complex model for which you need to estimate optimal input parameters, you should use GoldSim's optimization capabilities. But when you want to fit a known equation to a data set, the Gauss-Newton method is much more efficient.

**Details:** The Gauss-Newton method relies on matrix calculations. Input data are stored in Time Series elements. A data vector is generated from Time Series elements using the Time Series lookup capabilities. A Jacobian matrix also must be generated, which stores the negative first derivatives of the fitting function with respect to the function parameters. Basic matrix calculations are then used to iteratively update the parameter value guesses until the estimated parameter values stop changing appreciably.

*Input data options:* The data used in this example is for three major U.S. financial market indexes, the NASDAQ, S&P 500 and NYSE (New York Stock Exchange), between January 1985 and April 2016. Data were downloaded from http://finance.yahoo.com/. In the dashboard, you can select which data set to use as well as the date range of data over which to fit the trend line. This way you can, for example, get an estimate of the annual percentage rate of change over the full 30 years of data or just over the last 7 years. The 'data interval' specifies the between-data point time interval for which the data vector and Jacobian matrix are generated.

*Model fitting options:* In the model fitting options, you can select what kind of trend line to fit to the data (linear, exponential or compound interest). Available options for the trend line equation all have two parameters. These are generically called P1 and P2. In the case of a linear trend line, P1 is the y-intercept (i.e. the time-zero value) and P2 is the slope in units of 1/yr (i.e. the per-year market value increase). In the case of the exponential and compound interest equations, P1 is the time-zero starting value and P2 is the annual interest rate or percent change.

**Generating and Viewing Results:** In the input data options section, select a data set and date range to use. Then, in the model fitting options section, select an equation and enter an initial guess for the equation parameters, P1 and P2. Note that the current default values, 500 and 0.1, work well for most settings. You may not need to change these values at all. Then click the 'Run Model' button to run the model. View the data and the estimated trend line by clicking the 'Plot' button under 'Model Fit' in the results section.

# Additional Information

## Keywords

optimization, gauss, newton, trend, line, model, fitting, data, parameter, estimation

## Categories

GoldSim Features & Capabilities, GoldSim Applications, Time Series & Lookup Tables, Containers & SubModels, Dashboards, Arrays, Optimization and Sensitivity Analysis, Financial Modeling