##
**Solution 11576:** Algorithm Used for Calculating the Sine Regression On the TI-86, TI-83 Family, or TI-84 Plus Family.

### What is the algorithm used to calculate the sine regression on a TI-86, TI-83 family, or TI-84 Plus family?

Sine regression on the TI-86, TI-83 family, or TI-84 Plus family, accepts as an input an (x,y) pair list for the independent and dependent variables respectively. The algorithm assumes that the k^{th} (x,y) data pairs are related by the following formula.

y(k) = a*sin(b*x(k) + c) + d, for k = 1,2,...,n

where, n = length of the x and y lists and a,b,c,d are the SinReg parameters. In addition, optional arguments are allowed for the maximum number of iterations and/or initial guess of the sine functions period. The SinReg problem is therefore to find the a,b,c,d values for the sine formula that best fit the x,y data lists. More formally, best is defined to be the values that makes the following sum of the square error function as small as possible.

J = sum[ (a*sin(b*x(k) + c) + d - y(k))^2 ], for k = 1,2,...,n

Solving this equation for the a,b,c,d parameters that minimize J involves setting the partial derivatives of J with respect to the parameters to zero and solving for the parameters. Unfortunately, doing this produces a set of four non-linear equations in terms of the parameters, which do not have a closed form solution.

To get around this problem, a technique is needed to solve the set of four simultaneous non-linear equations. Sine regression uses the Newton-Raphson technique, which produces a recursion relationship by linearizing the equations. This allows one to iteratively refine the initial parameter guess until the values that minimize J are found. In order to start the recursion, an initial parameter guess must be obtained. It is important that the guess be sufficiently close to the optimal solution to ensure algorithm convergence.

The initial parameter guess is formed using one of two techniques depending on the information provided by the user. The simplest case is when the user provides a period guess, p. A frequency guess can be obtained from the period using the relation:

b = 2*p/p

Given a frequency, the non-linear parameter estimation problem can be reformulated as a three parameter linear regression problem using the double angle sine formula, i.e., given b, find the three parameters a*cos(c), a*sin(c) and d that minimize the sum of the square errors.

J? = sum[({a*cos(c)}*sin(b*x(k)) + {a*sin(c)}*cos(b*x(k)) + d - y(k))^2], for n = 1,2,...,n

Hence, the period/frequency guess can be used to compute the three parameters a*cos(c), a*sin(c) and d that minimize J? using linear regression methods. This gives us an initial guess at all the parameters in the Sine Regression problem and allows us to iteratively refine the parameter estimates using the Newton-Raphson technique outlined earlier.

Iterative refinement of the parameters continues using the Newton-Raphson technique until convergence is achieved, the maximum number of iteration is exhausted, or a singularity error is encountered. If convergence is achieved, the parameters are reported to the user. If the number of iterations exceeds the maximum, the final parameter values are reported along with a warning that the maximum number of iterations was exceeded, meaning that full convergence was not achieved. Note that 3 is the default value for the maximum number of iterations. Hence, many problems will necessitate the user entering a larger number if full convergence is desired.

During the design of this algorithm, it was felt that users would often prefer to quickly receive a less accurate answer than to wait for full algorithm convergence. If full convergence is desired, enter 16 for the maximum number of iterations. Note that the frequency parameter b from a prior SinReg execution can be used to provide a refined period guess for another SinReg execution using,

p = 2*p/b

The singularity error case indicates that iteration of the algorithm is not causing the solution to converge. This means that the data is either not sufficiently sinusoidal to fit the model or that the initial period/frequency guess was not sufficiently accurate to allow algorithm convergence. If a better period guess can be provided, it may still be possible to achieve algorithm convergence.

Assume up to this point that a period guess is provided to start the algorithm. It was felt during the design that users would want the period estimate to be an optional parameter, with the algorithm providing the initial guess when desired. For this option to function properly the data points must be uniformly spaced in time with the x list being in sorted order. More precisely, the user must have

x(k) - x(k-1) = constant, for k = 2,3,...,n

If this restriction can be satisfied, an initial frequency guess for some data sets can be obtained by finding the parameters e,f,g that minimize the following sum of the square errors.

Jd = sum[(y(k) +e*y(k-1) + f*y(k-2) +g)^2], for k = 3,4,...,n

This occurs because the parameters in this equation represent the coefficient of a linear difference equation having a sine function with the frequency of interest as its solution. Note that the parameters that minimize Jd form a linear regression problem and one may therefore obtain a closed form solution for e,f,g using linear regression methods.

Given the parameters, e and f, one may solve for the frequency of the sine function and proceed as if the user had provided a period guess. Note that this technique relies on the data being sufficiently sinusoidal to satisfy the linear constant coefficient difference equation embedded in Jd. If this is not the case, a singularity error will occur, indicating that this technique cannot be used estimate the frequency. At this point, a user provided period estimate must be reentered to use the SinReg algorithm.

Please see the TI-86, TI-83 family or TI-84 Plus family __guidebooks__ for additional information.