annotate toolboxes/FullBNT-1.0.7/KPMstats/fit_partitioned_model.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [model, partition_size] = fit_partitioned_model(...
wolffd@0 2 inputs, outputs, selectors, sel_sizes, min_size, partition_names, fn_name, varargin)
wolffd@0 3 %function [models, partition_sizes] = fit_partitioned_model(...
wolffd@0 4 % inputs, outputs, selectors, sel_sizes, min_size, partition_names, fn_name, varargin)
wolffd@0 5 %
wolffd@0 6 % Fit models to different subsets (columns) of the input/output data,
wolffd@0 7 % as chosen by the selectors matrix. If there is only output data, set input=[].
wolffd@0 8 % If there is less than min_size data in partition i,
wolffd@0 9 % we set model{i} = []
wolffd@0 10 %
wolffd@0 11 % Example:
wolffd@0 12 % selectors = [1 2 1 1 1
wolffd@0 13 % 1 2 2 1 2]
wolffd@0 14 % sel_sizes = [2 2] so there are 4 models: (1,1), (2,1), (1,2), (2,2)
wolffd@0 15 % We fit model{1} to data from columns 1,4
wolffd@0 16 % We fit model{2} to no data
wolffd@0 17 % We fit model{3} to data from column 3,5
wolffd@0 18 % We fit model{4} to data from column 2 (assuming min_size <= 1)
wolffd@0 19 %
wolffd@0 20 % For each partition, we call the specified function with the specified arguments
wolffd@0 21 % as follows:
wolffd@0 22 % model{i} = fn(input(:,cols{i}), output(:,cols{i}), args)
wolffd@0 23 % (We omit input if [])
wolffd@0 24 % partition_size(i) is the amount of data in the i'th partition.
wolffd@0 25 %
wolffd@0 26 % Example use: row 1 of selectors is whether an object is present/absent
wolffd@0 27 % and row 2 is the location.
wolffd@0 28 %
wolffd@0 29 % Demo:
wolffd@0 30 % inputs = 1:5; outputs = 6:10; selectors = as above
wolffd@0 31 % fn = 'fit_partitioned_model_testfn';
wolffd@0 32 % [model, partition_size] = fit_partitioned_model(inputs, outputs, selectors, [2 2], fn)
wolffd@0 33 % should produce
wolffd@0 34 % model{1}.input = [1 4], model{1}.output = [6 9]
wolffd@0 35 % model{2} = []
wolffd@0 36 % model{3}.input = [3 5], model{3}.output = [8 10],
wolffd@0 37 % model{4}.input = [2], model{3}.output = [7],
wolffd@0 38 % partition_size = [2 0 2 1]
wolffd@0 39
wolffd@0 40
wolffd@0 41 sel_ndx = subv2ind(sel_sizes, selectors');
wolffd@0 42 Nmodels = prod(sel_sizes);
wolffd@0 43 model = cell(1, Nmodels);
wolffd@0 44 partition_size = zeros(1, Nmodels);
wolffd@0 45 for m=1:Nmodels
wolffd@0 46 ndx = find(sel_ndx==m);
wolffd@0 47 partition_size(m) = length(ndx);
wolffd@0 48 if ~isempty(partition_names) % & (partition_size(m) < min_size)
wolffd@0 49 fprintf('partition %s has size %d, min size = %d\n', ...
wolffd@0 50 partition_names{m}, partition_size(m), min_size);
wolffd@0 51 end
wolffd@0 52 if partition_size(m) >= min_size
wolffd@0 53 if isempty(inputs)
wolffd@0 54 model{m} = feval(fn_name, outputs(:, ndx), varargin{:});
wolffd@0 55 else
wolffd@0 56 model{m} = feval(fn_name, inputs(:,ndx), outputs(:, ndx), varargin{:});
wolffd@0 57 end
wolffd@0 58 end
wolffd@0 59 end