## Some Notes on Optimal Portfolio Theory

This note just reviews the derivation of portfolios that maximize the Sharpe ratio.

Suppose that you have some stocks that you want to invest in. We will think of the returns of these stocks as being a random column vector $G$ in $R^n$. Suppose that $r=E[G]\in R^n$ is a vector of the expected return of the stocks and $C= E\left[ (G-r) (G-r)^T\right]$ is the covariance matrix of $G$ with the superscript $T$ indicating the transpose, thus $C\in R^{n\times n}$.

We will often want to maximize the Sharp ratio of a portfolio which is defined as the expected return of the portfolio minus the risk free return divided by the standard deviation.  In order to simplify the math a little, we will assume that the risk free return is 0 and $C$ is positive definite, $a^T C a>0$ for all vectors $a\in R^n\setminus\{0\}$. Thus for our purposes, the Sharpe ratio for an “allocation vector” $a\in R^n\setminus\{0\}$ will be defined $$\rho(a) := \frac{E[a^T G]}{\sqrt{E[ (a^T G - a^T r)^2]}} = \frac{a^T r}{\sqrt{a^T C a}}.$$ We could say that the allocation vector is in dollars, so $a_1$ would be the dollar value of the stocks held in the portfolio for the first stock.  The value of $a_1$ could be negative indicating that the stock was shorted.

It is helpful to notice that the Sharpe ratio does not change if we double or triple the amount invested in each stock.  In fact, for any real number $\gamma\neq 0$ and any nonzero allocation vector $a\in R^n$, $$\rho(\gamma a)= \gamma \rho(a).$$ So, when maximizing $\rho$ we can restrict ourselves to vectors $a$ where $a^T C a=1$.

The matrix $C$ is real symmetric positive semidefinite, so it has a Cholesky decomposition $C=U^T U$ where $U$ is upper triangular.  Let $u= U a$.  Then $$1=a^T C a= a^T U^T U a = u^T u= ||u||^2,$$ so $u$ has norm 1. Thus if we want to maximize $\rho(a)$, it suffices (by restricting to vectors $a$ where $a^T C a=1$) to maximize $$\rho(a) = \frac{a^T r}{\sqrt{a^T C a}} = a^T r = u^T U^{-T} r$$ over all unit vectors $u$. (We use $U^{-T}$ to denote $(U^T)^{-1}$, the inverse transpose of $U$.)  The unit vector which maximizes $u^T U^{-T} r$ is simply $$u^*= \frac{U^{-T} r}{|| U^{-T} r||}.$$ We can now generate an optimal allocation vector $a^*$ by

$$a^* = U^{-1} u^*= \frac{U^{-1} U^{-T} r}{|| U^{-T} r||} = \frac{ (U^T U )^{-1} r}{|| U^{-T} r||} = \frac{ C^{-1} r}{|| U^{-T} r||}.$$ The scalar factor $|| U^{-T} r||$ has no effect on $\rho$, so $$a^{**} = C^{-1} r$$ is also an optimal allocation vector.  Note that the Sharpe ratio of $a^*$

$$\rho(a^{**})=\rho(a^*)=\frac{(a^{*})^T r}{\sqrt{(a^{*})^T C a^*}}=(a^{*})^T r= \frac{r^T U^{-1} U^{-T} r}{|| U^{-T} r||}= || U^{-T} r||.$$

### Example 1

Suppose that you want to invest in two uncorrelated stocks.  Assume that their expected returns are $r=( 0.001, 0.001)^T$ and their covariance matrix is $$C=\left(\begin{matrix} 10^{-4} & 0 \\ 0 & 10^{-4}\end{matrix}\right).$$  All optimal allocations $a$ of the stocks are multiples of $$a^{**} = C^{-1} r = \left(\begin{matrix} 10^{4} & 0 \\ 0 & 10^{4}\end{matrix}\right)( 0.001, 0.001)^T= (10, \ 10)^T.$$ This merely indicates that the optimal Sharpe ratio is attained if and only if you invest the same amount in money in each of these stocks.

### EXAMPLE 2

Suppose that you want to invest in two uncorrelated stocks.   Assume that their returns are $r=( 0.001, 0.0005)^T$ and their covariance matrix is $$C=\left(\begin{matrix} 10^{-4} & 0 \\ 0 & 10^{-4}\end{matrix}\right).$$  All optimal allocations $a$ of the stocks are multiples of $$a^{**} = C^{-1} r = \left(\begin{matrix} 10^{4} & 0 \\ 0 & 10^{4}\end{matrix}\right)( 0.001, 0.0005)^T= (10, \ 5)^T.$$ This indicates that the optimal Sharpe ratio is attained if and only if you invest the twice as much money in the first stock and a nonzero amount of money is invested.  Note that Kelly Criterion often indicates that your bets should be proportional to the edge of your investments, so it gives similar advice.

### EXAMPLE 3

Suppose that we have two gamblers.  The first gambler is willing to give you 2.2 times your wager if candidate A wins the election, but you lose the bet if candidate A does not win.  (I.e. if you wager $\$10$with the first gambler, the your net gain will be$\$22 – \$10 = \$12$ if you win.)   The second gambler is willing to pay you twice your bet if candidate B wins and you lose your bet with the second gambler if candidate B loses.

This could be called an arbitrage situation.

Let’s assume that there is a 50% chance that candidate A will win and a 50% chance that candidate B will win.  We can think of each gambler as being a stock that we can invest in.  The expected value of the first gambler is 0.1  (i.e. if you wager ${\$}10$with the first gambler, your expected net gain is 0.1*${\$}10$ = ${\$}1$.) The expected value of the second gambler is 0. The covariance matrix requires some computations. $$C_{11} = E[ (G_1-r_1)^2] = 1/2 ( (1.2 – 0.1)^2 + (-1 – 0.1)^2 ) = 1.21.$$ $$C_{12} = C_{21} = E[ (G_1-r_1)(G_2-r_2)] = 1/2 ( (1.2 – 0.1)(-1) + (-1 – 0.1)1 ) = -1.1.$$ $$C_{22} = C_{21} = E[ (G_2-r_2)^2] = 1/2 ( (-1)^2 + (1)^2 ) = 1.$$ $$C = \left(\begin{matrix} 1.21 & -1.1 \\ -1.1 & 1 \end{matrix}\right).$$ Interestingly,$C$is not invertible. This is because$(10, 11) C (10, 11)^T = 0$. This means that if you wager$\$10$ with gambler 1 and $\$11$with gambler 2, you will always win$\$1$.  If candidate A wins, then you gain $\$12$from the first gambler and lose$\$11$ to the second.  If candidate B wins, then you lose $\$10$to the first gambler and gain$\$11$ from the second.  Since you always win $\$1$, your volatility is zero and your Sharpe ratio is infinite. In the derivation, we assumed that$C$was positive definite, but in this example, it is not. In a future post, I would like to give a few more examples and maybe even compare the optimal Sharp ratio allocation with a Kelly allocation. ## One reason why I want 30 digits of Pi or even more I was wondering how fast the Taylor Series for$\exp(\pi i)$would converge. According to Taylor, $$\exp(\pi i) = 1 + (\pi i) + (\pi i)^2/2! + (\pi i)^3/3! + (\pi i)^4/4! + \ldots = \sum_{j=0}^\infty \frac{(\pi i)^j}{j!}.$$ According to Euler,$\exp(\pi i)= -1$. I wanted to estimate the convergence rate, so I just fired up Mathematica and plotted the values of $$error(k) = \left| 1+ \sum_{j=0}^\infty \frac{(\pi i)^j}{j!}\right|.$$ The Mathematica plot is shown below. It occurred to me that Mathematica knows thousands of digits of pi. I could force it to only use the first 16 digits by using double precision floating point numbers. If I do that, then the log(Error) plot is “wrong” because the approximation for pi is not accurate enough. The erroneous plot is shown below. If you want a good estimate of the truncated Taylor series error using$k$terms, you could use the absolute value of term$k+1$which is $$error(k)\approx f(k)= \frac{\pi^{k+1}}{(k+1)!}$$. Here is a plot of$r(k)= error(k)/f(k)$. If the estimator is good, then the$r(k)$values (y-coordinates) should be near 1. Creating these plots requires around 30 digits of pi! If I wanted to look at more terms, I would need more digits of pi. Here is the code I used to generate the plots. SetOptions[ListPlot, GridLines -> Automatic, Frame -> True, ImageSize -> 250]; error[k_] := Abs[ 1 + Sum[ (I Pi)^j/j!, {j, 0, k}]]; plt0 = ListPlot[ Array[error, 10], Frame -> True, FrameLabel -> (Style[#, 18] & /@ {"Number of Terms", "Error"})]; plt1 = ListPlot[Log[ Array[error, 40]], FrameLabel -> (Style[#, 18] & /@ {"Number of Terms", "log(Error)"})]; error[k_] := Abs[ 1 + Sum[ (I N[Pi])^j/j!, {j, 0, k}]]; plt3 = ListPlot[Log[ Array[error, 40]], Frame -> True, FrameLabel -> (Style[#, 18] & /@ {"Number of Terms", "log(Error)", "Bad Error plot Caused by using only 16 digits of pi", " "}), PlotLabel -> Style["Bad Error plot Caused by using\n only 16 digits of pi", 16]]; errorRat[k_] := Abs[ 1 + Sum[ (I Pi)^j/j!, {j, 0, k}]]/ (Pi^(k + 1)/(k + 1)! ); plt4 = ListPlot[Array[errorRat, 40], Frame -> True, FrameLabel -> (Style[#, 18] & /@ {"Number of Terms", "error(k)/f(k)"}), PlotLabel -> Style["Plot error(k) divided by estimated\n error(k) for \ k=1,2,..., 40", 16]]; ## Maximizing a function over integers (Part 3) In part 1, we defined the term strictly concave and introduced the theorem that if$f$is strictly concave and$f(x)=f(x+1)$, then the integer(s) that maximize$f$are exactly the set$\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\}$which is usually a set containing only one number$\mathrm{round}(x+1/2)$. In part 2, we showed how the theorem in part 1 can be used to prove that the maximum value that can be obtained when putting an integer into$f(x)=a x^2 +  b x + c$is$f(i)$where$i=\mathrm{round}(-b/(2a))$(assuming$a<0$). The next theorem says that the real number$x$that maximizes$f$is always near the integer$i$that maximizes$f$. ### THEOREM #3 If •$f$is a real valued strictly concave function, • there is a real number$x$such that$f(x)$is the maximum value that$f$attains with real inputs, and • there is an integer$i$such that$f(i)$is the maximum value that$f$attains with integer inputs, then$|x-i|<1$. The idea of the proof is fairly simple. Suppose that$i>x+1$. Then$i-1>x$. So$i-1$is between$i$and$x$. Concavity implies that that the point$(i-1, f(i-1))$lies above the line connecting$(x, f(x))$and$(i,f(i))$, but$f(x)\geq f(i)$. The fact that the point$(i-1, f(i-1))$is above the line implies that$f(i-1)> f(i)$, but that contradicts the definition of$i$, so using proof by contradiction,$i\leq x+1$. The other cases are either similar or easy. ### EXAMPLE Find the maximum value of$f(x) = x (x-14) (x-20)$among all integers between 0 and 14. Finding the maximum among reals is easy if you know differential calculus and the quadratic formula. According to differential calculus, the maximum can only occur if the “derivative” of$f(x)$is zero. $$f(x) = x (x-14) (x-20) =x^3-34 x^2 + 280x.$$ The derivative of a polynomial can be found by multiplying the coefficient by the exponent and decreasing the exponent by 1. The result is $$f'(x) = 3 x^2 -68 x + 280.$$ If we put that in the quadratic formula, we get $$x=\frac{68\pm\sqrt{68^2 – 4 \cdot 3 \cdot 280}}{2 \cdot 3} = \frac{68\pm\sqrt{1264}}{6}\approx 5.41\ \mathrm{or\ } 17.26.$$ It turns out that 17.26 is a local minimum, so the local maximum occurs when$x\approx 5.41$. According to the theorem, any integer$i$that maximizes$f(i)$must be within 1 of 5.41, so that integer can only be 5 or 6. Now$f(5)= 675$and$f(6) = 672$, so 5 is the integer that maximizes$f$. Incidentally,$f(5.41) = 678.025021$and the true maximum of$f(x)$is $$f\left(\frac{68-\sqrt{1264}}{6}\right) = \frac{16}{27} \left(442+79 \sqrt{79}\right)\approx 678.025101610626.$$ We could actually use the theorem from part 1 to find the integer$i$that maximizes$f(i)$. Solving$f(x)=f(x+1)$using the quadratic formula gives $$x=\frac{1}{6} \left(65\pm\sqrt{1261}\right).$$ If you replace the$\pm$with the plus sign, you get the local minimum. If you replace it with the minus sign, you get $$\frac{1}{6} \left(65-\sqrt{1261}\right)\approx 4.91.$$ According to the theorem from part 1, the integer that maximizes$f(x)$must be round(4.91+1/2)=round(5.41)=5. Often it is easier to find the maximum of$f$rather than trying to solve$f(x)=f(x+1)$, so that is why the theorem above is useful. If you want to see a more formal mathematical writeup, click here. That PDF shows how you can generalize the three given theorems to a larger class of functions that are not necessarily strictly concave. Also, in the last section of the paper, we sharpen the bound on the difference between the real maximum and the integer maximum. Above, we showed that this difference was less than 1. If$f$is continuously twice differentiable, then sharper bounds can be computed. This concludes the three part series. ## Maximizing a function over integers (Part 2) In part 1, we defined the term strictly concave and introduced the theorem that if$f$is strictly concave and$f(x)=f(x+1)$, then the integer(s) that maximize$f$are exactly the set$\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\}$which is usually a set containing only one number$\mathrm{round}(x+1/2)$. ### Parabolas The simplest concave functions are parabolas with the vertex at the top. All parabolas can be described by three real numbers$a$,$b$, and$c$, and the formula$y=a x^2 + b x + c$. For strictly concave parabolas$a<0$. For example, if$a=-2$,$b=3$, and$c=5$, then the function$y= f(x) = -2x^2 + 3 x +5$is the parabola shown below. If the vertex (shown above in blue) is above the x-axis, then the x-coordiante of the vertex can be computed by finding the points where the concave parabola crosses the x-axis (shown above in orange). In our case, the parabola$y=f(x)$crosses the x-axis when$0=y=f(x), or when \begin{aligned}0&= -2x^2 + 3 x +5\\0&=-(2x^2-3×-5)\\0&=-(2×-5)(x+1).\end{aligned}. If0=\alpha\cdot \beta$, then either$\alpha=0$or$\beta=0$. So, the parabola crosses the x-axis when either$0=2×-5$or$0=x+1$which means that it crosses when$x=5/2$or$x=-1$. Parabolas are symmetric about the vertical line going through the vertex, so the x-coordinate of the vertex is half way between the x-coordinates of the crossing points (a.k.a the roots of$f(x)). \begin{aligned} x_\mathrm{vertex} &= \frac{x_\mathrm{crossing1} + x_\mathrm{crossing2}}2\\&= \frac{ -1 +5/2}2\\&=\frac{3/2}2\\&=3/4.\end{aligned} So the x-coordinate of the vertex isx=3/4$. This is also the real number that maximizes$f(w)$over all real numbers$w$. In general, the x-coordinates of the crossing points can be found with the quadratic formula $$x=\frac{-b\pm\sqrt{b^2-4ac}}{2 a}.$$ The$\pm$sign means that one crossing can be found by replacing$\pm$with + and the other can be found by replacing the$\pm$with -. But, if you have one number that is$x_1=\alpha+\beta$and another that is$x_2=\alpha-\beta$, then the average of the two numbers is just$\alpha$. In other words if the two values of$x$are$x=\alpha\pm\beta$, then the average value is just$\alpha$. So, to computer the average x-coordinate of the crossing points, all we have to do is remove the$\pm$sign and whatever it is applied to from the quadratic formula. $$x_\mathrm{vertex} = \frac{-b}{2 a}.$$ ### theorem #2 Informally, if$y=f(x)$is a parabola,$f(x) = a x^2 + b x + c$, and$a<0$, then the integer(s) that maximize$f(x)$are the set$\{\mathrm{floor}(x+1/2), \mathrm{ceil}(x-1/2)\}$where$x=\frac{-b}{2 a}$. If$x$is an integer plus 1/2 (e.g. 2.5, 7.5, …), this set has two elements$x+1/2$and$x-1/2$. If$x$is not an integer plus 1/2, the set has only one element$\mathrm{round}(x)$and that is the integer that produces the highest possible value of$f(z)$among all integers$z$. ### examples Example 1: If$f(x)= -2x^2 + 3 x +5$, then$a=-2$,$b=3$, and$c=5$. The values of$x$in the theorem is the same as the x-coordinate of the vertex $$x=\frac{-b}{2a} = \frac{-3}{2\cdot(-2)}= \frac{-3}{-4}=3/4.$$ The integer(s) that maximize$f(z)$among all integers$zare the set \begin{aligned}\{\mathrm{floor}(x+1/2), \mathrm{ceil}(x-1/2)\}&= \{\mathrm{floor}(3/4+1/2), \mathrm{ceil}(3/4-1/2)\}\\&= \{\mathrm{floor}(5/4), \mathrm{ceil}(1/4)\}\\&=\{1\}.\end{aligned} Example 2: If we lower the parabola from example 1 by a little bit settingf(x)= -2x^2 + 3 x +\sqrt{17}$, then$a=-2$,$b=3$, and$c=\sqrt{17}$. The value of$x$in the theorem is the same as the x-coordinate of the vertex$x=\frac{-b}{2a} =3/4.$The result does not depend on$c$, so as in example 1, the integer that maximizes$f(z)$among all integers$z$is$z=\mathrm{round}(3/4)=1$. Example 3:$f(x)= -x^2+x = (1-x)x$, then$a=-1$,$b=1$, and$c=0$. The value of$x$in the theorem is the same as the x-coordinate of the vertex $$x=\frac{-b}{2a} =\frac{-1}{2\cdot (-1)}=1/2.$$ The integer(s) that maximize$f(z)$among all integers$zare the set \begin{aligned}\{\mathrm{floor}(x+1/2), \mathrm{ceil}(x-1/2)\}&= \{\mathrm{floor}(1/2+1/2), \mathrm{ceil}(1/2-1/2)\}\\&= \{\mathrm{floor}(1), \mathrm{ceil}(0)\}\\&=\{1,0\}.\end{aligned} The integers that maximizef(z)$among all integers$z$are 0 and 1. If we look at the graph of$f(x)$below, we can see that the graph crosses the x-axis at$x=0$and$x=1$. So,$f(0)=f(1)=0$. All other integers inputs produce negative values. ### the spirit of the proof It turns out that Theorem 2 can be proven from theorem 1. Recall that in Theorem 1, we wanted to find the value of$x$where$f(x)=f(x+1)$. If we found that value, then the integer(s) that maximize$f$are exactly the set$\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\}$. In Theorem 2,$f(x) = a x^2 + b x + 1$. So if$f(x)=f(x+1), then \begin{aligned}a x^2 + b x + c &= a (x+1)^2 + b (x+1) + c\\a x^2 + b x &= a (x+1)^2 + b (x+1)\\ a x^2 + b x &= a (x^2+2x+1)+ b x+b\\ a x^2 &= a x^2+2ax+a+ b \\ 0 &= 2ax+a+ b \\ -a-b &= 2ax \\ \frac{-a-b}{2a} &= x\\ \frac{-b}{2a}-\frac12 &= x\\\end{aligned}. Thus the integers that maximizef(x)must be \begin{aligned} \{\mathrm{floor}(x+1), \mathrm{ceil}(x)\ \}&= \{\mathrm{floor}(\frac{-b}{2a}-\frac12+1), \mathrm{ceil}(\frac{-b}{2a}-\frac12)\} \\ &= \{\mathrm{floor}(\frac{-b}{2a}+\frac12), \mathrm{ceil}(\frac{-b}{2a}-\frac12)\}.\end{aligned} ### Why DId I WANTED to know this I was looking at several games where the optimal strategy depended on finding an integert$that maximized$f(t)$. In the game Slay the Spire, I wanted to maximize the amount of poison damage that I was going to do with the cards “Noxious Fumes” and “Catalyst”. If I played the “Catalyst” on turn$t$and the combat ended on turn$T$, then the poison damage done was $$\frac{(t+1)t}{2} -1 + (T-t)t$$ where$ \frac{(t+1)t}{2}-1$(note the triangle number) was the damage done by “Noxious Fumes” and$ (T-t)t$was the additional damage done by playing the Catalyst. I wanted to maximize$f(t) = (T-t)t = -t^2 + T t$. Using Theorem 2,$a=-1$,$b=T$, and$c=0$. The Theorem says that the maximum damage occurs when you play the “Catalyst” on round$t$where$t$is contained in the set$\{\mathrm{floor}(x+1/2), \mathrm{ceil}(x-1/2)\}$with$x=\frac{-b}{2 a}$. So$x=\frac{-T}{2\cdot(-1)}=T/2$. The best time to play the Catalyst was around half way through the combat. The catalyst should be played on round$T/2$if$T$is even. If$T$is odd, then the best round to play the “Catalyst” was for$t$in the set $$\{\mathrm{floor}(T/2+1/2), \mathrm{ceil}(T/2-1/2)\}= \{\frac{T+1}{2}, \frac{T-1}{2} \}.$$ ### summary So the first two theorems formalized rules of thumb that I though were true. 1. If you can find an$x$where$f(x)=f(x+1)$, then the optimal integer(s) is$z=\mathrm{round}(x+1/2)$with a special round that provides two answers if$x$is an integer, and 2. If$f(x)=a x^2 + b x + c$(a parabola), then the optimal integer(s) is$z=\mathrm{round}(x)$where$x=\frac{-b}{2 a}$which is the$x$that maximizes$f(w)$over all real numbers$w$(i.e. the x-coordinate of the vertex). If you want to see a more formal mathematical writeup, click here. ## Maximizing a function over integers (Part 1) I proved a few simple but interesting theorems about integers that maximize a function$f$when$f$is a strictly concave function that maps real numbers to real numbers. For example, the real number that maximizes$f(x)=x(1-x)$is$x=1/2$, but among all the integers, the maximum possible value of the function is 0. And that maximum is achieved twice with integers 0 and 1,$f(0)=0=f(1)$. A function is strictly concave if you can pick any two points on its graph, draw a line between them, and the curve$y=f(x)$between the two points lies entirely above the line between the two points. ### Theorem #1 Informally stated, the first theorem is that if you can find a real number$x$such that$f(x)=f(x+1)$, then the integer(s) that maximize$f$are the set$\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\}$where “floor” just means round down and “ceil” means round up. That set usually contains only one element which is$\mathrm{round}(x+1/2)$, but if$x$is an integer, then it will contain two consecutive integers$x$and$x+1$. For example, if$f(x) = 4-4x^2$, then the value of$x$that satisfies$f(x)=f(x+1)$is$x=-1/2$because$f(-1/2)=3=f(1/2)$. So the theorem says that any integer that maximizes$fmust be in the set \begin{aligned}\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\} &= \{\mathrm{floor}(-1/2+1), \mathrm{ceil}(-1/2)\}\\&= \{\mathrm{floor}(1/2), \mathrm{ceil}(-1/2)\} \\&= \{0\}.\end{aligned} The integer that does the best is 0 which is also the real number that maximizesf$. Here is another example. If$f(x) = \sin(x)$, then the value of$x$that satisfies$f(x)=f(x+1)$is$x=1.0708$because$f(1.0708)=0.877583=f(2.0708)$. So the theorem says that any integer that maximizes$fmust be in the set \begin{aligned}\{\mathrm{floor}(x+1), \mathrm{ceil}(x)\} &= \{\mathrm{floor}(1.0708+1), \mathrm{ceil}(1.0708)\}\\&= \{\mathrm{floor}(2.0708), \mathrm{ceil}(1.0708)\} \\&= \{2\}.\end{aligned}. The integer that does the best is 2. So, that was the first theorem. In part 2, we state a second theorem about finding the integer which maximizes a quadratic with some examples and one application the game “Slay the Spire”. If you want to see a more formal mathematical writeup, click here. ## Analyzing Slay the Spire and Sharing the Analysis So, I have played about 300 hours of Slay the Spire since I got it on July 26. It’s a turn-based deck building game. Many of these deck building games have interesting mathematics, so I have been devoting a fair amount of time to analyzing the game and writing about the game. The most interesting theorem about the game is D = h (( d – c b + w)/a + b/alpha) where D is the total damage that you take, h is the total amount of damage that you give to the enemy, d is the average attack per turn from the enemy, c is the average number of cards you play per turn, b is the average block per blocking card played, w is the average amount of waisted block per turn, and alpha is the average attack for the attack cards you played. (PDF slides here.) The nice thing about the formula is that h, d, c, b, and alpha are often precisely known or easy to calculate. Also, a large portion of the reasonable strategies have w=0. If you know h,d,c,b, and w and they are constant, then the correct strategy is simple: if (d-c b + w) is positive, then don’t block. If it’s negative, then block a lot. There are other analysis techniques that were mentioned in the Sept 27, 2020 reddit post “Simplified Spire Puzzles“. My favorite is looking at the ratio of damage received to damage given. Also, I wrote a computer program that computes the best possible strategy for just about any Slay Spire combat situation. The drawback is that if you have over 20 cards and 10 different types of cards, the program needs about 10 terabytes of ram and 3 years of cpu time to computer the answer. It is much quicker if you have only 10 cards the are all strike or block cards in which case it takes less than one cpu second and only a few kilobytes of ram. I have been wondering how to present this information to the world. Today, I showed the formula and my program to my friends Cat and Chuck who are both a) fans of Slay the Spire, and b) programmers. Additionally, I created about 10 power point slides and a 16 page document mostly describing simplified Slay the Spire problems and their solutions. Additionally, I would like to document all the card combinations that result in either an infinite sequence of actions (resulting from hyperbolic growth) or another kind of growth. Growth in this game seems to be limited to quadratic (1 4 9 16 25…), cubic (1 8 27 64 125…), or exponential (1 2 4 8 16 32…). I have never seen any other kind of growth in any game except dominion which can have polynomial growth where the exponent is a fraction. I don’t mind writing, but I do mind rewriting and combining my short essays into a larger, more useful work especially if no one is going to read it. Cheers, Hein ## “Category Theory for Programmers” I’ve been reading “Category Theory for Programmers” which was suggested to me by Mark Ettinger. This book presents many examples in C++ and Haskell. It teaches you some Haskell as you read the book. It uses almost zero upper level mathematics and it skips almost all of the mathematical formalities. If you decide that you want to read it, then you might want to read the first six chapters of “Learn You a Haskell for Great Good!” and write a few small Haskell programs first. (I also would suggest trying to solve the first three problems in Project Euler https://projecteuler.net/archives using Haskell.) I find the book to be easy to read and informative. When the author makes a statement like A*(B+C) = A*B + A*C where * means categorical product, + means coproduct, and = means isomorphic, I find myself trying to figure out the categories where the statement is true and the categories for which it is false. (It is true for the Category of Set and the Category Hask. The book is mostly about those categories.) That type of thinking improves my understanding of category theory. The book is also reawakening the parts of my brain that had forgotten parts of category theory and Haskell. Interestingly, in category theory,A*(B+C) = A*B + A*C$can be translated into the following theorems : 1. A*(B+C) = A*B + A*C is true for all positive integers A,B, and C, 2. max(A, min(B,C)) = min( max(A,B), max(A,C)) for all real numbers A, B, and C, 3. lcm(A, gcd(B,C)) = gcd( lcm(A,B), lcm(A,C) ) where lcm means least common multiple and gcd means greatest common denominator, and 4. intersection(A, union(B,C)) = union( intersection(A,B), intersection(A, C)). If you don’t believe the four theorems, here is some Mathematica Code which tests each theorem: Unprotect[C]; test[ funcRandChoose_, prod_, sum_, i_] := Tally[ Table[ A = funcRandChoose[]; B = funcRandChoose[]; C = funcRandChoose[]; prod[ A, sum[B, C]] == sum[ prod[A, B] , prod[A, C]], {i}]]; test[ RandomInteger[{1, 1000}] &, Times, Plus, 100] test[ RandomInteger[{-1000, 1000}] &, Max, Min, 100] test[ RandomInteger[{-1000, 1000}] &, LCM, GCD, 100] test[ RandomSample[ Subsets[ Range[5]]] &, Intersection, Union, 100] ## Seven Links for the History of Mathematical Proof ## Cute math,$\exp(x)\cdot\exp(-x)=1$$$\exp(x)\cdot\exp(-x)$$ $$=\sum_{n=0}^\infty \frac{x^n}{n!}\cdot\sum_{n=0}^\infty \frac{(-x)^n}{n!}$$ $$=\sum_{n=0}^\infty \frac{x^n}{n!}\cdot\sum_{n=0}^\infty(-1)^n \frac{x^n}{n!}$$ $$=\sum_{n=0}^\infty\sum_{i=0}^n (-1)^i \frac1{i!}\frac1{(n-i)!}x^n \quad\mathrm{collecting\ coef\ of\ } x^n$$ $$= 1+ \sum_{n=1}^\infty \frac{ (1-1)^n}{ n!} x^n = 1$$ using the binomial theorem on the second to last equality. ## Retrograde If you have two planets in circular orbits with semi-major axes$a_1$and$a_2$, and periods$T_1$and$T_2$, then the amount of time spent in retrograde is $$T_\mathrm{retro} = T_1\left|\frac{\cos ^{-1}\left(\frac{\sqrt{r}+1}{r+\frac{1}{\sqrt{r}}}\right)}{\pi\left(1-\frac{1}{r^{3/2}}\right)}\right|$$ where$r= \frac{a_2}{a_1}$. You can apply this formula to the planets in our solar system to get approximations for their time in retrograde as seen from Earth. (These approximations are often within 10% of the correct value, but sometimes the error is larger.) $$\begin{array}{ccc} & \text{a in AU} & T_{\text{retro}} \\ \text{Mercury} & 0.387 & 23 \text{ days} \\ \text{Venus} & 0.723 & 42 \text{ days} \\ \text{Mars} & 1.524 & 73 \text{ days} \\ \text{Jupiter} & 5.204 & 121 \text{ days} \\ \text{Saturn} & 9.54 & 138 \text{ days} \\ \text{Uranus} & 19.19 & 152 \text{ days} \\ \text{Neptune} & 30.05 & 158 \text{ days} \\ \end{array}$$ It’s interesting to note that: •$\lim_{r\rightarrow\infty} T_\mathrm{retro}(r) = T_1/2$, •$\lim_{r\rightarrow 1} T_\mathrm{retro}(r) = T_1\cdot \frac{\sqrt{2}}{3 \pi }$, and •$\lim_{r\rightarrow 0} \frac{T_\mathrm{retro}(r)}{r^{3/2}T_1}=1/2\$. If people are interested, I can post a derivation.

If people are interested, I can post a derivation.