From e0ca41303e94de422f9459d3b54d847aa6474517 Mon Sep 17 00:00:00 2001 From: tekhnofiend Date: Tue, 15 Jan 2008 20:06:19 +0000 Subject: [PATCH] added get_scores.m to niche/functional --- fastlib/u/niche/functional/funcica.m | 14 +++-- fastlib/u/niche/functional/get_scores.m | 22 +++++++ .../u/niche/functional/test_smooth_funcica.m | 62 +++++++++---------- 3 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 fastlib/u/niche/functional/get_scores.m diff --git a/fastlib/u/niche/functional/funcica.m b/fastlib/u/niche/functional/funcica.m index 1730b77e30..60d5427c6d 100644 --- a/fastlib/u/niche/functional/funcica.m +++ b/fastlib/u/niche/functional/funcica.m @@ -1,5 +1,5 @@ function [ic_curves_pos, ic_coef_pos, Y_pos, h_Y_pos, pc_coef, pc_curves, pc_scores, mean_coef, W_pos, whitening_transform] = ... - funcica(t, s, myfd_data, p, basis_curves, myfdPar); + funcica(t, s, myfd_data, p, basis_curves, myfdPar, basis_inner_products); % funcica() - functional ICA % first call prelim_funcica % USAGE: [ic_curves_pos, ic_coef_pos, h_Y_pos] = funcica(t, s, data) @@ -11,7 +11,11 @@ data_coef = getcoef(myfd_data); pca_results = pca_fd(myfd_data, p, myfdPar); pc_coef = getcoef(pca_results.harmfd); %pc_curves = basis_curves * pc_coef; -pc_scores = pca_results.harmscr; + +% pc_scores = pca_results.harmscr;% this doesn't work if lambda > 0 +% instead, we do: +pc_scores = get_scores(data_coef, pc_coef', basis_inner_products); + %mean_coef = getcoef(pca_results.meanfd); %figure(1); plot(t, pc_curves(:,1)); @@ -22,7 +26,7 @@ pc_scores = pca_results.harmscr; % p_small should be automatically selected according to some % reconstruction error threshold -%{ + total_sum_var = 0; for i = 1:p total_sum_var = total_sum_var + sum(pc_scores(:,i).^2); @@ -36,8 +40,8 @@ for p_small = 1:p break end end -%} -p_small = 2; + +% p_small = 2; p_small diff --git a/fastlib/u/niche/functional/get_scores.m b/fastlib/u/niche/functional/get_scores.m new file mode 100644 index 0000000000..f4f055c721 --- /dev/null +++ b/fastlib/u/niche/functional/get_scores.m @@ -0,0 +1,22 @@ +function scores = get_scores(data_coef, pc_coef, ... + basis_inner_products); +% USAGE: scores = get_scores(data_coef, pc_coef, basis_inner_products) +% data_coef is a matrix of size num_pc by N +% pc_coef is a matrix of num_pc by num_basis +% basis_inner_products = full(eval_penalty(mybasis, int2Lfd(0))) +% scores is a matrix of size N by num_pc + + +[num_pc, num_basis] = size(pc_coef); +N = size(data_coef, 2); + +scores = zeros(N, num_pc); + +for j = 1:num_pc + pc_coef_j = pc_coef(j,:); + for i = 1:N + scores(i,j) = ... + sum(sum((data_coef(:,i) * ... + pc_coef_j) .* basis_inner_products)); + end +end diff --git a/fastlib/u/niche/functional/test_smooth_funcica.m b/fastlib/u/niche/functional/test_smooth_funcica.m index 3f26b5c94d..6ae966c03c 100644 --- a/fastlib/u/niche/functional/test_smooth_funcica.m +++ b/fastlib/u/niche/functional/test_smooth_funcica.m @@ -2,31 +2,31 @@ % h_Y2_test_set, h_Y_test_set, ... % h_P1_train_set, h_P2_train_set, h_P_train_set, h_P1_test_set, ... % h_P2_test_set, h_P_test_set] = ... -% test_smooth_funcica(data, t, s); +% test_smooth_funcica(data, t, s, basis_inner_products); N = size(data, 2); p = 30; % hardcoded for now mybasis = create_bspline_basis([0 1], p, 4); basis_curves = eval_basis(t, mybasis); -pen = full(eval_penalty(mybasis, int2Lfd(0))); +basis_inner_products = full(eval_penalty(mybasis, int2Lfd(0))); myfd_data = data2fd(data, t, mybasis); cut_fraction = .9; cut = round(cut_fraction * N); -myfd_data_coef = getcoef(myfd_data); +data_coef = getcoef(myfd_data); -num_tests = 10; +num_tests = 1; % a simple method for smoothing %indices = 1:200:1000; %data = data(indices,:); -%lambda_set = 0; -lambda_set = [0 1e-4 1e-3 5e-3 1e-2]; +lambda_set = 1e-4; +%lambda_set = [0 1e-4 1e-3 5e-3 1e-2]; for lambda_i = 1:length(lambda_set) @@ -48,26 +48,28 @@ for lambda_i = 1:length(lambda_set) % extract and center train data data_train = center(data(:, rand_train_indices)); myfd_data_train = data2fd(data_train, t, mybasis); - myfd_data_train_coef = getcoef(myfd_data_train); + data_train_coef = getcoef(myfd_data_train); % extract and center test data data_test = center(data(:, rand_test_indices)); myfd_data_test = data2fd(data_test, t, mybasis); - myfd_data_test_coef = getcoef(myfd_data_test); + data_test_coef = getcoef(myfd_data_test); % alternate way of generating train and test data % not used because no centering %{ - myfd_data_train_coef = myfd_data_coef(:,rand_train_indices); - myfd_data_test_coef = myfd_data_coef(:,rand_test_indices); + data_train_coef = data_coef(:,rand_train_indices); + data_test_coef = data_coef(:,rand_test_indices); myfd_data_train = ... - fd(myfd_data_train_coef, getbasis(myfd_data), getnames(myfd_data)); + fd(data_train_coef, getbasis(myfd_data), getnames(myfd_data)); myfd_data_test = ... - fd(myfd_data_test_coef, getbasis(myfd_data), getnames(myfd_data)); + fd(data_test_coef, getbasis(myfd_data), getnames(myfd_data)); %} [ic_curves_pos, ic_coef_pos, Y_pos, h_Y_pos, pc_coef, pc_curves, pc_scores, mean_coef, W, whitening_transform] = ... - funcica(t, s, myfd_data_train, p, basis_curves, myfdPar); + funcica(t, s, myfd_data_train, p, basis_curves, myfdPar, ... + basis_inner_products); + @@ -77,34 +79,30 @@ for lambda_i = 1:length(lambda_set) scores_train = zeros(cut, p_small); scores_test = zeros(N - cut, p_small); + + + % rescale the utilized parts of pc_coef such that % the pc_curves square integrate to 1 %{ for j = 1:p_small pc_coef_j = pc_coef(j,:); - alpha = sum(sum((pc_coef_j' * pc_coef_j) .* pen)); - pc_coef(j,:) = pc_coef(j,:) / sqrt(alpha); + alpha = sqrt(sum(sum((pc_coef_j' * pc_coef_j) .* basis_inner_products))); + pc_coef(j,:) = pc_coef(j,:) / alpha; end %} - - - for j = 1:p_small - pc_coef_j = pc_coef(j,:); - for i = 1:cut - scores_train(i,j) = sum(sum((myfd_data_train_coef(:,i) * ... - pc_coef_j) .* pen)); - - end - - for i = 1:(N - cut) - scores_test(i,j) = sum(sum((myfd_data_test_coef(:,i) * ... - pc_coef_j) .* pen)); - end - end - scores_train = scores_train'; - scores_test = scores_test'; + scores_train = ... + get_scores(data_train_coef, ... + pc_coef(1:p_small,:), ... + basis_inner_products)'; + + scores_test = ... + get_scores(data_train_coef, ... + pc_coef(1:p_small,:), ... + basis_inner_products)'; + Y_scores_train = W * scores_train(1:2,:); Y_scores_test = W * scores_test(1:2,:);