(B1, Y_res, C1, R, n1, m1, maxiter, lambdax)
return (Y_i, Y_res, C1)
Compute first order sensitivities.
Compute first order sensitivities.
Compute first order sensitivities .
def _first_order(B1, Y_res, C1, R, n1, m1, maxiter, lambdax):
    """Compute first order sensitivities."""
    Y_i = np.zeros((R, n1))       # Initialize 1st order contributions
    T1 = np.zeros((m1, R, n1))    # Initialize T(emporary) matrix - 1st
    it = 0                        # Initialize iteration counter

    # First order individual estimation
    for j in range(n1):
        # Regularized least squares inversion ( minimize || C1 ||_2 )
        B11 = np.matmul(np.transpose(B1[:, :, j]), B1[:, :, j])

        # if it is ill-conditioned matrix, the default value is zero
        if np.all(np.linalg.svd(B11)[1]): # sigma, diagonal matrix, from svd
            T1[:, :, j] = np.linalg.solve(np.add(B11, np.multiply(
                lambdax, np.identity(m1))), np.transpose(B1[:, :, j]))

        C1[:, j] = np.matmul(T1[:, :, j], Y_res).reshape(m1,)
        Y_i[:, j] = np.matmul(B1[:, :, j], C1[:, j])

    # Backfitting Method
    var1b_old = np.sum(np.square(C1), axis=0)
    varmax = 1
    while (varmax > 1e-3) and (it < maxiter):
        for j in range(n1):
            Y_r = Y_res
            for z in range(n1):
                if j != z:
                    Y_r = np.subtract(Y_r, np.matmul(
                        B1[:, :, z], C1[:, z]).reshape(R, 1))

            C1[:, j] = np.matmul(T1[:, :, j], Y_r).reshape(m1,)

        var1b_new = np.sum(np.square(C1), axis=0)
        varmax = np.max(np.absolute(np.subtract(var1b_new, var1b_old)))
        var1b_old = var1b_new
        it += 1

    # Now compute first-order terms
    for j in range(n1):
        Y_i[:, j] = np.matmul(B1[:, :, j], C1[:, j])

        # Subtract each first order term from residuals
        Y_res = np.subtract(Y_res, Y_i[:, j].reshape(R, 1))

    return (Y_i, Y_res, C1)
def _first_order ( B1 , Y_res , C1 , R , n1 , m1 , maxiter , lambdax ) : Y_i = np . zeros ( ( R , n1 ) ) # Initialize 1st order contributions T1 = np . zeros ( ( m1 , R , n1 ) ) # Initialize T(emporary) matrix - 1st it = 0 # Initialize iteration counter # First order individual estimation for j in range ( n1 ) : # Regularized least squares inversion ( minimize || C1 ||_2 ) B11 = np . matmul ( np . transpose ( B1 [ : , : , j ] ) , B1 [ : , : , j ] ) # if it is ill-conditioned matrix, the default value is zero if np . all ( np . linalg . svd ( B11 ) [ 1 ] ) : # sigma, diagonal matrix, from svd T1 [ : , : , j ] = np . linalg . solve ( np . add ( B11 , np . multiply ( lambdax , np . identity ( m1 ) ) ) , np . transpose ( B1 [ : , : , j ] ) ) C1 [ : , j ] = np . matmul ( T1 [ : , : , j ] , Y_res ) . reshape ( m1 , ) Y_i [ : , j ] = np . matmul ( B1 [ : , : , j ] , C1 [ : , j ] ) # Backfitting Method var1b_old = np . sum ( np . square ( C1 ) , axis = 0 ) varmax = 1 while ( varmax > 1e-3 ) and ( it < maxiter ) : for j in range ( n1 ) : Y_r = Y_res for z in range ( n1 ) : if j != z : Y_r = np . subtract ( Y_r , np . matmul ( B1 [ : , : , z ] , C1 [ : , z ] ) . reshape ( R , 1 ) ) C1 [ : , j ] = np . matmul ( T1 [ : , : , j ] , Y_r ) . reshape ( m1 , ) var1b_new = np . sum ( np . square ( C1 ) , axis = 0 ) varmax = np . max ( np . absolute ( np . subtract ( var1b_new , var1b_old ) ) ) var1b_old = var1b_new it += 1 # Now compute first-order terms for j in range ( n1 ) : Y_i [ : , j ] = np . matmul ( B1 [ : , : , j ] , C1 [ : , j ] ) # Subtract each first order term from residuals Y_res = np . subtract ( Y_res , Y_i [ : , j ] . reshape ( R , 1 ) ) return ( Y_i , Y_res , C1 )