Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

What is wrong with this Matlab script. It produces errors. clearvars; clc; mu = [0.02;…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    What is wrong with this Matlab script. It produces errors.

    clearvars; clc;

    mu = [0.02; 0.06; 0.07]; sigma = [0.02; 0.04; 0.07]; Sigma = sigma * sigma';

    H = Sigma; f = zeros(3, 1); A = -mu'; b = -0.05; % Target return of 5% Aeq = [1, 1, 1]; beq = 1; lb = [0; 0; 0]; ub = [1; 1; 1];

    options = optimoptions('quadprog', 'Display', 'off');

    [w, ~, exitflag] = quadprog(H, f, A, b, Aeq, beq, lb, ub)

  2. Qwen Assistant

    The provided MATLAB script contains a subtle issue related to the structure of the covariance matrix Sigma, which leads to an ill-conditioned problem for the quadprog solver. Here's a breakdown of the problem and its resolution:


    Key Issue: Rank-Deficient Covariance Matrix

    The covariance matrix Sigma is defined as sigma * sigma', where sigma is a 3x1 vector. This results in a rank-1 matrix (since it's an outer product of a vector with itself). While mathematically valid, this makes Sigma positive semi-definite but not positive definite, which can cause numerical instability in solvers like quadprog.


    Why This Causes Problems

    • Positive Semi-Definite (PSD) Matrices: While quadprog can handle PSD matrices, the solver may struggle with singular or near-singular matrices, especially in constrained optimization problems.
    • Quadratic Objective: The objective function 0.5 * x' * H * x becomes a perfect square (sigma' * x)^2, which is flat in certain directions, leading to potential solver failures or non-unique solutions.

    Solution: Add a Small Diagonal Perturbation

    To ensure the matrix is positive definite, add a small value (e.g., 1e-6) to the diagonal of Sigma. This is a common practice in portfolio optimization to avoid singularity.

    mu = [0.02; 0.06; 0.07];
    sigma = [0.02; 0.04; 0.07];
    Sigma = sigma * sigma' + 1e-6 * eye(3);  % Add small diagonal perturbation
    H = Sigma;
    f = zeros(3, 1);
    A = -mu';
    b = -0.05;
    Aeq = [1, 1, 1];
    beq = 1;
    lb = [0; 0; 0];
    ub = [1; 1; 1];
    
    options = optimoptions('quadprog', 'Display', 'off');
    [w, ~, exitflag] = quadprog(H, f, A, b, Aeq, beq, lb, ub);
    

    Additional Notes

    • Feasibility: The problem is feasible (e.g., x = [0; 0; 1] satisfies all constraints).
    • Solver Behavior: Even with the corrected H, quadprog may return a solution, but the exit flag (exitflag) should be checked to confirm success.
    • Alternative Approach: If the problem persists, consider using a different solver (e.g., fmincon) or reformulating the problem to avoid rank-deficient matrices.

    Final Answer

    The error arises because Sigma is a rank-1 matrix (not positive definite), causing numerical instability in quadprog. To fix this, add a small diagonal perturbation to Sigma to ensure it is positive definite:

    Sigma = sigma * sigma' + 1e-6 * eye(3);
    

    This adjustment ensures the solver can handle the quadratic optimization problem effectively.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.