Mercurial > forge
changeset 10222:77cb74e47659 octave-forge
econometrics: functions mle_variance, __kernel_epanechnikov, average_moments, __kernel_normal, sum_moments_nodes, kernel_density_nodes, kernel_regression_nodes, gmm_obj, nls_obj_nodes were made private.
line wrap: on
line diff
--- a/main/econometrics/INDEX Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/INDEX Fri May 11 12:21:58 2012 +0000 @@ -1,22 +1,18 @@ econometrics >> Econometrics Econometrics - average_moments delta_method - gmm_estimate gmm_example gmm_obj gmm_results gmm_variance gmm_variance_inefficient - mle_estimate mle_example mle_obj mle_results mle_variance + delta_method + gmm_estimate gmm_example gmm_results gmm_variance gmm_variance_inefficient + mle_estimate mle_example mle_obj mle_results parameterize prettyprint scale_data unscale_parameters nls_estimate nls_obj mle_obj_nodes nls_example - nls_obj_nodes poisson poisson_moments prettyprint_c - sum_moments_nodes kernel_density kernel_density_cvscore - kernel_density_nodes kernel_example kernel_optimal_bandwidth kernel_regression kernel_regression_cvscore - kernel_regression_nodes
--- a/main/econometrics/NEWS Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/NEWS Fri May 11 12:21:58 2012 +0000 @@ -1,5 +1,11 @@ Summary of important user-visible changes for econometrics 1.1.0: ------------------------------------------------------------------- + ** The following functions have been made private: + + average_moments gmm_obj kernel_density_nodes + __kernel_epanechnikov __kernel_normal kernel_regression_nodes + mle_variance nls_obj_nodes sum_moments_nodes + ** Calls to deprecated functions have been replaced for compatibility with newer octave versions.
--- a/main/econometrics/inst/__kernel_epanechnikov.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -## Copyright (C) 2006 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## __kernel_epanechnikov: this function is for internal use by kernel_density -## and kernel_regression -## -## multivariate spherical Epanechnikov kernel -## input: PxK matrix - P data points, each of which is in R^K -## output: Px1 vector, input matrix passed though the kernel -## other multivariate kernel functions should follow this convention - -function z = __kernel_epanechnikov(z) - - K = columns(z); - - # Volume of d-dimensional unit sphere - c = pi ^ (K/2) / gamma(K/2 + 1); - - # compute kernel - z = sumsq(z, 2); - z = ((1/2) / c * (K + 2) * (1 - z)) .* (z < 1); - - -endfunction
--- a/main/econometrics/inst/__kernel_normal.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -## Copyright (C) 2006 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## __kernel_normal: this function is for internal use by kernel_density -## and kernel_regression -## -## product normal kernel -## input: PxK matrix - P data points, each of which is in R^K -## output: Px1 vector, input matrix passed though the kernel -## other multivariate kernel functions should follow this convention - -function z = __kernel_normal(z) - - z = normpdf(z); - z = prod(z,2); - -endfunction
--- a/main/econometrics/inst/average_moments.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## for internal use by gmm_estimate - -## average moments (separate function so it can be differentiated) -function m = average_moments(theta, data, moments, momentargs) - - global NSLAVES PARALLEL NEWORLD NSLAVES TAG; - - n = rows(data); - - if PARALLEL - nn = floor(n/(NSLAVES + 1)); # save some work for master - - # The command that the slave nodes will execute - cmd=['contrib = sum_moments_nodes(theta, data, moments, momentargs, nn); ',... - 'MPI_Send(contrib,0,TAG,NEWORLD);']; - - # send items to slaves - NumCmds_Send({"theta", "nn", "cmd"},{theta, nn, cmd}); - - # evaluate last block on master while slaves are busy - m = feval("sum_moments_nodes", theta, data, moments, momentargs, nn); - - # collect slaves' results - contrib = zeros(1,columns(m)); - for i = 1:NSLAVES - MPI_Recv(contrib,i,TAG,NEWORLD); - m = m + contrib; - endfor - - m = m'; # we want a column vector, please - m = m/n; # average please, not sum - - else # serial version - m = feval(moments, theta, data, momentargs); - m = mean(m)'; # returns Gx1 moment vector - endif - -endfunction
--- a/main/econometrics/inst/gmm_obj.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## The GMM objective function, for internal use by gmm_estimate -## This is scaled so that it converges to a finite number. -## To get the chi-square specification -## test you need to multiply by n (the sample size) - -function obj_value = gmm_obj(theta, data, weight, moments, momentargs) - - m = average_moments(theta, data, moments, momentargs); - - obj_value = m' * weight *m; - - if (((abs(obj_value) == Inf)) || (isnan(obj_value))) - obj_value = realmax; - endif - -endfunction
--- a/main/econometrics/inst/kernel_density.m Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/inst/kernel_density.m Fri May 11 12:21:58 2012 +0000 @@ -52,7 +52,7 @@ # set defaults for optional args if (nargin < 3) bandwidth = (n ^ (-1/(4+k))); endif # bandwidth - see Li and Racine pg. 26 - if (nargin < 4) kernel = "__kernel_normal"; endif # what kernel? + if (nargin < 4) kernel = "kernel_normal"; endif # what kernel? if (nargin < 5) prewhiten = false; endif # automatic prewhitening? if (nargin < 6) do_cv = false; endif # ordinary or leave-1-out if (nargin < 7) computenodes = 0; endif # parallel?
--- a/main/econometrics/inst/kernel_density_nodes.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -## Copyright (C) 2006, 2007 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## kernel_density_nodes: for internal use by kernel_density - does calculations on nodes - -function z = kernel_density_nodes(eval_points, data, do_cv, kernel, points_per_node, nslaves, debug) - - if (nslaves > 0) - global NEWORLD - [info, myrank] = MPI_Comm_rank(NEWORLD); - else myrank = 0; # if not parallel then do all on master node - endif - - if myrank == 0 # Do this if I'm master - startblock = nslaves*points_per_node + 1; - endblock = rows(eval_points); - else # this is for the slaves - startblock = myrank*points_per_node - points_per_node + 1; - endblock = myrank*points_per_node; - endif - - # the block of eval_points this node does - myeval = eval_points(startblock:endblock,:); - nn = rows(myeval); - n = rows(data); - - W = __kernel_weights(data, myeval, kernel); - if (do_cv) - W = W - diag(diag(W)); - z = sum(W,2) / (n-1); - else - z = sum(W,2) / n; - endif - - if debug - printf("z on node %d: \n", myrank); - z' - endif -endfunction - -
--- a/main/econometrics/inst/kernel_example.m Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/inst/kernel_example.m Fri May 11 12:21:58 2012 +0000 @@ -61,7 +61,7 @@ # bandwidth = kernel_optimal_bandwidth(data); # get the fitted density and do a plot tic; -dens = kernel_density(grid_x, data, bandwidth, "__kernel_normal", false, false, compute_nodes); +dens = kernel_density(grid_x, data, bandwidth, "kernel_normal", false, false, compute_nodes); t1 = toc; printf("\n"); printf("########################################################################\n"); @@ -90,7 +90,7 @@ # bandwidth = kernel_optimal_bandwidth(data); # get the fitted density and do a plot tic; -dens = kernel_density(eval_points, data, bandwidth, "__kernel_normal", false, false, compute_nodes); +dens = kernel_density(eval_points, data, bandwidth, "kernel_normal", false, false, compute_nodes); t1 = toc; printf("\n"); printf("########################################################################\n"); @@ -128,7 +128,7 @@ y = trueline + sig*randn(n,1); bandwidth = 0.45; tic; - fit = kernel_regression(x, y, x, bandwidth, "__kernel_normal", false, false, compute_nodes); + fit = kernel_regression(x, y, x, bandwidth, "kernel_normal", false, false, compute_nodes); t1 = toc; ts(i, nodes) = t1; plot(nodes, t1, "*");
--- a/main/econometrics/inst/kernel_optimal_bandwidth.m Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/inst/kernel_optimal_bandwidth.m Fri May 11 12:21:58 2012 +0000 @@ -25,7 +25,7 @@ function bandwidth = kernel_optimal_bandwidth(data, depvar, kernel) if (nargin < 2) error("kernel_optimal_bandwidth: 3 arguments required"); endif - if (nargin < 3) kernel = "__kernel_epanechnikov"; endif + if (nargin < 3) kernel = "kernel_epanechnikov"; endif do_density = false; if isempty(depvar) do_density = true; endif;
--- a/main/econometrics/inst/kernel_regression.m Fri May 11 12:15:24 2012 +0000 +++ b/main/econometrics/inst/kernel_regression.m Fri May 11 12:21:58 2012 +0000 @@ -48,7 +48,7 @@ # set defaults for optional args if (nargin < 4) bandwidth = (n ^ (-1/(4+k))); endif # bandwidth - see Li and Racine pg. 66 - if (nargin < 5) kernel = "__kernel_normal"; endif # what kernel? + if (nargin < 5) kernel = "kernel_normal"; endif # what kernel? if (nargin < 6) prewhiten = true; endif # automatic prewhitening? if (nargin < 7) do_cv = false; endif # ordinary or leave-1-out if (nargin < 8) computenodes = 0; endif # parallel?
--- a/main/econometrics/inst/kernel_regression_nodes.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -## Copyright (C) 2006, 2007 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## kernel_regression_nodes: for internal use by kernel_regression - does calculations on nodes - -function z = kernel_regression_nodes(eval_points, data, do_cv, kernel, points_per_node, nslaves, debug) - - if (nslaves > 0) - global NEWORLD - [info, myrank] = MPI_Comm_rank(NEWORLD); - else myrank = 0; # if not parallel then do all on master node - endif - - if myrank == 0 # Do this if I'm master - startblock = nslaves*points_per_node + 1; - endblock = rows(eval_points); - else # this is for the slaves - startblock = myrank*points_per_node - points_per_node + 1; - endblock = myrank*points_per_node; - endif - - # the block of eval_points this node does - myeval = eval_points(startblock:endblock,:); - nn = rows(myeval); - n = rows(data); - - y = data(:,1); - data = data(:,2:columns(data)); - W = __kernel_weights(data, myeval, kernel); - - # drop own weight for CV - if (do_cv) W = W - diag(diag(W)); endif - - den = sum(W,2); - if !all(den) - warning("kernel_regression: some evaluation points have no neighbors - increase the bandwidth"); - den = den + eps; # avoid divide by zero - endif - - W = W ./ (repmat(den,1,n)); - z = W*y; - - if debug - printf("z on node %d: \n", myrank); - z' - endif -endfunction - -
--- a/main/econometrics/inst/mle_variance.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## usage: [V,scorecontribs,J_inv] = -## mle_variance(theta, data, model, modelargs) -## -## This is for internal use by mle_results - -# sandwich form of var-cov matrix -function [V, scorecontribs, J_inv] = mle_variance(theta, data, model, modelargs) - scorecontribs = numgradient(model, {theta, data, modelargs}); - n = rows(scorecontribs); - I = scorecontribs'*scorecontribs / n; - J = numhessian("mle_obj", {theta, data, model, modelargs}); - J_inv = inverse(J); - V = J_inv*I*J_inv/n; -endfunction
--- a/main/econometrics/inst/nls_obj_nodes.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -## Copyright (C) 2005 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## This is for internal use by nls_estimate - -function contrib = nls_obj_nodes(theta, data, model, modelargs, nn) - global NEWORLD NSLAVES - # Who am I? - [info, rank] = MPI_Comm_rank(NEWORLD); - - if rank == 0 # Do this if I'm master - startblock = NSLAVES*nn + 1; - endblock = rows(data); - else # this is for the slaves - startblock = rank*nn-nn+1; - endblock = rank*nn; - endif - - data = data(startblock:endblock,:); - contrib = feval(model, theta, data, modelargs); - contrib = sum(contrib); - -endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/average_moments.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,53 @@ +## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## for internal use by gmm_estimate + +## average moments (separate function so it can be differentiated) +function m = average_moments(theta, data, moments, momentargs) + + global NSLAVES PARALLEL NEWORLD NSLAVES TAG; + + n = rows(data); + + if PARALLEL + nn = floor(n/(NSLAVES + 1)); # save some work for master + + # The command that the slave nodes will execute + cmd=['contrib = sum_moments_nodes(theta, data, moments, momentargs, nn); ',... + 'MPI_Send(contrib,0,TAG,NEWORLD);']; + + # send items to slaves + NumCmds_Send({"theta", "nn", "cmd"},{theta, nn, cmd}); + + # evaluate last block on master while slaves are busy + m = feval("sum_moments_nodes", theta, data, moments, momentargs, nn); + + # collect slaves' results + contrib = zeros(1,columns(m)); + for i = 1:NSLAVES + MPI_Recv(contrib,i,TAG,NEWORLD); + m = m + contrib; + endfor + + m = m'; # we want a column vector, please + m = m/n; # average please, not sum + + else # serial version + m = feval(moments, theta, data, momentargs); + m = mean(m)'; # returns Gx1 moment vector + endif + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/gmm_obj.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,31 @@ +## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## The GMM objective function, for internal use by gmm_estimate +## This is scaled so that it converges to a finite number. +## To get the chi-square specification +## test you need to multiply by n (the sample size) + +function obj_value = gmm_obj(theta, data, weight, moments, momentargs) + + m = average_moments(theta, data, moments, momentargs); + + obj_value = m' * weight *m; + + if (((abs(obj_value) == Inf)) || (isnan(obj_value))) + obj_value = realmax; + endif + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/kernel_density_nodes.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,53 @@ +## Copyright (C) 2006, 2007 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## kernel_density_nodes: for internal use by kernel_density - does calculations on nodes + +function z = kernel_density_nodes(eval_points, data, do_cv, kernel, points_per_node, nslaves, debug) + + if (nslaves > 0) + global NEWORLD + [info, myrank] = MPI_Comm_rank(NEWORLD); + else myrank = 0; # if not parallel then do all on master node + endif + + if myrank == 0 # Do this if I'm master + startblock = nslaves*points_per_node + 1; + endblock = rows(eval_points); + else # this is for the slaves + startblock = myrank*points_per_node - points_per_node + 1; + endblock = myrank*points_per_node; + endif + + # the block of eval_points this node does + myeval = eval_points(startblock:endblock,:); + nn = rows(myeval); + n = rows(data); + + W = __kernel_weights(data, myeval, kernel); + if (do_cv) + W = W - diag(diag(W)); + z = sum(W,2) / (n-1); + else + z = sum(W,2) / n; + endif + + if debug + printf("z on node %d: \n", myrank); + z' + endif +endfunction + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/kernel_epanechnikov.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,35 @@ +## Copyright (C) 2006 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## kernel_epanechnikov: this function is for internal use by kernel_density +## and kernel_regression +## +## multivariate spherical Epanechnikov kernel +## input: PxK matrix - P data points, each of which is in R^K +## output: Px1 vector, input matrix passed though the kernel +## other multivariate kernel functions should follow this convention + +function z = kernel_epanechnikov(z) + + K = columns(z); + + # Volume of d-dimensional unit sphere + c = pi ^ (K/2) / gamma(K/2 + 1); + + # compute kernel + z = sumsq(z, 2); + z = ((1/2) / c * (K + 2) * (1 - z)) .* (z < 1); + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/kernel_normal.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,29 @@ +## Copyright (C) 2006 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## kernel_normal: this function is for internal use by kernel_density +## and kernel_regression +## +## product normal kernel +## input: PxK matrix - P data points, each of which is in R^K +## output: Px1 vector, input matrix passed though the kernel +## other multivariate kernel functions should follow this convention + +function z = kernel_normal(z) + + z = normpdf(z); + z = prod(z,2); + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/kernel_regression_nodes.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,61 @@ +## Copyright (C) 2006, 2007 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## kernel_regression_nodes: for internal use by kernel_regression - does calculations on nodes + +function z = kernel_regression_nodes(eval_points, data, do_cv, kernel, points_per_node, nslaves, debug) + + if (nslaves > 0) + global NEWORLD + [info, myrank] = MPI_Comm_rank(NEWORLD); + else myrank = 0; # if not parallel then do all on master node + endif + + if myrank == 0 # Do this if I'm master + startblock = nslaves*points_per_node + 1; + endblock = rows(eval_points); + else # this is for the slaves + startblock = myrank*points_per_node - points_per_node + 1; + endblock = myrank*points_per_node; + endif + + # the block of eval_points this node does + myeval = eval_points(startblock:endblock,:); + nn = rows(myeval); + n = rows(data); + + y = data(:,1); + data = data(:,2:columns(data)); + W = __kernel_weights(data, myeval, kernel); + + # drop own weight for CV + if (do_cv) W = W - diag(diag(W)); endif + + den = sum(W,2); + if !all(den) + warning("kernel_regression: some evaluation points have no neighbors - increase the bandwidth"); + den = den + eps; # avoid divide by zero + endif + + W = W ./ (repmat(den,1,n)); + z = W*y; + + if debug + printf("z on node %d: \n", myrank); + z' + endif +endfunction + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/mle_variance.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,29 @@ +## Copyright (C) 2003, 2004, 2005 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## usage: [V,scorecontribs,J_inv] = +## mle_variance(theta, data, model, modelargs) +## +## This is for internal use by mle_results + +# sandwich form of var-cov matrix +function [V, scorecontribs, J_inv] = mle_variance(theta, data, model, modelargs) + scorecontribs = numgradient(model, {theta, data, modelargs}); + n = rows(scorecontribs); + I = scorecontribs'*scorecontribs / n; + J = numhessian("mle_obj", {theta, data, model, modelargs}); + J_inv = inverse(J); + V = J_inv*I*J_inv/n; +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/nls_obj_nodes.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,35 @@ +## Copyright (C) 2005 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## This is for internal use by nls_estimate + +function contrib = nls_obj_nodes(theta, data, model, modelargs, nn) + global NEWORLD NSLAVES + # Who am I? + [info, rank] = MPI_Comm_rank(NEWORLD); + + if rank == 0 # Do this if I'm master + startblock = NSLAVES*nn + 1; + endblock = rows(data); + else # this is for the slaves + startblock = rank*nn-nn+1; + endblock = rank*nn; + endif + + data = data(startblock:endblock,:); + contrib = feval(model, theta, data, modelargs); + contrib = sum(contrib); + +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/econometrics/inst/private/sum_moments_nodes.m Fri May 11 12:21:58 2012 +0000 @@ -0,0 +1,37 @@ +## Copyright (C) 2005 Michael Creel <michael.creel@uab.es> +## +## 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 (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/>. + +## for internal use by gmm_estimate + +function contrib = sum_moments_nodes(theta, data, moments, momentargs, nn) + + global NEWORLD NSLAVES + + # Who am I? + [info, rank] = MPI_Comm_rank(NEWORLD); + + if rank == 0 # Do this if I'm master + startblock = NSLAVES*nn + 1; + endblock = rows(data); + else # this is for the slaves + startblock = rank*nn-nn+1; + endblock = rank*nn; + endif + + data = data(startblock:endblock,:); + contrib = feval(moments, theta, data, momentargs); + contrib = sum(contrib); + +endfunction
--- a/main/econometrics/inst/sum_moments_nodes.m Fri May 11 12:15:24 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -## Copyright (C) 2005 Michael Creel <michael.creel@uab.es> -## -## 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 (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/>. - -## for internal use by gmm_estimate - -function contrib = sum_moments_nodes(theta, data, moments, momentargs, nn) - - global NEWORLD NSLAVES - - # Who am I? - [info, rank] = MPI_Comm_rank(NEWORLD); - - if rank == 0 # Do this if I'm master - startblock = NSLAVES*nn + 1; - endblock = rows(data); - else # this is for the slaves - startblock = rank*nn-nn+1; - endblock = rank*nn; - endif - - data = data(startblock:endblock,:); - contrib = feval(moments, theta, data, momentargs); - contrib = sum(contrib); - -endfunction