Hi Lucas, There is a closed formula for this, which I implemented in IQ-TREE. Have a look at phylotesting.cpp. For your convenience I paste here the method (copied from the comments in the code):
/*
Problem: solve the following linear system equation:
a_1*x + b_1*y = c_1
a_2*x + b_2*y = c_2
....
a_n*x + b_n*y = c_n
becomes minimizing weighted least square:
sum_k { w_k*[ c_k - (a_k*x + b_k*y) ]^2 }
the solution is:
x = [(sum_k w_k*b_k*c_k)*(sum_k w_k*a_k*b_k) - (sum_k w_k*a_k*c_k)(sum_k w_k*b_k^2)] /
[ (sum_k w_k*a_k*b_k)^2 - (sum_k w_k*a_k^2)*(sum_k w_k*b_k^2) ]
y = [(sum_k w_k*a_k*c_k)*(sum_k w_k*a_k*b_k) - (sum_k w_k*b_k*c_k)(sum_k w_k*a_k^2)] /
[ (sum_k w_k*a_k*b_k)^2 - (sum_k w_k*a_k^2)*(sum_k w*k*b_k^2) ]
@param n number of data points
@param w weight vector of length n
@param a a value vector of length n
@param b b value vector of length n
@param c c value vector of length n
@param[out] x x-value
@param[out] y y-value
@return least square value
*/
Cheers,
Minh