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

<channel>
	<title>NMath Tutorial Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/category/nmath/nmath-tutorial/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/category/nmath/nmath-tutorial</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Thu, 17 Oct 2019 01:13:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Filtering with Wavelet Transforms</title>
		<link>https://www.centerspace.net/wavelet-transforms</link>
					<comments>https://www.centerspace.net/wavelet-transforms#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Fri, 18 Dec 2015 17:00:25 +0000</pubDate>
				<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[DWT]]></category>
		<category><![CDATA[ECG filtering]]></category>
		<category><![CDATA[filtering]]></category>
		<category><![CDATA[filtering with wavelets]]></category>
		<category><![CDATA[mass spec filtering]]></category>
		<category><![CDATA[wavelet filtering]]></category>
		<category><![CDATA[wavelets]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=5713</guid>

					<description><![CDATA[<p><img class="excerpt" src="https://www.centerspace.net/wp-content/uploads/2015/10/ec13.jpg" alt="ECG waveform" width="350" class="size-full wp-image-5819" /><br />
Wavelet transforms have found engineering applications in computer vision, pattern recognition, signal filtering and perhaps most widely in signal and image compression.  In 2000 the ISO JPEG committee proposed a new<br />
JPEG2000 image compression standard that is based on the wavelet transform using two Daubechies wavelets.  This standard made the relatively new image decomposition algorithm ubiquitous on desktop around the world. </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/wavelet-transforms">Filtering with Wavelet Transforms</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Discrete time wavelet transforms have found engineering applications in computer vision, pattern recognition, signal filtering and perhaps most widely in signal and image compression.  In 2000 the ISO JPEG committee proposed a new JPEG2000 image compression standard that is based on the wavelet transform using two Daubechies wavelets.  This standard made the relatively new image decomposition algorithm ubiquitous on desktops around the world.  </p>
<p>In signal processing, wavelets have been widely investigated for use in filtering bio-electric signals, among many other applications.  Bio-electric signals are good candidates for multi-resolution wavelet filtering over standard Fourier analysis due to their non-stationary character.  In this article we&#8217;ll discuss the filtering of electrocardiograms or ECGs and demonstrate with code examples how to filter an ECG waveform using <strong>NMath</strong>&#8216;s new wavelet classes; keeping in mind that the techniques and code shown here apply to a wide class of time series measurements.  If wavelets and their applications to filtering are unfamiliar to the reader, read a gentle and brief introduction to the subject in <em>Wavelets for Kids: A Tutorial Introduction</em> <a href="#5">[5]</a>.</p>
<h2> Filtering a Time Series with Wavelets</h2>
<p><a href="http://www.physionet.org/">PhysioNet </a>provides free access to a large collections of recorded physiologic signals, including many ECG&#8217;s.  The ECG signal we will filter here, named <em>aami-ec13</em> on PhysioNet, is shown below.<br />
<figure id="attachment_5721" aria-describedby="caption-attachment-5721" style="width: 600px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2015/03/ScreenClip.png"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/03/ScreenClip.png" alt="ECG Signal" width="600" class="size-full wp-image-5721" srcset="https://www.centerspace.net/wp-content/uploads/2015/03/ScreenClip.png 1090w, https://www.centerspace.net/wp-content/uploads/2015/03/ScreenClip-300x59.png 300w, https://www.centerspace.net/wp-content/uploads/2015/03/ScreenClip-1024x202.png 1024w" sizes="(max-width: 1090px) 100vw, 1090px" /></a><figcaption id="caption-attachment-5721" class="wp-caption-text">ECG Signal</figcaption></figure></p>
<p>Our goal will be to remove the high frequency noise while preserving the character of the wave form, including the high frequency transitions at the signal peaks.  Fourier based filter methods are ill suited for filtering this type of signal due to both it&#8217;s non-stationarity, as mentioned, but also the need to preserve the peak locations (phase) and shape.</p>
<h3> A Wavelet Filter </h3>
<p>As with Fourier analysis there are three basic steps to filtering signals using wavelets.</p>
<ol>
<li> <em>Decompose </em>the signal using the DWT.
<li> Filter the signal in the wavelet space using <em>thresholding</em>.
<li> Invert the filtered signal to <em>reconstruct </em>the original, now filtered signal, using the inverse DWT.
</ol>
<p>Briefly, the filtering of signals using wavelets is based on the idea that as the DWT decomposes the signal into <em>details </em>and <em>approximation </em> parts, at some scale the details contain mostly the insignificant noise and can be removed or zeroed out using thresholding without affecting the signal.  This idea is discussed in more detail in the introductory paper [5].  To implement this DWT filtering scheme there are two basic filter design parameters: the wavelet type and a threshold.  Typically the shape and form of the signal to be filtered is qualitatively matched to the general shape of the wavelet.  In this example we will use the Daubechies forth order wavelet.   </p>
<table>
<tr>
<td><figure id="attachment_5827" aria-describedby="caption-attachment-5827" style="width: 220px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/Image.png" alt="ECG Signal closeup" width="200" class="alignnone size-full wp-image-5852" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/Image.png 706w, https://www.centerspace.net/wp-content/uploads/2015/10/Image-300x226.png 300w" sizes="(max-width: 706px) 100vw, 706px" /><figcaption id="caption-attachment-5827" class="wp-caption-text">ECG Waveform</figcaption></figure></td>
<td><figure id="attachment_5827" aria-describedby="caption-attachment-5827" style="width: 220px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/DB4.png" alt="Daubechies 4 wavelet" width="200" class="size-full wp-image-5827" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/DB4.png 560w, https://www.centerspace.net/wp-content/uploads/2015/10/DB4-300x214.png 300w" sizes="(max-width: 560px) 100vw, 560px" /><figcaption id="caption-attachment-5827" class="wp-caption-text">Daubechies 4 wavelet</figcaption></figure></td>
</tr>
</table>
<p>The general shape of this wavelet roughly matches, at various scales, the morphology of the ECG signal.  Currently <strong>NMath </strong>supports the following wavelet families: Harr, Daubechies, Symlet, Best Localized, and Coiflet, 27 in all.  Additionally, any custom wavelet of your invention can be created by passing in the wavelet&#8217;s low &#038; high pass decimation filter values.  The wavelet class then imposes the wavelet&#8217;s symmetry properties to compute the reconstruction filters.</p>
<pre lang="csharp">
   // Build a Coiflet wavelet.
   var wavelet = new FloatWavelet( Wavelet.Wavelets.C4 );

   // Build a custom reverse bi-orthogonal wavelet.
   var wavelet = new DoubleWavelet( new double[] {0.0, 0.0, 0.7071068, 0.7071068, 0.0, 0.0}, new double[] {0.0883883, 0.0883883, -0.7071068, 0.7071068, -0.0883883, -0.0883883} );
</pre>
<p>The <code>FloatDWT</code> class provides four different thresholding strategies: Universal, UniversalMAD, Sure, and Hybrid (a.k.a SureShrink).  We&#8217;ll use the Universal threshold strategy here.  This is a good starting point but this strategy can over smooth the signal.  Typically some empirical experimentation is done here to find the best threshold for the data (see [1], also see [4] for a good overview of common thresholding strategies.)</p>
<h3> Wavelet Filtering Code</h3>
<p>The three steps outlined above are easily coded using two classes in the <b>NMath</b> library: the <code>FloatDWT</code> class and the <code>FloatWavelet</code> class.  As always in <b>NMath</b>, the library offers both a float precision and a double precision version of each of these classes.  Let&#8217;s look at a code snippet that implements a DWT based filter with <b>NMath</b>.</p>
<pre lang="csharp">
   // Choose wavelet, the Daubechies 4 wavelet
   var wavelet = new FloatWavelet( Wavelet.Wavelets.D4 );

   // Build DWT object using our wavelet & data
   var dwt = new FloatDWT( data, wavelet );

   // Decompose signal with DWT to level 5
   dwt.Decompose( 5 );

   // Find Universal threshold & threshold all detail levels
   double lambdaU = dwt.ComputeThreshold( FloatDWT.ThresholdMethod.Universal, 1 );
   dwt.ThresholdAllLevels( FloatDWT.ThresholdPolicy.Soft, new double[] { lambdaU, 
       lambdaU, lambdaU, lambdaU, lambdaU } );

   // Rebuild the filtered signal.
   float[] reconstructedData = dwt.Reconstruct();
</pre>
<p>The first two lines of code build the wavelet object and the DWT object using both the input data signal and the abbreviated Daubechies wavelet name <code>Wavelet.Wavelets.D4</code>.  The third line of code executes the wavelet decomposition at five consecutive scales.  Both the signal&#8217;s <em>details</em> and <em>approximations</em> are stored in the DWT object at each step in the decomposition.  Next, the <code>Universal</code> threshold is computed and the wavelet details are thresholded using the same threshold with a <code>Soft</code> policy (see [1], pg. 63).  Lastly, the now filtered signal is reconstructed.</p>
<p>Below, the chart on the left shows the unfiltered ECG signal and the chart on the right shows the wavelet filtered ECG signal.  It&#8217;s clear that this filter very effectively removed the noise while preserving the signal.</p>
<table>
<tr>
<td>
<figure id="attachment_5888" aria-describedby="caption-attachment-5888" style="width: 350px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/Image2.png" alt="Raw ECG Signal" width="350" class="size-full wp-image-5888" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/Image2.png 506w, https://www.centerspace.net/wp-content/uploads/2015/10/Image2-300x180.png 300w" sizes="(max-width: 506px) 100vw, 506px" /><figcaption id="caption-attachment-5888" class="wp-caption-text">Raw ECG Signal</figcaption></figure>
</td>
<td>
<figure id="attachment_5887" aria-describedby="caption-attachment-5887" style="width: 350px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/Image1.png" alt="Filtered ECG Signal" width="350"  class="size-full wp-image-5887" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/Image1.png 506w, https://www.centerspace.net/wp-content/uploads/2015/10/Image1-300x180.png 300w" sizes="(max-width: 506px) 100vw, 506px" /><figcaption id="caption-attachment-5887" class="wp-caption-text">Filtered ECG Signal</figcaption></figure></td>
</table>
<p>These two charts below show a detail from the chart above from indices 500 to 1000.  Note how well the signal shape, phase, and amplitude has been preserved in this non-stationary wavelet-filtered signal.</p>
<table>
<tr>
<td>
<figure id="attachment_5890" aria-describedby="caption-attachment-5890" style="width: 350px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/Image4.png" alt="Detail Raw ECG Signal" width="350"  class="size-full wp-image-5890" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/Image4.png 506w, https://www.centerspace.net/wp-content/uploads/2015/10/Image4-300x180.png 300w" sizes="(max-width: 506px) 100vw, 506px" /><figcaption id="caption-attachment-5890" class="wp-caption-text">Detail Raw ECG Signal</figcaption></figure>
</td>
<td>
<figure id="attachment_5889" aria-describedby="caption-attachment-5889" style="width: 350px" class="wp-caption alignnone"><img decoding="async" src="https://www.centerspace.net/blog/wp-content/uploads/2015/10/Image3.png" alt="Detail Filtered ECG Signal" width="350"  class="size-full wp-image-5889" srcset="https://www.centerspace.net/wp-content/uploads/2015/10/Image3.png 506w, https://www.centerspace.net/wp-content/uploads/2015/10/Image3-300x180.png 300w" sizes="(max-width: 506px) 100vw, 506px" /><figcaption id="caption-attachment-5889" class="wp-caption-text">Detail Filtered ECG Signal</figcaption></figure>
</td>
</table>
<p>It is this ability to preserve phase, form, and amplitude in DWT based filters all while having a O(n log n) runtime that Fourier-based filters enjoy that has made wavelets such an important part of signal processing today.  The complete code for this example along with a link to the ECG data is provided below.</p>
<p>Paul</p>
<h3> References </h3>
<div id="1">[1] Guomin Luo and Daming Zhang (2012). <em>Wavelet Denoising</em>, Advances in Wavelet Theory and Their Applications in Engineering, Physics and Technology, Dr. Dumitru Baleanu (Ed.), ISBN: 978-953-51-0494-0, InTech, pp. 59-80.  Available from: <a href="http://www.intechopen.com/books/advances-in-wavelet-theory-and-their-applicationsin-engineering-physics-and-technology/wavelet-denoising">http://www.intechopen.com/books/advances-in-wavelet-theory-and-their-applicationsin-engineering-physics-and-technology/wavelet-denoising</a> </div>
<div id="2">[2] Burhan Ergen (2012). <em>Signal and Image Denoising Using Wavelet Transform</em>, Advances in Wavelet Theory and Their Applications in Engineering, Physics and Technology, Dr. Dumitru Baleanu (Ed.), ISBN: 978-953-51-0494-0, InTech, DOI: 10.5772/36434.  Available from: <a href="http://www.intechopen.com/books/advances-in-wavelet-theory-and-their-applications-in-engineering-physics-and-technology/wavelet-signal-and-image-denoising">http://www.intechopen.com/books/advances-in-wavelet-theory-and-their-applications-in-engineering-physics-and-technology/wavelet-signal-and-image-denoising</a> </div>
<div id="3">[3] Rami Cohen: <em>Signal Denoising Using Wavelets</em>, Project Report, 2012.  Available from: <a href="https://pdfs.semanticscholar.org/3dfd/6b2bd3d6ad3c6eca50747e686d5ad88b4fc1.pdf">https://pdfs.semanticscholar.org/3dfd/6b2bd3d6ad3c6eca50747e686d5ad88b4fc1.pdf</a> </div>
<div id="4">[4] M. C. E. Rosas-Orea, M. Hernandez-Diaz, V. Alarcon-Aquino, and L. G. Guerrero-Ojeda, <em>A Comparative Simulation Study of Wavelet Based Denoising Algorithms</em>, Proceedings of the 15th International Conference on Electronics, Communications and Computers (CONIELECOMP 2005), 2005 © IEEE </div>
<div id="5">[5] Brani Vidakovic and Peter Mueller, <em>Wavelets for Kids: A Tutorial Introduction</em>, Duke University, 1991.  Available from: <a target="_blank" href="http://gtwavelet.bme.gatech.edu/wp/kidsA.pdf">http://gtwavelet.bme.gatech.edu/wp/kidsA.pdf</a> </div>
<h3> Test Data </h3>
<p>To copy the data file provided by <a href="http://www.physionet.org/">PhysioNet</a> for this example click: <a href="https://www.centerspace.net/blog/wp-content/uploads/2015/10/ECG_AAMIEC13.data_.txt">ECG_AAMIEC13.data</a><br />
This ECG data was taken from the ANSI EC13 test data set <a href="http://www.physionet.org/physiobank/database/aami-ec13/">waveforms</a>.</p>
<h3> Complete Test Code </h3>
<pre lang="csharp">

    public void BlogECGExample()
    {
      // Define your own dataDir
      var dataDir = "................";

      // Load ECG wave from physionet.org data file.
      string filename = Path.Combine( dataDir, "ECG_AAMIEC13.data.txt" );
      string line;
      int cnt = 0;
      FloatVector ecgMeasurement = new FloatVector( 3000 );
      var fileStrm = new System.IO.StreamReader( filename );
      fileStrm.ReadLine(); fileStrm.ReadLine();
      while ( ( line = fileStrm.ReadLine() ) != null && cnt < 3000 )
      {
        ecgMeasurement[cnt] = Single.Parse( line.Split( ',' )[1] );
        cnt++;
      }

      // Choose wavelet
      var wavelet = new FloatWavelet( Wavelet.Wavelets.D4 );

      // Build DWT object
      var dwt = new FloatDWT( ecgMeasurement.DataBlock.Data, wavelet );

      // Decompose signal with DWT to level 5
      dwt.Decompose( 5 );

      // Find Universal threshold &#038; threshold all detail levels with lambdaU
      double lambdaU = dwt.ComputeThreshold( FloatDWT.ThresholdMethod.Universal, 1 );
      dwt.ThresholdAllLevels( FloatDWT.ThresholdPolicy.Soft, new double[] { lambdaU, lambdaU, lambdaU, lambdaU, lambdaU } );

      // Rebuild the signal to level 1 - the original (filtered) signal.
      float[] reconstructedData = dwt.Reconstruct();

      // Display DWT results.
      BlogECGExampleBuildCharts( dwt, ecgMeasurement, reconstructedData );

    }

    public void BlogECGExampleBuildCharts( FloatDWT dwt, FloatVector ECGMeasurement, float[] ReconstructedData )
    {

      // Plot out approximations at various levels of decomposition.
      var approxAllLevels = new FloatVector();
      for ( int n = 5; n > 0; n-- )
      {
        var approx = new FloatVector( dwt.WaveletCoefficients( DiscreteWaveletTransform.WaveletCoefficientType.Approximation, n ) );
        approxAllLevels.Append( new FloatVector( approx ) );
      }

      var detailsAllLevels = new FloatVector();
      for ( int n = 5; n > 0; n-- )
      {
        var approx = new FloatVector( dwt.WaveletCoefficients( DiscreteWaveletTransform.WaveletCoefficientType.Details, n ) );
        detailsAllLevels.Append( new FloatVector( approx ) );
      }

      // Create and display charts.
      Chart chart0 = NMathChart.ToChart( detailsAllLevels );
      chart0.Titles.Add( "Concatenated DWT Details to Level 5" );
      chart0.ChartAreas[0].AxisY.Title = "DWT Details";
      chart0.Height = 270;
      NMathChart.Show( chart0 );

      Chart chart1 = NMathChart.ToChart( approxAllLevels );
      chart1.Titles.Add("Concatenated DWT Approximations to Level 5");
      chart1.ChartAreas[0].AxisY.Title = "DWT Approximations";
      chart1.Height = 270;
      NMathChart.Show( chart1 );

      Chart chart2 = NMathChart.ToChart( (new FloatVector( ReconstructedData ))[new Slice(500,500)] );
      chart2.Titles[0].Text = "Thresholded & Reconstructed ECG Signal";
      chart2.ChartAreas[0].AxisY.Title = "mV";
      chart2.Height= 270;
      NMathChart.Show( chart2 );

      Chart chart3 = NMathChart.ToChart( (new FloatVector( ECGMeasurement ))[new Slice(500,500)] );
      chart3.Titles[0].Text = "Raw ECG Signal";
      chart3.ChartAreas[0].AxisY.Title = "mV";
      chart3.Height = 270;
      NMathChart.Show( chart3 );

    }
</pre>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/wavelet-transforms">Filtering with Wavelet Transforms</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/wavelet-transforms/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5713</post-id>	</item>
		<item>
		<title>NMath Tutorial videos</title>
		<link>https://www.centerspace.net/nmath-tutorial-videos</link>
					<comments>https://www.centerspace.net/nmath-tutorial-videos#comments</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Fri, 01 Nov 2013 00:21:16 +0000</pubDate>
				<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[.NET math library]]></category>
		<category><![CDATA[.NET statistics library]]></category>
		<category><![CDATA[C# math library]]></category>
		<category><![CDATA[C# statistics library]]></category>
		<category><![CDATA[VB math library]]></category>
		<category><![CDATA[VB statistics library]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=5044</guid>

					<description><![CDATA[<p>We are proud to announce a series of tutorial videos on how to use CenterSpace's NMath products. We are starting, naturally, with <a href="https://www.youtube.com/watch?v=0DczJpmyE10"><strong>Getting Started with NMath</strong></a>.</p>
<p>You can download it here: <a href="https://s3.amazonaws.com/centerspace-videos/tutorials/Intro_Captions.mp4">MP4</a></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-tutorial-videos">NMath Tutorial videos</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We are proud to announce a series of NMath tutorial videos on how to use CenterSpace&#8217;s math library products. We are starting, naturally, with <strong>Getting Started with NMath</strong>.</p>
<p><iframe loading="lazy" class="youtube-player" width="640" height="360" src="https://www.youtube.com/embed/0DczJpmyE10?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent" allowfullscreen="true" style="border:0;" sandbox="allow-scripts allow-same-origin allow-popups allow-presentation"></iframe></p>
<p>You can download it here: <a href="https://s3.amazonaws.com/centerspace-videos/tutorials/Intro_Captions.mp4" class="broken_link">MP4</a></p>
<p>Please let us know which topics you want us to cover. Email support@centerspace.net</p>
<p>Cheers,<br />
Trevor </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-tutorial-videos">NMath Tutorial videos</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/nmath-tutorial-videos/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		<enclosure url="https://s3.amazonaws.com/centerspace-videos/tutorials/Intro_Captions.mp4" length="20962812" type="video/mp4" />

		<post-id xmlns="com-wordpress:feed-additions:1">5044</post-id>	</item>
		<item>
		<title>Distribution Fitting Demo</title>
		<link>https://www.centerspace.net/distribution-fitting-demo</link>
					<comments>https://www.centerspace.net/distribution-fitting-demo#respond</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Mon, 09 Apr 2012 14:49:02 +0000</pubDate>
				<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[CDF]]></category>
		<category><![CDATA[CDF C#]]></category>
		<category><![CDATA[gaussian distribution]]></category>
		<category><![CDATA[nonlinear least squares]]></category>
		<category><![CDATA[normal distribution]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[PDF C#]]></category>
		<category><![CDATA[probability distribution]]></category>
		<category><![CDATA[Trust Region minimization]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3719</guid>

					<description><![CDATA[<p><img class="excerpt" title="Distribution Fit" src="https://www.centerspace.net/blog/wp-content/uploads/2012/04/distribution_fit_pdf.png" alt="Distribution Fit" /><br />
A customer recently asked how to fit a normal (Gaussian) distribution to a vector of experimental data. Here's a demonstration of how to do it.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/distribution-fitting-demo">Distribution Fitting Demo</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A customer recently asked how to fit a normal (Gaussian) distribution to a vector of experimental data. Here&#8217;s a demonstration of how to do it.</p>
<p>Let&#8217;s start by creating a data set: 100 values drawn from a normal distribution with known parameters (mean = 0.5, variance = 2.0).</p>
<pre lang="csharp">int n = 100;
double mean = .5;
double variance = 2.0;
var data = new DoubleVector( n, new RandGenNormal( mean, variance ) );</pre>
<p>Now, compute y values based on the empirical cumulative distribution function (CDF), which returns the probability that a random variable X will have a value less than or equal to x&#8211;that is, f(x) = P(X &lt;= x). Here&#8217;s an easy way to do, although not necessarily the most efficient for larger data sets:</p>
<pre lang="csharp">var cdfY = new DoubleVector( data.Length );
var sorted = NMathFunctions.Sort( data );
for ( int i = 0; i &lt; data.Length; i++ )
{
  int j = 0;
  while ( j &lt; sorted.Length &amp;&amp; sorted[j] &lt;= data[i] ) j++;
  cdfY[i] = j / (double)data.Length;
}</pre>
<p>The data is sorted, then for each value x in the data, we iterate through the sorted vector looking for the first value that is greater than x.</p>
<p>We&#8217;ll use one of NMath&#8217;s non-linear least squares minimization routines to fit a normal distribution CDF() function to our empirical CDF. NMath provides classes for fitting generalized one variable functions to a set of points. In the space of the function parameters, beginning at a specified starting point, these classes finds a minimum (possibly local) in the sum of the squared residuals with respect to a set of data points.</p>
<p>A one variable function takes a single double x, and returns a double y:</p>
<pre class="code">y = f(x)</pre>
<p>A <em>generalized</em> one variable function additionally takes a set of parameters, p, which may appear in the function expression in arbitrary ways:</p>
<pre class="code">y = f(p1, p2,..., pn; x)</pre>
<p>For example, this code computes y=a*sin(b*x + c):</p>
<pre lang="csharp">public double MyGeneralizedFunction( DoubleVector p, double x )
{
  return p[0] * Math.Sin( p[1] * x + p[2] );
}</pre>
<p>In the distribution fitting example, we want to define a parameterized function delegate that returns CDF(x) for the distribution described by the given parameters (mean, variance):</p>
<pre lang="csharp">Func<doublevector, double,="" double=""> f =
  ( DoubleVector p, double x ) =&gt;
    new NormalDistribution( p[0], p[1] ).CDF( x );</doublevector,></pre>
<p>Now that we have our data and the function we want to fit, we can apply the curve fitting routine. We&#8217;ll use a bounded function fitter, because the variance of the fitted normal distribution must be constrained to be greater than 0.</p>
<pre lang="csharp">var fitter = new BoundedOneVariableFunctionFitter<trustregionminimizer>( f );
var start = new DoubleVector( new double[] { 0.1, 0.1 } );
var lowerBounds = new DoubleVector( new double[] { Double.MinValue, 0 } );
var upperBounds = 
   new DoubleVector( new double[] { Double.MaxValue, Double.MaxValue } );
var solution = fitter.Fit( data, cdfY, start, lowerBounds, upperBounds );
var fit = new NormalDistribution( solution[0], solution[1] );

Console.WriteLine( "Fitted distribution:\nmean={0}\nvariance={1}",
  fit.Mean, fit.Variance );</trustregionminimizer></pre>
<p>The output for one run is</p>
<pre class="code">Fitted distribution: 
mean=0.567334190790594
variance=2.0361207956132</pre>
<p>which is a reasonable approximation to the original distribution (given 100 points).</p>
<p>We can also visually inspect the fit by plotting the original data and the CDF() function of the fitted distribution.</p>
<pre lang="csharp">ToChart( data, cdfY, SeriesChartType.Point, fit,
  NMathStatsChart.DistributionFunction.CDF );

private static void ToChart( DoubleVector x, DoubleVector y,
  SeriesChartType dataChartType, NormalDistribution dist,
  NMathStatsChart.DistributionFunction distFunction )
{
  var chart = NMathStatsChart.ToChart( dist, distFunction );
  chart.Series[0].Name = "Fit";

  var series = new Series() {
    Name = "Data",
    ChartType = dataChartType
  };
  series.Points.DataBindXY( x, y );
  chart.Series.Insert( 0, series );

  chart.Legends.Add( new Legend() );
  NMathChart.Show( chart );
}</pre>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2012/04/distribution_fit_cdf.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3727" title="distribution_fit_cdf" src="https://www.centerspace.net/blog/wp-content/uploads/2012/04/distribution_fit_cdf.png" alt="CDF() of fitted distribution" width="482" height="488" srcset="https://www.centerspace.net/wp-content/uploads/2012/04/distribution_fit_cdf.png 482w, https://www.centerspace.net/wp-content/uploads/2012/04/distribution_fit_cdf-296x300.png 296w" sizes="(max-width: 482px) 100vw, 482px" /></a></p>
<p>We can also look at the probability density function (PDF) of the fitted distribution, but to do so we must first construct an empirical PDF using a histogram. The x-values are the midpoints of the histogram bins, and the y-values are the histogram counts converted to probabilities, scaled to integrate to 1.</p>
<pre lang="csharp">int numBins = 10;
var hist = new Histogram( numBins, data );

var pdfX = new DoubleVector( hist.NumBins );
var pdfY = new DoubleVector( hist.NumBins );
for ( int i = 0; i &lt; hist.NumBins; i++ )
{
  // use bin midpoint for x value
  Interval bin = hist.Bins[i];
  pdfX[i] = ( bin.Min + bin.Max ) / 2;

   // convert histogram count to probability for y value
   double binWidth = bin.Max - bin.Min;
   pdfY[i] = hist.Count( i ) / ( data.Length * binWidth );
}

ToChart( pdfX, pdfY, SeriesChartType.Column, fit,
  NMathStatsChart.DistributionFunction.PDF );</pre>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2012/04/distribution_fit_pdf.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3728" title="distribution_fit_pdf" src="https://www.centerspace.net/blog/wp-content/uploads/2012/04/distribution_fit_pdf.png" alt="PDF() of fitted distribution" width="485" height="484" srcset="https://www.centerspace.net/wp-content/uploads/2012/04/distribution_fit_pdf.png 485w, https://www.centerspace.net/wp-content/uploads/2012/04/distribution_fit_pdf-150x150.png 150w, https://www.centerspace.net/wp-content/uploads/2012/04/distribution_fit_pdf-300x300.png 300w" sizes="(max-width: 485px) 100vw, 485px" /></a></p>
<p>You might be tempted to try to fit a distribution PDF() function directly to the histogram data, rather than using the CDF() function like we did above, but this is problematic for several reasons. The bin counts have different variability than the original data. They also have a fixed sum, so they are not independent measurements. Also, for continuous data, fitting a model based on aggregated histogram counts, rather than the original data, throws away information.</p>
<p>Ken</p>
<p>Download the <a href="https://drive.google.com/open?id=1KlctDEKniD8SdmQiBGmcrJWMuhvU-WYP">source code</a></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/distribution-fitting-demo">Distribution Fitting Demo</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/distribution-fitting-demo/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3719</post-id>	</item>
		<item>
		<title>Clearing a vector</title>
		<link>https://www.centerspace.net/clearing-a-vector</link>
					<comments>https://www.centerspace.net/clearing-a-vector#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 22:28:01 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[clearing a matrix]]></category>
		<category><![CDATA[clearing a vector]]></category>
		<category><![CDATA[NMath matirx]]></category>
		<category><![CDATA[NMath vector]]></category>
		<category><![CDATA[zeroing a matrix]]></category>
		<category><![CDATA[zeroing a vector]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3621</guid>

					<description><![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 followed by performance timing and any drawbacks. The following tests were performed on a DoubleVector of length 10,000,000. 1) Create a new vector. This isn&#8217;t [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/clearing-a-vector">Clearing a vector</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></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 followed by performance timing and any drawbacks.</p>
<p>The following tests were performed on a <code>DoubleVector</code> 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 lang="csharp" line="1"> 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 lang="csharp" line="1">
for ( int i = 0; i < v.Length; i++ )
{
  v[i] = 0.0;
}</pre>
<p>We have to do some checking in the index operator. No new memory is 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 lang="csharp" line="1"> 
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's easier for the CLR to optimize this loop. Time: <strong>173.5ms</strong></p>
<p>4) We can use the power of Intel's MKL to multiply the vector by zero.</p>
<pre lang="csharp" line="1"> 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="/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 lang="csharp" line="1"> 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 efficiently clearing a vector simpler for NMath users we have created a <code>Clear()</code> method and a <code>Clear( Slice )</code> method on the vector and matrix classes.  It will do the right thing in the right circumstance. It will be released in NMath 5.2 in 2012.</p>
<h3> Test Code </h3>
<pre lang="csharp" line="1">
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>The post <a rel="nofollow" href="https://www.centerspace.net/clearing-a-vector">Clearing a vector</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/clearing-a-vector/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3621</post-id>	</item>
		<item>
		<title>Initializing NMath</title>
		<link>https://www.centerspace.net/initializing-nmath</link>
					<comments>https://www.centerspace.net/initializing-nmath#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 22:01:27 +0000</pubDate>
				<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[<p>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 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/initializing-nmath">Initializing NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></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-83549.htm">Initializing NMath</a> for more details.</p>
<p>&#8211; Trevor</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/initializing-nmath">Initializing NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/initializing-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3611</post-id>	</item>
		<item>
		<title>Fitting Geometric Primitives to Points Using Nonlinear Least Squares</title>
		<link>https://www.centerspace.net/fitting-geometric-primitives-to-points-using-nonlinear-least-squares</link>
					<comments>https://www.centerspace.net/fitting-geometric-primitives-to-points-using-nonlinear-least-squares#respond</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Tue, 09 Aug 2011 15:39:51 +0000</pubDate>
				<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[<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 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/fitting-geometric-primitives-to-points-using-nonlinear-least-squares">Fitting Geometric Primitives to Points Using Nonlinear Least Squares</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></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="https://www.centerspace.net/doc/NMath/user/nonlinear-least-squares.htm">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="https://www.centerspace.net/doc/NMathSuite/ref/html/T_CenterSpace_NMath_Core_DoubleMultiVariableFunction.htm">DoubleMultiVariableFunction</a>, and override the <code>Evaluate()</code> method:</p>
<pre lang="csharp">public class CircleFitFunction : DoubleMultiVariableFunction
{

  public CircleFitFunction( DoubleVector x, DoubleVector y )
    : base( 3, x.Length )
  {
    if( x.Length != y.Length )
      throw new Exception( "Unequal number of x,y values." );

    X = x;
    Y = y;
  }

  public DoubleVector X { get; internal set; }
  public DoubleVector Y { get; internal set; }

  public override void Evaluate( DoubleVector parameters, ref DoubleVector residuals )
  {
    // parameters of circle with center (a,b) and radius r
    double a = parameters[0];
    double b = parameters[1];
    double r = parameters[2];

    for( int i = 0; i < X.Length; i++ )
    {
      // distance of point from circle center
      double d = Math.Sqrt( Math.Pow( X[i] - a, 2.0 ) + Math.Pow( Y[i] - b, 2.0 ) );

      residuals[i] = d - r;
    }
  }
}</pre>
<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>
<pre lang="csharp">: base( 3, x.Length )</pre>
<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 - r. The nonlinear least squares method will minimize the L2 norm of this function.</p>
<p>To demonstrate the fitting process, let'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>
<pre lang="csharp">int n = 20;
DoubleVector x = new DoubleVector( n );
DoubleVector y = new DoubleVector( n );

double a = 0.5;
double b = 0.25;
double r = 2;

RandGenUniform noise = new RandGenUniform( -.75, .75 );
for( int i = 0; i < n; i++ )
{
  double t = i * 2 * Math.PI / n;
  x[i] = a + r * Math.Cos( t ) + noise.Next();
  y[i] = b + r * Math.Sin( t ) + noise.Next();
}</pre>
<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>
<pre lang="csharp">CircleFitFunction f = new CircleFitFunction( x, y );
TrustRegionMinimizer minimizer = new TrustRegionMinimizer();
DoubleVector start = new DoubleVector( "0.1 0.1 0.1" );
DoubleVector solution = minimizer.Minimize( f, start );

Console.WriteLine( "solution = " + solution );
Console.WriteLine( "final residual = " + minimizer.FinalResidual );</pre>
<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="/using-nmath-with-microsoft-chart-controls-for-net/">Microsoft Chart Controls for .NET</a>.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3465" title="Fitted Circle" src="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit.png" alt="Fitted Circle" width="485" height="491" srcset="https://www.centerspace.net/wp-content/uploads/2011/08/fit.png 485w, https://www.centerspace.net/wp-content/uploads/2011/08/fit-296x300.png 296w" sizes="(max-width: 485px) 100vw, 485px" /></a></p>
<p>Now that we're sure the minimization is working as desired, let's try it again with some random points:</p>
<pre lang="csharp">int n = 10;
RandGenUniform rng = new RandGenUniform( -1, 2 );
DoubleVector x = new DoubleVector( n, rng );
DoubleVector y = new DoubleVector( n, rng );

CircleFitFunction f = new CircleFitFunction( x, y );
TrustRegionMinimizer minimizer = new TrustRegionMinimizer();
DoubleVector start = new DoubleVector( "0.1 0.1 0.1" );
DoubleVector solution = minimizer.Minimize( f, start );</pre>
<p>Again, plotting the solution:</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit2.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3466" title="Fitted Circle" src="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit2.png" alt="Fitted Circle" width="488" height="488" srcset="https://www.centerspace.net/wp-content/uploads/2011/08/fit2.png 488w, https://www.centerspace.net/wp-content/uploads/2011/08/fit2-150x150.png 150w, https://www.centerspace.net/wp-content/uploads/2011/08/fit2-300x300.png 300w" sizes="(max-width: 488px) 100vw, 488px" /></a>In some runs, the fit may seem counter-intuitive. For example:</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit3.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3467" title="Fitted Circle" src="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit3.png" alt="Fitted Circle" width="488" height="490" srcset="https://www.centerspace.net/wp-content/uploads/2011/08/fit3.png 488w, https://www.centerspace.net/wp-content/uploads/2011/08/fit3-150x150.png 150w, https://www.centerspace.net/wp-content/uploads/2011/08/fit3-298x300.png 298w" sizes="(max-width: 488px) 100vw, 488px" /></a></p>
<p>But if you think about it, this sort of fit makes perfect sense, given how we defined our fit function. We'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="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit4.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3470" title="Fitted Circle" src="https://www.centerspace.net/blog/wp-content/uploads/2011/08/fit4.png" alt="Fitted Circle" width="493" height="488" srcset="https://www.centerspace.net/wp-content/uploads/2011/08/fit4.png 493w, https://www.centerspace.net/wp-content/uploads/2011/08/fit4-300x296.png 300w" sizes="(max-width: 493px) 100vw, 493px" /></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>The post <a rel="nofollow" href="https://www.centerspace.net/fitting-geometric-primitives-to-points-using-nonlinear-least-squares">Fitting Geometric Primitives to Points Using Nonlinear Least Squares</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/fitting-geometric-primitives-to-points-using-nonlinear-least-squares/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3460</post-id>	</item>
		<item>
		<title>Advanced Curve Fitting using Excel and NMath</title>
		<link>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath</link>
					<comments>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath#respond</comments>
		
		<dc:creator><![CDATA[CenterSpace]]></dc:creator>
		<pubDate>Mon, 14 Mar 2011 22:10:15 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3145</guid>

					<description><![CDATA[<p>In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work. Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points. This example combines the ease of Excel's charting capabilities with CenterSpace's powerful NMath library.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath">Advanced Curve Fitting using Excel and NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In recent blog posts, we have discussed how to call CenterSpace&#8217;s Libraries from Excel.  In his March 2010 blog post, CenterSpace&#8217;s Ken Baldwin demonstrated how to replicate <a href="https://www.centerspace.net">Excel&#8217;s existing Trendline functions using C# and NMath</a>.  In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work.</p>
<p>Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points.  For example, if we collect a series of changing data values over time, we look to explain the relationship that time effects the generated values or in mathematical terms y=f(x).  Here we are using x to represent time values and f(x) to represent the relationship or function that generates the resulting values or y.  So if we can find a function f(x) that represents a good fit of the data we should be able to predict the result at any moment in time.</p>
<p>With this in mind, let us get started with some data points (x, y) that we have collected.  I have entered the following values in an Excel spreadsheet.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif"><img decoding="async" class="size-full wp-image-3146" title="Curve Fitting Sample data" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit1.gif 777w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit1-300x279.gif 300w" sizes="(max-width: 777px) 100vw, 777px" /></a></p>
<p>We can now use Excel&#8217;s charting function to create the following XY scatterplot with our data:</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif"><img decoding="async" class="size-full wp-image-3147" title="Excel XY scatterplot of sample data" width="700" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2.gif 1473w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2-300x184.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2-1024x629.gif 1024w" sizes="(max-width: 1473px) 100vw, 1473px" /></a></p>
<p>At this point, we can add an Excel trendline to get the best fit it can provide.  By right clicking on a data value in our chart we will get the option to add a trendline. Choose a 2nd order polynomial and these options.</p>
<p>Name the trendline &#8220;2nd Order Polynomial&#8221; and check &#8220;Display equation on chart&#8221; and &#8220;Display R-squared value on chart&#8221;.</p>
<p>Excel calculates and plots the line while returning the equation and the R2 value.  If our R2 equals 1 we would have found a perfect fit.  For a second order polynomial Excel returned a value of 0.8944 which means that roughly 10.6 percent (1-.894) are not on this line.</p>
<p>If we continue increasing our polynomial orders up to the maximum of six we can achieve the best R2 value of 0.9792, but look at the curve we have fitted to these points.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif"><img decoding="async" class="size-full wp-image-3149" title="Excel Trendline using a polynomial function" width="700" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3.gif 1167w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3-300x188.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3-1024x642.gif 1024w" sizes="(max-width: 1167px) 100vw, 1167px" /></a></p>
<p>A better fit might be an exponential function so let us try Excel&#8217;s trendline option using exponentials.<br />
<a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif"><img decoding="async" class="alignnone size-full wp-image-3150" title="Excel exponential trendline fit of sample data" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif" width="700"  alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4.gif 1169w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4-300x203.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4-1024x695.gif 1024w" sizes="(max-width: 1169px) 100vw, 1169px" /></a></p>
<p>Clearly the results are visually a better fit but the R2 value tells us that over 15% of the data points are not on this line.  This pretty much represents the best we can do with Excel&#8217;s trendline functions for our data.</p>
<p>Looking to CenterSpace&#8217;s NMath and NStat libraries to give us more robust analysis, we can utilize more powerful curve fitting tools quickly and with little effort.</p>
<p>Using the linkage provided by ExcelDNA that we examined in our previous posts, we can create the following C# code for our ExcelDNA text file.</p>
<pre lang="csharp"><![CDATA[

using System;
using ExcelDna.Integration;
using CenterSpace.NMath.Core;
using CenterSpace.NMath.Matrix;
using CenterSpace.NMath.Analysis;
using CenterSpace.NMath.Stats;

public class NMathExcelCurveFit
{

 [ExcelFunction(Description="Four parameterized Fit")]
 public static double[] NOneVarFunctFitFour(double[] xValues, double[] yValues, double[] start)
 {
  DoubleVector TempVx = new DoubleVector(xValues);
  DoubleVector TempVy = new DoubleVector(yValues);
  DoubleVector TempVs = new DoubleVector(start);
  OneVariableFunctionFitter
<TrustRegionMinimizer> fitter = new  OneVariableFunctionFitter
<TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
  DoubleVector solution = fitter.Fit(TempVx, TempVy, TempVs);
  return solution.ToArray();
 }

 [ExcelFunction(Description="Four parameterized R2")]
 public static double NOneVarFunctFitFourR2(double[] xValues, double[] yValues, double[] start)
 {
  DoubleVector TempVx = new DoubleVector(xValues);
  DoubleVector TempVy = new DoubleVector(yValues);
  DoubleVector TempVs = new DoubleVector(start);
  OneVariableFunctionFitter
<TrustRegionMinimizer> fitter = new  OneVariableFunctionFitter
<TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
  DoubleVector solution = fitter.Fit(TempVx, TempVy, TempVs);
  GoodnessOfFit gof = new GoodnessOfFit(fitter, TempVx, TempVy, solution);
  return gof.RSquared;
 }
}
]]&gt;</pre>
<p>As you can see by the code, I have chosen to use NMath&#8217;s OneVariableFunctionFitter with the FourParameterLogistic predefined generalized function.</p>
<pre class="code">
<img decoding="async" title="{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" src="http://latex.codecogs.com/gif.latex?{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" alt=""/></pre>
<p>From CenterSpace&#8217;s documentation, we get the above equation and need to solve for the model parameters <code> a, b, c, d </code>.</p>
<p>The OneVariableFunction call will require us to provide a starting guess along with the ranges containing the xValues and yValues.  The following screen shows us making the function call from Excel with the necessary parameters.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif"><img decoding="async" class="size-full wp-image-3156" title="CurveFit6" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6.gif 1296w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6-300x172.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6-1024x587.gif 1024w" sizes="(max-width: 1296px) 100vw, 1296px" /></a></p>
<p>After computing these values, we can call for the R2 value and generate some points to be plotted.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif"><img decoding="async" class="size-full wp-image-3157" title="Obtaining the R2 value from a NMath OneVariableFunction call" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7.gif 1059w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7-300x175.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7-1024x600.gif 1024w" sizes="(max-width: 1059px) 100vw, 1059px" /></a></p>
<p>Note that I choose to hide the rfows from r17 to r107 for display purposes. You will need to have them unhid for the chart to look right.  As you can see we returned the best R2 value so far at 0.9923.</p>
<p>In the next screen, we will have added the series to our chart and drawn a line through our calculated points.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif"><img decoding="async" class="size-full wp-image-3158" title="OneVariableFunction using FourParameterLogistic function plot" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8.gif 1179w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8-300x200.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8-1024x682.gif 1024w" sizes="(max-width: 1179px) 100vw, 1179px" /></a></p>
<p>This example illustrates the ease that Excel can use the power of NMath curve fitting routines to compute accurate fits to a collection of data.</p>
<p>Mike Magee</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath">Advanced Curve Fitting using Excel and NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3145</post-id>	</item>
		<item>
		<title>Using NMath with Microsoft Chart Controls for .NET</title>
		<link>https://www.centerspace.net/using-nmath-with-microsoft-chart-controls-for-net</link>
					<comments>https://www.centerspace.net/using-nmath-with-microsoft-chart-controls-for-net#respond</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Wed, 09 Mar 2011 11:04:53 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[.NET chart]]></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[VB.NET charts]]></category>
		<category><![CDATA[VB.NET plotting]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3262</guid>

					<description><![CDATA[<p><img class="excerpt" title="chart2" src="https://www.centerspace.net/blog/wp-content/uploads/2011/03/chart2.png"  /> The Microsoft Chart Controls for .NET provide an easy (and free) solution for visualizing NMath numerical types. The Chart controls are available as a separate download for .NET 3.5. Beginning in .NET 4.0, the Chart controls are part of the .NET Framework. </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-nmath-with-microsoft-chart-controls-for-net">Using NMath with Microsoft Chart Controls for .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In 2007, Microsoft acquired the Dundas chart components, in order to deliver data visualization directly within Microsoft products. In October 2008, they released the Microsoft Chart Controls for .NET, which includes the Dundas ASP.NET and Windows Forms Chart controls. The Chart controls are available as a <a href="https://www.microsoft.com/en-us/download/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 System.Windows.Forms.DataVisualization.</p>
<p>A <strong>Chart</strong> object contains one or more <strong>ChartArea</strong>s, each of which contain one or more data <strong>Series</strong>. Each <strong>Series</strong> has an associated chart type, and a <strong>DataPoint</strong> collection. <strong>DataPoint</strong>s can be manually appended or inserted into the collection, or added automatically when a series is bound to a datasource using either the <tt>DataBindY()</tt> or <tt>DataBindXY()</tt> method. Since any <strong>IEnumerable</strong> can act as a datasource, it&#8217;s easy to use an NMath vector or vector view (of a matrix row or column, for example) as a datasource.</p>
<p>For example, suppose we want to create a scatter plot of the first two columns of a 20 x 5 <strong>DoubleMatrix</strong> (that is, column 0 vs. column 1).</p>
<pre lang="csharp">DoubleMatrix data = new DoubleMatrix( 20, 5, new RandGenUniform() );
DoubleVector x = data.Col( 0 );
DoubleVector y = data.Col( 1 );</pre>
<p>Begin by creating a new <strong>Chart</strong> object, and optionally adding a <strong>Title</strong>.</p>
<pre lang="csharp">Chart chart = new Chart()
{
  Size = new Size( 500, 500 ),
};

Title title = new Title()
{
  Name = chart.Titles.NextUniqueName(),
  Text = "My Data",
  Font = new Font( "Trebuchet MS", 12F, FontStyle.Bold ),
};
chart.Titles.Add( title );</pre>
<p>Next, add a <strong>ChartArea</strong>.</p>
<pre lang="csharp">ChartArea area = new ChartArea()
{
  Name = chart.ChartAreas.NextUniqueName(),
};
area.AxisX.Title = "Col 0";
area.AxisX.TitleFont = new Font( "Trebuchet MS", 10F, FontStyle.Bold );
area.AxisX.MajorGrid.LineColor = Color.LightGray;
area.AxisX.RoundAxisValues();
area.AxisY.Title = "Col 1";
area.AxisY.TitleFont = new Font( "Trebuchet MS", 10F, FontStyle.Bold );
area.AxisY.MajorGrid.LineColor = Color.LightGray;
area.AxisY.RoundAxisValues();
chart.ChartAreas.Add( area );</pre>
<p>Finally, add a new data <strong>Series</strong>, and bind the datasource to the NMath x,y vectors.</p>
<pre lang="csharp">Series series = new Series()
{
  Name = "Points",
  ChartType = SeriesChartType.Point,
  MarkerStyle = MarkerStyle.Circle,
  MarkerSize = 8,
};
series.Points.DataBindXY( x, y );
chart.Series.Add( series );</pre>
<p>To display the chart, we can use a utility function like this, which shows the given chart in a default form running in a new thread.</p>
<pre lang="csharp">public static void Show( Chart chart )
{
  Form form = new Form();
  form.Size = new Size( chart.Size.Width + 20, chart.Size.Height + 40 );
  form.Controls.Add( chart );
  Thread t = new Thread( () =&gt; Application.Run( form ) );
   t.Start();
}</pre>
<p>After calling</p>
<pre lang="csharp">Show( chart );</pre>
<p>the result looks like this:</p>
<p style="text-align: center;"><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/03/chart11.png"><img decoding="async" loading="lazy" class="size-full wp-image-3267 aligncenter" title="chart1" src="https://www.centerspace.net/blog/wp-content/uploads/2011/03/chart11.png" alt="Microsoft Chart Controls for .NET" width="491" height="486" srcset="https://www.centerspace.net/wp-content/uploads/2011/03/chart11.png 491w, https://www.centerspace.net/wp-content/uploads/2011/03/chart11-300x296.png 300w" sizes="(max-width: 491px) 100vw, 491px" /></a></p>
<p>Of course, you can easily plot more complicated data as well. For instance, suppose we fit a 4th degree polynomial to this (random) data:</p>
<pre lang="csharp">int degree = 4;
PolynomialLeastSquares pls = new PolynomialLeastSquares( degree, x, y );</pre>
<p>To add the fitted polynomial to the cart, just add a new data <strong>Series</strong> interpolating over the range of x values.</p>
<pre lang="csharp">Series series2 = new Series()
{
  Name = "Polynomial",
  ChartType = SeriesChartType.Line,
  MarkerStyle = MarkerStyle.None,
};
int numInterpolatedValues = 100;
double xmin = NMathFunctions.MinValue( x );
double xmax = NMathFunctions.MaxValue( x );
double step = ( xmax - xmin ) / ( numInterpolatedValues - 1 );
DoubleVector xi = new DoubleVector( numInterpolatedValues, xmin, step );
series2.Points.DataBindXY( xi, pls.FittedPolynomial.Evaluate( xi ) );
chart.Series.Add( series2 );</pre>
<p>Let&#8217;s also add a subtitle with the fitted function, and a <strong>Legend</strong>.</p>
<pre lang="csharp">Title subtitle = new Title()
{
  Name = chart.Titles.NextUniqueName(),
  Text = "f(x) = " + pls.FittedPolynomial.ToString( "N2" ),
};
chart.Titles.Add( subtitle );

Legend legend = new Legend()
{
  Name = chart.Legends.NextUniqueName(),
  DockedToChartArea = chart.ChartAreas[0].Name,
};
chart.Legends.Add( legend );</pre>
<p>Now the chart looks like this:<br />
<a href="https://www.centerspace.net/blog/wp-content/uploads/2011/03/chart2.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3268" title="chart2" src="https://www.centerspace.net/blog/wp-content/uploads/2011/03/chart2.png" alt="" width="491" height="494" srcset="https://www.centerspace.net/wp-content/uploads/2011/03/chart2.png 491w, https://www.centerspace.net/wp-content/uploads/2011/03/chart2-150x150.png 150w, https://www.centerspace.net/wp-content/uploads/2011/03/chart2-298x300.png 298w" sizes="(max-width: 491px) 100vw, 491px" /></a></p>
<p>If you add a <strong>Chart</strong> control to your application using the visual designer (by dragging a <strong>Chart</strong> control from the Data category in the Toolbox), this generates code to create a default <strong>Chart</strong>, with a single <strong>ChartArea</strong>, <strong>Series</strong>, and <strong>Legend</strong>. You can then edit the properties in the Properties tab, and bind the generated <strong>Series</strong> to an NMath datasource. For example:</p>
<pre lang="csharp">public Form1()
{
  InitializeComponent();

  DoubleMatrix data = new DoubleMatrix( 20, 5, new RandGenUniform() );
  this.chart1.Series[0].Points.DataBindXY( data.Col(0), data.Col(1) );
}</pre>
<p>The Microsoft Chart Controls for .NET provide an easy (and free) solution for visualizing NMath numerical types.</p>
<p>Ken</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-nmath-with-microsoft-chart-controls-for-net">Using NMath with Microsoft Chart Controls for .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/using-nmath-with-microsoft-chart-controls-for-net/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3262</post-id>	</item>
		<item>
		<title>Quasirandom Points</title>
		<link>https://www.centerspace.net/quasirandom-points</link>
					<comments>https://www.centerspace.net/quasirandom-points#respond</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Wed, 09 Feb 2011 16:05:44 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[.net quasirandom]]></category>
		<category><![CDATA[c# quasi random]]></category>
		<category><![CDATA[c# quasirandom]]></category>
		<category><![CDATA[pseudorandom]]></category>
		<category><![CDATA[quasi-random numbers]]></category>
		<category><![CDATA[quasirandom numbers]]></category>
		<category><![CDATA[vb.net quasirandom]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3192</guid>

					<description><![CDATA[<p><img class="excerpt" title="Quasirandom Points" src="https://www.centerspace.net/blog/wp-content/uploads/2011/02/quasirandom2.png" alt="Quasirandom Points" /><br />
A quasirandom sequence is a set of n-tuples that fills n-space more uniformly than uncorrelated random points. NMath provides both Sobol and Niederreiter quasirandom number generators for .NET applications.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/quasirandom-points">Quasirandom Points</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A <a href="https://en.wikipedia.org/wiki/Low-discrepancy_sequence">quasirandom</a> sequence is a set of n-tuples that fills n-space more uniformly than uncorrelated random points. NMath provides both Sobol and Niederreiter quasirandom number generators for .NET applications.</p>
<p>For example, this C# code creates a Niederreiter quasirandom number generator object to generate quasirandom vectors of length 2:</p>
<pre lang="csharp">int dim = 2;
NiederreiterQuasiRandomGenerator nqrg =
  new NiederreiterQuasiRandomGenerator(dim);</pre>
<p>You can use an NMath quasirandom generator to fill a matrix or array. The points are the columns of the matrix, so the number of rows in the given matrix must be equal to the dimension of the quasirandom number generator. Here we create and fill a 2 x 500 matrix with quasirandom numbers from a uniform (0,1) distribution.</p>
<pre lang="csharp">int n = 500;
DoubleMatrix A = new DoubleMatrix(dim, n);
nqrg.Fill(A);</pre>
<p>We can plot the points to see the distribution.</p>
<p style="text-align: center;"><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/02/quasirandom2.png"><img decoding="async" loading="lazy" class="size-full wp-image-3196 aligncenter" title="Quasirandom Points" src="https://www.centerspace.net/blog/wp-content/uploads/2011/02/quasirandom2.png" alt="Quasirandom Points" width="387" height="400" srcset="https://www.centerspace.net/wp-content/uploads/2011/02/quasirandom2.png 387w, https://www.centerspace.net/wp-content/uploads/2011/02/quasirandom2-290x300.png 290w" sizes="(max-width: 387px) 100vw, 387px" /></a></p>
<p>Let&#8217;s compare the results above with the distribution from a standard pseudorandom generator.</p>
<pre lang="csharp">int seed = 0x345;
RandomNumberStream stream = new RandomNumberStream( seed, RandomNumberStream.BasicRandGenType.MersenneTwister );
DoubleRandomUniformDistribution unifDist = new DoubleRandomUniformDistribution();
DoubleMatrix B = new DoubleMatrix( dim, n, stream, unifDist );</pre>
<p>Plotting the results shows that the pseudorandom points are much less uniformly distributed. Regions of higher and lower density are clearly evident.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/02/pseudorandom.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-3214" title="pseudorandom" src="https://www.centerspace.net/blog/wp-content/uploads/2011/02/pseudorandom.png" alt="" width="400" height="392" srcset="https://www.centerspace.net/wp-content/uploads/2011/02/pseudorandom.png 400w, https://www.centerspace.net/wp-content/uploads/2011/02/pseudorandom-300x294.png 300w" sizes="(max-width: 400px) 100vw, 400px" /></a></p>
<p>Quasirandom numbers have many applications, especially in Monte Carlo simulations. For instance, a classic use of the Monte Carlo method is for the evaluation of definite integrals. Let&#8217;s use an NMath Sobol generator to approximate the integral of a function F over a 6-dimensional unit cube. The function is defined by</p>
<p>F(x) = 1*cos(1*x1) * 2*cos(2*x2) * 3*cos(3*x3) *&#8230;* 6*cos(6*x6)</p>
<p>where x = (x1, x2,&#8230;, x6) is a point in 6-dimensional space.</p>
<p>This is the implementation of the function F:</p>
<pre lang="csharp">static double F( DoubleVector x )
{
  double y = 1;
  for ( int i = 1; i &lt;= x.Length; i++ )
  {
    y *= i * Math.Cos( i * x[i - 1] );
  }
  return y;
}</pre>
<p>In the following C# code we provide our own primitive polynomials for initializing the quasirandom generator. Primitive polynomials have coefficients in {0, 1} and are specified by BitArray&#8217;s containing the polynomial coefficients with the leading coefficient at index 0. We can supply either dim polynomials, or dim &#8211; 1 polynomials. If we specify dim &#8211; 1 polynomials, the primitive polynomial for the first dimension will be initialized with a default value.</p>
<pre lang="csharp">dim = 6;

BitArray[] primitivePolys = new BitArray[dim];
primitivePolys[0] = new BitArray( new bool[] { true, true } ); // x + 1
primitivePolys[1] = new BitArray( new bool[] { true, true, true } ); // x^2 + x + 1
primitivePolys[2] = new BitArray( new bool[] { true, false, true, true } ); // x^3 + x + 1
primitivePolys[3] = new BitArray( new bool[] { true, true, false, true } ); // x^3 + x^2 + 1
primitivePolys[4] = new BitArray( new bool[] { true, false, false, true, true } ); // x^4 + x + 1
primitivePolys[5] = new BitArray( new bool[] { true, true, false, false, true } ); // x^4 + x^3 + 1

SobolQuasiRandomGenerator sobol = new SobolQuasiRandomGenerator( dim, primitivePolys );</pre>
<p>Now let&#8217;s use this generator to approximate the integral:</p>
<pre lang="csharp">int numPoints = 180000;
DoubleMatrix points = new DoubleMatrix( dim, numPoints );
sobol.Fill( points );

double sum = 0;
for ( int i = 0; i &lt; numPoints; i++ )
{
  sum += F( points.Col( i ) );
}
sum /= numPoints;

double actualIntegralValue = -0.022;
Console.WriteLine( "Actual integral value = " + actualIntegralValue );
Console.WriteLine( "\nMonte-Carlo approximated integral value = " + sum );</pre>
<p>The output is:</p>
<pre>Actual integral value = -0.022
Monte-Carlo approximated integral value = -0.0232768655843053</pre>
<p>Ken</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/quasirandom-points">Quasirandom Points</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/quasirandom-points/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3192</post-id>	</item>
		<item>
		<title>Using C# and ExcelDNA to call .NET Libraries</title>
		<link>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries</link>
					<comments>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries#comments</comments>
		
		<dc:creator><![CDATA[CenterSpace]]></dc:creator>
		<pubDate>Fri, 14 Jan 2011 05:00:21 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[c# math with excel]]></category>
		<category><![CDATA[Calling Excel from NMath]]></category>
		<category><![CDATA[Curve fitting with Excel]]></category>
		<category><![CDATA[ExcelDNA math]]></category>
		<category><![CDATA[ExcelDNA NMath]]></category>
		<category><![CDATA[NMath Excel]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3058</guid>

					<description><![CDATA[<p><img src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif" alt="Excel and NMath interop"  class="excerpt" /><br />
The outcome of these blog articles should illustrate that Excel can become a powerful tool to quickly access a comprehensive .NET library without needing a large supporting programming environment.  This made possible by Govert van Drimmelen's freeware tool ExcelDNA.  ExcelDNA can interpret VB, C#, and F# instructions stored in a text file as Excel is loaded eliminating the need for a separate compiler.  In our last blog, we named our file <strong>NMathExcel.dna</strong> to hold our VB code.  Below, I have provided C# code that performs the same matrix functions as the VB code in our <a href="https://www.centerspace.net/blog/nmath/calling-external-net-libraries-from-excel/">previous post</a>.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries">Using C# and ExcelDNA to call .NET Libraries</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In my <a href="/calling-external-net-libraries-from-excel/">last post</a>, I demonstrated calling NMath from Excel using ExcelDNA and Visual Basic (VB) code.  In this blog post, we will duplicate that functionality using C# instead of Visual Basic.  In addition we will use the functionality of NMath to enabled the marshaling of data between Excel and NMath, and provide some additional code examples.</p>
<p>The outcome of these blog articles should illustrate that Excel can become a powerful tool to quickly access a comprehensive .NET library such as CenterSpace&#8217;s NMath without needing a large supporting programming environment.  This is all made possible by Govert van Drimmelen&#8217;s freeware tool ExcelDNA.  ExcelDNA can interpret VB, C#, and F# instructions stored in a text file as Excel is loaded eliminating the need for a separate compiler.  ExcelDNA does require the text file to have the extension <code>.DNA</code>.  I recommend using an editor like <a href="https://notepad-plus-plus.org/">NotePad++</a> to edit this file.  In our last blog post, we named our file <strong>NMathExcel.dna</strong> to hold our VB code.  Below, I have provided C# code that performs the same functions as the VB code in our <a href="/calling-external-net-libraries-from-excel/">previous post</a>.  This code below provides examples on how to use several basic routines in NMath.</p>
<pre lang="csharp">

<DnaLibrary Language="CS">
<Reference Name="CenterSpace.NMath.Core" />
<Reference Path="C:\Program Files\CenterSpace\NMath 4.1\Assemblies\NMath.dll" />
<Reference Path="C:\Program Files\CenterSpace\NMath 4.1\Assemblies\NMathShared.dll" />
<Reference Name="CenterSpace.NMath.Stats" />
<Reference Path="C:\Program Files\CenterSpace\NMath Stats 3.2\Assemblies\NMathStats.dll" />

<![CDATA[
 	 using ExcelDna.Integration;
 	 using CenterSpace.NMath.Core;
  	 using CenterSpace.NMath.Stats;

  	public class NMathExcelFunctions
  	{
  		[ExcelFunction(Description="Returns x raised to the y power")]
  		public static double NPower(double x, double y)
  		{
  			return NMathFunctions.PowFunction(x, y);
  		}
  		[ExcelFunction(IsVolatile=true)]
  		public static double NRand()
  		{
  			RandGenMTwist randomGenMTwist = new RandGenMTwist();
  			return randomGenMTwist.Next53BitRes();
  		}
  		[ExcelFunction(Description="Binomial Distribution: Number of Successes, Trials, Probability, Cumulative is True or False")]
  		public static double NBinomDist(int NSuccess, int NTrials, double Prob, bool Cumul)
  		{
 	 		BinomialDistribution binomialDistribution = new BinomialDistribution();
  			binomialDistribution.N = NTrials;
  			binomialDistribution.P = Prob;
  			if (Cumul)
  				return binomialDistribution.CDF(NSuccess);
  			else
  				return binomialDistribution.PDF(NSuccess);
  		}
  		[ExcelFunction(Description="Create a r by c matrix and fill with Random # between L and B")]
  		public static double[,] NDoubleMatrixRand(int rsize, int csize, int RandLBx, int RandUBx)
  		{
  			RandGenUniform randomGenUniform = new RandGenUniform(RandLBx,RandUBx);
  			randomGenUniform.Reset(0x124);
  			DoubleMatrix TempAr = new DoubleMatrix(rsize,csize,randomGenUniform);
  			return TempAr.ToArray();
  		}
  	}
]]&gt;
</DnaLibrary>
</pre>
<p>In reviewing the differences between the VB and C# versions, the first order of business was to add the language specification to &#8220;CS&#8221; for C# so that ExcelDNA knows to switch from the default language of VB. Aside from the obvious differences between the two languages, we have added a reference to our NMathShared assembly and an explicit reference to using ExcelDNA.Integration.  In general, the C# code is slightly simpler than VB and enables very simple code to call the library.  In our C# sample code function for creating a <code>DoubleMatrix</code> with a random generation, we used the library&#8217;s built in conversion function <code>ToArray()</code> to marshal the data into an array format for Excel.  Using NMath&#8217;s built-in array handling capabilities we can simply copy data in and out in the proper formats.  This illustrates the ease by which we can utilize the NMath math libraries and Excel.</p>
<p>To further demonstrate this capability, we can add the following code to our NMathExcel.dna text file that calls the matrix transpose function in NMath.  We will show how to create array data in Excel, pass it to the library in the proper format, and then return a result for display.</p>
<pre lang="csharp">
   public static double[,] NDoubleMatrixTranspose(double[,] InputAr)
   {
      DoubleMatrix TempAr = new DoubleMatrix(InputAr);
     TempAr = TempAr.Transpose();
      return TempAr.ToArray();
   }
</pre>
<p>Make sure you close Excel and reopen it after saving your code changes to the .DNA file so that ExcelDNA has the opportunity to incorporate the new code.</p>
<p>We can now send our library a range of data cells to be acted on and display the result.  In the following screen shot I show setting up a 5 by 5 range with values to be transposed.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic1.gif"><img decoding="async" loading="lazy" width="829" height="603" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic1.gif" alt="" title="Creating a 5x5 matrix with data" class="alignnone size-full wp-image-3071" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic1.gif 829w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic1-300x218.gif 300w" sizes="(max-width: 829px) 100vw, 829px" /></a></p>
<p>We can now select an available empty cell to insert our transpose computation.  The following screenshot shows selecting the input range for calling our NMath transpose function.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif"><img decoding="async" loading="lazy" width="993" height="698" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif" alt="" title="Selecting a range of value to send to the Transpose function  class="alignnone size-full wp-image-3073" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic2.gif 993w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic2-300x210.gif 300w" sizes="(max-width: 993px) 100vw, 993px" /></a></p>
<p>As in our previous blog, the returned result is stored in a single cell and only one value is displayed.  As a quick review to display the full result, first paint (select) the area for the data to be displayed with the function call in the upper left hand corner, then press the F2 key, next press Ctrl-Shift (hold), followed by the Enter key &#8211; this will build the array formula to populate the selected area with the result.  Your result should look like the following screen.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic3.gif"><img decoding="async" loading="lazy" width="773" height="555" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic3.gif" alt="" title="Completed example of the matrix transpose function" class="alignnone size-full wp-image-3074" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic3.gif 773w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic3-300x215.gif 300w" sizes="(max-width: 773px) 100vw, 773px" /></a></p>
<p>As a further example of what is possible, we can tackle an least squares example from the NMath documentation.  The problem is to calculate the residual norm square using the Cholesky method, something that would be awkward to do natively in Excel alone. In this example the inputs are the number of rows and columns of a <code>DoubleMatrix</code> uniformly filled with random numbers. To accomplish this we need to add the following code to our DNA text file.</p>
<pre lang="csharp">
using CenterSpace.NMath.Matrix;
  .
  .
public static object NDoubleCholeskyLeastSqRNS(int rsize, int csize, int RandLBx, int RandUBx)
{
	RandGenUniform randomGenUniform = new RandGenUniform(RandLBx,RandUBx);
	randomGenUniform.Reset(0x124);
	DoubleMatrix TempAr = new DoubleMatrix(rsize,csize,randomGenUniform);
	DoubleCholeskyLeastSq cholLsq = new DoubleCholeskyLeastSq(TempAr);
	if (cholLsq.IsGood)
	{
 		DoubleVector b = new DoubleVector(TempAr.Rows,randomGenUniform);
		DoubleVector x = cholLsq.Solve(b);
		return cholLsq.ResidualNormSqr(b);
	}
	else
		return "The random matrix doesn't have full rank";
}
</pre>
<h3> Summary </h3>
<p>Approaching the complex computations with Excel by performing the necessary computation by calling a C# library with data passed in from Excel, creates a robust approach to solving problems.   The following screenshot shows how providing our function with the input parameters from the example produces the desired result.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic4.gif"><img decoding="async" loading="lazy" width="870" height="418" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic4.gif" alt="" title="Using the Cholesky Least Squares method in Excel to solve." class="alignnone size-full wp-image-3077" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic4.gif 870w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic4-300x144.gif 300w" sizes="(max-width: 870px) 100vw, 870px" /></a></p>
<p>If we tried to implement the solution line by line in Excel, we would be loosing the power of an object language like C# and powerful third-party .NET libraries, and increasing the generation of tedious Excel code without a strong development environment. </p>
<p>To wrap up, combining Excel, ExcelDNA, and the CenterSpace&#8217;s NMath libraries can be used to quickly generate solutions to complex problems without an extensive programming environment.  In my next blog, I plan to solve a curve fitting example with this approach and using more of Excel features to display the results.</p>
<p>Mike Magee</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries">Using C# and ExcelDNA to call .NET Libraries</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3058</post-id>	</item>
	</channel>
</rss>
