<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://eurisko.us/feed.xml" rel="self" type="application/atom+xml" /><link href="https://eurisko.us/" rel="alternate" type="text/html" /><updated>2023-07-09T14:34:19-07:00</updated><id>https://eurisko.us/feed.xml</id><title type="html">Eurisko</title><author><name>{&quot;name&quot;=&gt;nil, &quot;avatar&quot;=&gt;nil, &quot;bio&quot;=&gt;nil, &quot;location&quot;=&gt;nil, &quot;employer&quot;=&gt;nil, &quot;googlescholar&quot;=&gt;nil, &quot;email&quot;=&gt;nil, &quot;uri&quot;=&gt;nil, &quot;bitbucket&quot;=&gt;nil, &quot;codepen&quot;=&gt;nil, &quot;dribbble&quot;=&gt;nil, &quot;flickr&quot;=&gt;nil, &quot;facebook&quot;=&gt;nil, &quot;foursquare&quot;=&gt;nil, &quot;github&quot;=&gt;nil, &quot;google_plus&quot;=&gt;nil, &quot;keybase&quot;=&gt;nil, &quot;instagram&quot;=&gt;nil, &quot;lastfm&quot;=&gt;nil, &quot;linkedin&quot;=&gt;nil, &quot;orcid&quot;=&gt;nil, &quot;pinterest&quot;=&gt;nil, &quot;soundcloud&quot;=&gt;nil, &quot;stackoverflow&quot;=&gt;nil, &quot;steam&quot;=&gt;nil, &quot;tumblr&quot;=&gt;nil, &quot;twitter&quot;=&gt;nil, &quot;vine&quot;=&gt;nil, &quot;weibo&quot;=&gt;nil, &quot;xing&quot;=&gt;nil, &quot;youtube&quot;=&gt;nil, &quot;wikipedia&quot;=&gt;nil}</name></author><entry><title type="html">Debugging 101</title><link href="https://eurisko.us/debugging-101/" rel="alternate" type="text/html" title="Debugging 101" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/debugging-101</id><content type="html" xml:base="https://eurisko.us/debugging-101/">&lt;p&gt;&lt;i&gt;Note to reader: this post was written before students had access to VS Code and its associated debugging capabilities. Consequently, some valuable debugging tools like breakpoints are not covered in the post.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Debugging can seem like a difficult challenge, but once you understand how to see what’s under the hood, it becomes much simpler. Many of your problems may not actually be problems at all, but simple placement or division errors. Hopefully these 5 tips will help solve all your problems – they have for me!&lt;/p&gt;

&lt;h2&gt;Tip 1: Look at the Error&lt;/h2&gt;

&lt;p&gt;If something is wrong, the error will give you the line, file, and sometimes even tell you how it went wrong. This is the most helpful for silly errors as the console will tell you if something is not defined, the wrong type, dividing by 0, etc. Once you know the error, it’ll either be an easy fix or a big problem.&lt;/p&gt;

&lt;p&gt;For definition/attribute errors, check the variable names and look around where they are defined. You might have misspelled the name by accident or defined the variable after you called it or the function it’s in. Other than that, check the lines around the error and make sure your parentheses, brackets, curly brackets, quotes, and apostrophes are closed.&lt;/p&gt;

&lt;h3&gt;Example 1.1: Misspelled, Missing and Misplaced Items&lt;/h3&gt;

&lt;p&gt;Consider the code below. This is the proper version that will output 6 with no errors.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;] one &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;] five &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;5&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; one &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; five
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, consider the following three snippets. The first snippet has a misspelling.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;] one &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;] fove &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;5&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; one &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; five
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The second snippet has a missing item.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;] one &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;] 
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; one &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; five
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The third snippet has a misplaced item.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;] one &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;] 
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;] &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; one &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; five
[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;4&lt;/span&gt;] five &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;All of these have the same error code:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;File &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;file_name.py&amp;quot;&lt;/span&gt;, line &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;, &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;&lt;/span&gt;module&lt;span style=&quot;color: #333333&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; one &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; five
&lt;span style=&quot;color: #FF0000; font-weight: bold&quot;&gt;NameError&lt;/span&gt;: name &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;five&amp;#39;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;is&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;not&lt;/span&gt; defined
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Anything having to do with a name will be a Name Error. Attribute errors (these have to do with classes) also have the above causes.&lt;/p&gt;

&lt;h3&gt;Example 1.2: Wrong Types&lt;/h3&gt;

&lt;p&gt;Type errors are very simple: they occur when you try to put two things together that don’t go together. The two kinds I see most often are as follows.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Any code of the form &lt;code&gt;'string' + number&lt;/code&gt; will have an error code &lt;code&gt;TypeError: can only concatenate str (not &quot;int&quot;) to str&lt;/code&gt;. With strings, the $+$ will put two strings together. But if the second variable is an integer and not a string, the console will print that it cannot concatenate str with integers. If it was a different kind of variable, the console would say what the variable type is in the quotation marks.&lt;/li&gt;
  &lt;li&gt;Any code of the form &lt;code&gt;None + number&lt;/code&gt; will have an error code &lt;code&gt;TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'&lt;/code&gt;. This is saying that there is no way for these two types to combine using the $+$ sign, and then displaying the two types you are trying to use. The types will come up in order of which came first in the code. For these two errors, you just have to find a way so that this doesn't happen. Maybe making an if statement or changing the type first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Example 1.3: Zero Division&lt;/h3&gt;

&lt;p&gt;This is really self explanatory. Anytime you wind up dividing by zero, the console will spit out &lt;code&gt;ZeroDivisionError: division by zero&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Example 1.4: Syntax Error&lt;/h3&gt;

&lt;p&gt;Syntax Errors are your missing ending or starting quotations, parenthesis, brackets, curly brackets, etc, or just when something is typed wrong and is making the computer confused on what you’re trying to tell it to do. The error code usually says where the syntax is going wrong, so you can just head to that area and fix the syntax. If you don’t understand the error code, go to tip 4.&lt;/p&gt;

&lt;h2&gt;Tip 2: Look Under the Hood&lt;/h2&gt;

&lt;p&gt;There are times where no error is thrown, but you still aren’t getting the right output from your code. During times like this it is best to print out your variables. Printing out your variables can also help show if you are in an infinite while loop, as those will just go forever.&lt;/p&gt;

&lt;p&gt;In general, I start by following the returned variable (abbreviated RV) from its initialization to its return to see exactly what happens to it. If the RV is based off of one or more intermediate variables, check first to see if the RV was ever correct in the first place. If not, just keep on backtracking until you get to a variable that IS computed correctly.&lt;/p&gt;

&lt;p&gt;Tip 3 might be helpful here depending on how long your code is. Closely monitor and double check every way that variable is changed until the RV is finally correct. Now to double check that everything is working properly, try to run your function with a different set of data. You should know what the correct answer should be and the steps between before you try.&lt;/p&gt;

&lt;h3&gt;Example 2.1: Infinite While Loops&lt;/h3&gt;

&lt;p&gt;A “while loop” is a kind of loop that will keep on restarting as long as its condition is true. Infinite while loops are created when the condition is never false (when it hits the end of each run through), if there is no “break” command, or if the “break” command is never triggered.&lt;/p&gt;

&lt;p&gt;You can check if your loop is infinite by printing out the values of any variables involved in the condition. If things start going too fast for you to read, you can import the &lt;code&gt;time&lt;/code&gt; library and use &lt;code&gt;time.sleep(number of seconds)&lt;/code&gt; to make the code pause before continuing.&lt;/p&gt;

&lt;p&gt;If the condition variables go past the stopping condition, check what values they take at the end of the looped code. You may need to make it a break condition instead (just the “break” command inside an if statement). If the condition variable is cycling, check what it is each time you change it. You may have a logical problem in your code.&lt;/p&gt;

&lt;h2&gt;Tip 3: Separate into Chunks&lt;/h2&gt;

&lt;p&gt;As your coding gets more advanced, the code you write generally gets longer. Longer code is more difficult to debug, so if there is any code that you don’t actually need, delete it. This reduces how much code you have to look through when something goes wrong. Of course, don’t simplify to the point that you can’t read your code, but try to remove as many unnecessary things as possible.&lt;/p&gt;

&lt;p&gt;Second, generalize and create helper functions and child classes when possible. In general, try to break up your code so that in the long run there is less code to go through each time something breaks.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;i&gt;Helper functions&lt;/i&gt; are functions that carry out sub-tasks involved in your main function. I personally like to create helper functions for things that will be run over and over or things that are long and complex. For example, in almost all of my various graph classes, &lt;code&gt;set_breadth_first_distance and_previous&lt;/code&gt; is a long, semi-complex bit of code that is run multiple times and is just part of what the main function accomplishes. We call this in &lt;code&gt;calc_distance&lt;/code&gt; and &lt;code&gt;calc_shortest_path&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A &lt;i&gt;child class&lt;/i&gt; gains access to all of the parent class's methods. This means that you can state one or more core methods inside of one class and then create child classes off of it. Each child class now has access to these core methods without having to restate them. Here is how you state a child class: &lt;code&gt;class ChildClassName (ParentClassName): ...&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that your code is hopefully simplified, it’s time to find where things go wrong. You want to separate your code into “chunks” so that it’s more of a sieve. I would suggest making a copy of your code at this point. You find the general area that the code went wrong, and then begin dissecting it. Check each helper function to see if they are returning the proper things, expand out comprehensions and make sure everything is being set properly. Sometimes, everything will be correct except for one variable. Keep tracing back the error by repeating the same process on the area where that variable came from.&lt;/p&gt;

&lt;h2&gt;Tip 4: Look it Up the Error Code&lt;/h2&gt;

&lt;p&gt;I personally find this method VERY helpful for Haskell. Haskell can be very particular in what it accepts, where, and how, as well as not giving the most readable error codes. Let us go into an example from a semi-recent quiz, where all of us had a certain problem.&lt;/p&gt;

&lt;h3&gt;Example 4.1: Looking Up a Haskell Error&lt;/h3&gt;

&lt;p&gt;As you might remember in Quiz 2-3, Problem 2 was a Haskell problem on calculating the GPA Average. The code I turned in went like this:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;calcPoints :: Char &lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt; Int
calcPoints char  
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;A&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;4&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;D&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;F&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;

calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt;([calcPoints x &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;])

calcGPA &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; (calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (length &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;)

main &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;print&lt;/span&gt;( calcGPA [&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;A&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;D&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;F&amp;#39;&lt;/span&gt;] )
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;If you remember this, you know it didn’t work. The first thing I did to fix it was to look at the error code thrown out:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;No instance &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; (Fractional Int) arising &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;a&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;use&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;of&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;/&amp;#39;&lt;/span&gt; \\
In the expression: (calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (length &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;) \\
In an equation &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;calcGPA&amp;#39;&lt;/span&gt;: \\
    calcGPA &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; (calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (length &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;From that I knew the problem was in &lt;code&gt;calcGPA&lt;/code&gt;, but I checked &lt;code&gt;calcTotalPoints&lt;/code&gt; just in case (it was fine). There was nothing else to do but look up the error code, so I copied and pasted the first line into the Google search engine.&lt;/p&gt;

&lt;p&gt;I would recommend selecting one of the results from Stack Overflow, a response page where people put out questions on coding and others will respond. The answers and tips on there are usually pretty good and the people usually explain why something is wrong or why they think their version is better.&lt;/p&gt;

&lt;p&gt;From the first link that I found, one of the answers explains that this error means that these are Ints, which do not have division when the result would be fractional. Hence Haskell is asking where the Fractional Ints are. We just need to divide these Ints and get a decimal. So now we look up a new search, “haskell how to divide ints and get a decimal”.&lt;/p&gt;

&lt;p&gt;Again choosing a link from Stack Overflow, we see that we need to convert these Ints into Floats BEFORE dividing them, and we can do that with a nice little built in function called &lt;code&gt;fromIntegral (...)&lt;/code&gt;. Now knowing this we can make a few small adjustments to our code and see if it works.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;calcPoints :: Char &lt;span style=&quot;color: #333333&quot;&gt;-&amp;gt;&lt;/span&gt; Int
calcPoints char  
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;A&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;4&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;D&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
    &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; char &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;F&amp;#39;&lt;/span&gt;  &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;  &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;

calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt;([calcPoints x &lt;span style=&quot;color: #333333&quot;&gt;|&lt;/span&gt; x &lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;])

calcGPA &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; fromIntegral(calcTotalPoints &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; fromIntegral(length &lt;span style=&quot;color: #007020&quot;&gt;list&lt;/span&gt;)

main &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;print&lt;/span&gt;( calcGPA [&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;A&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;D&amp;#39;&lt;/span&gt;, &lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;F&amp;#39;&lt;/span&gt;] )
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This will return $2.125,$ which is the correct answer for the problem.&lt;/p&gt;

&lt;h2&gt;Tip 5: Ask for Help from Teachers or Peers&lt;/h2&gt;

&lt;p&gt;Sometimes, you really can’t tell what the problem is with your code. You’ve taken it apart, checked everything, it all seems to be working right, but for some reason it just isn’t outputting what it’s supposed to. Now it is time to surrender and ask for someone else to check your code. Sometimes the answer is such a small stupid thing that you never thought to check.&lt;/p&gt;

&lt;p&gt;I would personally recommend asking if your debugging takes more than 45 minutes. You don’t want to annoy other people with silly questions that you could resolve yourself, but you also don’t want to waste time making no progress (even though there is nothing more satisfying than fixing your code after having worked on it for almost 10 hours straight!).&lt;/p&gt;

&lt;p&gt;Now that your code is finished and bug free, get ready to go through the whole debugging process again when you implement the next thing!&lt;/p&gt;</content><author><name>Maia Dimas</name></author><category term="Debugging" /><summary type="html">Note to reader: this post was written before students had access to VS Code and its associated debugging capabilities. Consequently, some valuable debugging tools like breakpoints are not covered in the post.</summary></entry><entry><title type="html">Depth-First and Breadth-First Search</title><link href="https://eurisko.us/depth-first-and-breadth-first-search/" rel="alternate" type="text/html" title="Depth-First and Breadth-First Search" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/depth-first-and-breadth-first-search</id><content type="html" xml:base="https://eurisko.us/depth-first-and-breadth-first-search/">&lt;p&gt;To understand what depth-first and breadth-first search are, we must first know what graphs and directed graphs are. A &lt;b&gt;graph&lt;/b&gt; is a set of objects (often called nodes) that are connected to each other. A pair of nodes is called an &lt;b&gt;edge&lt;/b&gt;, and edges make up the “structure” of a graph. Put simply, a graph is a set of nodes and edges. A &lt;b&gt;directed graph&lt;/b&gt; is a special instance of a graph. The difference is that in a regular (undirected) graph, the edges are bidirectional, meaning that they don’t have a specific direction. However, in a directed graph, each edge has a specified direction.&lt;/p&gt;

&lt;p&gt;Graphs and directed graphs can be used to model many real-life phenomena. For example, a graph can model a social network since communication between social accounts goes both ways. On the other hand, a directed graph can model cities connected by railroad tracks since each railroad track has a specific direction.&lt;/p&gt;

&lt;h2&gt;Examples of Graphs&lt;/h2&gt;

&lt;p&gt;The following picture is an example of an undirected graph:&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/depth-first-and-breadth-first-search-1-undirected-graph.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Here, the labeled circles represent nodes and the lines between the nodes represent undirected edges.&lt;/p&gt;

&lt;p&gt;In the context of a social network, think of node 1 as yourself. The nodes 2, 7, and 8 would be your friends. The nodes 3 and 6 would be the friends of 2 and the nodes 9 and 12 would be the friends of 8. Since no other nodes are connected to 7, you are node 7’s only friend. By the same logic, nodes 4 and 5 are friends of 3 while nodes 9 and 12 are friends of 8.&lt;/p&gt;

&lt;p&gt;The following picture is an example of a directed graph:&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/depth-first-and-breadth-first-search-2-directed-graph.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Again, the nodes are labeled circles and the edges are the lines between the nodes. However, notice that these edges have an arrow designating the direction.&lt;/p&gt;

&lt;p&gt;In the context of cities connected by railroad tracks, node A would be your home city. The directed edge $A \to B$ represents a railroad that goes from city A to city B (but does not go back to city A). From city A, we can only go to city B. From city B, we can only go to city C (since the edge connecting B and D is $D \to B$, not $B \to D$). City C can only go to city E. When we get to city E, we can either go to city D (which in turn will allow us to loop back to city A city B) or city F (from which we can go to city D).&lt;/p&gt;

&lt;h2&gt;Depth-First and Breadth-First Search&lt;/h2&gt;

&lt;p&gt;Now that we know what graphs and directed graphs are, we can introduce depth-first and breadth-first search. As implied by the name, these methods traverse the graph by prioritizing either depth or breadth.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Depth-first search&lt;/b&gt; prioritizes depth. Starting at some node in the graph, we travel along one of the edges that connects to this node. This edge takes us to a new node, and we repeat the process, traveling along one of the edges that connects to the new node. This takes us deeper and deeper into the graph. Once we reach a node for which there are no more edges that we can travel along, we backtrack and go back up the graph until there is a path that we haven’t taken already. We then go deep down the path as far as possible, and then repeat the process until we’ve visited each and every node in the graph.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Breadth-first search&lt;/b&gt; prioritizes breadth. Instead of repeatedly traveling deep into the graph, breadth-first search has us traveling through each “layer” of the graph. If we think about a family tree diagram as an example, a “layer” would be a single generation of people. In the case of a graph, a “layer” would be a single generation of nodes, so to speak. In this context, breadth-first searching can be interpreted as traversing by “generation”, whereas depth-first searching can be interpreted as traversing by direct “branches” of the family tree.&lt;/p&gt;

&lt;h2&gt;Implementing a Graph Class&lt;/h2&gt;

&lt;p&gt;A graph class is a collection of nodes along with methods for operating on the nodes. Each node has an index and a list of “neighbors” (or “parents”, in the case of a directed graph).&lt;/p&gt;

&lt;p&gt;To initialize a graph, we can pass in a list of edges, where each edge is a tuple of node indices. From the edges list, we can determine all the possible indices of nodes, and then create a node that corresponds to each of those indices.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #BB0066; font-weight: bold&quot;&gt;Graph&lt;/span&gt;:
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, edges):
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;edges &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; edges
        indices &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []

        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; edge &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; edges:
            indices&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(edge[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;])
            indices&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(edge[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;])

        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; [Node(n) &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; n &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;max&lt;/span&gt;(indices) &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;)]
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now that we have initialized our graph, we are now able to build it. Building a graph involves setting the neighbors of each node.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;build_from_edges&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;):
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; edge &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;edges:
    	i &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; edge[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;]
    	j &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; edge[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;]
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[i]&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;neighbors&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[j])
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[j]&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;neighbors&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[i])
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Implementing Depth-First and Breadth-First Search&lt;/h2&gt;

&lt;p&gt;To implement a breadth-first search, we use a queue, a data structure that has elements inserted and removed according to the first-in first-out principle. Every time we visit a node, we want to add it to our queue if we haven’t visited it already. Our queue keeps track of which nodes we still need to “deal with”, so to speak. When we “deal with” a node, we add the node to our output array, add the node’s unvisited neighbors to the queue, and then finally remove the node from the queue. We continue doing this until there are no more nodes left in the queue.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;fetch_nodes_breadth_first&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, root_index):
    root_node &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[root_index]
    queue &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    queue&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(root_node)

    visited &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; {}
    visited[root_node&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index] &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;True&lt;/span&gt;

    result &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    result&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(root_node)

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;len&lt;/span&gt;(queue) &lt;span style=&quot;color: #333333&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;:
    	node &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; queue[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;]

        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; neighbor &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; node&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;neighbors:
            &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; neighbor&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; visited:
                queue&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(neighbor)
                result&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(neighbor)
                visited[neighbor&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index] &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;True&lt;/span&gt;

        queue &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; queue[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;:]

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; result
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Depth-first search is similar to breadth-first search. The only difference is that instead of a queue, we use a stack, a data structure that has elements inserted and removed according to the first-in last-out principle.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;fetch_nodes_depth_first&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, root_index):
    root_node &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;nodes[root_index]
    stack &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    stack&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(root_node)

    visited &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; {}
    visited[root_node&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index] &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;True&lt;/span&gt;

    result &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    result&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(root_node)

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;len&lt;/span&gt;(queue) &lt;span style=&quot;color: #333333&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;:
    	node &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; stack[&lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;]

        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; neighbor &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; node&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;neighbors:
            &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; neighbor&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; visited:
                stack&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(neighbor)
                result&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(neighbor)
                visited[neighbor&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;index] &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;True&lt;/span&gt;

        stack &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; stack[:&lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;]

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; result
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that there can be different outputs for each of the search methods. In the case of breadth first search, you can switch up the order of elements on the same layer. In the case of depth first search, you can choose different paths to start going down.&lt;/p&gt;</content><author><name>Cayden Lau</name></author><category term="Algorithms" /><summary type="html">To understand what depth-first and breadth-first search are, we must first know what graphs and directed graphs are. A graph is a set of objects (often called nodes) that are connected to each other. A pair of nodes is called an edge, and edges make up the “structure” of a graph. Put simply, a graph is a set of nodes and edges. A directed graph is a special instance of a graph. The difference is that in a regular (undirected) graph, the edges are bidirectional, meaning that they don’t have a specific direction. However, in a directed graph, each edge has a specified direction.</summary></entry><entry><title type="html">Efficiently Computing the Determinant of a Matrix, Part 1: Determinant by Cofactors</title><link href="https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-1-determinant-by-cofactors/" rel="alternate" type="text/html" title="Efficiently Computing the Determinant of a Matrix, Part 1: Determinant by Cofactors" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-1-determinant-by-cofactors</id><content type="html" xml:base="https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-1-determinant-by-cofactors/">&lt;p&gt;&lt;i&gt;Note: This post is part 1 of a 2-part series: &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/2021-06-01-efficiently-computing-the-determinant-of-a-matrix-part-1-determinant-by-cofactors/&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/2021-06-01-efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations/&quot;&gt;part 2&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;The determinant is a number associated with a matrix that can be used to figure out many characteristics of that matrix. You can find the determinant of any square matrix.&lt;/p&gt;

&lt;p&gt;For example, the determinant can be used to tell if a matrix is invertible, and it can be used to perform a change of variables in a higher-order integral. It can also tell us if a system of equations has a unique solution, and has multiple uses in calculus and linear algebra, including defining the characteristic polynomial of a matrix.&lt;/p&gt;

&lt;p&gt;We denote the determinant of a matrix $A$ as $\vert A \vert$ or $\det(A).$&lt;/p&gt;

&lt;p&gt;The determinant of a $1$ by $1$ matrix is the value in the matrix. In the following section, you will learn how to find the determinant of a $2$ by $2$ matrix. After that, you will learn $2$ different methods that you can use to find the determinant of any square matrix. One of those methods (using cofactors) will be very inefficient, whereas the other method (using elementary row operations) will be much more efficient.&lt;/p&gt;

&lt;h2&gt;Determinant of a 2 by 2 Matrix&lt;/h2&gt;

&lt;p&gt;The formula for finding the determinant of a $2$ by $2$ matrix is very simple. For a matrix&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
A = \begin{bmatrix} 
a &amp;amp; b \\
c &amp;amp; d 
\end{bmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;we have&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\text{det}(A) = ad - bc.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;To show how simple finding the determinant of a $2$ by $2$ matrix can be, I’ll provide an example. Let’s say we have the matrix&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
A = \begin{bmatrix}
1 &amp;amp; 2 \\
3 &amp;amp; 4
\end{bmatrix}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Using the formula, the determinant of this matrix is&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\det(A) &amp;amp;= 1 \cdot 4 - 3 \cdot 2 \\
  &amp;amp;= 4 - 6 \\
  &amp;amp;= -2.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Determinant by Cofactors: 3 by 3 Example&lt;/h2&gt;

&lt;p&gt;The cofactor method is a way to find the determinant of a matrix. This method can be used to find the determinant of any $n$ by $n$ matrix, although it is an inefficient method to use when $n$ is not small.&lt;/p&gt;

&lt;p&gt;Here, I’ll demonstrate how to use the cofactor method to find the determinant of the $3$ by $3$ matrix shown below.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
0 &amp;amp; 2 &amp;amp; 4\\
7 &amp;amp; 24 &amp;amp; 1\\
2 &amp;amp; 6 &amp;amp; 8
\end{bmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The first step is to choose which row you want to expand. In general, you want to try to expand the row with the most zeros in it. You will see why in the next step. For this example, I’m going to expand the first row.&lt;/p&gt;

&lt;p&gt;The second step is to expand your chosen row. In this step, we use that fact that the determinant of a $3$ by $3$ matrix is equal to a linear combination of $2$ by $2$ matrices.&lt;/p&gt;

&lt;p&gt;The coefficients of these $2$ by $2$ matrices are the elements of the row that we chose times each element’s sign from the sign chart. The sign chart for a $3$ by $3$ matrix, shown below, dictates whether the sign of an element needs to be flipped in the linear combination.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
+ &amp;amp; - &amp;amp; + \\
- &amp;amp; + &amp;amp; - \\
+ &amp;amp; - &amp;amp; +
\end{bmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the 0, the sign does not matter because $0 = -0$. For the $2$ in the first row, we can see that the corresponding sign is a negative sign. That mean that the sign of $2$ needs to be flipped to be a $-2$. For the $4$ in the first row, we see that the corresponding sign is a positive sign. That means that the sign of $4$ does not need to change.&lt;/p&gt;

&lt;p&gt;The $2$ by $2$ matrices in the linear combination will contain all of the elements in the $3$ by $3$ matrix that aren’t in the same row or column of each of the $2$ by $2$ matrices’ coefficients. To find all of these elements for each element in your chose row, go through each element in your chosen row and use your finger (or imagine doing so) to block out the row and column that contains the element. All elements of the $3$ by $3$ matrix that aren’t covered by your fingers will be in that $2$ by $2$ matrix.&lt;/p&gt;

&lt;p&gt;For the first $2$ by $2$ matrix, the corresponding coefficient is 0, so we can stop here. This is because any $2$ by $2$ matrix times $0$ will be the zero matrix, which we can disregard when doing addition. This is why it is important to choose to expand the row that has the most zeros.&lt;/p&gt;

&lt;p&gt;For the second $2$ by $2$ matrix, the corresponding coefficient is $2$. We see that the $2$ that we are looking for is in the first row and the second column. Blocking out the first row and the second column with our fingers, we see that the $2$ by $2$ matrix will contain the elements $7$, $1$, $2$, and $8$. This gives us the matrix below.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
7 &amp;amp; 1 \\
2 &amp;amp; 8
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the third element in the first row, we use the same strategy from the second element, where we use our fingers to block out all of the elements in the same row as the corresponding coefficient. That coefficient is now $4$, so we see that this $2$ by $2$ matrix will contain the elements $7$, $24$, $2$, and $6$. This gives us the matrix below.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
7 &amp;amp; 24 \\
2 &amp;amp; 6
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Plugging in the $2$ by $2$ matrices, their corresponding coefficients, and the coefficients’ new signs, we have&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
0 &amp;amp; 2 &amp;amp; 4 \\
7 &amp;amp; 24 &amp;amp; 1 \\
2 &amp;amp; 6 &amp;amp; 8
\end{vmatrix}
=
0
-
2 \times \begin{vmatrix}
7 &amp;amp; 1 \\
2 &amp;amp; 8
\end{vmatrix}
+
4 \times \begin{vmatrix}
7 &amp;amp; 24 \\
2 &amp;amp; 6
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;We can now solve each $2$ by $2$ matrix as usual, multiply those determinants by the coefficients, and add them up to find the determinant of the $3$ by $3$ matrix.&lt;/p&gt;

&lt;p&gt;Doing this with our example, we have&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
0 &amp;amp; 2 &amp;amp; 4 \\
7 &amp;amp; 24 &amp;amp; 1 \\
2 &amp;amp; 6 &amp;amp; 8
\end{vmatrix}
&amp;amp;=
0
-
2 \times \begin{vmatrix}
7 &amp;amp; 1 \\
2 &amp;amp; 8
\end{vmatrix}
+
4 \times \begin{vmatrix}
7 &amp;amp; 24 \\
2 &amp;amp; 6
\end{vmatrix} \\
&amp;amp;= -2 \times \left(7 \times 8 - 1 \times 2\right) + 4 \times \left(7 \times 6 - 24 \times 2\right) \\
&amp;amp;= -2 \times 54 + 4 \times -6 \\
&amp;amp;= -132.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This method works for any $n$ by $n$ matrix, where $n$ is a natural number.&lt;/p&gt;

&lt;h2&gt;Determinant by Cofactors: 4 by 4 Example&lt;/h2&gt;

&lt;p&gt;Let’s see how we would use the cofactor method to find the determinant of the following $4$ by $4$ matrix:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
0 &amp;amp; 2 &amp;amp; 4 &amp;amp; 9 \\
7 &amp;amp; 24 &amp;amp; 1 &amp;amp; 14 \\
2 &amp;amp; 6 &amp;amp; 8 &amp;amp; 0 \\
7 &amp;amp; 2 &amp;amp; 8 &amp;amp; 13
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Following the steps above, we choose the first row as the one to expand, because it is tied for the most zeros. Second, we expand the matrix. The coefficients will be $0, 2, 4,$ and $9,$ respectively. When we put the coefficients together with the signs of $+, -, +,$ and $-,$ respectively, we have the new coefficients $0, -2, 4,$ and $-9.$&lt;/p&gt;

&lt;p&gt;For the coefficient $0,$ the corresponding matrix does not matter, because it is multiplied by $0.$&lt;/p&gt;

&lt;p&gt;For the coefficient $-2,$ the corresponding $3$ by $3$ matrix is&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
7 &amp;amp; 1 &amp;amp; 14 \\
2 &amp;amp; 8 &amp;amp; 0 \\
7 &amp;amp; 8 &amp;amp; 13
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the coefficient $4,$ the corresponding $3$ by $3$ matrix is&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
7 &amp;amp; 24 &amp;amp; 14 \\
2 &amp;amp; 6 &amp;amp; 0 \\
7 &amp;amp; 2 &amp;amp; 13
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the coefficient $-9,$ the corresponding $3$ by $3$ matrix is&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
7 &amp;amp; 24 &amp;amp; 1  \\
2 &amp;amp; 6 &amp;amp; 8 \\
7 &amp;amp; 2 &amp;amp; 8
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Therefore, adding the matrices and their coefficients together, we see that&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{vmatrix}
0 &amp;amp; 2 &amp;amp; 4 &amp;amp; 9 \\
7 &amp;amp; 24 &amp;amp; 1 &amp;amp; 14 \\
2 &amp;amp; 6 &amp;amp; 8 &amp;amp; 0 \\
7 &amp;amp; 2 &amp;amp; 8 &amp;amp; 13
\end{vmatrix}
=
0 \cdot *
-
2 \cdot \begin{vmatrix}
7 &amp;amp; 1 &amp;amp; 14 \\
2 &amp;amp; 8 &amp;amp; 0 \\
7 &amp;amp; 8 &amp;amp; 13
\end{vmatrix}
+
4 \cdot \begin{vmatrix}
7 &amp;amp; 24 &amp;amp; 14 \\
2 &amp;amp; 6 &amp;amp; 0 \\
7 &amp;amp; 2 &amp;amp; 13
\end{vmatrix}
-
9 \cdot \begin{vmatrix}
7 &amp;amp; 24 &amp;amp; 1  \\
2 &amp;amp; 6 &amp;amp; 8 \\
7 &amp;amp; 2 &amp;amp; 8
\end{vmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;We can use the same process from before to find the determinant of each $3$ by $3$ matrix, then add them together to find the determinant of the $4$ by $4$ matrix.&lt;/p&gt;

&lt;h2&gt;Implementing Determinant by Cofactors&lt;/h2&gt;

&lt;p&gt;Computing the determinant of a matrix using the cofactor method can take a long time. Instead of doing it all by hand, we can write a program that will do this for us. Below, I have a program that can compute the determinant of a matrix using the cofactor method. This program, technically called a method, is part of an object. In python, the language that I’m using, an object is a collection of data and methods that act of that data. When we set a variable equal to this object and include parameters, the variable becomes a class. When inside the object, we use “self” to refer to this variable. Self has different attributes that we have given it, and can use the methods in the class to manipulate those attributes and give us what we want.&lt;/p&gt;

&lt;p&gt;The method below is from an object that I have created python. This object has one attribute, called “elements”, which is the elements of our matrix. It is really a lists of lists, not an array. The method &lt;code&gt;calc_cofactor_method_determinant()&lt;/code&gt; below takes self and finds the determinant of &lt;code&gt;self.elements&lt;/code&gt; (the “elements” attribute) using the cofactor method. From now on, I may say “the matrix” to refer to &lt;code&gt;self.elements&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To access an element of the matrix, we write &lt;code&gt;self.elements[i][j]&lt;/code&gt;, where &lt;code&gt;i&lt;/code&gt; is the index of the row that the element is in, and &lt;code&gt;j&lt;/code&gt; is the index of the column that the element is in. The indices of lists start from $0$ and increase one at a time. That means the first row/column has index $0$, the second row/column has index $1$, and so on. So if we wanted to access the element in the first column and the first row, we would write &lt;code&gt;self.elements[0][0]&lt;/code&gt;. If we canted to access the element in the third column and the second row of a matrix, we could write &lt;code&gt;self.elements[1][2]&lt;/code&gt;.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;calc_cofactor_method_determinant&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;):
    
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_rows &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_cols:
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;raise&lt;/span&gt; &lt;span style=&quot;color: #FF0000; font-weight: bold&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Cannot take cofactor_method_determinant of a non-square matrix&amp;quot;&lt;/span&gt;)
    
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_rows &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;:
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;]
    
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_rows &lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;2&lt;/span&gt;:
        a, b &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;], &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;][&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;]
        c, d &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;], &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;][&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;]
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; a&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;d &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; b&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;c
    
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;else&lt;/span&gt;:
        ans &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;
        row_index &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; col_index &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_cols):
            
            coefficient_sign &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;**&lt;/span&gt; col_index)
            coefficient &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[row_index][col_index]
            minor &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;calc_minor(row_index, col_index)
            
            ans &lt;span style=&quot;color: #333333&quot;&gt;+=&lt;/span&gt; coefficient_sign &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt; coefficient &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt; minor&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;cofactor_method_determinant()
        
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; ans
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;First, because we can only take the determinant of a square matrix, we need to make sure that the matrix is a square matrix. If &lt;code&gt;self.num_rows&lt;/code&gt; (which counts the number of rows in our matrix) is not equal to &lt;code&gt;self.num_cols&lt;/code&gt; (which counts the number of columns in our matrix), then we raise an exception.&lt;/p&gt;

&lt;p&gt;Then, we check if the matrix is either a $1$ by $1$ matrix or a $2$ by $2$ matrix. We do this because we know the formulas for computing the determinant of these matrices. If the matrix is a $1$ by $1$ matrix, then we return the only element in the matrix. If the matrix is a $2$ by $2$ matrix, then we use the formula discussed in introduction: we set $a, b, c,$ and $d$ to their respective numbers in the matrix and return $ad - bc.$&lt;/p&gt;

&lt;p&gt;If a matrix is not a $1$ by $1$ matrix or a $2$ by $2$ matrix, then the process is more complicated. We start to use the cofactor method here. As usual with the cofactor method, the first step is to choose which row we want to expand. For simplicity, I chose the first row. Next, we loop through the number of columns in the matrix. Before the recursion kicks in, we must compute the first minor, its coefficient, and the coefficient’s sign.&lt;/p&gt;

&lt;p&gt;We compute the sign by raising $-1$ to the index of the current column. If &lt;code&gt;col_index&lt;/code&gt; equals $0$, for example then the sign is $-1^{0} = 1$. When we multiply the coefficient by its sign, the coefficient keeps the same sign, as it should. To access the coefficient, we take the current element, which is at the row index $0$ and the current column index.&lt;/p&gt;

&lt;p&gt;To compute the minor, we use the helper method called &lt;code&gt;calc_minor&lt;/code&gt;. This method takes self, a row index, and a column index, and returns a modified version of self called minor that only has the elements that don’t have the same row index or column index as the parameters of the method.&lt;/p&gt;

&lt;p&gt;Because we are using recursion, we will take the determinant of that minor using the cofactor method. Then, we take the determinant of the minors of that minor. This continues until we have the determinants of all the minors for the first minor of the matrix. Then, we multiply each minor to its corresponding coefficient that that coefficient’s sign, and add this sum to out answer. Then, &lt;code&gt;col_index&lt;/code&gt; increases and we do the whole thing again for the second minor of the matrix.&lt;/p&gt;

&lt;p&gt;Eventually, we loop through all the minors of the matrix. We just return our answer and we are done! We have successfully computed the determinant of a matrix using the cofactor method and the help of a method.&lt;/p&gt;

&lt;h2&gt;Time Complexity&lt;/h2&gt;

&lt;p&gt;When we found the determinant of a $4 \times 4$ matrix, the first step was to expand it into 4 $3 \times 3$ matrices. When we found the determinant of a $3 \times 3$ matrix, the first step was to expand it into 3 $2 \times 2$ matrices. We can see a pattern here: when solving an $n \times n$ matrix, we expand it into $n$ $(n-1) \times (n-1)$ matrices.&lt;/p&gt;

&lt;p&gt;Continuing the expansion, when solving an $n \times n$ matrix, we would have $\dfrac{n!}{2}$ $2 \times 2$ matrices.&lt;/p&gt;

&lt;p&gt;There are three operations per $2 \times 2$ matrix: multiplying the first and fourth elements, multiplying the second and third elements, and subtracting the latter from the former. Given that we have $\dfrac{n!}{2}$ $2 \times 2$ matrices, for the $2 \times 2$ matrices in the expansion of an $n \times n$ matrix, we have $\dfrac{3}{2}n!$ operations.&lt;/p&gt;

&lt;p&gt;Finally, we must multiply our number of operations for all $2 \times 2$ matrices by $2$, because every $2 \times 2$ matrix can have a coefficient, which is one extra operation. This brings our maximum number of operations for finding the determinant of an $n \times n$ matrix to $3 \cdot n!$ operations. This is very inefficient!&lt;/p&gt;

&lt;p&gt;&lt;i&gt;This post is part 1 of a 2-part series. &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/2021-06-01-efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations/&quot;&gt;Click here to continue to part 2.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Charlie Weinberger</name></author><category term="Algorithms" /><summary type="html">Note: This post is part 1 of a 2-part series: part 1, part 2.</summary></entry><entry><title type="html">Efficiently Computing the Determinant of a Matrix, Part 2: Determinant by Elementary Row Operations</title><link href="https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations/" rel="alternate" type="text/html" title="Efficiently Computing the Determinant of a Matrix, Part 2: Determinant by Elementary Row Operations" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations</id><content type="html" xml:base="https://eurisko.us/efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations/">&lt;p&gt;&lt;i&gt;Note: This post is part 2 of a 2-part series: &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/2021-06-01-efficiently-computing-the-determinant-of-a-matrix-part-1-determinant-by-cofactors/&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/2021-06-01-efficiently-computing-the-determinant-of-a-matrix-part-2-determinant-by-elementary-row-operations/&quot;&gt;part 2&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Computing the determinant by hand is often annoying, and using the cofactor method takes more time the larger the matrix. The most efficient way to compute the determinant of a matrix is through using elementary row operations. Through this process, we reduce the matrix to echelon form and take the operations we did on the rows to compute the determinant.&lt;/p&gt;

&lt;p&gt;Our first step is to take any given matrix to echelon form, but we have to keep track of the row operations we apply to be able to use them later when computing the determinant. Below, I have a randomly generated a $3\times 3$ matrix:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
0 &amp;amp; 2 &amp;amp; 4\\
7 &amp;amp; 24 &amp;amp; 1\\
2 &amp;amp; 6 &amp;amp; 8
\end{bmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The frst thing we would do is switch our top and bottom rows ($R_1$ and $R_3$). Then, we would divide our top row by 2 to get our pivot row for column $1$. We’ll need to keep a list of all numbers we divide any row by and the number of times we switch rows.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
1&amp;amp;3&amp;amp;4\\
7&amp;amp;24&amp;amp;1\\
0&amp;amp;2&amp;amp;4
\end{bmatrix} , \text{row divisors:(2), row swaps:(1)}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Next we subtract $7\times R_{1}$ from $R_{2}$. After that we subtract $R_{2}$ from $R_{1}$.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
1&amp;amp;3&amp;amp;4\\
0&amp;amp;3&amp;amp;20\\
0&amp;amp;2&amp;amp;4
\end{bmatrix}, \text{row divisors:(2), row swaps:(1)}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
1&amp;amp;0&amp;amp;-16\\
0&amp;amp;3&amp;amp;-27\\
0&amp;amp;2&amp;amp;4
\end{bmatrix}, \text{row divisors:(2), row swaps:(1)}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now we divide $R_{2}$ by 3, and subtract $2\times R_{2}$ from $R_{3}$. We can also divide $R_{3}$ by $22$ as well.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{bmatrix}
1&amp;amp;0&amp;amp;-16\\
0&amp;amp;1&amp;amp;-9\\
0&amp;amp;0&amp;amp;1
\end{bmatrix},\text{row divisors:(2,3,22), row swaps:(1)} 
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;At this point we can see the matrix does reduce to the identity, so we can stop here. Now with the row divisors and swaps we collected we can use them to calculate the determinant. To get it, we take the product of the divisors and multiply it by $-1$ to the power of the number of row swaps:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
(2\times 3\times 22)\times -1^{1} = -132
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;When computing the determinant using either the cofactor method or by using a calculator, we can see this is actually the determinant.&lt;/p&gt;

&lt;h2&gt;Implementation&lt;/h2&gt;

&lt;p&gt;Below is the actual code for computing the determinant via row operations that I implemented into my matrix class. The first thing it’s doing is making a copy of the matrix, so that we don’t mutate the original matrix. Then we make a variable called &lt;code&gt;factors&lt;/code&gt; that will be what the method returns, and that’s what we multiply our dividing numbers by.&lt;/p&gt;

&lt;p&gt;After, we ensure the matrix is square, otherwise you can’t compute the determinant. We then loop throw each row of the matrix, checking which row is the pivot row, and making sure the pivot row is where its supposed to be (in the position of the next column that needs a pivot). Once we have the pivot row in the correct position, we multiply the &lt;code&gt;factors&lt;/code&gt; variable by the pivot number, and then reduce the column that number is in to $1$’s and $0$’s. Then we move on to the next row. After going through the whole matrix, we return the &lt;code&gt;factors&lt;/code&gt; variable.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;calc_determinant&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;):
    matrix &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;copy()
    factors &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;
    row_index &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_cols &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_rows:
       &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;quot;Non-square matrices have no determinant.&amp;quot;&lt;/span&gt;)

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; col &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;range&lt;/span&gt;(matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;num_cols):
        pivot &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;get_pivot_row(col)

        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; pivot &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;None&lt;/span&gt;:
            &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; pivot &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; row_index:
                matrix &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;swap_rows(row_index, pivot)
                factors &lt;span style=&quot;color: #333333&quot;&gt;*=&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;

            factors &lt;span style=&quot;color: #333333&quot;&gt;*=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;elements[pivot][col]
            matrix &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;normalize_row(row_index)
            matrix &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;clear_above(row_index)
            matrix &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; matrix&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;clear_below(row_index)
            row_index &lt;span style=&quot;color: #333333&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;

        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;else&lt;/span&gt;:
            mult_constant &lt;span style=&quot;color: #333333&quot;&gt;*=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;
            &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;continue&lt;/span&gt;

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; factors
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Time Complexity&lt;/h2&gt;

&lt;p&gt;When using this method to compute the determinant, the computation is relatively fast compared to other methods. Lets assume we have a $n \times n$ matrix that can be taken to echelon form, then we can easily find the maximum amount of computations needed to find the determinant using this method. Assuming that all the entries are not $1$, then for each row, you would divide by a number at some point, so that’s $n$ computations so far. For each row, you will also have to subtract it from all the other rows to make it the pivot row, which is $n-1$ more computations for $n$ rows, which is $n(n-1)$. This gives us $n+n(n-1) = n^2$ total computations.&lt;/p&gt;

&lt;p&gt;This is much more efficient than computing the determinant via the cofactor method, which can take $n!$ operations.&lt;/p&gt;</content><author><name>Nathan Reynoso</name></author><category term="Algorithms" /><summary type="html">Note: This post is part 2 of a 2-part series: part 1, part 2.</summary></entry><entry><title type="html">Learning to Debug</title><link href="https://eurisko.us/learning-to-debug/" rel="alternate" type="text/html" title="Learning to Debug" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/learning-to-debug</id><content type="html" xml:base="https://eurisko.us/learning-to-debug/">&lt;p&gt;&lt;i&gt;Note to reader: this post was written before students had access to VS Code and its associated debugging capabilities. Consequently, some valuable debugging tools like breakpoints are not covered in the post.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;At the beginning of the year, I knew nothing about Python. I learned different methods and syntaxes, but I had no idea what it meant, and if something went wrong, I was practically hopeless. My main source for help would be asking friends - which is a very good resource - though I grew reliant upon them, instead of using them as a tool for help. Looking back now, I realize that understanding what you’re coding - not just how Python works - but also the problem you are trying to solve, is very important. If you don’t know how the code is supposed to work, you can’t debug the issues.&lt;/p&gt;

&lt;h2&gt;Write Clear, Concise Code&lt;/h2&gt;

&lt;p&gt;When I look back at old problem files, it can be hard to understand my thought process in the code, even though it works. When coding, it is extremely important that the code makes sense and is readable, not just for you but also for others to understand. This is crucial when debugging, so that you can easily find and discrepancies in your logic, especially if your code is not returning errors. A core part of having clear code is a systematic variable naming convention. If you are debugging by printing your variables, having good names makes it easy to know what they should be equal to, so you can clearly pick out anything wrong.&lt;/p&gt;

&lt;p&gt;An example of unclear coding is as follows:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;funct&lt;/span&gt;(var, var2):
    number &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;
    number &lt;span style=&quot;color: #333333&quot;&gt;+=&lt;/span&gt; var
    number &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; number &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; var2
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; number
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Though this code would run and return the sum of the two inputs, it can be hard to tell exactly what it’s doing. There are multiple ways that this code could be cleaned up:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;On line 1, the two input variables (&lt;code&gt;var&lt;/code&gt;, &lt;code&gt;var2&lt;/code&gt;) could be defined as &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; (or similar), to show that these are numbers.&lt;/li&gt;
  &lt;li&gt;Function naming is also very important. The name of the function can tell you what it does very easily without even having to look at the code. In this example, &lt;code&gt;funct&lt;/code&gt; explains nothing about the function itself. Instead, &lt;code&gt;calc_sum&lt;/code&gt; would be a better name for the function, as it is clear that you are taking the sum of the two inputs.&lt;/li&gt;
  &lt;li&gt;In the function's body, there are multiple confusions and inconsistencies. The method used to calculate the sum is fine: initialize a variable, add each input, return the variable. However, the execution can be confusing. Initializing the variable as 0 in line 2 is fine, but unnecessary. Lines 3 and 4 use two different methods of variable addition and reassignment, which can be confusing to switch between (line 3's method is typically better, as it is more concise). These lines could also be condensed into a single line.&lt;/li&gt;
  &lt;li&gt;One issue in the code is knowing if you need a variable at all. In this case, &lt;code&gt;number&lt;/code&gt; could be done without, since you can just return the sum without assigning it to an intermediate the variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the changes listed above, here is a better version of the code:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;calc_sum&lt;/span&gt;(num_1, num_2):
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; num_1 &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; num_2
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;It’s much easier to read, and consequently, much easier to debug.&lt;/p&gt;

&lt;h2&gt;Don't Do Too Much on Any Single Line&lt;/h2&gt;

&lt;p&gt;There are often many different solutions to the same problem. Concise solutions are generally better, but not if they make the code messy. For example, list and dictionary comprehensions eliminate the need for a multi-line “for” loop but can become cluttered very easily. Comprehensions can be especially complex when they are nested, where multiple lists/dictionaries are being created within one big one. This can make the code very confusing to understand, especially if the reader has no context of the problem.&lt;/p&gt;

&lt;p&gt;An example of condensed code becoming confusing is as follows:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;get_mod_binary&lt;/span&gt;(n, low_range, high_range):
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; [convert_to_binary(num&lt;span style=&quot;color: #333333&quot;&gt;%&lt;/span&gt;n) &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;range&lt;/span&gt;(low_range, high_range) &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; num&lt;span style=&quot;color: #333333&quot;&gt;%&lt;/span&gt;n&lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;]
        
&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;print&lt;/span&gt;(get_mod_binary(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;5&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;50&lt;/span&gt;))
&lt;span style=&quot;color: #888888&quot;&gt;#[11001, 110010, 1001011, 1100100, 1111101, 10010110, 10101111, 11001000, 11100001]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The above function’s purpose is to take in a number and a range, find all numbers divisible by $n$ in the provided range, and convert the product of $n$ and the divisible numbers to a binary number. The function is confusing to read visually, but to help with the readability, we can break up the list comprehension to show the flow of the problem more clearly:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;get_mod_binary&lt;/span&gt;(n, low_range, high_range):
    binary_list &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; []
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; num &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;range&lt;/span&gt;(low_range, high_range):
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; num&lt;span style=&quot;color: #333333&quot;&gt;%&lt;/span&gt;n&lt;span style=&quot;color: #333333&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;:
            product &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; num&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;n
            binary_list&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;append(convert_to_binary(product))
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; binary_list
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Though the function has more lines, anyone reading your code can understand it better. Another upside to not having overly-condensed code is the ease of the debugging process: it’s much easier to print intermediate steps in spaced-out code than in a compact list comprehension.&lt;/p&gt;

&lt;h2&gt;Use the Right Data Structure&lt;/h2&gt;

&lt;p&gt;Choosing the right structure to store and manipulate your data is very important. Not having the right data structure can make your code very confusing and difficult to work with.&lt;/p&gt;

&lt;p&gt;One example of this is in my SQL parser in my custom DataFrame class. Originally, I stored each word as an item in a list, but this made it very difficult to work with, as it was hard to differentiate between the operations and the inputs to the operations. To solve this issue, I stored the data within a dictionary. This way, I could have the key be the operation and the value be the input to the operation. This allowed me to be able to carry out those operations easier than before, as I changed the data structure.&lt;/p&gt;</content><author><name>William Wallius</name></author><category term="Debugging" /><summary type="html">Note to reader: this post was written before students had access to VS Code and its associated debugging capabilities. Consequently, some valuable debugging tools like breakpoints are not covered in the post.</summary></entry><entry><title type="html">Simulating a Biological Neuron Using the Hodgkin-Huxley Model</title><link href="https://eurisko.us/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model/" rel="alternate" type="text/html" title="Simulating a Biological Neuron Using the Hodgkin-Huxley Model" /><published>2021-06-01T00:00:00-07:00</published><updated>2021-06-01T00:00:00-07:00</updated><id>https://eurisko.us/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model</id><content type="html" xml:base="https://eurisko.us/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model/">&lt;p&gt;To understand the Hodgkin-Huxley model, we must first understand what a neuron is. A neuron is a type of cell that is most prominently found in nerves and the brain, and neurons the primary building blocks of the nervous system. Neurons are connected by synapses, which allow signals to be sent and received rapidly and precisely.&lt;/p&gt;

&lt;p&gt;There are three major types of neurons:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Sensory neurons make sense of the outside world (e.g. touch, pain, vision, hearing, and taste) by sending signals from sensory organs to the brain.&lt;/li&gt;
  &lt;li&gt;Inter-neurons make up most of the neurons in our bodies and brain, and allow us to to think, see, and perceive things.&lt;/li&gt;
  &lt;li&gt;Motor neurons are in charge of contracting muscles so we can move.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most basic structure of a neuron consists of the main cell body (the plasma membrane, nucleus, etc.), dendrites, and an axon. Dendrites allow the neuron to receive signals, while the single axon is how the cell sends signals.&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model-1-neuron-structure.jpg&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Neurons send signals via spikes in electrical activity called &lt;b&gt;action potentials&lt;/b&gt;. Before we jump into modeling action potentials, let’s learn a bit more about them.&lt;/p&gt;

&lt;p&gt;Each neuron has a resting membrane potential around -70 mV (it is negative because the surroundings of the neuron have accumulated ions). As neurotransmitters bind to the receptors, the neuron is depolarized, moving the membrane potential closer to 0 mV. When the membrane potential reaches a certain threshold (around -55 mV for a neuron starting at -70 mV), sodium channels open, allowing many sodium ions into the cell. Rapid depolarization follows, and the membrane potential becomes positive. This influx of positive charge is what is known as the action potential.&lt;/p&gt;

&lt;p&gt;When the action potential reaches its peak, sodium channels close, potassium channels open, and the cell loses membrane potential (repolarization). The drop in membrane potential causes the neuron to become hyperpolarized, and enter a state where it is very difficult to cause the neuron to depolarize again. Eventually the neuron reaches its resting membrane potential again, where it is no longer hyperpolarized.&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model-2-action-potential.jpg&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;The Hodgkin-Huxley Model&lt;/h2&gt;

&lt;p&gt;The Hodgkin-Huxley model won the Nobel Prize in 1963 for Physiology and Medicine, and the prize was awarded to Sir John Carew Eccles, Alan Lloyd Hodgkin, and Andrew Fielding Huxley. These men won the prize for their model of the action potentials of neurons using differential equations.&lt;/p&gt;

&lt;p&gt;The model yields a graph of how electrical stimulus affects the action potential (change in voltage) of a neuron over time. Hodgkin and Huxley derived much of the model from experimentation, and those results combined with physics to yield usable differential equations.&lt;/p&gt;

&lt;p&gt;From physics, we have that the current is proportional to the change in voltage via the capacitance&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
I = C \dfrac{dV}{dt},
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;and from this we can obtain&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\dfrac{dV}{dt} = \dfrac{I}{C}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In neurons, the capacitance is roughly $1$.&lt;/p&gt;

&lt;p&gt;Next the current must be split into parts: the stimulus ($s$), the flux across the sodium and potassium channels ($I_{Na}$, $I_{K}$ respectively), and the current leakage ($I_{L}$). We then turn the previous equation into&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\dfrac{dV}{dt} = \dfrac{1}{C} \left[ s - I_{Na} - I_{K} - I_{L} \right].
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In this model it’s really just&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\dfrac{dV}{dt} = s - I_{Na} - I_{K} - I_{L}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;since $C=1.$&lt;/p&gt;

&lt;p&gt;One thing to keep in mind for all of these equations is that $V$ represents voltage &lt;i&gt;offset&lt;/i&gt; from the resting potential (-70 mV), and not the actual voltage of the neuron. This is done to make graphing and showing the model more simple.&lt;/p&gt;

&lt;p&gt;The current across each channel (including the leakage) is related to the voltage difference relative to that channel’s equilibrium voltage. The proportionality constants were modeled experimentally, and some are written in terms of $n, m,$ and $h,$ which represent the probability of active/inactive channels. The equations can be written as follows (with $V$ representing voltage offset from the resting potential):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;$I_{Na}(V, m, h) = g_{Na}(m,h)(V-V_{Na})$, with equilibrium voltage $V_{Na} = 115$ and proportionality constant $g_{Na}(m,h) = 120m^{3}h$&lt;/li&gt;
  &lt;li&gt;$I_{K}(V, n) = g_{K}(n)(V-V_{K})$, with $V_{K} = -12$ and $g_{K}(n) = 36n^{4}$&lt;/li&gt;
  &lt;li&gt;$I_{L}(V) = 0.3(V-V_{L})$, with $V_{L}=10.6$&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These variables $n, m,$ and $h$ still have to be dealt with. The rates of change for these variables depends on functions of voltage (alphas and betas), and are written as so:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\dfrac{\text dn}{\text dt} &amp;amp;= \alpha_n(V) (1-n) - \beta_n(V) n \\
\dfrac{\text dm}{\text dt} &amp;amp;= \alpha_m(V)(1-m) - \beta_m(V) m \\
\dfrac{\text dh}{\text dt} &amp;amp;= \alpha_h(V) (1-h) - \beta_h(V) h
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The alpha and beta functions are shown here:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\alpha_n(V) &amp;amp;= \dfrac{0.01(10-V)}{\exp \left[ 0.1 (10-V) \right] - 1}, \quad&amp;amp; \alpha_m(V) &amp;amp;= \dfrac{0.1(25-V)}{\exp \left[ 0.1 (25-V) \right] - 1}, \quad&amp;amp; \alpha_h(V) &amp;amp;= 0.07 \exp \left[ -\dfrac{V}{20} \right],  \\
\beta_n(V) &amp;amp;= 0.125 \exp \left[ -\dfrac{V}{80} \right], \quad&amp;amp; \beta_m(V) &amp;amp;= 4 \exp \left[ - \dfrac{V}{18} \right], \quad&amp;amp; \beta_h(V) &amp;amp;= \dfrac{1}{\exp \left[ 0.1( 30-V) \right] + 1}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In this particular example, the stimulus will be provided to the neuron at certain intervals&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
s(t) = \begin{cases}
150, &amp;amp; t \in [10,11] \cup [20,21] \cup [30,40] \cup [50,51] \cup [53,54] \\
 &amp;amp; \phantom{t \in [} \cup [56,57] \cup [59,60] \cup [62,63] \cup [65,66] \\
 0 &amp;amp; \text{otherwise}.
\end{cases}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Lastly, initial values must be dealt with. We have $V_{0} = 0$ (remember, $V$ represents the &lt;i&gt;offset&lt;/i&gt; from the resting membrane potential), and $n_0, m_0, h_0$ can be approximated with by setting $V = 0$ and setting each $n, m, h$ to their asymptotic values:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
n_0 &amp;amp;= \dfrac{\alpha_n(V_0)}{\alpha_n(V_0) + \beta_n(V_0)} \\
m_0 &amp;amp;= \dfrac{\alpha_m(V_0)}{\alpha_m(V_0) + \beta_m(V_0)} \\
h_0 &amp;amp;= \dfrac{\alpha_h(V_0)}{\alpha_h(V_0) + \beta_h(V_0)}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Simulating a Hodgkin-Huxley Neuron&lt;/h2&gt;

&lt;p&gt;Creating the Hodgkin-Huxley model through code first requires a class or function (using the Python coding language in this case) that is able to create graphs with just differential equations. Euler estimation is the method that is used to do just that. At its very core, the Euler estimator takes the rate of change of a function at a point and predicts the next point based on that rate of change and the user-inputted step size (rate of change times step size gives us the change from the original point to the next point). As the step size decreases, the graph becomes more accurate.&lt;/p&gt;

&lt;p&gt;For the Euler estimator used in this case, the user inputs rates of change for each dependent variable, and uses that to calculate the variables needed to graph. Point output is in the form $(t,x),$ where $t$ represents time and $x$ can represent multiple dependent variables via Python dictionary. The estimator will graph time vs. each dependent variable.&lt;/p&gt;

&lt;p&gt;All that is left to do is turn the differentials and functions into Python functions, and use the Euler estimator to graph the model.&lt;/p&gt;

&lt;p&gt;Here are some examples of turning the equations into code. There is one Python function for every “part” of the model: alphas and betas, initial values, currents, etc.&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;alpha_n&lt;/span&gt;(t,x):
    V &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; x[&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;V&amp;#39;&lt;/span&gt;]
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;color: #6600EE; font-weight: bold&quot;&gt;0.01&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;V)) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (math&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;exp(&lt;span style=&quot;color: #6600EE; font-weight: bold&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt;V)) &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;)

n_0 &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; alpha_n(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;,{&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;V&amp;#39;&lt;/span&gt;:&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;}) &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (alpha_n(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;,{&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;V&amp;#39;&lt;/span&gt;:&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;}) &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; beta_n(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;,{&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;V&amp;#39;&lt;/span&gt;:&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;}))

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;I_Na&lt;/span&gt;(t,x):
    V &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; x[&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;V&amp;#39;&lt;/span&gt;]
    m &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; x[&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;m&amp;#39;&lt;/span&gt;]
    h &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; x[&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;h&amp;#39;&lt;/span&gt;]
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;120&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;(m&lt;span style=&quot;color: #333333&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;3&lt;/span&gt;)&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;h&lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt;(V &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; V_Na)

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;stim&lt;/span&gt;(t):
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;t&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;11&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;t&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;21&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;t&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;40&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;t&lt;span style=&quot;color: #333333&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;51&lt;/span&gt; &lt;span style=&quot;color: #888888&quot;&gt;# (etc)&lt;/span&gt;
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;150&lt;/span&gt;
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;0&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;dV_dt&lt;/span&gt;(t,x):
    s &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; stim(t)
    Na_curr &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; I_Na(t,x)
    K_curr &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; I_K(t,x)
    L_curr &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; I_L(t,x)
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt;C) &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt; (s &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; Na_curr &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; K_curr &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; L_curr)
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The end product looks something like this:&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/simulating-a-biological-neural-network-using-the-hodgkin-huxley-model-3-end-product.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2&gt;Analyzing the Model&lt;/h2&gt;

&lt;p&gt;Now that we have the actual model, we can analyze how it relates to all the information about neurons above, as well as what the model could imply.&lt;/p&gt;

&lt;p&gt;If we look at the first burst of stimulus (between $t=10$ and $t=11$), we can see the most basic reaction to stimulus: a sudden spike caused by both depolarization and the stimulus provided (two factors), a more gradual decrease in voltage as the repolarizes (it’s slower than the increase because there is only one factor repolarizing the neuron), and then where the neuron becomes hyperpolarized. This occurs at both $t=20$ and $t=30$ as well, but the example for $t=30$ will be discussed in the following paragraph.&lt;/p&gt;

&lt;p&gt;The prolonged stimulus between $t=30$ and $t=40$ causes something interesting to occur in the neuron. The first thing to be noticed is that the neuron initially repolarizes at a slower rate. This is due to constant voltage being added into the system while the neuron is repolarizing. The reason the voltage doesn’t keep going down is because it about reaches the threshold where the cell can once again depolarize (-55 mV, which would be represented as 15 mV on the graph). The odd half-spike that starts around $t=33$ is the second thing to be analyzed. The reason the voltage through the neuron does not increase as quickly as previous increases is because the neuron is still repolarizing (losing voltage) as the stimulus is providing voltage. And the little spike eventually reaches the point where sodium channels just all close, and depolarization stops.&lt;/p&gt;

&lt;p&gt;The example shown in the preceding paragraph shows what happens when stimulus is constantly being poured into the system. But what happens when stimulus is provided in many short bursts? This question can be answered by analyzing the neuron’s behavior at $t = 50, 53, 56, 59, 62, 65$. The first burst of stimulus looks normal, but since the second burst of stimulus came before the neuron could properly repolarize (and while it was hyperpolarized), the amount of depolarization was very low. The next burst of stimulus (third one) got a better reaction because the neuron was less hyperpolarized, but it was still less than what we would normally expect. The fourth and sixth bursts are similar to the second, and the fifth one to the first and third.&lt;/p&gt;</content><author><name>Justin Hong</name></author><category term="Algorithms" /><category term="Modeling" /><summary type="html">To understand the Hodgkin-Huxley model, we must first understand what a neuron is. A neuron is a type of cell that is most prominently found in nerves and the brain, and neurons the primary building blocks of the nervous system. Neurons are connected by synapses, which allow signals to be sent and received rapidly and precisely.</summary></entry><entry><title type="html">The Ultimate High School Computer Science Sequence: 9 Months In</title><link href="https://eurisko.us/the-ultimate-high-school-computer-science-sequence-9-months-in/" rel="alternate" type="text/html" title="The Ultimate High School Computer Science Sequence: 9 Months In" /><published>2021-02-21T00:00:00-08:00</published><updated>2021-02-21T00:00:00-08:00</updated><id>https://eurisko.us/the-ultimate-high-school-computer-science-sequence-9-months-in</id><content type="html" xml:base="https://eurisko.us/the-ultimate-high-school-computer-science-sequence-9-months-in/">&lt;p&gt;&lt;i&gt;The Eurisko sequence started during the summer of 2020 with an initial cohort of 5 high school students, all aged 15-16 years old and entering their junior year (11th grade). The content of this sequence similar to what would be covered in upper-level undergraduate courses (e.g. data structures/algorithms ranging from linked lists &amp;amp; sorting algorithms to graphs &amp;amp; traversals), and some content may even be beyond (e.g. building a machine learning library in Python from the ground up). The students build everything from scratch: for example, instead of using external libraries like numpy or pandas, the students built their regressors and classifiers on top of matrix and dataframe classes that they wrote themselves.&lt;/i&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Last June, &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/jason-roberts&quot; target=&quot;_blank&quot;&gt;Jason Roberts&lt;/a&gt;, the founder of &lt;a class=&quot;body&quot; href=&quot;https://www.mathacademy.us/&quot; target=&quot;_blank&quot;&gt;Math Academy&lt;/a&gt; and one of the original developers of Uber’s real-time technology, most widely known for coining the term &lt;a class=&quot;body&quot; href=&quot;https://www.google.com/search?q=luck+surface+area&quot; target=&quot;_blank&quot;&gt;“luck surface area”&lt;/a&gt;, asked me teach his son Colby some computer science. Colby was just finishing up his sophomore year in &lt;a class=&quot;body&quot; href=&quot;http://www.theappacademy.us/index.html&quot; target=&quot;_blank&quot;&gt;App Academy&lt;/a&gt;, but Jason felt that the curriculum was not geared towards students who had a strong aptitude in the subject, and that Colby could and should be learning a lot more. Plus, it’s the pandemic, and many extracurriculars have been shut down.&lt;/p&gt;

&lt;p&gt;As I’ve come to expect with Jason, that initial idea grew quickly: he pulled in some of Colby’s buddies who had the necessary mathematical background, and we put together a summer computer science group that met Mon / Wed / Fri with ~10 hours of problem sets each week. Long story short (which I’ll elaborate on in a later post), the kids made progress faster than either of us could have possibly expected, and now App Academy is funding an official high school class in which a second cohort has joined the ranks.&lt;/p&gt;

&lt;p&gt;It’s been 9 months since our initial summer crew (&lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/colby-roberts&quot; target=&quot;_blank&quot;&gt;Colby&lt;/a&gt;, &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/riley-paddock&quot; target=&quot;_blank&quot;&gt;Riley&lt;/a&gt;, &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/george-meza&quot; target=&quot;_blank&quot;&gt;George&lt;/a&gt;, &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/david-gieselman&quot; target=&quot;_blank&quot;&gt;David&lt;/a&gt;, and &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/elijah-tarr&quot; target=&quot;_blank&quot;&gt;Elijah&lt;/a&gt;) first started meeting, and in those 9 months, the students have gone from initially not knowing how to write helper functions, to building a machine learning library (and numerous other things) from scratch. This post is meant to summarize what we’ve done so far and what our plans are for the future.&lt;/p&gt;

&lt;p&gt;All the relevant problem sets, quizzes/tests, and class recordings are documented in the class pages for &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/computation-and-modeling-2020-summer&quot; target=&quot;_blank&quot;&gt;Computation &amp;amp; Modeling (Summer 2020)&lt;/a&gt; and &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/machine-learning-2020-21&quot; target=&quot;_blank&quot;&gt;Machine Learning (2020-21)&lt;/a&gt;. Throughout this post I’ve included links to some of the more noteworthy problems that the students have completed, but any non-linked problems can also be found on those class pages. The students also have GitHub repositories which can be found on their Eurisko pages.&lt;/p&gt;

&lt;p&gt;Lastly, before we dive in, here is a bit of important background information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These students are currently high school juniors (16-17 years old).&lt;/li&gt;
&lt;li&gt;Whenever I say &quot;implemented&quot; or &quot;built&quot;, I mean from scratch. The students aren't allowed to use external libraries. They have to build everything themselves. We've been working primarily in Python (though recently, we've also introduced C++ and Haskell). The students collaborate a lot, but every student writes every problem up on their own. We eat what we kill.&lt;/li&gt;
&lt;li&gt;The students are all mathematically advanced and have learned at least through linear algebra and multivariable calculus. Most are in Math Academy, which means that by 11th grade, they've also learned much of differential equations, discrete math, and abstract algebra.&lt;/li&gt;
&lt;li&gt;Most of the students had very little programming experience prior to Eurisko. Something as simple as checking if a string was a palindrome was not trivial to them. They didn't know how to write classes and helper functions, how to work with dictionaries, or even how to systematically debug their code. They've come a long way in a short time!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Why We're Doing It&lt;/h2&gt;

&lt;p&gt;We want to teach students the art and craft of software development, while simultaneously giving equal weighting to the discipline of formal computer science and leveraging the advanced mathematics that the students have been learning through Math Academy. We also want the course to be very fun.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;i&gt;The art and craft of software development.&lt;/i&gt; We want the students to be good enough to do an internship at a tech company and be productive (as opposed to burdensome). This means they need to know how to write and debug code given high-level requirements, test their code effectively, and use source control like GitHub.&lt;/li&gt;
  &lt;li&gt;&lt;i&gt;Leveraging mathematics.&lt;/i&gt; We're interested in teaching the students a lof of machine learning because frankly, it's super cool, and it builds on their strengths. We want to pull the powerful and interesting tools of mathematics into practical use-cases in the context of modeling and prediction (as opposed to just building CRUD apps).&lt;/li&gt;
   &lt;li&gt;&lt;i&gt;Formal computer science.&lt;/i&gt; In addition to becoming competent software developers, we wanted to teach the students undergraduate-level computer science (data structures / algorithms, programming languages, etc) so that no matter how competitive or rigorous the undergraduate curriculum they encounter, they'll be overprepared and find it easygoing (as opposed to overwhelming/intimidating).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We chose Python as our primary programming language since it’s one of the most productive multi-purpose languages, it’s a great learning language, and these days it’s the lingua franca of machine learning. But we also wanted to expose the students to the basics of multiple programming languages, so we picked C++ and Haskell as two other languages that would stretch the students in other ways. C++ forces them to think in terms of how the machine actually works, whereas Haskell represents a much more abstract idealized conception of computation. C++ and Haskell are also the kinds of languages that the students might run into early in an undergraduate computer science program, and if they’re not prepared, they could easily find themselves frustrated and struggling. So we’re taking preventative measures.&lt;/p&gt;

&lt;p&gt;We’re also having the students learn SQL, since it’s an example of a different category of language: declarative, as opposed to imperative (Python, C++) or functional (Haskell). It also happens to be incredibly useful. In addition to learning SQL, the students will also build their own SQL parser, which will give them further insight into how programming languages are structured and what goes on behind the scenes when you run a program.&lt;/p&gt;

&lt;h2&gt;What We've Done&lt;/h2&gt;

&lt;p&gt;Here is a list of the main topics we’ve covered. We pulled the basics from MIT’s &lt;a class=&quot;body&quot; href=&quot;https://mitpress.mit.edu/books/introduction-computation-and-programming-using-python-second-edition&quot; target=&quot;_blank&quot;&gt;Introduction to Computation and Programming Using Python&lt;/a&gt;, and then added on a bunch of more advanced topics.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Algorithms and Data Structures.&lt;/b&gt; The students have implemented linked lists, stacks, queues, sorting algorithms (selection sort, bubble sort, heapsort, mergesort, quicksort) and have used recursion in many different contexts. They’ve also implemented trees, undirected graphs, directed graphs, and weighted graphs, along with methods for computing depth-first / breadth-first traversals and distance / shortest path between two nodes (i.e. Dijkstra’s algorithm). They’ve also implemented a simple version of backtracking to solve a &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/files/all_problems_iteration_1.html#Problem-44-1&quot; target=&quot;_blank&quot;&gt;magic square&lt;/a&gt; and a mini sudoku puzzle. (Elijah wrote about the magic square problem &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/solving-magic-squares-using-backtracking/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Optimization.&lt;/b&gt; The students have implemented Newton’s method, gradient descent (both single-variable and multivariable), and grid search. They’ve also implemented randomized hill climbers in the context of the 8-queens problem.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Probability and Statistics.&lt;/b&gt; The students have worked with many different types of distributions and have done some &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/files/all_problems_iteration_1.html#Problem-41-1&quot; target=&quot;_blank&quot;&gt;basic Bayesian inference&lt;/a&gt; (for example, if you have a set of numbers randomly selected from below some upper bound, the students can construct a confidence interval for the upper bound). The students also write up their problems in LaTeX (using &lt;a class=&quot;body&quot; href=&quot;https://www.overleaf.com/&quot; target=&quot;_blank&quot;&gt;Overleaf&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Matrix, DataFrame, and EulerEstimator.&lt;/b&gt; The students have built their own Matrix class from scratch, which includes methods to compute RREF, inverse, determinant, etc. They also built a DataFrame class which they use to manipulate datasets, and they built an EulerEstimator class that they use to simulate systems of differential equations.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Machine Learning.&lt;/b&gt; The students have built linear/logistic regressors, a naive Bayes classifier, Gini decision trees, simple random forests, and they have just started building neural networks. These machine learning models all run on top of the Matrix, DataFrame, and Graph classes that the students had previously built. The students have also used these models for some &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/files/all_problems_iteration_1.html#Problem-35-2&quot; target=&quot;_blank&quot;&gt;elementary prediction tasks&lt;/a&gt; that have required the use of dummy variables and interaction terms. (George, Colby, and Riley wrote a 3-part series about linear and logistic regression: &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/&quot; target=&quot;_blank&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot; target=&quot;_blank&quot;&gt;part 2&lt;/a&gt;, and &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/&quot; target=&quot;_blank&quot;&gt;part 3&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Differential Equations.&lt;/b&gt; After building an EulerEstimator from scratch, the students used it to simulate several systems of differential equations: a predator-prey model (which David wrote about &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/predator-prey-modeling-with-euler-estimation/&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;), the SIR epidemiological model, the &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/files/all_problems_iteration_1.html#Problem-52-2&quot; target=&quot;_blank&quot;&gt;Hodgkin-Huxley neuron&lt;/a&gt; (which won the Nobel Prize in the 1960s), and a network of Hodgkin-Huxley neurons connected together.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Object-Oriented Programming.&lt;/b&gt; In addition to implementing numerous classes in the context of algorithms / data structures and machine learning, the students have been implementing the Space Empires board game along with intelligent agents that battle against each other. Space Empires is incredibly rich and complex and will be discussed extensively later in this post.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Programming Languages.&lt;/b&gt; The students have recently started learning C++, Haskell, Shell, and SQL. So far, the exercises they’ve completed have been simple HackerRank-style problems.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Writing.&lt;/b&gt; The students each wrote a blog post last semester (the posts are linked in the categories above). Elijah’s post made it to the front page of Hacker News last weekend (&lt;a class=&quot;body&quot; href=&quot;https://news.ycombinator.com/item?id=26126652&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;Space Empires&lt;/h2&gt;

&lt;p&gt;The Space Empires game is incredibly complicated, which is in part why we chose it as the “big project” for the class. It’s provided the students with extensive practice planning, writing, and debugging code that’s spread over multiple directories and files. The &lt;a class=&quot;body&quot; href=&quot;https://www.gmtgames.com/spaceemp/SE-Rules-4B.pdf&quot; target=&quot;_blank&quot;&gt;rule book&lt;/a&gt; is incredibly dense, but here is the gist of how the game works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are 2 players on a grid. Each player starts with a &quot;home colony&quot; and some initial ships, and their goal is to destroy the opponent's home colony by sending ships to attack it.&lt;/li&gt;
&lt;li&gt;Players have a currency called Construction Points (CPs). Players receive CP income from their home colony on each turn, and they can use these CPs to buy more ships or &quot;technology&quot; for future ships. Technology supplements ships' stats -- for example, buying movement technology would allow ships to move more spaces at once, and buying attack or defense technology would increase the attack of defense strength of ships during combat. Players also have to pay maintenance costs on their ships each round (if they don't pay the maintenance cost for a ship, they lose the ship).&lt;/li&gt;
&lt;li&gt;After both players move their ships, combat occurs at any grid square that contains ships from both players. Combat proceeds in rounds until only one player's ships remain at that spot.&lt;/li&gt;
&lt;li&gt;During each round of combat, a &quot;combat order&quot; is constructed, in which ships are sorted by their attack class. The first ship in the combat order can attack any other ship. A 10-sided die is rolled, and if the attacker's (attack strength + attack technology) minus the defender's (defense strength + defense technology) is less than or equal to the die roll, then a hit is scored. Once a ship sustains a number of hits equal to its hull size, it is destroyed. This procedure is repeated for each ship in the combat order.&lt;/li&gt;
&lt;li&gt;At the end of a round of combat, if there are still ships from both teams left over, another round of combat begins. Combat continues until only one team's ships occupy the square.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many other details. I won’t mention them all here, but here are a few examples to get the point across:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Players can send colonyships to colonize other planets. Then, players can collect CPs and build ships at those colonies.&lt;/li&gt;
&lt;li&gt;The number of ships that a player can build on any given turn is limited by the player's number of shipyards at each colony.&lt;/li&gt;
&lt;li&gt;During combat, if two ships have the same tactics level, then the defending ship attacks first. (The defending ship is the ship that was the first to occupy the grid square).&lt;/li&gt;
&lt;li&gt;Each round of combat starts with &quot;ship screening&quot;, in which a player with more ships is given the opportunity to remove its ships from the combat round (but the number of ships that are left in combat must be at least the number of ships that the opponent has in that square).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our initial approach to implementing the game, we tried to implement the main rules of the game along with a subset of the more interesting details. Along the way, we created a couple simple strategy players to test that our games gave the same results, and it seemed like things were going fine. But once the students built more complex custom strategies and tried to have the custom strategies battle against each other, we ran into tons of edge-cases and details that we hadn’t otherwise considered, and everyone’s game implementations were giving different results. (Each student has their own implementation of the game.) After several weeks of attempting to reconcile the discrepancies in our games, we decided to peel back to a much simpler version of the game, reconcile any discrepancies on that simple version, and gradually work our way back up to the full implementation, continuing to reconcile discrepancies at each “level” along the way.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Level 1.&lt;/b&gt; We started off with the simplest game imaginable: each player has 3 scout units and that’s it. There was no economic phase, no CP, no technology. Level 1 consisted of each player moving their 3 units and engaging in combat, and that’s it. We created several strategy players, matched them up against each other, and engaged in pair coding sessions until all of the discrepancies in the outcomes were resolved.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Level 2.&lt;/b&gt; We extended level 1 by introducing a single economic phase at the very beginning of the game, having players start with 3 shipyards in addition to 3 scouts (these are the normal starting conditions of the game), and allowing players to buy technology and/or more scouts during the single economic phase. This way, players would get some CP income from their home colony and have to make a choice between spending it all on a couple basic scouts, or buying just one scout with upgraded technology. Again, we created several basic strategy players and resolved any discrepancies in the outcomes of matchups.&lt;/p&gt;

&lt;p&gt;Level 2 was also where we started competing with custom strategies. It turned out that the best strategy was to buy as many scouts as possible, wait for the opponent to attack, and then send all of one’s scouts on a direct path to attack the opponent’s home base once the opponent’s scouts had all been destroyed. We’ll call this the “camper” strategy because its units “camp” at the home colony and wait for the opponent to attack first.&lt;/p&gt;

&lt;p&gt;The camper strategy exploited the fact that, when two units of the same tactics level are involved in combat, the defending ship gets to attack first. By waiting for the opponent to travel to the camper’s home colony, the camper was able to attack the opponent first. Additionally, because shipyards at a player’s home colony can engage in combat, the camper not only attacked first, but also had twice as many ships in the initial combat. These advantages gave the camper a much higher probability of winning the initial combat and destroying the opponent’s scouts, which in turn gave the camper a much higher probability of winning the second combat once it sent its scouts to the opponent’s home base.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Level 3.&lt;/b&gt; We’re currently implementing level 3. Level 3 extends level 2 by introducing repeated economic phases. This means that the players get CP income on every turn, and have the opportunity to buy technology and/or more scouts on each turn. The optimal strategy is likely similar to the camper strategy from level 2, but it’s not entirely obvious what the best thing to do is while camping, and when the camper should pull the trigger and rush the opponent.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If the opponent is going to attack the camper quickly, then it's in the camper's best interest to ignore technology and just buy as many scouts as possible. That way, it can outnumber the opponent.&lt;/li&gt;
  &lt;li&gt;If the opponent is going to wait a while before attacking the camper, then it's in the camper's best interest to first buy technology and then buy scouts only after all technology has been upgraded to the maximum. The reasoning for this strategy depends on a couple nuances: 1) because of maintenance costs, a player cannot maintain infinitely many scouts, and 2) a ship inherits the technology from the player at the time of building. This way, the resulting army of scouts will be both maximally large and equipped with maximum technology.&lt;/li&gt;
   &lt;li&gt;If the opponent is going to wait for the camper to attack first, then the camper may be able to exploit the opponent's way of detecting that the camper has attacked. For example, if the opponent attacks right after the camper does, then the camper can just send a single scout over to the opponent (as a sacrifice) and keep the rest of its scouts camped at the home base for the defender-attacks-first advantage. On the other hand, if the opponent refuses to attack until all of the camper's scouts are destroyed, then the camper can repeatedly build an army of scouts and send all but one of them to attack the opponent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’re now at a stage where the optimal strategy is no longer obvious.&lt;/p&gt;

&lt;h2&gt;How Well is it Working?&lt;/h2&gt;

&lt;p&gt;Again, the kids are making progress faster than either Jason or I could have possibly expected. But they’re also having more fun than either Jason or I could have possibly expected, too. Class often runs over time due to interesting discussions surrounding Space Empires, and I have to regularly tell kids to leave class to go to their other classes. The kids are on Slack all the time, and I’ve heard them mention that “it’s the only class that matters.” It’s their hardest class, by far – even compared to their Math Academy classes (where they’re studying upper-division college math), which is in turn far more advanced than their AP classes. But they’re having a blast.&lt;/p&gt;</content><author><name>Justin Skycak</name></author><category term="Eurisko Program" /><summary type="html">The Eurisko sequence started during the summer of 2020 with an initial cohort of 5 high school students, all aged 15-16 years old and entering their junior year (11th grade). The content of this sequence similar to what would be covered in upper-level undergraduate courses (e.g. data structures/algorithms ranging from linked lists &amp;amp; sorting algorithms to graphs &amp;amp; traversals), and some content may even be beyond (e.g. building a machine learning library in Python from the ground up). The students build everything from scratch: for example, instead of using external libraries like numpy or pandas, the students built their regressors and classifiers on top of matrix and dataframe classes that they wrote themselves.</summary></entry><entry><title type="html">Linear and Logistic Regression, Part 1: Understanding the Models</title><link href="https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/" rel="alternate" type="text/html" title="Linear and Logistic Regression, Part 1: Understanding the Models" /><published>2020-12-21T00:00:00-08:00</published><updated>2020-12-21T00:00:00-08:00</updated><id>https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models</id><content type="html" xml:base="https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/">&lt;p&gt;&lt;i&gt;Note: This post is part 1 of a 3-part series: &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot;&gt;part 2&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/&quot;&gt;part 3&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;Regression is when you measure specific data points and fit a function to the trend. This can be used to establish connections between known variables and uncertainties, like the probability of a heart attack occurring via known traits. Another example could be determining the perfect amount of something, like the perfect amount of toppings on a pizza. You can relate the amount of toppings with customer satisfaction and determine an average amount of toppings that would lead to best reviews from customers.&lt;/p&gt;

&lt;p&gt;There are two main types of regression I’m going to talk about, linear and logistic. Linear regression comes in the form of a straight line:&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/linear-and-logistic-regression-part-1-understanding-the-models-1.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Linear regression can be modeled with this equation:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \beta_0 + \beta_1x
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Logistic regression is a form of regression that comes in a sigmoid shape and has an upper and lower limit.  This sigmoid shape starts at the lower limit, but once it increases and goes towards the higher limit, it levels out again, forming the graph’s s-like shape.&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/linear-and-logistic-regression-part-1-understanding-the-models-2.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Logistic regression can be modeled with this equation:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \dfrac{1}{1+e^{\beta x}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This is the standard one-variable equation but both can be changed to fit multiple variables. Linear becomes&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3 +\ldots,
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;and logistic becomes&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \dfrac{1}{1+e^{\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3 + \ldots}}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;These regressions can be used to fit different scenarios. The crucial difference between the regressions is their limits.&lt;/p&gt;

&lt;p&gt;For example, say we want to model the population of a species in a new environment. This would be a clear cut case of using logistic over linear because you can’t have the people of a species grow forever due to environmental constraints. This is where we should choose logistic regression over linear regression.&lt;/p&gt;

&lt;p&gt;Linear regression, on the other hand, doesn’t have bounds and is used to model different scenarios. Say you’re modeling experience with a game versus how many points you can score. The more you practice, the better you get, and the more points you can score. You could also use linear regression to model the yield of crops depending on how much water or fertilizer you use.\newline&lt;/p&gt;

&lt;h2&gt;More on Logistic Regression&lt;/h2&gt;

&lt;p&gt;Logistic regression is also perfect for determining probabilities. The reason you can use it to predict probabilities is due to its limits. Probability must be between $0\%$ and $100\%.$ A specific example of this is the weather. You can determine the chance of rain or sunshine using past weather patterns. Linear regression can’t model probabilities because there are no limits. There isn’t a $200\%$ of something happening.\newline&lt;/p&gt;

&lt;p&gt;Logistic regression also doesn’t have to be in the bounds of 0 to 1, though that is normal. Remember this equation:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \dfrac{1}{1+e^{\beta x}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;If you change the numerator, then that is how you can change the upper limit of your regression.&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \dfrac{U}{1+e^{\beta x}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In the above equation, $U$ is the upper limit. This can be used for, say, a movie rating system that goes from 0 to 10 or 0 to 5 stars.&lt;/p&gt;

&lt;p&gt;Not only can you change the upper bound, but you can also change the lower bound. Here is a generalized formula to fit a logistic regression for bounds of your choice:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = L + \dfrac{U - L}{1+e^{\beta x}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In this equation, $U$ is your upper limit, and $L$ is your lower limit. We would want to change the limits for different scenarios, just like usual.&lt;/p&gt;

&lt;p&gt;For example, say you have a crowd of people and you want to predict the direction in which the crowd will move. This could go from $-180^\circ$ to $180\circ$ where $0^\circ$ represents straight ahead, $-90^\circ$ represents left, and $90^\circ$ represents right. There are many different scenarios in which you wouldn’t want the standard $0$ to $1$ bounds.
&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;This post is part 1 of a 3-part series. &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot;&gt;Click here to continue to part 2.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>George Meza</name></author><category term="Machine Learning" /><summary type="html">Note: This post is part 1 of a 3-part series: part 1, part 2, part 3.</summary></entry><entry><title type="html">Linear and Logistic Regression, Part 2: Fitting the Models</title><link href="https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/" rel="alternate" type="text/html" title="Linear and Logistic Regression, Part 2: Fitting the Models" /><published>2020-12-21T00:00:00-08:00</published><updated>2020-12-21T00:00:00-08:00</updated><id>https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models</id><content type="html" xml:base="https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/">&lt;p&gt;&lt;i&gt;Note: This post is part 2 of a 3-part series: &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot;&gt;part 2&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/&quot;&gt;part 3&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;This is a blog post exploring how to fit linear and logistic regressions. First, note that linear and logistic regressors have different shapes. The shape of linear regression is a line, while the shape of logistic regression is a sigmoid:&lt;/p&gt;

&lt;center&gt;&lt;img src=&quot;https://eurisko.us/images/blog/linear-and-logistic-regression-part-2-fitting-the-models-1.png&quot; style=&quot;border: none; height: 20em;&quot; alt=&quot;icon&quot; /&gt;&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Also note that the same procedure can be used to fit a linear or logistic regressor, because the logistic equation can be rearranged to become a linear one.&lt;/p&gt;

&lt;center&gt;
&lt;table style=&quot;width:80%&quot;&gt;
  &lt;tr&gt;
    &lt;td width=&quot;50%&quot;&gt;&lt;b&gt;&lt;center&gt;Linear Function&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
    &lt;td width=&quot;50%&quot;&gt;&lt;b&gt;&lt;center&gt;Logistic Function&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$\beta_0 + \beta_1  x_1 + \ldots + \beta_n  x_n=y$&lt;/center&gt;&lt;/td&gt;
   &lt;td&gt;&lt;center&gt;$\begin{align*}
\dfrac{1}{1+e^{\beta_0 + \beta_1  x_1 + \ldots + \beta_n  x_n}}&amp;amp;=y \\
  \beta_0 + \beta_1  x_1 + \ldots + \beta_n  x_n &amp;amp;= \ln\left(\dfrac{1}{y}-1\right)
     \end{align*}$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Let $y^\prime = y$ for the case of a linear regression, and $y^\prime = \ln\left(\dfrac{1}{y}-1\right)$ for the case of a logistic regression. Then, we need to fit the regression to the following dataset:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\left\{ \begin{matrix} (x_{11}, &amp;amp; x_{12}, &amp;amp; \ldots &amp;amp; x_{1n}, &amp;amp; y_1^\prime) \\ (x_{21}, &amp;amp; x_{22}, &amp;amp; \ldots &amp;amp; x_{2n}, &amp;amp; y_2^\prime) \\ \vdots &amp;amp; \vdots &amp;amp; \ddots &amp;amp; \vdots &amp;amp; \vdots \\ (x_{m1}, &amp;amp; x_{m2}, &amp;amp; \ldots &amp;amp; x_{mn}, &amp;amp; y_m^\prime) \end{matrix} \right\}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;So, we need to solve the matrix equation&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\begin{pmatrix} 1 &amp;amp; x_{11} &amp;amp; x_{12} &amp;amp; \ldots &amp;amp; x_{1n} \\ 1 &amp;amp; x_{21} &amp;amp; x_{22} &amp;amp; \ldots &amp;amp; x_{2n} \\ \vdots &amp;amp; \vdots &amp;amp; \vdots &amp;amp; \ddots &amp;amp; \vdots \\ 1 &amp;amp; x_{m1} &amp;amp; x_{m2} &amp;amp; \ldots &amp;amp; x_{mn} \end{pmatrix} \begin{pmatrix} \beta_0 \\ \beta_1 \\ \vdots \\ \beta_m \end{pmatrix} &amp;amp;\approx \begin{pmatrix} y_1^\prime \\ y_2^\prime \\ \vdots \\ y_m^\prime \end{pmatrix}.
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;We can put this in equation form and perform operations to isolate $\vec{\beta}\mathbin{:}$&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
    \mathbf{X} \vec{\beta} &amp;amp;\approx \vec{y}\\
    \mathbf{X} \vec{\beta} &amp;amp;\approx \vec{y} \\
    \mathbf{X}^T \mathbf{X} \vec{\beta} &amp;amp;\approx \mathbf{X}^T \vec{y} \\
    \vec{\beta} &amp;amp;\approx \left( \mathbf{X}^T \mathbf{X} \right)^{-1} \mathbf{X}^T \vec{y}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This way of finding $\vec{\beta}$ involves using the pseudoinverse, $\left( \mathbf{X}^T \mathbf{X} \right)^{-1} \mathbf{X}^T.$ A matrix is not invertible unless it is square and we cannot guarantee this for $\mathbf{X},$ so we must take the pseudoinverse. By multiplying a $\mathbf{X}$ by its transpose, we can ensure that the result is square, and therefore, we can compute the inverse. Using the pseudoinverse minimizes the sum of squared error between the desired output  $\vec{y}$ and the actual output  $\mathbf{X}\vec{\beta}.$&lt;/p&gt;

&lt;p&gt;For example, let’s fit a logistic regression to a medical data set&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
[(0, 0, 0.1), (1, 0, 0.2), (0, 2, 0.5), (4,5,0.6)]
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;which takes the form&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
(\textrm{amount medicine A}, \textrm{amount medicine B}, \textrm{survival probability}).
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;This data set is for a new medicine where the first column shows the amount of medicine A and the second medicine B. We have data on how well these medicines did when given to patients in differing amounts.&lt;/p&gt;

&lt;p&gt;We can fit a logistic regression as follows:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\mathbf{X} = \begin{pmatrix} 1 &amp;amp; 0 &amp;amp; 0 \\ 1 &amp;amp; 1 &amp;amp; 0 \\ 1 &amp;amp; 0 &amp;amp; 2 \\ 1 &amp;amp; 4 &amp;amp; 5 \end{pmatrix} \ \ \&amp;amp; \ \ \vec{y} = \begin{pmatrix} \ln \left( \dfrac{1}{0.1} - 1 \right) \\ \ln \left( \dfrac{1}{0.2} - 1 \right) \\ \ln \left( \dfrac{1}{0.5} - 1 \right) \\ \ln \left( \dfrac{1}{0.6} - 1 \right) \end{pmatrix}
    = \begin{pmatrix} \ln \left( 9 \right) \\ \ln \left( 4 \right) \\ \ln \left( 1 \right) \\ \ln \left( \dfrac{2}{3} \right) \end{pmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\vec{\beta} &amp;amp;\approx (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \vec{y} \\
        \vec{\beta} &amp;amp;\approx \left(\begin{pmatrix} 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 1\\ 0 &amp;amp; 1 &amp;amp; 0 &amp;amp; 4 \\ 0 &amp;amp; 0 &amp;amp; 2 &amp;amp; 5 \end{pmatrix} \begin{pmatrix} 1 &amp;amp; 0 &amp;amp; 0 \\ 1 &amp;amp; 1 &amp;amp; 0 \\ 1 &amp;amp; 0 &amp;amp; 2 \\ 1 &amp;amp; 4 &amp;amp; 5 \end{pmatrix}\right)^{-1} \begin{pmatrix} 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 1\\ 0 &amp;amp; 1 &amp;amp; 0 &amp;amp; 4 \\ 0 &amp;amp; 0 &amp;amp; 2 &amp;amp; 5 \end{pmatrix} \begin{pmatrix}\ln \left( 9 \right) \\ \ln \left( 4 \right) \\ \ln \left( 1 \right) \\ \ln \left( \dfrac{2}{3} \right)\end{pmatrix} \\
        \vec{\beta} &amp;amp;\approx \begin{pmatrix} 4 &amp;amp; 5 &amp;amp; 7 \\ 5 &amp;amp; 17 &amp;amp; 20 \\ 7 &amp;amp; 20 &amp;amp; 29 \end{pmatrix} ^{-1} \begin{pmatrix} 1 &amp;amp; 1 &amp;amp; 1 &amp;amp; 1\\ 0 &amp;amp; 1 &amp;amp; 0 &amp;amp; 4 \\ 0 &amp;amp; 0 &amp;amp; 2 &amp;amp; 5 \end{pmatrix} \begin{pmatrix}\ln \left( 9 \right) \\ \ln \left( 4 \right) \\ \ln \left( 1 \right) \\ \ln \left( \dfrac{2}{3} \right)\end{pmatrix} \\
        \vec{\beta} &amp;amp;\approx \begin{pmatrix} 1.567 \\ 0.278 \\ -0.640 \end{pmatrix}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now we know the logistic $\beta$’s which are $\beta_0 = 1.567 \ \&amp;amp; \ \beta_1 = 0.278 \ \&amp;amp; \ \beta_2 = -0.640,$ so we plug in the variables $x_1, \ \&amp;amp; \ x_2$ into the equation:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
     f(x_1,x_2) &amp;amp;=\dfrac{1}{1 + e ^ {\beta_0 + \beta_1  x_1 + \beta_2  x_2}} \\
     &amp;amp;=\dfrac{1}{1 + e ^ {1.567 + 0.278 x_1 -0.640 x_2}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Because very little changes from the linear regressor to the logistic regressor, my Python code for the logistic regressor inherits from the linear regressor class and changes only 2 things: it transforms the $\vec{y}$ using $y’ = \ln \left( \dfrac{1}{y}-1 \right)$ and puts the $\beta$’s in a sigmoid function rather than a linear function.&lt;/p&gt;

&lt;p&gt;First, let’s go through the code for the linear regressor. We start by importing a Matrix class and a Dataframe class that I had written to help process data. Then, we initialize the linear regressor:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;matrix&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;import&lt;/span&gt; Matrix
&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;from&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;dataframe&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;import&lt;/span&gt; DataFrame
&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #0e84b5; font-weight: bold&quot;&gt;math&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #BB0066; font-weight: bold&quot;&gt;LinearRegressor&lt;/span&gt;:
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, dataframe, dependent_variable&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;ratings&amp;#39;&lt;/span&gt;):
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;dependent_variable &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; dependent_variable
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;independent_variables &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; [column &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; column &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; dataframe&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;columns &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;if&lt;/span&gt; column &lt;span style=&quot;color: #333333&quot;&gt;!=&lt;/span&gt; dependent_variable]
        X_dataframe &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; dataframe&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;select&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;columns(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;independent_variables)
        y_dataframe &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; dataframe&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;select_columns([&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;dependent_variable])
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;X &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; Matrix(X_dataframe&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;to_array())
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;y &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; Matrix(X_dataframe&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;to_array())
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;coefficients &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; {}
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The way we would solve to get the $\vec{\beta}$’s is as follows:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;solve_coefficients&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;):
        beta &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; (((&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;X&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;transpose() &lt;span style=&quot;color: #FF0000; background-color: #FFAAAA&quot;&gt;@&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;X)&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;inverse()) &lt;span style=&quot;color: #FF0000; background-color: #FFAAAA&quot;&gt;@&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;X&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;transpose()) &lt;span style=&quot;color: #FF0000; background-color: #FFAAAA&quot;&gt;@&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;y
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;set_coefficients(beta)

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;set_coefficients&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, beta):
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; i, column_name &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;enumerate&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;dependent_variables):
            &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;coefficients[column_name] &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; beta[i]
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;In order to find the actual prediction that the regression with the $\beta$’s, we need to plug the $\beta$’s into the regression function. For the linear regressor, this is just a linear function $f(x_1,\ldots, x_n)=\beta_0 + \beta_1  x_1 + \ldots + \beta_n  x_n.$&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;predict&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, input_dict):
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;regression_function(input_dict)

    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;regression_function&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, input_dict):
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt;([input_dict[key] &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;coefficients[key] &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; key &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; input_dict])
&lt;/pre&gt;&lt;/div&gt;
  &lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the logistic regression, it’s the same process but we need to transform the $y$ values:&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
  &lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #BB0066; font-weight: bold&quot;&gt;LogisticRegressor&lt;/span&gt;(LinearRegressor):
    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;__init__&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, dataframe, dependent_variable&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;ratings&amp;#39;&lt;/span&gt;):
        &lt;span style=&quot;color: #007020&quot;&gt;super&lt;/span&gt;()&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;__init__(dataframe, dependent_variable&lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0&quot;&gt;&amp;#39;ratings&amp;#39;&lt;/span&gt;)
        &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;y &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;y&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;apply(&lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;lambda&lt;/span&gt; y: math&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;log(&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt;y &lt;span style=&quot;color: #333333&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt;))
&lt;/pre&gt;&lt;/div&gt;
  &lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;And we use a different regression function:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
f(x_1,\ldots, x_n)=\dfrac{1}{1+e^{\beta_0 + \beta_1  x_1 + \ldots + \beta_n  x_n}}
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;font size=&quot;3em&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;div style=&quot;background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;&quot;&gt;&lt;pre style=&quot;margin: 0; line-height: 125%&quot;&gt;    &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;def&lt;/span&gt; &lt;span style=&quot;color: #0066BB; font-weight: bold&quot;&gt;regression_function&lt;/span&gt;(&lt;span style=&quot;color: #007020&quot;&gt;self&lt;/span&gt;, input_dict):
        linear_sum &lt;span style=&quot;color: #333333&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #007020&quot;&gt;sum&lt;/span&gt;([gathered_inputs[key] &lt;span style=&quot;color: #333333&quot;&gt;*&lt;/span&gt; coefficients[key] &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;for&lt;/span&gt; key &lt;span style=&quot;color: #000000; font-weight: bold&quot;&gt;in&lt;/span&gt; gathered_inputs])
        &lt;span style=&quot;color: #008800; font-weight: bold&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;/&lt;/span&gt; (&lt;span style=&quot;color: #0000DD; font-weight: bold&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #333333&quot;&gt;+&lt;/span&gt; math&lt;span style=&quot;color: #333333&quot;&gt;.&lt;/span&gt;e &lt;span style=&quot;color: #333333&quot;&gt;**&lt;/span&gt; linear_sum)
&lt;/pre&gt;&lt;/div&gt;
&lt;/font&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;This post is part 2 of a 3-part series. &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/&quot;&gt;Click here to continue to part 3.&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Colby Roberts</name></author><category term="Machine Learning" /><summary type="html">Note: This post is part 2 of a 3-part series: part 1, part 2, part 3.</summary></entry><entry><title type="html">Linear and Logistic Regression, Part 3: Categorical Variables, Interaction Terms, and Nonlinear Transformations of Variables</title><link href="https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/" rel="alternate" type="text/html" title="Linear and Logistic Regression, Part 3: Categorical Variables, Interaction Terms, and Nonlinear Transformations of Variables" /><published>2020-12-21T00:00:00-08:00</published><updated>2020-12-21T00:00:00-08:00</updated><id>https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables</id><content type="html" xml:base="https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/">&lt;p&gt;&lt;i&gt;Note: This post is part 3 of a 3-part series: &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/&quot;&gt;part 1&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot;&gt;part 2&lt;/a&gt;, &lt;a class=&quot;body&quot; target=&quot;_blank&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-3-categorical-variables-interaction-terms-and-nonlinear-transformations-of-variables/&quot;&gt;part 3&lt;/a&gt;.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;This blog post will explore categorical variables, interaction terms, and non-linear transformations variables. All of these topics revolve around linear regression, which is a way of solving for the coefficients of a linear function that best fits a set of data points. If you want to understand how linear regressions work, look at &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-1-understanding-the-models/&quot; target=&quot;_blank&quot;&gt;Part 1&lt;/a&gt; and &lt;a class=&quot;body&quot; href=&quot;https://eurisko.us/linear-and-logistic-regression-part-2-fitting-the-models/&quot; target=&quot;_blank&quot;&gt;Part 2&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Using Linear and Logistic Regression with Categorical Variables&lt;/h2&gt;

&lt;p&gt;For this blog post, you have to understand the general idea of fitting a line to a data set. However, keep an open mind about what the collection of data can be. It is not always as simple as $(x,y)$ coordinates. For most of this post, we will use the example of solving for the rating of a sandwich that can have some number of slices beef, some number of tablespoons of peanut butter, and some condiments (mayo and jelly).&lt;/p&gt;

&lt;p&gt;“Condiments” is a categorical variable because it takes non-numeric values. Each entry is a list of condiments that may include mayo, jelly, both, or neither. In other words, the condiments variable is “categorized” as mayo, jelly, both, or none. An example set of data points is shown below. Each row corresponds to a different sandwich.&lt;/p&gt;

&lt;center&gt;
&lt;table style=&quot;width:80%&quot;&gt;
  &lt;tr&gt;
    &lt;td width=&quot;25%&quot;&gt;&lt;b&gt;&lt;center&gt;beef&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
    &lt;td width=&quot;25%&quot;&gt;&lt;b&gt;&lt;center&gt;pb&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td width=&quot;25%&quot;&gt;&lt;b&gt;&lt;center&gt;condiments&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td width=&quot;25%&quot;&gt;&lt;b&gt;&lt;center&gt;rating&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;[]&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$4$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo', 'jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;[]&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$4$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$8$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo', 'jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;[]&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$9$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo', 'jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;[]&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;['mayo', 'jelly']&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;As you can see here, we have numeric values for beef and peanut butter because the number of slices of beef and tablespoons of peanut butter can vary. But we don’t have a number to plug in the condiments variable. Sometimes there is mayo or jelly in the sandwich, or there isn’t. That is why they aren’t represented by a number but rather by name.&lt;/p&gt;

&lt;p&gt;The way you interpret these categorical variables is true or false, which can be represented numerically as a $1$ or a $0.$ So we merely break down this condiments variable into a mayo variable and a jelly variable, each of which is a $1$ if it appears on the sandwich or a $0$ if it doesn’t.&lt;/p&gt;

&lt;center&gt;
&lt;table style=&quot;width:80%&quot;&gt;
  &lt;tr&gt;
    &lt;td width=&quot;20%&quot;&gt;&lt;b&gt;&lt;center&gt;beef&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
    &lt;td width=&quot;20%&quot;&gt;&lt;b&gt;&lt;center&gt;pb&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td width=&quot;20%&quot;&gt;&lt;b&gt;&lt;center&gt;mayo&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
    &lt;td width=&quot;20%&quot;&gt;&lt;b&gt;&lt;center&gt;jelly&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
        &lt;td width=&quot;20%&quot;&gt;&lt;b&gt;&lt;center&gt;rating&lt;/center&gt;&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$4$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$4$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$8$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$9$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$5$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$1$&lt;/center&gt;&lt;/td&gt;
    &lt;td&gt;&lt;center&gt;$0$&lt;/center&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, our data consists of all numeric values, and we can plug it into a regressor.&lt;/p&gt;

&lt;h2&gt;Interactions Between Variables&lt;/h2&gt;

&lt;p&gt;Interaction terms are additional terms added to regression models to account for when two variables together have an effect that is different from each of them in isolation.&lt;/p&gt;

&lt;p&gt;An example of this when you are trying to fit a model that predicts a the rating of a sandwich with beef or peanut butter. Without interaction terms, our model would be&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \beta_0+\beta_1(\textrm{beef})+\beta_1(\textrm{pb})
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;where $\beta_1$ and $\beta_2$ are coefficients that our regression algorithm calculates.&lt;/p&gt;

&lt;p&gt;Without interaction, the model will say “Well, beef is good, so I’ll give that a positive rating, and peanut butter is good, so I’ll give that a positive rating too.” Then we get to a sandwich with beef and peanut butter, and because there is no interaction, it says “Wow, beef is good, and peanut butter is good so that sandwich will be the best sandwich ever.” Which we both know is not valid.&lt;/p&gt;

&lt;p&gt;When we have a sandwich with peanut butter and beef, we need an interaction term that will recognize that because both peanut butter and beef are present, it is a bad sandwich. The way we do this is by adding a term to our model:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \beta_0+\beta_1(\textrm{beef})+\beta_2(\textrm{pb})+\beta_3(\textrm{beef})(\textrm{pb})
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;When either beef or peanut butter is $0,$ the interaction term will be $0$ and will not influence the rating. However, if we have beef and peanut butter, the interaction term will be able to bring down the rating by a lot.&lt;/p&gt;

&lt;h2&gt;Fitting Non-Linear Data with a Linear Regression&lt;/h2&gt;

&lt;p&gt;An exciting property of linear regressions is that many non-linear models (such as polynomials) can be fit using linear regression. The trick is to use linear regression to solve for the coefficient of a &lt;i&gt;function&lt;/i&gt; of $x$ like seen here:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y =\beta_0 + \beta_1f_1(x_1) + \beta_2f_2(x_2)+ \ldots +\beta_nf_n(x_n) + \beta_kf_k(x_k)
\end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;These $f_n(x)$ can be any function of x such as $x^2,x^3, \ldots, x^n$ which is how we fit polynomials. We can even fit more complex functions like&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \beta_1\sin(x) + \beta_2\ln(x) + \beta_3\sqrt{x}.
  \end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The only constraint is that each term of the function must be of the form $\beta f(x)$. So, we could not regress $y =x^a$ because that would require fitting an exponent, not a coefficient. Linear regression can only solve for the coefficients of a model.&lt;/p&gt;

&lt;p&gt;The way we fit a regression of the form&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y =\beta_0+\beta_1f_1(x_1) + \beta_2f_2(x_2)+...+\beta_nf_n(x_n)
  \end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;is we transform the data using the functions in the model, like so:&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
(x_1,x_2,\ldots,x_n,y) \rightarrow (f_1(x_1), f_2(x_2), \ldots , f_n(x_n), y)
  \end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;The model is a linear function of the transformed data points. So by fitting the altered data to the model, we are solving a linear regression. And when we plug the coefficients back into the model, we will get a function that is the best fit for the original data.&lt;/p&gt;

&lt;p&gt;An important consequence of this is that we can fit a logistic regression using the linear regression algorithm. This is possible because the format of the logistic regression is&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
y = \dfrac{M}{1+e^{\beta_1x_1+\beta_2x_2+...+\beta_nx_n}}
  \end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;which can be rearranged to&lt;/p&gt;

&lt;center&gt;
$\begin{align*}
\beta_1x_1+\beta_2x_2+...+\beta_nx_n = \ln \left( \dfrac{M}{y} - 1 \right)
  \end{align*}$
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;which fits in our category of $\beta \cdot f(x).$&lt;/p&gt;</content><author><name>Riley Paddock</name></author><category term="Machine Learning" /><summary type="html">Note: This post is part 3 of a 3-part series: part 1, part 2, part 3.</summary></entry></feed>