Suppose I have an investment portfolio made up of $n$ stocks each worth $p_i$, with the count of each being $c_i$, where $i \in [1, n]$.
Let's say I've found that the optimal/target split of each asset, as a percent of my whole portfolio, is $g_i$ for each stock $i$, where the current split is $f_i$. I have recently gotten some more cash (call the amount $\Delta$) to buy stock with. Assuming that stock price has drifted somewhat, how do I determine how much more of each stock I need to buy ($\delta_i$) to realign my portfolio to the target split?
For the $n=2$ case I've determined the following but I'm wondering how this could be generalised beyond that.
$$ \begin{aligned} V &= c_1p_1+c_2p_2 \\ V+\Delta &= (c_1+\delta_1)p_1+(c_2+\delta_2)p_2\\ \therefore \Delta &= \delta_1p_1 + \delta_2p_2 & (1) \\ \end{aligned} $$
$$ \begin{aligned} \because f_i = \frac{c_i p_i}{\sum_k c_k p_k}, g_i = \frac{(c_i+\delta_i)p_i}{\sum_k (c_k+\delta_k)p_k} \\ \frac{g_if_j}{g_jf_i} = \frac{(n_i+\delta_i)n_j}{(n_j+\delta_j)n_i} = \frac{1+\frac{\delta_i}{n_i}}{1+\frac{\delta_j}{n_j}} \\ g_i f_j \left(1 + \frac{\delta_j}{n_j}\right) = f_i g_j \left(1 + \frac{\delta_i}{n_i}\right) \\ g_if_j - f_ig_j = f_ig_jn_i^{-1}\delta_i - f_jg_in_j^{-1}\delta_j \\ g_1f_2 - f_1g_2 = f_1g_2n_1^{-1}\delta_1 - f_2g_1n_2^{-1}\delta_2 && (2) \\ \end{aligned} $$ From (1) and (2): $$ \begin{bmatrix} p_1 & p_2 \\\ f_1g_2n_1^{-1} & -f_2g_1n_2^{-1}\end{bmatrix} \begin{bmatrix} \delta_1 \\\ \delta_2 \end{bmatrix} = \begin{bmatrix} \Delta \\\ g_1f_2 - f_1g_2 \end{bmatrix} \\ \begin{bmatrix} \delta_1 \\\ \delta_2 \end{bmatrix} = \begin{bmatrix} p_1 & p_2 \\\ f_1g_2n_1^{-1} & -f_2g_1n_2^{-1}\end{bmatrix} ^{-1} \begin{bmatrix} \Delta \\\ g_1f_2 - f_1g_2 \end{bmatrix} $$
Does the presence of $g_1f_2 - f_1g_2$ suggest I should be looking for a matrix determinant if I want to generalise the formula e.g. $\begin{vmatrix}g_1 & f_1 \\\ g_2 & f_2 \end{vmatrix}$?
Also I realise that this question exists but it doesn't really answer my case in the way I'm looking for.
I think you are complicating things a bit.
Calculate your target allocations....
$(V+\Delta)\mathbf g$ and subtract your current allocations $V\mathbf f$
To get the values of the shares you need to purchase.
i.e.
$V\mathbf f = (c_1p_1, \cdots, c_np_n)\\ (V+\Delta)\mathbf g = (c_1p_1 + \delta_1 p_1,\cdots, c_np_n + \delta_np_n)$
$(V+\Delta)\mathbf g - V\mathbf f = (\delta_1 p_1,\cdots, \delta_n p_n)$
And the number of shares for each transaction is then $\delta_i = \frac {(V+\Delta)g_i - Vf_i}{p_i}$