Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

Wirte me a Matlab code to compute the efficient frontier for 3 assets. The assets are…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Googlegemma-3n-e4b-it’s answer.
  1. User

    Wirte me a Matlab code to compute the efficient frontier for 3 assets. The assets are perfectly correlated, have returns 2%, 6%, 7% and volatilities of 2%, 4%, and 7%. As output I want the frontier with the assets shown on the same graph, but also a chart with the portfolio composition along the frontier.

  2. Claude Assistant

    I'll create a MATLAB code to compute and visualize the efficient frontier for your three assets. Note that since the assets are perfectly correlated, the efficient frontier will be linear rather than curved.

    % Clear workspace and close figures
    clear all; close all; clc;
    
    % Asset parameters
    returns = [0.02; 0.06; 0.07];  % Expected returns
    volatilities = [0.02; 0.04; 0.07];  % Standard deviations
    n_assets = length(returns);
    
    % Correlation matrix (perfectly correlated)
    correlation = ones(n_assets);
    
    % Covariance matrix
    covariance = zeros(n_assets);
    for i = 1:n_assets
        for j = 1:n_assets
            covariance(i,j) = correlation(i,j) * volatilities(i) * volatilities(j);
        end
    end
    
    % Generate target returns for the efficient frontier
    n_points = 100;
    target_returns = linspace(min(returns), max(returns), n_points);
    
    % Initialize arrays for results
    frontier_volatility = zeros(n_points, 1);
    frontier_weights = zeros(n_points, n_assets);
    
    % Compute efficient frontier
    for i = 1:n_points
        % Minimize portfolio variance subject to target return
        % min w'*Cov*w subject to w'*r = target_return and sum(w) = 1
        
        % Quadratic programming formulation
        H = 2 * covariance;
        f = zeros(n_assets, 1);
        
        % Equality constraints: Aeq*w = beq
        Aeq = [returns'; ones(1, n_assets)];
        beq = [target_returns(i); 1];
        
        % No short-selling constraint (optional - remove if short-selling allowed)
        lb = zeros(n_assets, 1);
        ub = ones(n_assets, 1);
        
        % Solve optimization
        options = optimoptions('quadprog', 'Display', 'off');
        [w, fval] = quadprog(H, f, [], [], Aeq, beq, lb, ub, [], options);
        
        % Store results
        frontier_weights(i, :) = w';
        frontier_volatility(i) = sqrt(w' * covariance * w);
    end
    
    % Figure 1: Efficient Frontier with Individual Assets
    figure('Position', [100, 100, 800, 600]);
    hold on;
    
    % Plot efficient frontier
    plot(frontier_volatility * 100, target_returns * 100, 'b-', 'LineWidth', 2);
    
    % Plot individual assets
    scatter(volatilities * 100, returns * 100, 100, 'filled');
    text(volatilities(1)*100 + 0.2, returns(1)*100, 'Asset 1', 'FontSize', 12);
    text(volatilities(2)*100 + 0.2, returns(2)*100, 'Asset 2', 'FontSize', 12);
    text(volatilities(3)*100 + 0.2, returns(3)*100, 'Asset 3', 'FontSize', 12);
    
    % Labels and formatting
    xlabel('Volatility (%)', 'FontSize', 14);
    ylabel('Expected Return (%)', 'FontSize', 14);
    title('Efficient Frontier with Individual Assets', 'FontSize', 16);
    grid on;
    legend('Efficient Frontier', 'Individual Assets', 'Location', 'northwest');
    hold off;
    
    % Figure 2: Portfolio Composition Along the Frontier
    figure('Position', [950, 100, 800, 600]);
    
    % Create area plot for portfolio weights
    area(target_returns * 100, frontier_weights * 100);
    
    % Labels and formatting
    xlabel('Expected Return (%)', 'FontSize', 14);
    ylabel('Portfolio Weight (%)', 'FontSize', 14);
    title('Portfolio Composition Along the Efficient Frontier', 'FontSize', 16);
    legend('Asset 1', 'Asset 2', 'Asset 3', 'Location', 'eastoutside');
    grid on;
    ylim([0, 100]);
    
    % Display some key statistics
    fprintf('Asset Statistics:\n');
    fprintf('================\n');
    for i = 1:n_assets
        fprintf('Asset %d: Return = %.1f%%, Volatility = %.1f%%\n', ...
            i, returns(i)*100, volatilities(i)*100);
    end
    
    fprintf('\nNote: Since assets are perfectly correlated, the efficient frontier is linear.\n');
    
    % Optional: Display portfolio weights at specific return levels
    fprintf('\nPortfolio Weights at Key Points:\n');
    fprintf('================================\n');
    key_returns = [0.02, 0.04, 0.06, 0.07];
    for r = key_returns
        idx = find(abs(target_returns - r) < 0.0001, 1);
        if ~isempty(idx)
            fprintf('Return = %.1f%%: ', r*100);
            fprintf('w1 = %.1f%%, w2 = %.1f%%, w3 = %.1f%%\n', ...
                frontier_weights(idx,:)*100);
        end
    end
    

    This code will generate two figures:

    1. Efficient Frontier Plot: Shows the efficient frontier as a line with the three individual assets plotted as points. Since the assets are perfectly correlated, the frontier will be a straight line connecting the assets.

    2. Portfolio Composition Chart: An area chart showing how the portfolio weights change as you move along the efficient frontier from the lowest return to the highest return.

    Key points about the code:

    • Uses quadratic programming to solve the mean-variance optimization problem
    • Includes a no short-selling constraint (you can remove the lb and ub constraints if you want to allow short positions)
    • Since the assets are perfectly correlated, the efficient frontier will be linear, consisting of combinations of only two assets at any point
    • The portfolio composition will show a transition from Asset 1 (lowest risk/return) to Asset 3 (highest risk/return)

    The perfect correlation means that diversification benefits are eliminated, and the optimal portfolios will typically consist of combinations of adjacent assets on the risk-return spectrum.

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