-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_boxcox.m
66 lines (62 loc) · 1.97 KB
/
palm_boxcox.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
function varargout = palm_boxcox(varargin)
% Find the lambda parameter and compute the Box-Cox transformation.
%
% Usage:
% [Y,L] = palm_boxcox(X)
% Y = palm_boxcox(X,L)
%
% X : Input data. All values need to be positive.
% Y : Transformed data.
% L : Lambda parameter.
%
% Reference:
% Box GEP and Cox DR. An Analysis of Transformations.
% J R Stat Soc Series B. 1964;26(2):211-252.
%
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Dec/2015
% 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,0,optimset('Display','off','MaxFunEvals',1000));
varargout{1} = bct(X,L);
else
L = varargin{2};
varargout{1} = bct(X,L);
end
if nargout == 2,
varargout{2} = L;
end
% ==============================================================
function LL = loglike(X,L)
% Likelihood function.
Y = bct(X,L);
LL = numel(X)*log(std(Y,1,1)^2)/2 - (L-1)*sum(log(X));
% ==============================================================
function Y = bct(X,L)
% Actual Box-Cox transformation.
if L == 0,
Y = log(X);
else
Y = (X.^L - 1)/L;
end