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

<channel>
	<title>singular matrix &#8211; Aptech</title>
	<atom:link href="https://www.aptech.com/blog/tag/singular-matrix/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.aptech.com</link>
	<description>GAUSS Software - Fastest Platform for Data Analytics</description>
	<lastBuildDate>Thu, 21 May 2020 20:20:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Diagnosing a singular matrix</title>
		<link>https://www.aptech.com/blog/diagnosing-a-singular-matrix/</link>
					<comments>https://www.aptech.com/blog/diagnosing-a-singular-matrix/#respond</comments>
		
		<dc:creator><![CDATA[aptech]]></dc:creator>
		<pubDate>Sun, 25 Nov 2018 16:51:46 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[positive definite]]></category>
		<category><![CDATA[singular matrix]]></category>
		<guid isPermaLink="false">https://www.aptech.com/?p=18455</guid>

					<description><![CDATA[`G0121: Matrix not positive definite` and `G0048: Matrix singular` are common errors encountered during estimation. Today we will learn how to diagnose these errors using GAUSS code to compute ordinary least squares estimates, using real data from some golf shots hit by this author and recorded by a launch monitor.]]></description>
										<content:encoded><![CDATA[<h3 id="introduction">Introduction</h3>
<p><code>G0121: Matrix not positive definite</code> and <code>G0048: Matrix singular</code> are common errors encountered during estimation. Today we will run some code to compute OLS estimates, using real data from some golf shots hit by this author and recorded by a launch monitor.</p>
<p><a href="https://www.aptech.com/wp-content/uploads/2018/11/gblog-golf-launch-vs-ballspeed.png"><img src="https://www.aptech.com/wp-content/uploads/2018/11/gblog-golf-launch-vs-ballspeed.png" alt="Graph of golf ball flight ball speed vs launch angle." width="500" height="350" class="aligncenter size-full wp-image-18459" /></a></p>
<h2 id="the-data">The data</h2>
<p>Our dataset, <a href="https://www.aptech.com/wp-content/uploads/2018/11/golf_ballflight.csv">golf_ballflight.csv</a>, contains 46 observations with the following variables:</p>
<ul>
<li>club_speed - The speed of the clubhead at impact.</li>
<li>ball_speed - The initial speed of the golf ball after impact.</li>
<li>launch - The angle at which the ball takes off relative to the ground in degrees.</li>
<li>back_spin - The spin around the horizontal axis.</li>
<li>carry - The distance the ball travels in the air in yards.</li>
</ul>
<h2 id="the-model">The model</h2>
<p>    <!-- MathJax configuration -->
    <style>
        .mjx-svg-href {
            fill: "inherit" !important;
            stroke: "inherit" !important;
        }
    </style>
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "AMS"} } });
    </script>
    <script type="text/javascript">
window.MathJax = {
  tex2jax: {
    inlineMath: [ ['$','$'] ],
    displayMath: [ ['$$','$$'] ],
    processEscapes: true,
    processEnvironments: true
  },
  // Center justify equations in code and markdown cells. Elsewhere
  // we use CSS to left justify single line equations in code cells.
  displayAlign: 'center',
  "HTML-CSS": {
    styles: {'.MathJax_Display': {"margin": 0}},
    linebreaks: { automatic: false }
  },
  "SVG": {
    styles: {'.MathJax_SVG_Display': {"margin": 0}},
    linebreaks: { automatic: false }
  },
  showProcessingMessages: false,
  messageStyle: "none",
  menuSettings: { zoom: "Click" },
  AuthorInit: function() {
    MathJax.Hub.Register.StartupHook("End", function() {
            var timeout = false, // holder for timeout id
            delay = 250; // delay after event is "complete" to run callback
            var shrinkMath = function() {
              //var dispFormulas = document.getElementsByClassName("formula");
              var dispFormulas = document.getElementsByClassName("MathJax_SVG_Display");
              if (dispFormulas){
                // caculate relative size of indentation
                var contentTest = document.getElementsByTagName("body")[0];
                var nodesWidth = contentTest.offsetWidth;
                // if you have indentation
                var mathIndent = MathJax.Hub.config.displayIndent; //assuming px's
                var mathIndentValue = mathIndent.substring(0,mathIndent.length - 2);
                for (var i=0; i<dispFormulas.length; i++){
                  var dispFormula = dispFormulas[i];
                  var wrapper = dispFormula;
                  //var wrapper = dispFormula.getElementsByClassName("MathJax_Preview")[0].nextSibling;
                  var child = wrapper.firstChild;
                  wrapper.style.transformOrigin = "center"; //or top-left if you left-align your equations
                  var oldScale = child.style.transform;
                  //var newValue = Math.min(0.80*dispFormula.offsetWidth / child.offsetWidth,1.0).toFixed(2);
                  var newValue = Math.min(dispFormula.offsetWidth / child.offsetWidth,1.0).toFixed(2);
                  var newScale = "scale(" + newValue + ")";
                  if(newValue != "NaN" && !(newScale === oldScale)){
                    wrapper.style.transform = newScale;
                    wrapper.style["margin-left"]= Math.pow(newValue,4)*mathIndentValue + "px";
                    var wrapperStyle = window.getComputedStyle(wrapper);
                    var wrapperHeight = parseFloat(wrapperStyle.height);
                    wrapper.style.height = "" + (wrapperHeight * newValue) + "px";
                    if(newValue === "1.00"){
                      wrapper.style.cursor = "";
                      wrapper.style.height = "";
                    }
                    else {
                      wrapper.style.cursor = "zoom-in";
                    }
                  }

                }
            }
            };
            shrinkMath();
            window.addEventListener('resize', function() {
              clearTimeout(timeout);
              timeout = setTimeout(shrinkMath, delay);
            });
          });
  }
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS_SVG"></script>
To keep things simpler, we will not estimate a constant term.
$$ carry = \beta_1 club\_speed + \beta_2 ball\_speed + \beta_3 launch + \beta_4 back\_spin $$</p>
<h2 id="the-code">The code</h2>
<pre class="hljs-container hljs-container-solo"><code class="lang-gauss">fname = "golf_ballflight.csv";

// Load the dependent variable
y = loadd(fname, "carry");

// Load all variables except 'carry'
X = loadd(fname, ". -carry");

// Compute least squares estimates
XTX = X'X;
XXI = invpd(XTX);
b_hat = XXI * X'y;</code></pre>
<p>Running the code above, returns the error <code>G0121 : Matrix not positive definite</code> from line 11, <code>XXI = invpd(XTX);</code>. If the columns of <code>X</code> are all linearly independent vectors, the result of <code>X'X</code> should be a positive definite matrix.</p>
<h2 id="step-1-check-the-data">Step 1: Check the data</h2>
<p>Since the result of <code>X'X</code> is not positive definite, we have a problem with either <code>X</code> or <code>XTX</code>. The first thing we should do is examine the data to make sure it was loaded correctly.</p>
<p><a href="https://www.aptech.com/wp-content/uploads/2018/11/gblog_golf_view_data.png"><img src="https://www.aptech.com/wp-content/uploads/2018/11/gblog_golf_view_data.png" alt="Examine data in GAUSS." width="742" height="278" class="aligncenter size-full wp-image-18471" /></a></p>
<p>In this case, after a thorough examination, the data appears as we would expect. It matches our CSV dataset and we do not see any infinities or missing values.</p>
<h2 id="step-2-check-for-linear-dependence">Step 2: Check for linear dependence</h2>
<p>Now that we have verified that our data was loaded correctly, we need to check our data for linear dependencies.</p>
<p>We can use the GAUSS function <code>qre</code>. This function returns a pivoted <code>R</code> matrix from the QR decomposition. Nearly zero elements on the diagonal of <code>R</code> indicate a linearly dependent vector.</p>
<pre class="hljs-container hljs-container-solo"><code class="lang-gauss">// Compute the pivoted R matrix
// and the permutation vector, 'P'
{ R, P } = qre(X);

print P~diag(R);</code></pre>
<p>Running the code above will produce the following output:</p>
<pre>4.000       -27522.553
2.000        269.89232
3.000       -24.417453
1.000   -1.1589374e-13 </pre>
<p>The right column contains the elements from the diagonal of the <code>R</code> matrix. The permutation vector in the left column shows us which column from the original matrix, the diagonal element of <code>R</code> corresponds to.</p>
<p>The number -1.1589374e-13 to the right of 1.000 tells us that the first column of <code>X</code> is linearly dependent on another column in <code>X</code>.</p>
<h3 id="which-column-is-x_1-linearly-dependent-on">Which column is $X_1$ linearly dependent on?</h3>
<p>We can find which column $X_1$ is linearly dependent on by performing a regression with parts of the <code>R</code> matrix.</p>
<table>
<tr><th>P</th><th colspan="4">R</th></tr>
<tr><td>4</td><td style="text-align: right; border: 1px solid blue">-27522.56</td><td style="text-align: right; border: 1px solid blue">-676.7</td><td style="text-align: right; border: 1px solid blue">-102.46</td><td style="text-align: right; border: 1px solid orange">-466.7</td></tr>

<tr><td>2</td><td style="text-align: right; border: 1px solid blue">0</td><td style="text-align: right; border: 1px solid blue">269.9</td><td style="text-align: right; border: 1px solid blue">10.23</td><td style="text-align: right; border: 1px solid orange">186.13</td></tr>

<tr><td>3</td><td style="text-align: right; border: 1px solid blue">0</td><td style="text-align: right; border: 1px solid blue">0</td><td style="text-align: right; border: 1px solid blue">-24.42</td><td style="text-align: right; border: 1px solid orange">-6.82e-15</td></tr> 

<tr><td>1</td><td style="text-align: right">0</td><td style="text-align: right">0</td><td style="text-align: right">0</td><td style="text-align: right">-1.159e-13</td></tr> 
</table>
<p>The dependent variable for this regression will be the fourth column of <code>R</code>, highlighted in orange above. The independent variables will be the first three columns of <code>R</code>, highlighted in blue above. Note that the final row of <code>R</code> is not included. By definition, it will be a row of zeros.</p>
<p>Non-zero parameters in this regression will indicate a linear dependence.</p>
<pre class="hljs-container hljs-container-solo"><code class="lang-gauss">// Create variables corresponding to table above
X_r = R[1:3,1:3];
y_r = R[1:3,4];

// Perform regression
b_r = y_r / X_r;</code></pre>
<p>After running the above, code we see that <code>b_r</code> is equal to:</p>
<pre>3.4695e-18
0.68965517
2.7935e-16</pre>
<p>As we can see, the second parameter is non-zero. Looking at the permutation vector, <code>P</code>, we see that the second column of <code>R</code> corresponds to the second column of <code>X</code>. Therefore, the first and second columns of our <code>X</code> matrix are linearly dependent on each other.</p>
<h2 id="next-steps">Next steps</h2>
<p>We now know that we got the error <code>G0121: Matrix not positive definite</code> because our first two variables, <code>ball_speed</code> and <code>club_speed</code> are linearly dependent on each other. What are our options?</p>
<h3 id="collect-more-data">Collect more data</h3>
<p>In some cases, collecting more observations will resolve the problem.</p>
<h3 id="drop-one-of-the-variables">Drop one of the variables</h3>
<p>If we cannot collect more data, or that does not resolve our problem, we may have to drop one of the variables. In many cases, it will not matter which variable we choose to drop.</p>
<p>In this particular case, some research indicated that the ball launch monitor used to collect the data measures ball speed, but estimates club speed by dividing ball speed by a constant factor, known as efficiency or smash factor. Therefore, we should choose to remove club speed.</p>
<h2 id="final-model">Final model</h2>
<p>Our new code looks like this:</p>
<pre class="hljs-container hljs-container-solo"><code class="lang-gauss">fname = "golf_ballflight.csv";

// Load the dependent variable
y = loadd(fname, "carry");

// Load all variables except 'carry' and 'club_speed'
X = loadd(fname, ". -carry -club_speed");

// Compute least squares estimates
XTX = X'X;
XXI = invpd(XTX);
b_hat = XXI * X'y;</code></pre>
<p>and runs without error.</p>
<h3 id="conclusions">Conclusions</h3>
<p>Great job working through to the end! Today we have learned:</p>
<ol>
<li>Errors <code>G0121: Matrix not positive definite</code> and <code>G0048: Matrix singular</code> can be caused by linear dependencies or bad data.</li>
<li>How to diagnose linear dependencies with the function <code>qre</code>.</li>
</ol>
<p>Code and data from this blog can be found <a href="https://github.com/aptech/gauss_blog/tree/master/programming/singular-matrix-11.25.18">here</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.aptech.com/blog/diagnosing-a-singular-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
