function ThetaPhiPartial = PartialThetaPartialPhi(phi,thetaIndex,UniformBounds)
% PartialThetaPartialPhi: Computes a diagonal matrix with the partial derivatives of the untransformed parameters
%                         with respect to the the transformed parameters
%
% USAGE:
%
%       ThetaPhiPartial = PartialThetaPartialPhi(phi,thetaIndex,UniformBounds)
%
% REQUIRED INPUT:  phi (vector) with values of the transformed parameters.
%
%                  thetaIndex (vector) with integers where 1=gamma, invgamma, truncnormal 2=beta, and 0=normal.
%                             This is used to determine the type of transformation that is performed on the
%                             theta parameters.
%
%                  UniformBounds (matrix) with the same number of rows as parameters and 2 columns. The columns
%                             are 0 1 except for the uniform and the beta distribution, where they represent
%                             the upper and lower bounds.
%
% REQUIRED OUTPUT: ThetaPhiPartial (matrix), a diagonal matrix with the partial derivatives along the diagonal
%
%
%                       Written by: Anders Warne
%                                   New Area Wide Model Project
%                                   DG-R/EMO
%                                   European Central Bank (ECB)
%                                   Email: anders.warne@ecb.europa.eu
%                                   Copyright © 2006-2008 European Central Bank.
%
%                       First version: August 14, 2006.
%                        This version: December 18, 2008.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% LICENSE INFORMATION:
%
%      YADA is free software: you can redistribute it and/or modify
%      it under the terms of the GNU General Public License as published by
%      the Free Software Foundation, either version 3 of the License, or
%      (at your option) any later version.
%
%      This program is distributed in the hope that it will be useful,
%      but WITHOUT ANY WARRANTY; without even the implied warranty of
%      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%      GNU General Public License for more details.
%
%      You should have received a copy of the GNU General Public License
%      along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%      YADA is released under the GNU General Public License, Version 3,
%      29 June 2007 <http://www.gnu.org/licenses/>. The current release of
%      the program was last modified by the ECB on the "This version" date
%      above.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% CHANGELOG:
%
% * 25-8-2006:  Added the input argument UniformBounds needed to handle parameters with a uniform prior.
%
% * 13-11-2007: Updated the documentation.
%
% * 23-5-2008:  Updated the documentation.
%
% * 23-7-2008:  Took into account that the beta distribution also support lower and upper bounds different
%               from 0 and 1. Fixed a bug where the columns of UniformBounds were added instead of taking
%               the upper (column 2) minus the lower (column 1).
%
% * 18-12-2008: Updated the documentation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

p = length(phi);
tpvec = (ones(p,1) .* (thetaIndex==0)) + (exp(phi.*(thetaIndex==1)) .* (thetaIndex==1)) + ( ((UniformBounds(:,2)-UniformBounds(:,1)) .* exp(phi.*((thetaIndex==2)+(thetaIndex==3))) .* ((thetaIndex==2)+(thetaIndex==3))) ./ ((1+(exp(phi.*((thetaIndex==2)+(thetaIndex==3))) .* ((thetaIndex==2)+(thetaIndex==3)))).^2) );
ThetaPhiPartial = diag(tpvec);

%
% end of PartialThetaPartialPhi.m
%
