time | Calls | line |
---|
| | 7 | function [ar_choice_prob_byY, ar_choice_unique_sorted_byY, ...
|
| | 8 | mt_choice_prob_byYZ, mt_choice_prob_byYA] = fft_disc_rand_var_mass2outcomes(varargin)
|
| | 9 | %% FFT_DISC_RAND_VAR_MASS2OUTCOMES find f(y) based on f(a,z), and y(a,z)
|
| | 10 | % Having derived f(a,z) the probability mass function of the joint discrete
|
| | 11 | % random variables, we now obtain distributional statistics. Note that we
|
| | 12 | % know f(a,z), and we also know relevant policy functions a'(a,z), c(a,z),
|
| | 13 | % or other policy functions. We can simulate any choices that are a
|
| | 14 | % function of the random variables (a,z), using f(a,z).
|
| | 15 | %
|
| | 16 | % The procedure here has these steps:
|
| | 17 | %
|
| | 18 | % # Sort [c(a,z), f(a,z)] by c(a,z)
|
| | 19 | % # Generate unique IDs of sorted c(a,z): unique
|
| | 20 | % # sum(f(a,z)|c) for each unique c(a,z): accumarray, this generates f(c)
|
| | 21 | % # calculate statistics based on f(c), the discrete distribution of c.
|
| | 22 | %
|
| | 23 | % Outputs are:
|
| | 24 | %
|
| | 25 | % * unique sorted outcomes, note different (a,z) can generate the same
|
| | 26 | % outcomes and not in order
|
| | 27 | % * find total probabiliy for p(outcome, a) = sum_{z}( 1{outcome(a,z)==outcome}*f(a,z))
|
| | 28 | %
|
| | 29 | % $$ p(y,a) = \sum_{z} \left(1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 30 | %
|
| | 31 | % * find total probabiliy for p(outcome, z) = sum_{a}( 1{outcome(a,z)==outcome}*f(a,z))
|
| | 32 | %
|
| | 33 | % $$ p(y,z) = \sum_{a} \left(1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 34 | %
|
| | 35 | % * find total probabiliy for p(outcome) = sum_{a,z}( 1{outcome(a,z)==outcome}*f(a,z) )
|
| | 36 | %
|
| | 37 | % $$ p(Y=y) = \sum_{a,z} \left( 1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 38 | %
|
| | 39 | % @param st_var_name string name of the variable (choice/outcome) been analyzed
|
| | 40 | %
|
| | 41 | % @param mt_choice_bystates matrix N by M of choices along two dimensions,
|
| | 42 | % N could be endogenous states, M could be exogenous shocks, or vice-versa
|
| | 43 | %
|
| | 44 | % @param mt_dist_bystates matrix N by M of probability mass on states, N
|
| | 45 | % could be endogenous states, M could be exogenous shocks, or vice versa
|
| | 46 | %
|
| | 47 | % @return tb_choice_drv_cur_byY table table containing two columns, unique
|
| | 48 | % outcomes/choices y from y(a,z) and probability mass associated with each
|
| | 49 | % y f(y)
|
| | 50 | %
|
| | 51 | % @return ar_choice_prob_byY table array probability mass associated with each
|
| | 52 | % y f(y), second column from tb_choice_drv_cur_byY, dimension unknown,
|
| | 53 | % determined by y(a,z) function
|
| | 54 | %
|
| | 55 | % @return ar_choice_unique_sorted_byY table array unique Ys, dimension
|
| | 56 | % unknown, determined by y(a,z) function
|
| | 57 | %
|
| | 58 | % @return mt_choice_prob_byYZ matrix f(y,z), meaning for y outcomes along
|
| | 59 | % the column dimension.
|
| | 60 | %
|
| | 61 | % @return mt_choice_prob_byYA matrix f(y,a), meaning for y outcomes along
|
| | 62 | % the row dimension.
|
| | 63 | %
|
| | 64 |
|
| | 65 | %% Default
|
| | 66 | % use binomial as test case, z maps to binomial win prob, remember binom
|
| | 67 | % approximates normal.
|
| | 68 |
|
< 0.001 | 4 | 69 | params_len = length(varargin);
|
< 0.001 | 4 | 70 | bl_input_override = 0;
|
< 0.001 | 4 | 71 | if (params_len == 4)
|
< 0.001 | 4 | 72 | bl_input_override = varargin{4};
|
< 0.001 | 4 | 73 | end
|
| | 74 |
|
< 0.001 | 4 | 75 | if (bl_input_override)
|
| | 76 |
|
| | 77 | % if invoked from outside overrid fully
|
< 0.001 | 4 | 78 | [st_var_name, mt_choice_bystates, mt_dist_bystates, ~] = varargin{:};
|
< 0.001 | 4 | 79 | bl_display_drvm2outcomes = false;
|
< 0.001 | 4 | 80 | bl_drvm2outcomes_vec = true;
|
| | 81 |
|
| | 82 | else
|
| | 83 |
|
| | 84 | clear all;
|
| | 85 | close all;
|
| | 86 |
|
| | 87 | it_states = 6;
|
| | 88 | it_shocks = 5;
|
| | 89 | fl_binom_n = it_states-1;
|
| | 90 | ar_binom_p = (1:(it_shocks))./(it_shocks+2);
|
| | 91 | ar_binom_x = 0:1:(it_states-1);
|
| | 92 |
|
| | 93 | % a
|
| | 94 | ar_choice_unique_sorted_byY = ar_binom_x;
|
| | 95 | % f(z)
|
| | 96 | ar_binom_p_prob = binopdf(0:(it_shocks-1), it_shocks-1, 0.5);
|
| | 97 | % f(a,z), mass for a, z
|
| | 98 | mt_dist_bystates = zeros([it_states, it_shocks]);
|
| | 99 | for it_z=1:it_shocks
|
| | 100 | % f(a|z)
|
| | 101 | f_a_condi_z = binopdf(ar_binom_x, fl_binom_n, ar_binom_p(it_z));
|
| | 102 | % f(z)
|
| | 103 | f_z = ar_binom_p_prob(it_z);
|
| | 104 | % f(a,z)=f(a|z)*f(z)
|
| | 105 | mt_dist_bystates(:, it_z) = f_a_condi_z*f_z;
|
| | 106 | end
|
| | 107 |
|
| | 108 | % y(a,z), some non-smooth structure
|
| | 109 | rng(123);
|
| | 110 | mt_choice_bystates = ar_binom_x' - 0.01*ar_binom_x'.^2 + ar_binom_p - 0.5*ar_binom_p.^2 + rand([it_states, it_shocks]);
|
| | 111 | mt_choice_bystates = round(mt_choice_bystates*2);
|
| | 112 |
|
| | 113 | % display
|
| | 114 | st_var_name = 'binomtest';
|
| | 115 |
|
| | 116 | % display
|
| | 117 | bl_display_drvm2outcomes = true;
|
| | 118 | bl_drvm2outcomes_vec = true;
|
| | 119 |
|
< 0.001 | 4 | 120 | end
|
| | 121 |
|
| | 122 | %% 1. Generate Y(a) and f(y) and f(y,a) and f(y,z)
|
| | 123 | % 1. Get Choice Matrix (choice or outcomes given choices)
|
| | 124 | % see end of
|
| | 125 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
|
| | 126 | % ff_az_vf_vecsv> outcomes in result_map are cells with two elements,
|
| | 127 | % first element is y(a,z), second element will be f(y) and y, generated
|
| | 128 | % here.
|
< 0.001 | 4 | 129 | ar_choice_cur_bystates = mt_choice_bystates(:);
|
| | 130 |
|
| | 131 | %% 2. Sort and Generate Unique
|
| | 132 |
|
0.001 | 4 | 133 | ar_choice_bystates_sorted = sort(ar_choice_cur_bystates);
|
0.002 | 4 | 134 | ar_choice_unique_sorted_byY = unique(ar_choice_bystates_sorted);
|
| | 135 |
|
| | 136 | %% 3. Sum up Density at each element of ar_choice
|
| | 137 |
|
< 0.001 | 4 | 138 | ar_choice_prob_byY = zeros([length(ar_choice_unique_sorted_byY),1]);
|
< 0.001 | 4 | 139 | mt_choice_prob_byYZ = zeros([length(ar_choice_unique_sorted_byY), size(mt_dist_bystates,2)]);
|
< 0.001 | 4 | 140 | mt_choice_prob_byYA = zeros([length(ar_choice_unique_sorted_byY), size(mt_dist_bystates,1)]);
|
| | 141 |
|
< 0.001 | 4 | 142 | if (~bl_drvm2outcomes_vec)
|
| | 143 |
|
| | 144 | %% 2. Looped solution
|
| | 145 |
|
| | 146 | for it_z_i = 1:size(mt_dist_bystates,2)
|
| | 147 | for it_a_j = 1:size(mt_dist_bystates,1)
|
| | 148 |
|
| | 149 | % get f(a,z) and c(a,z)
|
| | 150 | fl_mass_curstate = mt_dist_bystates(it_a_j, it_z_i);
|
| | 151 | fl_choice_cur = mt_choice_bystates(it_a_j, it_z_i);
|
| | 152 |
|
| | 153 | % add f(a,z) to f(c(a,z))
|
| | 154 | ar_choice_in_unique_idx = (ar_choice_unique_sorted_byY == fl_choice_cur);
|
| | 155 |
|
| | 156 | % add probability to p(y)
|
| | 157 | ar_choice_prob_byY(ar_choice_in_unique_idx) = ar_choice_prob_byY(ar_choice_in_unique_idx) + fl_mass_curstate;
|
| | 158 |
|
| | 159 | % add probability to p(y,z)
|
| | 160 | mt_choice_prob_byYZ(ar_choice_in_unique_idx, it_z_i) = mt_choice_prob_byYZ(ar_choice_in_unique_idx, it_z_i) + fl_mass_curstate;
|
| | 161 |
|
| | 162 | % add probability to p(y,a)
|
| | 163 | mt_choice_prob_byYA(ar_choice_in_unique_idx, it_a_j) = mt_choice_prob_byYA(ar_choice_in_unique_idx, it_a_j) + fl_mass_curstate;
|
| | 164 | end
|
| | 165 | end
|
| | 166 |
|
< 0.001 | 4 | 167 | else
|
| | 168 |
|
| | 169 | %% 3 Vectorized Solution
|
| | 170 |
|
| | 171 | % Generating Unique Index
|
0.005 | 4 | 172 | [~, ~, ar_idx_of_unique] = unique(mt_choice_bystates(:));
|
< 0.001 | 4 | 173 | mt_idx_of_unique = reshape(ar_idx_of_unique, size(mt_choice_bystates));
|
| | 174 |
|
| | 175 | %% 3.1 Vectorized solution for f(Y)
|
| | 176 |
|
< 0.001 | 4 | 177 | ar_choice_prob_byY = accumarray(ar_idx_of_unique, mt_dist_bystates(:));
|
| | 178 |
|
| | 179 | %% 3.2 Vectorized solution for f(Y,z)
|
| | 180 |
|
< 0.001 | 4 | 181 | for it_z_i = 1:size(mt_dist_bystates,2)
|
| | 182 |
|
| | 183 | % f(y,z) for one z
|
0.003 | 60 | 184 | ar_choice_prob_byY_curZ = accumarray(mt_idx_of_unique(:, it_z_i), mt_dist_bystates(:, it_z_i), [length(ar_choice_unique_sorted_byY), 1]);
|
| | 185 | % add probability to p(y,z)
|
0.002 | 60 | 186 | mt_choice_prob_byYZ(:, it_z_i) = ar_choice_prob_byY_curZ;
|
| | 187 |
|
< 0.001 | 60 | 188 | end
|
| | 189 |
|
| | 190 | %% 3.3 Vectorized solution for f(Y,a)
|
| | 191 |
|
< 0.001 | 4 | 192 | for it_a_j = 1:size(mt_dist_bystates,1)
|
| | 193 |
|
| | 194 | % f(y,z) for one z
|
0.117 | 5100 | 195 | mt_choice_prob_byY_curA = accumarray(mt_idx_of_unique(it_a_j, :)', mt_dist_bystates(it_a_j, :)', [length(ar_choice_unique_sorted_byY), 1]);
|
| | 196 | % add probability to p(y,a)
|
0.125 | 5100 | 197 | mt_choice_prob_byYA(:, it_a_j) = mt_choice_prob_byY_curA;
|
| | 198 |
|
< 0.001 | 5100 | 199 | end
|
| | 200 |
|
< 0.001 | 4 | 201 | end
|
| | 202 |
|
| | 203 |
|
| | 204 | %% Display
|
< 0.001 | 4 | 205 | if (bl_display_drvm2outcomes)
|
| | 206 |
|
| | 207 | disp('INPUT f(a,z): mt_dist_bystates');
|
| | 208 | disp(mt_dist_bystates);
|
| | 209 |
|
| | 210 | disp('INPUT y(a,z): mt_choice_bystates');
|
| | 211 | disp(mt_choice_bystates);
|
| | 212 |
|
| | 213 | disp('OUTPUT f(y): ar_choice_prob_byY');
|
| | 214 | disp(ar_choice_prob_byY);
|
| | 215 |
|
| | 216 | disp('OUTPUT f(y,z): mt_choice_prob_byYZ');
|
| | 217 | disp(mt_choice_prob_byYZ);
|
| | 218 |
|
| | 219 | disp('OUTPUT f(y,a): mt_choice_prob_byYA');
|
| | 220 | disp(mt_choice_prob_byYA);
|
| | 221 |
|
| | 222 | disp('OUTPUT f(y) and y in table: tb_choice_drv_cur_byY');
|
| | 223 | tb_choice_drv_cur_byY = table(ar_choice_unique_sorted_byY, ar_choice_prob_byY);
|
| | 224 | tb_choice_drv_cur_byY.Properties.VariableNames = matlab.lang.makeValidName([string([char(st_var_name) ' outcomes']), 'prob mass function']);
|
| | 225 | disp(tb_choice_drv_cur_byY);
|
| | 226 |
|
| | 227 | end
|
< 0.001 | 4 | 228 | end
|