time | Calls | line |
---|
| | 7 | function [result_map] = ff_az_ds_post_stats(varargin)
|
| | 8 | %% FF_AZ_DS_POST_STATS post ff_az_ds statistics generation
|
| | 9 | % Having derived f(a,z) the probability mass function of the joint discrete
|
| | 10 | % random variables, we now obtain distributional statistics. Note that we
|
| | 11 | % know f(a,z), and we also know relevant policy functions a'(a,z), c(a,z),
|
| | 12 | % or other policy functions. We can simulate any choices that are a
|
| | 13 | % function of the random variables (a,z), using f(a,z)
|
| | 14 | %
|
| | 15 | % parameter structure provides a list of
|
| | 16 | %
|
| | 17 | % # from result_map('ar_st_pol_names'), get list of outcome matrix on state
|
| | 18 | % space
|
| | 19 | % # simulate each outcome using f(a,z) for probability draws
|
| | 20 | % # compute key statistics: (1) mean (expectation=sum) (2) sd (3) min and
|
| | 21 | % max (4) iqr (5) fraction = 0 (6) percentiles including: 99.9, 99, 95,
|
| | 22 | % every 5 in between 5, 1, 0.01.
|
| | 23 | %
|
| | 24 | % Uses fake binomial data when file is invoke with defaults.
|
| | 25 | %
|
| | 26 | % @param param_map container parameter container
|
| | 27 | %
|
| | 28 | % @param support_map container support container
|
| | 29 | %
|
| | 30 | % @param result_map container contains policy function matrix, value
|
| | 31 | % function matrix, iteration results
|
| | 32 | %
|
| | 33 | % @param mt_dist_az matrix N by M where N are asset states and M are shock
|
| | 34 | % states, the f(a,z) probability mass function derived earlier in ff_az_ds
|
| | 35 | % or ff_az_ds_vec
|
| | 36 | %
|
| | 37 | % @return result_map container with statistics added to result_map
|
| | 38 | %
|
| | 39 | % * the first element of each of these cell array is y(a,z), the
|
| | 40 | % outcome/choice at the state space points
|
| | 41 | % * the second element of the cell is another container, which contains
|
| | 42 | % statistics computed for f(y) based on y(a,z) and f(a,z), f(y) is the
|
| | 43 | % probability mass function for outcome y given the stationary distribution
|
| | 44 | % f(a,z). The second element container also includes f(y) itself as well as
|
| | 45 | % f(y,z).
|
| | 46 | % * additionally, result_map also stores some of the statistics for
|
| | 47 | % different variables jointly together. (a) *tb_outcomes_meansdperc*: where
|
| | 48 | % each row is a different outcome of the model, and each table column
|
| | 49 | % stores a different statistics of interest. (b) *tb_outcomes_fracheld*:
|
| | 50 | % which measures the fraction of asset held by different people.
|
| | 51 | %
|
| | 52 | % @example
|
| | 53 | %
|
| | 54 | % bl_input_override = true;
|
| | 55 | % result_map = ff_az_ds_post_stats(support_map, result_map, mt_dist_az, bl_input_override);
|
| | 56 | %
|
| | 57 | % @include
|
| | 58 | %
|
| | 59 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html fft_disc_rand_var_stats>
|
| | 60 | % * <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html fft_disc_rand_var_mass2outcomes>
|
| | 61 | %
|
| | 62 |
|
| | 63 | %% Default
|
| | 64 | % use binomial as test case, z maps to binomial win prob, remember binom
|
| | 65 | % approximates normal.
|
| | 66 |
|
< 0.001 | 1 | 67 | params_len = length(varargin);
|
< 0.001 | 1 | 68 | bl_input_override = 0;
|
< 0.001 | 1 | 69 | if (params_len == 4)
|
< 0.001 | 1 | 70 | bl_input_override = varargin{4};
|
< 0.001 | 1 | 71 | end
|
| | 72 |
|
< 0.001 | 1 | 73 | if (bl_input_override)
|
| | 74 | % if invoked from outside overrid fully
|
< 0.001 | 1 | 75 | [support_map, result_map, mt_dist_az, ~] = varargin{:};
|
| | 76 | else
|
| | 77 | clear all;
|
| | 78 | close all;
|
| | 79 |
|
| | 80 | it_states = 6;
|
| | 81 | it_shocks = 5;
|
| | 82 | fl_binom_n = it_states-1;
|
| | 83 | ar_binom_p = (1:(it_shocks))./(it_shocks+2);
|
| | 84 | ar_binom_x = 0:1:(it_states-1);
|
| | 85 |
|
| | 86 | % a
|
| | 87 | ar_choice_unique_sorted_byY = ar_binom_x;
|
| | 88 | % f(z)
|
| | 89 | ar_binom_p_prob = binopdf(0:(it_shocks-1), it_shocks-1, 0.5);
|
| | 90 | % f(a,z), mass for a, z
|
| | 91 | mt_dist_az = zeros([it_states, it_shocks]);
|
| | 92 | for it_z=1:it_shocks
|
| | 93 | % f(a|z)
|
| | 94 | f_a_condi_z = binopdf(ar_binom_x, fl_binom_n, ar_binom_p(it_z));
|
| | 95 | % f(z)
|
| | 96 | f_z = ar_binom_p_prob(it_z);
|
| | 97 | % f(a,z)=f(a|z)*f(z)
|
| | 98 | mt_dist_az(:, it_z) = f_a_condi_z*f_z;
|
| | 99 | end
|
| | 100 |
|
| | 101 | % y(a,z), some non-smooth structure
|
| | 102 | rng(123);
|
| | 103 | mt_pol_a = ar_binom_x' - 0.01*ar_binom_x'.^2 + ar_binom_p - 0.5*ar_binom_p.^2 + rand([it_states, it_shocks]);
|
| | 104 | mt_pol_a = round(mt_pol_a*2);
|
| | 105 |
|
| | 106 | mt_pol_c = ar_binom_x' + ar_binom_p + rand([it_states, it_shocks]);
|
| | 107 | mt_pol_c = round(mt_pol_c*3);
|
| | 108 |
|
| | 109 | % Generate result_map
|
| | 110 | result_map = containers.Map('KeyType','char', 'ValueType','any');
|
| | 111 | result_map('cl_mt_pol_a') = {mt_pol_a, zeros(1)};
|
| | 112 | result_map('cl_mt_pol_c') = {mt_pol_c, zeros(1)};
|
| | 113 | result_map('ar_st_pol_names') = ["cl_mt_pol_a", "cl_mt_pol_c"];
|
| | 114 |
|
| | 115 | % support_map
|
| | 116 | support_map = containers.Map('KeyType','char', 'ValueType','any');
|
| | 117 | support_map('bl_display_final_dist') = true;
|
| | 118 |
|
< 0.001 | 1 | 119 | end
|
| | 120 |
|
| | 121 | %% Parse
|
| | 122 |
|
| | 123 | % support_map
|
< 0.001 | 1 | 124 | params_group = values(support_map, {'bl_display_final_dist', 'bl_display_final_dist_detail'});
|
< 0.001 | 1 | 125 | [bl_display_final_dist, bl_display_final_dist_detail] = params_group{:};
|
< 0.001 | 1 | 126 | if (bl_display_final_dist_detail)
|
| | 127 | bl_display_drvstats = true;
|
< 0.001 | 1 | 128 | else
|
< 0.001 | 1 | 129 | bl_display_drvstats = false;
|
< 0.001 | 1 | 130 | end
|
| | 131 |
|
| | 132 | % result_map
|
< 0.001 | 1 | 133 | params_group = values(result_map, {'ar_st_pol_names'});
|
< 0.001 | 1 | 134 | [ar_st_pol_names] = params_group{:};
|
| | 135 |
|
| | 136 | %% *f(y), f(c), f(a)*: Generate Key Distributional Statistics for Each outcome
|
| | 137 | % Loop over outcomes, see end of
|
| | 138 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
|
| | 139 | % ff_az_vf_vecsv> where these are created
|
< 0.001 | 1 | 140 | for it_outcome_ctr=1:length(ar_st_pol_names)
|
| | 141 |
|
| | 142 | %% *f(y), f(c), f(a)*: Find p(outcome(states)), proability mass function for each outcome
|
| | 143 | % Using from tools:
|
| | 144 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html
|
| | 145 | % fft_disc_rand_var_mass2outcomes>, compute unique sorted outcomes for
|
| | 146 | % y(a,z) and find:
|
| | 147 | %
|
| | 148 | % $$ p(y,z) = \sum_{a} \left(1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 149 | %
|
| | 150 | % $$ p(y,a) = \sum_{z} \left(1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 151 | %
|
| | 152 | % $$ p(Y=y) = \sum_{a,z} \left( 1\left\{Y(a,z)=y\right\} \cdot p(a,z) \right)$$
|
| | 153 | %
|
| | 154 | % note: sum(mt_dist_az, 2) = result_map('cl_mt_pol_a'){2}, but not at
|
| | 155 | % small simulation grids. These two might be different because pol_a is
|
| | 156 | % based on a choices, mt_dist_az is based on a states
|
| | 157 | %
|
| | 158 | % see end of
|
| | 159 | % <https://fanwangecon.github.io/CodeDynaAsset/m_az/solve/html/ff_az_vf_vecsv.html
|
| | 160 | % ff_az_vf_vecsv> outcomes in result_map are cells with two elements,
|
| | 161 | % first element is y(a,z), second element will be f(y) and y, generated
|
| | 162 | % here.
|
| | 163 | %
|
| | 164 |
|
< 0.001 | 4 | 165 | st_cur_output_key = ar_st_pol_names(it_outcome_ctr);
|
< 0.001 | 4 | 166 | cl_mt_choice_cur = result_map(st_cur_output_key);
|
< 0.001 | 4 | 167 | mt_choice_cur = cl_mt_choice_cur{1};
|
| | 168 |
|
| | 169 | % run function from tools: fft_disc_rand_var_mass2outcomes
|
| | 170 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_mass2outcomes.html>
|
< 0.001 | 4 | 171 | bl_input_override = true;
|
0.267 | 4 | 172 | [ar_choice_prob_byY, ar_choice_unique_sorted_byY, mt_choice_prob_byYZ, mt_choice_prob_byYA] = ...
|
| 4 | 173 | fft_disc_rand_var_mass2outcomes(st_cur_output_key, mt_choice_cur, mt_dist_az, bl_input_override);
|
| | 174 |
|
| | 175 | %% *f(y), f(c), f(a)*: Compute Statistics for outcomes
|
| | 176 | % Using from tools:
|
| | 177 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html
|
| | 178 | % fft_disc_rand_var_stats>, compute these outcomes:
|
| | 179 | %
|
| | 180 | % * $\mu_Y = E(Y) = \sum_{y} p(Y=y) \cdot y $
|
| | 181 | % * $\sigma_Y = \sqrt{ \sum_{y} p(Y=y) \cdot \left( y - \mu_y \right)^2}$
|
| | 182 | % * $p(y=0)$
|
| | 183 | % * $p(y=\max(y))$
|
| | 184 | % * percentiles: $min_{y} \left\{ P(Y \le y) - percentile \mid P(Y \le y) \ge percentile \right\}$
|
| | 185 | % * fraction of outcome held by up to percentiles: $E(Y<y)/E(Y)$
|
| | 186 | %
|
| | 187 |
|
| | 188 | % run function fft_disc_rand_var_stats.m from tools:
|
| | 189 | % <https://fanwangecon.github.io/CodeDynaAsset/tools/html/fft_disc_rand_var_stats.html>
|
0.064 | 4 | 190 | [ds_stats_map] = fft_disc_rand_var_stats(st_cur_output_key, ar_choice_unique_sorted_byY', ar_choice_prob_byY', bl_display_drvstats);
|
| | 191 |
|
| | 192 | % prcess results
|
| | 193 | % retrieve scalar statistics:
|
< 0.001 | 4 | 194 | fl_choice_mean = ds_stats_map('fl_choice_mean');
|
< 0.001 | 4 | 195 | fl_choice_sd = ds_stats_map('fl_choice_sd');
|
< 0.001 | 4 | 196 | fl_choice_coefofvar = ds_stats_map('fl_choice_coefofvar');
|
< 0.001 | 4 | 197 | fl_choice_min = ds_stats_map('fl_choice_min');
|
< 0.001 | 4 | 198 | fl_choice_max = ds_stats_map('fl_choice_max');
|
< 0.001 | 4 | 199 | fl_choice_prob_zero = ds_stats_map('fl_choice_prob_zero');
|
< 0.001 | 4 | 200 | fl_choice_prob_below_zero = ds_stats_map('fl_choice_prob_below_zero');
|
< 0.001 | 4 | 201 | fl_choice_prob_above_zero = ds_stats_map('fl_choice_prob_above_zero');
|
< 0.001 | 4 | 202 | fl_choice_prob_min = ds_stats_map('fl_choice_prob_min');
|
< 0.001 | 4 | 203 | fl_choice_prob_max = ds_stats_map('fl_choice_prob_max');
|
| | 204 | % retrieve distributional array stats
|
< 0.001 | 4 | 205 | ar_choice_percentiles = ds_stats_map('ar_choice_percentiles');
|
< 0.001 | 4 | 206 | ar_choice_perc_fracheld = ds_stats_map('ar_choice_perc_fracheld');
|
| | 207 |
|
| | 208 | %% *f(y), f(c), f(a)*: Store Statistics Specific to Each Outcome
|
| | 209 | % see intro section
|
| | 210 |
|
| | 211 | % Append prob mass functions to ds_stats_map
|
< 0.001 | 4 | 212 | ds_stats_map('mt_choice_prob_byYZ') = mt_choice_prob_byYZ;
|
< 0.001 | 4 | 213 | ds_stats_map('mt_choice_prob_byYA') = mt_choice_prob_byYA;
|
< 0.001 | 4 | 214 | ds_stats_map('ar_choice_unique_sorted_byY') = ar_choice_unique_sorted_byY;
|
< 0.001 | 4 | 215 | ds_stats_map('ar_choice_prob_byY') = ar_choice_prob_byY;
|
| | 216 | % ds_stats_map is second element of cell for the key for the variable
|
| | 217 | % in result_map
|
< 0.001 | 4 | 218 | cl_mt_choice_cur{2} = ds_stats_map;
|
< 0.001 | 4 | 219 | result_map(st_cur_output_key) = cl_mt_choice_cur;
|
| | 220 |
|
| | 221 | % key stats
|
< 0.001 | 4 | 222 | ar_keystats = [fl_choice_mean fl_choice_sd fl_choice_coefofvar fl_choice_min fl_choice_max ...
|
| 4 | 223 | fl_choice_prob_zero fl_choice_prob_below_zero fl_choice_prob_above_zero ...
|
| 4 | 224 | fl_choice_prob_min fl_choice_prob_max ar_choice_percentiles];
|
< 0.001 | 4 | 225 | cl_outcome_names(it_outcome_ctr) = st_cur_output_key;
|
< 0.001 | 4 | 226 | if (it_outcome_ctr == 1)
|
< 0.001 | 1 | 227 | mt_outcomes_meansdperc = ar_keystats;
|
< 0.001 | 1 | 228 | mt_outcomes_fracheld = ar_choice_perc_fracheld;
|
< 0.001 | 3 | 229 | else
|
< 0.001 | 3 | 230 | mt_outcomes_meansdperc = [mt_outcomes_meansdperc; ar_keystats];
|
< 0.001 | 3 | 231 | mt_outcomes_fracheld = [mt_outcomes_fracheld; ar_choice_perc_fracheld];
|
< 0.001 | 4 | 232 | end
|
| | 233 |
|
< 0.001 | 4 | 234 | end
|
| | 235 |
|
| | 236 | %% *f(y), f(c), f(a)*: Store Statistics Shared Table All Outcomes
|
| | 237 |
|
| | 238 | % Add to result_map
|
< 0.001 | 1 | 239 | result_map('mt_outcomes_meansdperc') = mt_outcomes_meansdperc;
|
< 0.001 | 1 | 240 | result_map('mt_outcomes_fracheld') = mt_outcomes_fracheld;
|
| | 241 |
|
| | 242 | % Display
|
< 0.001 | 1 | 243 | if (bl_display_final_dist)
|
| | 244 |
|
| | 245 | disp('xxx All Variables PERCENTILES AND STATS xxx')
|
| | 246 |
|
| | 247 | % Process mean and and percentiles
|
| | 248 | tb_outcomes_meansdperc = array2table(mt_outcomes_meansdperc);
|
| | 249 | ar_fl_percentiles = ds_stats_map('ar_fl_percentiles');
|
| | 250 | cl_col_names = ['mean', 'sd', 'coefofvar', 'min', 'max', ...
|
| | 251 | 'pYis0', 'pYls0', 'pYgr0', 'pYisMINY', 'pYisMAXY', strcat('p', string(ar_fl_percentiles))];
|
| | 252 | tb_outcomes_meansdperc.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
|
| | 253 | tb_outcomes_meansdperc.Properties.RowNames = matlab.lang.makeValidName(cl_outcome_names);
|
| | 254 |
|
| | 255 | disp('tb_outcomes_meansdperc: mean, sd, percentiles')
|
| | 256 | disp(tb_outcomes_meansdperc);
|
| | 257 | end
|
| | 258 |
|
< 0.001 | 1 | 259 | if (bl_display_final_dist_detail)
|
| | 260 |
|
| | 261 | disp('xxx All Variables Fraction of Y Held up to Percentile xxx')
|
| | 262 |
|
| | 263 | % Process Aset Held by up to percentiles
|
| | 264 | tb_outcomes_fracheld = array2table(mt_outcomes_fracheld);
|
| | 265 | cl_col_names = [strcat('fracByP', string(ar_fl_percentiles))];
|
| | 266 | tb_outcomes_fracheld.Properties.VariableNames = matlab.lang.makeValidName(cl_col_names);
|
| | 267 | tb_outcomes_fracheld.Properties.RowNames = matlab.lang.makeValidName(cl_outcome_names);
|
| | 268 |
|
| | 269 | disp('tb_outcomes_fracheld: fraction of asset/income/etc held by hh up to this percentile')
|
| | 270 | disp(tb_outcomes_fracheld);
|
| | 271 |
|
| | 272 | end
|
| | 273 |
|
| | 274 |
|
< 0.001 | 1 | 275 | end
|
Other subfunctions in this file are not included in this listing.