0001 function [theta, decode] = encode_parameters(param)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 theta = [];
0024
0025 for j=1:length(param)
0026 v = param(j).value;
0027 param(j).size = size(v);
0028 last_idx = length(theta);
0029 switch param(j).constraint
0030 case 'equal'
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
0042 p = size(v,2);
0043
0044
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
0051 p = size(v,2);
0052
0053
0054
0055
0056 theta = [theta, zeros(1,p*(p-1)/2)];
0057 param(j).index = last_idx + (1:p*(p-1)/2);
0058 case 'spd'
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
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)
0076 param(j).name = p0(j).name;
0077 th = theta(p0(j).index);
0078 sz = p0(j).size;
0079 switch p0(j).constraint
0080 case 'equal'
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
0089 S = vector_to_skew_symmetric(th, p);
0090
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
0096 S = vector_to_skew_symmetric(th, p);
0097
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
0106 param(j).value = reshape(th, sz);
0107 end
0108 end
0109 end