Trendlines
Trendlines represent a model of the relationship between two variables. Multiple kinds of trendlines can be added to plots:
- A
LinearTrendLine
represents a linear relationship $y = a \cdot x + b$. - An
ExponentialTrendLine
represents a relationship of the form $y = b \cdot \mathrm{e}^{a \cdot x}$. - A
LogarithmicTrendLine
represents a relationship of the form $y = a \cdot \mathrm{ln} \left ( x \right ) + b$. - A
PowerLawTrendLine
represents a relationship of the form $y = b \cdot x^a$. - A
PolynomialTrendLine
represents a relationship of the form $y = \sum_{i = 0}^n a_i \cdot x^i$. - A
MovingAverageTrendLine
represents a moving average (see below).
Trendlines can be created either by manually specifying the parameters of the functions, or by providing the constructor with the data and letting it perform a regression to automatically determine their values. With the exception of the MovingAverageTrendLine
and the PolynomialTrendLine
, all trendline classes have a Slope
and Intercept
property, which represent, respectively, $a$ and $b$ in the equations above. They also have MinX
, MaxX
, MinY
and MaxY
properties, which define the range in which the trendline is drawn.
The following example shows how to use these elements:
Expand source code
The LinearTrendLine
class
The LinearTrendLine
class represents a simple linear trendline, with the following equation:
This class has three constructors: one allows you to manually specify the slope
($a$), intercept
($b$), and range of values across which to plot the trendline, while the other two determine these automatically from data provided as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) or as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples). These also allow you to fix the intercept value.
The ExponentialTrendLine
class
The ExponentialTrendLine
class represents an exponential trendline, with the following equation:
Like the LinearTrendLine
class, this class has three constructors: one allowing you to manually specify the slope
($a$), intercept
($b$), and range of values across which to plot the trendline, and the other two that determine these automatically from data provided as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) or as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples). These also allow you to fix the intercept value.
The LogarithmicTrendLine
class
The LogarithmicTrendLine
class represents a logarithmic trendline, with the following equation:
Like the LinearTrendLine
class, this class has three constructors: one allowing you to manually specify the slope
($a$), intercept
($b$), and range of values across which to plot the trendline, and the other two that determine these automatically from data provided as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) or as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples). These also allow you to fix the intercept value.
The PowerLawTrendLine
class
The PowerLawTrendLine
class represents a power law trendline, with the following equation:
Like the LinearTrendLine
class, this class has three constructors: one allowing you to manually specify the slope
($a$), intercept
($b$), and range of values across which to plot the trendline, and the other two that determine these automatically from data provided as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) or as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples). Note that you cannot fix the intercept in this case.
The PolynomialTrendLine
class
The PolynomialTrendLine
class represents a polynomial trendline, with the following equation:
Like the LinearTrendLine
class, this class has three constructors: one allowing you to manually specify the coefficients ($a_i$) and range of values across which to plot the trendline, and the other two that determine these automatically from data provided as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) or as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples). For these, you also have to specify the order of the polynomial (i.e., $n$ in the equation above), and you can fix the intercept value (i.e., $a_0$).
The MovingAverageTrendLine
class
The MovingAverageTrendLine
represents a moving average. Given:
- A set of data points $\left \{ \left (x_i, y_i \right) \right \}_i$;
- A “weight function” $w(\Delta i, \Delta x )$;
- A “period” $p \in \mathbb{N^+}$;
For each $i$, we define: \(y'_i = \frac {\sum_{k=-p}^{p} y_{i + k} \cdot w \left (k, x_{i+k} - x_i \right )}{\sum_{k=-p}^{p} w \left (k, x_{i+k} - x_i \right )}\)
Then, the moving average trendline is a polygonal line that passes through the points $\left \{ \left (x_i, y’_i \right) \right \}_i$.
This class has two constructors, one accepting the data as an IReadOnlyList<IReadOnlyList<double>>
(i.e., an array of double[]
) and the other as an IReadOnlyList<(double, double)>
(i.e., an array of (double, double)
tuples); both require you to also specify the period
$p$. Note that from the definition above it follows that the ordering of the data is important, therefore you should ensure that the data array is properly sorted (e.g., based on X
values).
The weight function $w$ is determined by the Weight
property, which should be set to a function accepting two parameters: a int
representing the difference in indices between the point being weighted and the “focus point” for the average, and a double
representing the difference in X
values. The default weight function returns 1
regardless of the values of its arguments.