-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_yeojohnson.m
77 lines (73 loc) · 2.51 KB
/
palm_yeojohnson.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function varargout = palm_yeojohnson(varargin)
% Find the lambda parameter and compute the Yeo-Johnson transformation.
%
% Usage:
% [Y,L] = palm_yeojohnson(X)
% Y = palm_yeojohnson(X,L)
%
% X : Input data.
% Y : Transformed data.
% L : Lambda parameter.
%
% Reference:
% Yeo IK and Johnson RA. A new family of power transformations to
% improve normality or symmetry. Biometrika. 2000;87(4):954-9.
% _____________________________________
% Anderson M. Winkler
% Jul/2017
% http://brainder.org
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PALM -- Permutation Analysis of Linear Models
% Copyright (C) 2015 Anderson M. Winkler
%
% This program 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
% 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/>.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X = varargin{1};
if nargin == 1,
Lfun = @(L)loglike(X,L);
L = fminsearch(Lfun,1,optimset('Display','off','MaxFunEvals',1000));
varargout{1} = yjt(X,L);
else
L = varargin{2};
varargout{1} = yjt(X,L);
end
if nargout == 2,
varargout{2} = L;
end
% ==============================================================
function LL = loglike(X,L)
% Log-likelihood function (eq 3.1 of the paper), with the
% sign reversed to be minimised.
Y = yjt(X,L);
N = numel(X);
mu = mean(Y);
sg2 = var(Y,1);
LL = N*(log(2*pi*sg2))/2 ...
+ sum((Y-mu).^2)/2/sg2 ...
- (L-1)*sum(sign(X.*log(abs(X)+1)));
% ==============================================================
function Y = yjt(X,L)
% Yeo-Johnson transformation (eq 2.1 of the paper)
Y = nan(size(X));
pos = X >= 0;
if L == 0,
Y( pos) = log(X(pos)+1); % X >= 0, L == 0
Y(~pos) = -((1-X(~pos)).^(2-L)-1)/(2-L); % X < 0, L ~= 2
elseif L == 2,
Y( pos) = ((X(pos)+1).^L-1)./L; % X >= 0, L ~= 0
Y(~pos) = -log(1-X(~pos)); % X < 0, L == 2
else
Y( pos) = ((X(pos)+1).^L-1)./L; % X >= 0, L ~= 0
Y(~pos) = -((1-X(~pos)).^(2-L)-1)/(2-L); % X < 0, L ~= 2
end