0001 function pdraw = prior_draw(init,uniform)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 persistent p6 p7 p3 p4 lb ub
0036 persistent uniform_index gaussian_index gamma_index beta_index inverse_gamma_1_index inverse_gamma_2_index
0037 persistent uniform_draws gaussian_draws gamma_draws beta_draws inverse_gamma_1_draws inverse_gamma_2_draws
0038
0039
0040 if nargin>0 && init
0041 p6 = evalin('base', 'bayestopt_.p6');
0042 p7 = evalin('base', 'bayestopt_.p7');
0043 p3 = evalin('base', 'bayestopt_.p3');
0044 p4 = evalin('base', 'bayestopt_.p4');
0045 lb = evalin('base', 'bayestopt_.lb');
0046 ub = evalin('base', 'bayestopt_.ub');
0047 number_of_estimated_parameters = length(p6);
0048 if nargin>1 && uniform
0049 prior_shape = repmat(5,number_of_estimated_parameters,1);
0050 else
0051 prior_shape = evalin('base', 'bayestopt_.pshape');
0052 end
0053 beta_index = find(prior_shape==1);
0054 if isempty(beta_index)
0055 beta_draws = 0;
0056 else
0057 beta_draws = 1;
0058 end
0059 gamma_index = find(prior_shape==2);
0060 if isempty(gamma_index)
0061 gamma_draws = 0;
0062 else
0063 gamma_draws = 1;
0064 end
0065 gaussian_index = find(prior_shape==3);
0066 if isempty(gaussian_index)
0067 gaussian_draws = 0;
0068 else
0069 gaussian_draws = 1;
0070 end
0071 inverse_gamma_1_index = find(prior_shape==4);
0072 if isempty(inverse_gamma_1_index)
0073 inverse_gamma_1_draws = 0;
0074 else
0075 inverse_gamma_1_draws = 1;
0076 end
0077 uniform_index = find(prior_shape==5);
0078 if isempty(uniform_index)
0079 uniform_draws = 0;
0080 else
0081 uniform_draws = 1;
0082 end
0083 inverse_gamma_2_index = find(prior_shape==6);
0084 if isempty(inverse_gamma_2_index)
0085 inverse_gamma_2_draws = 0;
0086 else
0087 inverse_gamma_2_draws = 1;
0088 end
0089 pdraw = zeros(number_of_estimated_parameters,1);
0090 return
0091 end
0092
0093 if uniform_draws
0094 pdraw(uniform_index) = rand(length(uniform_index),1).*(p4(uniform_index)-p3(uniform_index)) + p3(uniform_index);
0095 end
0096
0097 if gaussian_draws
0098 pdraw(gaussian_index) = randn(length(gaussian_index),1).*p7(gaussian_index) + p6(gaussian_index);
0099 out_of_bound = find( (pdraw(gaussian_index)'>ub(gaussian_index)) | (pdraw(gaussian_index)'<lb(gaussian_index)));
0100 while ~isempty(out_of_bound),
0101 pdraw(gaussian_index(out_of_bound)) = randn(length(gaussian_index(out_of_bound)),1).*p7(gaussian_index(out_of_bound)) + p6(gaussian_index(out_of_bound));
0102 out_of_bound = find( (pdraw(gaussian_index)'>ub(gaussian_index)) | (pdraw(gaussian_index)'<lb(gaussian_index)));
0103 end
0104 end
0105
0106 if gamma_draws
0107 pdraw(gamma_index) = gamrnd(p6(gamma_index),p7(gamma_index))+p3(gamma_index);
0108 out_of_bound = find( (pdraw(gamma_index)'>ub(gamma_index)) | (pdraw(gamma_index)'<lb(gamma_index)));
0109 while ~isempty(out_of_bound),
0110 pdraw(gamma_index(out_of_bound)) = gamrnd(p6(gamma_index(out_of_bound)),p7(gamma_index(out_of_bound)))+p3(gamma_index(out_of_bound));
0111 out_of_bound = find( (pdraw(gamma_index)'>ub(gamma_index)) | (pdraw(gamma_index)'<lb(gamma_index)));
0112 end
0113 end
0114
0115 if beta_draws
0116 pdraw(beta_index) = (p4(beta_index)-p3(beta_index)).*betarnd(p6(beta_index),p7(beta_index))+p3(beta_index);
0117 out_of_bound = find( (pdraw(beta_index)'>ub(beta_index)) | (pdraw(beta_index)'<lb(beta_index)));
0118 while ~isempty(out_of_bound),
0119 pdraw(beta_index(out_of_bound)) = (p4(beta_index(out_of_bound))-p3(beta_index(out_of_bound))).*betarnd(p6(beta_index(out_of_bound)),p7(beta_index(out_of_bound)))+p3(beta_index(out_of_bound));
0120 out_of_bound = find( (pdraw(beta_index)'>ub(beta_index)) | (pdraw(beta_index)'<lb(beta_index)));
0121 end
0122 end
0123
0124 if inverse_gamma_1_draws
0125 pdraw(inverse_gamma_1_index) = ...
0126 sqrt(1./gamrnd(p7(inverse_gamma_1_index)/2,2./p6(inverse_gamma_1_index)))+p3(inverse_gamma_1_index);
0127 out_of_bound = find( (pdraw(inverse_gamma_1_index)'>ub(inverse_gamma_1_index)) | (pdraw(inverse_gamma_1_index)'<lb(inverse_gamma_1_index)));
0128 while ~isempty(out_of_bound),
0129 pdraw(inverse_gamma_1_index(out_of_bound)) = ...
0130 sqrt(1./gamrnd(p7(inverse_gamma_1_index(out_of_bound))/2,2./p6(inverse_gamma_1_index(out_of_bound))))+p3(inverse_gamma_1_index(out_of_bound));
0131 out_of_bound = find( (pdraw(inverse_gamma_1_index)'>ub(inverse_gamma_1_index)) | (pdraw(inverse_gamma_1_index)'<lb(inverse_gamma_1_index)));
0132 end
0133 end
0134
0135 if inverse_gamma_2_draws
0136 pdraw(inverse_gamma_2_index) = ...
0137 1./gamrnd(p7(inverse_gamma_2_index)/2,2./p6(inverse_gamma_2_index))+p3(inverse_gamma_2_index);
0138 out_of_bound = find( (pdraw(inverse_gamma_2_index)'>ub(inverse_gamma_2_index)) | (pdraw(inverse_gamma_2_index)'<lb(inverse_gamma_2_index)));
0139 while ~isempty(out_of_bound),
0140 pdraw(inverse_gamma_2_index(out_of_bound)) = ...
0141 sqrt(1./gamrnd(p7(inverse_gamma_2_index(out_of_bound))/2,2./p6(inverse_gamma_2_index(out_of_bound))))+p3(inverse_gamma_2_index(out_of_bound));
0142 out_of_bound = find( (pdraw(inverse_gamma_2_index)'>ub(inverse_gamma_2_index)) | (pdraw(inverse_gamma_2_index)'<lb(inverse_gamma_2_index)));
0143 end
0144 end