Home > code > util > parameter_transformation > encode_parameters.m

encode_parameters

PURPOSE ^

ENCODE_PARAMETERS Encodes the struct array param into a single vector

SYNOPSIS ^

function [theta, decode] = encode_parameters(param)

DESCRIPTION ^

ENCODE_PARAMETERS Encodes the struct array param into a single vector

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [theta, decode] = encode_parameters(param)
0002 %ENCODE_PARAMETERS Encodes the struct array param into a single vector
0003 
0004 %  [theta, decode] = encode_parameters(param) encodes the list of parameters
0005 %    in param into a flat parameter vector theta  which can be decoded by the
0006 %    returned function decode.
0007 %    param is expected to be a struct array with the following fields:
0008 %    - param.name the parameter's name
0009 %    - param.constraint a string representing the constraint to be
0010 %      enforced on this parameter. Can be one of
0011 %      'equal', 'positive', 'correlation', 'orthogonal_quadratic',
0012 %      'orthogonal_non_quadratic', 'spd'.
0013 %       Any other value will be considered as no constraint.
0014 %    - param.value the value to be encoded
0015 %    For individual models there exist parameter encoding and decoding
0016 %    functions that conveniently wrap away the setup of param.
0017 %
0018 % See also ENCODE_PCSV_PARAM, ENCODE_WASC_PARAM.
0019 %
0020 % created by Benedikt Rudolph
0021 % DATE: 20-Aug-2012
0022   
0023   theta = [];
0024   
0025   for j=1:length(param) % iterate over all parameters
0026     v = param(j).value;
0027     param(j).size = size(v);
0028     last_idx = length(theta);
0029     switch param(j).constraint
0030       case 'equal' % forced to equal initial value
0031         param(j).index = [];
0032       case 'positive'
0033         v = pos_to_real(v(:))';
0034         theta = [theta, v];
0035         param(j).index = last_idx + (1:length(v));
0036       case 'correlation'
0037         v = corr_to_real(v(:))';
0038         theta = [theta, v];
0039         param(j).index = last_idx + (1:length(v));
0040       case 'orthogonal_quadratic'
0041         % v is assumed to be quadratic
0042         p = size(v,2);
0043         % perform a Cayley transformation of Q to obtain
0044         % the corresponding skew-symmetric matrix S
0045         S = (eye(p) - v) * inv(eye(p) + v);
0046         s = S(logical(tril(ones(p),-1)))';
0047         theta = [theta, s];
0048         param(j).index = last_idx + (1:length(s));
0049       case 'orthogonal_non_quadratic'
0050         % v can be non-quadratic
0051         p = size(v,2);
0052         % actual base value is stored in param(j).value
0053         % theta encodes skew symmetric matrix for Cayley transformation
0054         % initially that is just zeros
0055         % see decode function for details
0056         theta = [theta, zeros(1,p*(p-1)/2)];
0057         param(j).index = last_idx + (1:p*(p-1)/2);
0058       case 'spd' % symmetric positive definite
0059         p = size(v,1);
0060         R = chol(v);
0061         r = R(logical(triu(ones(p))))';
0062         theta = [theta, r];
0063         param(j).index = last_idx + (1:length(r));
0064       otherwise % no constraint
0065         v = v(:)';
0066         theta = [theta, v];
0067         param(j).index = last_idx + (1:length(v));
0068     end
0069   end
0070   
0071   decode = @(th) decode_parameters(th, param);
0072 end
0073 
0074 function param = decode_parameters(theta, p0)
0075   for j=1:length(p0) % iterate over all parameters
0076     param(j).name = p0(j).name; % copy name
0077     th = theta(p0(j).index); % extract relevant part of theta
0078     sz = p0(j).size;
0079     switch p0(j).constraint
0080       case 'equal' % forced to equal initial value
0081         param(j).value = p0(j).value;
0082       case 'positive'
0083         param(j).value = reshape(real_to_pos(th), sz);
0084       case 'correlation'
0085         param(j).value = reshape(real_to_corr(th), sz);
0086       case 'orthogonal_quadratic'
0087         p = sz(2);
0088         % obtain skew symmetric matrix
0089         S = vector_to_skew_symmetric(th, p);
0090         % perform Cayley transformation
0091         param(j).value = (eye(p) + S) \ (eye(p) - S);
0092       case 'orthogonal_non_quadratic'
0093         A0 = p0(j).value;
0094         p = sz(2);
0095         % obtain skew symmetric matrix
0096         S = vector_to_skew_symmetric(th, p);
0097         % perform Cayley transformation
0098         Q = (eye(p) + S) \ (eye(p) - S);
0099         param(j).value = A0 * Q;
0100       case 'spd'
0101         p = sz(1);
0102         R = zeros(p);
0103         R(logical(triu(ones(p)))) = th;
0104         param(j).value = R'*R;
0105       otherwise % no constraint
0106         param(j).value = reshape(th, sz);
0107     end
0108   end
0109 end

Generated on Mon 29-Apr-2013 19:29:13 by m2html © 2005