# Did I derive the Kelly criterion correctly?

$$\frac{dX_t}{X_t}=\alpha\frac{dS_t}{S_t}+(1-\alpha)\frac{dS^0_t}{S^0_t}$$

where $$\alpha$$ is proportion of the investment in the risky asset $$S_t$$ and $$S^0_t$$ is the risk-free asset. $$S_t$$ follows a geometric Brownian motion,

\begin{aligned} \frac{dS_t}{S_t} &= \mu{dt} + \sigma{dW_t} \\ \frac{dS^0_t}{S^0_t} &= r dt \end{aligned}

Substituting the equation, we get

$$\frac{dX_t}{X_t}=(\alpha\mu+(1-\alpha)r)dt+\alpha\sigma{dW_t}$$

Solving the following SDE yields

$$X_t = X_0 \exp\left(\left(\alpha\mu + (1-\alpha) r -\frac{(\alpha\mu)^2}{2}\right)t+\sigma{W_t}\right)$$

So we have to maximize $$\alpha\mu+(1-\alpha)r-\frac{(\alpha\mu)^2}{2}$$. Differentiating the above expression $$\mu-r-\alpha\sigma^2$$, so $$\alpha=\frac{\mu-r}{\sigma^2}$$. I think there might be an error in my derivation. I looked at this part and felt that something was off.

$$X_t = X_0 \exp\left(\left(\alpha\mu+(1-\alpha)r-\frac{(\alpha\mu)^2}{2}\right)t+\sigma{W_t}\right)$$

Did I derive the Kelly formula correctly?

Might be a typo but you dropped the $$\alpha$$ on the noise term after solving the SDE: in $$\exp(...)$$ you should have $$\alpha \sigma W_t$$ instead of $$\sigma W_t$$. For deriving the Kelly criterion, it won't matter since we will take the mean and this will vanish (see below). But in simulations this is important to get right, since your investment fraction $$\alpha$$ will scale the volatility $$\sigma$$ you're getting from the asset $$S$$.
Let $$g(\alpha) = r+(\mu-r)\alpha-\frac12 \sigma^2 \alpha^2$$. This is just your expression, with some rearrangement. Your solution $$X_t$$ can now be written (with the above note in mind) as $$X_t = X_0 \exp(g(\alpha)t+\sigma \alpha W_t).$$ The Kelly criterion says to maximize the expected log utility: $$f(\alpha,t,x) = \mathbb{E}(\log X_t|X_0=x),$$ Taking logarithms gives $$\log X_t = \log X_0 +g(\alpha) t +\sigma \alpha W_t$$ Taking the expectation conditional on $$X_0=x$$, gives $$f(\alpha,t,x)=\log x +g(\alpha) t+0,$$ Differentiating this with respect to $$\alpha$$ gives $$\frac{\partial f}{\partial \alpha} = g'(\alpha)t$$ which is equal to zero if and only if $$g'(\alpha)=0$$ (we assume $$t>0$$). Computing $$g'(\alpha)$$ from the definition and setting it equal to zero means we have to solve $$g'(\alpha) = \mu-r-\frac12 \sigma^2 \alpha=0,$$ which gives the solution $$\alpha^* = (\mu-r)/\sigma^2$$ as desired. Your derivation is essentially the same but omits some of reasoning that would justify the steps (1. taking logs allows us to focus on the argument to $$\exp$$, 2. taking means makes the noise term vanish, so we just have to maximize the drift part).