1function [yy, xdir, isig, lam]=log_trans_(y0,xdir0,isig,lam)
2
3% Written by Marco Ratto
4% Joint Research Centre, The European Commission,
5% marco.ratto@ec.europa.eu
6
7% Copyright (C) 2012 European Commission
8% Copyright (C) 2012-2017 Dynare Team
9%
10% This file is part of Dynare.
11%
12% Dynare is free software: you can redistribute it and/or modify
13% it under the terms of the GNU General Public License as published by
14% the Free Software Foundation, either version 3 of the License, or
15% (at your option) any later version.
16%
17% Dynare is distributed in the hope that it will be useful,
18% but WITHOUT ANY WARRANTY; without even the implied warranty of
19% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20% GNU General Public License for more details.
21%
22% You should have received a copy of the GNU General Public License
23% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
24
25if nargin==4
26    % inverse transformation
27    yy = (exp(y0)-lam)*isig;
28    return
29end
30
31if nargin==1
32    xdir0='';
33end
34f=@(lam,y)gsa_skewness(log(y+lam));
35isig=1;
36if ~(max(y0)<0 | min(y0)>0)
37    if gsa_skewness(y0)<0,
38        isig=-1;
39        y0=-y0;
40    end
41    n=hist(y0,10);
42    if n(1)>20*n(end)
43        try
44            lam=fzero(f,[-min(y0)+10*eps -min(y0)+abs(median(y0))],[],y0);
45        catch
46            yl(1)=f(-min(y0)+10*eps,y0);
47            yl(2)=f(-min(y0)+abs(median(y0)),y0);
48            if abs(yl(1))<abs(yl(2))
49                lam=-min(y0)+eps;
50            else
51                lam = -min(y0)+abs(median(y0)); %abs(100*(1+min(y0)));
52            end
53        end
54        yy = log(y0+lam);
55        xdir=[xdir0,'_logskew'];
56    else
57        isig=0;
58        lam=0;
59        yy = log(y0.^2);
60        xdir=[xdir0,'_logsquared'];
61    end
62else
63    if max(y0)<0
64        isig=-1;
65        y0=-y0;
66        %yy=log(-y0);
67        xdir=[xdir0,'_minuslog'];
68    elseif min(y0)>0
69        %yy=log(y0);
70        xdir=[xdir0,'_log'];
71    end
72    try
73        lam=fzero(f,[-min(y0)+10*eps -min(y0)+median(y0)],[],y0);
74    catch
75        yl(1)=f(-min(y0)+10*eps,y0);
76        yl(2)=f(-min(y0)+abs(median(y0)),y0);
77        if abs(yl(1))<abs(yl(2))
78            lam=-min(y0)+eps;
79        else
80            lam = -min(y0)+abs(median(y0)); %abs(100*(1+min(y0)));
81        end
82    end
83    lam = max(lam,0);
84    yy = log(y0+lam);
85end
86