<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Note to Self × mirzo.net</title>
    <link>http://notes.mirzo.net/</link>
    <description>Recent content on Note to Self × mirzo.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <copyright>Mirzo Kanoatov</copyright>
    <lastBuildDate>Tue, 02 Dec 2025 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="http://notes.mirzo.net/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Design of Experiments (DOE): The Full Factorial Tutorial</title>
      <link>http://notes.mirzo.net/post/doe-factorial-tutorial/</link>
      <pubDate>Tue, 02 Dec 2025 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/doe-factorial-tutorial/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/doe-factorial-tutorial/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/doe-factorial-tutorial1_huc96c56c23aad4eae4da8941f2820cea2_1772827_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Design of Experiments (DOE): The Full Factorial Tutorial&#34; title=&#34;Design of Experiments (DOE): The Full Factorial Tutorial&#34;&gt;&lt;/a&gt;


&lt;p&gt;The Full Factorial design is foundational to the theory of Design of Experiments (DOE). Although it is not the most efficient in terms of time or resources, understanding this design is essential before moving on to more advanced and economical techniques. A full factorial experiment tests every combination of factors at the same number of levels, which allows for all main effects and interactions to be measured directly. In practice, however, full factorial designs can quickly become costly and labor intensive when as the number of factors and levels increases. As a result, this design is only well suited for smaller studies.&lt;/p&gt;
&lt;p&gt;In this tutorial, we will walk through a complete example of a full factorial DOE experiment. Using freely available Python libraries, we will generate the design matrix, explore the data, build a regression model, and interpret the results.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Design of Experiments (DOE)&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/doe-overview/&#39;&gt;Overview&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Full Factorial Tutorial&lt;/strong&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;h2 id=&#34;the-python-setup&#34;&gt;The Python Setup&lt;/h2&gt;
&lt;p&gt;Before we begin, let&amp;rsquo;s make sure we have all of the tools that we need. To set up a python environment capable of running the code in this tutorial, use the following YAML setup file for &lt;em&gt;conda&lt;/em&gt;. If you are not familiar, read about setting up reproducible environments with &lt;em&gt;conda&lt;/em&gt; in &lt;a href=&#34;http://notes.mirzo.net/post/managing-conda/&#34;&gt;a previous note&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;name&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;doe&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;channels&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;defaults&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;conda-forge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;dependencies&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;python=3.10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pip&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;anaconda&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pydoe3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once the environment is created, let&amp;rsquo;s start our Python script by importing all of the packages that we&amp;rsquo;ll need:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; pyDOE3 &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; doe
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; numpy &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; np
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; pandas &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; pd
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; matplotlib &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; pyplot &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; plt
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; matplotlib.transforms &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; offset_copy
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; seaborn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sns
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; scipy &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; stats
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; statsmodels.api &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; sm
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; statsmodels.formula.api &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; smf
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; statsmodels.graphics.gofplots &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; qqplot
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; statsmodels.stats.anova &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; anova_lm
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; itertools
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here is what each package is used for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://pydoe3.readthedocs.io/en/latest/&#34;&gt;pyDOE3&lt;/a&gt;&lt;/strong&gt; - generates DOE designs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://numpy.org/&#34;&gt;numpy&lt;/a&gt;&lt;/strong&gt; -  performs efficient numeric calculations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://pandas.pydata.org/&#34;&gt;pandas&lt;/a&gt;&lt;/strong&gt; - helps to wrangle tabular data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://matplotlib.org/&#34;&gt;matplotlib&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href=&#34;https://seaborn.pydata.org/&#34;&gt;seaborn&lt;/a&gt;&lt;/strong&gt; - create plots and statistical visualizations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://scipy.org/&#34;&gt;scipy&lt;/a&gt;&lt;/strong&gt; - offers a variety of scientific tools. Specifically, the &lt;em&gt;stats&lt;/em&gt; sub-module offers statistical functions, including probability distributions and hypothesis tests&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://www.statsmodels.org/stable/index.html&#34;&gt;statsmodels&lt;/a&gt;&lt;/strong&gt; - does a lot of the heavy lifting in creating our statistical model&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;https://docs.python.org/3/library/itertools.html&#34;&gt;itertools&lt;/a&gt;&lt;/strong&gt; - helps to efficiently generate combinations of our factors and levels&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;the-example-scenario&#34;&gt;The Example Scenario&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s imagine an example scenario. A chemist has developed a new reaction process to synthesize a specialty compound. The process had worked well on the small-scale in the lab, but the initial upscaled production batches have shown poor and inconsistent reaction yields. Instead of adjusting one variable at a time, the chemist decides to use a DOE approach to study how process parameters influence the final reaction yields. Wisely, he decides to follow the DOE steps from &lt;a href=&#34;http://notes.mirzo.net/post/doe-overview/&#34;&gt;our overview note&lt;/a&gt; to perform a screening study to uncover the importance of factors that govern the yields of his chemical process.&lt;/p&gt;
&lt;h2 id=&#34;step-1-define-the-objectives&#34;&gt;Step 1: Define the Objectives&lt;/h2&gt;
&lt;p&gt;Carefully considering his current needs, the chemist defines the following objectives for his screening DOE study:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Identify which process parameters significantly affect the reaction yield.&lt;/li&gt;
&lt;li&gt;Understand if any of the process parameters interact with one another.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;step-2-identify-factors-and-levels&#34;&gt;Step 2: Identify Factors and Levels&lt;/h2&gt;
&lt;p&gt;Based on his experience from small-scale development experiments, the chemist believes that five process parameters, or experimental &lt;strong&gt;factors&lt;/strong&gt;, may be responsible for yield fluctuations: &lt;em&gt;precursor concentration&lt;/em&gt;, &lt;em&gt;reaction temperature&lt;/em&gt;, &lt;em&gt;solution pH&lt;/em&gt;, &lt;em&gt;mixing speed&lt;/em&gt;, and &lt;em&gt;reaction time&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Since this is a screening study, he decides to test each factor at two &lt;strong&gt;levels&lt;/strong&gt;, i.e., to capture the low and high settings for each of the process parameters. Again, relying on his past knowledge of the process, he settles on the following values:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Low Setting&lt;/th&gt;
&lt;th&gt;High Setting&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Concentration&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;20 µM&lt;/td&gt;
&lt;td&gt;120 µM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Temperature&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5 °C&lt;/td&gt;
&lt;td&gt;20 °C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;pH&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Mixing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;10 rpm&lt;/td&gt;
&lt;td&gt;50 rpm&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Time&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;3 hours&lt;/td&gt;
&lt;td&gt;12 hours&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Let&amp;rsquo;s record our design parameters in python:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;factors &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;concentration&amp;#39;&lt;/span&gt;: [&lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;120&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;temperature&amp;#39;&lt;/span&gt;: [&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pH&amp;#39;&lt;/span&gt;: [&lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mixing&amp;#39;&lt;/span&gt;: [&lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;time&amp;#39;&lt;/span&gt;: [&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;]}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;units &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;concentration&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;µM&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;temperature&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;°C&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pH&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mixing&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;rpm&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;time&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;h&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mg&amp;#39;&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For convenience, let&amp;rsquo;s create some helper variables and make sure the data is sorted properly.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;main_effects &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; list(factors&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;keys())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;number_of_levels &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; factor &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; main_effects:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# make sure the levels are sorted&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    factors[factor] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sorted(factors[factor]) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# count levels for each factor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    number_of_levels&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;append(len(factors[factor])) 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;step-3-select-a-design&#34;&gt;Step 3: Select a Design&lt;/h2&gt;
&lt;p&gt;The chemist suspects that the poor yields of his process are caused not only by the direct influence of each process parameter, but also through their hidden interactions. Guided by this, he selects a full factorial design to comprehensively test the complete set of combinations of the factor levels. This approach will allow the chemist to quantify both the &lt;strong&gt;main effects&lt;/strong&gt; of each factor and the &lt;strong&gt;interaction effects&lt;/strong&gt; between factors.&lt;/p&gt;
&lt;p&gt;The number of experiments required for a full factorial design increases exponentially, following the relation \( \text{Runs} = \text{Levels}^{\text{Factors}} \). Because of this rapid increase, most practical full factorial experiments use only two levels per factor. In our case, the chemist needs to test \(2^5=32\) conditions, and this is before any replication!&lt;/p&gt;
&lt;p&gt;To create the full factorial design table, we will use the pyDOE3 package.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;design &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; doe&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fullfact(number_of_levels)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;astype(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;int&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we look inside the &lt;code&gt;design&lt;/code&gt; variable at this point, we will see the following numpy array.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;array([[0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [1, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [1, 0, 1, 0, 0],
       [0, 1, 1, 0, 0],
       [1, 1, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [1, 0, 0, 1, 0],
       [0, 1, 0, 1, 0],
       [1, 1, 0, 1, 0],
       [0, 0, 1, 1, 0],
       [1, 0, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [1, 1, 1, 1, 0],
       [0, 0, 0, 0, 1],
       [1, 0, 0, 0, 1],
       [0, 1, 0, 0, 1],
       [1, 1, 0, 0, 1],
       [0, 0, 1, 0, 1],
       [1, 0, 1, 0, 1],
       [0, 1, 1, 0, 1],
       [1, 1, 1, 0, 1],
       [0, 0, 0, 1, 1],
       [1, 0, 0, 1, 1],
       [0, 1, 0, 1, 1],
       [1, 1, 0, 1, 1],
       [0, 0, 1, 1, 1],
       [1, 0, 1, 1, 1],
       [0, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The generated full factorial array represents all 32 combinations of low and high levels (coded as 0, 1) for each of our five factors. To make it a little easier to understand and process, let&amp;rsquo;s convert the coded levels to actual values, and make it into a pretty &lt;em&gt;pandas&lt;/em&gt; table.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;DataFrame(design, columns&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;main_effects)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# convert the coded values to string, to make&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# replacement with actual numerical values &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# less prone to code/value confusion&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;astype(str)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; factor &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; factors:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# replace codes with actual values and convert&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# back to floats.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i, level &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; enumerate(factors[factor]):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        df[factor] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df[factor]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;replace(str(i), float(level))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    df[factor] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df[factor]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;astype(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;float&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which gives us a nice pandas dataframe:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;concentration&lt;/th&gt;
&lt;th&gt;temperature&lt;/th&gt;
&lt;th&gt;pH&lt;/th&gt;
&lt;th&gt;mixing&lt;/th&gt;
&lt;th&gt;time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;We now have our design matrix! But before we begin our experiments, we want to add replication and randomize the order of tests:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Replication&lt;/strong&gt; is the planned repetition of experimental runs under the same conditions used to estimate the natural variability of the process and to improve the precision of our statistical analysis. By including replicates, we can separate the variation caused by measurement noise or uncontrolled factors from the variation caused by the factors we are studying. The chemist decides to perform each experiment in duplicate, increasing the total number of tests to 64.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Randomization&lt;/strong&gt; is equally important because it helps to protect the study from hidden biases. If all runs were performed in a fixed sequence, external influences such as ambient temperature drift, equipment wear, or operator fatigue could create systematic errors. Randomizing the run order ensures that these potential disturbances are spread evenly across all treatment combinations, allowing for a more reliable and unbiased estimate of the true factor effects. Proper &amp;ldquo;blocking&amp;rdquo; and randomization of experiments is an advanced topic that we will revisit in a future note. For now, we will just shuffle all of our conditions in the dataframe.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;replicate_number &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# copy and append the design matrix to itself&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# to add a desired number of replicates&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;replicate_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;DataFrame()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, replicate_number):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    replicate_df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;concat([replicate_df, df])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; replicate_df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reset_index(drop&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Randomize the order&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sample(frac&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, random_state&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;88&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reset_index(drop&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note that during the randomization we seeded our random generator with the number 88. This is done for the sake of reproducibility of this tutorial, but is not needed in practice.&lt;/p&gt;
&lt;h2 id=&#34;step-4-conduct-the-experiments&#34;&gt;Step 4: Conduct the Experiments&lt;/h2&gt;
&lt;p&gt;Now comes the hard part. The chemist needs to go into the production facility and run the 64 experiments he planned. Each experiment produces a &lt;strong&gt;result&lt;/strong&gt; (the yield of the reaction in mg of the compound) which is recorded as the response variable. Once the results are obtained, we import them into our dataframe:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&lt;span style=&#34;color:#ae81ff&#34;&gt;3.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1.5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;8.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.8&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;8.4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;9.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5.0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.5&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;5.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;6.5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1.4&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;9.6&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.8&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;6.9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;6.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.4&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;3.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5.0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;6.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;7.9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;7.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;9.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;1.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;7.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;9.1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;11.1&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;7.0&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.8&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;9.4&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;5.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;6.6&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.7&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.0&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;3.4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;7.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;8.2&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;3.4&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;4.3&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#ae81ff&#34;&gt;8.6&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;9.3&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.5&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;5.8&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;step-5-analyze-the-data&#34;&gt;Step 5: Analyze the Data&lt;/h2&gt;
&lt;h3 id=&#34;exploration&#34;&gt;Exploration&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s explore the data to understand the variance and the distribution of the response values. Before we begin, we should note that a factorial design intentionally creates multimodal response mixes, which means that we should not be expecting a perfectly normal distribution of values in the result dataset. However, a roughly normal response distribution suggests that the experimental system is stable and free of strong skew or outliers, while significantly non-normal data may point to measurement issues, nonlinear effects, or problems with experimental blocking. Detecting these patterns early helps to determine whether any additional data transformations are needed before we start building our statistical model.&lt;/p&gt;
&lt;h4 id=&#34;shapiro-wilk-test&#34;&gt;Shapiro-Wilk Test&lt;/h4&gt;
&lt;p&gt;We will start with the Shapiro–Wilk test, which compares the order of the observed data to the expected values from a perfectly normal distribution. The test sets out with a null hypothesis that the data is normally distributed. A resulting p-value greater than 0.05 confirms this hypothesis, suggesting that the data do not significantly deviate from normality. Conversely, a p-value below 0.05 indicates that the results are not normally distributed.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Shapiro–Wilk p-value: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;shapiro(df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;])&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pvalue&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.2f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code produces &amp;ldquo;&lt;strong&gt;Shapiro–Wilk p-value: 0.0107&lt;/strong&gt;&amp;rdquo;, confirming that our data is not normally distributed, as expected.&lt;/p&gt;
&lt;h4 id=&#34;box-plot&#34;&gt;Box Plot&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s visualize the distribution of our results with a simple box plot.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;boxplot(data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df, y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;, ax&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ax)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;stripplot(data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df, y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;, ax&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ax, jitter&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.6&lt;/span&gt;, c&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;link rel=&#34;stylesheet&#34; href=&#34;http://notes.mirzo.net/css/hugo-easy-gallery.css&#34; /&gt;
&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_result_boxplot2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_result_boxplot2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The plot shows that the results are distributed evenly. All quartiles are approximately of the same size and symmetrically distributed around the median value, i.e., no skewing. There are also no significant outliers. So far, there is not a clear explanation for why the distribution of the data is not normal.&lt;/p&gt;
&lt;h4 id=&#34;distribution-histogram&#34;&gt;Distribution Histogram&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s investigate the &lt;em&gt;modality&lt;/em&gt; of our data using a histogram plot. If our data is normally distributed, it should have a single peak with symmetric tails.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df[[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;]]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(kind&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;hist&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Result Bins&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_result_histogram3.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_result_histogram3.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;It seems our results are bi-modal, as the histogram shows two peaks. Multi-modality of data in DOE occurs when the response variable changes systematically across the levels of experimental factors, producing clusters of results rather than a single smooth distribution. In other words, the two peaks may suggest that that one of the factors in our design dominates all others.&lt;/p&gt;
&lt;h4 id=&#34;normal-q-q-probability-plot&#34;&gt;Normal Q-Q Probability Plot&lt;/h4&gt;
&lt;p&gt;The histogram showed us that our data has systematic groupings, but how far from a normal distribution does it actually stray? For this, let&amp;rsquo;s look at a normal quantile-quantile (Q-Q) probability plot. This plot projects our results on to a diagonal line that represents a perfect normal distribution.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;qqplot(df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;], line&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;s&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Normal Q–Q plot of Response Variable&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_qq_plot_response2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_qq_plot_response2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The slight “zigzag” pattern of the data points around the diagonal reflects the two peaks observed in the histogram. There are also signs of minor tailing at both the low and high ends of the responses. However, the overall distribution of the response variable appears to follow normality quite closely.&lt;/p&gt;
&lt;p&gt;Why does the histogram disagree with the Q-Q plot? Unlike the histogram, which highlights the &lt;em&gt;local structure&lt;/em&gt; of the data, the Q–Q plot compares the cumulative distribution of the data to that of an ideal normal distribution, emphasizing the overall agreement with normality across the full range. In a sense, our systematic sampling of data around the two levels per factor may have created a sampling bias in our experiment, leading to the perceived multi-modality of the results. There is a good chance that if we sampled our design space more randomly or more completely, the histogram of the response would fill out to yield a more symmetric distribution.&lt;/p&gt;
&lt;p&gt;We conclude that our data is reasonably consistent with a normal distribution, and no data transformations are required at this stage.&lt;/p&gt;
&lt;h4 id=&#34;run-order-plot&#34;&gt;Run Order Plot&lt;/h4&gt;
&lt;p&gt;Before we start looking into the effects of our experimental factors, let&amp;rsquo;s check for signs of uncontrolled effects. A run order plot shows the response values in the sequence that the experimental runs were performed. It helps to detect trends or patterns over time, such as instrument drift, temperature changes, or operator effects. Ideally, the points should appear randomly scattered, indicating that the process was stable and that randomization successfully minimized time-related bias.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;scatter(x&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index, y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Run Order&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_ylabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Result&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_run_order2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_run_order2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The variability of our results seems to be consistent throughout the study. There are no upward or downward trends, and the vertical spread of the data does not seem to expand of narrow with the progression of the runs. It seems that our experimental environment did not have any significant uncontrolled factors.&lt;/p&gt;
&lt;h3 id=&#34;ranking-effects-and-interactions&#34;&gt;Ranking Effects and Interactions&lt;/h3&gt;
&lt;p&gt;Now that we&amp;rsquo;ve confirmed that the results are (approximately) normally distributed and free from obvious biases, we can start exploring how each factor influences the reaction yield. Before we start building our statistical model, we will continue exploring the model graphically, to get a sense of the importance of each model parameter.&lt;/p&gt;
&lt;h4 id=&#34;main-effects-plot&#34;&gt;Main Effects Plot&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s plot the average response at each level of a single factor to quickly see which variables have the strongest influence on the result.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;result_mean &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;level_means &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;deltas &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; factor &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; main_effects:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    level_means[factor] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (df[[factor, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;groupby(factor)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;reset_index()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                           &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;rename({factor: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;level&amp;#34;&lt;/span&gt;}, axis&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    deltas[factor] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (level_means[factor]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;max()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; level_means[factor]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;min())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;main_effects &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sorted(list(main_effects),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      key&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;lambda&lt;/span&gt; x: deltas[x],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      reverse&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ncols &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; len(level_means&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;keys())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, axes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         ncols&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ncols,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         nrows&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         sharex&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;False&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         sharey&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i, ax &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; enumerate(axes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;flat):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    factor &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; main_effects[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    dataset &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; level_means[factor]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    delta &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; deltas[factor]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot(dataset[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;level&amp;#39;&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            dataset[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;tab:blue&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            marker&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;o&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            linestyle&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;solid&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;factor&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; (Δ=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;round(float(delta),&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;)&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;factor&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; (&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;units[factor]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;)&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_ylabel(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Mean Result (&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;units[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;result&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;)&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axhline(y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;result_mean, c&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, ls&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;--&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_main_effects3.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_main_effects3.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;Looking at the slopes of the three lines, it seems that &lt;em&gt;concentration&lt;/em&gt; has the highest positive effect on the reaction yield. &lt;em&gt;Mixing&lt;/em&gt; also affects the yield in a positive way, but to a much smaller degree. Lastly, &lt;em&gt;temperature&lt;/em&gt; has a small but negative effect on the reaction yield. &lt;em&gt;Time&lt;/em&gt; and &lt;em&gt;pH&lt;/em&gt; seem to have little influence on the reaction yield.&lt;/p&gt;
&lt;p&gt;This line-based presentation is common in DOE, because it provides a simple summary of the main effects. A slightly more informative way of plotting the main effects is with box plots separated by level.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, axes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;15&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         ncols&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         nrows&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;len(main_effects),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         sharex&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;False&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         sharey&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i, effect &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; enumerate(main_effects):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; axes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;flat[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;boxplot(data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df, x&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;effect, y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;, ax&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ax)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tight_layout()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_main_effects_boxplot3.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_main_effects_boxplot3.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;We see the same trends, but we now have a better sense of variability at each level. &lt;em&gt;Concentration&lt;/em&gt; seems to be very important, as it is the only effect where the level-separated box plots are clearly differentiated from each other. This means that variability from &lt;em&gt;concentration&lt;/em&gt; dominates variability coming from all other factors. &lt;em&gt;Mixing&lt;/em&gt; and &lt;em&gt;temperature&lt;/em&gt; still show some effects, but they are much more convoluted by variability coming from the other factors.&lt;/p&gt;
&lt;h4 id=&#34;interaction-effects-plot&#34;&gt;Interaction Effects Plot&lt;/h4&gt;
&lt;p&gt;One of the greatest advantages of DOE is its ability to reveal interaction effects, showing how factors influence each other rather than acting independently. The usual one-factor-at-a-time testing cannot provide such insights. Factor &lt;strong&gt;interactions&lt;/strong&gt; occur when the effect of one factor on the response depends on the level of another factor. In other words, the influence of one variable is not constant but changes in combination with others. The simplest type is a &lt;strong&gt;two-factor interaction&lt;/strong&gt;, where two factors jointly influence the response in a way that cannot be explained by their individual effects alone. For example, the effect of temperature on yield might be stronger at high mixing speed than at low mixing speed. More complex interactions can also occur. A &lt;strong&gt;three-factor interaction&lt;/strong&gt; means that the two-factor interaction itself changes depending on the level of a third factor, and so on for higher orders. While higher-order interactions are mathematically possible, they are often difficult to interpret and rarely significant in practical experiments. As a result, in our tutorial we will focus on two-factor interactions only.&lt;/p&gt;
&lt;p&gt;In DOE, it is common to visualize interaction effects using a grouped linear plot.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;combinations &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; list(itertools&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;combinations(factors, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, axes &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         ncols&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;len(combinations),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         nrows&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         sharex&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;False&lt;/span&gt;, sharey&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; i, combination &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; enumerate(combinations):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; axes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;flat[i]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; : &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sub &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;copy()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# normalize the result to &amp;#34;overlay&amp;#34; the &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# lines on top of each other to make&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;# slope comparison easier&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sub[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;normalized_average_result&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                        &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; sub&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;groupby(combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                        &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;result&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;transform(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean&amp;#39;&lt;/span&gt;)) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sns&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pointplot(data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;sub, x&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;], 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        y&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;normalized_average_result&amp;#39;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        hue&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;], 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        errorbar&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;ax)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_linear_interaction_plot3.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_linear_interaction_plot3.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;In this plot, each panel corresponds to a pair of factors, with one factor on the x-axis and the other represented by color-coded lines. The y-axis shows the normalized average response, adjusted so that differences in scale between levels are easier to compare. These plots provide an intuitive visual way to identify which pairs of factors may warrant closer examination in the statistical model. If the lines in a plot are roughly &lt;em&gt;parallel&lt;/em&gt;, it suggests that the two factors act independently, meaning there is little or no interaction between them. However, if the lines &lt;em&gt;cross&lt;/em&gt; or &lt;em&gt;diverge&lt;/em&gt;, it indicates that the effect of one factor depends on the level of the other, and an interaction is likely present. The greater the deviation from parallelism, the stronger the interaction effect.&lt;/p&gt;
&lt;p&gt;In our example, most lines are fairly parallel, suggesting no interaction. However, for &lt;em&gt;temperature&lt;/em&gt; : &lt;em&gt;time&lt;/em&gt; the lines do diverge significantly, suggesting that interactions may be present.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s summarize our exploration so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Our data seems to follow a normal distribution quite closely.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Concentration&lt;/em&gt; seems to have the strongest effect on the result.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Mixing&lt;/em&gt; and &lt;em&gt;temperature&lt;/em&gt; seem to have smaller, but meaningful effects.&lt;/li&gt;
&lt;li&gt;There may be interactions between &lt;em&gt;temperature&lt;/em&gt; &amp;amp; &lt;em&gt;time&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;statistical-modeling&#34;&gt;Statistical Modeling&lt;/h3&gt;
&lt;p&gt;Exploratory plots help reveal patterns in the data, but modeling goes further by quantifying those relationships and testing their statistical significance. While visual inspection can suggest which factors might be important, a fitted model estimates how much each factor contributes to the response, identifies interactions, and separates real effects from random noise. Modeling also enables prediction of untested conditions, estimation of confidence intervals, and optimization of the response within the design space.&lt;/p&gt;
&lt;p&gt;Modeling in DOE is an &lt;strong&gt;iterative process&lt;/strong&gt;. The first fitted model rarely provides the final answer but serves as a guide for refinement. Early results reveal which factors and interactions are significant, highlight unexpected relationships, and sometimes uncover issues such as curvature or outliers. Based on these insights, the model can be improved by removing weak terms, adding higher-order effects, or adjusting the experimental design to explore new regions of the design space. This cycle of building, evaluating, and refining continues until the model reliably represents the process and supports accurate predictions.&lt;/p&gt;
&lt;p&gt;The goal of model selection is to balance simplicity and accuracy. A model should capture the key effects and interactions without overfitting random variation. For a full factorial design, the process typically begins with a &lt;strong&gt;linear model&lt;/strong&gt; that includes all main effects and two-factor interactions. Higher-order terms can then be added if the initial model does not explain enough of the observed variability.&lt;/p&gt;
&lt;h4 id=&#34;initial-model&#34;&gt;Initial Model&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s use the formula API from &lt;em&gt;statsmodels&lt;/em&gt; to build our linear equation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model_equation &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;result ~ &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; param &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; main_effects:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    model_equation &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;param&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; + &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; combination &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; combinations:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    model_equation &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;combination[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; + &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model_equation &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (model_equation[:&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;] 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt;  model_equation[&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;:] &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; + &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Looking inside the &lt;code&gt;model_equation&lt;/code&gt; variable we get the following linear equation (in &lt;a href=&#34;https://patsy.readthedocs.io/en/latest/&#34;&gt;Patsy&lt;/a&gt; notation):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;result &lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt; concentration &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mixing &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; time &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; pH
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; concentration:temperature &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; concentration:pH 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; concentration:mixing &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; concentration:time 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature:pH &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature:mixing 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature:time &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; pH:mixing 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;         &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; pH:time &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mixing:time
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It should be noted that &lt;em&gt;statsmodels&lt;/em&gt; includes an intercept term by default.&lt;/p&gt;
&lt;p&gt;Now that we have the equation with all of the desired terms, let&amp;rsquo;s create the model using the Ordinary Least Squares (OLS) fitting approach.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; smf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ols(model_equation, data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fit()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;summary &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary2()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;summary&lt;/code&gt; variable contains the descriptive statistics about our model. Let&amp;rsquo;s spend some time understanding it.&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model1_summary2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model1_summary2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;General Model Fit Statistics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Df Model&lt;/strong&gt; (degrees of freedom for the model): number of estimated parameters that are not the intercept.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Df Residuals&lt;/strong&gt; (degrees of freedom for residuals): the number of independent pieces of information left to estimate the variability of the error term after the model&amp;rsquo;s parameters have been fitted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;R-squared&lt;/strong&gt;: fraction of variance in the response explained by the model.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Adj. R-squared&lt;/strong&gt;: R-squared adjusted for model size, useful for comparing models with different numbers of terms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AIC&lt;/strong&gt; (Akaike Information Criterion) and &lt;strong&gt;BIC&lt;/strong&gt; (Bayesian Information Criterion): information criteria that trade off fit and complexity. Lower values indicate a better model. We will use these values to judge how our model improves with refinement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Log-Likelihood&lt;/strong&gt;: likelihood of the data under the fitted model. Larger is better, used by AIC and BIC.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F-statistic&lt;/strong&gt; and &lt;strong&gt;Prob (F-statistic)&lt;/strong&gt;: overall test that at least one coefficient (beyond the intercept) is nonzero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scale&lt;/strong&gt;: estimate of residual variance (often the mean squared error).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Model Coefficients:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Coef.&lt;/strong&gt;: estimated effect for each term, including the intercept which is included by default.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Std.Err.&lt;/strong&gt;: standard error of the estimate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;t&lt;/strong&gt; and &lt;strong&gt;P&amp;gt;|t|&lt;/strong&gt;: t test and p-value for the null hypothesis that the coefficient equals zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;[0.025, 0.975]&lt;/strong&gt;: 95 percent confidence interval for the coefficient.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Diagnostic Tests:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Durbin–Watson&lt;/strong&gt;: checks autocorrelation of residuals. Values near 2 suggest little autocorrelation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Omnibus&lt;/strong&gt; and &lt;strong&gt;Prob(Omnibus)&lt;/strong&gt;: joint test of skewness and kurtosis of residuals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Jarque–Bera&lt;/strong&gt;, &lt;strong&gt;Prob(JB)&lt;/strong&gt;, &lt;strong&gt;Skew&lt;/strong&gt;, &lt;strong&gt;Kurtosis&lt;/strong&gt;: normality-related diagnostics for residuals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cond. No.&lt;/strong&gt;: condition number of the design matrix. Large values suggest multicollinearity or scaling issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A few important take-aways from this summary:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Adj. R-squared:&lt;/strong&gt; is fairly high, suggesting that our model explains the vast majority of variance in the data. This is great!&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prob (F-statistic)&lt;/strong&gt; is very small, suggesting that at least one of the parameters in our model is significant.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prob(Omnibus)&lt;/strong&gt; and &lt;strong&gt;Prob(JB)&lt;/strong&gt; are both high, suggesting that the residuals of the model are normally distributed (i.e., random). This means that the model is fitting the data in a balanced and comprehensive way.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In DOE, another common way to evaluate the model is through an ANOVA table:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;aov &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; anova_lm(model, typ&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# scientific notation is hard to read&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PR(&amp;gt;F)&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PR(&amp;gt;F)&amp;#39;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;round(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This table summarizes how total variation in the response is split among model terms and residual error, listing degrees of freedom, sums of squares, mean squares, F-statistics, and p-values to test which terms explain significant variation.&lt;/p&gt;
&lt;p&gt;We specifically select a table with &amp;ldquo;Type III&amp;rdquo; sums of squares. This makes it so that each term is adjusted for all other terms in the model, which is important when we want to consider term interactions.&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_anova_table2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_anova_table2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;Looking at the coefficients table, we see that a few of the factors have PR(&amp;gt;F) values below 0.05, suggesting that they are significant. These individual p-values are a good estimate of significance, but for us to draw conclusions we should perform more formal statistical tests that employ multiple comparison correction. Let&amp;rsquo;s use the &lt;a href=&#34;https://en.wikipedia.org/wiki/Bonferroni_correction&#34;&gt;Bonferroni&lt;/a&gt; correction method and plot a &lt;a href=&#34;https://en.wikipedia.org/wiki/Pareto_principle&#34;&gt;Pareto&lt;/a&gt; diagram to visualize our results (not to be confused with the 80-20 Pareto principle).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.05&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;idx &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Index(model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;params&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;keep &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt;idx&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;str&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;lower()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;isin([&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;intercept&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;const&amp;#39;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;names &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; idx[keep]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tvals &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tvalues[names]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;abs_t &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; tvals&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;abs()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sort_values()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;df_e &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; int(model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;df_resid)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;t_alpha &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ppf(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; alpha&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, df_e)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;t_alpha_bonf &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ppf(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; (alpha&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;len(names))&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, df_e)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;barh(abs_t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index, abs_t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Absolute standardized effect (|t|)&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Effect (Pareto) Plot — Standardized Effects&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axvline(t_alpha, linestyle&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;--&amp;#39;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;           linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;α=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;alpha&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.2g&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; t_alpha_bonf &lt;span style=&#34;color:#f92672&#34;&gt;is&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axvline(t_alpha_bonf, linestyle&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;:&amp;#39;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;               label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Bonferroni α/m=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;alpha&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;len(names)&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.2g&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;legend(loc&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;upper right&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;invert_yaxis()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tight_layout()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_pareto1.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_pareto1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;As we saw in data exploration, &lt;em&gt;concentration&lt;/em&gt; dominates all of the main effects. &lt;em&gt;Mixing&lt;/em&gt; also seems to have a strong effect, while &lt;em&gt;time&lt;/em&gt; seems to show a small effect that does not make it past our adjusted significance threshold. What is most surprising about these results is that the &lt;em&gt;temperature:time&lt;/em&gt; interaction seems to be very strong, with an effect size that is second only to &lt;em&gt;concentration&lt;/em&gt;. What is more interesting, is that the &lt;em&gt;time&lt;/em&gt; coefficient is positive, while that of the &lt;em&gt;temperature:time&lt;/em&gt; is negative, suggesting that the full effect of &lt;em&gt;time&lt;/em&gt; may have been somewhat obscured by the interaction. The presence of such a strong interaction is what usually makes the behaviors of systems hard to predicts, and this is exactly the case where DOE shines!&lt;/p&gt;
&lt;p&gt;While we are exploring our fit, let&amp;rsquo;s also look at partial regression plots to confirm the findings from the Pareto plot.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;figure(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;18&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;g &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sm&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;graphics&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot_partregress_grid(model, fig&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;fig)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_partial_regression1.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_partial_regression1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;These &lt;strong&gt;partial regression plots&lt;/strong&gt; (sometimes called &amp;ldquo;added variable plots&amp;rdquo;) show the unique relationship between the response and one predictor after removing the linear effects of all the other predictors. On the x-axis you see the residuals of that predictor after regressing it on the remaining predictors, and on the y-axis you see the residuals of the response after regressing it on those same remaining predictors. If the points line up, that predictor contributes linearly to the model. The fitted line in the plot has a slope equal to the predictor&amp;rsquo;s coefficient in the full model, and its tightness reflects the partial correlation given the other variables.&lt;/p&gt;
&lt;p&gt;Indeed, keeping the spread of the values on the y-axis, we clearly see that the line-fits for &lt;em&gt;concentration&lt;/em&gt; and &lt;em&gt;mixing&lt;/em&gt; show predictive capacity, while the lines for &lt;em&gt;temperature&lt;/em&gt; and &lt;em&gt;pH&lt;/em&gt; just cut horizontally through the noise. As suggested by the Pareto plot, &lt;em&gt;time&lt;/em&gt; is really borderline. From the interactions group, &lt;em&gt;temperature:time&lt;/em&gt; shows clear separation between the levels, confirming that this unexpected interaction is real.&lt;/p&gt;
&lt;h4 id=&#34;assessing-the-model-fit&#34;&gt;Assessing the Model Fit&lt;/h4&gt;
&lt;p&gt;Before we make any conclusions, let&amp;rsquo;s make sure that our chosen model is appropriate for our experiment.&lt;/p&gt;
&lt;p&gt;When assessing a model, the first place we start looking is the residuals. Let&amp;rsquo;s look at their distribution.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fitted &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fittedvalues
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;resid &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;resid
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;scatter(fitted, resid, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.8&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axhline(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Fitted values&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_ylabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residuals&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residuals vs Fitted&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model_residual_distribution1.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model_residual_distribution1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The residuals are randomly scattered across the range of fitted values with a roughly constant spread, indicating homoscedasticity and no obvious pattern. This suggests the model captures the signal without systematic bias across the predictor space. In short, no region appears over- or underfit, and the remaining variation looks like noise rather than missed structure.&lt;/p&gt;
&lt;p&gt;We can assess it a little more formally with a Q-Q Plot and a Shapiro-Wilk test:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Shapiro–Wilk p-value:&amp;#34;&lt;/span&gt;, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;shapiro(resid)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pvalue&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;round(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;qqplot(resid, line&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;s&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Q–Q plot of residuals&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Shapiro–Wilk p-value: 0.2007&lt;/strong&gt;&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_qq_plot_residuals1.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_qq_plot_residuals1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;Both the Shapiro-Wilk test and the Q-Q plot confirm that the residuals are normally distributed. This, combined with the adjusted \( R^{2} = 0.991 \), tell us that the model fits our data quite well.&lt;/p&gt;
&lt;h4 id=&#34;refining-the-model&#34;&gt;Refining the Model&lt;/h4&gt;
&lt;p&gt;Despite the fact that our model seems to explain the data well, it contains a lot of insignificant terms. Their presence may be diluting the significance of other factors. In fact, the borderline significance of &lt;em&gt;time&lt;/em&gt; may improve once we prune out the insignificant terms. Let&amp;rsquo;s re-evaluate the model that only keeps the terms that surpass our un-adjusted alpha level. Before we start the re-evaluation, let&amp;rsquo;s package our model evaluation code in an object for better reusability.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;evaluate_model&lt;/span&gt;(object):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; __init__(self, model_equation, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.05&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model_equation &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model_equation
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;alpha &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; alpha
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; smf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ols(model_equation, data&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;df)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fit()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary_tables &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary2()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tables
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary_tables[&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;concat([self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             anova_lm(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model, typ&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                             axis&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PR(&amp;gt;F)&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PR(&amp;gt;F)&amp;#39;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;round(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_sq&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;sum_sq&amp;#39;&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;df&amp;#39;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; df[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;result&amp;#39;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_numpy()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sst &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; ((y &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; y&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;mean())&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sum()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sse &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residual&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;sum_sq&amp;#39;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        dfe &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residual&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;df&amp;#39;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        mse &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residual&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_sq&amp;#39;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ss_model &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sst &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; sse
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;report&lt;/span&gt;(self):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### Fit Summary&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary_tables[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### ANOVA Table&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;aov)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### Pareto Plot&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        model_params &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Index(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;params&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        no_constant &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt;model_params&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;str&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;lower()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;isin([&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;intercept&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;const&amp;#39;&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        compared_params &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model_params[no_constant]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        tvals &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tvalues[compared_params]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        abs_t &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; tvals&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;abs()&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sort_values()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        df_e &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; int(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;df_resid)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        t_alpha &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ppf(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;alpha&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;, df_e)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        t_alpha_bonf &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;ppf(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; (self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;alpha
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                        &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;len(compared_params))&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         df_e)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;significant_parameters &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (abs_t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[abs_t &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;=&lt;/span&gt; t_alpha]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                       &lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;to_list())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;8&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;barh(abs_t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;index, abs_t&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Absolute standardized effect (|t|)&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Effect (Pareto) Plot — Standardized Effects&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axvline(t_alpha, linestyle&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;--&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;α=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;alpha&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.2g&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; t_alpha_bonf &lt;span style=&#34;color:#f92672&#34;&gt;is&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;not&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axvline(t_alpha_bonf, 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       linestyle&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;:&amp;#39;&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                       label&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Bonferroni α/m=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;alpha
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                               &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;len(compared_params)&lt;span style=&#34;color:#e6db74&#34;&gt;:&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;.2g&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;legend(loc&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;upper right&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;invert_yaxis()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;tight_layout()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### Partial Regression Plot&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        fig &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;figure(figsize&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;18&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        g &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sm&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;graphics&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;plot_partregress_grid(self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model, fig&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;fig)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### Residual Plot&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        fitted &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fittedvalues
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        resid &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; self&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;resid
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        fig, ax &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;subplots()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;scatter(fitted, resid)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;axhline(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, color&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, linewidth&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_xlabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Fitted values&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_ylabel(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residuals&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ax&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;set_title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Residuals vs Fitted&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        display(Markdown(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;### Residual Normality Tests&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        print(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Shapiro–Wilk p-value for the Residuals:&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;              stats&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;shapiro(resid)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pvalue&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;round(&lt;span style=&#34;color:#ae81ff&#34;&gt;4&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        qqplot(resid, line&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;s&amp;#39;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;title(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Q–Q plot of residuals&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        plt&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;show()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If we re-run the new object on the original (full) equation, we can extract the significant terms and build a refined equation. If we are including interaction terms, we should always keep their parent terms as well, even if they are themselves not significant.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model1 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; evaluate_model(model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;refined_model_equation &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;result ~ &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; param &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; model1&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;significant_parameters:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    refined_model_equation &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;param&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; + &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;refined_model_equation &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (refined_model_equation[:&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;] 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt;  refined_model_equation[&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;:] &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; + &amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                 &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; refined_model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;print(refined_model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which outputs:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;result &lt;span style=&#34;color:#f92672&#34;&gt;~&lt;/span&gt; time &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; mixing &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature:time &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; concentration &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; temperature
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we can run our model evaluation code on the refined equation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model2 &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; evaluate_model(refined_model_equation)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;model2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;report()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which outputs the following information about the refined model:&lt;/p&gt;
&lt;p&gt;

&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_summary3.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_summary3.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;



&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_anova1.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_anova1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pareto Plot&lt;/strong&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_pareto2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_pareto2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Partial Regression Plot&lt;/strong&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_partial_regression2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_partial_regression2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Residual Plot&lt;/strong&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_residual_distribution2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_residual_distribution2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Residual Normality Tests&lt;/strong&gt;
Shapiro–Wilk p-value for the Residuals: 0.405&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;ff_doe_model2_qq_plot_residuals2.png&#34; /&gt;
    &lt;/div&gt;
    &lt;a href=&#34;ff_doe_model2_qq_plot_residuals2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;We see that with fewer terms, the &lt;em&gt;time&lt;/em&gt; parameter now exceeds our adjustded alpha level of significance. The adjusted \(R^{2} \) remains 0.991, signifying that the removed terms did not explain any of the variability in the results. Both the AIC and BIC parameters went down, AIC from 30.1 to 17.6, and BIC from 64.7 to 30.5, signifying improvement in model quality. The updated model seems to check all of the boxes, so we can end the refinement process here.&lt;/p&gt;
&lt;h3 id=&#34;model-interpretation&#34;&gt;Model Interpretation&lt;/h3&gt;
&lt;p&gt;Our statistical model has confirmed that &lt;em&gt;precursor concentration&lt;/em&gt;, &lt;em&gt;mixing speed&lt;/em&gt;, and &lt;em&gt;reaction time&lt;/em&gt; have significant impact on the reaction yield, while &lt;em&gt;reaction temperature&lt;/em&gt;, and &lt;em&gt;solution pH&lt;/em&gt; do not. What is perhaps most surprising, is that the interaction term between &lt;em&gt;temperature&lt;/em&gt; and &lt;em&gt;reaction time&lt;/em&gt; is significant. How can this be, and what exactly does it mean? How can temperature have virtually no effect at shorter times, but have a measurable effect at longer time? After scratching his head about these result for a bit, our Chemist realizes that one factor he did not directly control in this experiment is &lt;em&gt;solvent evaporation&lt;/em&gt;. At low temperatures, the solvent barely evaporates either at long or short experiment durations, while at increased temperatures, duration of the experiment significantly affects the extent of solvent evaporation. This, in turn, changes another uncontrolled parameter of the reaction: the &lt;em&gt;ionic strength of the solvent&lt;/em&gt;. With this insight, the Chemist is now able to come up with an engineering solution (re-design the reactor to prevent evaporation), as well as a new experiment design that involves the &lt;em&gt;ionic strength&lt;/em&gt; of the solvent as one of the experimental factors.&lt;/p&gt;
&lt;p&gt;This kind of insight would not be possible with the one-variable-at-a-time experimental approach. Being able to characterize factor interactions is one of the greatest strengths of DOE, and our Chemist sure is thankful for being able to use this technique to solve his problem.&lt;/p&gt;
&lt;h3 id=&#34;process-optimization&#34;&gt;Process Optimization&lt;/h3&gt;
&lt;p&gt;Optimizing processes with a simple linear model is usually trivial: maximize parameters with a positive slope, and minimize the ones with a negative slope. But for the sake of the examples, let&amp;rsquo;s present a simple corner search algorithm using our refined model.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;bounds_from_factors&lt;/span&gt;(factors):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; {k: (min(v), max(v)) &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; k, v &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; factors&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;items()}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;corner_search&lt;/span&gt;(model, factors, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.05&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    bnds &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bounds_from_factors(factors)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    names &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; list(bnds&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;keys())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    corners &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; list(itertools&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;product(&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;[(lo, hi) 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                   &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (lo, hi) &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; bnds&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;values()]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    Xcorner &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; pd&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;DataFrame(corners, columns&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;names)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sf &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; model&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;get_prediction(Xcorner)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;summary_frame(alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;alpha)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i_max &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; int(sf[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean&amp;#39;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;idxmax())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    i_min &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; int(sf[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean&amp;#39;&lt;/span&gt;]&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;idxmin())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    res &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;maximize&amp;#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;settings&amp;#39;&lt;/span&gt;: Xcorner&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[i_max],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pred&amp;#39;&lt;/span&gt;:     float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_max, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean&amp;#39;&lt;/span&gt;]),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_CI&amp;#39;&lt;/span&gt;:  (float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_max, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_ci_lower&amp;#39;&lt;/span&gt;]), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_max, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_ci_upper&amp;#39;&lt;/span&gt;])),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PI&amp;#39;&lt;/span&gt;:       (float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_max, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;obs_ci_lower&amp;#39;&lt;/span&gt;]),  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_max, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;obs_ci_upper&amp;#39;&lt;/span&gt;])),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        },
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;minimize&amp;#39;&lt;/span&gt;: {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;settings&amp;#39;&lt;/span&gt;: Xcorner&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;iloc[i_min],
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pred&amp;#39;&lt;/span&gt;:     float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_min, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean&amp;#39;&lt;/span&gt;]),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_CI&amp;#39;&lt;/span&gt;:  (float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_min, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_ci_lower&amp;#39;&lt;/span&gt;]), 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_min, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_ci_upper&amp;#39;&lt;/span&gt;])),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PI&amp;#39;&lt;/span&gt;:       (float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_min, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;obs_ci_lower&amp;#39;&lt;/span&gt;]),  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                         float(sf&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loc[i_min, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;obs_ci_upper&amp;#39;&lt;/span&gt;])),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; res
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;corner_search(model2&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;model, factors, alpha&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;0.05&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which produces:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;maximize&amp;#39;&lt;/span&gt;: {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;settings&amp;#39;&lt;/span&gt;: concentration    &lt;span style=&#34;color:#ae81ff&#34;&gt;120&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  temperature        &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pH                 &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mixing            &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  time              &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Name: &lt;span style=&#34;color:#ae81ff&#34;&gt;19&lt;/span&gt;, dtype: int64,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pred&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;10.540625000000018&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_CI&amp;#39;&lt;/span&gt;: (&lt;span style=&#34;color:#ae81ff&#34;&gt;10.37785691344415&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;10.703393086555886&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PI&amp;#39;&lt;/span&gt;: (&lt;span style=&#34;color:#ae81ff&#34;&gt;9.984666143597542&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;11.096583856402495&lt;/span&gt;)},
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;minimize&amp;#39;&lt;/span&gt;: {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;settings&amp;#39;&lt;/span&gt;: concentration    &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  temperature      &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  pH                &lt;span style=&#34;color:#ae81ff&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  mixing           &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  time             &lt;span style=&#34;color:#ae81ff&#34;&gt;12&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Name: &lt;span style=&#34;color:#ae81ff&#34;&gt;9&lt;/span&gt;, dtype: int64,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;pred&amp;#39;&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;1.4843749999999933&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;mean_CI&amp;#39;&lt;/span&gt;: (&lt;span style=&#34;color:#ae81ff&#34;&gt;1.321606913444126&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;1.6471430865558607&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;PI&amp;#39;&lt;/span&gt;: (&lt;span style=&#34;color:#ae81ff&#34;&gt;0.9284161435975177&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;2.040333856402469&lt;/span&gt;)}}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;In this tutorial we demonstrated the power of a simple full factorial DOE study to interpret real interactions in practical process terms. In future notes we will extend this workflow to more economical designs, introduce screening and blocking, get familiar with more formal lack-of-fit assessments and pure-error checks, discuss factor coding and hierarchical model selection, cover multi-response optimization with desirability functions, and touch on measurement system considerations and power planning for sequential experimentation.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Design of Experiments (DOE)&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/doe-overview/&#39;&gt;Overview&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Full Factorial Tutorial&lt;/strong&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>Real-World Evidence: The Introduction</title>
      <link>http://notes.mirzo.net/post/real-world-evidence/</link>
      <pubDate>Sat, 27 Jul 2024 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/real-world-evidence/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/real-world-evidence/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/real-world-evidence1_hu2c1ed3c1efe5c1de34298e294da404ec_1348570_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Real-World Evidence: The Introduction&#34; title=&#34;Real-World Evidence: The Introduction&#34;&gt;&lt;/a&gt;


&lt;p&gt;The late 1990s and early 2000s saw the widespread adoption of electronic health records (EHRs), which made the vast amounts of real-world patient data much more accessible to researchers. At the same time, the limitations of the classic Randomized Controlled Trials (RCT) were becoming more and more apparent: the highly controlled conditions of these trials did not always reflect the real-world usage of interventions, and their strict inclusion/exclusion criteria lead to study populations that did not represent the full diversity of patient groups who will ultimately use the drug. And with the rising healthcare costs, the need to demonstrate the value of new treatments in the real-world setting became increasingly important.&lt;/p&gt;
&lt;p&gt;The emergence of Real-World Data (RWD) and Real-World Evidence (RWE) concepts has revolutionized how drug efficacy and safety are evaluated, how clinical trials are designed, and how healthcare decisions are made. In this note, we will cover the definitions, applications, and implications of RWD and RWE in pharmaceutical research.&lt;/p&gt;
&lt;p&gt;In December 2016, the United States Congress signed into law the &amp;ldquo;21st Century Cures Act&amp;rdquo; that formally defined and legitimized the use of RWE in regulatory decision-making. This landmark legislation required the FDA to establish a program to evaluate the potential use of RWE to support approval of new indications for approved drugs or to satisfy post-approval study requirements. Subsequently, the FDA released a &lt;a href=&#34;https://www.fda.gov/science-research/science-and-research-special-topics/real-world-evidence&#34;&gt;framework&lt;/a&gt; for its RWE program in 2018, outlining how the agency would evaluate RWE for regulatory purposes. This legislative and regulatory groundwork has been crucial in accelerating the adoption of RWE in drug development and approval processes, not only in the US but globally, as other regulatory agencies have followed suit in recognizing the value of RWE.&lt;/p&gt;
&lt;h2 id=&#34;real-world-data-rwd&#34;&gt;Real World Data (RWD)&lt;/h2&gt;
&lt;p&gt;Real World Data refers to health-related information collected outside the controlled environment of randomized clinical trials. Sources of RWD include:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Electronic Health Records (EHRs)&lt;/li&gt;
&lt;li&gt;Claims and billing data&lt;/li&gt;
&lt;li&gt;Product and disease registries&lt;/li&gt;
&lt;li&gt;Patient-generated data (including from in-home-use settings)&lt;/li&gt;
&lt;li&gt;Data gathered from other sources that can inform on health status, such as mobile devices&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;RWD provides a more comprehensive view of patient experiences and outcomes in real-world settings, offering insights that may not be captured in traditional clinical trials.&lt;/p&gt;
&lt;h2 id=&#34;real-world-evidence-rwe&#34;&gt;Real World Evidence (RWE)&lt;/h2&gt;
&lt;p&gt;Real World Evidence is the clinical evidence regarding the usage and potential benefits or risks of a medical product derived from analysis of RWD. RWE can be generated by different study designs or analyses, including but not limited to:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Randomized trials (including large simple trials, pragmatic trials)&lt;/li&gt;
&lt;li&gt;Observational studies (prospective and/or retrospective)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;RWE complements the evidence generated from traditional randomized controlled trials (RCTs) and can provide a more complete picture of a drug&amp;rsquo;s effectiveness and safety profile in diverse patient populations.&lt;/p&gt;
&lt;h3 id=&#34;large-simple-trials-lsts&#34;&gt;Large Simple Trials (LSTs)&lt;/h3&gt;
&lt;p&gt;Large Simple Trials are a type of clinical trial designed to bridge the gap between traditional RCTs and real-world clinical practice. Key characteristics of LSTs include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Large sample sizes: Often involving thousands or tens of thousands of patients&lt;/li&gt;
&lt;li&gt;Broad inclusion criteria: To better represent the diverse patient populations that will use the drug in real-world settings&lt;/li&gt;
&lt;li&gt;Simplified protocol: Focusing on a few critical outcomes rather than numerous detailed measurements&lt;/li&gt;
&lt;li&gt;Streamlined data collection: Gathering only essential data points to reduce burden on participants and researchers&lt;/li&gt;
&lt;li&gt;Integration with routine clinical care: Often conducted within the framework of usual patient care to increase relevance and reduce costs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;LSTs are particularly useful for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Detecting rare but serious adverse events&lt;/li&gt;
&lt;li&gt;Comparing the effectiveness of different treatments in real-world settings&lt;/li&gt;
&lt;li&gt;Assessing long-term outcomes in large populations&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;pragmatic-trials&#34;&gt;Pragmatic Trials&lt;/h3&gt;
&lt;p&gt;Pragmatic trials are designed to evaluate the effectiveness of interventions in real-life routine practice conditions. Unlike explanatory trials, which aim to test efficacy under ideal conditions, pragmatic trials seek to inform decision-makers about the real-world benefits and risks of a treatment. Key features of pragmatic trials include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Diverse participant population: Including patients with comorbidities who might be excluded from traditional RCTs&lt;/li&gt;
&lt;li&gt;Flexible intervention protocol: Allowing for variations in how the intervention is delivered, as would occur in routine practice&lt;/li&gt;
&lt;li&gt;Comparison with usual care: Often comparing the new intervention to the current standard of care rather than a placebo&lt;/li&gt;
&lt;li&gt;Patient-relevant outcomes: Focusing on outcomes that matter to patients and clinicians in everyday practice&lt;/li&gt;
&lt;li&gt;Minimal extra data collection: Relying on routinely collected data where possible to reduce the burden on participants and healthcare providers&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Pragmatic trials are particularly valuable for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Assessing the real-world effectiveness of treatments&lt;/li&gt;
&lt;li&gt;Informing clinical and policy decision-making&lt;/li&gt;
&lt;li&gt;Understanding how interventions perform across different healthcare settings and patient subgroups&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both large simple trials and pragmatic trials play crucial roles in generating Real World Evidence. They provide a bridge between the rigorously controlled environment of traditional RCTs and the complexities of real-world clinical practice. By doing so, they offer valuable insights into the effectiveness, safety, and optimal use of medical interventions in the populations and settings where they will ultimately be used.&lt;/p&gt;
&lt;h2 id=&#34;impact-on-the-pharmaceutical-industry&#34;&gt;Impact on the Pharmaceutical Industry&lt;/h2&gt;
&lt;p&gt;The integration of RWD and RWE is significantly affecting the pharmaceutical industry in several ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Drug Development&lt;/strong&gt;: RWD can inform the design of clinical trials by providing insights into disease prevalence, patient populations, and current treatment patterns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regulatory Decision-Making&lt;/strong&gt;: Regulatory bodies like the FDA are increasingly considering RWE in their approval processes, potentially accelerating the path to market for new drugs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Post-Market Surveillance&lt;/strong&gt;: RWD allows for more comprehensive monitoring of drug safety and effectiveness after market launch.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Value Demonstration&lt;/strong&gt;: RWE can help demonstrate the value of drugs in real-world settings, which is crucial for negotiations with payers and health technology assessment bodies.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Personalized Medicine&lt;/strong&gt;: Analysis of RWD can reveal subgroups of patients who respond particularly well to certain treatments, advancing the field of personalized medicine.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;challenges-and-considerations&#34;&gt;Challenges and Considerations&lt;/h2&gt;
&lt;p&gt;While RWD and RWE offer immense potential, there are several challenges to consider:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Quality&lt;/strong&gt;: Ensuring the quality, reliability, and relevance of RWD is crucial.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Integration&lt;/strong&gt;: Combining data from diverse sources can be complex and requires sophisticated data management systems.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Privacy and Ethics&lt;/strong&gt;: Collecting and using patient data raises important privacy and ethical considerations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Methodological Rigor&lt;/strong&gt;: Developing robust methodologies for analyzing RWD and generating reliable RWE is an ongoing challenge.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regulatory Acceptance&lt;/strong&gt;: While regulators are increasingly open to RWE, standards for its use in regulatory decision-making are still evolving.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;strategies-for-effective-incorporation-of-rwe&#34;&gt;Strategies for Effective Incorporation of RWE&lt;/h2&gt;
&lt;p&gt;To effectively leverage RWD and RWE, pharmaceutical companies should consider the following strategies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Invest in Data Infrastructure&lt;/strong&gt;: Develop robust systems for collecting, storing, and analyzing large volumes of diverse data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Foster Collaborations&lt;/strong&gt;: Partner with healthcare providers, technology companies, and academic institutions to access diverse data sources and analytical expertise.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Develop In-House Expertise&lt;/strong&gt;: Build teams with skills in data science, epidemiology, and health outcomes research.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Engage with Regulators&lt;/strong&gt;: Maintain open dialogue with regulatory bodies to understand and shape the evolving landscape of RWE use in drug development and approval.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Prioritize Data Quality&lt;/strong&gt;: Implement rigorous processes for data validation and quality assurance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Integrate RWE Throughout the Product Lifecycle&lt;/strong&gt;: From early development to post-market surveillance, look for opportunities to generate and use RWE.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ensure Ethical Use of Data&lt;/strong&gt;: Develop clear policies and procedures for ethical data collection and use, ensuring patient privacy and consent.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Real World Evidence is a powerful tool that has the potential to transform pharmaceutical research and development. By providing insights into the real-world effectiveness and safety of drugs, it can accelerate innovation, improve patient outcomes, and increase the efficiency of healthcare systems. However, realizing this potential requires careful navigation of technical, ethical, and regulatory challenges. As the field continues to evolve, pharmaceutical companies that effectively incorporate RWE into their research and development processes will be well-positioned to lead in an increasingly data-driven healthcare landscape.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Savitzky–Golay Filter: 60 Years 🎂 of Smoothing Noisy Data</title>
      <link>http://notes.mirzo.net/post/sovgol-filter/</link>
      <pubDate>Sat, 04 May 2024 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/sovgol-filter/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/sovgol-filter/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/savgol1_hu75b4b39cb307ca520928d19d99949bcf_2778576_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Savitzky–Golay Filter: 60 Years 🎂 of Smoothing Noisy Data&#34; title=&#34;Savitzky–Golay Filter: 60 Years 🎂 of Smoothing Noisy Data&#34;&gt;&lt;/a&gt;


&lt;p&gt;Sixty years ago, in July of 1964, Abraham Savitzky and Marcel J. E. Golay published a seminal &lt;a href=&#34;https://pubs.acs.org/doi/abs/10.1021/ac60214a047&#34;&gt;paper&lt;/a&gt; in &lt;em&gt;Analytical Chemistry&lt;/em&gt; where they introduced a technique designed to smooth out noise in spectroscopic data and enhance the signal without significantly distorting the underlying trends. The Savitzky-Golay filter was one of the first signal processing techniques that I&amp;rsquo;ve learned in grad school, and the simplicity and elegance of this method has captured my heart ever since. As we approach the 60th anniversary of this influential technique, let&amp;rsquo;s explore its development, applications, and the enduring impact on science.&lt;/p&gt;
&lt;p&gt;Oh, and we are also going to implement it in Excel VBA.&lt;/p&gt;
&lt;h3 id=&#34;the-origins&#34;&gt;The Origins&lt;/h3&gt;
&lt;p&gt;The Savitzky-Golay filter arose from the need to correct for distortions in spectroscopic data, a common issue in analytical chemistry where precise data is crucial for accurate analysis. The genius of Savitzky and Golay was in using polynomial least squares fitting to a set of consecutive data points. This method applies a convolution operation using polynomial coefficients that are pre-computed, making the process computationally efficient.&lt;/p&gt;
&lt;p&gt;What sets the Savitzky-Golay filter apart from other smoothing techniques is its ability to preserve important data features such as relative maxima, minima, and width, which are crucial in interpretative and quantitative analysis. Its design allows it to smooth out noise while maintaining the shape and features of the underlying signal, a crucial advantage when accuracy and precision are paramount.&lt;/p&gt;
&lt;h3 id=&#34;the-math&#34;&gt;The Math&lt;/h3&gt;
&lt;p&gt;The Savitzky-Golay filter works by fitting successive subsets of adjacent data points with a low-degree polynomial by the method of linear least squares. When fitting each subset, the central point is then replaced with the value evaluated from the polynomial equation. This process is repeated across the data set. This method increases the accuracy of the fit and, therefore, of the data smoothing, and it tends to preserve features of the distribution that are usually &amp;lsquo;flattened&amp;rsquo; by other types of smoothing filters.&lt;/p&gt;
&lt;p&gt;The key mathematical concept behind the Savitzky-Golay filter is the fitting of a polynomial to a subset of the data. For a set of data points \((x_i, y_i)\), where \(i\) ranges from \(1\) to \(n\), a polynomial of degree \(k\) (usually \(k \leq 4\) for most applications) is fitted to each subset of \(m\) points (also known as the &amp;ldquo;running window&amp;rdquo;), where \(m\) is odd so that it is symmetric around the central point.&lt;/p&gt;
&lt;p&gt;The least squares method is used to find the polynomial coefficients \(a_0, a_1, \dots, a_k\) that minimize the sum of the squared residuals:&lt;/p&gt;
&lt;p&gt;$$
\sum_{i=1}^m [y_i - f(x_i)]^2
$$&lt;/p&gt;
&lt;p&gt;where \(f(x_i) = a_0 + a_1x_i + a_2x_i^2 + \dots + a_kx_i^k\).&lt;/p&gt;
&lt;p&gt;The output of the Savitzky-Golay filter is a linear combination of the input data, which means the smoothed value \(y&amp;rsquo;_i \) at each point \( i \) can be represented as a convolution of the input data \( y_i \) with a set of coefficients \( c_j \):&lt;/p&gt;
&lt;p&gt;$$
y&amp;rsquo;_i = \sum_{j=-\frac{m-1}{2}}^{\frac{m-1}{2}} c_j y_{i+j}
$$&lt;/p&gt;
&lt;p&gt;The coefficients \(c_j\) depend only on the relative position within the window of \(m\) points and are derived from the polynomial fit. They are computed such that they represent the convolution kernel for the least squares fitting of a polynomial of degree \(k\).&lt;/p&gt;
&lt;p&gt;The Savitzky-Golay filter can also be used to calculate derivatives of the data by modifying the convolution coefficients to represent the derivative of the polynomial rather than the polynomial itself. This is particularly useful for calculating the first or second derivative of a data set without amplifying the noise significantly, as would be typical with simpler numerical differentiation methods like finite differences.&lt;/p&gt;
&lt;h3 id=&#34;implementing-in-vba&#34;&gt;Implementing in VBA&lt;/h3&gt;
&lt;p&gt;The Savitzky-Golay filter was one of the first signal processing techniques I have implemented on my own in grad school, when I needed a good way to smooth data while analyzing it in Excel. It was a quick User-defined function (UDF) implementation in VBA. Let me bring you this bug-filled implementation in its full glory!&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-vb&#34; data-lang=&#34;vb&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;Option&lt;/span&gt; Explicit
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;Function&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;sovgol&lt;/span&gt; (rrange &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; Range, points &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Integer&lt;/span&gt;, &lt;span style=&#34;color:#66d9ef&#34;&gt;Optional&lt;/span&gt; derOrder &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Integer&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 0) &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Variant&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39; An array function that smoothes a range of equally spaced data using Savitzky-Golay method
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Description of Arguments:
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;rrange = Contiguous range of data to be smoothed
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;points = Number of points for a running filter (must be an odd number larger than 3)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;derOrder = (Optional) Order of derivation 0-2
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; m &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Integer&lt;/span&gt;, i &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Integer&lt;/span&gt;, j &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Integer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; c &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Variant&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; rsize &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Long&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; sumprod &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Double&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; p() &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Double&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Dim&lt;/span&gt; original() &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Variant&lt;/span&gt;, smoothed() &lt;span style=&#34;color:#f92672&#34;&gt;As&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Variant&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Check if $points is odd, and convert to m
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;If&lt;/span&gt; (points &lt;span style=&#34;color:#f92672&#34;&gt;Mod&lt;/span&gt; 2 &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;gt;&lt;/span&gt; 1) &lt;span style=&#34;color:#f92672&#34;&gt;Or&lt;/span&gt; (points &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; 3) &lt;span style=&#34;color:#66d9ef&#34;&gt;Then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sovgol &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; CVErr(xlErrValue)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Exit&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;Else&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        m &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (points&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;End&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;If&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;ReDim&lt;/span&gt; p(&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;m &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39; Pick the right equation to define the coeficients
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Select&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Case&lt;/span&gt; derOrder
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Case&lt;/span&gt; 0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=-&lt;/span&gt;m &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            p(i)&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (3&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(3&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(m&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;2)&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;3&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;1&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;5&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(i&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;2)))&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;((2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;3)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;1))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Case&lt;/span&gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=-&lt;/span&gt;m &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            p(i)&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (3&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;i)&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;((2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Case&lt;/span&gt; 2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=-&lt;/span&gt;m &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            p(i)&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (30&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(3&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;i&lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt;2&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)))&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;((2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;3)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(2&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;(m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;m)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Case&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sovgol &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; CVErr(xlErrValue)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Exit&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;End&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Select&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Count number of points in $rrange
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    rsize &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Application.WorksheetFunction.CountA(rrange)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;ReDim&lt;/span&gt; original(1 &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; rsize)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;ReDim&lt;/span&gt; smoothed(1 &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; rsize, 1 &lt;span style=&#34;color:#66d9ef&#34;&gt;to&lt;/span&gt; 1)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39; Import range
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;1 &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; rsize
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        original(i) &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; rrange.Cells(i,1).Value
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Start with m&amp;#39;th point
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1 &lt;span style=&#34;color:#66d9ef&#34;&gt;to&lt;/span&gt; rsize&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sumprod &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; j&lt;span style=&#34;color:#f92672&#34;&gt;=-&lt;/span&gt;m &lt;span style=&#34;color:#66d9ef&#34;&gt;To&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            sumprod &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sumprod &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; (original(i&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;j)&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;p(j))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; j
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        smoothed(i,1)&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;sumprod
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Fill in missing points at start
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;1 &lt;span style=&#34;color:#66d9ef&#34;&gt;to&lt;/span&gt; m
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        smoothed(i,1) &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; smoothed(m&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;1,1)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#75715e&#34;&gt;&amp;#39;Fill in missing points at end
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;For&lt;/span&gt; i&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;(rsize&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;(m&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;1)) &lt;span style=&#34;color:#66d9ef&#34;&gt;to&lt;/span&gt; rsize
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        smoothed(i,1) &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; smoothed(rsize&lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;m,1)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;Next&lt;/span&gt; i
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sovgol &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; smoothed
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;End&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;Function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This &lt;code&gt;sovgol&lt;/code&gt; function takes a range of data, a number of points that define the &amp;ldquo;running window&amp;rdquo; for the filter, and an optional order of the derivative to calculate (0 for smoothing, 1 for first derivative, and 2 for second derivative). Based on the derivative order and the number of points, equation coefficients are pre-calculated for the filter using polynomial least squares fitting. The function computes the smoothed value for each data point starting from the midpoint of the &amp;ldquo;running window&amp;rdquo; to avoid boundary issues. It uses a nested loop where each point is smoothed by summing the product of neighboring points and their corresponding coefficients. The function assigns smoothed values at the edges of the data range by replicating the smoothed values near the boundary. This simple approach handles edge effects but might not be ideal for all applications. As an output, the function returns a &amp;ldquo;smoothed&amp;rdquo; two-dimensional array.&lt;/p&gt;
&lt;p&gt;To use this function in Excel, paste it in as a VBA module. Then, you would enter a formula in a cell like &lt;code&gt;=sovgol(A1:A100, 5)&lt;/code&gt; for smoothing, or &lt;code&gt;=sovgol(A1:A100, 5, 1)&lt;/code&gt; to compute the first derivative. This function must be entered as an array formula, which involves selecting a range of cells for output, entering the formula, and pressing &lt;code&gt;Ctrl+Shift+Enter&lt;/code&gt; (in older versions of Excel) or simply &lt;code&gt;Enter&lt;/code&gt; in newer versions that support dynamic arrays.&lt;/p&gt;
&lt;h3 id=&#34;applications-across-disciplines&#34;&gt;Applications Across Disciplines&lt;/h3&gt;
&lt;p&gt;Over the decades, the versatility of the Savitzky-Golay filter has allowed it to be adopted in numerous fields beyond its original application in chemistry. In physics, it is used to process signals in instruments that require noise reduction without compromising the integrity of the signal. In astronomy, the filter helps enhance the visibility of celestial bodies by reducing background noise in images.&lt;/p&gt;
&lt;p&gt;The digital world has also benefitted from this filter, particularly in the realm of digital signal processing where it is used to process audio signals and other digital data streams. In finance, the Savitzky-Golay filter assists in smoothing out stock market price data, providing a clearer view of market trends without the distraction of short-term volatility.&lt;/p&gt;
&lt;p&gt;The Savitzky-Golay filter&amp;rsquo;s influence extends into the realm of technology development, particularly in the area of data science and machine learning. In these fields, data often contains a significant amount of noise, and maintaining the integrity of the signal during preprocessing is crucial. The filter&amp;rsquo;s ability to enhance signal clarity without distorting key data features makes it an invaluable tool for algorithms that require clean, accurate data for training and analysis.&lt;/p&gt;
&lt;p&gt;Moreover, the algorithm behind the Savitzky-Golay filter has inspired advancements in software and hardware implementations, optimizing it for real-time data processing in various high-tech applications. This adaptability demonstrates its enduring relevance in a rapidly evolving technological landscape.&lt;/p&gt;
&lt;p&gt;As we celebrate the 60th anniversary of the Savitzky-Golay filter, it is clear that the legacy of this simple yet powerful tool is ongoing. From its origins in analytical chemistry to its broad applications across numerous scientific and technological disciplines, the filter remains a testament to the enduring importance of fundamental research and its potential to offer practical solutions across decades. Its development not only enhanced our ability to analyze and interpret data more effectively but also set a foundation for future innovations in data processing. The Savitzky-Golay filter continues to be a cornerstone technique in scientific data analysis, symbolizing a bridge between the past and future of technological advancement.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Guidance for Industry: The Reference Section</title>
      <link>http://notes.mirzo.net/post/regulatory-guidance-reflist/</link>
      <pubDate>Tue, 13 Feb 2024 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/regulatory-guidance-reflist/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/regulatory-guidance-reflist/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/guidance2_hu45184c3931c2e6900fab579ccbf69c8c_1306447_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Guidance for Industry: The Reference Section&#34; title=&#34;Guidance for Industry: The Reference Section&#34;&gt;&lt;/a&gt;


&lt;p&gt;Anyone working in the pharmaceutical industry must have a good comprehension of the applicable regulations. While attending courses and workshops is very helpful in staying up to date, there is really no substitute to reading the source documents. Here I keep my currated &amp;ldquo;Reference Section&amp;rdquo; of regulatory documents and guidance that I find myself referring back to all the time. It may be useful to anyone working in clinical sample testing and/or IVD development.&lt;/p&gt;
&lt;h2 id=&#34;gxp&#34;&gt;GxP&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;GCP&lt;/strong&gt;: E6(R2) Good Clinical Practice, Integrated Addendum to ICH E6(R1) Guidance for Industry. &lt;em&gt;ICH, 2018&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/E6_R2_Addendum.pdf&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GCLP&lt;/strong&gt;: Good Clinical Laboratory Practice. &lt;em&gt;WHO (2009)&lt;/em&gt; (&lt;a href=&#34;https://fctc.who.int/publications/i/item/good-clinical-laboratory-practice-%28-gclp%29&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GLP&lt;/strong&gt;: 21 CFR Part 58, Good Laboratory Practice for nonclinical laboratory studies. &lt;em&gt;FDA (1978)&lt;/em&gt; (&lt;a href=&#34;https://www.ecfr.gov/current/title-21/chapter-I/subchapter-A/part-58&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GLP&lt;/strong&gt;: Principles of Good Laboratory Practice. &lt;em&gt;OECD (1997)&lt;/em&gt; (&lt;a href=&#34;https://www.oecd.org/chemicalsafety/testing/oecdseriesonprinciplesofgoodlaboratorypracticeglpandcompliancemonitoring.htm&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GMP&lt;/strong&gt;: Q7 Good Manufacturing Practice Guide for Active Pharmaceutical Ingredients. &lt;em&gt;ICH (2000)&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/Q7%20Guideline.pdf&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;quality-systems&#34;&gt;Quality Systems&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Q9 (R1) Guideline on Quality Risk Management. &lt;em&gt;ICH (2023)&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/ICH_Q9%28R1%29_Guideline_Step4_2023_0126_0.pdf&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;analyticalbioanalytical-methods&#34;&gt;Analytical/Bioanalytical Methods&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Q14 Analytical Procedure Development. &lt;em&gt;ICH (2023)&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/ICH_Q14_Guideline_2023_1116.pdf&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Scope: development and maintenance of analytical procedures for evaluation of the quality of drug substances and drug products.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Q2(R2) Validation of Analytical Procedures. &lt;em&gt;ICH (2023)&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/ICH_Q2%28R2%29_Guideline_2023_1130.pdf&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Scope: validation of analytical procedures used for release and stability testing of commercial drug substances and products.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;M10 Bioanalytical Method Validation and Study Sample Analysis. &lt;em&gt;ICH (2022)&lt;/em&gt; (&lt;a href=&#34;https://database.ich.org/sites/default/files/M10_Guideline_Step4_2022_0524.pdf&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Scope: validation of assays for quantification of chemical and biological drugs and their application in the analysis of study samples.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Bioanalytical Method Validation, Guidance for Industry. &lt;em&gt;FDA (2018)&lt;/em&gt;. (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/bioanalytical-method-validation-guidance-industry&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Scope: validation of assays for human clinical pharmacology, bioavailability, or bioequivalence studies that require pharmacokinetic, toxicokinetic, or biomarker concentration evaluation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;computerized-systems&#34;&gt;Computerized Systems&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;21 CFR Part 11, Electronic Records; Electronic Signatures - Scope and Application, Guidance for Industry. &lt;em&gt;FDA (2003)&lt;/em&gt; (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/part-11-electronic-records-electronic-signatures-scope-and-application&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Computerized Systems Used in Clinical Investigations, Guidance for Industry. &lt;em&gt;FDA (2007)&lt;/em&gt; (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/computerized-systems-used-clinical-investigations&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Supplement to 21CFR Part 11.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Draft, Computer Software Assurance for Production and Quality System Software &lt;em&gt;FDA (2022)&lt;/em&gt; (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/computer-software-assurance-production-and-quality-system-software&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;The next evolution of guidance for computerized systems in medical devices or medical device quality systems.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Guideline on computerised systems and electronic data in clinical trials. &lt;em&gt;EMA (2023)&lt;/em&gt; (&lt;a href=&#34;https://www.ema.europa.eu/en/documents/regulatory-procedural-guideline/guideline-computerised-systems-and-electronic-data-clinical-trials_en.pdf&#34;&gt;link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Includes description of ALCOA++ principles.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Data Integrity and Compliance with cGMP, Draft Guidance for Industry. &lt;em&gt;FDA (2016)&lt;/em&gt; (&lt;a href=&#34;https://www.fda.gov/files/drugs/published/Data-Integrity-and-Compliance-With-Current-Good-Manufacturing-Practice-Guidance-for-Industry.pdf&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;medical-devices&#34;&gt;Medical Devices&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;ISO 13485, Medical devices. &lt;em&gt;ISO (2016)&lt;/em&gt; (&lt;a href=&#34;https://www.iso.org/iso-13485-medical-devices.html&#34;&gt;paywalled link&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Describes Quality System requirements for medical devices&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;ISO 14971, Application of risk management to medical devices. &lt;em&gt;ISO (2019)&lt;/em&gt; (&lt;a href=&#34;https://www.iso.org/standard/72704.html&#34;&gt;paywalled link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;IVDR, Regulation on in vitro diagnostic medical devices. &lt;em&gt;EU (2017)&lt;/em&gt; (&lt;a href=&#34;https://eur-lex.europa.eu/legal-content/EN/TXT/PDF/?uri=CELEX:02017R0746-20170505&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;ISO 9001, Quality management systems. &lt;em&gt;ISO (2015)&lt;/em&gt; (&lt;a href=&#34;https://www.iso.org/standard/62085.html&#34;&gt;paywalled link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;clinical-data&#34;&gt;Clinical Data&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Data Retention When Subjects Withdraw from FDA-Regulated Clinical Trials. &lt;em&gt;FDA (2008)&lt;/em&gt; (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/data-retention-when-subjects-withdraw-fda-regulated-clinical-trials&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Guidance on Withdrawal of Subjects from Research:
Data Retention and Other Related Issues. &lt;em&gt;OHRP (2010)&lt;/em&gt; (&lt;a href=&#34;https://www.hhs.gov/ohrp/sites/default/files/ohrp/policy/subjectwithdrawal.pdf&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;clinical-trials&#34;&gt;Clinical Trials&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Multiple Endpoints in Clinical Trials, Draft Guidance for Industry. &lt;em&gt;FDA (2022)&lt;/em&gt;. (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/multiple-endpoints-clinical-trials-guidance-industry&#34;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- raw HTML omitted --&gt;
&lt;!-- raw HTML omitted --&gt;</description>
      
    </item>
    
    <item>
      <title>Setting up to run Mistral LLM locally</title>
      <link>http://notes.mirzo.net/post/setting-up-mistral/</link>
      <pubDate>Thu, 21 Dec 2023 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/setting-up-mistral/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/setting-up-mistral/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/mistral1_hu60b994349dd19ff0efa095aa4f50e6cc_1104441_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Setting up to run Mistral LLM locally&#34; title=&#34;Setting up to run Mistral LLM locally&#34;&gt;&lt;/a&gt;


&lt;p&gt;Mistral is a series of Large Language Models (LLM) developed by a French company Mistral AI. These models are notable for their high performance and efficiency in generating text. Many of Mistral&amp;rsquo;s models are open source and can be used without restrictions under the Apache 2.0 license. Here is a quick troubleshooting walkthrough on how I set up dolphin-2.5-mixtral-8x7b to run locally with cuBLAS back-end for hardware acceleration. I experienced a few annoying hangups in the process, so hopefully this little note can help anyone experiencing the same.&lt;/p&gt;
&lt;h2 id=&#34;setup-the-environment&#34;&gt;Setup the environment&lt;/h2&gt;
&lt;p&gt;First, make sure you have CUDA installed:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;nvcc --version
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Create an environment definition file &lt;code&gt;mistral.yml&lt;/code&gt; to be installed by mamba/conda:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yml&#34; data-lang=&#34;yml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;name&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;mistral&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;channels&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;nvidia&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;defaults&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;conda-forge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;dependencies&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;python=3.11&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pip&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;anaconda&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;langchain&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;huggingface_hub&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch-cuda=12.1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#f92672&#34;&gt;pip&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;torchaudio&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;torchvision&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Notably, you will see that &lt;code&gt;llama-cpp-python&lt;/code&gt; is missing from the package list. We will install it separately using pip with additional build parameters to enable cuBLAS.&lt;/p&gt;
&lt;p&gt;Create and switch into the environment:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mamba env create -f mistral.yml
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;mamba activate mistral
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Set environmental variables to have CMake compile llama.cpp with cuBLAS back-end. Then install using pip. The &lt;code&gt;--no-cache-dir&lt;/code&gt; is important, because you want to make sure you are building fresh from source, rather than using a potential pre-chached version. The &lt;code&gt;--verbose&lt;/code&gt; flag will help you troubleshoot if CUDA of cuBLAS issues arise during the build.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;set&lt;/span&gt; CMAKE_ARGS=-DLLAMA_CUBLAS=on  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;set&lt;/span&gt; FORCE_CMAKE=1  
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir --verbose
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Use the huggingface CLI to get the appropriate model. Going to use the recommended 4-bit quantized model &lt;code&gt;dolphin-2.5-mixtral-8x7b.Q4_K_M.gguf&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;huggingface-cli download TheBloke/dolphin-2.5-mixtral-8x7b-GGUF dolphin-2.5-mixtral-8x7b.Q5_K_M.gguf --local-dir . --local-dir-use-symlinks False
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;minimal-python-code&#34;&gt;Minimal python code&lt;/h2&gt;
&lt;p&gt;Now let&amp;rsquo;s use langchain to set up a minimal running example.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python3&#34; data-lang=&#34;python3&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; langchain.callbacks.manager &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; CallbackManager
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; langchain.callbacks.streaming_stdout &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; StreamingStdOutCallbackHandler
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; langchain.chains &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; LLMChain
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; langchain.llms &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; LlamaCpp
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; langchain.prompts &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; PromptTemplate
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;template &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&amp;#34;Question: &lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{question}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;Answer: Let&amp;#39;s work this out in a step by 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;step way to be sure we have the right 
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;answer.&amp;#34;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;prompt &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; PromptTemplate(template&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;template, input_variables&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;[&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;question&amp;#34;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;callback_manager &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; CallbackManager([StreamingStdOutCallbackHandler()])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;llm &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; LlamaCpp(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    model_path&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;r&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;.\dolphin-2.5-mixtral-8x7b.Q4_K_M.gguf&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n_gpu_layers&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    n_batch&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;500&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    callback_manager&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;callback_manager,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    verbose&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    )
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;llm_chain &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; LLMChain(prompt&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;prompt, llm&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;llm)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;question &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;What is the best planet in the galaxy?&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;llm_chain&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;run(question)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>Design of Experiments (DOE): The Overview</title>
      <link>http://notes.mirzo.net/post/doe-overview/</link>
      <pubDate>Wed, 22 Nov 2023 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/doe-overview/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/doe-overview/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/doe-primer_hu869254cf930dbcf704cd4ec59519ab60_1283397_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Design of Experiments (DOE): The Overview&#34; title=&#34;Design of Experiments (DOE): The Overview&#34;&gt;&lt;/a&gt;


&lt;p&gt;Design of Experiments (DOE) is a systematic approach to planning, conducting, and analyzing efficient scientific experiments. It is an indispensable tool in optimization of complex processes, especially in engineering, applied research, and manufacturing. Unlike traditional one-variable-at-a-time methods, DOE involves simultaneously varying multiple factors to efficiently assess their individual and interactive effects on the outcome. The most obvious benefit of DOE is that it allows to dramatically reduce the number of experiments needed to characterize a system (well, depending on the design you choose). But more importantly, it allows us to uncover interactions between variables, something that one-variable-at-a-time testing simply cannot do.&lt;/p&gt;
&lt;p&gt;This note provides a high-level overview of DOE principles; future notes in this series will explore specific design types and analysis techniques in more detail.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Design of Experiments (DOE)&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
						&lt;li&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/doe-factorial-tutorial/&#39;&gt;Full Factorial Tutorial&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;p&gt;The key features of DOE:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Efficiency:&lt;/strong&gt; DOE allows for the investigation of multiple factors in a relatively small number of experiments. This efficiency is achieved through a carefully designed experimental plan.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Interactions:&lt;/strong&gt; DOE explicitly accounts for interactions between variables, recognizing that the combined effect of factors may differ from their individual contributions. Conventional methods often overlook these interactions, leading to incomplete or inaccurate conclusions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Statistical Rigor:&lt;/strong&gt; DOE incorporates statistical principles from the outset, providing a robust framework for data analysis. This ensures that results are not merely anecdotal but are backed by statistical significance, enhancing the reliability of conclusions drawn from the experiments.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimization:&lt;/strong&gt; One of the primary goals of DOE is to optimize processes or systems by identifying the ideal conditions for the desired outcome. This is achieved by systematically exploring the experimental space and determining the settings that yield the best results.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Prioritization:&lt;/strong&gt; DOE provides a comprehensive understanding of the factors influencing a system and their relative importance. This allows us to prioritize which changes and improvements will have the most significant impact on the performance of a process.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;the-stages-of-experimental-development&#34;&gt;The Stages of Experimental Development&lt;/h2&gt;
&lt;p&gt;DOE is not a single experiment but a structured sequence of learning stages. In other words, each DOE study should exist within a larger experimental development lifecycle where each study builds on previous knowledge to progressively refine understanding and control of a system. The DOE lifecycle usually includes the following types of studies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Screening:&lt;/strong&gt; The first step is to identify which factors have the greatest influence on the outcome. Sparse designs are often used at this stage to efficiently test many variables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Characterization (Modeling):&lt;/strong&gt; Once key factors are known, more detailed experiments are performed to model how they interact and to understand the shape of the response surface.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimization:&lt;/strong&gt; Using methods that sample the response surface in a lot of detail, the goal is to find the combination of factors that maximizes (or minimizes) the desired response.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verification (Confirmation):&lt;/strong&gt; The optimized settings are tested in confirmatory experiments to ensure that the predicted improvements hold in practice.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This staged, &lt;em&gt;iterative approach&lt;/em&gt; is what makes DOE powerful. Each round of experimentation improves knowledge and narrows focus, reducing uncertainty before committing to full-scale implementation.&lt;/p&gt;
&lt;h2 id=&#34;a-doe-study-step-by-step&#34;&gt;A DOE Study, Step-by-Step&lt;/h2&gt;
&lt;p&gt;Within each DOE study, the following fundamental steps are typically carried out:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define the Objectives&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Identify Factors and their Levels&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Select a Design&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Conduct Experiments&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Analyze Data&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s take a deeper look into each step.&lt;/p&gt;
&lt;h3 id=&#34;step-1-define-the-objectives&#34;&gt;Step 1: Define the Objectives&lt;/h3&gt;
&lt;p&gt;Defining DOE objectives involves clearly articulating the specific goals or outcomes you aim to achieve through the experimental process. These objectives guide the entire DOE process. Objectives can vary based on the context of the experiment but generally include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimization:&lt;/strong&gt; Determine the optimal combination of factors and levels to achieve the best outcome or performance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Understanding and Prioritizing Factors:&lt;/strong&gt; Gain insights into the individual and interactive effects of factors on the outcome to better understand the system, and identify the most influential factors that significantly impact the response variable.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Process and Quality Improvement:&lt;/strong&gt; Improve the efficiency, quality, or performance of a process, system, or a product.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cost Reduction and Resource Optimization:&lt;/strong&gt; Identify factors that minimize costs or optimize resource utilization without sacrificing desired outcomes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Robustness Testing and Troubleshooting:&lt;/strong&gt; Assess the robustness of a process by understanding how variations in factors affect the outcome, and Identify and resolve issues or challenges within a process.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Model Validation:&lt;/strong&gt; Validate mathematical or predictive models by comparing their predictions with experimental results.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;step-2-identify-factors-and-their-levels&#34;&gt;Step 2: Identify Factors and their Levels&lt;/h3&gt;
&lt;p&gt;Factors are the independent variables that may influence the outcome (dependent variables) of your experiment, while levels represent the different values or settings each factor can take during the experiment.&lt;/p&gt;
&lt;p&gt;Identifying and prioritizing experimental factors requires a good knowledge of the system. It may be useful to first conduct some pilot studies, or initial exploratory experiments, to gather additional information about the individual impact of each factor.&lt;/p&gt;
&lt;p&gt;When identifying factors, consider both controllable and uncontrollable factors. Categorize factors as either independent variables (factors you can control) or nuisance variables (factors that may affect the outcome but are difficult to control). Prioritize factors based on their expected impact on the outcome and their controllability. Focus on factors that are most likely to influence the response variable. Choose a manageable number of factors to keep the study focused and efficient.&lt;/p&gt;
&lt;p&gt;Lastly, for each identified factor, determine the different levels or settings that will be tested during the experiment. Levels should cover the practical range of values for each factor. Pilot studies are very helpful in determining the useful &amp;ldquo;design space&amp;rdquo; that the levels of each factor define.&lt;/p&gt;
&lt;h3 id=&#34;step-3-select-a-design&#34;&gt;Step 3: Select a Design&lt;/h3&gt;
&lt;p&gt;There are a few types of DOE designs, each suited to different experimental goals, stage of the study, and resource constraints. Here are some common DOE design types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Full Factorial Design:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Examines all possible combinations of factor levels. It provides a complete picture of how each factor and their interactions affect the response variable. Full factorial designs are often too costly to run, since the sample size grows exponentially with the number of factors and levels.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fractional Factorial Design:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Investigates a fraction of the total possible combinations, significantly reducing the number of experimental runs. It is useful when the full factorial design is impractical due to resource constraints. Often used for screening and pilot studies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Response Surface Methodology (RSM):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Focuses on modeling and optimizing the relationship between independent variables (factors) and a response variable. It often involves a sequence of factorial or fractional factorial experiments. To appropriately characterize the curvature of the response surface, each factor needs to be measured on at least three levels. Some design subtypes that fall under RSM umbrella include Central Composite Design (CCD) and Box-Behnken Design.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There is a wide variety of more advanced and model specific designs, which may be worth considering in the later stage of system characterization.&lt;/p&gt;
&lt;h3 id=&#34;step-4-conduct-experiments&#34;&gt;Step 4: Conduct Experiments&lt;/h3&gt;
&lt;p&gt;Before executing the experimental plan, make sure to use effective randomization schemes and build in appropriate replication to minimize the impact of external variables and ensure the reliability of results. Performing the experiments is usually the hard part. When running the experiments, it is important to follow the usual best practices for experimental work: plan carefully, make sure your equipment is well calibrated and maintained, and record everything accurately.&lt;/p&gt;
&lt;h3 id=&#34;step-5-analyze-the-data&#34;&gt;Step 5: Analyze the Data&lt;/h3&gt;
&lt;p&gt;Analysis of the data usually includes the following steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Review the data usign basic descriptive statistics approaches to gain a general understanding of the sources of variability, wheter the data follows the normal distribution, and whether linear or non-linear models would be more appropriate. Popular approaches include box plots, histograms, Q-Q plots, etc.&lt;/li&gt;
&lt;li&gt;Use DOE-specific plots to rank the importance of the factors and to narrow down on the more useful set of their levels. Such tools include the main effects mean plots, interaction plots, and normal or half-normal plots of effects. Sometimes, these visuals may provide clear answers to your experimental questions, allowing you to proceed directly to step 5.&lt;/li&gt;
&lt;li&gt;Construct a model tailored to address your objectives. Use a linear model when possible, simplify  using stepwise regression methods or considering parameter p-value significance. Assess the model&amp;rsquo;s assumptions through residual graphs. If assumptions hold, proceed to examine the ANOVA and further simplify the model if necessary.&lt;/li&gt;
&lt;li&gt;If model assumptions are violated, investigate potential causes, such as missing terms or the need for a response transformation.&lt;/li&gt;
&lt;li&gt;Finally, using new newfound model address your experimental objectives, whether it involves identifying significant factors or determining optimal settings.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;DOE provides a structured path from experimentation to understanding. By treating experimentation as a deliberate and statistically informed process, DOE not only saves time and resources but also deepens insight into how complex systems truly behave.&lt;/p&gt;
&lt;p&gt;Future notes will dive deeper into specific design types, model interpretation, and optimization techniques.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Design of Experiments (DOE)&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
						&lt;li&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/doe-factorial-tutorial/&#39;&gt;Full Factorial Tutorial&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>SDTM vs. CDASH: Why do we need two standards?</title>
      <link>http://notes.mirzo.net/post/sdtm-vs-cdash/</link>
      <pubDate>Fri, 10 Feb 2023 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/sdtm-vs-cdash/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/sdtm-vs-cdash/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/sdtm_vs_cdash1_hue8c39bbf1cfc963d4791c52fc44d037d_1348910_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled SDTM vs. CDASH: Why do we need two standards?&#34; title=&#34;SDTM vs. CDASH: Why do we need two standards?&#34;&gt;&lt;/a&gt;


&lt;p&gt;In the realm of clinical research and data management, adherence to standardized formats and structures is paramount. Two such standards, CDASH (Clinical Data Acquisition Standards Harmonization) and SDTM (Study Data Tabulation Model), play crucial roles in ensuring consistency and interoperability across clinical trial data. There is a lot of overlap between CDASH and SDTM, which often creates a false impression that they serve the same purpose, or even compete with each other. In reality, the two standards are designed to complement each other, while addressing similar but distinct needs. In this note, we delve into the key differences between these two standards to better understand their respective roles in the clinical research landscape.&lt;/p&gt;
&lt;h3 id=&#34;cdash-streamlining-data-collection&#34;&gt;CDASH: Streamlining Data Collection&lt;/h3&gt;
&lt;p&gt;CDASH focuses primarily on data collection during the clinical trial process. It provides a standardized set of data collection fields, forms, and guidelines to facilitate the uniform capture of clinical trial data across different studies and sites. CDASH aims to improve efficiency, reduce errors, and enhance data quality by establishing a common language for data collection.&lt;/p&gt;
&lt;p&gt;Key Features of CDASH:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Standardized Data Collection Forms&lt;/strong&gt;: CDASH provides standardized data collection forms known as Case Report Forms (CRFs), which outline the specific data points to be collected during a clinical trial. These CRFs are designed to capture essential information relevant to the study protocol.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Collection Guidelines&lt;/strong&gt;: CDASH offers guidelines for data collection, defining terminology and specifying how data should be recorded and organized. By adhering to these guidelines, researchers ensure consistency and clarity in their data collection processes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Interoperability&lt;/strong&gt;: CDASH promotes interoperability by enabling data exchange and integration between different systems and platforms. This interoperability facilitates data sharing and collaboration among researchers, sponsors, and regulatory authorities.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;sdtm-standardizing-data-tabulation-and-submission&#34;&gt;SDTM: Standardizing Data Tabulation and Submission&lt;/h3&gt;
&lt;p&gt;While CDASH focuses on data collection, SDTM comes into play during the analysis and submission phases of a clinical trial. SDTM provides a standardized framework for organizing and formatting clinical trial data to support regulatory submissions to agencies.&lt;/p&gt;
&lt;p&gt;Key Features of SDTM:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tabulation Model&lt;/strong&gt;: SDTM defines a tabulation model that structures clinical trial data into standardized domains, variables, and datasets. This structured format facilitates data analysis, review, and regulatory evaluation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Mapping&lt;/strong&gt;: SDTM requires mapping of collected data from CDASH CRFs to SDTM domains and variables. This mapping process ensures that data collected according to CDASH standards are transformed into a format compatible with SDTM.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regulatory Compliance&lt;/strong&gt;: SDTM compliance is essential for regulatory submissions. Regulatory agencies expect clinical trial data to be organized and formatted according to SDTM standards to facilitate review and analysis.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;key-differences&#34;&gt;Key Differences&lt;/h3&gt;
&lt;p&gt;SDTM is geared towards organizing finalized CRF data in a structured format conducive to data transmission, review, and reuse. In contrast, CDASH emphasizes user-friendly data collection methods to enhance data quality and seamless integration with SDTM.&lt;/p&gt;
&lt;p&gt;For instance, when considering missing data, SDTM assumes cleanliness and doesn&amp;rsquo;t include placeholders for missing data. Conversely, CDASH acknowledges the absence of evidence is not evidence of absence, necessitating verification of missing data. This is exemplified in CDASH&amp;rsquo;s inclusion of indicator questions like &amp;ldquo;Were any adverse events experienced?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Furthermore, non-standard variables are managed differently. While SDTM segregates them into separate datasets linked via RELREC, CDASH incorporates them within the parent data. Additionally, CDASH prioritizes human-readable data formats, while SDTM focuses on machine-readability for enhanced reusability.&lt;/p&gt;
&lt;p&gt;Data organization also varies; CDASH allows flexibility in grouping variables across domains on CRFs, while SDTM mandates strict domain-based organization. Similarly, SDTM favors vertical data structures, whereas CDASH may require horizontal data structuring. This stems from the fact that it may not be possible to generate all encompassing codelists during the study, before all of the data has been collected. Hence, CDASH allows horizontal data structures which maintain information relationships without the need for coding or normalization. In SDTM, proper data coding and normalization is expected.&lt;/p&gt;
&lt;p&gt;Overall, while CDASH ensures data traceability and quality, it seamlessly integrates with SDTM. Utilizing both standards optimizes data flow, ensures consistency, and enhances data quality from collection to analysis and beyond the initial study.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Mermaid: The Cheatsheet</title>
      <link>http://notes.mirzo.net/post/mermaid-cheatsheet/</link>
      <pubDate>Sun, 08 Jan 2023 12:00:00 -0400</pubDate>
      
      <guid>http://notes.mirzo.net/post/mermaid-cheatsheet/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/mermaid-cheatsheet/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/mermaid2_hufae0ff24fb6e74d7eba4c151ebc86fc8_1771483_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Mermaid: The Cheatsheet&#34; title=&#34;Mermaid: The Cheatsheet&#34;&gt;&lt;/a&gt;


&lt;p&gt;&lt;a href=&#34;https://mermaid.js.org&#34;&gt;Mermaid.js&lt;/a&gt; is one of the coolest and nerdiest projects I&amp;rsquo;ve seen in a while. I mean, you write code to generate diagrams!&lt;/p&gt;
&lt;p&gt;And we all know that diagrams make everything better. Here, let me show you with a diagram!&lt;/p&gt;
&lt;pre class=&#34;mermaid&#34;&gt;journey
      Without Diagrams: 0
      With Diagrams: 7
&lt;/pre&gt;

&lt;h3 id=&#34;start-with-a-diagram-type&#34;&gt;Start with a diagram type&lt;/h3&gt;
&lt;p&gt;I typically only need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;flowchart&lt;/li&gt;
&lt;li&gt;classDiagram&lt;/li&gt;
&lt;li&gt;gantt&lt;/li&gt;
&lt;li&gt;pie&lt;/li&gt;
&lt;li&gt;quadrantChart&lt;/li&gt;
&lt;li&gt;mindmap&lt;/li&gt;
&lt;li&gt;timeline&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;then-give-a-direction&#34;&gt;Then give a direction&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;LR: Left to right&lt;/li&gt;
&lt;li&gt;TD/TB: Top-down&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;flowchart-example&#34;&gt;Flowchart example&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;You can first declare element by &lt;code&gt;ID&lt;/code&gt; and &lt;code&gt;Label&lt;/code&gt; and then call on them just by &lt;code&gt;ID&lt;/code&gt; later.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;flowchart TD
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ThumbsUp[&amp;#34;Quotes to include unicode👍&amp;#34;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;SlightlyRounded(&amp;#34;It&amp;#39;s nealry inperceptive!&amp;#34;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Rounded([Not all labels are made with quotes])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;JustIDnoLabel
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Square[[So many brackets]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Cylinder[(Wow, even 3D shapes!)]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Circle((Classic!))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Diamond{$$$}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;style Diamond fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;classDef pinky fill:#f9f,stroke:#333,stroke-width:4px
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;class Square,Cylinder pinky
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Circle:::pinky
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;subgraph collection
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Square &amp;amp; JustIDnoLabel
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;end
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;SlightlyRounded --&amp;gt; ThumbsUp
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;SlightlyRounded -.-&amp;gt; Rounded
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Rounded &amp;amp; Circle --&amp;gt; JustIDnoLabel
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;JustIDnoLabel --&amp;gt; Square
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ThumbsUp --- Square
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ThumbsUp --&amp;gt; SlightlyRounded
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Rounded -- What kind of connection is this? --&amp;gt; Cylinder
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Rounded ==&amp;gt; Circle
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Rounded --&amp;gt; Diamond --&amp;gt; Circle
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Diamond &amp;lt;--&amp;gt; Cylinder
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre class=&#34;mermaid&#34;&gt;flowchart TD

ThumbsUp[&#34;Quotes to include unicode👍&#34;]
SlightlyRounded(&#34;It&#39;s nealry inperceptive!&#34;)
Rounded([Not all labels are made with quotes])
JustIDnoLabel
Square[[So many brackets]]
Cylinder[(Wow, even 3D shapes!)]
Circle((Classic!))
Diamond{$$$}
style Diamond fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5
classDef pinky fill:#f9f,stroke:#333,stroke-width:4px
class Square,Cylinder pinky
Circle:::pinky

subgraph collection
  Square &amp; JustIDnoLabel
end

SlightlyRounded --&gt; ThumbsUp
SlightlyRounded -.-&gt; Rounded
Rounded &amp; Circle --&gt; JustIDnoLabel
JustIDnoLabel --&gt; Square
ThumbsUp --- Square
ThumbsUp --&gt; SlightlyRounded
Rounded -- What kind of connection is this? --&gt; Cylinder
Rounded ==&gt; Circle
Rounded --&gt; Diamond --&gt; Circle
Diamond &lt;--&gt; Cylinder
&lt;/pre&gt;</description>
      
    </item>
    
    <item>
      <title>Hugo: The Basics</title>
      <link>http://notes.mirzo.net/post/hugo-basics/</link>
      <pubDate>Fri, 16 Sep 2022 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/hugo-basics/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/hugo-basics/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/hugo1_hu536e235dc62f90050fc805d99dfc85ad_747909_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Hugo: The Basics&#34; title=&#34;Hugo: The Basics&#34;&gt;&lt;/a&gt;


&lt;p&gt;&lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt; is an open-source static site generator (SSG) designed for speed and efficiency. It offers sub-second rendering times, freedom to organize your content to your heart&amp;rsquo;s desire, and a good toolset for setting up advanced file processing pipelines. Having &lt;del&gt;suffered through&lt;/del&gt; dealt with many SSGs before, I was pleasantly surprised by the simplicity × power factor that Hugo offers. Here I document my experimentation with this tool.&lt;/p&gt;
&lt;h2 id=&#34;installation&#34;&gt;Installation&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://gohugo.io/installation&#34;&gt;Installation&lt;/a&gt; is quite straight forward. You just get a binary for your system and write it into your PATH. It has a single mandatory dependency (Dart Sass), but you might also want to install GO lang, if you feel like breaking up your project into &lt;a href=&#34;https://gohugo.io/hugo-modules/&#34;&gt;modules&lt;/a&gt; (but that&amp;rsquo;s for later).&lt;/p&gt;
&lt;h2 id=&#34;basic-cli-commands&#34;&gt;Basic CLI commands&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Create a new site skeleton: &lt;code&gt;hugo new site your-site-name&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Create a new content page from an &lt;em&gt;archetype&lt;/em&gt; template: &lt;code&gt;hugo new content/page-name.md&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Start a preview server: &lt;code&gt;hugo server -D&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Generate site: &lt;code&gt;hugo&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;configuration&#34;&gt;Configuration&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;hugo.toml&lt;/code&gt; file houses all of the configuration, but it starts off pretty sparse. I found this a good place to start:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;baseURL&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;http://mysite.com/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;title&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;A very cool site!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;theme&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;cooltheme&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;paginate&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;rssLimit&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;copyright&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Me!!!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;defaultContentLanguage&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;en&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[&lt;span style=&#34;color:#a6e22e&#34;&gt;languages&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;en&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;languageCode&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;en&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[&lt;span style=&#34;color:#a6e22e&#34;&gt;menu&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Homepage&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;weight&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;identifier&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;home&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pageref&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[&lt;span style=&#34;color:#a6e22e&#34;&gt;menu&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Tags&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;weight&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;20&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;identifier&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;tags&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pageref&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/tags/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[&lt;span style=&#34;color:#a6e22e&#34;&gt;menu&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Archive&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;weight&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;30&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;identifier&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;archives&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pageref&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/post/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[&lt;span style=&#34;color:#a6e22e&#34;&gt;menu&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;About&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;weight&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;40&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;identifier&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;about&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;pageref&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/about/&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[[&lt;span style=&#34;color:#a6e22e&#34;&gt;menu&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;My other site&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;weight&lt;/span&gt; = &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;rel&lt;/span&gt;= &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;external&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;url&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;http://othercoolsite.com&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[&lt;span style=&#34;color:#a6e22e&#34;&gt;params&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;keywords&lt;/span&gt; = [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;cool stuff&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;awesomeness&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;just rad&amp;#34;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;description&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;This is my awesome site that is just so cool and rad!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;themes&#34;&gt;Themes&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://themes.gohugo.io/&#34;&gt;Theme&lt;/a&gt; selection for Hugo is pretty decent. If you want a quick start, download one into the &lt;em&gt;themes&lt;/em&gt; folder, and just copy over the appropriate configs. For a lot of people that would be it, you have your site now! But that is not how we roll. Here, we like to suffer through our own design and templating!&lt;/p&gt;
&lt;h2 id=&#34;content&#34;&gt;Content&lt;/h2&gt;
&lt;p&gt;Hugo expects your content to be organized within a specific directory structure. By default, Hugo uses a &lt;code&gt;content&lt;/code&gt; directory at the root of your Hugo project. Inside this directory, you should have subdirectories representing the different sections of your site. For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;content/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── blog/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   ├── my-first-post.md
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   ├── my-second-post.md
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   └── another-post/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│       └── index.md
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── news/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   ├── latest-news.md
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   └── older-news.md
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;└── about.md
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;blog/&lt;/code&gt;, &lt;code&gt;news/&lt;/code&gt;, and &lt;code&gt;about.md&lt;/code&gt; are sections of your site.&lt;/li&gt;
&lt;li&gt;Each Markdown file (&lt;code&gt;.md&lt;/code&gt;) represents a piece of content like a blog post or a page.
&lt;ul&gt;
&lt;li&gt;You can use html files as well, but it somewhat defeats the purpose of templating. It is only very useful for singular pages that present content in ways that are different from the rest of your site.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Subdirectories within sections (e.g., &lt;code&gt;another-post/&lt;/code&gt;) can represent nested content (e.g., for hierarchical pages or sub-sections).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each Markdown file in Hugo should start with &lt;a href=&#34;https://gohugo.io/content-management/front-matter/&#34;&gt;front matter&lt;/a&gt;. Front matter is written in YAML, TOML, or JSON and provides Hugo with essential information about the content. Here’s an example of front matter in a Markdown file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;title: &amp;#34;My First Post&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;date: 2024-04-25
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;draft: false
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Content goes here.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;title&lt;/code&gt; specifies the title of the content.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;date&lt;/code&gt; is the publication date.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;draft&lt;/code&gt; indicates whether the content is a draft (&lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hugo supports &lt;a href=&#34;https://gohugo.io/content-management/taxonomies/&#34;&gt;taxonomies&lt;/a&gt; like categories and tags, which are useful for organizing and filtering content. Taxonomies are defined in your site configuration (&lt;code&gt;config.toml&lt;/code&gt;). For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[&lt;span style=&#34;color:#a6e22e&#34;&gt;taxonomies&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;tag&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;tags&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;category&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;categories&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can then assign tags or categories to your content in the front matter of each Markdown file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;title: &amp;#34;My First Post&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;tags: [&amp;#34;technology&amp;#34;, &amp;#34;Hugo&amp;#34;, &amp;#34;static site&amp;#34;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;categories: [&amp;#34;tutorial&amp;#34;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hugo allows you to define custom content types, which can have their own templates and behavior. Content types are typically defined in the &lt;code&gt;config.toml&lt;/code&gt; file. For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[&lt;span style=&#34;color:#a6e22e&#34;&gt;types&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#75715e&#34;&gt;# Define a custom content type &amp;#34;product&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  [[&lt;span style=&#34;color:#a6e22e&#34;&gt;types&lt;/span&gt;]]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;description&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Products for sale&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;layout&lt;/span&gt; = &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;product&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#a6e22e&#34;&gt;isPlainText&lt;/span&gt; = &lt;span style=&#34;color:#66d9ef&#34;&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;templating&#34;&gt;Templating&lt;/h2&gt;
&lt;p&gt;We have content, now let&amp;rsquo;s learn how to apply a hierarchy of templates to render the content. We will go over a basic but functional example.&lt;/p&gt;
&lt;p&gt;There is a certain &lt;a href=&#34;https://gohugo.io/templates/lookup-order/#home-page&#34;&gt;hierarchy&lt;/a&gt; of templating in Hugo. The scope of the template defines which pages it applies to. As a basic example, head on over to the &lt;code&gt;layouts&lt;/code&gt; folder and create a &lt;code&gt;_default&lt;/code&gt; subfolder. In this folder, let&amp;rsquo;s create two files &lt;code&gt;baseof.html&lt;/code&gt; and &lt;code&gt;single.html&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;baseof.html&lt;/code&gt; defines the base template of all of our html documents. It will contain html that is common to all pages on our website.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;baseof.html:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;html&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;lang&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;{{ $.Site.LanguageCode | default &amp;#34;en&amp;#34; }}&amp;#39;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;meta&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;charset&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;utf-8&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;title&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      {{- block &amp;#34;title&amp;#34; . -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {{ if .Title }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          {{- .Title }} × {{ .Site.Title -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {{ else }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          {{- .Site.Title -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        {{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      {{- end -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;title&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  {{ partial &amp;#34;head.html&amp;#34; . }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  {{ block &amp;#34;head&amp;#34; . }}{{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;head&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ block &amp;#34;body&amp;#34; . }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;body&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  {{ partial &amp;#34;scripts.html&amp;#34; . }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;html&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now let&amp;rsquo;s look at &lt;code&gt;single.html&lt;/code&gt;, a template for rendering a single piece of content (e.g., a blog post).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;single.html:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ define &amp;#34;head&amp;#34; }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  {{ $style := resources.Get &amp;#34;sass/single.scss&amp;#34; | toCSS  }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;link&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;rel&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;stylesheet&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;href&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{{ $style.RelPermalink }}&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;media&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;screen&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;crossorigin&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;anonymous&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{end}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ define &amp;#34;body&amp;#34;}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;id&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;menu&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;id&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;menu-links&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          {{ range .Site.Menus.main }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;menu-item&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;a&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;homepage-menu-link&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;href&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{{ .URL | safeURL }}&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                    {{ .Name }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                  &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;a&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          {{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;div&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;main&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;id&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;content&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;role&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;main&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      {{ .Content }}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;main&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There is a lot going on here, so let&amp;rsquo;s break things down. Hugo uses GO lang’s &lt;a href=&#34;https://pkg.go.dev/text/template&#34;&gt;text/template&lt;/a&gt; and &lt;a href=&#34;https://pkg.go.dev/html/template&#34;&gt;html/template&lt;/a&gt; packages. We write the static portions of any document in the native html format, and introduce dynamic content within code elements enclosed in double curly brackets.&lt;/p&gt;
&lt;h3 id=&#34;blocks-and-defines&#34;&gt;Blocks and Defines&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;block&lt;/code&gt; is the most basic templating element, and it essentially behaves like a placeholder with a default value. It delimits a region within the template that can be easily substituted within the final document.&lt;/p&gt;
&lt;p&gt;The basic structure of a block:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ block &amp;#34;block_name&amp;#34;}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;p&lt;/span&gt;&amp;gt;Default content.&amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;p&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here we define the block name and enclose the default content to appear if no override information is defined.&lt;/p&gt;
&lt;p&gt;In any document (or a higher level template) that uses this base template, we will use the &lt;code&gt;define&lt;/code&gt; element to populate the values of blocks with their final content:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ define &amp;#34;block_name&amp;#34;}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;p&lt;/span&gt;&amp;gt;Content that will actually appear.&amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;p&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{ end }}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may have noticed that some blocks in our &lt;code&gt;baseof.html&lt;/code&gt; example look a little different.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{- block &amp;#34;title&amp;#34; . -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{- end -}}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The dashes at the beginning and end of the code elements are calls to the &amp;ldquo;trim spaces&amp;rdquo; function provided by Hugo. This tells the template that leading and trailing spaces introduced within the corresponding &lt;code&gt;define&lt;/code&gt; element will be removed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The dot after the block name tells Hugo that the information context from the template is being passed to the higher level document or template.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In our example, &lt;code&gt;baseof.html&lt;/code&gt; contains blocks for the document title, the head tag, and the body of the document. In &lt;code&gt;single.html&lt;/code&gt; we &lt;em&gt;define&lt;/em&gt; the information that gets populated when the two templates get merged during rendering. Specifically:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Within the &lt;em&gt;head&lt;/em&gt; block we use a Hugo function to render a SASS file to CSS and then to link the resulting style sheet to the page.&lt;/li&gt;
&lt;li&gt;Within the &lt;em&gt;body&lt;/em&gt; block:
&lt;ul&gt;
&lt;li&gt;We pull the information about the site&amp;rsquo;s menu from the page&amp;rsquo;s context, and use Hugo&amp;rsquo;s &lt;em&gt;range&lt;/em&gt; function to iterate over and present each of the menu elements.&lt;/li&gt;
&lt;li&gt;Define where the page&amp;rsquo;s content will appear.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;We do not define the &lt;em&gt;title&lt;/em&gt; block in &lt;code&gt;single.html&lt;/code&gt;, which means that the default value from &lt;code&gt;baseof.html&lt;/code&gt; will be used.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;partials&#34;&gt;Partials&lt;/h3&gt;
&lt;p&gt;In addition to blocks, our &lt;code&gt;baseof.html&lt;/code&gt; template contains calls to &lt;code&gt;partials&lt;/code&gt;, which are context-aware reusable  components. Partials are saved under the &lt;code&gt;layouts/partials&lt;/code&gt; subfolder, and are referenced by their filename, e.g., &lt;code&gt;{{ partial &amp;quot;head.html&amp;quot; . }}&lt;/code&gt;. Here is an example of a partial we use in our head tag:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{- if or .Params.author .Site.Author.name -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{- $author := .Params.author | default .Site.Author.name -}}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;meta&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;name&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;author&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;content&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;{{ $author | safeHTML }}&amp;#34;&lt;/span&gt; /&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{{- end -}}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here is what is going on in this example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We are using the &lt;code&gt;if&lt;/code&gt; logical statement to check if the specific page&amp;rsquo;s front matter provides a value for author&amp;rsquo;s name, and otherwise take site wide value for author. We then assign it to the &lt;code&gt;$author&lt;/code&gt; variable and then render it within an html meta tag.&lt;/li&gt;
&lt;li&gt;We use a &lt;code&gt;safeHTML&lt;/code&gt; function to declare a provided string as a “safe” HTML element to avoid escaping by Go templates.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can re-use this partial in the head section of any template we develop.&lt;/p&gt;
&lt;h3 id=&#34;shortcodes&#34;&gt;Shortcodes&lt;/h3&gt;
&lt;p&gt;Hugo&amp;rsquo;s templating language is designed to be used in the layout elements. You cannot use these dynamic features of Hugo in your content files directly. However, you can package your dynamic code within &lt;em&gt;Shortcodes&lt;/em&gt;,  function-like snippets of code that can be called from within your content files. They are essentially like partials, except you use them in Markdown, instead of layout templates.&lt;/p&gt;
&lt;p&gt;To make a shortcode:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Save an html file in &lt;code&gt;layouts\shortcodes&lt;/code&gt;. The filename is the name of the shortcode.&lt;/li&gt;
&lt;li&gt;Write html and/or Go template functions and variables.&lt;/li&gt;
&lt;li&gt;You can pass attributes to a shortcode. Get the positional arguments by &lt;code&gt;{{ .Get 0 }}&lt;/code&gt;, and the named arguments by &lt;code&gt;{{ .Get &amp;quot;namedAttribute&amp;quot; }}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Insert your shortcode in your Markdown content: &lt;code&gt;{{&amp;lt; shortcodeName positional namedAttribute=&amp;quot;value&amp;quot;&amp;gt;}}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Hugo stands out as a powerful and efficient static site generator ideal for developers looking to streamline their website development process. From its swift installation to the versatile configuration options, Hugo simplifies the complex task of site generation without sacrificing flexibility or control. The basic command line interface commands, combined with advanced templating and content organization features, empower users to create and manage sophisticated sites with ease. Moreover, the support for custom content types and taxonomies enhances Hugo&amp;rsquo;s ability to handle diverse content needs. Whether you&amp;rsquo;re a seasoned developer or a newcomer to static site generation, Hugo offers a robust framework that can adapt to various project requirements, making it a compelling choice for building fast, reliable, and maintainable websites.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Hugo: Formatting Cheatsheet</title>
      <link>http://notes.mirzo.net/post/hugo-formatting-cheatsheet/</link>
      <pubDate>Wed, 14 Sep 2022 12:00:00 -0400</pubDate>
      
      <guid>http://notes.mirzo.net/post/hugo-formatting-cheatsheet/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/hugo-formatting-cheatsheet/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/programmer1_hu536e235dc62f90050fc805d99dfc85ad_563047_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Hugo: Formatting Cheatsheet&#34; title=&#34;Hugo: Formatting Cheatsheet&#34;&gt;&lt;/a&gt;


&lt;p&gt;Here is a Markdown-formatted list about the things that I always forget about Markdown.&lt;/p&gt;
&lt;p&gt;First, a self-reminder on how to create a new article: &lt;code&gt;hugo new content post/page-name.md&lt;/code&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A note about Hugo Shortcodes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since this site is built in Hugo, I cannot properly show a code example of a Hugo shortcode, as it will be automatically rendered. Since there is no official way to escape shortcodes, the convention I take here is to insert a space between the first two curly brakets. This space needs to be removed to yield properly functioning snippet: &lt;code&gt;{ {&amp;lt; shortcode parameter=&amp;quot;value&amp;quot; &amp;gt;}}&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;images&#34;&gt;Images&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;![&lt;span style=&#34;color:#f92672&#34;&gt;Alt text&lt;/span&gt;](&lt;span style=&#34;color:#a6e22e&#34;&gt;/path/to/img.jpg&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;![&lt;span style=&#34;color:#f92672&#34;&gt;Alt text&lt;/span&gt;](&lt;span style=&#34;color:#a6e22e&#34;&gt;/path/to/img.jpg &amp;#34;Optional title&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Or you can use the built-in &lt;em&gt;Figure&lt;/em&gt; shortcode (&lt;a href=&#34;https://gohugo.io/content-management/shortcodes/&#34;&gt;full description&lt;/a&gt;).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;{ {&amp;lt; &lt;span style=&#34;color:#f92672&#34;&gt;figure&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;src&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;title&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;caption&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;link&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;target&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;alt&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;class&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;&amp;gt;}} 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;syntax-highlighting&#34;&gt;Syntax Highlighting&lt;/h2&gt;
&lt;p&gt;Specify language after triple ticks:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;```language
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;```&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;text:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;plain text
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;shell:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sudo rm -rf / &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt; &amp;gt; /dev/sda
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;js:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-js&#34; data-lang=&#34;js&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;function&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;helloWorld&lt;/span&gt; () {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#a6e22e&#34;&gt;alert&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Hello, World!&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;java {style=github}&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#998;font-style:italic&#34;&gt;// github style
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#998;font-style:italic&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#458;font-weight:bold&#34;&gt;HelloWorld&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;public&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#458;font-weight:bold&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#900;font-weight:bold&#34;&gt;main&lt;/span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;(&lt;/span&gt;String&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;[]&lt;/span&gt; args&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;)&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    System&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color:#008080&#34;&gt;out&lt;/span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;.&lt;/span&gt;&lt;span style=&#34;color:#008080&#34;&gt;println&lt;/span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color:#d14&#34;&gt;&amp;#34;Hello, World!&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;math-blocks&#34;&gt;Math Blocks&lt;/h2&gt;
&lt;p&gt;Render &lt;em&gt;LaTeX&lt;/em&gt; using &lt;strong&gt;MathJax&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-latex&#34; data-lang=&#34;latex&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;$$&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\mathbf{V}_&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; \times \mathbf{V}_&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;  \begin{vmatrix}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\mathbf{i} &amp;amp; \mathbf{j} &amp;amp; \mathbf{k} \\
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\frac{\partial X}{\partial u} &amp;amp;  \frac{\partial Y}{\partial u} &amp;amp; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt; \\
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\frac{\partial X}{\partial v} &amp;amp;  \frac{\partial Y}{\partial v} &amp;amp; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt; \\
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\end{vmatrix}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;$$&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;$$
\mathbf{V}_1 \times \mathbf{V}_2 =  \begin{vmatrix}
\mathbf{i} &amp;amp; \mathbf{j} &amp;amp; \mathbf{k} \
\frac{\partial X}{\partial u} &amp;amp;  \frac{\partial Y}{\partial u} &amp;amp; 0 \
\frac{\partial X}{\partial v} &amp;amp;  \frac{\partial Y}{\partial v} &amp;amp; 0 \
\end{vmatrix}
$$&lt;/p&gt;
&lt;p&gt;For inline math, i.e. \( \mathbb{R} \), use &lt;code&gt;\\(&lt;/code&gt; and &lt;code&gt;\\)&lt;/code&gt; delimiters.&lt;/p&gt;
&lt;p&gt;You can label an equation using a &lt;code&gt;\tag{}&lt;/code&gt; (visible) and a &lt;code&gt;\label{}&lt;/code&gt; (for reference):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-latex&#34; data-lang=&#34;latex&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;$$&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;y &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; mx &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; b
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\tag{&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;\label{eq_of_line}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;$$&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;$$
y = mx + b
\tag{1}
\label{eq_of_line}
$$&lt;/p&gt;
&lt;p&gt;And then refer to the equation using &lt;code&gt;\eqref{label}&lt;/code&gt;: \( \eqref{eq_of_line} \)&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-latex&#34; data-lang=&#34;latex&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;\\&lt;/span&gt;( &lt;span style=&#34;color:#66d9ef&#34;&gt;\eqref&lt;/span&gt;{eq_of_line} &lt;span style=&#34;color:#66d9ef&#34;&gt;\\&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;tables&#34;&gt;Tables&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| Name              | Markdown            | HTML tag             |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| ----------------- | ------------------- | -------------------- |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| &lt;span style=&#34;font-style:italic&#34;&gt;*Emphasis*&lt;/span&gt;        | &lt;span style=&#34;color:#e6db74&#34;&gt;`*Emphasis*`&lt;/span&gt;        | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;em&amp;gt;&amp;lt;/em&amp;gt;`&lt;/span&gt;          |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| &lt;span style=&#34;font-weight:bold&#34;&gt;**Strong**&lt;/span&gt;        | &lt;span style=&#34;color:#e6db74&#34;&gt;`**Strong**`&lt;/span&gt;        | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;strong&amp;gt;&amp;lt;/strong&amp;gt;`&lt;/span&gt; |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| &lt;span style=&#34;color:#e6db74&#34;&gt;`code`&lt;/span&gt;            | ``code``            | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;`&lt;/span&gt;      |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| &lt;span style=&#34;color:#f92672&#34;&gt;~~Strikethrough~~&lt;/span&gt; | &lt;span style=&#34;color:#e6db74&#34;&gt;`~~Strikethrough~~`&lt;/span&gt; | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;del&amp;gt;&amp;lt;/del`&lt;/span&gt;         |
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;| &amp;lt;&lt;span style=&#34;color:#f92672&#34;&gt;u&lt;/span&gt;&amp;gt;Underline&amp;lt;/&lt;span style=&#34;color:#f92672&#34;&gt;u&lt;/span&gt;&amp;gt;  | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;u&amp;gt;underline&amp;lt;/u&amp;gt;`&lt;/span&gt;  | &lt;span style=&#34;color:#e6db74&#34;&gt;`&amp;lt;u&amp;gt;&amp;lt;/u&amp;gt;`&lt;/span&gt;            |
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Markdown&lt;/th&gt;
&lt;th&gt;HTML tag&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Emphasis&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;*Emphasis*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;em&amp;gt;&amp;lt;/em&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Strong&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;**Strong**&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt; &amp;lt;strong&amp;gt;&amp;lt;/strong&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;code&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;`code`&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;del&gt;Strikethrough&lt;/del&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;~~Strikethrough~~&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;del&amp;gt;&amp;lt;/del&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;!-- raw HTML omitted --&gt;Underline&lt;!-- raw HTML omitted --&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;u&amp;gt;underline&amp;lt;/u&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;u&amp;gt;&amp;lt;/u&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&#34;links&#34;&gt;Links&lt;/h2&gt;
&lt;h3 id=&#34;inline-links&#34;&gt;Inline Links&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;This is [&lt;span style=&#34;color:#f92672&#34;&gt;an example&lt;/span&gt;](&lt;span style=&#34;color:#a6e22e&#34;&gt;http://example.com/ &amp;#34;Title&amp;#34;&lt;/span&gt;) inline link.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[&lt;span style=&#34;color:#f92672&#34;&gt;This link&lt;/span&gt;](&lt;span style=&#34;color:#a6e22e&#34;&gt;http://example.net/&lt;/span&gt;) has no title attribute.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Links to internal content use the &lt;em&gt;ref&lt;/em&gt; (produces an absolute reference) or &lt;em&gt;relref&lt;/em&gt; (produces an relative reference) shortcodes.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;My other [&lt;span style=&#34;color:#f92672&#34;&gt;awesome post&lt;/span&gt;](&lt;span style=&#34;color:#a6e22e&#34;&gt; { { &amp;lt; relref &amp;#34;/post/awesome.md&amp;#34; &amp;gt; }}&lt;/span&gt;).
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;reference-links&#34;&gt;Reference Links&lt;/h3&gt;
&lt;p&gt;Reference-style links use a second set of square brackets, inside which you place a label of your choosing to identify the link:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;This is [an example][id] reference-style link.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Then, anywhere in the document, you define your link label like this, on a line by itself:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[id]: http://example.com/  &amp;#34;Optional Title Here&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is &lt;a href=&#34;http://example.com/&#34; title=&#34;Optional Title Here&#34;&gt;an example&lt;/a&gt; reference-style link.&lt;/p&gt;
&lt;p&gt;The implicit link name shortcut allows you to omit the name of the link, in which case the link text itself is used as the name. Just use an empty set of square brackets — e.g., to link the word “Google” to the google.com web site, you could simply write:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[Google][]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;And then define the link:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[Google]: http://google.com/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;footnotes&#34;&gt;Footnotes&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;You can create footnotes like this[^footnote].
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;[^footnote]: Here is the &lt;span style=&#34;font-style:italic&#34;&gt;*text*&lt;/span&gt; of the &lt;span style=&#34;font-weight:bold&#34;&gt;**footnote**&lt;/span&gt;.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;will produce:
You can create footnotes like this&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;.
This is another footnote&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;Here is the &lt;em&gt;text&lt;/em&gt; of the &lt;strong&gt;footnote&lt;/strong&gt;.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;And then there was another one.&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>Power Query: My Custom Functions</title>
      <link>http://notes.mirzo.net/post/power-query-functions/</link>
      <pubDate>Wed, 29 Jun 2022 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/power-query-functions/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/power-query-functions/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/power-query-functions1_hue8c39bbf1cfc963d4791c52fc44d037d_1525502_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Power Query: My Custom Functions&#34; title=&#34;Power Query: My Custom Functions&#34;&gt;&lt;/a&gt;


&lt;p&gt;Let&amp;rsquo;s face it, Microsoft Excel is the most impactful data analysis tool in existence. And since 2013, its impact has grown dramatically with the introduction of Power Query, a data refinement and transformation tool that allows the users to create analysis and reporting pipelines from a variety of sources. And while Power Query&amp;rsquo;s built-in functionality is quite&amp;hellip; err&amp;hellip; powerful, advanced users will quickly find the need to develop custom solutions. In this note, I keep a constantly growing collection of Power Query custom functions that I use across most of my projects.&lt;/p&gt;
&lt;h2 id=&#34;m-formula-language&#34;&gt;M formula language&lt;/h2&gt;
&lt;p&gt;At the heart of Power Query lies a formula language called M. It&amp;rsquo;s a functional language specifically crafted for data transformation tasks. While Power Query provides a user-friendly interface for data manipulation, M empowers users with the ability to customize and extend these transformations beyond the built-in tools.&lt;/p&gt;
&lt;h3 id=&#34;importing-custom-m-functions&#34;&gt;Importing custom M functions&lt;/h3&gt;
&lt;p&gt;To import custom M functions into Power Query, you can follow these steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Save your custom functions as an M query file (.pq) or as a Power BI template (.pbit) if you&amp;rsquo;re working in Power BI.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In Excel, go to the Data tab and click on &amp;ldquo;Get Data&amp;rdquo; &amp;gt; &amp;ldquo;Get Data From File&amp;rdquo; &amp;gt; &amp;ldquo;From Workbook.&amp;rdquo; In Power BI, go to the &amp;ldquo;Home&amp;rdquo; tab and click on &amp;ldquo;Transform Data.&amp;rdquo;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Browse to the location where you saved your custom function file and select it. Click &amp;ldquo;Import&amp;rdquo; or &amp;ldquo;Load&amp;rdquo; to bring it into the Power Query Editor.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once imported, your custom function will appear as a query in the &lt;code&gt;Queries&lt;/code&gt; pane of the Power Query Editor. You can now then use it like any other function within Power Query.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;(Optional)&lt;/strong&gt; You can organize your custom functions by creating a separate folder or group within the Queries pane and moving your function query into it. This helps keep your workspace tidy and makes it easier to locate your functions when needed.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;custom-functions&#34;&gt;Custom Functions&lt;/h2&gt;
&lt;h3 id=&#34;combining-tables&#34;&gt;Combining Tables&lt;/h3&gt;
&lt;p&gt;The ability to combine data tables is a critical piece of Power Query functionality, but I find that the built-in process is quite sub-optimal. The biggest drawback lies in the fact that it forces you to explicitly define the names of the columns that you are combining, making your pipelines very inflexible and prone to breakage. The below function achieves the same task, but in a much more flexible way. It does not expect a list of columns to combine, which means that if your data source gains or loses a column, your pipeline will still work without any issues (at least related to combining tables).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Function &amp;ldquo;CombineTables&amp;rdquo;:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (TableColumn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; list&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; optional SourceNameColumn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; list) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  AddIDs &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    SourceNameColumn&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;then&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      TableColumn 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        ZipNames &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          List.Zip({TableColumn&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; SourceNameColumn})&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        AddColumnFunction &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          (ListIn &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; list) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          Table.AddColumn(ListIn{&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;}&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Source&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;each&lt;/span&gt; ListIn{&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;})&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        AddColumns &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;          List.Transform(ZipNames&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;each&lt;/span&gt; AddColumnFunction(_))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        AddColumns&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Combine &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Table.Combine(AddIDs)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  Combine
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; CombineTables(#&amp;#34;Previous Step&amp;#34;[#&amp;#34;Column with Tables&amp;#34;]&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; #&amp;#34;Previous Step&amp;#34;[#&amp;#34;Optional Column with Source name&amp;#34;])
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To use this function, first create a table containing (at least) two columns: a column containing the &lt;code&gt;Table&lt;/code&gt;s to combine, and an optional &amp;ldquo;Source&amp;rdquo; column that somehow describes the origin of each specific Table (e.g. the filename from which the table is derived). Such tables are generated by default in many Power Query built-in data import procedures, including &amp;ldquo;Import from Folder&amp;rdquo;. The function simply takes the references to these two columns, and outputs a row-wise concatenation of tables.&lt;/p&gt;
&lt;h3 id=&#34;remove-empty-columns&#34;&gt;Remove Empty Columns&lt;/h3&gt;
&lt;p&gt;Sometimes, when importing or combining a messy set of tables, you end up with a set of completely empty columns. While you can hunt them down and delete them manually, why not delegate it to a function? Just point it to a table from a past state, and poof, they are gone!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Function &amp;ldquo;RemoveEmptyCols&amp;rdquo;:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (InTable &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; table) &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; table &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        NonEmptyColumns &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; List.Select(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            Table.ColumnNames(InTable)&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            (colName) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; List.NonNullCount(Table.Column(InTable&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; colName)) &lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        )&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        CleanTable &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Table.SelectColumns(InTable&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; NonEmptyColumns)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        CleanTable
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; RemoveEmptyCols(#&amp;#34;Previous Step&amp;#34;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;get-tidy-with-trim&#34;&gt;Get tidy with Trim&lt;/h3&gt;
&lt;p&gt;When dealing with messy strings you often end up with leading and trailing white spaces, and you would expect that a powerful data pipeline tool like Power Query would have built-in functionality to deal with that&amp;hellip; but it doesn&amp;rsquo;t. Good thing there is a formula for that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Function &amp;ldquo;TrimAll&amp;rdquo;:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (text &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; text&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; optional char_to_trim &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; text) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    char &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; char_to_trim &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;null&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;then&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; char_to_trim&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    split &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; Text.Split(text&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; char)&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    removeblanks &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; List.Select(split&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;each&lt;/span&gt; _ &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    result&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;Text.Combine(removeblanks&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; char)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    result
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;TrimAll(#&amp;#34;Previous Step&amp;#34;[Column]&lt;span style=&#34;color:#f92672&#34;&gt;,&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;_&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The second argument is optional, and defines a custom target for trimming. If left unspecified, the function will default to trimming spaces.&lt;/p&gt;
&lt;h3 id=&#34;get-a-value-from-an-excel-named-range&#34;&gt;Get a value from an Excel named range&lt;/h3&gt;
&lt;p&gt;When using Power Query from Excel, sometimes you want to pull a specific value from within the spreadsheet into Power Query (e.g., a path to a file that you want to grab). While it is preferable to use Parameters to define such dynamic elements of your pipeline, it is often hard to ignore the familiar simplicity of changing a value in a spreadsheet cell. All you have to do is define such a cell as a named range (&lt;code&gt;rangeName&lt;/code&gt; in our example), and then use the following function.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Function &amp;ldquo;GetNamedValue&amp;rdquo;:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;(rangeName) &lt;span style=&#34;color:#f92672&#34;&gt;=&amp;gt;&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    Excel.CurrentWorkbook(){[Name&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;rangeName]}[Content]{&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;}[Column1]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-powerquery&#34; data-lang=&#34;powerquery&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;GetNamedValue(rangeName)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Custom M functions serve as invaluable assets in the toolkit of any Power Query user. By harnessing the flexibility and extensibility of M, these functions empower users to tailor their data transformation workflows to precise requirements, enhancing efficiency, scalability, and maintainability.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Artificial Neural Networks: Transformers</title>
      <link>http://notes.mirzo.net/post/ann-transformers/</link>
      <pubDate>Thu, 12 May 2022 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/ann-transformers/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/ann-transformers/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/ann-transformers2_hue11f5469e6ea3e6c2ecf513b760c3fde_1442406_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Artificial Neural Networks: Transformers&#34; title=&#34;Artificial Neural Networks: Transformers&#34;&gt;&lt;/a&gt;


&lt;p&gt;The Transformer architecture has revolutionized Artificial Neural Networks (ANN), demonstrating unparalleled performance in natural language processing, speech recognition, image processing, and other areas that require efficient handling of sequence data. This architecture is the main catalyst behind the current boom in generative AI, which made models like GPT and Stable Diffusion mainstream names. In this note we will take a deep look into the original implementation of the transformer model, and will then expand it to some of the modern variants.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Transformers&lt;/strong&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;h2 id=&#34;so-whats-the-big-deal&#34;&gt;So what&amp;rsquo;s the big deal?&lt;/h2&gt;
&lt;p&gt;The success of the Transformer model is largely driven by its innovative use of the &lt;strong&gt;attention&lt;/strong&gt; mechanism, which allows it to both ingest and generate sequence data (e.g., text, audio, images) with greater efficiency than any of the preceding models. Unlike &lt;a href=&#34;http://notes.mirzo.net/post/ann-architectures/#recurrent-neural-networks-rnns&#34;&gt;Recurrent Neural Networks (RNNs)&lt;/a&gt; that process sequence data serially, Transformers allow parallel processing and significantly improve both the training and inference times. The attention mechanism allows direct connections to be made between distant positions within long sequences, effectively capturing complex contextual relationships without running into unstable gradient problems. The scalability and flexibility of the Transformer architecture make it exceptionally well-suited for training on massive datasets and the creation of large &amp;ldquo;foundational&amp;rdquo; models.&lt;/p&gt;
&lt;h2 id=&#34;the-architecture&#34;&gt;The Architecture&lt;/h2&gt;
&lt;p&gt;In this note we will focus on the structure of the original Transformer presented in the seminal paper &lt;a href=&#34;https://arxiv.org/abs/1706.03762&#34;&gt;&amp;ldquo;Attention is All you Need&amp;rdquo;&lt;/a&gt;. This model was designed for text translation, a task where an input sequence may be transformed not only on the level of tokens, but also on the levels of sequence length and order. This structure is well suited as an introductory example because it includes all of the key building blocks of a Transformer, as some of the follow-up variants of the model (more on those later) essentially just removed some of the blocks in favor of others.&lt;/p&gt;
&lt;h3 id=&#34;structure-overview&#34;&gt;Structure Overview&lt;/h3&gt;
&lt;p&gt;In our example, the translation model follows the &lt;strong&gt;Encoder-Decoder&lt;/strong&gt; design. The encoder stack transforms an input sequence (in the original language) into a context-rich vector representation that captures its meaning and essence within a latent space. The decoder then uses this vector representation to generate a coherent and contextually appropriate variable-length output sequence in the translated language.&lt;/p&gt;
&lt;p&gt;The flow of the data through the model starts with a static &lt;strong&gt;&lt;a href=&#34;http://notes.mirzo.net/post/ann-embedding/&#34;&gt;embedding&lt;/a&gt;&lt;/strong&gt; layer, which converts tokens into vectors of a fixed size. As an important step, &lt;strong&gt;positional encoding&lt;/strong&gt; of each token is added to the embedding to allow the model to retain information on sequence order. These vectors are then processed by the &lt;strong&gt;encoder stack&lt;/strong&gt;, which consists of multiple identical layers, each containing a &lt;strong&gt;multi-head self-attention&lt;/strong&gt; sub-layer followed by a position-wise fully connected &lt;strong&gt;feed-forward network (FFN)&lt;/strong&gt; sub-layer. The attention mechanism allows the model to weigh the relationship of different tokens in the input sequence. Token representations that are found to bear a significant dependency to each other are combined in a way that merges their semantic information. For example, for the token pair &amp;ldquo;purple house&amp;rdquo;, the representation of &amp;ldquo;house&amp;rdquo; gets modified in a way that make it gain a quality of &amp;ldquo;purpleness&amp;rdquo;, while &amp;ldquo;purple&amp;rdquo; gains some quality of &amp;ldquo;housiness&amp;rdquo;. Once the data exits the encoder stack, it is passed into the &lt;strong&gt;decoder stack&lt;/strong&gt;, which has a very similar sub-layer structure, but includes an additional &lt;strong&gt;cross-attention&lt;/strong&gt; mechanism to focus on relevant parts of the encoder&amp;rsquo;s output. Finally, the decoder&amp;rsquo;s output is transformed into a final probability distribution for the next token in the sequence through a combination of a &lt;strong&gt;linear layer&lt;/strong&gt; and the &lt;strong&gt;softmax&lt;/strong&gt; function.&lt;/p&gt;

&lt;link rel=&#34;stylesheet&#34; href=&#34;http://notes.mirzo.net/css/hugo-easy-gallery.css&#34; /&gt;
&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;transformers-figure.png&#34; alt=&#34;Structure of a Transformer model.&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;https://arxiv.org/abs/1706.03762&#34; target=&#34;_blank&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;Structure of a Transformer model.&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Reproduced from the &lt;a href=&#34;https://arxiv.org/abs/1706.03762&#34;&gt;original article&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now that we have the lay of the land, let&amp;rsquo;s take a much deeper look at each of these elements and see how they work together.&lt;/p&gt;
&lt;h3 id=&#34;input-preprocessing&#34;&gt;Input Preprocessing&lt;/h3&gt;
&lt;p&gt;Training of an encoder-decoder model is performed using a parallel corpus, where the source sequences (e.g., sentences in the original language) are paired with their output sequences (e.g., sentence translations in the target language). A lot of work goes into the preparation of such a corpus, including removal of errors, proper alignment, and removal of poorly formed example pairs.&lt;/p&gt;
&lt;p&gt;Both the encoder and the decoder stacks have their own inputs. The encoder takes in the source sequences and the decoder the target sequences (or the previously generated tokens during inference). Before the sequence pairs enter each of the stacks, they undergo several preprocessing steps to convert raw text into a format suitable for ingestion. First, the raw text is tokenized into either words, subwords, or even individual characters. Common tokenization methods include &lt;a href=&#34;https://arxiv.org/abs/1508.07909v5&#34;&gt;Byte Pair Encoding (BPE)&lt;/a&gt; and &lt;a href=&#34;https://arxiv.org/abs/1609.08144v2&#34;&gt;WordPiece&lt;/a&gt;, which break down words into subword units to handle out-of-vocabulary terms and reduce the vocabulary size.&lt;/p&gt;
&lt;p&gt;Next, each token is mapped to a unique integer index within a predefined vocabulary matrix. This vocabulary is built from the full corpus and includes all possible tokens that the model might encounter. The vocabulary also incorporates special tokens, like &amp;ldquo;[START]&amp;rdquo;, &amp;ldquo;[END]&amp;rdquo; and &amp;ldquo;[PAD]&amp;rdquo; (used to pad sequences to a uniform length).&lt;/p&gt;
&lt;p&gt;The maximum length of the input sequence the model can ingest in a single pass is called the &lt;strong&gt;context window&lt;/strong&gt; (also referred to as the &lt;strong&gt;block size&lt;/strong&gt;, especially in the context of training). It is a fixed hyperparameter set during the model design, and it is influenced by several factors. The attention mechanism has a computational complexity that scales quadratically with the sequence length, \( O(n^2) \), meaning that longer input sequences require significantly more memory and computational power. Therefore, the context window must be balanced with the available hardware resources to ensure efficient processing. Additionally, the nature of the input data and the specific requirements of the task can also influence the choice of the context window size. For example, tasks that involve understanding long documents or multiple sentences might benefit from a larger context window, while shorter tasks like single sentence classification might work well with smaller block sizes. To adhere to the context window limitations, shorter input sequences are padded by &amp;ldquo;[PAD]&amp;rdquo; tokens, while excessively long sequences are truncated.&lt;/p&gt;
&lt;h3 id=&#34;static-embedding&#34;&gt;Static Embedding&lt;/h3&gt;
&lt;p&gt;Once our text data is preprocessed, the token sequences are fed into the model where they first encounter the &lt;strong&gt;embedding layer&lt;/strong&gt;. Here, each token in a sequence is converted into a dense distributed vector representation through an embedding matrix similar to the type produced by models like &lt;a href=&#34;http://notes.mirzo.net/post/ann-embedding/#word2vec&#34;&gt;word2vec&lt;/a&gt;. Since Transformers take context words as input and then output a target word, the training of this embedding matrix, in essence, works very much like the training of a CBOW model (except with a lot of bells and whistles between the embedding matrix and the output). Also like in word2vec, this initial embedding is static, meaning that it does not change token representations based on their context. It is the job of the rest of the Transformer model to imbue these simple embeddings with rich contextual information.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dimension Tracker&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;As we move through the Transformer structure, I find it useful to keep track of various dimensions of the model. It helps us to better understand how information gets compressed and decompressed by the network, or what sort of projections are involved at each step. The specific numbers are taken from the original Transformer article (the base model), and are for illustration purposes only. In modern practice these numbers can be vastly different.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;\(d_{m} = 512\), &amp;ldquo;model size&amp;rdquo;, or the size of the embedded token representation that gets progressively updated with contextual information throughout the model.&lt;/li&gt;
&lt;li&gt;\(d_{b} = 100\), &amp;ldquo;block size&amp;rdquo;, not explicitly defined in the original publication, so I&amp;rsquo;ve picked a number that is distinctively different from other dimensions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h3 id=&#34;positional-encoding&#34;&gt;Positional Encoding&lt;/h3&gt;
&lt;p&gt;Unlike RNNs, Transformers do not consume input sequences in order. Instead, multiple input blocks can be processed in parallel. While this parallelization has many benefits, it also leads to the loss of information about token order. One of the key insights that enabled the development of the Transformer architecture is the realization that token order can be encoded into their embedding representation. If an embedding vector can efficiently represent latent qualities of a word, such as its relationship to the concept of &amp;ldquo;purple&amp;rdquo;, it can also encode the latent qualities of &amp;ldquo;first&amp;rdquo;, &amp;ldquo;second&amp;rdquo;, and so on.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s imagine how we can design such a positional encoding approach. Our goal is to introduce a smooth representation of positions that can be directly added to the values in the embedding vector, creating features that would allow the model to infer the order and relative distances between tokens. The addition of the positional encoding information will result in the embedding vector to be shifted in the latent space in some ways. As a consequence, our positional encoding approach must satisfy two conflicting requirements: the magnitude of the positional encoding must be large enough for the model to uniquely resolve all possible positions (i.e., the shift should be noticeable), while also keeping the magnitude small enough to avoid overwhelming the semantic information in the embedding vector (i.e., the shift should not completely distort the original vector).&lt;/p&gt;
&lt;p&gt;One trivial (but ineffective) approach to positional encoding is to introduce a single factor that is proportional to the order of the token in the sequence. This method fails, because as the sequence length increases, the magnitude of the encoding would eventually become too high and overshadow the semantic information in the embedding.&lt;/p&gt;
&lt;p&gt;If a monotonic increasing factor strategy does not work, perhaps we could use some sort of a periodic function? If radio can encode your favorite song into the frequency of a periodic wave, we can certainly do the same for positional encodings! In fact, the original Transformer publication used such an approach, assigning each position in the input sequence a unique continuous encoding based on alternating sine and cosine functions of different frequencies. Using such trigonometric waves will limit the maximum shift in each dimension of our embedding vector within the (-1, 1) range.&lt;/p&gt;
&lt;p&gt;Mathematically, for a given token position, \( pos \), the encoding is defined as follows:&lt;/p&gt;
&lt;p&gt;$$
PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{\frac{2i}{d_m}}}\right) \\
PE_{(pos, 2i+1)} = \cos\left(\frac{pos}{10000^{\frac{2i}{d_m}}}\right)
$$&lt;/p&gt;
&lt;p&gt;Here, \( d_m \) is the previously defined model size (i.e., the number of dimensions in the embedding vector), and \( i \) is a counter that goes between 0 and \( \frac{d_m}{2}\) (as a result, \( d_m \) must be an even number). This algorithm allows us to create a positional encoding vector of the same size as the embedding representation. The counter, \( i \), allows us to loop over all of the dimensions of the encoding vector and assign values to two dimensions at a time, a sine encoding for the even indexes, and a cosine encoding for the odd indexes.&lt;/p&gt;
&lt;p&gt;To make this clearer, let&amp;rsquo;s express it in Python code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;positional_encoding&lt;/span&gt;(sequence_length, embedding_size):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    pos_matrix &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;zeros((sequence_length, embedding_size))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; token_position &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, sequence_length):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; counter &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;, int(embedding_size&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            pos_matrix[token_position, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;counter] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;sin(token_position&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10000&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;((&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;counter)&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;embedding_size)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            pos_matrix[token_position, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;counter&lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; np&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;cos(token_position&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;(&lt;span style=&#34;color:#ae81ff&#34;&gt;10000&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;**&lt;/span&gt;((&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;counter)&lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt;embedding_size)))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; pos_matrix
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The reason for using both sine and cosine waves is to make the encoding more feature rich by increasing its variability. However, when trying to picture the trends in the different positional encoding dimensions, I find it easier to focus on one wave at a time. So let us look only at the even (sine-based) indexes in the vector, ignoring the odd dimensions for now. As an example, let&amp;rsquo;s set our embedding vector size, \( d_m \), to a total of 20 dimensions (10 odd, 10 even), and plot the positional encoding values as a function of the vector&amp;rsquo;s index. We will show the encodings for a sequence of 50 tokens, each represented by traces of different colors.&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;sine_positional_encoding1.png&#34; alt=&#34;Even indexes (sine-based) of the Positional Encoding vector for 50 tokens&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;sine_positional_encoding1.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;Even indexes (sine-based) of the Positional Encoding vector for 50 tokens&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;There is a lot going on here, but we can certainly see some periodic trends. A good analogy to describe this periodicity in positional encodings is that of a clock. A clock uses different hands to represent different units of time: seconds, minutes, and hours. Each hand moves at its own frequency: the second hand moves the fastest, the hour hand moves the slowest. Despite these different speeds, the positions of the hands collectively provide a unique representation of the exact time. Except in our example the clock has 20 hands! In the figure above, the wave in the first (leftmost) dimension has the highest frequency (shortest period). As such, the values in this dimension assigned to successive tokens will change very quickly, and will allow for the highest degree of their resolution. However, as this dimension &amp;ldquo;loops around the clock face&amp;rdquo; the fastest, some of the more distant tokens in the sequence have a high chance of getting a similar or an identical encoding value. For example, we see that at index = 0, at least 3 different tokens got an encoding value very close to -1. As we move through the vector indexes (from left to right in the figure), the frequency of the sine wave decreases. By approximately index = 6, the encoding values for our 50 tokens change so slowly that they fail to complete even half of the wave&amp;rsquo;s period (the values stop dipping into the negative range). By the last dimension, the positional encoding value for successive tokens barely changes. This last dimension offers very little resolution between close-by token positions, but it guarantees to have a unique value for every token in the sequence.&lt;/p&gt;
&lt;p&gt;To complete the picture, let&amp;rsquo;s now add back our odd (cosine-based) indexes of the encoding vector. We can still see the same periodicity trends, except now each successive dimension is more distinct from its neighbors.&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;sine_cosine_positional_encoding2.png&#34; alt=&#34;Even (sine) and odd (cosine) indexes of the Positional Encoding vector for 50 tokens&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;sine_cosine_positional_encoding2.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;Even (sine) and odd (cosine) indexes of the Positional Encoding vector for 50 tokens&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The beauty of this system is that it can easily handle sequences of very different lengths. Let&amp;rsquo;s expand our example from 50 tokens to 500:&lt;/p&gt;


&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;sine_cosine_positional_encoding2_500.png&#34; alt=&#34;Positional Encoding vector for 500 tokens&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;sine_cosine_positional_encoding2_500.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;Positional Encoding vector for 500 tokens&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;We see that our slower-moving dimensions easily pick up the slack and ensure that each of our 500 tokens still gets a unique representation, while the faster moving dimensions ensure that nearby tokens are easily resolvable from each other. And to top it all off, the effect that the positional encoding can have on the original vector is contained to the -1 to 1 range within each dimension.&lt;/p&gt;
&lt;p&gt;Using pre-computed positional encodings, such as the one we described above, saves our model from introducing a lot of additional parameters. But this is not the only way to introduce positional information. Other approaches to positional encoding have also been developed, including systems where the information is treated as learnable parameters. Such systems allow the model to adaptively learn the best representation of positional information from the data, potentially improving performance on specific tasks.&lt;/p&gt;
&lt;h3 id=&#34;the-encoder-stack&#34;&gt;The Encoder Stack&lt;/h3&gt;
&lt;p&gt;Once our embedded input sequence gets encoded with positional information, it gets passed into the Encoder stack. The purpose of the encoder is to transform input text sequences into contextually enriched representations that are packed with features that can be used for subsequent processing. This structure is composed of repeating encoder layers, each consisting of two main sub-layers: a &lt;strong&gt;multi-head self-attention mechanism&lt;/strong&gt; and a position-wise fully connected &lt;strong&gt;FFN&lt;/strong&gt;. Each of these sub-layers is wrapped in a &lt;strong&gt;residual connection&lt;/strong&gt; followed by &lt;strong&gt;layer normalization&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&#34;attention-mechanism&#34;&gt;Attention Mechanism&lt;/h4&gt;
&lt;p&gt;The attention mechanism allows the model to weigh how much two tokens affect each other semantically. Specifically within the encoder stack, we use the self-attention to gauge token relationships within the same sequence. Before we start covering it in detail, let&amp;rsquo;s introduce a few more dimensions to track.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dimension Tracker&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;\(d_{m} = 512\), &amp;ldquo;model size&amp;rdquo;&lt;/li&gt;
&lt;li&gt;\(d_{b} = 100\), &amp;ldquo;block size&amp;rdquo;&lt;/li&gt;
&lt;li&gt;\(d_{k} = 64\), &amp;ldquo;key size&amp;rdquo;, generally applies to vectors in both Query (Q) and Key (K) matrices.&lt;/li&gt;
&lt;li&gt;\(d_{v} = 64\), &amp;ldquo;value size&amp;rdquo;, or the size of the Value (V) vectors. In the original Transformer model, it was set to the same value as \(d_{k}\), but it does not have to be.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the attention mechanism, the token embedding matrix (size \( d_b \times d_m \)) gets projected onto 3 separate matrices: Query \((Q)\), Key \( (K) \), and Value \( (V) \). \( Q \) and \( K \) matrices usually share the same size, \( d_b \times d_k \), while the \( V \) matrix has a size \( d_b \times d_v \). These vectors are generated by multiplying the sequence of token embeddings by learned weight matrices \( W^Q \), \( W^K \), \( W^V \). Both \( d_k \) and \( d_v \) are usually smaller than \( d_m \), meaning that the information contained in the token embeddings gets projected onto a smaller number of dimensions. Functionally, this compression allows for all three of these matrices to select a specific set of features from the token embeddings space. It is useful to imagine that each of these three representations are trained to communicate a specific &amp;ldquo;message&amp;rdquo; to other tokens. The representations in the \( Q \) matrix send a message that says &lt;em&gt;&amp;ldquo;I get modified by tokens that have these features&amp;rdquo;&lt;/em&gt;. Representations contained in the \( K \) matrix, on the other hand, send a message &lt;em&gt;&amp;ldquo;I modify tokens that have these features&amp;rdquo;&lt;/em&gt;. When a \( Q \) vector from one token is similar to a \( K \) vector from another token, i.e., when their &lt;a href=&#34;http://notes.mirzo.net/post/linear-algebra-basics/#the-inner-dot-product&#34;&gt;dot product&lt;/a&gt; is large, it means that their dependency on each other within a sequence is strong. For example, it is likely that adjectives and nouns that are next to each other in the sequence (e.g., &amp;ldquo;purple house&amp;rdquo;) will end up with similar Q and K representations, because usually such pairs are strongly dependent on each other.&lt;/p&gt;
&lt;p&gt;The \( V \) matrix, in contrast, contains the actual values that get aggregated based on the attention scores computed from the dot products of \( Q \) and \(K \). The &amp;ldquo;message&amp;rdquo; that \( V \) sends can be imagined as &lt;em&gt;&amp;ldquo;I modify tokens by introducing these features into them&amp;rdquo;&lt;/em&gt;. The values in \( V \) are essentially the information that is passed along to the next layer in the network.&lt;/p&gt;
&lt;p&gt;Mathematically, the full attention mechanism can be expressed as follows:&lt;/p&gt;
&lt;p&gt;$$
\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + \text{mask}\right)V
$$&lt;/p&gt;
&lt;p&gt;We see that our explanation hasn&amp;rsquo;t touched on a few elements, so let&amp;rsquo;s bring them in. After \( QK^T \) is calculated, we want to scale and normalize the product using the &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/#activation-functions&#34;&gt;softmax&lt;/a&gt; function. It was found that passing the raw dot product to softmax often leads to vanishing gradients, so additional scaling by \(\sqrt{d_k}\) is usually added to contain their magnitude.&lt;/p&gt;
&lt;p&gt;Prior to application of softmax we also apply an &lt;strong&gt;attention mask&lt;/strong&gt;, which is used to control which positions in the input sequence should be attended to or ignored. The masks apply large negative values (e.g., negative infinity) to the attention scores of irrelevant positions (and zeroes everywhere else), effectively zeroing out the influence of irrelevant token relationships after the softmax operation. In the encoder, the attention mask is only used to ignore padding tokens.&lt;/p&gt;
&lt;p&gt;Finally, the softmax&amp;rsquo;ed dot products multiply the \(V \) vectors to scale their relative effect on each other. At this stage, for each token in our sequence (let&amp;rsquo;s call it the &amp;ldquo;focus token&amp;rdquo; for brevity), we have \( d_b \) corresponding value vectors. Each of these vectors hold the features that will modify the focus token in a way that is proportional to the dot product of corresponding vectors from \( Q \) and \( K \). Importantly, the value vector of the focus token itself is contained here too. To bring the dimensionality of the attention&amp;rsquo;s layer output back into the sequence scale, we simply add up all of the value vectors for every token, yielding an updated semantic representation for each.&lt;/p&gt;
&lt;h4 id=&#34;going-multi-head&#34;&gt;Going Multi-Head&lt;/h4&gt;
&lt;p&gt;What we have described so far represents a single &amp;ldquo;head&amp;rdquo; of the attention mechanism. It was found that running multiple instances of attention in parallel significantly improves performance. In multi-head attention, each head contains its own set of \( Q \), \( K \), and \( V \) matrices, providing each token with multiple representation subspaces.&lt;/p&gt;
&lt;p&gt;For an equivalent number of parameters, a multi-head approach to attention is more efficient than a single-head approach due to the ability to compute the multiple heads in parallel. In addition, each head can focus on different parts of the input, allowing for richer and more redundant representation of token dependencies. The redundancy is especially important here, as each attention head essentially casts an independent vote on the perceived importance of the relationship between each token pair, making it an example of ensemble learning.&lt;/p&gt;
&lt;p&gt;At this point, you may have noticed that we have two problems on our hands. In our example, each encoder layer contains an attention sub-layer with multiple independent heads (eight, to be specific), each producing it&amp;rsquo;s own attention-scaled \( V \) matrix. Each of these matrices now contain an independent representation of each of our tokens. How do we combine these eight matrices into a single representation? You may have noticed the second problem if you were paying a close attention to our dimension tracker. We&amp;rsquo;ve described that attention compresses our embedding representation from the larger \( d_m \) size to the smaller \( d_k \) size. But wouldn&amp;rsquo;t we expect that the \( V \) results should be decompressed back to the full representation size, \( d_m \)? Otherwise, our embedding representations might just keep getting smaller with each subsequent encoder layer. But the dimension tracker says that, at least in our specific example, \( d_v \) has the same decreased size as \( d_k \).&lt;/p&gt;
&lt;p&gt;Both of these problems are solved in one swoop by simply concatenating the separate \( V \) matrices together. You will notice that \( d_v = d_m / h \), where &lt;em&gt;h&lt;/em&gt; is the number of heads in multi-headed attention. In our example, it means that concatenating eight vectors of size 64 brings us back to the full embedding size of 512. To smooth out the merging of the concatenated representations, we finish off the attention sub-layer with one last multiplication with an &amp;ldquo;output&amp;rdquo; weight matrix, \( W_O\), of size \( d_b \times d_b \):&lt;/p&gt;
&lt;p&gt;$$
\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h)W^O
$$&lt;/p&gt;
&lt;p&gt;where
$$
\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)
$$&lt;/p&gt;
&lt;p&gt;This converts the data back to the dimensions of the original embedding matrix, \( d_b \times d_m \), except now the embedding representation of each token carries contextual information from all of the other tokens in the sequence.&lt;/p&gt;
&lt;h4 id=&#34;feed-forward-network&#34;&gt;Feed-Forward Network&lt;/h4&gt;
&lt;p&gt;Following the attention sub-layer, the resulting embedding representations are passed into a fully-connected &lt;strong&gt;FFN&lt;/strong&gt; sub-layer. The structure of this FFN follows that of a simple &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/&#34;&gt;multilayer perceptron&lt;/a&gt;, and in our specific example it consists of two linear layers with a ReLU activation function applied between them. This FFN serves several critical functions that enhance the model&amp;rsquo;s ability to represent and process complex patterns in the input data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;FFN introduces non-linearity into the model. While the self-attention mechanism is powerful, it is comprised solely of linear operations, which can limit its ability to capture complex relationships. By incorporating non-linear transformations through activation functions such as ReLU, the FFN enables the model to learn and represent more intricate patterns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;FFN is responsible for dimensionality transformation. Typically, the inner layers of the FFN increase the dimensionality of the representations (size \( d_{ff} \)) temporarily before reducing them back to their original size. This process allows the model to project the input data into a higher-dimensional space where more complex relationships can be discerned and then project it back to a lower-dimensional space, thereby enhancing the model&amp;rsquo;s expressive power.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It aids in feature extraction and interaction. By applying different linear transformations and non-linear activations, the FFN helps in extracting more abstract and high-level features from the input representations. These features encapsulate interactions between different dimensions of the input vectors that might not be explicitly captured by the self-attention mechanism alone. The FFN operates independently on each position in the input sequence, ensuring that the applied transformation is consistent across all tokens. This &lt;strong&gt;position-wise&lt;/strong&gt; processing independence is crucial as it allows the same learned transformation to be applied uniformly, regardless of a token&amp;rsquo;s position in the sequence.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dimension Tracker&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;\(d_{m} = 512\), &amp;ldquo;model size&amp;rdquo;&lt;/li&gt;
&lt;li&gt;\(d_{b} = 100\), &amp;ldquo;block size&amp;rdquo;&lt;/li&gt;
&lt;li&gt;\(d_{k} = 64\), &amp;ldquo;key size&amp;rdquo;, generally applies to vectors in both Query (Q) and Key (K) matrices.&lt;/li&gt;
&lt;li&gt;\(d_{v} = 64\), &amp;ldquo;value size&amp;rdquo;, or the size of the Value (V) vectors.&lt;/li&gt;
&lt;li&gt;\(d_{ff} = 2048\), size of the inner layer of the FFN. Expands the data representation to capture more intricate relationships.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h4 id=&#34;residual-connections-and-layer-normalization&#34;&gt;Residual Connections and Layer Normalization&lt;/h4&gt;
&lt;p&gt;Transformer models can grow quite large, so we have to take special care to avoid &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/#exploding-and-vanishing-gradients&#34;&gt;gradient problems&lt;/a&gt;. For this purpose, both the multi-head attention and FFN sub-layers are wrapped in &lt;strong&gt;residual connections&lt;/strong&gt; and their outputs are subjected to &lt;strong&gt;layer normalization&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;When transitioning between the self-attention sub-layer and the FFN, the embedding representations can run into a problem. Within a given sequence, usually only a small proportion of tokens carry a significant relationship to each other, while most tokens are only loosely co-dependent. As a result, attention score matrices are generally sparse, i.e., the majority of attention scores are low. This sparseness causes an issue in backpropagation, where gradients associated with low scores cause a slow-down in the training of preceding nodes in the network. To avoid such vanishing gradients, residual connections (also known as skip connections) are added between the original input data and the layer outputs. Mathematically, the residual connection are very simple: all we do is add the original input values to the layer output. This ensures that the input values are never &amp;ldquo;forgotten&amp;rdquo; by a given layer, and that the model in general does not lose track of its original goals. It is also useful to wrap the FFN in a residual connection for the same reason.&lt;/p&gt;
&lt;p&gt;Mechanistically, the main effect of residual connections is the improvement of the gradient flow, which mitigates the vanishing gradient problem by allowing gradients to bypass certain layers and flow directly through the network. This ensures that gradients remain large enough for effective backpropagation, leading to faster and more stable training. Residual connections also help prevent degradation, a problem where adding more layers can lead to higher training errors. By ensuring each layer contributes incrementally without degrading overall performance, residual connections maintain the effectiveness of deep networks.&lt;/p&gt;
&lt;p&gt;As a side effect of repetitively adding input values to the outputs, we can run into the opposite problem: excessive growth of the gradient. To balance this effect out (and to generally stabilize the gradient), layer outputs (including the residuals) are subjected to &lt;strong&gt;layer normalization&lt;/strong&gt;. It works by normalizing the activations of a layer across the features for each training example. This normalization is performed by adjusting and scaling the outputs to have a mean of zero and a variance of one, followed by a learned linear transformation to restore the model&amp;rsquo;s capacity to represent complex patterns.&lt;/p&gt;
&lt;p&gt;For an input vector \( x \), layer normalization can be described as:&lt;/p&gt;
&lt;p&gt;$$
\hat{x}_i = \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}}
$$&lt;/p&gt;
&lt;p&gt;Here, \( \mu \) is the mean of the input features, \( \sigma^2 \) is the variance, and \( \epsilon \) is a small constant added for numerical stability to prevent division by zero. After normalizing, the layer normalization scales and shifts the normalized values using learned parameters \( \gamma \) (scale) and \( \beta \) (shift), giving the final output:&lt;/p&gt;
&lt;p&gt;$$
y_i = \gamma \hat{x}_i + \beta
$$&lt;/p&gt;
&lt;p&gt;Layer normalization ensures that the output distribution of each layer remains stable, making the training process more robust. It also improves gradient flow through the network by maintaining consistent input distributions, helping to keep gradients within a manageable range. Additionally, layer normalization contributes to model regularization, reducing overfitting by imposing a constraint on the outputs of each layer. This constraint encourages the model to learn more generalized patterns rather than memorizing the training data. It also enhances the model&amp;rsquo;s ability to handle varying batch sizes, as the normalization is applied independently of the batch dimension. This flexibility is particularly beneficial for tasks with fluctuating batch sizes or where real-time processing is required.&lt;/p&gt;
&lt;p&gt;It should be noted, that many ANN architectures use batch normalization instead of layer normalization. In the batch approach, normalization occurs across the input data batch dimension rather than the model&amp;rsquo;s feature dimension. Transformers use layer normalization because the batch size in this model tends to vary a lot more and is generally a less relevant hyperparameter.&lt;/p&gt;
&lt;h3 id=&#34;the-decoder-stack&#34;&gt;The Decoder Stack&lt;/h3&gt;
&lt;p&gt;While the purpose of the encoder stack is to transform input text sequences into contextually enriched embedding representations, the decoder stack is designed to generate text sequences from such continuous representations. Structurally, the decoder stack is very similar to the encoder, sharing many of the sub-layers, but it introduces additional mechanisms to effectively manage the generation process.&lt;/p&gt;
&lt;p&gt;The decoder has a separate data input mechanism, which is structured in the same way as the input of the encoder. It includes the static embedding layer, with its own embedding matrix that is based on the output vocabulary (e.g., in the translated language). The decoder&amp;rsquo;s static embeddings also undergo positional encoding using the same method as in the encoder input. Following the data input mechanism, each layer in the decoder stack consists of three main sub-layers: a masked multi-head self-attention mechanism, a multi-head &lt;strong&gt;cross-attention&lt;/strong&gt; mechanism, and a position-wise FFN.&lt;/p&gt;
&lt;p&gt;The masked multi-head self-attention mechanism operates similarly to the self-attention mechanism in the encoder stack but includes a crucial distinction: &lt;strong&gt;the look-ahead mask&lt;/strong&gt;. This triangular mask ensures that the model can only attend to earlier positions in the sequence and not to future positions.&lt;/p&gt;
&lt;p&gt;The second sub-layer in each decoder is the multi-head cross-attention mechanism, which allows the decoder to focus on appropriate positions in the input sequence by creating attention vectors between the decoder&amp;rsquo;s current state and the encoder&amp;rsquo;s output. In cross-attention, the \( Q \) matrix comes from the decoder, while the \( K \) and \( V \) matrices come from the final encoder layer.&lt;/p&gt;
&lt;p&gt;The rest of the decoder layer structure is identical to the encoder. The attention sub-layers are followed by a position-wise FFN, and each of the sub-layers is wrapped in residual connections with layer normalization.&lt;/p&gt;
&lt;h4 id=&#34;autoregression&#34;&gt;Autoregression&lt;/h4&gt;
&lt;p&gt;The Transformer is an autoregressive architecture, meaning that at each inference pass it predicts one new token  based on the available context. At each pass (also known as a &lt;strong&gt;time step&lt;/strong&gt;), the prediction of the next token in the sequence is conditional both on the encoder input sequence and on all of the previously decoder-predicted output tokens. In the translation task example, the prediction of each output token depends on the knowledge of the full input sequence in the original language (to maintain the meaning of the sentence), and the previously generated translated tokens (to maintain proper gramatical structure of the sentence in the target language). The input sequence is ingested by the encoder all at once, hence there is no need for autoregression in this portion of the model. The autoregression behaviour comes solely from the decoder.&lt;/p&gt;
&lt;p&gt;Training an encoder-decoder architecture involves the optimization of both stacks simultaneously. In this process, the input sequence is fed into the encoder unmasked, while the corresponding target sequence is fed into the decoder with a look-ahead mask, making sure that the decoder&amp;rsquo;s self-attention block cannot rely on the contextual information from future tokens. Prior to the input into the decoder, the target sequence is shifted to the right by one position by introducing a [START] special token in the first position. An [END] token is also added to the end of the sequence, to teach the model when to stop generation. This right-shifted &amp;ldquo;context&amp;rdquo; sequence is used as the input to the decoder during training, while the original (unshifted) sequence serves as the expected output for evaluation of the cost function. This shift between the decoder&amp;rsquo;s context and the expected output during training teaches the decoder to predict new tokens. For example, on the first inference pass, the decoder only has the [START] token as the context. The model is trained to predict the first word of the translated sequence using the full encoder context and just the [START] token. On the second pass, the decoder output context is expanded to include both the [START] token and the first token predicted by the decoder, and so on for future passes. This goes on until the model decides that an [END] token is the most likely prediction.&lt;/p&gt;
&lt;h3 id=&#34;linear-layer-and-softmax&#34;&gt;Linear Layer and Softmax&lt;/h3&gt;
&lt;p&gt;We are almost ready for the model to make the final prediction. Exiting the decoder stack, the data passes though a &lt;strong&gt;linear layer&lt;/strong&gt; and the &lt;strong&gt;softmax&lt;/strong&gt; function. These components are responsible for transforming the decoder’s outputs into a probability distribution over the vocabulary.&lt;/p&gt;
&lt;p&gt;The linear layer simply multiplies the decoder output by an &lt;strong&gt;unembedding weight matrix&lt;/strong&gt; and adds a bias vector. Since the size of the representation matrix is \( d_b \times d_{m} \), multiplying it by a weight matrix of size  \( d_{m} \times V \) (with \(V\) being the size of the vocabulary), gets us to a vocabulary logits matrix of size \( d_b \times V \). This transformation projects the decoder’s output into a space where each dimension corresponds to a word in the vocabulary. Applying the softmax function then converts the logits into a probability distribution over the vocabulary. While the logits matrix has \( d_b \) number of vectors in the &amp;ldquo;sequence length&amp;rdquo; dimension, all of them do not matter except for the vector in the last non-padded position. This position corresponds to the probability distribution of the newly predicted token. As a result, most implementations unembed only this last vector, and not the whole representation matrix.&lt;/p&gt;
&lt;h3 id=&#34;output-sampling&#34;&gt;Output sampling&lt;/h3&gt;
&lt;p&gt;Once softmax produces a probability distribution over the vocabulary, a single token must be sampled from this distribution. Depending on the intended behaviour of the model, a few sampling methods can be used:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Argmax&lt;/strong&gt;: The simplest method is to select the token with the highest probability. This is known as greedy decoding. It always picks the most likely token at each step.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Random Sampling&lt;/strong&gt;: Instead of always picking the highest probability token, you can sample from the probability distribution. This introduces randomness and can generate more diverse outputs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-k Sampling&lt;/strong&gt;: This method limits the sampling to the top k tokens in the probability distribution, reducing the chance of selecting unlikely tokens.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-p (Nucleus) Sampling&lt;/strong&gt;: Instead of limiting the number of tokens, this method considers the smallest set of tokens whose cumulative probability exceeds a threshold p, ensuring that the selected tokens have a combined probability mass of at least p.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Beam Search&lt;/strong&gt;: This is a more complex method that maintains multiple candidate sequences (beams) and explores them simultaneously to find the most likely sequence.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The choice of the sampling method can significantly affect the model&amp;rsquo;s output. Greedy decoding makes each output more predictable and reproducible, but may make generative models sound repetitive. When output diversity is important, methods like top-k and top-p sampling introduce the needed variability.&lt;/p&gt;
&lt;h2 id=&#34;modern-transformer-variants&#34;&gt;Modern Transformer Variants&lt;/h2&gt;
&lt;p&gt;Since the introduction of the original Transformer model, numerous improvements and adaptations have been developed to enhance performance, scalability, and efficiency across various applications. Here, we discuss some of the most notable modern designs that build upon the foundation of the Transformer architecture.&lt;/p&gt;
&lt;h3 id=&#34;bert&#34;&gt;BERT&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://arxiv.org/abs/1810.04805&#34;&gt;BERT&lt;/a&gt; (Bidirectional Encoder Representations from Transformers), developed by Google, represents a significant evolution in transformer-based models. Unlike the original Transformer which focuses on sequence-to-sequence tasks like translation, BERT is designed to generate deep bidirectional representations by conditioning on both left and right contexts in all layers. This makes BERT particularly effective for tasks that require understanding the context of a word within a sentence.&lt;/p&gt;
&lt;p&gt;BERT&amp;rsquo;s architecture uses only the encoder stack of the original Transformer model. It introduces a novel pre-training objective: the Masked Language Model (MLM), where some tokens in the input are masked at random, and the model is trained to predict these masked tokens. Additionally, BERT uses the Next Sentence Prediction (NSP) task, where the model is trained to predict if two given sentences are sequential in the original text, enhancing its ability to understand the relationships between sentences.&lt;/p&gt;
&lt;h3 id=&#34;gpt&#34;&gt;GPT&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://openai.com/index/introducing-gpts/&#34;&gt;GPT&lt;/a&gt; (Generative Pre-trained Transformer), developed by OpenAI, is another groundbreaking model that diverges from the encoder-decoder structure by using only the decoder part of the Transformer architecture. GPT is an autoregressive model, meaning it generates text one token at a time, conditioning each token on the previous ones. This approach allows GPT to excel in tasks that involve text generation, such as conversational agents and creative writing.&lt;/p&gt;
&lt;p&gt;GPT-3, a notable version of this model, has 175 billion parameters, making it one of the largest language models to date. The model&amp;rsquo;s massive size and extensive pre-training on diverse internet text enable it to perform a wide range of natural language tasks with impressive proficiency, even without task-specific fine-tuning.&lt;/p&gt;
&lt;h3 id=&#34;t5&#34;&gt;T5&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://arxiv.org/abs/1910.10683v4&#34;&gt;T5&lt;/a&gt; (Text-To-Text Transfer Transformer), also developed by Google, proposes a unified approach to handling different NLP tasks by framing them all as text-to-text problems. This means that both the input and output are treated as text strings, allowing the same model, objective, training procedure, and decoding process to be used across various tasks.&lt;/p&gt;
&lt;p&gt;T5 uses a modified version of the Transformer architecture and introduces several improvements, such as the use of relative positional encodings and the exploration of different architectural modifications. The model is pre-trained on a diverse range of text-to-text tasks, enabling it to generalize well across different NLP applications.&lt;/p&gt;
&lt;h3 id=&#34;efficient-transformers&#34;&gt;Efficient Transformers&lt;/h3&gt;
&lt;p&gt;As Transformer models grow larger, their computational requirements increase significantly. Efficient Transformers aim to address these scalability issues by reducing the computational and memory costs associated with the attention mechanism. Some of the key innovations in this area include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sparse Attention&lt;/strong&gt;: Techniques like Longformer and BigBird introduce sparse attention mechanisms that limit the number of tokens each token attends to, reducing the quadratic complexity of the original attention mechanism to linear or near-linear complexity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linformer&lt;/strong&gt;: This model approximates the full attention matrix with a low-rank matrix, significantly reducing the memory footprint and computational cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performer&lt;/strong&gt;: The Performer model uses kernel-based methods to approximate the softmax attention mechanism, enabling linear time complexity and making it feasible to handle much longer sequences.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;vision-transformers&#34;&gt;Vision Transformers&lt;/h3&gt;
&lt;p&gt;Transformers have also made significant strides in the field of computer vision. Vision Transformers (ViT) apply the Transformer architecture to image classification tasks by treating image patches as sequence tokens. This approach has shown competitive performance compared to traditional convolutional neural networks (CNNs) on various image recognition benchmarks.&lt;/p&gt;
&lt;p&gt;ViTs divide an image into fixed-size patches, embed each patch into a vector, and then process the sequence of patch embeddings using a standard Transformer encoder. This method allows ViTs to capture long-range dependencies and global context in images, which are challenging for CNNs.&lt;/p&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;The evolution of Transformer models continues to push the boundaries of what is possible in machine learning and AI. From their initial application in natural language processing to their expansion into vision and other domains, Transformers have proven to be a versatile and powerful architecture. Modern variants like BERT, GPT, T5, and various efficient Transformer designs have further enhanced their capabilities, making them indispensable tools in the AI researcher&amp;rsquo;s toolkit.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Transformers&lt;/strong&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>Docker: The Basics</title>
      <link>http://notes.mirzo.net/post/docker-basics/</link>
      <pubDate>Sun, 15 Aug 2021 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/docker-basics/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/docker-basics/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/docker-basics1_hu120266655abd5e148e3baccc5ac8a47b_1183139_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Docker: The Basics&#34; title=&#34;Docker: The Basics&#34;&gt;&lt;/a&gt;


&lt;p&gt;Docker is just one of those technologies that make you fall in love 💕 with it, and make you wonder how you&amp;rsquo;ve manged to live your life without it for so long. It has revolutionized the way we develop, deploy, and run applications. On the surface, Docker is just a tool that allows us to create reproducible environments. But this simple concept is executed so well that it manages to completely change how you approach app development and service deployment as a whole. This note covers the fundamental concepts behind this wonderful tool and guides you through its basic usage.&lt;/p&gt;
&lt;h2 id=&#34;what-is-docker&#34;&gt;What is Docker?&lt;/h2&gt;
&lt;p&gt;Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings.&lt;/p&gt;
&lt;h3 id=&#34;docker-vs-virtual-machines&#34;&gt;Docker vs. Virtual Machines&lt;/h3&gt;
&lt;p&gt;While both Docker and virtual machines (VMs) provide isolation and virtualization, they differ in several key aspects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Resource Usage&lt;/strong&gt;: Docker containers share the host system&amp;rsquo;s kernel and are more lightweight than VMs, which require a full OS for each instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Startup Time&lt;/strong&gt;: Containers can start almost instantly, while VMs typically take longer to boot up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: Docker allows for better utilization of system resources, as containers consume only what they need.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portability&lt;/strong&gt;: Docker containers are highly portable and can run on any system that supports Docker, regardless of the underlying OS.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;key-terms&#34;&gt;Key Terms&lt;/h2&gt;
&lt;h3 id=&#34;1-dockerfile&#34;&gt;1. Dockerfile&lt;/h3&gt;
&lt;p&gt;A Dockerfile is a text document containing a series of instructions used to build a Docker image. It serves as a blueprint for creating a reproducible environment for your application.&lt;/p&gt;
&lt;h3 id=&#34;2-image&#34;&gt;2. Image&lt;/h3&gt;
&lt;p&gt;An image is a read-only template used to create Docker containers. It contains all the code, dependencies, and configurations needed to run an application. Images can be as basic as just an operating system (e.g., Ubuntu, Alpine) or as complex as a full application stack (e.g., WordPress with Apache and MySQL).&lt;/p&gt;
&lt;h3 id=&#34;3-container&#34;&gt;3. Container&lt;/h3&gt;
&lt;p&gt;A container is a runnable instance of an image. It represents a single running process that has been spun up from an image. Containers are isolated from each other and the host system, providing a consistent environment for applications to run.&lt;/p&gt;
&lt;h3 id=&#34;4-image-repository-docker-hub&#34;&gt;4. Image repository (Docker Hub)&lt;/h3&gt;
&lt;p&gt;Base images are  retrieved (by default) from &lt;a href=&#34;https://hub.docker.com/&#34;&gt;docker hub&lt;/a&gt;, one of cloud-based registry services for storing and sharing Docker images. Docker Hub is provided by Docker organizations itself, and offers a vast collection of pre-built images that developers can use as a starting point for their applications. Some &amp;ldquo;official&amp;rdquo; images are built by developers of specific tools (e.g., nginx, node, mysql), carrying their seal of approval. Docker Hub also allows users to push their custom images to public or private repositories, making it easy to share and distribute containerized applications across teams or with the wider community.&lt;/p&gt;
&lt;h2 id=&#34;key-concepts&#34;&gt;Key Concepts&lt;/h2&gt;
&lt;h3 id=&#34;ephemeral-containers&#34;&gt;Ephemeral Containers&lt;/h3&gt;
&lt;p&gt;Docker containers are ephemeral, meaning that they are designed to be temporary and disposable. This concept is fundamental to understanding how to effectively use Docker in your development and deployment workflows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Temporary by Design&lt;/strong&gt;: Containers are intended to be stopped, destroyed, and replaced with minimal setup or configuration.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stateless Applications&lt;/strong&gt;: The ephemeral nature of containers encourages the development of stateless applications, where the application doesn&amp;rsquo;t rely on the container&amp;rsquo;s state to function correctly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Persistence&lt;/strong&gt;: Any data or changes made inside a container are typically lost when the container is removed. To persist data, you need to use Docker volumes or bind mounts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Immutable Infrastructure&lt;/strong&gt;: Ephemerality supports the concept of immutable infrastructure, where containers are never modified in place but rather replaced entirely with new versions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scalability and Resilience&lt;/strong&gt;: The ability to quickly spin up and tear down containers facilitates easy scaling and improves application resilience.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Clean Environment&lt;/strong&gt;: Each time a container starts, it provides a clean, consistent environment, reducing &amp;ldquo;it works on my machine&amp;rdquo; type issues.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;the-single-process-model&#34;&gt;The Single-Process Model&lt;/h3&gt;
&lt;p&gt;In Docker, each container is designed to run a single primary process. This process is typically defined by the &lt;code&gt;CMD&lt;/code&gt; or &lt;code&gt;ENTRYPOINT&lt;/code&gt; instruction in the Dockerfile. When this primary process exits, the container stops running. This design philosophy aligns with the Unix principle of &amp;ldquo;do one thing and do it well,&amp;rdquo; promoting simplicity and modularity in application architecture.&lt;/p&gt;
&lt;p&gt;When designing applications that require multiple processess (e.g., the backend API and the database), they should be broken up into multiple containers. Such single-process containers are easier to manage, monitor, and debug. Taking this approach makes it easier to scale individual components of your application, and have better control over resource allocation and usage. The improved isolation between different parts of your application is also good from the security standpoint.&lt;/p&gt;
&lt;p&gt;Key points about the single process model:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Container Lifecycle&lt;/strong&gt;: The lifecycle of a container is tied directly to its main process. When the process exits, the container stops. A common mistake is starting a container without defining a running process. This causes the container to exit immediately after startup.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Process ID 1&lt;/strong&gt;: The main process in a container runs with Process ID (PID) 1, similar to the init process in a Linux system.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Signal Handling&lt;/strong&gt;: The primary process is responsible for handling system signals, such as SIGTERM for graceful shutdowns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Logging&lt;/strong&gt;: Output (stdout and stderr) from the main process is captured by Docker&amp;rsquo;s logging system.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;getting-started-with-docker&#34;&gt;Getting Started with Docker&lt;/h2&gt;
&lt;h3 id=&#34;installing-docker&#34;&gt;Installing Docker&lt;/h3&gt;
&lt;p&gt;Before we begin, make sure you have Docker installed on your system. You can download and install Docker from the official &lt;a href=&#34;https://www.docker.com/get-started&#34;&gt;website&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&#34;basic-docker-commands&#34;&gt;Basic Docker Commands&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pull an image&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker pull &amp;lt;image_name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Example: &lt;code&gt;docker pull nginx:1.23&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Run a container&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run [options] &amp;lt;image_name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Common options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--name&lt;/code&gt;: Assign a custom name to the container&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-p&lt;/code&gt;: Bind a port (host_port:container_port)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-d&lt;/code&gt;: Run in detached mode (background)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-it&lt;/code&gt;: Run interactively with a terminal&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--rm&lt;/code&gt;: Remove the container when it exits&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example: &lt;code&gt;docker run -d -p 8080:80 --name my-nginx nginx:1.23&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;List running containers&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Use &lt;code&gt;-a&lt;/code&gt; flag to show all containers (including stopped ones)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start/Stop containers&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker start &amp;lt;container_name_or_id&amp;gt;
docker stop &amp;lt;container_name_or_id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Remove a container&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker rm &amp;lt;container_name_or_id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;List images&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Remove an image&lt;/strong&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker rmi &amp;lt;image_name&amp;gt;:&amp;lt;tag&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;creating-docker-images&#34;&gt;Creating Docker Images&lt;/h2&gt;
&lt;p&gt;To create your own Docker image, you need to write a Dockerfile. Here&amp;rsquo;s a basic example for a Node.js application:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-dockerfile&#34; data-lang=&#34;dockerfile&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;FROM&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; node:19-alpine&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;WORKDIR&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; /app/&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; package.json .&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUN&lt;/span&gt; npm install&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; src ./src&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;CMD&lt;/span&gt; [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;node&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;src/server.js&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;rsquo;s break down this Dockerfile:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;FROM node:19-alpine&lt;/code&gt;: Specifies the base image to use (Node.js 19 on Alpine Linux). You can usually specify the &lt;code&gt;:latest&lt;/code&gt; tag, to retrieve the latest version of a given package, but it is more advisable to go with specific versions. After all, we are trying to achieve reproducibility here!&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WORKDIR /app/&lt;/code&gt;: Sets the working directory inside the container. The end slash in &lt;code&gt;/app/&lt;/code&gt; tells Docker to create the folder if does not exist&lt;/li&gt;
&lt;li&gt;&lt;code&gt;COPY package.json .&lt;/code&gt;: Copies the package.json file to the working directory&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RUN npm install&lt;/code&gt;: Installs the Node.js dependencies&lt;/li&gt;
&lt;li&gt;&lt;code&gt;COPY src ./src&lt;/code&gt;: Copies the application source code&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CMD [&amp;quot;node&amp;quot;, &amp;quot;src/server.js&amp;quot;]&lt;/code&gt;: Specifies the command to run when the container starts&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To build an image from this Dockerfile:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker build -t my-node-app:1.0 .
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;-t&lt;/code&gt; flag allows you to tag the image with a name and version.&lt;/p&gt;
&lt;h2 id=&#34;running-containers&#34;&gt;Running Containers&lt;/h2&gt;
&lt;p&gt;Once you have an image, you can run it as a container:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run -d -p 3000:3000 --name my-node-app my-node-app:1.0
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This command runs the container in detached mode (&lt;code&gt;-d&lt;/code&gt;), maps port 3000 on the host to port 3000 in the container (&lt;code&gt;-p 3000:3000&lt;/code&gt;), and names the container &amp;ldquo;my-node-app&amp;rdquo;.&lt;/p&gt;
&lt;h3 id=&#34;connecting-to-running-containers&#34;&gt;Connecting to Running Containers&lt;/h3&gt;
&lt;p&gt;Sometimes you need to interact directly with a running container, whether for debugging, maintenance, or running commands. Docker provides two main ways to connect to containers interactively:&lt;/p&gt;
&lt;p&gt;To start a new container and immediately connect to it, use the following command:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run -it [yourImage] [shell]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run -it ubuntu:20.04 bash
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This command does the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-i&lt;/code&gt;: Keeps STDIN open even if not attached, allowing you to interact with the container&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-t&lt;/code&gt;: Allocates a pseudo-TTY, giving you a terminal-like experience&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[yourImage]&lt;/code&gt;: Specifies the image to use (in this case, Ubuntu 20.04)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[shell]&lt;/code&gt;: Specifies the shell to use (in this case, bash)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This approach is useful when you want to start a new container specifically for interactive use. To connect to a container that&amp;rsquo;s already running, use the &lt;code&gt;docker exec&lt;/code&gt; command:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker exec -it [yourContainer] [shell]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker exec -it my-running-container bash
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This method is particularly useful for debugging or inspecting containers that are already running as part of your application. However, be cautious when making changes inside a running container. Remember that these changes are typically not persisted unless you&amp;rsquo;re using volumes or commit the changes to a new image.&lt;/p&gt;
&lt;h2 id=&#34;multi-stage-builds&#34;&gt;Multi-Stage Builds&lt;/h2&gt;
&lt;p&gt;Staged building allows you to create more efficient and smaller images. This technique is particularly useful for applications that require a build process, as it enables you to use one image for building your application and another for running it, without carrying over unnecessary build tools and dependencies to your final image.&lt;/p&gt;
&lt;p&gt;In a multi-stage build, you use multiple &lt;code&gt;FROM&lt;/code&gt; statements in your Dockerfile. Each &lt;code&gt;FROM&lt;/code&gt; instruction can use a different base image, and each one begins a new stage of the build. You can selectively copy artifacts from one stage to another, using &lt;code&gt;COPY --from=&amp;lt;stage_name_or_number&amp;gt;&lt;/code&gt;, leaving behind everything you don&amp;rsquo;t need in the final image.&lt;/p&gt;
&lt;p&gt;The general syntax involves adding multiple &lt;code&gt;FROM&lt;/code&gt; statements within your Dockerfile. The last &lt;code&gt;FROM&lt;/code&gt; statement defines the final base image for your resulting Docker image.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a basic structure of a Dockerfile using multi-stage builds:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-dockerfile&#34; data-lang=&#34;dockerfile&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Build stage&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;FROM&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; node:14 AS builder&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;WORKDIR&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; /app&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; package*.json ./&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUN&lt;/span&gt; npm install&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; . .&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUN&lt;/span&gt; npm run build&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Production stage&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;FROM&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; node:14-alpine&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;WORKDIR&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt; /app&lt;/span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; --from&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;builder /app/dist ./dist&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;COPY&lt;/span&gt; package*.json ./&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;RUN&lt;/span&gt; npm install --only&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;production&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;CMD&lt;/span&gt; [&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;npm&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;start&amp;#34;&lt;/span&gt;]&lt;span style=&#34;color:#960050;background-color:#1e0010&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The first stage uses a full Node.js image to build the application.&lt;/li&gt;
&lt;li&gt;The second stage uses a lighter Alpine-based Node.js image for the production environment.&lt;/li&gt;
&lt;li&gt;Only the built artifacts and production dependencies are copied to the final image.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Concepts to keep in mind:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Multiple FROM Statements&lt;/strong&gt;: Each &lt;code&gt;FROM&lt;/code&gt; instruction starts a new build stage.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Naming Stages&lt;/strong&gt;: You can name your stages using &lt;code&gt;AS &amp;lt;name&amp;gt;&lt;/code&gt;. This makes it easier to reference them later.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Copying Between Stages&lt;/strong&gt;: Use &lt;code&gt;COPY --from=&amp;lt;stage_name_or_number&amp;gt;&lt;/code&gt; to copy artifacts from one stage to another.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Final Image&lt;/strong&gt;: The last &lt;code&gt;FROM&lt;/code&gt; statement defines the base image for your final Docker image.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;volumes&#34;&gt;Volumes&lt;/h2&gt;
&lt;p&gt;Volumes are used to persist data generated by and used by Docker containers. They are especially useful for databases and stateful applications.&lt;/p&gt;
&lt;p&gt;To create and use a volume:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a volume:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker volume create my-volume
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run a container with the volume:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run -d -v my-volume:/data my-image
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;container-networking&#34;&gt;Container Networking&lt;/h2&gt;
&lt;p&gt;Docker provides various networking options to allow containers to communicate with each other and the outside world:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Bridge Network&lt;/strong&gt;: The default network for containers. Containers on the same bridge network can communicate with each other.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Host Network&lt;/strong&gt;: Removes network isolation between the container and the host system.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Overlay Network&lt;/strong&gt;: Enables communication between containers across multiple Docker hosts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Macvlan Network&lt;/strong&gt;: Assigns a MAC address to a container, making it appear as a physical device on the network.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To create a custom network:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker network create my-network
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To run a container on a specific network:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;docker run --network my-network my-image
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;docker-secrets&#34;&gt;Docker Secrets&lt;/h2&gt;
&lt;p&gt;Docker Secrets provide a secure way to manage sensitive data such as passwords, API keys, and certificates. They are encrypted at rest and during transit in a Docker swarm.&lt;/p&gt;
&lt;p&gt;To create a secret:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;echo &amp;#34;mypassword&amp;#34; | docker secret create my_secret -
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Secrets cannot be used in single containers. Instead they can be created through Docker Swarm, or Docker Compose (see the next section). When included in a container, secrets are mounted in &lt;code&gt;/run/secrets/&amp;lt;secret name&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;docker-compose&#34;&gt;Docker Compose&lt;/h2&gt;
&lt;p&gt;Docker Compose is a powerful tool for defining and running multi-container Docker applications. It allows you to use a YAML file to configure your application&amp;rsquo;s services, networks, and volumes, and then create and start all the services from your configuration with a single command.&lt;/p&gt;
&lt;h3 id=&#34;key-concepts-1&#34;&gt;Key Concepts&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Services&lt;/strong&gt;: Containers that make up your application.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Networks&lt;/strong&gt;: How your containers communicate with each other.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Volumes&lt;/strong&gt;: Persistent data storage for your containers.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;installation&#34;&gt;Installation&lt;/h3&gt;
&lt;p&gt;Docker Compose is included with Docker Desktop for Windows and Mac. For Linux, you may need to install it separately. Check the official Docker documentation for the most up-to-date installation instructions.&lt;/p&gt;
&lt;h3 id=&#34;the-composeyml-file&#34;&gt;The &amp;ldquo;compose.yml&amp;rdquo; file&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;compose.yml&lt;/code&gt; file is the heart of Docker Compose. It defines the services, networks, and volumes for your application. Here&amp;rsquo;s a basic structure:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;version&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;3&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;web&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;ports&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;5000:5000&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;redis&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;image&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;redis:alpine&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;key-components-of-composeyml&#34;&gt;Key Components of compose.yml&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;version&lt;/strong&gt;: Specifies the Compose file format version.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;services&lt;/strong&gt;: Defines the containers to be run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;build&lt;/strong&gt;: Specifies the path to the Dockerfile.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;image&lt;/strong&gt;: Specifies the image to start the container from.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ports&lt;/strong&gt;: Maps container ports to host ports.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;volumes&lt;/strong&gt;: Mounts paths as volumes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;environment&lt;/strong&gt;: Adds environment variables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;depends_on&lt;/strong&gt;: Expresses dependency between services.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;basic-commands&#34;&gt;Basic Commands&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;docker compose up&lt;/code&gt;: Create and start containers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;docker compose down&lt;/code&gt;: Stop and remove containers, networks, images, and volumes&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;advanced-configuration&#34;&gt;Advanced Configuration&lt;/h3&gt;
&lt;h4 id=&#34;environment-variables&#34;&gt;Environment Variables&lt;/h4&gt;
&lt;p&gt;You can use environment variables in your Compose file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;web&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;image&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;webapp:${TAG}&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Use a &lt;code&gt;.env&lt;/code&gt; file or export variables in your shell.&lt;/p&gt;
&lt;h4 id=&#34;extending-services&#34;&gt;Extending Services&lt;/h4&gt;
&lt;p&gt;You can extend service definitions:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;web&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;extends&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;file&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;common-services.yml&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      &lt;span style=&#34;color:#f92672&#34;&gt;service&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;webapp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;networks&#34;&gt;Networks&lt;/h4&gt;
&lt;p&gt;Define custom networks for your services:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;web&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;networks&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;frontend&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;backend&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;networks&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;frontend&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;driver&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;bridge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;backend&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;driver&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;bridge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;volumes-1&#34;&gt;Volumes&lt;/h4&gt;
&lt;p&gt;Define named volumes for data persistence:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;db&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;image&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;postgres&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;volumes&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;db-data:/var/lib/postgresql/data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;volumes&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;db-data&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;docker-compose-in-development-vs-production&#34;&gt;Docker Compose in Development vs Production&lt;/h3&gt;
&lt;p&gt;While Docker Compose is great for development and testing, it&amp;rsquo;s not typically used in production environments. For production, consider using orchestration platforms like Kubernetes or Docker Swarm.&lt;/p&gt;
&lt;p&gt;However, you can use Compose for production with some adjustments:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use production-ready images&lt;/li&gt;
&lt;li&gt;Set appropriate environment variables&lt;/li&gt;
&lt;li&gt;Configure proper logging and monitoring&lt;/li&gt;
&lt;li&gt;Use secrets management for sensitive data&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;best-practices&#34;&gt;Best Practices&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Version Control&lt;/strong&gt;: Keep your &lt;code&gt;compose.yml&lt;/code&gt; in version control with your application code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Environment Separation&lt;/strong&gt;: Use multiple Compose files for different environments (dev, test, prod).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Don&amp;rsquo;t Abuse Extends&lt;/strong&gt;: While useful, overusing &lt;code&gt;extends&lt;/code&gt; can make your configuration hard to understand.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Depends_on Wisely&lt;/strong&gt;: Understand that &lt;code&gt;depends_on&lt;/code&gt; doesn&amp;rsquo;t wait for a service to be &amp;ldquo;ready&amp;rdquo;, only for it to start.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;example-web-application-with-database-and-cache&#34;&gt;Example: Web Application with Database and Cache&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s a more complex example of a &lt;code&gt;compose.yml&lt;/code&gt; file for a web application with a database and cache:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;version&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;3&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;services&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;web&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;build&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;ports&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;8000:8000&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;depends_on&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;db&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;redis&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;environment&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;DATABASE_URL=postgres://user:pass@db:5432/dbname&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;REDIS_URL=redis://redis:6379&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;db&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;image&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;postgres:13&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;volumes&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;postgres_data:/var/lib/postgresql/data&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;environment&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;POSTGRES_DB=dbname&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;POSTGRES_USER=user&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;POSTGRES_PASSWORD=pass&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;redis&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#f92672&#34;&gt;image&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;redis:6&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;volumes&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  &lt;span style=&#34;color:#f92672&#34;&gt;postgres_data&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This configuration sets up three services: a web application, a PostgreSQL database, and a Redis cache. It also defines a volume for persistent database storage.&lt;/p&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Docker is a powerful tool with many more advanced features and use cases. As you become more comfortable with these basics, you can explore more complex scenarios and integrations to further improve your development and deployment workflows.&lt;/p&gt;
&lt;p&gt;Remember to always refer to the official Docker &lt;a href=&#34;https://docs.docker.com/&#34;&gt;documentation&lt;/a&gt; for the most up-to-date and detailed information.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Artificial Neural Networks: Embedding Models</title>
      <link>http://notes.mirzo.net/post/ann-embedding/</link>
      <pubDate>Wed, 28 Jul 2021 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/ann-embedding/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/ann-embedding/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/embeddings1_hu3155d4f838d7a703320a1936edc41fec_1382500_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Artificial Neural Networks: Embedding Models&#34; title=&#34;Artificial Neural Networks: Embedding Models&#34;&gt;&lt;/a&gt;


&lt;p&gt;Before we can process language-based data using Artificial Neural Networks (ANNs), we need to convert this data into some kind of a numerical representation. Embedding models are designed for this purpose. They transform language data into dense high-dimensional vectors that preserve the semantic associations between words. These vectors capture the essence of language data in a way that computers can understand and process. This note explores the most popular embedding model architectures, looks into how these models are trained, and discusses their critical role in Natural Language Processing (NLP).&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Embeddings&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NLP Concepts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This note requires some understanding of basic NLP concepts. Before proceeding, make sure that you are &lt;a href=&#34;http://notes.mirzo.net/post/nlp-primer/&#34;&gt;well primed on NLP basics&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;language-as-numbers&#34;&gt;Language as Numbers&lt;/h2&gt;
&lt;p&gt;Before the advent of embedding models, most NLP routines achieved text-to-numerical conversion using &lt;strong&gt;local representations&lt;/strong&gt;, where each entity is represented as an isolated unique identifier. This is generally achieved by one-hot encoding, where a word is represented by a vector with the size of the entire vocabulary (usually with millions of dimensions). Nearly all values in such vectors are equal to 0, except a single value equaling to 1 at an index uniquely representing the encoded word. One-hot encoded vectors, while simple and intuitive, come with several disadvantages. First, these representations rely on vectors with an extremely high dimensionality, especially for languages with large vocabularies. This can lead to memory inefficiencies and computational challenges that stem from the &lt;a href=&#34;http://notes.mirzo.net/post/principal-component-analysis/&#34;&gt;&amp;ldquo;curse of dimensionality&amp;rdquo;&lt;/a&gt;. Second, one-hot encoding does not capture any semantic or syntactic relationships between words, meaning that each word is treated as an entirely independent entity. This lack of contextual information prevents the model from understanding nuances, such as similarities between words or their meanings in different contexts. Embedding models are designed to address these issues by foregoing the sparse-vector representations, and instead producing dense-vector &lt;strong&gt;distributed  representations&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&#34;projections-on-a-manifold&#34;&gt;Projections on a Manifold&lt;/h2&gt;
&lt;p&gt;Embedding models operate on the &lt;strong&gt;distributional hypothesis&lt;/strong&gt;, which states that words with similar meanings tend to appear in similar contexts. Embedding models project data in the form of dense vectors onto a high-dimensional manifold, usually containing hundreds to thousands of dimensions. Each of these dimensions represent some semantic quality of a word. As a highly oversimplified example, one dimension can reflect how &amp;ldquo;purple&amp;rdquo; a word might be, while another can reflect how much a word relates to a concept of a &amp;ldquo;house&amp;rdquo;. In other words, each dimension represents a latent variable that combines specific features of the data into a quantifiable representation of a specific relationship. In this sense, embeddings achieve a distributed representation of words across the multiple latent dimensions. Since, embedding models are trained to map words based on their contextual relationships i.e., how often certain words appear in similar contexts, this leads to mappings where semantically similar words get an overall shorter distance between their vectors in the embedded space. Thus, vector similarity measures (e.g. &lt;a href=&#34;http://notes.mirzo.net/post/linear-algebra-basics/#the-inner-dot-product&#34;&gt;cosine similarity&lt;/a&gt;), or distance metrics, can be used to measure and query semantic relationships between words.&lt;/p&gt;
&lt;h2 id=&#34;data-preparation&#34;&gt;Data Preparation&lt;/h2&gt;
&lt;p&gt;Embedding models are trained on large corpora of text: large enough to represent the variety of contexts that each word can exhibit. Before model training can begin, the training data needs to be cleaned up and preprocessed. The preparation generally follows the usual &lt;a href=&#34;http://notes.mirzo.net/post/nlp-primer/#data-preprocessing&#34;&gt;NLP data preprocessing&lt;/a&gt; steps. It should be noted, however, that the removal of stopwords is somewhat of a contentious topic in embedding model training. In some applications, this can improve performance, but we have to be careful about the choice of stopwords. Words that carry semantic meaning, like negations, should not be filtered. Sub-sampling is a much more common approach to reduce the noise in the dataset for this application.&lt;/p&gt;
&lt;h2 id=&#34;models&#34;&gt;Models&lt;/h2&gt;
&lt;h3 id=&#34;word2vec&#34;&gt;Word2Vec&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://arxiv.org/pdf/1301.3781&#34;&gt;Word2Vec&lt;/a&gt; is a family of efficient embedding models that can be trained using one of two architectures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Continuous Bag-of-Words (CBOW)&lt;/strong&gt; predicts a target word based on a set of context words. Just like in the standard &lt;a href=&#34;http://notes.mirzo.net/post/nlp-primer/#bag-of-words&#34;&gt;BOW model&lt;/a&gt;, the order of words is ignored.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Skip-Gram&lt;/strong&gt; works in the opposite manner by predicting the most probable context words given a single target word. Unlike in CBOW, context words that are more proximate to the target are weighed more heavily. It is more effective in capturing detailed relationships from smaller datasets, but it is more computationally expensive.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The structure of a basic Word2Vec model is surprisingly simple: an input layer is followed by a single linear &amp;ldquo;projection&amp;rdquo; hidden layer, which is then followed by a softmax output layer.&lt;/p&gt;
&lt;pre class=&#34;mermaid&#34;&gt;flowchart LR

inputs[&#34;Inputs \n size: 1 × V \n one or multiple vectors&#34;]
embeddingM[[&#34;Embedding Matrix \n size: V × N&#34;]]
projection[&#34;Projection Layer \n size: N × 1&#34;]
unembeddingM[[&#34;Un-embedding Matrix \n size: N × V&#34;]]
output[&#34;Output Vector \n size: V × 1&#34;]

inputs --&gt; embeddingM --&gt; projection --&gt; unembeddingM --&#34;softmax&#34;--&gt; output
&lt;/pre&gt;

&lt;p&gt;Let&amp;rsquo;s first look at the CBOW architecture in more detail. At the input we start with a set of context words, each encoded into a one-hot vector of size V (the size of the vocabulary). Each of the context word vectors is multiplied by a weight matrix of size V × N, where N is the desired dimensionality of the embedding representation (usually 100-300). This &amp;ldquo;embedding matrix&amp;rdquo; is the most important piece of the model: once it is trained, the rows of this matrix end up holding the embedded representations of each word in the vocabulary. Multiplying this matrix by a one-hot encoded word vector essentially performs a filtering operation for the embedding representation of the word, essentially making it an embedding lookup table. It should be noted that during training the same embedding matrix is shared between all of the input context words. As a result, the matrix multiplication products of each of the context words are averaged together in a single vector, producing their composite representation. This averaging is the only operation performed in the &amp;ldquo;projection&amp;rdquo; layer, no use of non-linear functions required. This is one of the defining innovations that Word2Vec models introduced, significantly reducing their computational complexity. For model training purposes, this projection layer then gets multiplied by an &amp;ldquo;un-embedding&amp;rdquo; weight matrix of size N × V, which converts the contents of the projection layer back into the dimensionality of the vocabulary. This results in an intermediate output vector, which is then converted into the final output probability layer using &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/#activation-functions&#34;&gt;softmax&lt;/a&gt;. The training of the model then typically follows the standard &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/#backpropagation&#34;&gt;backpropagation&lt;/a&gt; approach with stochastic gradient descent optimization.&lt;/p&gt;
&lt;p&gt;The skip-gram model (or fill-in-the-blank model) works in a similar way, but with a few key differences. At the input, we start with a single one-hot encoded target word vector. As in the CBOW case, the input is multiplied by the embedding matrix, but since there is only one input, there is no need to perform any averaging in the projection layer. Again, an &amp;ldquo;un-embedding&amp;rdquo; matrix is used to convert the representation back into the vocabulary dimensionality, and softmax is used to predict a single context word. In training, one target word is forward propagated, and then multiple context words are used to update the weight matrixes by backpropagation, one at a time. This, in a sense, achieves the same kind of composite representation of the multiple context words as was achieved by averaging in CBOW. However, in this scheme we can weight more proximal words higher by using subsampling. For example, if our defined context window is of size C, during training the window size for each training set gets randomly chosen to be between 1 and C. This means that proximal context words get sampled more often than distant words, and thus, have a higher weight in the composite representation.&lt;/p&gt;
&lt;h3 id=&#34;glove&#34;&gt;GloVe&lt;/h3&gt;
&lt;p&gt;The &lt;a href=&#34;https://nlp.stanford.edu/pubs/glove.pdf&#34;&gt;GloVe&lt;/a&gt; (Global Vectors for Word Representation) model works by constructing word embeddings through the analysis of word co-occurrence statistics across a large corpus of text. GloVe captures the meaning of a word by leveraging global statistical information about word pairs. To achieve this, GloVe creates a word-word co-occurrence matrix, where each entry represents the number of times a word pair appears together within a specific context window (omitting the 0 co-occurrence entries). While somewhat computationally expensive, this co-occurrence matrix can be generated over a single pass through over the corpus. The efficiency of the model is recovered in training, since the number of co-occurrences is generally smaller than the total size of the corpus.&lt;/p&gt;
&lt;p&gt;Once the co-occurrence matrix is built, GloVe optimizes a cost function to &lt;a href=&#34;http://notes.mirzo.net/post/matrix-factorization/&#34;&gt;factorize&lt;/a&gt; this matrix, effectively transforming the sparse co-occurrence data into dense word vectors. The optimization process aims to find word vectors such that the dot product of two word vectors approximates the logarithm of their co-occurrence probability. Specifically, the cost function penalizes deviations between the dot product of the word vectors and the logarithm of the corresponding co-occurrence frequency, with a weighting function to ensure that rare and frequent co-occurrences are treated appropriately.&lt;/p&gt;
&lt;p&gt;During training, the GloVe model iteratively adjusts the word vectors to minimize the cost function, resulting in word embeddings that capture both syntactic and semantic relationships. Words that frequently co-occur with similar words are positioned close to each other in the vector space, while words with different contexts are placed farther apart. This approach allows GloVe embeddings to effectively represent complex linguistic patterns and relationships, providing a powerful tool for various natural language processing tasks.&lt;/p&gt;
&lt;p&gt;Compared to Word2Vec, GloVe is more computationally expensive, but is able to capture more accurate contextual information by leveraging the overall statistical properties of the corpus. While Word2Vec is very efficient at capturing local context within sentences, it often struggles with capturing global context of the corpus.&lt;/p&gt;
&lt;h3 id=&#34;elmo&#34;&gt;ELMo&lt;/h3&gt;
&lt;p&gt;Word2Vec and GloVe models provide static embeddings which use a single vector representation for each word regardless of its context. &lt;a href=&#34;https://arxiv.org/abs/1802.05365&#34;&gt;ELMo&lt;/a&gt; (Embeddings from Language Models), on the other hand, generates dynamic word embeddings that vary depending on the word&amp;rsquo;s context within a sentence, effectively capturing polysemy and disambiguating word meanings based on their usage. This is achieved through a multi-layer bidirectional LSTM (&lt;a href=&#34;http://notes.mirzo.net/post/ann-architectures/#recurrent-neural-networks-rnns&#34;&gt;Long Short-Term Memory&lt;/a&gt;) network. The model is pre-trained on a large corpus of text, where it learns to predict the next word in a sequence (forward direction) and the previous word in a sequence (backward direction). Let&amp;rsquo;s look at each step in detail:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ELMo does not use word one-hot encoding at the input, instead it converts character tokens from the input into dense vector representations using a character-level Convolutional Neural Network (CNN). This character-based representation helps in handling out-of-vocabulary words (including missspellings) and capturing morphological information. This character-level CNN is quite interesting on its own, so let&amp;rsquo;s briefly look at its structure:
&lt;ul&gt;
&lt;li&gt;At the input, one-hot encoded character vectors are converted into embeddings using a simple character-embedding layer that is very reminiscent of the Word2Vec structure.&lt;/li&gt;
&lt;li&gt;The convolutional layer then applies a series of filters (kernels) to the sequence of character embeddings. Each filter slides over the sequence, performing element-wise multiplications and summing the results to produce a feature map. These filters are trained to capture local patterns in the character sequence, such as prefixes, suffixes, and other morphological structures. The convolution layer is typically followed by a non-linear activation function like ReLU.&lt;/li&gt;
&lt;li&gt;A pooling layer, such as max pooling, is often applied after the convolutional layer. Pooling reduces the dimensionality of the feature map by selecting the maximum value within a specified window, effectively summarizing the presence of a feature in a particular region of the input.&lt;/li&gt;
&lt;li&gt;The pooled feature maps are then flattened and passed through one or more fully connected layers to produce the final character-level representation of the word. This representation captures the morphological and structural information of the word, which can be combined with other word-level features in subsequent layers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The core of ELMo consists of multiple layers of bidirectional LSTM networks. These layers process the sequence of input word vector representations in two directions: forward (left to right) and backward (right to left). The forward pass in each bidirectional layer is meant to encapsulate the preceding context of the target word, while the backward pass captures it&amp;rsquo;s following context. Each LSTM layer generates hidden states for each word in the sequence, capturing contextual information from both directions.&lt;/li&gt;
&lt;li&gt;The final word representation is obtained by applying a learned weighted sum (scalar mix) of the hidden states from all LSTM layers. These weights are task-specific and are fine-tuned during the training of downstream tasks. The scalar mix allows the model to dynamically adjust the importance of different layers based on the specific task.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Using embeddings can often feel like magic. They transform abstract linguistic concepts into tangible numerical forms that a computer can work with, revealing the hidden relationships and meanings in language data. It&amp;rsquo;s fascinating to see how a mathematical model can understand and capture the essence of human language, bringing words to life in a way that is both intuitive and powerful. Just the simple fact that embedding models work says a lot about human language and how how we use it to communicate information.&lt;/p&gt;
&lt;p&gt;You will notice that this article has a glaring omission in describing embeddings within transformer models. We will delve into this fascinating subject in a subsequent note, exploring how these modern architectures further enhance the capabilities of NLP systems.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Embeddings&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>How to Manage conda Environments</title>
      <link>http://notes.mirzo.net/post/managing-conda/</link>
      <pubDate>Thu, 08 Jul 2021 12:00:00 -0400</pubDate>
      
      <guid>http://notes.mirzo.net/post/managing-conda/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/managing-conda/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/programmer_snake1_hu536e235dc62f90050fc805d99dfc85ad_598751_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled How to Manage conda Environments&#34; title=&#34;How to Manage conda Environments&#34;&gt;&lt;/a&gt;


&lt;p&gt;If you are using &lt;a href=&#34;https://www.anaconda.com/download&#34;&gt;Anaconda&lt;/a&gt;, the popular python distribution for data science, you know that it comes with its own package and environment manager called &lt;code&gt;conda&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There are many tutorials out there, that tell you to update your &lt;code&gt;conda&lt;/code&gt; environments using &lt;code&gt;conda update --all&lt;/code&gt;. Luckily, you know that this is a &lt;strong&gt;terrible&lt;/strong&gt; idea!&lt;/p&gt;
&lt;p&gt;When distributions like Anaconda are created, a lot of care goes into ensuring version compatibility between the included packages. Performing a bulk update with &lt;code&gt;conda update --all&lt;/code&gt; unpins package versions and destroys all of this hard work.&lt;/p&gt;
&lt;p&gt;So instead of letting the chaos reign, let&amp;rsquo;s learn to harness the power of &lt;code&gt;conda&lt;/code&gt; as an environment manager. Just remember the main tenet: you &lt;em&gt;really&lt;/em&gt; should have a separate environment for each of your projects. While it may seem onerous, in the long term it will save you a lot of pain and surprises like &amp;ldquo;it was working just yesterday!&amp;rdquo;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sidenote: Never use the base environment&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;base&lt;/em&gt; conda environment is not for you! It is there to   support the basic functionality of conda itself. The only thing you are allowed to do from the &lt;em&gt;base&lt;/em&gt; environment is update conda: &lt;code&gt;conda update conda&lt;/code&gt;. You should never install anything extraneous into &lt;em&gt;base&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the examples below, we will be dealing with a hypothetical environment called &lt;em&gt;envy&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&#34;from-the-command-line&#34;&gt;From the Command Line&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s say you are starting a new project that uses a large distribution or collection of packages (Anaconda in our examples) and a handful of individual packages.&lt;/p&gt;
&lt;p&gt;I like to prioritize either the language version or the large distribution version as the defining feature of an environment and install them at the creation step.&lt;/p&gt;
&lt;p&gt;So, if you need the latest version of the distribution:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda create -n envy anaconda
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Or you can specify a &lt;a href=&#34;https://repo.anaconda.com/archive/&#34;&gt;specific release&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda create -n env_name anaconda&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;2021.05&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now let&amp;rsquo;s say you care more about a specific version of python:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda create -n envy python&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;3.8 anaconda&lt;span style=&#34;color:#e6db74&#34;&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, you can also specify versions of both components. But unless you have very specific needs, try to specify only one of the two, and let the package manager figure out the most compatible counterpart.&lt;/p&gt;
&lt;p&gt;I usually don&amp;rsquo;t install individual packages at the environment creation step. This is because at each individual installation command, &lt;code&gt;conda&lt;/code&gt; creates a revision (i.e., a snapshot) of the environment. If an installed package messes with the rest of the environment, you can always roll it back to a previous state.&lt;/p&gt;
&lt;p&gt;To view the list of available snapshots:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda list --revisions
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To roll back to a specific revision:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda install --revision N
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;where &lt;code&gt;N&lt;/code&gt; is the revision number.&lt;/p&gt;
&lt;p&gt;At the same time, don&amp;rsquo;t try to create an overly complex revision history by installing one package at a time. This can lead to dependency conflicts. Instead, group installations of related packages, or packages that tend to interact with each other a lot.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sidenote: using pip&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;conda&lt;/code&gt; repositories may not contain all packages present in PyPI. You can use &lt;code&gt;pip&lt;/code&gt; to install such packages, but doing so indiscriminately can break things. Always try to install as many packages with &lt;code&gt;conda&lt;/code&gt; as possible, then finish things off with &lt;code&gt;pip&lt;/code&gt;. If further modifications are needed to the environment, it is best to create a new environment rather than running &lt;code&gt;conda&lt;/code&gt; after &lt;code&gt;pip&lt;/code&gt;. Never run &lt;code&gt;pip&lt;/code&gt; from the &lt;em&gt;base&lt;/em&gt; environment.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, let&amp;rsquo;s imagine that you&amp;rsquo;ve been using your environment for a while. You&amp;rsquo;ve written a lot of code that works well with this environment. But now you need to update, add, or remove a package. As we&amp;rsquo;ve learned, we are not going to do it to your original environment. We are going to clone it  instead!&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda create --name envyMar2023 --clone envy
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then make the necessary changes in the new environment. As you see, it&amp;rsquo;s helpful to add differentiators (like a date) in the environment name to help manage them better.&lt;/p&gt;
&lt;h2 id=&#34;using-yaml-files&#34;&gt;Using YAML files&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Sidenote: Spot the difference!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Note that in the previous section, the commands to create the environments use &lt;code&gt;conda create&lt;/code&gt;, while in this section they will use &lt;code&gt;conda env create&lt;/code&gt;. These commands are not equivalent. &lt;code&gt;conda env&lt;/code&gt; is a special tool used to create environments from files, rather than from the command line.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you want to have full control over your environments, you need to learn about environment YAML files. These will help you maintain their reusable definitions to whatever level of specificity you desire. They are invaluable when you intend to recreate and update environments frequently.&lt;/p&gt;
&lt;p&gt;To get an example of what an environment YAML file looks like, activate your target environment and run:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda env export -f envy.yml
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will create the envy.yml file in the active directory. It will contain the list of all installed packages, their versions, and their builds. Such a file works great when you want to reproduce an environment exactly. But in our case, we are less concerned about specific versions of all packages, but rather in defining the minimal skeleton of an environment, and then letting conda come up with the best combination of versions. For this, we need to create a definition file manually:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;name&lt;/span&gt;: &lt;span style=&#34;color:#ae81ff&#34;&gt;envy&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;channels&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;nvidia&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;defaults&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;conda-forge&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;dependencies&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;python=3.10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;anaconda&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;langchain&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;psycopg2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;pytorch-cuda&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;sentence-transformers&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#ae81ff&#34;&gt;umap-learn&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  - &lt;span style=&#34;color:#f92672&#34;&gt;pip&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;gpt4all&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;pymupdf&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;torchaudio&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;torchvision&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;      - &lt;span style=&#34;color:#ae81ff&#34;&gt;hdbscan&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As in the case with the command line, I have pinned the language version, and left the other packages unpinned. Packages to be installed using pip are listed as well.&lt;/p&gt;
&lt;p&gt;Now, when you want to create an environment from the YAML file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda env create -f envy.yml
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you want to install the environment in a non-default location, e.g., inside the project folder (&amp;ldquo;node_modules&amp;rdquo; style), you can use a &amp;ldquo;prefix&amp;rdquo; definition:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda env create -f envy.yml --prefix&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;/path/to/your/folder
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can also use a YAML file to update an environment (hopefully a clone of an environment that you have been using previously)&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda env update -f envy.yml  --prune
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;recreating-from-history&#34;&gt;Recreating from History&lt;/h2&gt;
&lt;p&gt;When you run &lt;code&gt;conda list&lt;/code&gt; or &lt;code&gt;conda env export&lt;/code&gt;, you get a full list of every package in your environment, including both the ones you installed explicitly and the ones that were added as dependencies. This means these commands are not very useful if your goal is to create a minimal environment YAML file.&lt;/p&gt;
&lt;p&gt;Fortunately, conda keeps a history file that records all the commands used to create and modify an environment. You can use this file to reconstruct how the environment was originally built.&lt;/p&gt;
&lt;p&gt;First, find the directory (also called the &lt;em&gt;environment prefix&lt;/em&gt;) where your environment is installed:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;conda env list
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# conda environments:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# 
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# base           C:\Users\user\mambaforge
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# envy           C:\Users\user\mambaforge\envs\envy
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Copy the path for your target environment, such as &lt;code&gt;envy&lt;/code&gt;. Each environment has a history file located in its &lt;code&gt;conda-meta&lt;/code&gt; subfolder. For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;C:\Users\user\mambaforge\envs\envy\conda-meta\history
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The history file includes many types of information, but the most useful lines are those that begin with &lt;code&gt;# cmd:&lt;/code&gt;. These lines record the exact conda commands that were used to create and modify the environment.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-cmd&#34; data-lang=&#34;cmd&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# cmd: C:\Users\...\mamba create -n envy python=3.10 anaconda
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# cmd: C:\Users\...\mamba install psycopg2 biopython fuzzywuzzy pyahocorasick anytree
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;# cmd: C:\Users\...\mamba install pypdf
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can now extract the package names that you explicitly installed and use them to build a clean, minimal &lt;code&gt;environment.yml&lt;/code&gt;. This file will contain only the packages you chose directly, without all the automatically installed dependencies.&lt;/p&gt;
&lt;h2 id=&#34;bonus-ditch-conda-for-mamba&#34;&gt;Bonus: ditch conda for mamba!&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;conda&lt;/code&gt; is a decent environment manager, but it is awfully slow. &lt;a href=&#34;https://mamba.readthedocs.io/en/latest/mamba-installation.html&#34;&gt;Mamba&lt;/a&gt; is a conda re-write that is much faster and more reliable. I suggest installing a minimal &lt;em&gt;mambaforge&lt;/em&gt; distribution, that comes pre-configured with the conda-forge channel, and with a lean &lt;em&gt;base&lt;/em&gt; environment. Then just substitute &lt;code&gt;conda&lt;/code&gt; in all of the previous commands with &lt;code&gt;mamba&lt;/code&gt;, and enjoy all of the free time you gain from managing environments!&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Decentralized Clinical Trials</title>
      <link>http://notes.mirzo.net/post/decentralized-trials/</link>
      <pubDate>Tue, 06 Jul 2021 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/decentralized-trials/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/decentralized-trials/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/decentralized-trials1_hu4d27b27a28299f3d82e35824d73ed8be_1383726_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Decentralized Clinical Trials&#34; title=&#34;Decentralized Clinical Trials&#34;&gt;&lt;/a&gt;


&lt;p&gt;Decentralized clinical trials (DCTs) represent a transformative approach in the field of clinical research. By leveraging technology and innovative methodologies, DCTs offer a way to conduct studies that are less reliant on traditional, centralized site-based models. This shift is not just a logistical change; it&amp;rsquo;s a strategic one that can significantly impact drug development, patient accessibility, and the overall speed and efficiency of bringing new therapies to market.&lt;/p&gt;
&lt;p&gt;Decentralized clinical trials utilize digital health technologies, such as telemedicine, mobile health apps, wearable devices, and remote monitoring tools to facilitate participation from anywhere. This means that rather than traveling to a central hospital or clinic, participants can engage in the trial from their homes or local community centers. This model encompasses a range of formats, from fully remote trials to hybrid trials that mix traditional and decentralized methods.&lt;/p&gt;
&lt;h2 id=&#34;core-technologies&#34;&gt;Core Technologies&lt;/h2&gt;
&lt;p&gt;DCTs rely on a range of core technologies to implement various aspects of the trial process from patient recruitment to data collection and analysis.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Telemedicine:&lt;/strong&gt; Telemedicine platforms are essential for remote patient consultations, follow-ups, and even for obtaining informed consent. These platforms enable direct communication between patients and researchers or healthcare providers, making clinical trials more accessible to participants regardless of their geographic location.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Mobile Health Applications (mHealth apps):&lt;/strong&gt; These apps are used to manage and monitor patient health data remotely. They can prompt patients to take their medications, record symptoms, or provide health updates, which are then shared with the clinical trial team. This not only ensures continuous monitoring but also enhances patient engagement and compliance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Wearable Devices:&lt;/strong&gt; Wearable technologies like fitness trackers, smartwatches, and biosensors can continuously collect data on a patient’s physiological parameters such as heart rate, activity levels, sleep patterns, and more. This data provides insights into the efficacy and safety of the interventions being studied.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Electronic Patient-Reported Outcomes (ePRO) Tools:&lt;/strong&gt; ePRO tools collect data directly from patients using electronic systems. Unlike traditional paper-based methods, ePRO systems utilize electronic devices such as smartphones, tablets, or computers, enabling patients to report symptoms, side effects, well-being, and overall quality of life in real time, directly relating this information to critical trial endpoints. Additionally, ePRO systems can be integrated with reminders and alerts, improving patient engagement and compliance with the reporting schedule, which is crucial for maintaining the integrity and reliability of trial data. By leveraging these technologies, researchers can achieve a more continuous and dynamic assessment of treatment impacts, leading to better-informed healthcare decisions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Electronic Health Records (EHRs):&lt;/strong&gt; EHRs, the digital versions of patients&amp;rsquo; paper charts, are crucial for integrating and organizing patient data across different stages of the trial and from various sources. They can be used to streamline data collection, ensure accuracy, and facilitate data sharing under compliance with privacy regulations. EHRs are real-time, patient-centered records that make information available instantly and securely to authorized users. They help in organizing patient information across different healthcare providers and settings, allowing for coordinated and efficient care.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Management and Analytics Platforms:&lt;/strong&gt; Electronic Data Capture (EDC) systems, or larger data analytics platforms are used to handle the large volumes of data generated in DCTs. They support data integration, processing, and analysis, ensuring that data is reliable and usable for making clinical decisions. Advanced analytics, including AI and machine learning, can also be employed to uncover insights from the data more efficiently. Some popular examples include Medidata Rave, SAS Clinical Trial Data Management, IBM Watson Health, and others.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Blockchain:&lt;/strong&gt; Blockchain technology can enhance data security and integrity in DCTs. It provides a secure and transparent way to log and track data transactions, which is crucial for maintaining trust and adherence to regulatory requirements.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Internet of Things (IoT):&lt;/strong&gt; IoT devices can be used to collect additional environmental or physiological data, which can be integrated with other clinical data sources to provide a comprehensive view of a patient&amp;rsquo;s health and the environment they live in.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These technologies not only facilitate the logistical execution of decentralized clinical trials but also help in enhancing the quality of data collected, ensuring patient safety, and improving the overall efficiency of the trial process.&lt;/p&gt;
&lt;h2 id=&#34;benefits&#34;&gt;Benefits&lt;/h2&gt;
&lt;p&gt;One of the most significant benefits of DCTs is their ability to increase &lt;strong&gt;participant diversity&lt;/strong&gt;. Traditional trials often struggle with enrollment and retention, partly because potential participants may live far from study sites or cannot commit to frequent visits due to work, family responsibilities, or physical limitations. DCTs, by removing these barriers, can draw from a broader demographic pool, including underrepresented groups, leading to outcomes that are more generalizable across different populations.&lt;/p&gt;
&lt;p&gt;Participants in decentralized trials typically report higher satisfaction due to the convenience of participating from home and on their schedules. This convenience can lead to &lt;strong&gt;better compliance and retention rates&lt;/strong&gt;. Moreover, using technologies like apps to report outcomes or wearables to collect data can make participants feel more engaged and empowered in the trial process.&lt;/p&gt;
&lt;p&gt;By broadening the geographic constraints of traditional clinical trials, DCTs can accelerate the recruitment process. This &lt;strong&gt;faster recruitment&lt;/strong&gt; not only speeds up the overall trial timeline but also reduces the costs associated with participant acquisition and retention.&lt;/p&gt;
&lt;p&gt;Decentralized trials often use tools that collect data continuously and in &lt;strong&gt;real-time&lt;/strong&gt;, providing a more &lt;strong&gt;accurate&lt;/strong&gt; reflection of a treatment&amp;rsquo;s effectiveness and safety in a real-world setting. This type of data is invaluable in shaping regulatory decisions and health policy.&lt;/p&gt;
&lt;h2 id=&#34;challenges&#34;&gt;Challenges&lt;/h2&gt;
&lt;p&gt;Despite their advantages, DCTs also face several challenges:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Security and Privacy:&lt;/strong&gt; The increased use of digital tools raises concerns about data security and privacy. Robust cybersecurity measures and clear data governance policies are essential to protect sensitive patient information.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regulatory Acceptance:&lt;/strong&gt; While regulatory agencies have shown support for DCTs, especially highlighted during the COVID-19 pandemic, there is still a need for clear guidelines and standards specifically tailored to decentralized methodologies.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Technology Access and Literacy:&lt;/strong&gt; Ensuring that all participants have access to the necessary technology and are comfortable using it is crucial. This may require additional training or support, which can introduce new complexities in trial design.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;regulatory-landscape&#34;&gt;Regulatory Landscape&lt;/h2&gt;
&lt;p&gt;The regulations around DCTs are evolving rapidly across various global jurisdictions, reflecting the increasing adoption and integration of these trial formats within the pharmaceutical and biotech industries.&lt;/p&gt;
&lt;p&gt;In the U.S., the FDA has been proactive in addressing the needs of DCTs through specific guidances that encourage the use of digital health technologies (DHTs) and promote innovative trial designs. These guidances are designed to ensure that DCTs maintain good clinical practice (GCP) standards while leveraging technological advances to enhance trial efficiency and participant experience. The FDA&amp;rsquo;s efforts include detailed recommendations on the use of DHT-derived data in regulatory decision-making processes for drugs and biological products. The FDA has issued the significant &amp;ldquo;Decentralized Clinical Trials for Drugs, Biological Products, and Devices&amp;rdquo; guidance (&lt;a href=&#34;https://www.fda.gov/regulatory-information/search-fda-guidance-documents/decentralized-clinical-trials-drugs-biological-products-and-devices&#34;&gt;link&lt;/a&gt;), which offers comprehensive recommendations on implementing DCTs, ensuring robust trial designs, and maintaining data integrity and participant safety. This guidance covers various aspects, from the use of digital health technologies (DHTs) to the roles and responsibilities of trial sponsors and investigators. The FDA underscores the importance of securing data, handling investigational products appropriately, and monitoring participant safety throughout the DCT process.&lt;/p&gt;
&lt;p&gt;In Europe, the regulatory response has been somewhat varied across member states, but the European Commission has issued guidance to manage DCTs, particularly in response to the COVID-19 pandemic. This includes updates on electronic medical records, remote monitoring, and data verification practices. The European Union&amp;rsquo;s Clinical Trials Regulation (&lt;a href=&#34;https://www.ema.europa.eu/en/human-regulatory-overview/research-development/clinical-trials-human-medicines/clinical-trials-regulation&#34;&gt;link&lt;/a&gt;), which came into effect in 2022, addresses several aspects of DCTs, aiming to harmonize the approach across the EU.&lt;/p&gt;
&lt;h2 id=&#34;the-future-of-dcts&#34;&gt;The Future of DCTs&lt;/h2&gt;
&lt;p&gt;As technology continues to advance and integrate more deeply into healthcare, the potential for DCTs to become a standard option in clinical research grows. For DCTs to reach their full potential, collaboration across the healthcare ecosystem, including biotech companies, technology providers, regulatory bodies, and patient advocacy groups, is crucial.&lt;/p&gt;
&lt;p&gt;In conclusion, decentralized clinical trials offer a promising path forward in making clinical research more patient-centered, inclusive, and efficient. By addressing the current challenges and continuing to innovate, DCTs can significantly impact how new treatments are developed and delivered to those who need them most.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Clinical Trial Designs</title>
      <link>http://notes.mirzo.net/post/clinical-trial-designs/</link>
      <pubDate>Wed, 05 May 2021 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/clinical-trial-designs/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/clinical-trial-designs/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/clinical-trial-designs1_hu8b5f5a207c0e42e80bc58e1001cb9d96_1348838_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Clinical Trial Designs&#34; title=&#34;Clinical Trial Designs&#34;&gt;&lt;/a&gt;


&lt;p&gt;Clinical trials are crucial in medical research, helping to determine the safety and efficacy of new treatments. The designs of these trials vary based on their objectives and the phase of the trial. In this note, we&amp;rsquo;ll explore some of the most popular and impactful clinical trial designs, from the classic randomized controlled trials to innovative adaptive designs.&lt;/p&gt;
&lt;h2 id=&#34;clinical-trial-phases&#34;&gt;Clinical Trial Phases&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with a quick review of trial phases, looking at their specific objectives and characteristics. These phases are designed to answer different research questions and ensure the safety and efficacy of new treatments before they become widely available.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase 0&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objective&lt;/strong&gt;: To assess the toxicity and pharmacokinetics (PK) of a drug following a small subtherapeutic dose, usually administered for a short amount of time (no more than a week).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Participants&lt;/strong&gt;: Very small number, typically around 10-15.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: To decide if it is safe for a drug to move on to testing at therapeutic doses. First introduced by the FDA in 2006, Phase 0 trials are a fairly new addition to the scheme. They usually follow fairly straightforward designs, with a single pre-defined dose and administration schedule. As such, we will not consider this phase in our further discussions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase I&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objective&lt;/strong&gt;: To determine a drug&amp;rsquo;s safety profile, including the safe dosage range, and to identify side effects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Participants&lt;/strong&gt;: Small group of 20-100 volunteers, usually healthy participants but sometimes includes patients.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: The main focus is on a drug’s safety and side effects at different doses and how it is metabolized and excreted. The key output of a Phase I trial is the Recommended Phase 2 Dose (RP2D).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase II&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objective&lt;/strong&gt;: To assess the drug&amp;rsquo;s efficacy and further evaluate its safety.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Participants&lt;/strong&gt;: Larger group, typically 100-300 patients who have the condition the drug is meant to treat.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: This phase aims to obtain preliminary data on whether the drug works in people who have a certain disease or condition.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase III&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objective&lt;/strong&gt;: To confirm the drug’s effectiveness, monitor side effects, compare it to commonly used treatments, and collect information that will allow the drug to be used safely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Participants&lt;/strong&gt;: Large number of patients, ranging from several hundred to several thousand.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: These trials are pivotal for the approval process and often involve a randomized and blind testing in several hundred to several thousand patients.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase IV&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Objective&lt;/strong&gt;: To monitor the long-term effectiveness and impact of a treatment after it has been approved and is on the market.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Participants&lt;/strong&gt;: Varies widely depending on the drug&amp;rsquo;s widespread use and the specific aspects being studied.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purpose&lt;/strong&gt;: Phase IV trials can result in a drug being withdrawn from the market or restrictions on its use being placed based on the findings.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;phase-i-designs&#34;&gt;Phase I Designs&lt;/h2&gt;
&lt;p&gt;Phase I clinical trials are primarily designed to evaluate the safety, tolerability, pharmacokinetics, and pharmacodynamics of a drug. These trials are often the first step in testing new drugs in humans, and they use several common designs to meet their objectives effectively. Here are some of the most frequently used Phase I trial designs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Single Ascending Dose (SAD)&lt;/strong&gt;:
Participants receive a single dose of the drug, with each new group receiving a higher dose. Used to assess the safety profile and identify any side effects at increasing dose levels. Often, not a very efficient approach, both in terms of time and the number of subjects required.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Multiple Ascending Dose (MAD)&lt;/strong&gt;:
Participants receive multiple low doses of the drug, with the dose being escalated in subsequent groups over a period. Used to study the pharmacokinetics and pharmacodynamics when the drug is administered multiple times and to identify any cumulative toxicity.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;3+3 Dose-Escalation&lt;/strong&gt;:
This design involves starting with a cohort of three patients at a low dose and observing them for toxicity. If no significant toxicities are observed in all three patients, the dose is escalated to a higher level in a new cohort. If one of the three patients experiences toxicity, three additional patients are treated at the same dose. If two or more patients (including the additional set of 3) experience significant toxicity, the dose escalation is halted, and this dose may be considered above tolerable levels. This method is especially prevalent in oncology trials, where establishing the Maximum Tolerated Dose (MTD) of a new cancer therapy is crucial.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Accelerated Titration&lt;/strong&gt;:
Similar to the 3+3 design but includes fewer dose levels and allows for faster dose escalation. This is meant to ensure that fewer patients are treated at subtherapeutic dose levels, and the valuable results are determined faster. This is typically done by doubling doses until grade 2 toxicity (moderate toxicity) is observed. Early phases of the trial often involve single-patient cohorts, allowing for dose escalation without waiting for responses from multiple patients. Once any Dose-Limiting Toxicity (DLT) is observed, the design switches to a more traditional approach with larger patient cohorts and smaller dose increments (often ~40% increases). Special provisions are typically included to reduce patient risk, such as de-escalation options if the initial dose escalations prove too aggressive. Generally used to speed up the identification of the MTD, particularly useful when there is prior knowledge suggesting high toxicity of the compound.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Continuous Reassessment Method (CRM)&lt;/strong&gt;:
Another approach for efficient determination of MTD in oncology trials. CRM relies on statistical models to estimate the relationship between dose levels and the probability of DLT, and continuously updates the probability based on the collected data as the trial progresses. CRM typically requires fewer subjects, can adapt to new data in real-time, making it highly responsive to patient outcomes. However, the effectiveness of CRM is heavily reliant on the fitness of the model and the accuracy of the initial dose-toxicity estimates. Poor estimates can lead to an inefficient dose exploration.
Used to more accurately estimate the MTD with fewer patients than the traditional 3+3 design.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Adaptive Design&lt;/strong&gt;:
Allows for modifications to the trial procedures (like dosages, cohort sizes, patient selection criteria, etc.) based on interim results. This design can incorporate various statistical strategies (often relying on Bayesian methods), to adaptively find the optimal dose. To ensure the integrity of a trial, all potential changes to the trial must be planned &lt;em&gt;a priori&lt;/em&gt; and specified in the protocol before the trial begins. This pre-planning helps to prevent biases that often occur with post hoc decisions. The interim analyses are conducted at predefined points in the trial, and help to inform decisions about trial modifications. Adaptive designs often feature combined phases, such as Phase I/II or Phase II/III, to reduce the downtime and gain in efficiency. On the downside, the exhaustive planning and analysis of adaptive trials tends to make them more complex and costly than traditional trials, and requires sophisticated statistical expertise. Used to make the trial more flexible and efficient by using emerging data to inform ongoing decisions.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;phase-ii-designs&#34;&gt;Phase II Designs&lt;/h2&gt;
&lt;p&gt;Phase II clinical trials are designed to evaluate the efficacy of a drug or treatment and continue the safety assessment from Phase I. These trials often involve patients who have the disease or condition that the drug is intended to treat. Various designs can be utilized in Phase II to achieve the necessary balance between understanding the treatment effect and ensuring patient safety. Here are some of the most popular Phase II trial designs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Single-Arm Trials&lt;/strong&gt;:
All participants receive the same treatment. Often used when historical control data are available or when it&amp;rsquo;s difficult to recruit participants for a control group due to the severity or rarity of the condition. Advantages include the simplicity and faster setup; disadvantages include the lack of a control group, which can make it difficult to discern whether observed effects are due to the treatment or other factors.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Randomized Controlled Trials (RCTs)&lt;/strong&gt;:
Participants are randomly assigned to either the treatment group or a control group, which may receive a placebo or a standard treatment. Used to provide a clear comparison between the new treatment and an existing standard or placebo. Advantages include the higher scientific rigor and reduced bias; disadvantages include the requirement for blinding, which makes them more complex and costly to administer.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Crossover Trials&lt;/strong&gt;:
Each participant receives both the treatment and placebo at different times, separated by a washout period. Used to allow each participant to serve as their own control, improving the efficiency of the trial by reducing the variability between subjects. This design encourages the effective use of patient data and reduced sample size requirements. However, it is not suitable for treatments with long-lasting effects or diseases with variable natural histories.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Factorial Design&lt;/strong&gt;:
Participants are randomized into a trial that tests more than one intervention simultaneously. Used to study the effects of each treatment, as well as any interaction between them. Allow efficient assessment of multiple treatments, but the has high complexity in analysis and potential for higher participant burden.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Adaptive Design&lt;/strong&gt;:
Same as in Phase I, the trial design allows for modifications based on interim data. These changes might involve stopping for futility, adjusting dose levels, or reallocating patients to different arms.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;phase-iii-designs&#34;&gt;Phase III Designs&lt;/h2&gt;
&lt;p&gt;The pivotal phase III trials are usually large-scale, involving several hundred to several thousand participants, and are often the final step before a drug seeks regulatory approval for market entry. Many designs used in earlier phases are also used in phase III, including RCTs, Crossover, Factorial, and Adaptive designs. Some of the more phase III-specific designs include:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Parallel Group Design&lt;/strong&gt;:
Participants are randomly divided into two or more groups, each receiving a different treatment or the control (which could be a placebo or a standard treatment). Proper group randomization plays an especially important role in reducing bias and confounding variables. Participants remain in their assigned groups throughout the study. Importantly, each group receives the designated treatments during the same time period. This design is used to evaluate the effect of the treatment over time compared with a control or another treatment. Allows for simplicity in understanding and analyzing outcomes, but usually requires a larger sample size to ensure power to detect differences.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Equivalence Trials (or Non-inferiority)&lt;/strong&gt;:
These trials are designed to show that the new treatment is not worse than the comparator by more than a pre-specified margin (non-inferiority) or that it is statistically equivalent (equivalence). Often used when the new treatment is expected to have similar efficacy but other benefits (e.g., fewer side effects, lower cost). They are suitable for niche situations where surpassing the standard treatment in terms of efficacy is unlikely or unnecessary. Usually involves complex statistical analysis and interpretation.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;phase-iv-designs&#34;&gt;Phase IV Designs&lt;/h2&gt;
&lt;p&gt;Phase IV post-marketing surveillance trials are conducted after a drug has been approved for use by regulatory authorities and marketed to the public. These studies are designed to monitor the drug&amp;rsquo;s performance in real-world settings, assess long-term effects, and detect any rare or long-term adverse events that might not have been apparent in the earlier phases of clinical trials. Here’s an overview of some common Phase IV trial designs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Observational Studies&lt;/strong&gt;: In these studies, patients are observed under normal clinical practice conditions without any experimental intervention by the researchers. Typically involving large populations, observational studies are used to gather data on how a drug performs in a diverse patient population over a longer period, helping to assess effectiveness and safety in a real-world setting.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Registry Studies&lt;/strong&gt;: These studies collect data about patients who have a particular disease or condition, or who are receiving a particular treatment. Registry studies aim to understand the long-term impact of a treatment and to evaluate factors influencing the effectiveness and safety in various subgroups by tracking outcomes and identifying patterns over time.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Case-Control Studies&lt;/strong&gt;: These retrospective studies compare patients who have experienced a specific outcome (such as an adverse effect) to those who have not, to identify potential causes or contributing factors. They are particularly useful for analyzing rare adverse effects or outcomes that may be related to the drug usage.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cohort Studies&lt;/strong&gt;: A cohort of individuals is followed prospectively, and data on various outcomes are collected. Outcomes of cohorts using the drug are compared to those not using the drug over time, studying multiple outcomes, including efficacy and adverse reactions, and understanding how effectively the drug works in different population segments.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cross-Sectional Studies&lt;/strong&gt;: These studies collect data at a single point in time from a population using the drug. This snapshot helps to assess the current status and health outcomes of the population, quickly gathering data on how a drug is being used and its effects in the general population.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pharmacovigilance Studies&lt;/strong&gt;: These studies involve continuous monitoring for adverse events in drugs that are already on the market, often as a regulatory requirement. They involve collecting and analyzing reports of adverse effects to ensure ongoing evaluation of the drug’s risk-benefit balance and to detect any issues that occur when the drug is used widely.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Clinical trials play an indispensable role in the development and approval of new medical treatments, progressing through carefully structured phases that focus on safety, efficacy, and long-term effects. From the initial explorations in Phase 0 to the rigorous testing of Phase III, and the ongoing surveillance of Phase IV, each phase is designed to address specific research questions using various trial designs tailored to achieve these goals. As we&amp;rsquo;ve explored, the selection of trial designs is critical in maximizing the effectiveness and safety of new treatments. Understanding these designs not only aids in the proper execution of trials but also ensures that the results are reliable and meaningful, ultimately leading to better and safer healthcare solutions for patients worldwide.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Understanding Clinical Endpoints</title>
      <link>http://notes.mirzo.net/post/clinical-endpoints/</link>
      <pubDate>Wed, 17 Mar 2021 16:57:41 -0500</pubDate>
      
      <guid>http://notes.mirzo.net/post/clinical-endpoints/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/clinical-endpoints/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/clinical-endpoints_hu0e4614b522e7a837248b6d5c2aa2e1a5_1193337_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Understanding Clinical Endpoints&#34; title=&#34;Understanding Clinical Endpoints&#34;&gt;&lt;/a&gt;


&lt;p&gt;Anyone involved in clinical trials would be familiar with the term &amp;ldquo;endpoint&amp;rdquo;, but it is surprising how many clinical professionals cannot clearly define what the term means or what differentiates an endpoint from an objective. This note is intended to bring some clarity on the matter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Clinical Trial Objectives:&lt;/strong&gt;
The objectives of a clinical trial define its purpose and overall aims, and encapsulate the specific hypotheses that are being tested. Well formulated objectives define the success and failure criteria of the trial in statistical terms (i.e., in terms of frequency of observed outcomes). In other words, objectives take a zoomed-out view of a trial and its subjects, and consider their outcomes in an integrated manner.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Clinical Trial Endpoints:&lt;/strong&gt;
Clinical endpoints are specific, measurable outcomes used to evaluate the &lt;em&gt;benefits&lt;/em&gt; (or lack of thereof) of an intervention for an individual subject. An endpoint is always related to a clinical trial objective, but it is chosen based on it&amp;rsquo;s ability to provide meaningful data about the intervention&amp;rsquo;s impact for an individual participant. Importantly, a well defined endpoint also specifies the terms under which an individual subject exits or withdraws from the trial. Upon reaching the endpoint, a patient is commonly excluded from any additional experimental interventions, giving rise to the term &amp;ldquo;endpoint.&amp;rdquo; Endpoints take a zoomed-in view of the intervention&amp;rsquo;s effects for each subject.&lt;/p&gt;
&lt;h2 id=&#34;endpoint-levels&#34;&gt;Endpoint Levels&lt;/h2&gt;
&lt;h3 id=&#34;primary-endpoints&#34;&gt;Primary Endpoints&lt;/h3&gt;
&lt;p&gt;Primary endpoints represent the key outcomes that directly address the primary research questions. They directly determine the trial&amp;rsquo;s success or failure, and the results often influence regulatory decisions regarding an intervention approval. As such, the study is &lt;strong&gt;powered&lt;/strong&gt; based on the primary endpoints, meaning the sample size is calculated to detect a meaningful difference in these specific outcomes.&lt;/p&gt;
&lt;p&gt;For example, in a cancer treatment trial, the primary endpoint might be the overall survival, measuring the time from treatment initiation to the patient&amp;rsquo;s death. Other common primary endpoints include progression-free survival, disease response rates, or a specific clinical event such as a heart attack in cardiovascular trials.&lt;/p&gt;
&lt;h3 id=&#34;secondary-endpoints&#34;&gt;Secondary Endpoints&lt;/h3&gt;
&lt;p&gt;Secondary endpoints provide additional valuable information about the intervention being tested. These outcomes may include side effects, quality of life improvements, or other clinically relevant measures. Secondary endpoints contribute to a more comprehensive understanding of the treatment&amp;rsquo;s impact and can help researchers explore unexpected benefits or risks. Secondary endpoints, being more exploratory in nature, are deemed to be more tolerant to Type I statistical errors, meaning that the trials are not specifically powered with them in mind. However, the statistical treatment of secondary endpoint data must be pre-specified.&lt;/p&gt;
&lt;p&gt;In a diabetes trial, the primary endpoint might be the reduction in blood sugar levels, while secondary endpoints could encompass improvements in patient-reported outcomes such as daily functioning or the occurrence of diabetic complications.&lt;/p&gt;
&lt;h3 id=&#34;no-such-thing-as-exploratory-endpoints&#34;&gt;(No such thing as) Exploratory Endpoints&lt;/h3&gt;
&lt;p&gt;Exploratory objectives of a clinical trial are not statistically pre-specified and are often investigated to generate additional hypotheses or gain additional insights into the treatment&amp;rsquo;s effects. As exploratory objectives usually do not specify conditions for subject withdrawal from a trial, it is not appropriate to refer to them as endpoints, though in reality it happens all the time.&lt;/p&gt;
&lt;p&gt;For instance, in a psychiatric medication trial, exploratory endpoints could include changes in cognitive function, mood, or sleep patterns, providing a more comprehensive view of the drug&amp;rsquo;s impact.&lt;/p&gt;
&lt;h3 id=&#34;surrogate-endpoints&#34;&gt;Surrogate Endpoints&lt;/h3&gt;
&lt;p&gt;Surrogate endpoints are markers or measurements used as substitutes for clinical outcomes that may be take longer or more difficult to observe directly. These surrogate markers are assumed to reflect the treatment&amp;rsquo;s impact on the actual clinical endpoint. While they allow for faster trial completion, the use of surrogate endpoints requires validation to ensure their reliability as predictors of true clinical benefit. A classic example is the use of blood pressure reduction as a surrogate endpoint in hypertension trials. While the ultimate goal is to prevent cardiovascular events, blood pressure reduction serves as a more immediate and easily measurable indicator.&lt;/p&gt;
&lt;p&gt;Defining proper endpoints is crucial for the success and integrity of clinical trials. It ensures that the study is designed to answer specific research questions, that the outcomes are relevant and measurable, and that the results have both scientific and clinical significance. The careful selection and definition of endpoints contribute to the overall success of the trial and its potential impact on patient care and public health.&lt;/p&gt;</description>
      
    </item>
    
    <item>
      <title>Artificial Neural Networks: Architectures</title>
      <link>http://notes.mirzo.net/post/ann-architectures/</link>
      <pubDate>Wed, 15 Jan 2020 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/ann-architectures/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/ann-architectures/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/ann-architectures1_hu12afac2c9989a9bacf363483024d0551_1628608_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Artificial Neural Networks: Architectures&#34; title=&#34;Artificial Neural Networks: Architectures&#34;&gt;&lt;/a&gt;


&lt;p&gt;Not all Artificial Neural Networks (ANN) are made the same, each is designed to tackle a specific type of problem. In this note, we&amp;rsquo;ll explore the most prominent architectures in detail, and learn how they are used in different applications.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Architectures&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What about Transformers?&lt;/strong&gt;
At the time of writing of this note, the Transformer architecture has not taken over the ANN world yet. Instead of updating this note with new information, we look at Transformers in a lot more detail in a &lt;a href=&#34;http://notes.mirzo.net/post/ann-transformers/&#34;&gt;subsequent note&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;feedforward-neural-networks-fnns&#34;&gt;Feedforward Neural Networks (FNNs)&lt;/h2&gt;
&lt;p&gt;In FNNs, the information moves only in the forward direction, from the inputs to the output layer. This makes FNNs the simplest ANN architecture. There is no cycling or looping back of information, which simplifies the learning process. We had an in-depth look at FNN structure in our discussion of the &lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/&#34;&gt;ANN Basics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;FNNs are commonly used in cases where the output is expected from a set of inputs, such as image classification, customer classification for marketing, and more. A typical application is in handwriting recognition, where the input layer receives pixel data, and the output layer classifies the data into various categories like letters or numbers.&lt;/p&gt;
&lt;h2 id=&#34;convolutional-neural-networks-cnns&#34;&gt;Convolutional Neural Networks (CNNs)&lt;/h2&gt;
&lt;p&gt;CNNs are particularly tailored for grid-like data interpretation. They employ layers of convolutions whereby filters are applied to the original data to create feature maps that summarize key features of the data.&lt;/p&gt;
&lt;p&gt;The filters, also known as kernels, are essentially small weight matrices that CNNs learn during the training process. These filters are applied to the input data through a mathematical operation called convolution, which essentially measures the similarity between the filter and local regions of the input data.&lt;/p&gt;

&lt;link rel=&#34;stylesheet&#34; href=&#34;http://notes.mirzo.net/css/hugo-easy-gallery.css&#34; /&gt;
&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;2D_Convolution_Animation.gif&#34; alt=&#34;Convolution using a 2D filter. Animation by Michael Plotke, CC BY-SA 3.0&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;https://creativecommons.org/licenses/by-sa/3.0/&#34; target=&#34;_blank&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;Convolution using a 2D filter. Animation by Michael Plotke, CC BY-SA 3.0&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;During convolution, a filter is slid across the input image. At each position, the filter is element-wise multiplied by the values of the image it covers. The results of these multiplications are summed up to produce a single number in the output feature map for that filter at that location. The output of applying a filter across the entire input is called a feature map or activation map. This feature map represents specific features of the input data, detected by the filter. Different filters can detect edges, textures, colors, or other visual elements depending on what the filter has learned during training.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Nerd Moment: Convolution or Cross-correlation?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In &lt;a href=&#34;https://en.wikipedia.org/wiki/Convolution&#34;&gt;mathematical terms&lt;/a&gt;, the convolution operation performed on two functions involves the flipping (reflection) of one of the functions by a change of sign for the inputs. This operation allows the convolution operation to be commutative. In most CNNs, this reflection is not applied, which technically means that CNNs don&amp;rsquo;t really use the convolution operation, but instead rely on a similar operation called &lt;strong&gt;cross-correlation&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Initially, the values in the filters are set randomly. However, through backpropagation training, these values are adjusted to help the network minimize the error in its predictions. As the network trains, filters automatically learn to capture relevant features that are most useful for performing the task at hand, whether it&amp;rsquo;s recognizing faces, identifying objects, or classifying scenes.&lt;/p&gt;
&lt;p&gt;In the initial layers of a CNN, filters typically learn to detect low-level features such as edges, gradients, and simple textures. As we progress deeper into the network, the filters start detecting more complex features such as parts of objects (e.g., wheels, eyes, leaves). In the deeper layers, filters combine the earlier detected features to recognize high-level, more abstract features like entire objects or complex scenes.&lt;/p&gt;
&lt;p&gt;In addition to image and video recognition, CNNs are also used in natural language processing (e.g., sentence classification) and other areas requiring recognition of patterns in spatial data.&lt;/p&gt;
&lt;h2 id=&#34;recurrent-neural-networks-rnns&#34;&gt;Recurrent Neural Networks (RNNs)&lt;/h2&gt;
&lt;p&gt;RNNs introduce loops in the network architecture, allowing information to persist. This structure is crucial for handling sequential data of arbitrary size, where the current output depends not just on the current input but also on the previous ones. In essence, their architecture allows them to maintain a form of memory by using their output or state as input for the next step in the sequence. This makes RNNs particularly well-suited for tasks where the sequence of data points is important, such as time series prediction, natural language processing, speech recognition, and music processing. RNNs (in their true form) are essentially directed cyclic graphs.&lt;/p&gt;
&lt;p&gt;The basic unit within an RNN layer is a cell. The simplest form of an RNN cell has a structure similar to a standard neural network layer, plus a connection from the output of the cell back to its input.&lt;/p&gt;
&lt;p&gt;At each time step \( t \), the RNN takes two inputs: the current data point \( x_t \) from the sequence and the hidden state from the previous time step \( h_{t-1} \). The hidden state \( h_{t-1} \) serves as the &amp;ldquo;memory&amp;rdquo; of the network, containing information about the previous elements of the sequence. The cell uses these inputs to calculate the new hidden state \( h_t \), which will be used in the next time step and can also be used to generate an output \( y_t \).&lt;/p&gt;
&lt;p&gt;The hidden state \( h_t \) is updated using a non-linear function \( f \), often a tanh or sigmoid function, which combines the input \( x_t \) and the previous hidden state \( h_{t-1} \) as follows:&lt;/p&gt;
&lt;p&gt;$$
h_t = f(W_{hh} h_{t-1} + W_{xh} x_t + b_h)
$$&lt;/p&gt;
&lt;p&gt;Here, \( W_{hh} \) and \( W_{xh} \) are weights applied to the previous hidden state and the current input, respectively, and \( b_h \) is a bias term.&lt;/p&gt;
&lt;p&gt;The output \( y_t \) at each time step can be calculated using the hidden state \( h_t \), often with another function \( g \), typically a softmax for classification tasks:&lt;/p&gt;
&lt;p&gt;$$
y_t = g(W_{hy} h_t + b_y)
$$&lt;/p&gt;
&lt;p&gt;where \( W_{hy} \) is the weight matrix for the output, and \( b_y \) is the output bias.&lt;/p&gt;
&lt;p&gt;Despite their effectiveness for sequential data, RNNs face significant challenges:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vanishing Gradient Problem:&lt;/strong&gt; As gradients are propagated backward through each time step in the sequence, they can become extremely small (vanish), making it difficult to learn long-range dependencies within the sequence data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exploding Gradients:&lt;/strong&gt; Conversely, gradients can also grow exponentially, leading to very large updates to model weights and resulting in an unstable network.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To address these issues, several variants of RNNs have been developed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Long Short-Term Memory (LSTM):&lt;/strong&gt; LSTMs include mechanisms called gates that control the flow of information. These gates help the network retain or forget information, effectively addressing the vanishing gradient problem.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gated Recurrent Unit (GRU):&lt;/strong&gt; GRUs are a simpler alternative to LSTMs that use a similar gating concept but with fewer parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;RNNs excel in language modeling, speech recognition, and any other application that requires the interpretation of sequential data. In text generation, RNNs can predict the next character or word in a sequence based on the previous characters or words.&lt;/p&gt;
&lt;h2 id=&#34;autoencoders&#34;&gt;Autoencoders&lt;/h2&gt;
&lt;p&gt;Comprising an encoder and a decoder, autoencoders first compress the input into a latent-space representation and then reconstruct the output from this representation. The goal is typically to learn a compressed representation of the data. Typically used in anomaly detection, noise reduction, and data denoising, where the network learns to ignore the noise and reconstruct the original input. Training of autoencoders essentially involves unsupervised representation learning. They are designed to learn efficient codings of unlabeled data, often for the purposes of dimensionality reduction or feature extraction.&lt;/p&gt;
&lt;p&gt;An autoencoder consists of two main parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Encoder:&lt;/strong&gt; This part of the network compresses the input into a smaller, encoded representation. The encoder is typically formed by a series of layers that gradually decrease in size, funneling the input data into a more compact form.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Decoder:&lt;/strong&gt; This part of the network attempts to reconstruct the input data from the encoded representation. The decoder mirrors the encoder, with layers that gradually increase in size, culminating in an output layer that matches the dimensions of the input data.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Autoencoders are trained through a process that involves optimizing the parameters of the encoder and decoder so that the output (reconstructed input) closely matches the original input. During the forward pass, an input batch of data is passed through the encoder, which compresses it into a smaller, dense representation (often called the &amp;ldquo;latent space&amp;rdquo; or &amp;ldquo;encoding&amp;rdquo;). This encoding is then fed into the decoder, which attempts to reconstruct the original input from this compressed form. After the forward pass, the reconstruction is compared to the original input to calculate the loss, which quantifies the difference between the two. The loss calculated from the output is then used to perform backpropagation.&lt;/p&gt;
&lt;p&gt;There are various types of autoencoders:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Standard Autoencoders:&lt;/strong&gt; Focus on minimizing the reconstruction error between the input and the output.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variational Autoencoders (VAEs):&lt;/strong&gt; These introduce a probabilistic approach to the encoding process, producing a distribution over the possible inputs. They are used extensively in generative models.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Denoising Autoencoders:&lt;/strong&gt; Specifically designed to remove noise from the input during the encoding process, thus learning to recover the original undistorted input.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sparse Autoencoders:&lt;/strong&gt; Incorporate a sparsity constraint on the hidden layers to induce a lower capacity representation, thereby learning more robust features of the data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Autoencoders are used in a variety of applications:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dimensionality Reduction:&lt;/strong&gt; Similar to PCA, autoencoders can reduce the dimensionality of data, making it easier to visualize or process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Feature Learning:&lt;/strong&gt; Can automatically learn the features to be used for further machine learning or data analysis tasks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Anomaly Detection:&lt;/strong&gt; By learning to reproduce only the common patterns in the data, autoencoders can be used to detect outliers or anomalous data points.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Image Reconstruction and Denoising:&lt;/strong&gt; Useful in image processing tasks to enhance the quality of images or remove noise.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;generative-adversarial-networks-gans&#34;&gt;Generative Adversarial Networks (GANs)&lt;/h2&gt;
&lt;p&gt;A GAN consists of two competing neural network models. A generator creates samples intended to come from the same distribution as the training data, and a discriminator evaluates them against the real data, learning to differentiate between the two. GANs use two neural networks, pitting one against the other (thus the &amp;ldquo;adversarial&amp;rdquo;) to generate new, synthetic instances of data that can pass for real data. They are widely used in applications such as image generation, video generation, and more sophisticated machine learning tasks where synthesizing new data is beneficial.&lt;/p&gt;
&lt;p&gt;GANs consist of two distinct models, each with its own role:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Generator:&lt;/strong&gt; This model learns to generate plausible data. The generated instances become more realistic over the course of training. The generator starts with a random noise vector (seed) and transforms this seed into data with the same dimensions as the training set.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Discriminator:&lt;/strong&gt; This model is a classifier that learns to distinguish between real and synthetic data. The goal of the discriminator is to accurately predict whether a given data instance is real (from the training set) or fake (created by the generator).&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The training of GANs involves an iterative, adversarial process where the generator and the discriminator compete against each other. It is a delicate process that involves balancing the performance of the two models. Here’s how the process generally works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The discriminator is trained for a set number of iterations first. This training involves presenting it with samples from the training data (real) and samples of faked data.&lt;/li&gt;
&lt;li&gt;The discriminator&amp;rsquo;s job is to output the probability that the received data is real. Training consists of maximizing the probability of correctly labeling both real and fake data.&lt;/li&gt;
&lt;li&gt;The generator is trained to produce data that the discriminator will classify as real. This is typically done by using the gradients of the discriminator&amp;rsquo;s predictions to update the generator’s weights.&lt;/li&gt;
&lt;li&gt;Essentially, the generator is rewarded for fooling the discriminator.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Common challenges in training GANs include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Mode Collapse:&lt;/strong&gt; Sometimes, the generator finds and exploits weaknesses in the discriminator’s strategy, leading to the generator producing limited varieties of outputs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Failure to Converge:&lt;/strong&gt; The adversarial nature can sometimes lead to training stability issues, where neither model reaches a point where it can no longer improve because any change would lessen its performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vanishing Gradients:&lt;/strong&gt; This can occur when the discriminator gets too good, which results in the generator gradient vanishing and thus no longer updating effectively.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;GANs are widely used in image generation, video game content creation, fashion and design, drug discovery, and even in enhancing space exploration images by generating high-resolution versions of pixelated images.&lt;/p&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap-up&lt;/h2&gt;
&lt;p&gt;Each of these architectures leverages unique properties suited to specific tasks, from processing individual data points in isolation to handling inputs where context and order significantly impact the output. As the field of AI progresses, the development of these and newer architectures will continue to be central to overcoming the challenges posed by complex data and diverse applications&amp;hellip; unless Transformers get invented and make most of these architectures obsolete.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-basics/&#39;&gt;The Basics&lt;/a&gt;&lt;/li&gt;
					
				
					
						&lt;li&gt;&lt;strong&gt;Architectures&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
    <item>
      <title>Artificial Neural Networks: The Basics</title>
      <link>http://notes.mirzo.net/post/ann-basics/</link>
      <pubDate>Sat, 28 Dec 2019 00:00:00 +0000</pubDate>
      
      <guid>http://notes.mirzo.net/post/ann-basics/</guid>
      
        <description>&lt;a href=&#34;http://notes.mirzo.net/post/ann-basics/&#34;&gt;&lt;img src=&#34;http://notes.mirzo.net/images/neural-networks1_hu2eaa3e4bff78e65b690d98660e74d4d2_1837627_700x0_resize_box_3.png
&#34; alt=&#34;Header image for the post titled Artificial Neural Networks: The Basics&#34; title=&#34;Artificial Neural Networks: The Basics&#34;&gt;&lt;/a&gt;


&lt;p&gt;Artificial Neural Networks (ANNs) exist in a fascinating intersection of neuroscience and machine learning, giving rise to powerful systems that can emulate intelligent behaviors. This note covers the essential glossary and theory behind this powerful technology.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
						&lt;li&gt;&lt;strong&gt;The Basics&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;
	


&lt;h2 id=&#34;a-network-of-neurons&#34;&gt;A Network of Neurons&lt;/h2&gt;
&lt;pre class=&#34;mermaid&#34;&gt;flowchart LR
sigma((Σ + bias))
x1((Input&lt;sub&gt;1&lt;/sub&gt;)) -- Weight&lt;sub&gt;1&lt;/sub&gt; --&gt; sigma
x2((Input&lt;sub&gt;2&lt;/sub&gt;)) -- Weight&lt;sub&gt;2&lt;/sub&gt; --&gt; sigma
xn((Input&lt;sub&gt;n&lt;/sub&gt;)) -- Weight&lt;sub&gt;n&lt;/sub&gt; --&gt; sigma
sigma --&gt; activation((Activation\nFunction))--&gt;output((Output))
&lt;/pre&gt;

&lt;p&gt;The basic building block of an ANN is the neuron. Just like in biology, an artificial neuron consists of multiple input nodes and a single output node. Neurons are connected in multi-layer networks, where an output from one neuron becomes an input to the next. The layers that lie in between the &lt;strong&gt;input layer&lt;/strong&gt; and the &lt;strong&gt;output layer&lt;/strong&gt; are referred to as the &lt;strong&gt;hidden layers&lt;/strong&gt;, and a network that has more than a single hidden layer is referred to as a &lt;strong&gt;deep network&lt;/strong&gt;. The network can be arranged in many different architectures, depending on the intended application. In this note, we will focus on a basic feed-forward architecture, commonly referred to as a &lt;strong&gt;multilayer perceptron (MLP)&lt;/strong&gt;. We will expand the concepts to other architectures in a future note.&lt;/p&gt;
&lt;p&gt;Each connection between a neuron and its inputs carries a &lt;strong&gt;weight&lt;/strong&gt;. When a neuron operates in a &lt;strong&gt;forward&lt;/strong&gt; direction, i.e., to generate a prediction, the input values in each neuron are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;multiplied by their weights&lt;/li&gt;
&lt;li&gt;the products are summed-up&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;bias&lt;/strong&gt; term is added.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This &lt;strong&gt;weighted sum&lt;/strong&gt; of inputs (denoted by &lt;em&gt;z&lt;/em&gt;) is then passed through a non-linear &lt;strong&gt;activation function&lt;/strong&gt;. The resulting final value of a neuron is called it&amp;rsquo;s &lt;strong&gt;activation&lt;/strong&gt; (in many sources the usage of this term also includes the 0th layer inputs). It can be shown that the combination of linear weighted sum equations and the non-linear activation function allows ANNs to act as universal function approximations.&lt;/p&gt;
&lt;p&gt;Mathematically, a layer of neurons can be represented as:
$$
\vec{a}^{l+1} = \sigma(z^{l}) = \sigma(\matrix{W} \vec{a}^{l}+\vec{b}^l)
\tag{1.1}
\label{neuron}
$$&lt;/p&gt;
&lt;p&gt;where &lt;em&gt;l&lt;/em&gt; is the layer number, &lt;em&gt;a&lt;/em&gt; are the activations of a given layer, &lt;em&gt;W&lt;/em&gt; is the weights matrix, &lt;em&gt;b&lt;/em&gt; is the vector of bias terms, and \( \sigma\) is an activation function. See &lt;a href=&#34;http://notes.mirzo.net/post/linear-algebra-basics/&#34;&gt;here&lt;/a&gt; for a quick refresher on linear algebra.&lt;/p&gt;
&lt;p&gt;On occasion, we will need to zoom in on to a specific neuron, so here is the same equation that is stated in terms of the indeces of the &lt;em&gt;j&lt;/em&gt;&amp;rsquo;th neuron in layer &lt;em&gt;l&lt;/em&gt;:
$$
a^{l+1}_j = \sigma (\sum w_{jk}^l a_k^{l} + b_j^l)
\tag{1.2}
\label{neuron2}
$$
where &lt;em&gt;k&lt;/em&gt; is the index of a given input. As an additional note, in our notation the capital L will be used to represent the total number of layers, and when used as an index, will refer to the output layer.&lt;/p&gt;
&lt;p&gt;The weights and biases are the &lt;strong&gt;parameters&lt;/strong&gt; of an ANN model that get adjusted through &lt;strong&gt;training&lt;/strong&gt;. Training brings the weights to represent an input state that most corresponds to a specific output. Prior to training, the parameters are randomly initialized, and are then algorithmically adjusted to improve the model&amp;rsquo;s fit of the data.&lt;/p&gt;
&lt;p&gt;For simplicity, the bias parameters can be treated as weights with a constant activation term of 1. Thus, it is common that both types of parameters are implicitly referred to as weights.&lt;/p&gt;
&lt;p&gt;In most ANN architectures, the same set of inputs is fed to the many neurons contained in the next layer. This means that each neuron in a given layer can have a different weight assigned to the same input from the previous layer. In a sense, the weight reflects how &amp;ldquo;important&amp;rdquo; an input is to a specific neuron. By prioritizing specific sets of inputs, a neuron essentially picks out and focuses on a specific &lt;strong&gt;feature&lt;/strong&gt; of the inputs. Successive layers are able to recognize more complex features, since they aggregate and recombine features from past layers, a phenomenon referred to as the &lt;strong&gt;feature hierarchy&lt;/strong&gt;. Through this buildup of feature complexity, ANNs are able to perform &lt;strong&gt;automatic feature extraction&lt;/strong&gt; and discover complex &lt;strong&gt;latent structures&lt;/strong&gt; in data. ANNs are most commonly designed to solve classification  problems, but there are designs that allow regression-like value prediction as well.&lt;/p&gt;
&lt;h3 id=&#34;activation-functions&#34;&gt;Activation functions&lt;/h3&gt;
&lt;p&gt;An activation function is a non-linear function that remaps &lt;em&gt;z&lt;/em&gt; on to a more meaningful or operable scale, often between 0 and 1. The sigmoid is a popular activation function used in the hidden layers of simpler networks:&lt;/p&gt;
&lt;p&gt;$$
\sigma(z)=\frac{e^z}{1+e^z}
\tag{2}
\label{sigmoid}
$$&lt;/p&gt;

&lt;link rel=&#34;stylesheet&#34; href=&#34;http://notes.mirzo.net/css/hugo-easy-gallery.css&#34; /&gt;
&lt;div class=&#34;box&#34;&gt;
&lt;figure  itemprop=&#34;associatedMedia&#34;
  itemscope itemtype=&#34;http://schema.org/ImageObject&#34; &gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;sigmoid.png&#34; alt=&#34;An example of the sigmoid function&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;sigmoid.png&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
      &lt;figcaption&gt;
          &lt;p&gt;An example of the sigmoid function&lt;/p&gt;
      &lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The sigmoid function is a poster child of non-linear functions, but it presents an issue when training larger ANNs. When a specific weighted sum input (&lt;em&gt;z&lt;/em&gt;) results in an activation output that is embedded far in one of the two plateau ranges of the function (i.e., when input/output of the function is far from the sigmoid inflection point), &amp;ldquo;getting out&amp;rdquo; of this plateau using an step-wise numerical optimization algorithm can be very slow. This is often referred to as &lt;strong&gt;neuron saturation&lt;/strong&gt;, a detrimental behavior where changes in the input data do not lead to corresponding changes in the output, reducing the network&amp;rsquo;s sensitivity to variations in the data.&lt;/p&gt;
&lt;p&gt;Other activation functions that are frequently used for hidden layers include &lt;a href=&#34;https://en.wikipedia.org/wiki/Hyperbolic_function&#34;&gt;tanh&lt;/a&gt; and &lt;a href=&#34;https://en.wikipedia.org/wiki/Rectifier_(neural_networks)&#34;&gt;ReLU&lt;/a&gt; (Rectified Linear Unit), with the later one being a very popular choice for deep networks.&lt;/p&gt;
&lt;p&gt;The output layer often uses different activation functions. &lt;strong&gt;Softmax&lt;/strong&gt; is a function that is often applied for multi-class classification. It is essentially a sigmoid function that acts on a vector of &lt;em&gt;z&lt;/em&gt; values and returns a vector of &amp;ldquo;probabilities&amp;rdquo;, where all values add up to 1.&lt;/p&gt;
&lt;p&gt;$$
softmax(z)_i = \frac{e^{z_i}}{\sum_{j=1}^Ne^{z_j}}
\tag{3}
\label{softmax}
$$&lt;/p&gt;
&lt;p&gt;where &lt;em&gt;N&lt;/em&gt; is the number of outputs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Argmax&lt;/strong&gt; is another specialized function, which aims to &amp;ldquo;collapse&amp;rdquo; a probability distribution created by a function like softmax. It acts by setting the maximum value of the &lt;em&gt;z&lt;/em&gt; vector to 1, and everything else to 0.&lt;/p&gt;
&lt;p&gt;$$
argmax\left(\begin{bmatrix} 0.1 \\ 0.7 \\ 0.2 \end{bmatrix}\right) = \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix}
\tag{4}
\label{argmax}
$$&lt;/p&gt;
&lt;h2 id=&#34;training&#34;&gt;Training&lt;/h2&gt;
&lt;h3 id=&#34;cost-functions&#34;&gt;Cost functions&lt;/h3&gt;
&lt;p&gt;The progression of learning is measured by the minimization of a cost function (also often referred to as the &lt;strong&gt;loss&lt;/strong&gt; function). The cost function is a layer of complexity above the neural network. A cost function consists of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inputs:&lt;/strong&gt; weights and biases of the network&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameters:&lt;/strong&gt; a set of training examples and the corresponding predictions of the ANN.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; the cost&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A basic cost function is the &lt;strong&gt;mean squared error (MSE)&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;$$
C=\frac{1}{n}\sum_{i=1}^{n}(x_{i}-y_{i})^2
\tag{5}
\label{mse}
$$
where \( x \) is the ANN-predicted output, \( y \) is the corresponding label (or true value) from an &lt;em&gt;i&lt;/em&gt;&amp;rsquo;th training example, and &lt;em&gt;n&lt;/em&gt; in the number of such training pairs. While MSE is good for illustrative/educational purposes, it has significant performance issues, and is rarely used in practice.&lt;/p&gt;
&lt;p&gt;In modern ANN architectures, especially those used for multi-class classification problems, a more common cost function is the &lt;strong&gt;cross-entropy loss&lt;/strong&gt;. This function comes from the field of Information Theory, and (roughly speaking) allows us to calculate differences between two probability distributions. In ANNs, the cross-entropy loss function takes the output vector of probabilities and calculates the distance to the vector of labels from the training set, which is also a probability set except where all of the probability is usually concentrated in a single node.&lt;/p&gt;
&lt;p&gt;Understanding the math behind Cross-entropy requires a solid background in Information Theory, which is out of the scope of this note. The exact form of the cost function depends on the structure of the network (specifically, the output layer). A general form of the equation that is used in the context of ANNs can be represented as:&lt;/p&gt;
&lt;p&gt;$$
C = - \frac{1}{n} \sum_x \sum_j(y_j \ln a^L_j + (1-y_j) \ln (1-a^L_j))
\tag{6}
\label{cross_entropy}
$$
where &lt;em&gt;n&lt;/em&gt; is the number of training examples, consisting of the inputs (&lt;em&gt;x&lt;/em&gt;) and corresponding true values or labels (&lt;em&gt;y&lt;/em&gt;), and &lt;em&gt;a&lt;/em&gt; is the model-derived activation of the output neuron.&lt;/p&gt;
&lt;p&gt;The cross-entropy cost function has a useful property: when calculating it&amp;rsquo;s gradient (the importance of which will be described in the next section), it does not depend on the derivative of the activation function. Thus, it can cancel out any slowdowns in learning caused by &amp;ldquo;plateau traps&amp;rdquo; within activation functions (like a sigmoid).&lt;/p&gt;
&lt;h3 id=&#34;gradient-descent&#34;&gt;Gradient Descent&lt;/h3&gt;
&lt;p&gt;ANNs are routinely built with billions of parameters, and minimizing the large cost functions needed for their training is no easy task. Finding an analytical minimization solution using standard calculus is not feasible in any practical application (it&amp;rsquo;s one of those cases when the analytical solution is more computationally costly than a numerical approach). But it can be achieved using the method of &lt;strong&gt;gradient descent&lt;/strong&gt;, an iterative optimization algorithm used to minimize a function by taking steps in the direction of steepest downward change along the function profile.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;gradient&lt;/strong&gt; is a differentiation operation that takes in a scalar-valued multi-variable function and returns a vector-valued function, i.e. a function that returns a vector at every point. A vector returned at any given point of the original function&amp;rsquo;s multidimensional response hyper-surface will point in the direction of its fastest increase, with the magnitude of the vector proportional to the rate of increase. In other words, the gradient provides information about how the cost function changes as you move through the parameter space. Mathematically, the gradient is a vector containing the partial derivatives of a multivariable function with respect to each of its variables. For a given ANN layer, the gradient is described by:
$$
\nabla C = \begin{bmatrix}
\frac{\partial C}{\partial w_1} \\
\frac{\partial C}{\partial w_2} \\
\vdots \\
\frac{\partial C}{\partial w_n}
\end{bmatrix}
\tag{8}
\label{gradient}
$$&lt;/p&gt;
&lt;p&gt;As such, the gradient relates changes in the ANN parameters (&lt;em&gt;w&lt;/em&gt;) to the corresponding changes in the cost function:
$$
\Delta C \approx \nabla C \cdot \Delta w
\tag{9}
\label{gradient_change}
$$&lt;/p&gt;
&lt;p&gt;Being an iterative method, gradient descent takes small steps toward the minimization of the cost function. The size of this step is defined by a hyper-parameter called the &lt;strong&gt;learning rate&lt;/strong&gt; (\( \eta \)), which controls the convergence speed and stability of the optimization process:
$$
\Delta w = -\eta \nabla C
\tag{10}
\label{learning_rate}
$$
The negative sign in the above equation allows us to move against the gradient, descending toward one of the minima of a function. The learning rate then defines the size of the step in each iteration \( \epsilon \):&lt;/p&gt;
&lt;p&gt;$$
\epsilon = \Vert \Delta w \Vert = \eta \Vert \nabla C \Vert
\tag{11}
\label{step_size}
$$&lt;/p&gt;
&lt;p&gt;Gradient descent optimization usually starts off by setting the parameters of a minimized function (the cost function in our case) to random values. At each iteration, the gradient of the minimized function with respect to the parameters is computed, and the parameters are then updated in the opposite direction of the gradient using the following update rule:&lt;/p&gt;
&lt;p&gt;$$
w^{\prime} = w - \eta \nabla C
\tag{12}
\label{update_rule}
$$&lt;/p&gt;
&lt;p&gt;where \( w^{\prime} \) are the updated parameter values. The update iterations are applied until a stopping criterion is met, such as reaching a maximum number of iterations or observing negligible improvements in the objective function.&lt;/p&gt;
&lt;p&gt;In standard gradient descent, the gradient is calculated for every training example at every step towards the minimum, and the updates to the model parameters are made based on the average gradient. This becomes too costly given the number of parameters and training examples involved in training of ANNs. More commonly, the &lt;strong&gt;Stochastic Gradient Descent (SGD)&lt;/strong&gt; variant of the algorithm is used, where the gradient is estimated using a small sample (a &lt;strong&gt;mini-batch&lt;/strong&gt;) of randomly chosen training examples. On each iteration, a new mini-batch is selected, the estimate of the gradient is calculated and the model parameters are updated much more frequently. This goes on until all of the training data is exhausted. This denotes a training &lt;strong&gt;epoch&lt;/strong&gt;, and training usually continues for multiple epochs, until the stopping criteria are encountered. While using a small subset of training data does not reveal us a true gradient at every iteration, turns out it gives us a pretty decent approximation. As a result, SGD converges to a solution much faster than regular Gradient Descent, and also happens to be less prone to over-fitting due to the inherent noisiness of random sampling.&lt;/p&gt;
&lt;h3 id=&#34;backpropagation&#34;&gt;Backpropagation&lt;/h3&gt;
&lt;p&gt;We&amp;rsquo;ve explained how Gradient Descent uses the gradient to iteratively minimize the cost function, but we have (conveniently) said nothing about how to calculate the gradient in the first place! This is where the backpropagation algorithm comes in. It allows us to calculate the gradient of the cost function relative to the weights and biases of the ANN model in an iterative fashion, moving backwards from the output to the input layers.&lt;/p&gt;
&lt;h4 id=&#34;deriving-backpropagation-equations&#34;&gt;Deriving Backpropagation equations&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s explore the equations behind the algorithm. The equations may seem complex, but in reality it all boils down to the application of the derivative chain rule.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Quick Refresher of the Chain Rule:&lt;/strong&gt;
$$
f(g(x))^\prime = f^\prime (g(x)) g^\prime (x)
\tag{13.1}
\label{chain_rule_prime}
$$
or in Liebniz&amp;rsquo;s notation:
$$
\frac{dz}{dx} = \frac{dz}{dy} \cdot \frac{dy}{dx}
\tag{13.2}
\label{chain_rule_leibniz}
$$&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We will begin by introducing a term \( \delta \) that represents the partial derivative of the cost function with respect to the weighted sum (&lt;em&gt;z&lt;/em&gt;) of the &lt;em&gt;j&lt;/em&gt;&amp;rsquo;th neuron in the &lt;em&gt;l&lt;/em&gt;&amp;rsquo;th layer. This quantity is commonly referred to as an &amp;ldquo;error&amp;rdquo;, and it is a convenient starting point to start our derivation of the gradient equations.&lt;/p&gt;
&lt;p&gt;$$
\delta_j^l = \frac{\partial C}{\partial z_j^l}
\tag{14}
\label{error_z}
$$&lt;/p&gt;
&lt;p&gt;Using the chain rule, we can express the error term relative to the weights in a given layer:&lt;/p&gt;
&lt;p&gt;$$
\delta^l_j = \frac{\partial C}{\partial w^l_{kj}} \frac{\partial w^l_{kj}}{\partial z^l_j}
\tag{15.1}
\label{bp_grad_weights_init}
$$&lt;/p&gt;
&lt;p&gt;We can rearrange equation \( \eqref{neuron2} \) to express it in terms of the individual weights:&lt;/p&gt;
&lt;p&gt;$$
w^l_{kj} = \frac{1}{a^{l-1}_k}(z^l_j - b^l_j)
\tag{15.2}
\label{bp_grad_weights_w_term}
$$&lt;/p&gt;
&lt;p&gt;This, in turn, allows us to calculate the partial derivative of the weights relative to the weighted sum of the neuron:&lt;/p&gt;
&lt;p&gt;$$
\frac{\partial w^l_{kj}}{\partial z^l_j} = \frac{\partial (\frac{1}{a^{l-1}_k}(z^l_j - b^l_j))}{\partial z^l_j} = \frac{1}{a^{l-1}_k}
\tag{15.3}
\label{bp_grad_weights_w_term_dx}
$$&lt;/p&gt;
&lt;p&gt;By substituting the above into \( \eqref{bp_grad_weights_init} \), we can rearrange it to calculate the gradient of the cost relative to the weights:&lt;/p&gt;
&lt;p&gt;$$
\frac{\partial C}{\partial w^l_{kj}} = a^{l-1}_k \delta^l_j
\tag{15.4}
\label{bp_grad_weights_final}
$$&lt;/p&gt;
&lt;p&gt;Deriving the gradient of the cost function relative to the biases follows a similar logic. Again, we express the error term relative to the biases in a given layer using the chain rule:&lt;/p&gt;
&lt;p&gt;$$
\delta^l_j = \frac{\partial C}{\partial b^l_j} \frac{\partial b^l_j}{\partial z^l_j}
\tag{16.1}
\label{bp_grad_biases_init}
$$&lt;/p&gt;
&lt;p&gt;We rearrange equation \( \eqref{neuron2} \) in terms of the biases, and use it to calculate the partial derivative relative to the to the weighted sum of the neuron:&lt;/p&gt;
&lt;p&gt;$$
\frac{\partial b^l_j}{\partial z^l_j} = \frac{\partial (z^l_j - \sum_k w^l_{kj} \sigma (z^{l-1}_k))}{\partial z^l_j} = 1
\tag{16.2}
\label{bp_grad_biases_dx}
$$&lt;/p&gt;
&lt;p&gt;Substituting this and rearranging equation \( \eqref{bp_grad_biases_init} \) gets us:&lt;/p&gt;
&lt;p&gt;$$
\frac{\partial C}{\partial b^l_j} = \delta^l_j
\tag{16.3}
\label{bp_grad_biases_final}
$$&lt;/p&gt;
&lt;p&gt;Great! We can now calculate the gradient relative to the weights and biases! Except, we need to know how to calculate the \( \delta \) term in each layer. Luckily, we can just rinse and repeat the same chain rule approach used above.&lt;/p&gt;
&lt;p&gt;We can start with the output layer L, where the error term essentially represents the individual contribution of each of the output neurons to the cost. We will use the chain rule to represent the error in terms of the activations of the output layer:&lt;/p&gt;
&lt;p&gt;$$
\delta^L_j = \frac{\partial C}{\partial \sigma (z^L_j)} \frac{\partial \sigma (z_j^L)}{\partial z_j^L} = \frac{\partial C}{\partial \sigma (z^L_j)} \sigma^\prime(z_j^L)
\tag{17}
\label{bp_error_output}
$$&lt;/p&gt;
&lt;p&gt;Lastly, let&amp;rsquo;s generalize the equation above to other layers. We will use the chain rule to calculate the error in any given layer relative to the error in the next layer:&lt;/p&gt;
&lt;p&gt;$$
\delta^l_j = \sum_k \frac{\partial C}{\partial z^{l+1}_k} \frac{\partial z^{l+1}_k}{\partial z^l_j} = \sum_k \delta^{l+1}_{k} \frac{\partial z^{l+1}_k}{\partial z^l_j}
\tag{18.1}
\label{bp_error_next_layer_init}
$$&lt;/p&gt;
&lt;p&gt;As before, let&amp;rsquo;s calculate the partial derivative of the weighted sum in the next layer relative to the weighted sum in the current layer:&lt;/p&gt;
&lt;p&gt;$$
\frac{\partial z^{l+1}_k}{\partial z^l_j} = \frac{\partial (\sum_j w_{kj}^{l+1} \sigma(z^l_j) + b^{l+1}_k)}{\partial z^l_j} = w_{kj}^{l+1} \sigma^{\prime}(z^l_j)
\tag{18.2}
\label{bp_error_next_layer_dx}
$$&lt;/p&gt;
&lt;p&gt;And substituting the above back into \( \eqref{bp_error_next_layer_init} \), we get:&lt;/p&gt;
&lt;p&gt;$$
\delta^l_j = \sum_k \delta^{l+1}_{k} w_{kj}^{l+1} \sigma^{\prime}(z^l_j)
\tag{18.3}
\label{bp_error_next_layer_final}
$$&lt;/p&gt;
&lt;h4 id=&#34;backpropagation-steps&#34;&gt;Backpropagation steps&lt;/h4&gt;
&lt;p&gt;Now that we know how to calculate the individual components of the gradient, let&amp;rsquo;s put the whole algorithm into perspective:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A forward pass is performed to generate the outputs&lt;/li&gt;
&lt;li&gt;The cost associated with the outputs is calculated&lt;/li&gt;
&lt;li&gt;The error term in the output layer is calculated using equation \( \eqref{bp_error_output} \)&lt;/li&gt;
&lt;li&gt;Moving backwards, the error term in all other layers is calculated using equation \( \eqref{bp_error_next_layer_final} \)&lt;/li&gt;
&lt;li&gt;For each layer, the gradient of the cost function relative to the weights and biases is calculated using equations \( \eqref{bp_grad_weights_final} \) and \( \eqref{bp_grad_biases_final} \)&lt;/li&gt;
&lt;li&gt;With the gradient calculated, the Gradient Descent equation \( \eqref{update_rule} \) is used to update each of the parameters&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;challenges-in-training&#34;&gt;Challenges in Training&lt;/h3&gt;
&lt;h4 id=&#34;regularization&#34;&gt;Regularization&lt;/h4&gt;
&lt;p&gt;The flexibility of ANN models is both a boon and a detriment. The large number of parameters available to ANN models make them very prone to over-fitting. &lt;strong&gt;Regularization&lt;/strong&gt; is a set of techniques used to limit the degrees of freedom available to the model to safeguard against the tendency to shift from genuine learning to mere memorization of noise.&lt;/p&gt;
&lt;p&gt;Norm-based regularization techniques, like &lt;strong&gt;L1&lt;/strong&gt; and &lt;strong&gt;L2&lt;/strong&gt; regularization, approach the task by trying to reduce the effect of &lt;strong&gt;weight decay&lt;/strong&gt;: an unbounded growth of weight values due to the selection pressures excreted during training. By introducing a new pressure to keep the weight values low we can limit the excessive flexibility of the model. This is achieved by adding a weight based cost term into the overall cost function.&lt;/p&gt;
&lt;p&gt;L1 regularization:
$$
C = X + \frac{\lambda}{n}\sum_w |w|
\tag{7.1}
\label{l1_regularization}
$$&lt;/p&gt;
&lt;p&gt;L2 regularization:
$$
C = X + \frac{\lambda}{2n}\sum_w w^2
\tag{7.2}
\label{l2_regularization}
$$&lt;/p&gt;
&lt;p&gt;where \( \lambda \) is a regularization hyper-parameter, and &lt;em&gt;X&lt;/em&gt; is the non-regularized version of the cost function, e.g., cross-entropy loss from equation \( \eqref{cross_entropy} \). When the gradient of the regularized cost functions is calculated, L1 regularization causes the weights to decrease by a constant amount, whereas L2 regularization causes weights to decrease by a factor proportional to the weight value. As a result, L1 regularization ends up with prioritizing a small number of high-impact weights, whereas the L2 regularization results in a more even spread of weight values.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dropout&lt;/strong&gt; regularization involves randomly deactivating a specified subset of neurons (typically 20% to 50%) in each training mini-batch. By introducing noise and variability into the network training, dropout prevents neurons from relying too heavily on specific features in the training data, encouraging them to learn more robust and generalizable representations. Dropout can be viewed as a form of &lt;strong&gt;ensemble learning&lt;/strong&gt;, where different sub-networks (formed by dropping different sets of neurons) are trained simultaneously, leading to improved generalization performance on unseen data. Dropout is especially useful in training large, deep networks, where the problem of over-fitting is often acute.&lt;/p&gt;
&lt;p&gt;Another strategy to reduce over-fitting is to expand the training dataset by incorporating artificial noise or variability (e.g., rotating/skewing images by a small amount in vision tasks).&lt;/p&gt;
&lt;h4 id=&#34;exploding-and-vanishing-gradients&#34;&gt;Exploding and Vanishing Gradients&lt;/h4&gt;
&lt;p&gt;In deep networks, the repetitive application of the chain rule often leads to a scenario where the gradient in each preceding neuron becomes either too small or too large. Mathematically, if the derivatives of the activation functions or weight matrices are consistently less than or greater than 1, the product of these derivatives can result in an exponential build-up, either resulting in an explosion or vanishing of the gradient. Both problems hinder effective training of deep neural networks but in opposite manners: one by making the gradients too small to effect meaningful weight updates and the other by making the gradients too large, causing unstable updates.&lt;/p&gt;
&lt;p&gt;To avoid vanishing and exploding gradients in deep neural networks, several strategies are commonly employed, involving careful architectural choices, initialization techniques, and optimization methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Proper initialization of weights is crucial in avoiding gradient problems. A common way to initialize weight values is to use Gaussian random variables with a mean of 0 and a standard deviation of \( \frac{1}{\sqrt{n}}\), where &lt;em&gt;n&lt;/em&gt; is the number of input nodes of the network. This ensures that individual weight do not start at exceedingly high or low values.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using activation functions like ReLU and its variants, such as Leaky ReLU and Parametric ReLU, can significantly mitigate the vanishing gradient problem. ReLU does not saturate for positive values, which helps in maintaining gradients. However, ReLU can lead to the &amp;ldquo;dying ReLU&amp;rdquo; problem, where neurons become inactive if their outputs are always zero. Variants like Leaky ReLU address this by allowing a small, non-zero gradient when the input is negative, defined as \( f(x) = \begin{cases} x &amp;amp; \text{if } x &amp;gt; 0 \\ \alpha x &amp;amp; \text{if } x \leq 0 \end{cases} \), where \( \alpha \) is a small constant.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Normalization techniques such as Batch Normalization and Layer Normalization also play a critical role. Batch Normalization normalizes the inputs of each layer over a mini-batch, which mitigates internal covariate shift and smoothens the optimization landscape. The formula for Batch Normalization is \( \hat{x} = \frac{x - \mu_{\text{batch}}}{\sqrt{\sigma_{\text{batch}}^2 + \epsilon}}, \quad y = \gamma \hat{x} + \beta \), where \( \mu_{\text{batch}} \) and \( \sigma_{\text{batch}} \) are the mean and variance of the mini-batch, and \( \gamma \) and \( \beta \) are learnable parameters.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Gradient clipping is another effective technique, particularly useful for recurrent neural networks (RNNs) and other deep architectures. It prevents gradients from becoming excessively large by clipping them to a maximum threshold.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Residual connections, also known as skip connections, are employed in architectures like ResNet (Residual Networks) to help gradients flow more easily through the network. By adding the input of a layer to its output, residual connections prevent the gradients from diminishing as they propagate through many layers. This can be expressed as \( \text{Output} = \text{Activation}(x + \text{Layer}(x)) \), where \( x \) is the input to the layer.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using adaptive learning rate optimizers like Adam and RMSprop, or applying learning rate schedules that decrease the learning rate over time, can further stabilize training. These methods adjust the learning rate based on the gradients, which helps manage issues with both vanishing and exploding gradients. For instance, the Adam optimizer updates parameters using the formula \( \theta_{t+1} = \theta_t - \frac{\eta}{\sqrt{\hat{v}_t} + \epsilon} \hat{m}_t \), where \( \hat{m}_t \) and \( \hat{v}_t \) are bias-corrected first and second moment estimates.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;summing-it-all-up&#34;&gt;Summing it all up&lt;/h2&gt;
&lt;p&gt;In this note we&amp;rsquo;ve covered the fundamental structure of ANNs and the basic processes behind their training. The basic feedforward network we disected in this note represents only a subset of possible ANN structures. In future notes we will expand upon these concepts and explore specific ANN architectures and how they are adapted to different fields like image and speech recognition, natural language processing, and generative tasks.&lt;/p&gt;


	
	
	
	

	
		&lt;div class=&#34;series&#34;&gt;
			&lt;div class=&#34;series-header&#34;&gt; This note is part of a series on &lt;span class=&#39;series-name&#39;&gt;Artificial Neural Networks&lt;/span&gt;:&lt;/div&gt;

			&lt;ol&gt;
				
					
						&lt;li&gt;&lt;strong&gt;The Basics&lt;/strong&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-architectures/&#39;&gt;Architectures&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-embedding/&#39;&gt;Embeddings&lt;/a&gt;&lt;/li&gt;
					
				
					
					&lt;li&gt;&lt;a href=&#39;http://notes.mirzo.net//post/ann-transformers/&#39;&gt;Transformers&lt;/a&gt;&lt;/li&gt;
					
				
			&lt;/ol&gt;

		&lt;/div&gt;</description>
      
    </item>
    
  </channel>
</rss>
