# HG changeset patch # User Rik # Date 1300568018 25200 # Node ID d70c99028ba33e5a6d9de8b55d34648cef8730dc # Parent 0579a13f29a1c3fceb347b8fae04793273a087f7 Make helper functions for logistic_regression private. diff -r 0579a13f29a1 -r d70c99028ba3 scripts/ChangeLog --- a/scripts/ChangeLog Sat Mar 19 13:06:50 2011 -0700 +++ b/scripts/ChangeLog Sat Mar 19 13:53:38 2011 -0700 @@ -1,3 +1,12 @@ +2010-03-19 Rik + + * statistics/models/logistic_regression.m: Do not split function + declaration with line continuation. + * statistics/models/private/logistic_regression_likelihood.m, + statistics/models/private/logistic_regression_derivatives.m: Make + helper functions private. + * statistics/models/module.mk: Make helper functions private. + 2010-03-19 Rik * plot/isocolors.m, plot/isonormals.m, plot/isosurface.m: Improve diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/logistic_regression.m --- a/scripts/statistics/models/logistic_regression.m Sat Mar 19 13:06:50 2011 -0700 +++ b/scripts/statistics/models/logistic_regression.m Sat Mar 19 13:53:38 2011 -0700 @@ -84,8 +84,7 @@ ## Uses the auxiliary functions logistic_regression_derivatives and ## logistic_regression_likelihood. -function [theta, beta, dev, dl, d2l, p] ... - = logistic_regression (y, x, print, theta, beta) +function [theta, beta, dev, dl, d2l, p] = logistic_regression (y, x, print, theta, beta) ## check input y = round (vec (y)); diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/logistic_regression_derivatives.m --- a/scripts/statistics/models/logistic_regression_derivatives.m Sat Mar 19 13:06:50 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -## Copyright (C) 1995-2011 Kurt Hornik -## -## This file is part of Octave. -## -## Octave 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. -## -## Octave 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 Octave; see the file COPYING. If not, see -## . - -## -*- texinfo -*- -## @deftypefn {Function File} {[@var{dl}, @var{d2l}] =} logistic_regression_derivatives (@var{x}, @var{z}, @var{z1}, @var{g}, @var{g1}, @var{p}) -## Called by logistic_regression. Calculates derivates of the -## log-likelihood for ordinal logistic regression model. -## @end deftypefn - -## Author: Gordon K. Smyth -## Adapted-By: KH -## Description: Derivates of log-likelihood in logistic regression - -function [dl, d2l] = logistic_regression_derivatives (x, z, z1, g, g1, p) - - if (nargin != 6) - print_usage (); - endif - - ## first derivative - v = g .* (1 - g) ./ p; v1 = g1 .* (1 - g1) ./ p; - dlogp = [(diag (v) * z - diag (v1) * z1), (diag (v - v1) * x)]; - dl = sum (dlogp)'; - - ## second derivative - w = v .* (1 - 2 * g); w1 = v1 .* (1 - 2 * g1); - d2l = [z, x]' * diag (w) * [z, x] - [z1, x]' * diag (w1) * [z1, x] ... - - dlogp' * dlogp; - -endfunction diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/logistic_regression_likelihood.m --- a/scripts/statistics/models/logistic_regression_likelihood.m Sat Mar 19 13:06:50 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -## Copyright (C) 1995-2011 Kurt Hornik -## -## This file is part of Octave. -## -## Octave 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. -## -## Octave 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 Octave; see the file COPYING. If not, see -## . - -## -*- texinfo -*- -## @deftypefn {Function File} {[@var{g}, @var{g1}, @var{p}, @var{dev}] =} logistic_regression_likelihood (@var{y}, @var{x}, @var{beta}, @var{z}, @var{z1}) -## Calculates likelihood for the ordinal logistic regression model. -## Called by logistic_regression. -## @end deftypefn - -## Author: Gordon K. Smyth -## Adapted-By: KH -## Description: Likelihood in logistic regression - -function [g, g1, p, dev] = logistic_regression_likelihood (y, x, beta, z, z1) - - if (nargin != 5) - print_usage (); - endif - - e = exp ([z, x] * beta); e1 = exp ([z1, x] * beta); - g = e ./ (1 + e); g1 = e1 ./ (1 + e1); - g = max (y == max (y), g); g1 = min (y > min(y), g1); - - p = g - g1; - dev = -2 * sum (log (p)); - -endfunction diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/module.mk --- a/scripts/statistics/models/module.mk Sat Mar 19 13:06:50 2011 -0700 +++ b/scripts/statistics/models/module.mk Sat Mar 19 13:53:38 2011 -0700 @@ -1,9 +1,12 @@ FCN_FILE_DIRS += statistics/models +statistics_models_PRIVATE_FCN_FILES = \ + statistics/models/private/logistic_regression_derivatives.m \ + statistics/models/private/logistic_regression_likelihood.m + statistics_models_FCN_FILES = \ statistics/models/logistic_regression.m \ - statistics/models/logistic_regression_derivatives.m \ - statistics/models/logistic_regression_likelihood.m + $(statistics_models_PRIVATE_FCN_FILES) FCN_FILES += $(statistics_models_FCN_FILES) diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/private/logistic_regression_derivatives.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/statistics/models/private/logistic_regression_derivatives.m Sat Mar 19 13:53:38 2011 -0700 @@ -0,0 +1,47 @@ +## Copyright (C) 1995-2011 Kurt Hornik +## +## This file is part of Octave. +## +## Octave 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. +## +## Octave 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 Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{dl}, @var{d2l}] =} logistic_regression_derivatives (@var{x}, @var{z}, @var{z1}, @var{g}, @var{g1}, @var{p}) +## Calculate derivatives of the log-likelihood for ordinal logistic regression +## model. +## Private function called by @code{logistic_regression}. +## @seealso{logistic_regression} +## @end deftypefn + +## Author: Gordon K. Smyth +## Adapted-By: KH +## Description: Derivates of log-likelihood in logistic regression + +function [dl, d2l] = logistic_regression_derivatives (x, z, z1, g, g1, p) + + if (nargin != 6) + print_usage (); + endif + + ## first derivative + v = g .* (1 - g) ./ p; v1 = g1 .* (1 - g1) ./ p; + dlogp = [(diag (v) * z - diag (v1) * z1), (diag (v - v1) * x)]; + dl = sum (dlogp)'; + + ## second derivative + w = v .* (1 - 2 * g); w1 = v1 .* (1 - 2 * g1); + d2l = [z, x]' * diag (w) * [z, x] - [z1, x]' * diag (w1) * [z1, x] ... + - dlogp' * dlogp; + +endfunction diff -r 0579a13f29a1 -r d70c99028ba3 scripts/statistics/models/private/logistic_regression_likelihood.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/statistics/models/private/logistic_regression_likelihood.m Sat Mar 19 13:53:38 2011 -0700 @@ -0,0 +1,43 @@ +## Copyright (C) 1995-2011 Kurt Hornik +## +## This file is part of Octave. +## +## Octave 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. +## +## Octave 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 Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{g}, @var{g1}, @var{p}, @var{dev}] =} logistic_regression_likelihood (@var{y}, @var{x}, @var{beta}, @var{z}, @var{z1}) +## Calculate the likelihood for the ordinal logistic regression model. +## Private function called by @code{logistic_regression}. +## @seealso{logistic_regression} +## @end deftypefn + +## Author: Gordon K. Smyth +## Adapted-By: KH +## Description: Likelihood in logistic regression + +function [g, g1, p, dev] = logistic_regression_likelihood (y, x, beta, z, z1) + + if (nargin != 5) + print_usage (); + endif + + e = exp ([z, x] * beta); e1 = exp ([z1, x] * beta); + g = e ./ (1 + e); g1 = e1 ./ (1 + e1); + g = max (y == max (y), g); g1 = min (y > min(y), g1); + + p = g - g1; + dev = -2 * sum (log (p)); + +endfunction