Calculating trendlines using slope and y-intercept

I have a requirement in one of my project to draw trendlines on charts. I need to know in the below equations (where $a$ is slope and $b$ is y-intercept):

• [Linear Trendline]: $y = ax + b$
• [Logarithmic Trendline]: $y = a \log x + b$
• [Polynomial Trendline]: $y = a(n)x^{n} + a(n-1)x^{n-1} + a(1)x + a(0)$
• [Power Trendline]: $y = ax^b$
• [Exponential Trendline]: $y = ab^x$

Does the value of $m$ and $b$ can be calculated once and used in all of the equations or the values of $a$ and $b$ are calculated in a different way for every equation. If they are calculated differently for every equation, how are they calculated? Can someone provide me a link for that. Currently, I am using this method to calculate $a$ and $b$.

Another question is how accurate are the other equations to calculate trendlines? Is there any other method that is more accurate than this?

Your first question: in the models you have provided, all coefficients must be recalculated from scratch when switching models.

There do exist models with the property that the coefficients that you calculated can be re-used when switching to another model. A famous example is the Newton polynomial that runs through $N$ points $(x,f(x))$:

$$\hat f (x) = \sum_{k=0}^{N} a_k \cdot \begin{cases} \prod_{m=0}^{k-1} (x - f(x)) & k \ge 1 \\ 1 & \text{otherwise .}\end{cases}$$

The $a_k$ that causes $\hat f (x)$ to run through all of the points $(x,f(x))$ are given by a formula that depends only on $(x_0, f(0))$ through $(x_k, f(x_k))$, so adding more points to the right (increasing $k$ from $k_0$ to $k_0+b$) will introduce new coefficients $a_{k_0+1}$ through $a_{k_0+b}$ without changing any of the existing coefficients $a_0$ through $a_k$.

For probably most models, all the existing coefficients must be adjusted in some way. An example is an $N$-term Fourier series that runs through $N$ equispaced points $(x,f(x))$:

$$\hat f (x) = \sum_{k=-(N-1)/2}^{(N-1)/2} a_k \cdot e^{i (2 \pi / N) k x}$$

The $a_k$ that causes $\hat f (x)$ to run through all of the points $(x,f(x))$ are given by an $O(N^2)$ algorithm:

$$a_k = \sum_{x=x_0+0}^{x_0+N-1} f(x) \cdot e^{-i (2 \pi / N) k x}$$

If you double the number of samples over the same time interval from $N$ = $N_0$ to $2N_0$, it can be shown that $a_k$ changes to $a_k^* = a_k + \left(e^{-i(2 \pi / (2N_0))k} \right) \cdot b_k$ where $b_k$ are the coefficients of the $N$-term Fourier series over just the new samples. This property is the basis of the Cooley—Tukey FFT algorithm which turns the calculation of $a_k$ for any $N$-term Fourier series where $N$ is a power-of-2 into an $O(N\,log\,N)$ algorithm.

Now, I think that answering your second question will answer the rest of your first question. Accurate is subjective. For $n$ points $(x_i, y(x_i))$, you can come up with all sorts of "doodles", in a sense, all of which have the property that they run through or near all of the points (but some will be better looking to the human eye than others). Similarly, for a real-valued function $f(x)$, the most accurate trend line would be $f(x)$ itself. But say that you have decided that the data "looks linear" or "looks exponential", or "should theoretically be following this equation within uncertainty for some unknown parameters $a, b, c$". The most common way of picking the parameters in practice is to ask which set of parameters minimizes the sum of squared error between the model and the original data at each point:

$$a, b, c \text{ are defined such that } \sum_{k=1}^{N} (\hat f(x_k) - y_k)^2 \text{ is minimized.}$$

It's under this criteria (the "least squares" criteria) that the formulas for calculating the parameters for models are derived (using calculus to differentiate the expression with respect to $x$ and isolate $a, b, c$). Problems of this nature are known as optimization problems, and in case general formulas to find $a, b, c$ can't be determined, $a, b, c$ can be approximated by a computer using simulated annealing and gradient descent.

There is no necessarily right or wrong criteria for $a, b, c$, and least squares is not valid for all situations. For example, humans perceive certain types of distortion in an image or an audio signal more than other types of distortion, even if those distortions have a smaller sum of squared error than the latter. The choice of error criteria in image processing is discussed in the magazine article Mean Squared Error: Love It or Leave It?.

(Also, to answer your question, I believe all the formulas you stumbled upon most likely calculate the same parameters in different ways arriving at the same exact numbers; if that's the case, they're equally accurate because they satisfy the same least squares criteria, but they might not be as accurate in all situations if a computer calculates them using floating point arithmetic, because some algorithms might have better numerical stability than others, but that's a separate problem that I don't think you're talking about.)

There are some other models that you didn't mention. The most popular general interpolating curves these days are cubic splines (just a bunch of piecewise cubic polynomials of the form $a + bx + cx^2 + dx^3$ spliced together), and Bézier curves. There also always exists an ($n+1$)-degree polynomial that runs through $n$ points on a graph, and finding the coefficients of that polynomial can be done by solving an ($n+1$) by ($n+1$) system of equations, but then there's the unfortunate Runge's phenomenon.

• If I have a set of values for x and y coordinates say (1,2)(2,3)(3,4) then the calculated values of 'a' and 'b' are going to be always the same if I use the approach mentioned at <a>classroom.synonym.com/calculate-trendline-2709.html< /a>. Then what is the use of calculating them for scratch? I have only one set of values for which I need to generate y coordinate based on the value of x for each type of trendline. – Rohan Jun 12 '14 at 9:17