Skip to content

try and remove cvxpy dependency #112

Closed

Description

@sbillinge
No description provided.

Activity

  1. added this to the 0.1.4 milestone on Oct 21, 2024
  2. sbillinge commented on Oct 21, 2024

    @sbillinge
    ContributorAuthor

    this is the solution suggested to Simon by chatgpt but it needs cleaning and testing:

    You can rewrite this optimization problem using scipy.optimize.minimize, which handles constrained optimization problems. The function minimize allows specifying bounds for variables, which can replace the constraints in cvxpy.

    Here's the rewritten version of the function using scipy.optimize.minimize:

    python

    import numpy as np
    from scipy.optimize import minimize
    
    def get_weights(stretched_component_gram_matrix, linear_coefficient, lower_bound, upper_bound):
        """
        Finds the weights of stretched component signals under a two-sided constraint.
    
        Solves min J(y) = (linear_coefficient)' * y + (1/2) * y' * (quadratic coefficient) * y where
        lower_bound <= y <= upper_bound and stretched_component_gram_matrix is symmetric positive definite.
    
        Parameters
        ----------
        stretched_component_gram_matrix: 2d array like
            The Gram matrix constructed from the stretched component matrix. It is a square positive definite matrix.
    
        linear_coefficient: 1d array like
            The vector containing the product of the stretched component matrix and the transpose of the observed data matrix.
    
        lower_bound: 1d array like
            The lower bound on the values of the output weights.
    
        upper_bound: 1d array like
            The upper bound on the values of the output weights.
    
        Returns
        -------
        1d array like
            The vector containing the weightings of the components.
        """
        stretched_component_gram_matrix = np.asarray(stretched_component_gram_matrix)
        linear_coefficient = np.asarray(linear_coefficient)
        lower_bound = np.asarray(lower_bound)
        upper_bound = np.asarray(upper_bound)
    
        # Objective function: J(y) = 0.5 * y.T @ stretched_component_gram_matrix @ y + linear_coefficient.T @ y
        def objective_function(y):
            return 0.5 * np.dot(y.T, np.dot(stretched_component_gram_matrix, y)) + np.dot(linear_coefficient.T, y)
    
        # Define the bounds for each element of y
        bounds = [(lb, ub) for lb, ub in zip(lower_bound, upper_bound)]
    
        # Initial guess for the solution
        initial_guess = np.zeros_like(linear_coefficient)
    
        # Use scipy.optimize.minimize with the L-BFGS-B method (supports bounds)
        result = minimize(objective_function, initial_guess, method='L-BFGS-B', bounds=bounds)
    
        # Return the optimized weights (solution)
        return result.x
    

    Explanation:

    Objective function: We defined a function objective_function representing the quadratic cost function J(y)=0.5∗yT∗Q∗y+cT∗yJ(y)=0.5∗yT∗Q∗y+cT∗y, where stretched_component_gram_matrix is QQ and linear_coefficient is cc.
    Bounds: The bounds are provided as a list of tuples (lower_bound, upper_bound) for each element of y.
    Optimization: We use the L-BFGS-B algorithm, which supports bound-constrained optimization, to solve this problem.
    Initial guess: The initial guess is set to a zero vector, but this could be adjusted based on the problem.
    

    This should provide a solution similar to your cvxpy version but using scipy.optimize.

  3. bobleesj commented on Oct 21, 2024

    @bobleesj
    Contributor

    Just to add another reason discussed is that cvxpy as of now does not support Python 3.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions