<?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/"
	>

<channel>
	<title>CenterSpace Blog &#187; NMath</title>
	<atom:link href="http://www.centerspace.net/blog/category/nmath/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.centerspace.net/blog</link>
	<description>The CenterSpace blog about our NMath mathematical and statistical libraries in .NET/C#, and object-oriented numerics in general.</description>
	<lastBuildDate>Tue, 13 Dec 2011 17:35:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>The Importance of Graphing Your Data</title>
		<link>http://www.centerspace.net/blog/nmath/the-importance-of-graphing-your-data/</link>
		<comments>http://www.centerspace.net/blog/nmath/the-importance-of-graphing-your-data/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 17:28:10 +0000</pubDate>
		<dc:creator>Ken Baldwin</dc:creator>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Stats]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[.NET charting]]></category>
		<category><![CDATA[.NET plotting]]></category>
		<category><![CDATA[C# charts]]></category>
		<category><![CDATA[C# ploting]]></category>
		<category><![CDATA[Chart Controls for .NET]]></category>
		<category><![CDATA[F# charts]]></category>
		<category><![CDATA[VB.NET charts]]></category>
		<category><![CDATA[VB.NET plotting]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3639</guid>
		<description><![CDATA[<img class="excerpt" title="Anscombe's Quartet" src="http://www.centerspace.net/blog/wp-content/uploads/2011/12/anscombe2.png" alt="Anscombe's Quartet" />
In his classic book <em>The Visual Display of Quantitative Information</em>, Edward R. Tufte argued that "graphics can be more precise and revealing than conventional statistical computations".  As an example, he described <a href="http://en.wikipedia.org/wiki/Anscombe%27s_quartet">Anscombe's Quartet</a>--four datasets that have identical simple statistical properties, yet appear very different when graphed.]]></description>
			<content:encoded><![CDATA[<p>In his classic book <em>The Visual Display of Quantitative Information</em>, Edward R. Tufte argued that &#8220;graphics can be more precise and revealing than conventional statistical computations&#8221;. As an example, he described <a href="http://en.wikipedia.org/wiki/Anscombe%27s_quartet">Anscombe&#8217;s Quartet</a>&#8211;four datasets that have identical simple statistical properties, yet appear very different when graphed.<br />
<a href="http://www.centerspace.net/blog/wp-content/uploads/2011/12/anscombe2.png"><img class="aligncenter size-full wp-image-3655" title="Anscombe's Quartet" src="http://www.centerspace.net/blog/wp-content/uploads/2011/12/anscombe2.png" alt="Anscombe's Quartet" width="451" height="283" /></a><br />
These data sets&#8211;each consisting of 11 x,y points&#8211;were constructed by statistician Francis Anscombe in 1973.</p>
<p>As <a href="http://www.centerspace.net/blog/nmath/net-math-with-microsoft-chart-controls-revisited/">previously described</a>, <strong>NMath 5.1</strong> and <strong>NMath Stats 3.4</strong> include classes for plotting NMath types using the Microsoft Chart Controls for .NET. (<a href="http://www.centerspace.net/blog/visualization/nmath-integration-with-essential-chart/">Free adapter code</a> is also available for using NMath with Syncfusion Essential Chart.) Let&#8217;s use Anscombe&#8217;s data to explore how NMath&#8217;s new visualization capabilities can be used to reveal the differences in the data sets.</p>
<p>First, we&#8217;ll load the data into a <strong>DoubleMatrix</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">DoubleMatrix A <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleMatrix<span style="color: #000000;">&#40;</span> <span style="color: #666666;">@&quot;11x8 [
  10.0 8.04  10.0 9.14 10.0 7.46  8.0  6.58
  8.0  6.95  8.0  8.14 8.0  6.77  8.0  5.76
  13.0 7.58  13.0 8.74 13.0 12.74 8.0  7.71
  9.0  8.81  9.0  8.77 9.0  7.11  8.0  8.84
  11.0 8.33  11.0 9.26 11.0 7.81  8.0  8.47
  14.0 9.96  14.0 8.10 14.0 8.84  8.0  7.04
  6.0  7.24  6.0  6.13 6.0  6.08  8.0  5.25
  4.0  4.26  4.0  3.10 4.0  5.39  19.0 12.50
  12.0 10.84 12.0 9.13 12.0 8.15  8.0  5.56
  7.0  4.82  7.0  7.26 7.0  6.42  8.0  7.91
  5.0  5.68  5.0  4.74 5.0  5.73  8.0  6.89 ]&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now let&#8217;s perform some simple descriptive statistics.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #FF0000;">int</span> groups <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
 Slice rows <span style="color: #008000;">=</span> Slice.<span style="color: #0000FF;">All</span><span style="color: #008000;">;</span>
 Slice xCols <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Slice<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">0</span>, groups, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Slice yCols <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Slice<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">1</span>, groups, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #FF0000;">double</span> unbiased <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span><span style="color: #000000;">&#41;</span>A.<span style="color: #0000FF;">Rows</span> <span style="color: #008000;">/</span> <span style="color: #000000;">&#40;</span> A.<span style="color: #0000FF;">Rows</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Mean of x: {0}&quot;</span>,
   NMathFunctions.<span style="color: #0000FF;">Mean</span><span style="color: #000000;">&#40;</span> A<span style="color: #000000;">&#91;</span> rows, xCols <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Variance of x: {0}&quot;</span>,
   NMathFunctions.<span style="color: #0000FF;">Variance</span><span style="color: #000000;">&#40;</span> A<span style="color: #000000;">&#91;</span>rows, xCols<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">*</span> unbiased <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Mean of y: {0}&quot;</span>,
   NMathFunctions.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> NMathFunctions.<span style="color: #0000FF;">Mean</span><span style="color: #000000;">&#40;</span> A<span style="color: #000000;">&#91;</span>rows, yCols<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Variance of y: {0}&quot;</span>,
   NMathFunctions.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span>
     NMathFunctions.<span style="color: #0000FF;">Variance</span><span style="color: #000000;">&#40;</span> A<span style="color: #000000;">&#91;</span>rows, yCols<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">*</span> unbiased, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Correlation of x-y: &quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> A.<span style="color: #0000FF;">Cols</span><span style="color: #008000;">;</span> i <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
   Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span> NMathFunctions.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span>
    StatsFunctions.<span style="color: #0000FF;">Correlation</span><span style="color: #000000;">&#40;</span> A.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>, A.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span>
 Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You can see from the output that the statistics are nearly identical for all four data sets:</p>
<pre class="code">Mean of x: [ 9 9 9 9 ]
Variance of x: [ 11 11 11 11 ]
Mean of y: [ 7.5 7.5 7.5 7.5 ]
Variance of y: [ 4.127 4.128 4.123 4.123 ]
Correlation of x-y: 0.816 0.816 0.816 0.817</pre>
<p>Now let&#8217;s fit a linear model to each data set.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> LinearRegression<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> lrs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinearRegression<span style="color: #000000;">&#91;</span>groups<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> groups<span style="color: #008000;">;</span> i <span style="color: #008000;">++</span> <span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Group {0}&quot;</span>, i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #FF0000;">bool</span> addIntercept <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
   lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinearRegression<span style="color: #000000;">&#40;</span> <span style="color: #008000;">new</span> DoubleMatrix<span style="color: #000000;">&#40;</span> A.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>,
     A.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span>, addIntercept <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;equation of regression line: Y = {0} + {1}X&quot;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   LinearRegressionParameter param <span style="color: #008000;">=</span>
     <span style="color: #008000;">new</span> LinearRegressionParameter<span style="color: #000000;">&#40;</span> lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;standard error of estimate of slope: {0}&quot;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> param.<span style="color: #0000FF;">StandardError</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;t-statistic: {0}&quot;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> param.<span style="color: #0000FF;">TStatistic</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   LinearRegressionAnova anova <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinearRegressionAnova<span style="color: #000000;">&#40;</span> lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;regression sum of squares: {0}&quot;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> anova.<span style="color: #0000FF;">RegressionSumOfSquares</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;residual Sum of squares: {0}&quot;</span>,
     Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> anova.<span style="color: #0000FF;">ResidualSumOfSquares</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;r2: {0}&quot;</span>, Math.<span style="color: #0000FF;">Round</span><span style="color: #000000;">&#40;</span> anova.<span style="color: #0000FF;">RSquared</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Again, the output is nearly identical for each data set:</p>
<pre class="code">Group 1
equation of regression line: Y = 3 + 0.5X
standard error of estimate of slope: 0.118
t-statistic: 4.24
regression sum of squares: 27.51
residual Sum of squares: 13.76
r2: 0.67

Group 2
equation of regression line: Y = 3 + 0.5X
standard error of estimate of slope: 0.118
t-statistic: 4.24
regression sum of squares: 27.5
residual Sum of squares: 13.78
r2: 0.67

Group 3
equation of regression line: Y = 3 + 0.5X
standard error of estimate of slope: 0.118
t-statistic: 4.24
regression sum of squares: 27.47
residual Sum of squares: 13.76
r2: 0.67

Group 4
equation of regression line: Y = 3 + 0.5X
standard error of estimate of slope: 0.118
t-statistic: 4.24
regression sum of squares: 27.49
residual Sum of squares: 13.74
r2: 0.67</pre>
<p>Finally, let&#8217;s use the new <strong>NMath</strong> charting functionality to plot each linear regression fit. Note that we make use of the <code>Compose()</code> method to combine multiple charts into a single composite <strong>Chart</strong> control.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> List<span style="color: #008000;">&lt;</span>Chart<span style="color: #008000;">&gt;</span> charts <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Chart<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> lrs.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
   charts.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span> NMathStatsChart.<span style="color: #0000FF;">ToChart</span><span style="color: #000000;">&#40;</span> lrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>, <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span>
 Chart all <span style="color: #008000;">=</span> NMathStatsChart.<span style="color: #0000FF;">Compose</span><span style="color: #000000;">&#40;</span> charts, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">2</span>,
   NMathChart.<span style="color: #0000FF;">AreaLayoutOrder</span>.<span style="color: #0000FF;">RowMajor</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> groups<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisX</span>.<span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;x&quot;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisX</span>.<span style="color: #0000FF;">Minimum</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisX</span>.<span style="color: #0000FF;">Maximum</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">22</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisX</span>.<span style="color: #0000FF;">Interval</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
&nbsp;
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisY</span>.<span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;y&quot;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisY</span>.<span style="color: #0000FF;">Minimum</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisY</span>.<span style="color: #0000FF;">Maximum</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">14</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">ChartAreas</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AxisY</span>.<span style="color: #0000FF;">Interval</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span>
&nbsp;
   all.<span style="color: #0000FF;">Series</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Color</span> <span style="color: #008000;">=</span> Color.<span style="color: #0000FF;">DarkOrange</span><span style="color: #008000;">;</span>
   all.<span style="color: #0000FF;">Series</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Color</span> <span style="color: #008000;">=</span> Color.<span style="color: #0000FF;">SteelBlue</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span>
 NMathStatsChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> all <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/12/anscombe.png"><img class="aligncenter size-full wp-image-3640" title="Anscombe's Quartet" src="http://www.centerspace.net/blog/wp-content/uploads/2011/12/anscombe.png" alt="Anscombe's Quartet" width="497" height="499" /></a>The charts reveal  dramatic differences between the data sets, despite the identical fitted models. Group 1 shows a linear relationship, while in Group 2 the releationship is clearly non-linear.  Groups 3 and 4 demonstrate how a single outlier can have a large effect on simple statistics.</p>
<p>Ken</p>
<p><strong>References</strong></p>
<p>Anscombe, F. J. (1973). &#8220;Graphs in Statistical Analysis&#8221;. American Statistician 27 (1): 17–21. JSTOR 2682899.<br />
Tufte, Edward R. (2001). The Visual Display of Quantitative Information (2nd ed.). Cheshire, CT: Graphics Press</p>
]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/the-importance-of-graphing-your-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clearing a vector</title>
		<link>http://www.centerspace.net/blog/nmath/clearing-a-vector/</link>
		<comments>http://www.centerspace.net/blog/nmath/clearing-a-vector/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 22:28:01 +0000</pubDate>
		<dc:creator>Trevor Misfeldt</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3621</guid>
		<description><![CDATA[A customer recently asked us for the best method to zero out a vector. We decided to run some tests to find out. Here are the five methods we tried, with any drawbacks and the timings. These were performed on a DoubleVector, v,  of length 10,000,000. 1) Create a new vector. This isn&#8217;t really clearing...]]></description>
			<content:encoded><![CDATA[<p>A customer recently asked us for the best method to zero out a vector. We decided to run some tests to find out. Here are the five methods we tried, with any drawbacks and the timings.</p>
<p>These were performed on a DoubleVector, v,  of length 10,000,000.</p>
<p>1) Create a new vector. This isn&#8217;t really clearing out an existing vector but we thought we should include it for completeness.</p>
<pre> DoubleVector v2 = new DoubleVector( v.Length, 0.0 );</pre>
<p>The big drawback here is that you&#8217;re creating new memory. Time: <strong>419.5ms</strong></p>
<p>2) Probably the first thing to come to mind is to simply iterate through the vector and set everything to zero.</p>
<pre>
for ( int i = 0; i &lt; v.Length; i++ )
{
  v[i] = 0.0;
}</pre>
<p>We have to do some checking in the index operator. No new memory created. Time: <strong>578.5ms</strong></p>
<p>3) In some cases, you could iterate through the underlying array of data inside the DoubleVector.</p>
<pre>
for ( int i = 0; i &lt; v.DataBlock.Data.Length; i++ )
{
  v.DataBlock.Data[i] = 0.0;
}</pre>
<p>This is a little less intuitive. And, very importantly, it will not work with many views into other data structures. For example, a row slice of a matrix. However, it&#8217;s easier for the CLR to optimize this loop. Time: <strong>173.5ms</strong></p>
<p>4) We can use the power of Intel&#8217;s MKL to multiply the vector by zero.</p>
<pre> v.Scale( 0.0 );</pre>
<p>Scale() does this in-place. No new memory is created. In this example, we assume that MKL has already been loaded and is ready to go which is true if another MKL-based NMath call was already made or if NMath was <a href="http://www.centerspace.net/blog/nmath/initializing-nmath/">initialized</a>. This method will work on all views of other data structures. Time: <strong>170ms</strong></p>
<p>5) This surprised us a bit but the best method we could find was to clear out the underlying array using Array.Clear() in .NET</p>
<pre> Array.Clear( v.DataBlock.Data, 0, v.DataBlock.Data.Length );</pre>
<p>This creates no new memory. However, this will not work with non-contiguous views. However, this method is very fast. Time: <strong> 85.8ms</strong></p>
<p>To make this much simpler, we have created a <code>Clear()</code> method and a <code>Clear( Slice)</code> method on vectors and matrices. It will do the right thing in the right circumstance. It will be released in NMath 5.2 in 2012.</p>
<p>And, here is the code we used for this test:</p>
<pre>
using System;
using CenterSpace.NMath.Core;

namespace Test
{
  class ClearVector
  {
    static int size = 100000000;
    static int runs = 10;
    static int methods = 5;

    static void Main( string[] args )
    {
      System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
      DoubleMatrix times = new DoubleMatrix( runs, methods );
      NMathKernel.Init();

      for ( int run = 0; run < runs; run++ )
      {
        Console.WriteLine( "Run {0}...", run );
        DoubleVector v = null;

        // Create a new one
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Start();
        DoubleVector v2 = new DoubleVector( v.Length, 0.0 );
        sw.Stop();
        times[run, 0] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v2 ) );

        // iterate through vector
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        for ( int i = 0; i < v.Length; i++ )
        {
          v[i] = 0.0;
        }
        sw.Stop();
        times[run, 1] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );

        // iterate through array
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        for ( int i = 0; i < v.DataBlock.Data.Length; i++ )
        {
          v.DataBlock.Data[i] = 0.0;
        }
        sw.Stop();
        times[run, 2] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );

        // scale
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        v.Scale( 0.0 );
        sw.Stop();
        times[run, 3] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );

        // Array Clear
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        Array.Clear( v.DataBlock.Data, 0, v.DataBlock.Data.Length );
        sw.Stop();
        times[run, 4] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );
        Console.WriteLine( times.Row( run ) );
      }
      Console.WriteLine( "Means: " + NMathFunctions.Mean( times ) );
    }

    private static bool Assert( DoubleVector v )
    {
      if ( v.Length != size )
      {
        return false;
      }
      for ( int i = 0; i < v.Length; ++i )
      {
        if ( v[i] != 0.0 )
        {
          return false;
        }
      }
      return true;
    }
  }
}
</pre>
<p>- Trevor</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Fclearing-a-vector%2F&amp;title=Clearing%20a%20vector"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/clearing-a-vector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Initializing NMath</title>
		<link>http://www.centerspace.net/blog/nmath/initializing-nmath/</link>
		<comments>http://www.centerspace.net/blog/nmath/initializing-nmath/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 22:01:27 +0000</pubDate>
		<dc:creator>Trevor Misfeldt</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3611</guid>
		<description><![CDATA[NMath uses Intel&#8217;s Math Kernel Library (MKL) internally. This code contains native, optimized code to wring out the best performance possible. There is a one-time delay when the appropriate x86 or x64 native code is loaded. This cost can be easily controlled by the developer by using the NMathKernel.Init() method. Please see Initializing NMath for...]]></description>
			<content:encoded><![CDATA[<p>NMath uses Intel&#8217;s Math Kernel Library (MKL) internally. This code contains native, optimized code to wring out the best performance possible.</p>
<p>There is a one-time delay when the appropriate x86 or x64 native code is loaded. This cost can be easily controlled by the developer by using the NMathKernel.Init() method. Please see <a href="http://centerspace.net/doc/NMath/user/overview-4.html#81117">Initializing NMath</a> for more details.</p>
<p>- Trevor</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Finitializing-nmath%2F&amp;title=Initializing%20NMath"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/initializing-nmath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CEM Solver at UT Austin</title>
		<link>http://www.centerspace.net/blog/nmath/cem-solver-at-ut-austin/</link>
		<comments>http://www.centerspace.net/blog/nmath/cem-solver-at-ut-austin/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 23:55:16 +0000</pubDate>
		<dc:creator>Andy Gray</dc:creator>
				<category><![CDATA[Case Studies]]></category>
		<category><![CDATA[NMath]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3602</guid>
		<description><![CDATA[The folks at the Center for Electromechanics at University of Texas at Austin (UT-CEM) are doing some neat simulation projects with power systems, and we were honored to learn that NMath is at the core of their CEM Solver software. CEM Solver demonstrates substantial performance improvements over SimPowerSystems, reducing simulation time for a typical example...]]></description>
			<content:encoded><![CDATA[<p>The folks at the Center for Electromechanics at University of Texas at Austin (UT-CEM) are doing some neat simulation projects with power systems, and we were honored to learn that NMath is at the core of their CEM Solver software. CEM Solver demonstrates substantial performance improvements over SimPowerSystems, reducing simulation time for a typical example from 23 minutes down to 30 seconds, for a 46x improvement!</p>
<p>Dr. Fabian M. Uriarte, Research Associate at UT-CEM, recently had this to say about NMath:</p>
<p style="padding-left: 30px">“If performance, documentation, and reliable technical support are important to you, then NMath is what you need. In our application, we parallelize the solution of large, sparse Ax=b systems. Using NMath, we saw significant performance gains over Math.NET. Additionally, NMath has a very large set of classes that suit our needs in other numerical aspects as well. We are extremely pleased with the performance of NMath and highly recommend it to others. This library is a &#8216;must have.&#8217;&quot;</p>
<p>Thanks for the kind words, Fabian!&#160; You can check out the CEM Solver project at <a href="http://www.utexas.edu/research/cem/projects/CEM_solver.html" target="_blank">http://www.utexas.edu/research/cem/projects/CEM_solver.html</a>, or check out the demonstration video below.</p>
<div style="text-align: center"><object width="512" height="288"><param name="movie" value="https://www.youtube.com/v/I4JpiP0OF8w&amp;hl=en_US&amp;feature=player_embedded&amp;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="https://www.youtube.com/v/I4JpiP0OF8w&amp;hl=en_US&amp;feature=player_embedded&amp;version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="512" height="288"></embed></object></div>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Fcem-solver-at-ut-austin%2F&amp;title=CEM%20Solver%20at%20UT%20Austin"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/cem-solver-at-ut-austin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Math with Microsoft Chart Controls, Revisited</title>
		<link>http://www.centerspace.net/blog/nmath/net-math-with-microsoft-chart-controls-revisited/</link>
		<comments>http://www.centerspace.net/blog/nmath/net-math-with-microsoft-chart-controls-revisited/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 18:28:35 +0000</pubDate>
		<dc:creator>Ken Baldwin</dc:creator>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Stats]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[.NET plotting]]></category>
		<category><![CDATA[C# ploting]]></category>
		<category><![CDATA[Chart Controls for .NET]]></category>
		<category><![CDATA[VB.NET charts]]></category>
		<category><![CDATA[VB.NET plotting]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3538</guid>
		<description><![CDATA[<img class="excerpt" title="Linear Regression Chart" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/lr.png" alt="Linear Regression Chart" />
With the most recent releases of NMath 5.1 and NMath Stats 3.4, creating charts from our class types is easy with new adapter classes.  These classes use the .NET Microsoft Chart Controls for visualization, and allow for complex customization or simple data visualization with one line of code.  ]]></description>
			<content:encoded><![CDATA[<h3>Easy Charting</h3>
<p>In <a href="http://www.centerspace.net/blog/nmath/using-nmath-with-microsoft-chart-controls-for-net/">an earlier post</a>, we described how NMath types can be plotted using the Microsoft Chart Controls for .NET, but programmatically constructing <strong>Chart</strong> objects with data series bound to NMath objects. The <a href="http://www.centerspace.net/blog/nmath/new-versions-of-nmath-libraries-released-3/">latest release</a> of the NMath libraries makes this process much simpler by providing <em>adapter classes</em> which easily construct basic charts for you from NMath types:</p>
<ul>
<li>NMath 5.1 includes assembly <strong>NMathChartMicrosoft.dll</strong> containing class <strong>NMathChart</strong>, which provides convenience methods for plotting NMath types using the Microsoft Chart Controls</li>
<li>NMath Stats 3.4 includes assembly <strong>NMathStatsChartMicrosoft.dll</strong> containing <strong>NMathStatsChart</strong> which provides convenience methods for plotting NMath Stats types using the Microsoft Chart Controls</li>
</ul>
<p>The generated charts can then be customized to meet your needs.</p>
<h3>Using the Chart Adapters</h3>
<p>The Microsoft Chart Controls for .NET are available as a <a href="http://www.microsoft.com/download/en/details.aspx?id=14422">separate download for .NET 3.5</a>. Beginning in .NET 4.0, the Chart controls are part of the .NET Framework. To use the Chart controls, add a reference to <code>System.Windows.Forms.DataVisualization</code>.<br />
To use the adapters, add a reference to <strong>NMathChartMicrosoft.dll</strong> and/or <strong>NMathStatsChartMicrosoft.dll,</strong> and <code>using</code> statement:</p>
<pre class="code">using CenterSpace.NMath.Charting.Microsoft;</pre>
<p>A <a href="http://www.centerspace.net/blog/nmath/using-nmath-charts-with-net-4-0/">previous blog article</a> provides a simple <code>app.config</code> solution for deploying your chart dependent applications to target both .NET 3.5 or .NET 4.0.  Without this <code>app.config</code> modification, your application will only target .NET 3.5.</p>
<h3>The Adapter API</h3>
<p>Both <strong>NMathChart</strong> and <strong>NMathStatsChart</strong> classes share a common API. Overloads of the <code>ToChart()</code> function are provided for our common math and stats types. <code>ToChart()</code> returns an instance of <code>System.Windows.Forms.DataVisualization.Charting.Chart</code>, which can be customized as desired. For example</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Polynomial poly <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Polynomial<span style="color: #000000;">&#40;</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">5</span>, <span style="color: #008000;">-</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Chart chart <span style="color: #008000;">=</span> NMathChart.<span style="color: #0000FF;">ToChart</span><span style="color: #000000;">&#40;</span> poly, <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
chart.<span style="color: #0000FF;">Titles</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello World&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>For prototyping and debugging console applications, the <code>Show()</code> function shows a given chart in a default form.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NMathChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> chart <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Note that when the window is closed, the chart is disposed.</p>
<p>If you do not need to customize the chart, overloads of <code>Show()</code> are also provided for common NMath types.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NMathChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> poly <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This is equivalent to calling:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NMathChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> NMathChart.<span style="color: #0000FF;">ToChart</span><span style="color: #000000;">&#40;</span> poly <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The <code>Save()</code> function saves a chart to a file or stream.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">NMathChart.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span> chart, <span style="color: #666666;">&quot;chart.png&quot;</span>, ChartImageFormat.<span style="color: #0000FF;">Png</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If you are developing a Windows Forms application using the Designer, add a Microsoft Chart control to your form, then update it with an NMath object using the appropriate <code>Update()</code> function after initialization.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  Polynomial poly <span style="color: #008000;">=</span>
    <span style="color: #008000;">new</span> Polynomial<span style="color: #000000;">&#40;</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">5</span>, <span style="color: #008000;">-</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  NMathChart.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span> <span style="color: #0600FF;">ref</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">chart1</span>, poly, <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This has the following effect on your existing Chart object:</p>
<ul>
<li>a new, default ChartArea is added if one does not exist, otherwise chart.ChartAreas[0] is used</li>
<li>axis titles, and DefaultAxisTitleFont and DefaultMajorGridLineColor, only have an effect if a new ChartArea is added</li>
<li>titles are added only if the given Chart does not already contain any titles</li>
<li>chart.Series[0] is replaced, or added if necessary</li>
</ul>
<h3>Charting Examples</h3>
<p>Here are a few examples of usage in C# (Of course, as with all NMath functionality, these examples will also work, given the necessary syntactic changes, from VB.NET or F#, or any other .NET language.) :</p>

<div class="wp_syntax"><div class="code"><pre class="chsarp" style="font-family:monospace;">double xmin = -1;
double xmax = 1;
int numInterpolatedValues = 100;
Polynomial poly = new Polynomial( new DoubleVector( 4, 2, 5, -2, 3 ) );
NMathChart.Show( poly, xmin, xmax, numInterpolatedValues );</pre></div></div>

<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/polynomial.png"><img class="aligncenter size-full wp-image-3542" title="Polynomial" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/polynomial.png" alt="" width="489" height="489" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">DoubleVector x <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">100</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0.1</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector cos <span style="color: #008000;">=</span> x.<span style="color: #0000FF;">Apply</span><span style="color: #000000;">&#40;</span> NMathFunctions.<span style="color: #0000FF;">CosFunction</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector sin <span style="color: #008000;">=</span> x.<span style="color: #0000FF;">Apply</span><span style="color: #000000;">&#40;</span> NMathFunctions.<span style="color: #0000FF;">SinFunction</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
DoubleVector<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> cos, sin <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
NMathChart.<span style="color: #0000FF;">Unit</span> unit <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NMathChart.<span style="color: #0000FF;">Unit</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0.1</span>, <span style="color: #666666;">&quot;x&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Chart chart <span style="color: #008000;">=</span> NMathChart.<span style="color: #0000FF;">ToChart</span><span style="color: #000000;">&#40;</span> data, unit <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
chart.<span style="color: #0000FF;">Series</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;cos(x)&quot;</span><span style="color: #008000;">;</span>
chart.<span style="color: #0000FF;">Series</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sin(x)&quot;</span><span style="color: #008000;">;</span>
NMathChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> chart <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/vector_array.png"><img class="aligncenter size-full wp-image-3541" title="Vector Array" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/vector_array.png" alt="" width="489" height="487" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">double</span> lambda <span style="color: #008000;">=</span> <span style="color: #FF0000;">4.0</span><span style="color: #008000;">;</span>
PoissonDistribution poisson <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PoissonDistribution<span style="color: #000000;">&#40;</span> lambda <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
NMathStatsChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> poisson,
  NMathStatsChart.<span style="color: #0000FF;">DistributionFunction</span>.<span style="color: #0000FF;">PDF</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/poisson_pdf1.png"><img class="aligncenter size-full wp-image-3544" title="Poisson Distribution" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/poisson_pdf1.png" alt="" width="487" height="490" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">DoubleMatrix predictors <span style="color: #008000;">=</span>
  <span style="color: #008000;">new</span> DoubleMatrix<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">100</span>, <span style="color: #FF0000;">3</span>, <span style="color: #008000;">new</span> RandGenUniform<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">;</span>
DoubleVector observations <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> predictors.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span>
  <span style="color: #008000;">-</span><span style="color: #FF0000;">0.75</span> <span style="color: #008000;">*</span> predictors.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1.25</span> <span style="color: #008000;">*</span> predictors.<span style="color: #0000FF;">Col</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.5</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> addIntercept <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
LinearRegression lr <span style="color: #008000;">=</span>
  <span style="color: #008000;">new</span> LinearRegression<span style="color: #000000;">&#40;</span> predictors, observations, addIntercept <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
NMathStatsChart.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span> lr, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<pre class="csharp"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/lr.png"><img class="aligncenter size-full wp-image-3545" title="Linear Regression" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/lr.png" alt="" width="492" height="491" /></a></pre>
<p><strong>More Information</strong></p>
<p>For more information, and many more examples, see:</p>
<ul>
<li>Whitepaper: <a href="http://www.centerspace.net/doc/NMath/whitepapers/NMath.ChartMicrosoft.pdf">NMath Visualization Using the Microsoft Chart Controls</a></li>
<li>Whitepaper: <a href="http://www.centerspace.net/doc/NMathStats/whitepapers/NMath.Stats.ChartMicrosoft.pdf">NMath Stats Visualization Using the Microsoft Chart Controls</a></li>
<li>the <a href="http://www.centerspace.net/doc/NMathSuite/ref/">API docs</a> for the <code>CenterSpace.NMath.Charting.Microsoft </code>namespace</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/net-math-with-microsoft-chart-controls-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NMath Charts with .NET 4.0</title>
		<link>http://www.centerspace.net/blog/nmath/using-nmath-charts-with-net-4-0/</link>
		<comments>http://www.centerspace.net/blog/nmath/using-nmath-charts-with-net-4-0/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 23:10:47 +0000</pubDate>
		<dc:creator>Ken Baldwin</dc:creator>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Stats]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[.NET charting]]></category>
		<category><![CDATA[C# charts]]></category>
		<category><![CDATA[deploying .NET charts]]></category>
		<category><![CDATA[F# charts]]></category>
		<category><![CDATA[VB.NET charts]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3558</guid>
		<description><![CDATA[The NMath Chart DLLs (NMathChartMicrosoft.dll and NMathStatsChartMicrosoft.dll) are built against the .NET 3.5 version of the Microsoft Chart Controls for .NET (System.Windows.Forms.DataVisualization). The .NET runtime will not automatically replace this reference with the version of the Microsoft Chart DLL built into the .NET 4.0 Framework, unless you explicitly tell it to do so by adding the...]]></description>
			<content:encoded><![CDATA[<p>The NMath Chart DLLs (<strong>NMathChartMicrosoft.dll</strong> and <strong>NMathStatsChartMicrosoft.dll</strong>) are built against the .NET 3.5 version of the Microsoft Chart Controls for .NET (<strong>System.Windows.Forms.DataVisualization</strong>). The .NET runtime will not automatically replace this reference with the version of the Microsoft Chart DLL built into the .NET 4.0 Framework, unless you explicitly tell it to do so by adding the following lines to your <code>app.config</code>:</p>
<pre class="code">
<table>
<tbody>
<tr>
<td>
&lt;configuration&gt;
   ...
   &lt;runtime&gt;
      ...
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
         ...
         &lt;dependentAssembly&gt;
            &lt;assemblyIdentity name="System.Windows.Forms.DataVisualization"
              publicKeyToken="31bf3856ad364e35"/&gt;
            &lt;bindingRedirect oldVersion="3.5.0.0-3.5.0.0" newVersion="4.0.0.0"/&gt;
         &lt;/dependentAssembly&gt;
         ...
      &lt;/assemblyBinding&gt;
      ...
   &lt;/runtime&gt;
   ...
&lt;/configuration&gt;
</td>
</tr>
</tbody>
</table>
</pre>
<p>In this way, your application with charts can be <em>deployed to both .NET 3.5 and .NET 4.0.</em> Please note that all other NMath assemblies (with non-visual functionality) can be used in .NET 3.5 and .NET 4.0 without this step.</p>
<p>See <a href="http://msdn.microsoft.com/en-us/library/dd990785.aspx">this tutorial</a> for more information.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Fusing-nmath-charts-with-net-4-0%2F&amp;title=Using%20NMath%20Charts%20with%20.NET%204.0"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/using-nmath-charts-with-net-4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Versions of NMath Libraries Released</title>
		<link>http://www.centerspace.net/blog/nmath/new-versions-of-nmath-libraries-released-3/</link>
		<comments>http://www.centerspace.net/blog/nmath/new-versions-of-nmath-libraries-released-3/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 15:29:36 +0000</pubDate>
		<dc:creator>Ken Baldwin</dc:creator>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Stats]]></category>
		<category><![CDATA[.NET math library]]></category>
		<category><![CDATA[C# math library]]></category>
		<category><![CDATA[F# math library]]></category>
		<category><![CDATA[new C# math library release]]></category>
		<category><![CDATA[new NMath release]]></category>
		<category><![CDATA[new NMath Stats release]]></category>
		<category><![CDATA[VB math libary]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3504</guid>
		<description><![CDATA[CenterSpace is proud to announce the immediate availability of new versions of our .NET math libraries, NMath 5.1 and NMath Stats 3.4. This release adds many new features and performance enhancements. Version 5.1 of NMath adds: Assembly NMathChartMicrosoft.dll containing class NMathChart, which provides static methods for plotting NMath types using the Microsoft Chart Controls for .NET. [More]...]]></description>
			<content:encoded><![CDATA[<p>CenterSpace is proud to announce the immediate availability of new versions of our .NET math libraries, <strong>NMath 5.1</strong> and <strong>NMath Stats 3.4</strong>. This release adds many new features and performance enhancements.</p>
<p>Version 5.1 of <strong>NMath</strong> adds:</p>
<ul>
<li>Assembly <code>NMathChartMicrosoft.dll</code> containing class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Charting_Microsoft_NMathChart.htm">NMathChart</a>, which provides static methods for plotting <strong>NMath</strong> types using the Microsoft Chart Controls for .NET. [<a href="http://www.centerspace.net/doc/NMath/whitepapers/NMath.ChartMicrosoft.pdf">More</a>]</li>
</ul>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/fit1.png"><img class="aligncenter size-full wp-image-3533" title="OneVariableFunctionFitter" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/fit1.png" alt="" width="490" height="493" /></a></p>
<ul>
<li>Classes for solving first order initial value differential equations by the Runge-Kutta method. Class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Analysis_FirstOrderInitialValueProblem.htm">FirstOrderInitialValueProblem</a> encapsulates a first order initial value differential equation, and class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Analysis_RungeKuttaSolver.htm">RungeKuttaSolver</a> solves them. [<a href="http://www.centerspace.net/doc/NMath/user/differential-equations.html">More</a>]</li>
<li>Exponential moving average filter weights to class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Core_MovingWindowFilter.htm">MovingWindowFilter</a>.</li>
</ul>
<p>A complete changelog is located <a href="http://www.centerspace.net/doc/NMath/changelog.txt">here</a>.</p>
<p>Version 3.4 of <strong>NMath Stats</strong> adds:</p>
<ul>
<li>Assembly <code>NMathStatsChartMicrosoft.dll</code> containing class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Charting_Microsoft_NMathStatsChart.htm">NMathStatsChart</a>, which provides static methods for plotting <strong>NMath Stats</strong> types using the Microsoft Chart Controls for .NET. [<a href="http://www.centerspace.net/doc/NMathStats/whitepapers/NMath.Stats.ChartMicrosoft.pdf">More</a>]</li>
</ul>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/09/poisson_pdf.png"><img class="aligncenter size-full wp-image-3534" title="poisson_pdf" src="http://www.centerspace.net/blog/wp-content/uploads/2011/09/poisson_pdf.png" alt="" width="487" height="490" /></a></p>
<ul>
<li>Class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Stats_TwoSampleUnpairedUnequalTTest.htm">TwoSampleUnpairedUnequalTTest</a>. Unlike <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Stats_TwoSampleUnpairedTTest.htm">TwoSampleUnpairedTTest</a>, a pooled estimate of the variance is not used.</li>
</ul>
<p>A complete changelog is located <a href="http://www.centerspace.net/doc/NMathStats/changelog.txt">here</a>.</p>
<p>The new release is build-compatible with the prior release. Upgrades are provided free of charge to customers with current annual maintenance contracts.  Maintenance contracts are available through our <a href="http://www.centerspace.net/order/how-to-order/">webstore</a>.</p>
<p>This release features simplified NMath and NMath Stats installers. Updated license keys will be issued at upgrade time. A license file must now be deployed with your NMath applications. [<a href="http://www.centerspace.net/doc/NMath/user/overview-4.html#82645">More</a>]</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Fnew-versions-of-nmath-libraries-released-3%2F&amp;title=New%20Versions%20of%20NMath%20Libraries%20Released"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/new-versions-of-nmath-libraries-released-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fitting Geometric Primitives to Points Using Nonlinear Least Squares</title>
		<link>http://www.centerspace.net/blog/nmath/nmath-tutorial/fitting-geometric-primitives-to-points-using-nonlinear-least-squares/</link>
		<comments>http://www.centerspace.net/blog/nmath/nmath-tutorial/fitting-geometric-primitives-to-points-using-nonlinear-least-squares/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 15:39:51 +0000</pubDate>
		<dc:creator>Ken Baldwin</dc:creator>
				<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[.NET Levenberg-Marquardt]]></category>
		<category><![CDATA[.NET nonlinear least squares]]></category>
		<category><![CDATA[C# Levenberg-Marquardt]]></category>
		<category><![CDATA[C# nonlinear least squares]]></category>
		<category><![CDATA[VB.NET Levenberg-Marquardt]]></category>
		<category><![CDATA[VB.NET nonlinear least squares]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3460</guid>
		<description><![CDATA[We were recently contacted by a customer looking for help on how to use NMath to fit geometric primitives to clouds of 2D points. The solution is to cast the problem as a minimization. In the space of the parameters which define the geometric object, minimize the residuals with respect to the 2D points. NMath...]]></description>
			<content:encoded><![CDATA[<p>We were recently contacted by a customer looking for help on how to use NMath to fit geometric primitives to clouds of 2D points. The solution is to cast the problem as a minimization. In the space of the parameters which define the geometric object, minimize the residuals with respect to the 2D points. NMath provides .NET classes for solving <a href="http://www.centerspace.net/doc/NMath/user/nonlinear-least-squares.html">nonlinear least squares problems</a> such as this, using the Trust-Region or Levenberg-Marquardt methods.</p>
<p>For instance, let&#8217;s try fitting a circle to a set of points. A circle is defined by three parameters: a center (a,b), and a radius r. The circle is all points such that (x-a)^2 + (y &#8211; b)^2 = r^2. To setup the minimization problem, we first need to define a function which given a set of circle parameters, returns the residuals with respect to a cloud of 2D points. There are several methods for doing this in NMath. In the following C# code, we extend the abstract base class <a href="http://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Core_DoubleMultiVariableFunction.htm">DoubleMultiVariableFunction</a>, and override the <code>Evaluate()</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CircleFitFunction <span style="color: #008000;">:</span> DoubleMultiVariableFunction
<span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> CircleFitFunction<span style="color: #000000;">&#40;</span> DoubleVector x, DoubleVector y <span style="color: #000000;">&#41;</span>
    <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">3</span>, x.<span style="color: #0000FF;">Length</span> <span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> x.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> y.<span style="color: #0000FF;">Length</span> <span style="color: #000000;">&#41;</span>
      <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;Unequal number of x,y values.&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    X <span style="color: #008000;">=</span> x<span style="color: #008000;">;</span>
    Y <span style="color: #008000;">=</span> y<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> DoubleVector X <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">internal</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
  <span style="color: #0600FF;">public</span> DoubleVector Y <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">internal</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Evaluate<span style="color: #000000;">&#40;</span> DoubleVector parameters, <span style="color: #0600FF;">ref</span> DoubleVector residuals <span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// parameters of circle with center (a,b) and radius r</span>
    <span style="color: #FF0000;">double</span> a <span style="color: #008000;">=</span> parameters<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> b <span style="color: #008000;">=</span> parameters<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> r <span style="color: #008000;">=</span> parameters<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> X.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// distance of point from circle center</span>
      <span style="color: #FF0000;">double</span> d <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span> X<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">-</span> a, <span style="color: #FF0000;">2.0</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span> Y<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">-</span> b, <span style="color: #FF0000;">2.0</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      residuals<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> d <span style="color: #008000;">-</span> r<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The constructor takes the set of x,y points to fit, which are stored on the class. Note that we call the base constructor with the dimensions of the domain and range of the function:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">3</span>, x.<span style="color: #0000FF;">Length</span> <span style="color: #000000;">&#41;</span></pre></div></div>

<p>In this case, the function maps the 3-dimensional space of the circle parameters to the x.Length-dimension space of the residuals.</p>
<p>Next we override the <code>Evaluate()</code> method, which takes an input vector and output vector passed by reference. Our implementation computes the residual for each point with respect to a circle defined by the given parameters. We calculate the distance, d, of each point from the circle center; the residual is then equal to d &#8211; r. The nonlinear least squares method will minimize the L2 norm of this function.</p>
<p>To demonstrate the fitting process, let&#8217;s first start with a set of points generated from a circle of known parameters. For example, this C# code creates 20 x,y points evenly spaced around a circle with center (0.5, 0.25) and radius 2, plus some added noise:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">20</span><span style="color: #008000;">;</span>
DoubleVector x <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> n <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector y <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> n <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">double</span> a <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.5</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">double</span> b <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.25</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">double</span> r <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
&nbsp;
RandGenUniform noise <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RandGenUniform<span style="color: #000000;">&#40;</span> <span style="color: #008000;">-</span>.75, .75 <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> n<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span> <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #FF0000;">double</span> t <span style="color: #008000;">=</span> i <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">*</span> Math.<span style="color: #0000FF;">PI</span> <span style="color: #008000;">/</span> n<span style="color: #008000;">;</span>
  x<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> a <span style="color: #008000;">+</span> r <span style="color: #008000;">*</span> Math.<span style="color: #0000FF;">Cos</span><span style="color: #000000;">&#40;</span> t <span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> noise.<span style="color: #0000FF;">Next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  y<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> b <span style="color: #008000;">+</span> r <span style="color: #008000;">*</span> Math.<span style="color: #0000FF;">Sin</span><span style="color: #000000;">&#40;</span> t <span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> noise.<span style="color: #0000FF;">Next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now that we have our function defined and some points to fit, performing the minimization takes only a few lines of code. This C# code finds the minimum and prints the solution and final residual:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">CircleFitFunction f <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CircleFitFunction<span style="color: #000000;">&#40;</span> x, y <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
TrustRegionMinimizer minimizer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TrustRegionMinimizer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector start <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;0.1 0.1 0.1&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector solution <span style="color: #008000;">=</span> minimizer.<span style="color: #0000FF;">Minimize</span><span style="color: #000000;">&#40;</span> f, start <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;solution = &quot;</span> <span style="color: #008000;">+</span> solution <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;final residual = &quot;</span> <span style="color: #008000;">+</span> minimizer.<span style="color: #0000FF;">FinalResidual</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Sample output:</p>
<pre>solution = [ 0.446122611523468 0.175483486509012 1.93511389538286 ]
final residual = 1.62414259527024</pre>
<p>Starting from point (0.1, 0.1, 0.1) in the parameter space of the circle, we minimized the sum of the squared residuals with respect to the (x,y) points. In the run above, the minimum found was center (0.45, 0.18) and radius 1.9, close to the actual parameters which generated our (noisy) data. To visually inspect the fit, we can use NMath with the <a href="http://www.centerspace.net/blog/nmath/using-nmath-with-microsoft-chart-controls-for-net/">Microsoft Chart Controls for .NET</a>.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit.png"><img class="aligncenter size-full wp-image-3465" title="Fitted Circle" src="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit.png" alt="Fitted Circle" width="485" height="491" /></a></p>
<p>Now that we&#8217;re sure the minimization is working as desired, let&#8217;s try it again with some random points:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
RandGenUniform rng <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RandGenUniform<span style="color: #000000;">&#40;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector x <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> n, rng <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector y <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> n, rng <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
CircleFitFunction f <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CircleFitFunction<span style="color: #000000;">&#40;</span> x, y <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
TrustRegionMinimizer minimizer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TrustRegionMinimizer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector start <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;0.1 0.1 0.1&quot;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DoubleVector solution <span style="color: #008000;">=</span> minimizer.<span style="color: #0000FF;">Minimize</span><span style="color: #000000;">&#40;</span> f, start <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Again, plotting the solution:</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit2.png"><img class="aligncenter size-full wp-image-3466" title="Fitted Circle" src="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit2.png" alt="Fitted Circle" width="488" height="488" /></a>In some runs, the fit may seem counter-intuitive. For example:</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit3.png"><img class="aligncenter size-full wp-image-3467" title="Fitted Circle" src="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit3.png" alt="Fitted Circle" width="488" height="490" /></a></p>
<p>But if you think about it, this sort of fit makes perfect sense, given how we defined our fit function. We&#8217;re asking the minimizer to minimize the residuals with respect to the circle perimeter, and allowing it vary the circle center and radius however it can to achieve that goal. In an extreme case, if our points fall approximately along a line, the best fit will be a circle with a very large radius, so that the circle perimeter is nearly linear as it passes through the points.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit4.png"><img class="aligncenter size-full wp-image-3470" title="Fitted Circle" src="http://www.centerspace.net/blog/wp-content/uploads/2011/08/fit4.png" alt="Fitted Circle" width="493" height="488" /></a>In this case, the fitted circle has center (428.0, -757.8) and radius 870.6.  If we wish to avoid solutions such as these, we could define our fit function differently. For example, we might constrain the circle center to be the center of mass of the points, and vary only the radius.</p>
<p>Obviously, a similar technique can be used to fit other geometric primitives, though as the complexity of the shape increases, so does the complexity of the fit function.</p>
<p>Ken</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Fnmath%2Fnmath-tutorial%2Ffitting-geometric-primitives-to-points-using-nonlinear-least-squares%2F&amp;title=Fitting%20Geometric%20Primitives%20to%20Points%20Using%20Nonlinear%20Least%20Squares"><img src="http://www.centerspace.net/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/nmath-tutorial/fitting-geometric-primitives-to-points-using-nonlinear-least-squares/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing .Net Libraries in SQL Server</title>
		<link>http://www.centerspace.net/blog/nmath/accessing-net-libraries-in-sql-server/</link>
		<comments>http://www.centerspace.net/blog/nmath/accessing-net-libraries-in-sql-server/#comments</comments>
		<pubDate>Thu, 12 May 2011 01:16:11 +0000</pubDate>
		<dc:creator>Mike Magee</dc:creator>
				<category><![CDATA[CenterSpace]]></category>
		<category><![CDATA[Corporate]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[NMath]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3399</guid>
		<description><![CDATA[Running NMath in the SQL Server CLR using User Defined Types (UDTs) and User Defined Functions (UDFs).]]></description>
			<content:encoded><![CDATA[<p>  In previous posts we demonstrated calling CenterSpace&#8217;s NMath libraries from Excel.  Some customers asked if we could call these libraries from Microsoft&#8217;s SQL Server so we decided to give it a shot.  It turns out the problem is a substantially harder problem to solve as the SQL environment is an order of magnitude more complex.  This post will show it is possible, but with a few caveats.</p>
<p>  There are a myriad of ways SQL Server can be setup and for this post, we took whatever shortcut we could find.  Our goal was to get this running and worry about doing it &#8220;right&#8221; later.  We found ourselves on a fairly steep learning curve without a lot of examples to work off of. Consider this more of a &#8220;proof of concept&#8221; post than the definitive &#8220;how to&#8221;.</p>
<p>  As mentioned above there are lot of different approaches to this problem.  In our minds, we wanted to see if we could couple our library at the server side and access results via Reporting Services and Report Builder. We believed that the computations would be more efficient to be handled at the server while the result could take advantage of the graphing packages in Report Services.</p>
<p>  Our approach was to build a standalone system to develop and test this work on.  We choose a standard PC platform running Windows 7.  The first task at hand was to decide which version of Microsoft SQL Server to work with.  We chose SQL Server 2008R2 full version as we needed the Report Services support.  Microsoft supplies a six month trial version that has all services available which was perfect for our project.  We installed the full version with support for the Management Studio, Report Services, and Analysis services. In addition, we downloaded Microsoft&#8217;s AdventureWorks sample databases for our testing environment.</p>
<p>  We got this all running and ran some sample scripts to make sure we had everything running correctly before moving on.  Since we were going to be developing code we installed Visual Studio 2010 next and made sure we included the necessary development templates for the SQL Server environment.</p>
<p>  After installing CenterSpace&#8217;s NMath 5.0 library, we were ready to write some code.</p>
<p>  We will start by creating a new project in Visual Studio called NMathSQL.</p>
<div id="attachment_3404" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_01.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_01.png" alt="" title="Creating a SQL CLR project in Visual Studio" width="500" class="size-full wp-image-3404" /></a><p class="wp-caption-text">Creating a new SQL CLR project in Visual Studio</p></div>
<p>We then asked to pick a server and identify the database we will be using.<br />
<div id="attachment_3406" class="wp-caption aligncenter" style="width: 410px"><br />
<a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_02.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_02.png" alt="" title="Connecting to the Database" width="400"  class="size-full wp-image-3406" /></a><p class="wp-caption-text">Connecting to the Database</p></div></p>
<p>  In the next screen we will want to enable SQL/CLR debugging.</p>
<div id="attachment_3411" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_03.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_03.png" alt="" title="Enabling SQL/CLR Debugging" width="400" class="size-full wp-image-3411" /></a><p class="wp-caption-text">Enabling SQL/CLR debugging</p></div>
<p>  At this point we need to start running SQL scripts as part of the overall setup. We could exit VS and perform these tasks in the Management Studio, but VS can do the job.  By selecting from the VS menu bar Data>Transact-SQL Editor>New Query Connection, we get a new window to enter scripts to be run on our database.</p>
<div id="attachment_3414" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_04.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_04.png" alt="" title="Accessing the Transact SQL Editor " width="500" class="size-full wp-image-3414" /></a><p class="wp-caption-text">Accessing the Transact SQL Editor in Visual Studio</p></div>
<p>  We found it easier to copy and paste our scripts into this windows as we needed to run SQL statements.</p>
<p>  Our first task at hand is to load the assembly NMath.dll into the database we have selected to work in. It is at this point we hit our first real obstacle.  It turns out that in our shipping NMath.dll we are using some instructions to automatically load x86 or x64. These are illegal in the SQL CLR.  We were forced to remove these instructions and build an entirely separate version of NMath.dll for SQL Server.  Stepping back and looking at the big picture, we decided that this was an opportunity to also include some of the &#8220;glue&#8221; code we knew we wanted to write.  </p>
<p>  We had been looking at writing some User Defined Types (UDT) and User Defined Aggregate (UDA) functions to handle the calls into our library. It seemed to make sense to extend our libraries type definitions to be T-SQL types. The result of this approach would enable T-SQL to use NMath types such as DoubleVector and DoubleMatrix. We needed to have some way to load our new data types so we built UDAs LoadDoubleVector, LoadDoubleMatrixByRow, and LoadDoubleMatrixByColumn.  After building our new, custom NMath.dll, we can then run a script to load our assembly into the database.  </p>
<p>  Before we could do this, though, we discovered that we needed some basic assemblies that SQL server had left out of their CLR.  The following SQL script loads those assemblies.</p>
<div id="attachment_3415" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_05.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_05.png" alt="" title="Loading Supporting Assemblies" width="500"  class="size-full wp-image-3415" /></a><p class="wp-caption-text">Loading Supporting Assemblies to a SQL Database</p></div>
<p>  We are now ready to run the following SQL script to load NMath for SQL.</p>
<div id="attachment_3418" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_06.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_06.png" alt="" title="Loading NMath assembly in the SQL Database" width="500"  class="size-full wp-image-3418" /></a><p class="wp-caption-text">Loading NMath assembly in the SQL Database</p></div>
<p>  The next step is to add the reference to our project. From the main toolbar launch the Solution Explorer and right click on Reference then select Add Reference.  Add the new assemblies we have loaded so far.</p>
<div id="attachment_3419" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_07.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_07.png" alt="" title="Adding NMath reference to the SQL/CLR project" width="500" class="size-full wp-image-3419" /></a><p class="wp-caption-text">Adding NMath reference to the SQL/CLR project</p></div>
<p>  We will now have to tell SQL about the new types and functions we have added.  We will start by adding the new UDTs DoubleVector and DoubleMatrix.</p>
<div id="attachment_3421" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_08.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_08.png" alt="" title="SQL commands to add UDTs for LoadDoubleVector" width="500" class="size-full wp-image-3421" /></a><p class="wp-caption-text">SQL commands to add UDTs for LoadDoubleVector</p></div>
<p>  Now that SQL knows about our new types we can add the UDA functions with the following SQL commands.</p>
<div id="attachment_3422" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_09.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_09.png" alt="" title="SQL Commands to add UDAs to the Database" width="500"  class="size-full wp-image-3422" /></a><p class="wp-caption-text">SQL Commands to add NMaths UDAs to the Database</p></div>
<p>  At this point we are ready to test our library.  We have chosen a problem that represents the work expected to be perform.  We will create our sample datapoint tables rather than use tables from the AdventureWorks database. We will then load the data from the tables into DoubleVectors and call our library using a User Defined Function(UDF). Our UDF will be a call to library to find the function values for a curve that fits our datapoints.  From our function solution we will create a table with datapoints that represent our curve function to compare against our original datapoints.</p>
<p>  We will start by creating the UDF call to our library. From the Solution Explorer, we will add a UDF template called NMathFunction.cs.</p>
<p>  Here is the C# code for this function call:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Analysis</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> UserDefinedFunctions
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>Microsoft.<span style="color: #0000FF;">SqlServer</span>.<span style="color: #0000FF;">Server</span>.<span style="color: #0000FF;">SqlFunction</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> DoubleVector NMathFunction<span style="color: #000000;">&#40;</span>DoubleVector xValues, DoubleVector yValues, DoubleVector start<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        NMathFunctions.<span style="color: #0000FF;">GeneralizedDoubleUnaryFunction</span> f <span style="color: #008000;">=</span> AnalysisFunctions.<span style="color: #0000FF;">FourParameterLogistic</span><span style="color: #008000;">;</span>
        OneVariableFunctionFitter fitter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> OneVariableFunctionFitter<span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>fitter.<span style="color: #0000FF;">Fit</span><span style="color: #000000;">&#40;</span>xValues, yValues, start<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>  We can now tell VS to build the assembly NmathSQL.dll and use SQL to load the assembly to our database.</p>
<div id="attachment_3423" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_10.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_10.png" alt="" title="Loading our custom NMathFunctions to the database" width="500" class="size-full wp-image-3423" /></a><p class="wp-caption-text">Loading our custom NMathFunctions to the database</p></div>
<p>  In order for our UDF NmathFunction to be recognized we need to run a SQL create function command. Note that this command must be the only command in the script.  </p>
<div id="attachment_3424" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_11.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_11.png" alt="" title="SQL Commands to Create a UDF function" width="500" class="size-full wp-image-3424" /></a><p class="wp-caption-text">SQL Commands to add our UDF to the database</p></div>
<p>  We have now loaded our NMath function call in the database that can be called by scripts. We are ready to write a script to solve the problem we described above.</p>
<p>  We will start by using the same data from the example we did with our excel post.</p>
<div id="attachment_3425" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_12.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_12.png" alt="" title="Sample Data for our example" width="500" class="size-full wp-image-3425" /></a><p class="wp-caption-text">Sample Data for our example</p></div>
<p>  We can now build a script that uses the functionality we have built in with our library to find the solution.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">-------------------------------------------------------</span>
Declare @xv DoubleVector
Declare @yv DoubleVector
&nbsp;
select @xv <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">LoadDoubleVector</span><span style="color: #000000;">&#40;</span>xcol<span style="color: #000000;">&#41;</span> from xvalues
select @yv <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">LoadDoubleVector</span><span style="color: #000000;">&#40;</span>ycol<span style="color: #000000;">&#41;</span> from yvalues
&nbsp;
Declare @sPar DoubleVector
Declare @solution DoubleVector
&nbsp;
set @sPar <span style="color: #008000;">=</span> <span style="color: #666666;">'[ 0.1 0.1 0.1 0.1]'</span>
&nbsp;
select @solution <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">NMathFunction</span><span style="color: #000000;">&#40;</span>@xv, @yv, @sPar<span style="color: #000000;">&#41;</span>
&nbsp;
select @solution.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
declare @newxval <span style="color: #FF0000;">float</span>
declare @newyval <span style="color: #FF0000;">float</span>
declare @inc <span style="color: #FF0000;">float</span>
declare @a <span style="color: #FF0000;">float</span>
declare @b <span style="color: #FF0000;">float</span>
declare @c <span style="color: #FF0000;">float</span>
declare @d <span style="color: #FF0000;">float</span>
&nbsp;
set @a <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">DVItem</span><span style="color: #000000;">&#40;</span>@solution, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
set @b <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">DVItem</span><span style="color: #000000;">&#40;</span>@solution, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
set @c <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">DVItem</span><span style="color: #000000;">&#40;</span>@solution, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span>
set @d <span style="color: #008000;">=</span> dbo.<span style="color: #0000FF;">DVItem</span><span style="color: #000000;">&#40;</span>@solution, <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span>
&nbsp;
create table SolutionTBL <span style="color: #000000;">&#40;</span>newxval <span style="color: #FF0000;">float</span>, newyval <span style="color: #FF0000;">float</span><span style="color: #000000;">&#41;</span>
	set @newxval <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
&nbsp;
	<span style="color: #0600FF;">while</span> @newxval <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">35.10</span>
	begin
&nbsp;
	    set @newyval <span style="color: #008000;">=</span> @d<span style="color: #008000;">+</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>@a<span style="color: #008000;">-</span>@d<span style="color: #000000;">&#41;</span><span style="color: #008000;">/</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">+</span>POWER<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>@newxval<span style="color: #008000;">/</span>@c<span style="color: #000000;">&#41;</span>, @b<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	    insert into SolutionTBL values<span style="color: #000000;">&#40;</span>@newxval, @newyval<span style="color: #000000;">&#41;</span>
&nbsp;
	  set @newxval <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>@newxval <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.33</span><span style="color: #000000;">&#41;</span>
	end
	select <span style="color: #008000;">*</span> from SolutionTBL
	go
<span style="color: #008000;">---------------------------------------------------------------------</span></pre></div></div>

<p>  Here is the output from this script</p>
<div id="attachment_3427" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_13.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_13.png" alt="" title="Returned Solution from NMath library call" width="500" class="size-full wp-image-3427" /></a><p class="wp-caption-text">Returned Solution from NMath library call</p></div>
<p>  At this point we can pull data from tables, call our Math libraries, and put the solution in a table to be displayed. In production, this last script would be a stored procedure that would be run from the Management Studio. All of this work would reside on the SQL Server.  </p>
<p>We can now move on to the Reporting Services to see how this solution could be displayed.</p>
<p>  After launching Report Builder and establishing connection to the database. The data is accessed by setting up a dataset with a query into the table for the necessary data.</p>
<div id="attachment_3430" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_13a.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_13a.png" alt="" title="Selecting the data in Report Builder" width="500" class="size-full wp-image-3430" /></a><p class="wp-caption-text">Selecting the chart data in Report Builder</p></div>
<p>  From there it is merely Report Builder to build the necessary graphs.  Unfortunately, the chart wizard doesn&#8217;t include scatterplots. It is best to select a chart type of Line and then run through the wizard.  After your chart is set up you can change its properties to scatterplot.</p>
<div id="attachment_3431" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_14.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_14.png" alt="" title="Report Builder ScatterPlot" width="500" class="size-full wp-image-3431" /></a><p class="wp-caption-text">Report Builder ScatterPlot</p></div>
<p>  We can include our solution in the same report and generate a smooth line as we did in excel. The Report Builder is a very powerful tool which we are still learning.</p>
<div id="attachment_3432" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_15.png"><img src="http://www.centerspace.net/blog/wp-content/uploads/2011/04/NMathSQL_15.png" alt="" title="Solution Fitted Line" width="500" class="size-full wp-image-3432" /></a><p class="wp-caption-text">Solution Fitted Line</p></div>
<p>  Our results are equivilent to our previous excel post and demonstrate that the approach on the SQL Server is as accurate.</p>
<p>In conclusion, we have demonstrated that we can call the CenterSpace NMath libraries from SQL Server and display the results using Report Services powerful charting capabilities.  We plan to work on more examples and compare how we might improve on what Analysis Services can produce.  We certainly will entertain feedback on useful approaches that should be examined. As a reminder this interface is not possible with our current release of NMath 5.0 and require a &#8220;special&#8221; version of our assembly.  Depending on customer interest and feedback we may or may not decide to develop a product for this interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/accessing-net-libraries-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced Curve Fitting using Excel and NMath</title>
		<link>http://www.centerspace.net/blog/nmath/nmath-tutorial/advanced-curve-fitting-using-excel-and-nmath/</link>
		<comments>http://www.centerspace.net/blog/nmath/nmath-tutorial/advanced-curve-fitting-using-excel-and-nmath/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 22:10:15 +0000</pubDate>
		<dc:creator>Mike Magee</dc:creator>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[NMath Tutorial]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3145</guid>
		<description><![CDATA[In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work. Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points. This example combines the ease of Excel's charting capabilities with CenterSpace's powerful NMath library.]]></description>
			<content:encoded><![CDATA[<p>In recent blog posts, we have discussed how to call CenterSpace&#8217;s Libraries from Excel.  In his March 2010 blog post, CenterSpace&#8217;s Ken Baldwin demonstrated how to replicate <a href="http://www.centerspace.net">Excel&#8217;s existing Trendline functions using C# and NMath</a>.  In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work.</p>
<p>Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points.  For example, if we collect a series of changing data values over time, we look to explain the relationship that time effects the generated values or in mathematical terms y=f(x).  Here we are using x to represent time values and f(x) to represent the relationship or function that generates the resulting values or y.  So if we can find a function f(x) that represents a good fit of the data we should be able to predict the result at any moment in time.</p>
<p>With this in mind, let us get started with some data points (x, y) that we have collected.  I have entered the following values in an Excel spreadsheet.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif"><img class="size-full wp-image-3146" title="Curve Fitting Sample data" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif" alt="" /></a></p>
<p>We can now use Excel&#8217;s charting function to create the following XY scatterplot with our data:</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif"><img class="size-full wp-image-3147" title="Excel XY scatterplot of sample data" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif" alt="" /></a></p>
<p>At this point, we can add an Excel trendline to get the best fit it can provide.  By right clicking on a data value in our chart we will get the option to add a trendline. Choose a 2nd order polynomial and these options.</p>
<p>Name the trendline &#8220;2nd Order Polynomial&#8221; and check &#8220;Display equation on chart&#8221; and &#8220;Display R-squared value on chart&#8221;.</p>
<p>Excel calculates and plots the line while returning the equation and the R2 value.  If our R2 equals 1 we would have found a perfect fit.  For a second order polynomial Excel returned a value of 0.8944 which means that roughly 10.6 percent (1-.894) are not on this line.</p>
<p>If we continue increasing our polynomial orders up to the maximum of six we can achieve the best R2 value of 0.9792, but look at the curve we have fitted to these points.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif"><img class="size-full wp-image-3149" title="Excel Trendline using a polynomial function" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif" alt="" /></a></p>
<p>A better fit might be an exponential function so let us try Excel&#8217;s trendline option using exponentials.<br />
<a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif"><img class="alignnone size-full wp-image-3150" title="Excel exponential trendline fit of sample data" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif" alt="" /></a></p>
<p>Clearly the results are visually a better fit but the R2 value tells us that over 15% of the data points are not on this line.  This pretty much represents the best we can do with Excel&#8217;s trendline functions for our data.</p>
<p>Looking to CenterSpace&#8217;s NMath and NStat libraries to give us more robust analysis, we can utilize more powerful curve fitting tools quickly and with little effort.</p>
<p>Using the linkage provided by ExcelDNA that we examined in our previous posts, we can create the following C# code for our ExcelDNA text file.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;!</span><span style="color: #000000;">&#91;</span>CDATA<span style="color: #000000;">&#91;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">ExcelDna.Integration</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Core</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Matrix</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Analysis</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">CenterSpace.NMath.Stats</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NMathExcelCurveFit
<span style="color: #000000;">&#123;</span>
&nbsp;
 <span style="color: #000000;">&#91;</span>ExcelFunction<span style="color: #000000;">&#40;</span>Description<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Four parameterized Fit&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> NOneVarFunctFitFour<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> xValues, <span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> yValues, <span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> start<span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
  DoubleVector TempVx <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>xValues<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector TempVy <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>yValues<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector TempVs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>start<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  OneVariableFunctionFitter
<span style="color: #008000;">&lt;</span>TrustRegionMinimizer<span style="color: #008000;">&gt;</span> fitter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span>  OneVariableFunctionFitter
<span style="color: #008000;">&lt;</span>TrustRegionMinimizer<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span> AnalysisFunctions.<span style="color: #0000FF;">FourParameterLogistic</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector solution <span style="color: #008000;">=</span> fitter.<span style="color: #0000FF;">Fit</span><span style="color: #000000;">&#40;</span>TempVx, TempVy, TempVs<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF;">return</span> solution.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #000000;">&#91;</span>ExcelFunction<span style="color: #000000;">&#40;</span>Description<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Four parameterized R2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> NOneVarFunctFitFourR2<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> xValues, <span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> yValues, <span style="color: #FF0000;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> start<span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
  DoubleVector TempVx <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>xValues<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector TempVy <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>yValues<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector TempVs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DoubleVector<span style="color: #000000;">&#40;</span>start<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  OneVariableFunctionFitter
<span style="color: #008000;">&lt;</span>TrustRegionMinimizer<span style="color: #008000;">&gt;</span> fitter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span>  OneVariableFunctionFitter
<span style="color: #008000;">&lt;</span>TrustRegionMinimizer<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span> AnalysisFunctions.<span style="color: #0000FF;">FourParameterLogistic</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  DoubleVector solution <span style="color: #008000;">=</span> fitter.<span style="color: #0000FF;">Fit</span><span style="color: #000000;">&#40;</span>TempVx, TempVy, TempVs<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  GoodnessOfFit gof <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GoodnessOfFit<span style="color: #000000;">&#40;</span>fitter, TempVx, TempVy, solution<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF;">return</span> gof.<span style="color: #0000FF;">RSquared</span><span style="color: #008000;">;</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span></pre></div></div>

<p>As you can see by the code, I have chosen to use NMath&#8217;s OneVariableFunctionFitter with the FourParameterLogistic predefined generalized function.</p>
<pre class="code">
<img title="{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" src="http://latex.codecogs.com/gif.latex?{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" alt="" /></pre>
<p>From CenterSpace&#8217;s documentation, we get the above equation and need to solve for the model parameters <code> a, b, c, d </code>.</p>
<p>The OneVariableFunction call will require us to provide a starting guess along with the ranges containing the xValues and yValues.  The following screen shows us making the function call from Excel with the necessary parameters.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif"><img class="size-full wp-image-3156" title="CurveFit6" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif" alt="" /></a></p>
<p>After computing these values, we can call for the R2 value and generate some points to be plotted.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif"><img class="size-full wp-image-3157" title="Obtaining the R2 value from a NMath OneVariableFunction call" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif" alt="" /></a></p>
<p>Note that I choose to hide the rfows from r17 to r107 for display purposes. You will need to have them unhid for the chart to look right.  As you can see we returned the best R2 value so far at 0.9923.</p>
<p>In the next screen, we will have added the series to our chart and drawn a line through our calculated points.</p>
<p><a href="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif"><img class="size-full wp-image-3158" title="OneVariableFunction using FourParameterLogistic function plot" src="http://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif" alt="" /></a></p>
<p>This example illustrates the ease that Excel can use the power of NMath curve fitting routines to compute accurate fits to a collection of data.</p>
<p>Mike Magee</p>
]]></content:encoded>
			<wfw:commentRss>http://www.centerspace.net/blog/nmath/nmath-tutorial/advanced-curve-fitting-using-excel-and-nmath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

