<?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</title>
	<atom:link href="http://www.centerspace.net/blog/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>NMath integration with Essential Chart</title>
		<link>http://www.centerspace.net/blog/visualization/nmath-integration-with-essential-chart/</link>
		<comments>http://www.centerspace.net/blog/visualization/nmath-integration-with-essential-chart/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 18:33:49 +0000</pubDate>
		<dc:creator>Andy Gray</dc:creator>
				<category><![CDATA[Syncfusion]]></category>
		<category><![CDATA[Visualization]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/visualization/nmath-integration-with-essential-chart/</guid>
		<description><![CDATA[When building numerical applications, you&#8217;ll inevitably be faced with the challenge of taking raw data and computed results and presenting information in a way that makes business sense to the end user. NMath 5.1 added some new visualization features to help. We&#8217;ve also worked closely with the team at Syncfusion to make it very easy...]]></description>
			<content:encoded><![CDATA[<p>When building numerical applications, you&#8217;ll inevitably be faced with the challenge of taking raw data and computed results and presenting information in a way that makes business sense to the end user. <a href="http://www.centerspace.net/blog/nmath/new-versions-of-nmath-libraries-released-3/" target="_blank">NMath 5.1 added some new visualization features</a> to help. We&#8217;ve also worked closely with the team at <a href="http://www.syncfusion.com" target="_blank">Syncfusion</a> to make it very easy to create visualizations of NMath data with their powerful <a href="http://www.syncfusion.com/products/user-interface-edition/windows-forms/chart" target="_blank">Essential Chart</a> component.</p>
<p>We&#8217;ve released some <a href="http://www.syncfusion.com/downloads/resources/whitepaper/centerspace1111" target="_blank">free adapter code</a> that will allow you to create an Essential Chart control from NMath data structures with as little as one line of code. We&#8217;ve also written some technical documentation that walk you through many common graphing scenarios using the Syncfusion components. These papers detail how to:</p>
<ul>
<li>Download and reference the CenterSpace.NMath.Charting.Syncfusion.NMathChart and CenterSpace.NMath.Charting.Syncfusion.NMathStatsChart adapters.</li>
<li>Create and configure Essential Chart controls, and bind charts to NMath data structures.</li>
<li>Use the NMathChart and NMathStatsChart adapters to create and customize Essential Chart ChartControl instances easily.</li>
<li>Plot vectors, matrices, functions, fitted functions, function peaks, least squares classes, and histograms using a variety of chart types.</li>
<li>Plot NMath Stats data structures such as data frames, probability distributions, linear regressions, goodness of fit, principal component analysis, and cluster analyses.</li>
</ul>
<p><a href="http://www.syncfusion.com/downloads/resources/whitepaper/centerspace1111" target="_blank">Click here</a> to download the free adapter code and detailed instructions. Questions or comments? Please let us know in the <a href="http://centerspace.net/forums/" target="_blank">forums</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%2Fvisualization%2Fnmath-integration-with-essential-chart%2F&amp;title=NMath%20integration%20with%20Essential%20Chart"><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/visualization/nmath-integration-with-essential-chart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speaking at SC11</title>
		<link>http://www.centerspace.net/blog/uncategorized/speaking-at-sc11/</link>
		<comments>http://www.centerspace.net/blog/uncategorized/speaking-at-sc11/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 20:26:17 +0000</pubDate>
		<dc:creator>Andy Gray</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.centerspace.net/blog/uncategorized/speaking-at-sc11/</guid>
		<description><![CDATA[CenterSpace is attending the SC11 conference this week in Seattle. On Tuesday, November 15 at 3:45 pm in the Intel booth, I will be presenting, “Numerical Programming in Managed Code: This Might Just Work.”&#160; Please come join us! &#160; Whether or not you make it to the presentation, please drop us a line if you’re...]]></description>
			<content:encoded><![CDATA[<p>CenterSpace is attending the <a href="http://sc11.supercomputing.org/" target="_blank">SC11 conference</a> this week in Seattle. On Tuesday, November 15 at 3:45 pm in the <a href="http://www.intel.com" target="_blank">Intel</a> booth, I will be presenting, “Numerical Programming in Managed Code: This Might Just Work.”&#160; Please come join us!</p>
<p>&#160;</p>
<p>Whether or not you make it to the presentation, please <a href="mailto:info@centerspace.net" target="_blank">drop us a line</a> if you’re attending SC11 – it’s always great to catch up with our users in person!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.centerspace.net%2Fblog%2Funcategorized%2Fspeaking-at-sc11%2F&amp;title=Speaking%20at%20SC11"><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/uncategorized/speaking-at-sc11/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>
	</channel>
</rss>

