<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>.NET exponential regression Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/net-exponential-regression/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/net-exponential-regression</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 07 Feb 2023 21:49:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Excel Trendlines</title>
		<link>https://www.centerspace.net/excel-trendlines</link>
					<comments>https://www.centerspace.net/excel-trendlines#comments</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Thu, 25 Mar 2010 15:45:35 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[.NET exponential regression]]></category>
		<category><![CDATA[.NET linear regression]]></category>
		<category><![CDATA[.NET logarithmic regression]]></category>
		<category><![CDATA[.NET moving average filter]]></category>
		<category><![CDATA[.NET polynomial regression]]></category>
		<category><![CDATA[.NET power regression]]></category>
		<category><![CDATA[C# exponential regression]]></category>
		<category><![CDATA[C# linear regression]]></category>
		<category><![CDATA[C# logarithmic regression]]></category>
		<category><![CDATA[C# moving average filter]]></category>
		<category><![CDATA[C# polynomial regression]]></category>
		<category><![CDATA[C# power regression]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=1870</guid>

					<description><![CDATA[<p>We are sometimes asked how to reproduce the various Excel Trendline types in NMath: Linear, Logarithmic, Exponential, Power, Polynomial, and Moving Average. In this post, we show you how to compute each trendline using NMath, including printing out the form of the equation and the R2 value (coefficient of determination). </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/excel-trendlines">Excel Trendlines</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We are sometimes asked how to reproduce the various Excel Trendline types in NMath, including printing out the form of the equation and the R2 value (coefficient of determination). Excel offers these trend types:</p>
<ul>
<li>Linear Trendline</li>
<li>Logarithmic Trendline</li>
<li>Exponential Trendline</li>
<li>Power Trendline</li>
<li>Polynomial Trendline</li>
<li>Moving Average Trendline</li>
</ul>
<p>These can all be easily computed using NMath. Let&#8217;s see how. First, let&#8217;s start with a simple data series:</p>
<pre lang="csharp">DoubleVector x = new DoubleVector(11, 15, 18, 23, 26, 31, 39, 44, 54, 64, 74);
DoubleVector y = new DoubleVector(0.00476, 0.0105, 0.0207, 0.0619, 0.337, 0.74, 1.7, 2.45, 3.5, 4.5, 5.09);</pre>
<p>These data represent the evolution of an algal bloom in the Adriatic Sea. The x-values are time expressed in days, and the y-values are the size of the surface of the bloom in mm<sup>2</sup>.</p>
<h3>Linear Trendline</h3>
<p>The Linear trendline fits a line, <code>y = a*x + b</code>, to the data. This is easily computed using the LinearRegression class in NMath Stats, as shown in the following C# code:</p>
<pre lang="csharp">bool addIntercept = true;
LinearRegression lr = new LinearRegression(new DoubleMatrix(x), y, addIntercept);
LinearRegressionAnova lrAnova = new LinearRegressionAnova(lr);
double a = lr.Parameters[0];
double b = lr.Parameters[1];
double r2 = lrAnova.RSquared;

Console.WriteLine("y = {0}*x + {1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output</p>
<pre class="code">y = 0.0913403802489977*x -1.63908651994092
r2 = 0.967828167696715</pre>
<p>Note that to compute the coefficient of determination (R2), we construct a LinearRegressionAnova object from the LinearRegression instance. This class tests overall model significance for regressions computed by LinearRegression.</p>
<h3>Logarithmic Trendline</h3>
<p>The Logarithmic trendline fits a line to ln(x), y&#8211;that is, <code>y = a*ln(x) + b</code>. Again, we can use the LinearRegression class for this:</p>
<pre lang="csharp">DoubleVector logX = NMathFunctions.Log(x);
lr = new LinearRegression(new DoubleMatrix(logX), y, addIntercept);
a = lr.Parameters[0];
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*ln(x) + {1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = -8.17805531083818*ln(x) + 2.87283105614145
r2= 0.840393353070466</pre>
<h3>Exponential Trendline</h3>
<p>The Exponential trendline fits the function <code>y = a * e ^(b * x)</code>. This can also be computed using LinearRegression by fitting a line to x, ln(y). Taking the log of both sides of the function gives:</p>
<pre>ln(y) = ln(a * e ^(b * x)) = ln(a) + bx</pre>
<p>The following C# code does this:</p>
<pre lang="csharp">DoubleVector logY = NMathFunctions.Log(y);
lr = new LinearRegression(new DoubleMatrix(x), logY, addIntercept);
a = Math.Exp(lr.Parameters[0]);
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*e^{1}*x", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = 0.00552689525130073*e^0.112876522212724*x
r2 = 0.812366433832176</pre>
<p>Note that because the intercept of the fitted line is the natural log of the &#8220;a&#8221; parameter in the exponential function, we need to take the exponential of the found intercept (using <code>Math.Exp</code>) to recover the value of &#8220;a&#8221;.</p>
<h3>Power Trendline</h3>
<p>The Power trendline fits the function <code>y = a * x^b</code>. This can be computed using LinearRegression by fitting a line to ln(x), ln(y). Taking the log of both sides of the equation gives:</p>
<pre>ln(y) = ln(a * x^b) = ln(a) + b * ln(x)</pre>
<pre lang="csharp">lr = new LinearRegression(new DoubleMatrix(logX), logY, addIntercept);
lrAnova = new LinearRegressionAnova(lr);
a = Math.Exp(lr.Parameters[0]);
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*x^{1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = 2.46993343563889E-07*x^4.11443630332377
r2 = 0.947447653331871</pre>
<p>Again, we recover the value of parameter &#8220;a&#8221; by taking the exponential of the found intercept.</p>
<h3>Polynomial Trendline</h3>
<p>NMath provides class PolynomialLeastSquares for fitting a polynomial of the specified degree to paired vectors of x- and y-values. For example, this code fits a cubic to our data:</p>
<pre lang="csharp">int degree = 3;
PolynomialLeastSquares pls = new PolynomialLeastSquares(degree, x, y);

// compute R2
DoubleVector predictions = pls.FittedPolynomial.Evaluate(x);
double regressionSumOfSquares = StatsFunctions.SumOfSquares(predictions - StatsFunctions.Mean(y));
double residualSumOfSquares = pls.LeastSquaresSolution.Residuals.TwoNormSquared();
r2 = regressionSumOfSquares / (regressionSumOfSquares + residualSumOfSquares);

Console.WriteLine("y = " + pls.FittedPolynomial);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = -4.68278158094222E-05x^3 + 0.00640408381023593x^2 - 
      0.1643720340709x + 1.12703300920657
r2 = 0.998634459376868</pre>
<p>Note that PolynomialLeastSquares does not currently provide the R2 value, so in the code above we compute it directly.</p>
<h3>Moving Average Trendline</h3>
<p>Unlike the trendlines we&#8217;ve examined so far, a moving average does not fit a functional form to data. Rather, it filters data in order to smooth out noise. NMath provides class MovingWindowFilter for this purpose.</p>
<p>Class MovingWindowFilter replaces data points with a linear combination of the data points immediately to the left and right of each point, based on a given set of coefficients to use in the linear combination. Static class methods are provided for generating coefficients to implement a moving average filter and a Savitzky-Golay smoothing filter.</p>
<p>For example, the following C# code filters the data using a window of width 3 (the &#8220;period&#8221; parameter in Excel):</p>
<pre lang="csharp">int numberLeft = 1;
int numberRight = 1;
DoubleVector movingAvgCoefficents = MovingWindowFilter.MovingAverageCoefficients(numberLeft, numberRight);
MovingWindowFilter filter = new MovingWindowFilter(numberLeft, numberRight, movingAvgCoefficents);
DoubleVector yfiltered = filter.Filter(y, MovingWindowFilter.BoundaryOption.DoNotFilterBoundaryPoints);
Console.WriteLine("yfiltered = " + yfiltered);</pre>
<p>Output:</p>
<pre class="code">yfiltered = [ 0.00476 0.0119866666666667 0.0310333333333333
              0.139866666666667 0.379633333333333 0.925666666666667 1.63
              2.55 3.48333333333333 4.36333333333333 5.09 ]</pre>
<h3>Advanced Curve Fitting</h3>
<p>That covers the simple trendlines produced by Excel. For advanced curve fitting, NMath provides class OneVariableFunctionFitter which fits arbitrary one variable functions to a set of points. (You must supply at least as many data points to fit as your function has parameters.) In a future post, we&#8217;ll take a look at this class. In the meantime, see <a href="https://www.centerspace.net/examples/nmath/csharp/one-variable-curve-fitting-example.php">this page</a> for an example demonstrating the use of OneVariableFunctionFitter, including how to define your own function.</p>
<p>Ken</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/excel-trendlines">Excel Trendlines</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/excel-trendlines/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1870</post-id>	</item>
	</channel>
</rss>
