<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dylan Skinner on Medium]]></title>
        <description><![CDATA[Stories by Dylan Skinner on Medium]]></description>
        <link>https://medium.com/@dylanskinner65?source=rss-f0a6e7109945------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HIylZwyTGvxmtJkbTuIqIQ.gif</url>
            <title>Stories by Dylan Skinner on Medium</title>
            <link>https://medium.com/@dylanskinner65?source=rss-f0a6e7109945------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:38:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dylanskinner65/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Convex Linear Optimization with CVXPY]]></title>
            <link>https://medium.com/@dylanskinner65/convex-linear-optimization-with-cvxpy-5fa1024254ff?source=rss-f0a6e7109945------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fa1024254ff</guid>
            <category><![CDATA[cvxpy]]></category>
            <category><![CDATA[convex-optimization]]></category>
            <category><![CDATA[linear-optimization]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Dylan Skinner]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 00:59:05 GMT</pubDate>
            <atom:updated>2023-02-25T00:59:05.894Z</atom:updated>
            <content:encoded><![CDATA[<p>CVXPY is a library in Python that contains a set of classes and functions designed for solving convex optimization problems. There are many different convex optimization problems we can solve using CVXPY, but today we are going to look at linear convex optimization problems.</p><p>A linear convex optimization problem (sometimes called linear programming) is a linear constrained optimization problem. This means that the problem can be stated in a way similar to</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*m_8Npki5Gfb7QgnzDyyk1A.png" /></figure><p>CVXPY accepts ≤, ≥, and = in its constraints as long as the equations satisfy convexity requirements.</p><p>In our CVXPY problems, CVXPY accepts NumPy arrays and SciPy sparse matrices as constraints, but the variable <em>x</em> must be a CVXPY Variable.</p><p>Let’s do a simple example to see how this works. Consider the following problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*heyTMMEBjZYy2pwYei8hXw.png" /></figure><p>How would we represent this using CVXPY? Here is the code to represent this problem. I will present the code and then break down each line.</p><pre># Import necessary libraries<br>import cvxpy as cp<br><br># Initialize the variables<br>x1, x2 = cp.Variable(nonneg=True), cp.Variable(nonneg=True)<br><br># Create objective function<br>objective = cp.Minimize(-4*x1 - 5*x2)<br><br># Initialize constraints (must be a list)<br>constraints = [x1 + 2*x2 &lt;= 3,<br>               2*x1 + x2 == 3]<br><br># Create our problem<br>problem = cp.Problem(objective, constraints)<br><br># Solve our problem<br>problem.solve()<br><br># Find out what the optimal values of x1 and x2 are, and what the answer<br>  # our optimization problem is<br>print(problem.value)<br>print(x1.value, x2.value)</pre><p>There are more optimal ways to do the code above, and we will go over those potential improvements. Now, let’s break down each line.</p><p>First, we initialize the variables.</p><pre># Initialize the variables<br>x1, x2 = cp.Variable(nonneg=True), cp.Variable(nonneg=True)</pre><p>Here we create the CVXPY Variables x1 and x2. These are the variables we use to solve our problem. From our optimization constraints, we learn that both x1 and x2 must be greater than or equal to 0. So, by adding nonneg=True into the cp.Variable() declaration, CVXPY knows that these variables must be greater than or equal to 0. You could omit this function argument and instead put x1 ≥ 0, x2 ≥ 0 in the constraints list. Either way works. If, in your optimization problem, the variables x1 and x2 can take on negative values, then omit nonneg=True and do not include anything in your constraints list.</p><p>Next, create our objective function.</p><pre># Create objective function<br>objective = cp.Minimize(-4*x1 - 5*x2)</pre><p>Creating the objective function in CVXPY is simple. If you wish to minimize your objective function, simply do cp.Minimize() with the problem inside the function—using the CVXPY variables you have already declared. If your problem involves maximization, simply do cp.Maximize() with the problem inside the function—again, using the CVXPY variables you have already declared.</p><p>Moving on, we have our constraints.</p><pre># Initialize constraints (must be a list)<br>constraints = [x1 + 2*x2 &lt;= 3,<br>               2*x1 + x2 == 3]</pre><p>The very first thing to mention is that our constraints must be in list form! CVXPY will not work otherwise. Even if you have only one constraint, that single constraint must be in a list. Otherwise, creating our constraints is simply encoding our constraints in Python!</p><p>Now we create our problem.</p><pre># Create our problem<br>problem = cp.Problem(objective, constraints)</pre><p>With this step, use cp.Problem() and put in the objective of the convex problem as the first argument, and the constraints as the second argument. Of course, if you desired, you could skip the steps of defining the objective function and constraints and do all of this inside cp.Problem().</p><p>Once we have our problem created, we solve our problem!</p><pre># Solve our problem<br>problem.solve()</pre><p>I feel that this step is self-explanatory. Just type the above!</p><p>That is it! If you find the run the code, you get:</p><pre>problem.value -&gt; -8.999999999850528<br>x1.value -&gt; 1.<br>x2.value -&gt; 1.</pre><p>And it is that easy! Now, let’s do it with a different, more difficult example.</p><p>Consider the following network:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*Woao8nRDdvNnct7Z0HRKrg.png" /></figure><p>If we represent the edges between the nodes and their associated weights, we have:</p><pre><br>From  To  Weight<br>A     B     2<br>A     D     5<br>B     C     5<br>B     D     2<br>B     E     7<br>B     F     9<br>C     F     2<br>D     E     4<br>E     F     3</pre><p>Assume that the supply (or demand, depending on the sign) at the nodes is</p><p>bA = 10, bB = 1, bC = -2, bD = -3, bE = 4, bF = -10</p><p>and that the capacity of each edge is bounded by 6. Using this information, we can build a linear optimization problem whose solution we can find using CVXPY and will tell us the cheapest flow in this network with the above constraints. The first thing we need to do is build our linear optimization problem.</p><p>We know that we want to find the most efficient way to distribute our supplies. There are 9 different routes to take (represented in the 9 different from-to pairs above), so we know that we will need 9 CVXPY variables (or, rather, one variable with 9 spots). If A is the matrix that represented the edges connecting different nodes, x is the vector representing the values we are trying to find, b represents the supply/demand of the nodes, and cost represents the weight of the edges in the from-to pairs above, we can structure our linear optimization problem as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*8H5oL4F6GEhCfS7bnNW6Sw.png" /></figure><p>In Python (and using NumPy), we can represent these variables as:</p><pre>import numpy as np<br><br># How much it costs to go around the edges of our network <br>  # (the weight of each edge)<br>cost = np.array([2, 5, 5, 2, 7, 9, 2, 4, 3])<br><br><br># Our matrix, representing the connections between the nodes.<br>  # 1 for an edge going from the node, -1 for an edge going into the node.<br>  # The rows represent the nodes, and the columns represent the connections<br>    # associated with that node.<br>  # So row 0 is node A, row 1 is node B, etc, and column 0 is the connection<br>    # from A to B, column 1 is the connection from A to D, etc.<br>A = np.array([[ 1,  1,  0,  0,  0,  0,  0,  0,  0],<br>              [-1,  0,  1,  1,  1,  1,  0,  0,  0],<br>              [ 0,  0, -1,  0,  0,  0,  1,  0,  0],<br>              [ 0, -1,  0, -1,  0,  0,  0,  1,  0],<br>              [ 0,  0,  0,  0, -1,  0,  0, -1,  1],<br>              [ 0,  0,  0,  0,  0, -1, -1,  0, -1]])<br><br># Our b vector, which contains the information for bA to bF as seen above.<br>b = np.array([10, 1, -2, -3, 4, -10])<br></pre><p>Now, in order to represent x, we will need to initialize a CVXPY variable.</p><pre># Instead of initializing x1, x2, ..., x9, we can instead initialize<br>  # x to be a vector with 9 entries.<br>x = cp.Variable((9), nonneg=True)</pre><p>Now that we have all of our variables ready and declared, we need to create our objective function,</p><pre># Similiar to NumPy, represent matrix multiplication in CVXPY with @<br>objective = cp.Minimize(cost.T@x)</pre><p>and our constraints</p><pre># Remember: Constraints must be in list form!<br>  # Additionally, since we said in the creation of the x-variable that<br>    # it must be non-negative, the only constraint we need to include is that<br>    # each value of x cannot be greater than 6.<br>constraints = [A@x == b,<br>               x &lt;= 6]</pre><p>With our variables, objective function, and constraints all defined, it is time to solve our convex linear optimization problem using CVXPY! Try to do it on your own before looking at the provided code.</p><pre>import cvxpy as cp<br>import numpy as np<br><br># How much it costs to go around the edges of our network <br>  # (the weight of each edge)<br>cost = np.array([2, 5, 5, 2, 7, 9, 2, 4, 3])<br><br># Our matrix, representing the connections between the nodes.<br>  # 1 for an edge going from the node, -1 for an edge going into the node.<br>  # The rows represent the nodes, and the columns represent the connections<br>    # associated with that node.<br>  # So row 0 is node A, row 1 is node B, etc, and column 0 is the connection<br>    # from A to B, column 1 is the connection from A to D, etc.<br>A = np.array([[ 1,  1,  0,  0,  0,  0,  0,  0,  0],<br>              [-1,  0,  1,  1,  1,  1,  0,  0,  0],<br>              [ 0,  0, -1,  0,  0,  0,  1,  0,  0],<br>              [ 0, -1,  0, -1,  0,  0,  0,  1,  0],<br>              [ 0,  0,  0,  0, -1,  0,  0, -1,  1],<br>              [ 0,  0,  0,  0,  0, -1, -1,  0, -1]])<br><br># Our b vector, which contains the information for bA to bF as seen above.<br>b = np.array([10, 1, -2, -3, 4, -10])<br><br># Instead of initializing x1, x2, ..., x9, we can instead initialize<br>  # x to be a vector with 9 entries.<br>x = cp.Variable((9), nonneg=True)<br><br># Similiar to NumPy, represent matrix multiplication in CVXPY with @<br>objective = cp.Minimize(cost.T@x)<br><br># Remember: Constraints must be in list form!<br>  # Additionally, since we said in the creation of the x-variable that<br>    # it must be non-negative, the only constraint we need to include is that<br>    # each value of x cannot be greater than 6.<br>constraints = [A@x == b,<br>               x &lt;= 6]<br><br># Create and solve our convex linear optimization problem.<br>problem = cp.Problem(objective, constraints)<br>problem.solve()<br><br># See what our solution is:<br>print(problem.value)<br>print(x.value)</pre><p>The solution is:</p><pre>problem.value -&gt; 98.00000002103977<br>x.value       -&gt; [5.99999999 4.00000001 6.         0.31411362 0.00000001 0.68588636<br> 4.         1.31411363 5.31411364]<br><br># After rounding...<br>x.value       -&gt; [6, 4, 6, 0, 0, 1, 4, 1, 5]</pre><p>And there you have it! CVXPY is a great library that makes solving convex optimization problems a breeze! You can solve more than linear optimization problems, so keep searching to learn more!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fa1024254ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating One-Factor ANOVA tables in R (without extra libraries)]]></title>
            <link>https://medium.com/@dylanskinner65/creating-one-factor-anova-tables-in-r-without-extra-libraries-d896b024bfc2?source=rss-f0a6e7109945------2</link>
            <guid isPermaLink="false">https://medium.com/p/d896b024bfc2</guid>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[anova]]></category>
            <category><![CDATA[r-programming]]></category>
            <dc:creator><![CDATA[Dylan Skinner]]></dc:creator>
            <pubDate>Wed, 16 Mar 2022 22:08:34 GMT</pubDate>
            <atom:updated>2022-03-16T22:08:34.154Z</atom:updated>
            <content:encoded><![CDATA[<p>A simple step-by-step tutorial for calculating One-Factor ANOVA tables in R!</p><p><strong>This article assumes you already know what ANOVA tables are.</strong></p><p>In this article, I will show you how to create a One-Factor ANOVA table in R without downloading or using any libraries outside of the standard R ecosystem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3KzUycUaAKE73ve9dRecYg.png" /><figcaption>All the fun parts of calculating ANOVA tables!</figcaption></figure><p><strong>Motivating Example</strong></p><p>I want to start this off with an example from the article <a href="https://www.sciencedirect.com/science/article/pii/S0925857411000802">“Nutrient Deprivation Improves Field Performance of Woody Seedlings in a Degraded Semi-arid Shrubland” (R. Trubata, J. Cortina, and A. Vilagrosaa, Ecological Engineering, 2011:1164–1173)</a>. One part of this article looks at the effect of three different types of fertilizers on the height of a specific Mediterranean tree species. One experiment the group conducts takes three samples of 10 different trees, with each sample being grown with a different fertilizer. One group—the control group—was grown with a standard fertilizer, a second group was grown with a fertilizer that contained half the nutrients of the standard fertilizer, and the third group was grown using a commercial slow-release fertilizer. After one year, the heights of the trees were measured and are listed in the following table.</p><figure><img alt="A table that has the three different types of fertilizers as the columns, with the corresponding height as the rows. There are 10 rows in total." src="https://cdn-images-1.medium.com/max/1024/1*Vm7VcoTtJA6C5N-WxOK3zQ.png" /><figcaption>Data table courtesy of <a href="https://www.mheducation.com/highered/product/statistics-engineers-scientists-navidi/M9781259717604.html">William Navidi, “Statistics for Engineers and Scientists,” 5th edition</a> (page 688–9).</figcaption></figure><p>This leads us to our research question: does fertilizer type affect the height of these Mediterranean trees?</p><p>One statistical tool we can use to analyze the data and figure out the answer to our research question is called <strong>An</strong>alysis <strong>o</strong>f <strong>Va</strong>riance (ANOVA). The idea being to use an ANOVA test is to compare the variance in means of level (fertilizer type), to the variance that occurs by change (variance of errors). Now, you can compute this ANOVA table by hand, but it is long, tedious, and simply unnessary. Instead, we can use R!</p><p><strong>Calculating ANOVA Tables in R</strong></p><p>Everything I am doing in the following steps can be done in the terminal of R.</p><ol><li>The first thing we need to do is read in our data.</li></ol><pre>Control &lt;- c(17.9, 12.2, 14.9, 13.8, 26.1, 15.4, 20.3, 16.9, 20.8, 14.8)<br>Deficient &lt;- c(7.0, 6.9, 13.3, 11.1, 11.0, 16.5, 12.7, 12.4, 17.1, 9.0)<br>Slow_release &lt;- c(19.8, 20.3, 16.1, 17.9, 12.4, 12.5, 17.4, 19.9, 27.3, 14.4)</pre><p>2. Next, we want to put these variables into a dataframe</p><pre>height_dataset &lt;- data.frame(Control, Deficient, Slow_release)</pre><p>This will give us a nice data frame that looks like this:</p><figure><img alt="Our height_dataset after we create a data frame of our variables." src="https://cdn-images-1.medium.com/max/652/1*sDKyWUMl8GuOb_qcdIncfw.png" /></figure><p>3. To help prepare the data for the ANOVA table, we will want to stack the data together using the stack() function</p><pre>stack_height_dataset &lt;- stack(height_dataset)</pre><p>When we stack our dataset together, we see that our data frame turns into a two column frame where the first column has the heights, and the second column as the three types of fertilizer. But, the column headers do not fully represent this.</p><figure><img alt="Our data frame after it has been stacked. The column headers do not accurately represent what is in the columns." src="https://cdn-images-1.medium.com/max/424/1*rTtKYDoqsWuDosTiYzQHBA.png" /><figcaption>This data frame has 30 rows, but these 21 should help you get the idea.</figcaption></figure><p>4. To make things easier for later, rename the column headers to accurately represent what the columns are showing.</p><pre>names(stack_height_dataset) &lt;- c(&quot;Height&quot;, &quot;Method&quot;)</pre><p>If we look at our data frame again, we will see that the column headers are renamed and accurately represent what we are looking at.</p><figure><img alt="Our data frame after we update the column headers to more accurately represent what is in the columns." src="https://cdn-images-1.medium.com/max/424/1*FI5iF7MWy6J4dzBFvoINdg.png" /><figcaption>We see that the column headers are renamed.</figcaption></figure><p>5. With our renamed column headers, we will now want to turn our data frame into a linear model using the lm() function in R.</p><pre>height_fit &lt;- lm(Height~Method, data=stack_height_dataset)</pre><p>If you look at our newly created height_fit linear model, we see a few weird things. Do not worry too much about this. This is all information that our next step requires.</p><figure><img alt="What R shows us when we look at our height_fit variable looks like after passing it through the lm() function." src="https://cdn-images-1.medium.com/max/1024/1*DpQqPbiZDA2oPgyduUsksQ.png" /><figcaption>There is a lot of information here, but do not worry about it if you are new! It is not super important for you to understand right now.</figcaption></figure><p>6. Final step! All of our data preparation has brought us up to this point! All we need to do is pass our height_fit linear model into the anova() function that R provides.</p><pre>anova(height_fit)</pre><p>When you run this function, R will automatically output the ANOVA table will all the information we need!</p><pre>Analysis of Variance Table</pre><pre>Response: Height<br>          Df Sum Sq Mean Sq F value  Pr(&gt;F)   <br>Method     2 229.74 114.870  7.0587 0.00342 **<br>Residuals 27 439.39  16.274                   <br>---<br>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</pre><p>That is it! If you recall our original research question, we wanted to know if fertilizer type impacts the height of these trees. If you recall from your statistics class, we compare the p-value with our significance value, 𝛂. In this case, we used a significance 𝛂 = 0.05 (which is pretty standard). Looking at our ANOVA table, we see that our p-value = 0.00342. Comparing our p-value and our significance level, we see that p-value = 0.00342 &lt; 0.05 = 𝛂, so we reject the null hypothesis and conclude that the fertilizer does have an impact on the height of these Mediterranean trees!</p><p>(If you are interested in finding the critical value of our experiment to compare with the F-value found on our ANOVA table, use the R function qf(). All you need is a significance level (1-𝛂), df1, and df2 where df1 is the degrees of freedom of the Method, and df2 is the degrees of freedom of the Residuals (simply look at the Df column of our ANOVA table).)</p><p>And that is it! Creating One-Factor ANOVA tables in R is super easy! Make sure to check out the original study that inspired our motivating example, and the textbook that helped provide the data table for our experiment!</p><p>Here is a picture of the full code in an R console.</p><figure><img alt="All the code used in this article." src="https://cdn-images-1.medium.com/max/1024/1*l6m_K6UVKQ5iUAfVg61USw.png" /></figure><p>Here is a code block of the full code in an R console.</p><pre>&gt; Control &lt;- c(17.9, 12.2, 14.9, 13.8, 26.1, 15.4, 20.3, 16.9, 20.8, 14.8)<br>&gt; Deficient &lt;- c(7.0, 6.9, 13.3, 11.1, 11.0, 16.5, 12.7, 12.4, 17.1, 9.0)<br>&gt; Slow_release &lt;- c(19.8, 20.3, 16.1, 17.9, 12.4, 12.5, 17.4, 19.9, 27.3, 14.4)<br>&gt; <br>&gt; height_dataset &lt;- data.frame(Control, Deficient, Slow_release)<br>&gt; View(height_dataset)<br>&gt; stack_height_dataset &lt;- stack(height_dataset)<br>&gt; <br>&gt; names(stack_height_dataset) &lt;- c(&quot;Height&quot;, &quot;Method&quot;)<br>&gt;<br>&gt; height_fit &lt;- lm(Height~Method, data=stack_height_dataset)<br>&gt;<br>&gt; anova(height_fit)<br>Analysis of Variance Table</pre><pre>Response: Height<br>          Df Sum Sq Mean Sq F value  Pr(&gt;F)   <br>Method     2 229.74 114.870  7.0587 0.00342 **<br>Residuals 27 439.39  16.274                   <br>---<br>Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d896b024bfc2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>