function [supply, demand, total, Lambda_hat, xi] = outputresponse(mu, theta, sigma_c, sigma, Omega, alpha, beta, gamma, varphi)

%OUTPUTRESPONSE  Supply- and demand-side decomposition of the output
%                response to a monetary shock (Proposition 2).
%
%   [supply, demand] = outputresponse(mu, theta, sigma_c, sigma, Omega, ...
%                                     alpha, beta, gamma, varphi)
%
%   Proposition 2 of the paper decomposes the elasticity of real final
%   output with respect to the money supply into a supply-side effect (the
%   TFP response, operating through the misallocation channel) and a
%   demand-side effect (the labor response to nominal rigidities):
%
%       dlogY/dlogM = dlogTFP/dlogM + dlogL/dlogM
%                   = [1 - Lambda_hat + varphi*(1 - Lambda_hat - xi)] / D
%
%   with
%
%       dlogTFP/dlogM = (gamma + varphi)*(1 - Lambda_hat - xi) / D
%       dlogL/dlogM   = [1 - Lambda_hat - gamma*(1 - Lambda_hat - xi)] / D
%       D             = 1 + (gamma - 1)*Lambda_hat + varphi*(1 - xi)
%
%   where
%
%       Lambda_hat = E_b[Psi_hat_iL]  is the wage pass-through to the
%                    consumer price, i.e. the final-expenditure weighted
%                    average of the sticky-price labor Leontief column;
%       xi         = dlogLambda_L/dlogw = 1 - Lambda_hat - dlogTFP/dlogw.
%
%   By the definition of xi, the numerator term (1 - Lambda_hat - xi) is
%   exactly the sufficient statistic of Proposition 1,
%
%       dlogTFP/dlogw = (1/Lambda_L) * sum_j (sigma_j*lambda_j/mu_j) * ...
%                       Cov_Omega_tilde(j,:)(Psi_hat_iL, Psi_iL),
%
%   which is what supplyside.m returns as its first output. This function
%   therefore calls supplyside.m rather than duplicating that computation.
%   The denominator D is positive under the maintained assumption xi <= 1
%   (see the proof of Lemma "isomorphism between money supply and wage").
%
%   Inputs are the same as for supplyside.m, plus the preference parameters:
%       mu       N x 1   initial (steady state) markups
%       theta    N x 1   probability of price adjustment (price flexibility)
%       sigma_c  scalar  elasticity of substitution in consumption
%       sigma    N x 1   elasticities of substitution in production
%       Omega    N x N   cost-based intermediate input shares
%       alpha    N x 1   cost-based labor shares
%       beta     N x 1   final expenditure shares
%       gamma    scalar  inverse elasticity of intertemporal substitution
%                        (strength of the wealth effect on labor supply)
%       varphi   scalar  inverse Frisch elasticity of labor supply
%
%   Outputs:
%       supply      dlogTFP/dlogM, the supply-side effect
%       demand      dlogL/dlogM,   the demand-side effect
%       total       dlogY/dlogM,   the total output response
%       Lambda_hat  wage pass-through to the consumer price
%       xi          elasticity of the labor income share to the wage
%
%   See also SUPPLYSIDE, OPTIMALPOLICY.


    N = length(Omega);

    % Supply-side effect per unit of nominal wage, dlogTFP/dlogw. This is
    % the sufficient statistic of Proposition 1 and equals 1-Lambda_hat-xi.
    SSE_w                 = supplyside(mu, theta, sigma_c, sigma, Omega, alpha, beta);

    % Sticky-price IO variables (same construction as in supplyside.m and
    % optimalpolicy.m: the economy is augmented with labor as sector N+1,
    % which households do not consume and which uses no inputs).

    beta                  = [beta;0];
    Omega_tilde           = [Omega, alpha; zeros(1,N+1)];
    theta_trans           = [theta;1];
    Psi_hat               = (eye(N+1)-diag(theta_trans) * Omega_tilde)^(-1);
    Domar_hat             = (beta' * Psi_hat)';
    Lambda_hat            = Domar_hat(N+1);

    % Elasticity of the labor income share to the nominal wage
    xi                    = 1 - Lambda_hat - SSE_w;

    % Denominator of Proposition 2, dlogM/dlogw up to the factor gamma+varphi
    D                     = 1 + (gamma - 1) * Lambda_hat + varphi * (1 - xi);

    % Supply-side effect: dlogTFP/dlogM
    supply                = (gamma + varphi) * SSE_w / D;

    % Demand-side effect: dlogL/dlogM
    demand                = (1 - Lambda_hat - gamma * SSE_w) / D;

    % Total output response: dlogY/dlogM
    total                 = supply + demand;


end
