Search code examples
pythonleast-squares

optimize common parameters across several equations using least squares in python


I would like to optimize common parameters among several equations, but i dont know how to fit them simultaneously.

The problem is essentially like this, with four equations to solve and three parameters to optimize:

a+b+c+1750=T

12=a/T*100

15=b/T*100

37=c/T*100

where I would like to find the optimal values of a,b, and c. Does anybody have a suggestion, perhaps using a least squares method? I am only familiar when there is but one equation to solve.


Solution

  • It seems your equations actually have 4 parameters, a, b, c and T, so you have a set of 4 linear equations with 4 parameters:

    a + b + c - T = -1750
    100 * a - 12 * Y = 0
    100 * b - 15 * Y = 0
    100 * c - 37 * Y = 0
    

    You can solve this using the coefficients in a matrix:

    import numpy as np
    
    a = np.array([[1., 1., 1., -1.],
                  [100., 0, 0, -12.],
                  [0, 100., 0, -15.],
                  [0, 0, 100., -37.]])
    b = np.array([-1750., 0, 0, 0])
    

    If there is an analytical solution, you can use

    res = np.linalg.solve(a, b)
    # res: [  583.33333333   729.16666667  1798.61111111  4861.11111111]
    

    Otherwise (or for a more general case) you can approximate a solution using least-squared algorithm

    res, err, _, __ = np.linalg.lstsq(a, b)
    # res: [  583.33333333   729.16666667  1798.61111111  4861.11111111]