Chapter 3 Summarize Policy and Value

3.1 FF_SUMM_ND_ARRAY Examples

Go back to fan’s MEconTools Toolbox (bookdown), Matlab Code Examples Repository (bookdown), or Math for Econ with Matlab Repository (bookdown).

Examples](https://fanwangecon.github.io/M4Econ/), or** Dynamic Asset This is the example vignette for function: ff_summ_nd_array from the MEconTools Package. This function summarizes policy and value functions over states.

3.1.1 Test FF_SUMM_ND_ARRAY Defaults

Call the function with defaults.

ff_summ_nd_array();

xxx  Summ over (a,z), condi age as cols, kids/marriage as rows  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    marry    kids    mean_age_18    mean_age_19    mean_age_20    mean_age_21
    _____    _____    ____    ___________    ___________    ___________    ___________

      1        0       1        0.53448        0.44448         0.5053        0.52914  
      2        1       1         0.4564        0.44512        0.44998        0.51775  
      3        0       2        0.52415        0.49903        0.48403        0.44429  
      4        1       2        0.49235        0.43684        0.44717        0.45226  
      5        0       3         0.4668        0.52676        0.49386        0.51855  
      6        1       3        0.47097        0.60345        0.58319        0.46238  
      7        0       4        0.55484        0.53601        0.53069        0.49323  
      8        1       4         0.5283        0.44091        0.53317        0.51062  

3.1.2 Test FF_SUMM_ND_ARRAY with Random 2 Dimensional Matrix

Summarize over 6 dimensional array, iteratively change how many dimensions to group over.

First, generate matrix:

st_title = "Random 2D dimensional Array Testing Summarizing";
rng(123)
mn_polval = rand(5,4);
bl_print_table = true;
ar_st_stats = ["mean"];
cl_mp_datasetdesc = {};
cl_mp_datasetdesc{1} = containers.Map({'name', 'labval'}, ...
    {'a', linspace(0,1,size(mn_polval,1))});
cl_mp_datasetdesc{2} = containers.Map({'name', 'labval'}, ...
    {'z', linspace(-1,1,size(mn_polval,2))});
disp(mn_polval);

    0.6965    0.4231    0.3432    0.7380
    0.2861    0.9808    0.7290    0.1825
    0.2269    0.6848    0.4386    0.1755
    0.5513    0.4809    0.0597    0.5316
    0.7195    0.3921    0.3980    0.5318

Second, show the entire matrix (no labels):

it_aggd = 0; 
bl_row = 1; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row);

xxx  Random 2D dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    vardim2    mean_vardim1_1    mean_vardim1_2    mean_vardim1_3    mean_vardim1_4    mean_vardim1_5
    _____    _______    ______________    ______________    ______________    ______________    ______________

      1         1          0.69647           0.28614           0.22685            0.55131          0.71947    
      2         2          0.42311           0.98076           0.68483            0.48093          0.39212    
      3         3          0.34318           0.72905           0.43857           0.059678          0.39804    
      4         4            0.738           0.18249           0.17545            0.53155          0.53183    

Third, rotate row and column, and now with labels:

it_aggd = 0; 
bl_row = 1; 
ar_permute = [2,1];
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row, ...
    cl_mp_datasetdesc, ar_permute);

xxx  Random 2D dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group     a      mean_z__1    mean_z__0_33333    mean_z_0_33333    mean_z_1
    _____    ____    _________    _______________    ______________    ________

      1         0     0.69647         0.42311            0.34318         0.738 
      2      0.25     0.28614         0.98076            0.72905       0.18249 
      3       0.5     0.22685         0.68483            0.43857       0.17545 
      4      0.75     0.55131         0.48093           0.059678       0.53155 
      5         1     0.71947         0.39212            0.39804       0.53183 

Fourth, dimension one as columns, average over dim 2:

it_aggd = 1; 
bl_row = 1; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row, ...
    cl_mp_datasetdesc);

xxx  Random 2D dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    x    mean_z__1    mean_z__0_33333    mean_z_0_33333    mean_z_1
    _____    _    _________    _______________    ______________    ________

      1      1     0.49605         0.59235            0.3937        0.43186 

Fifth, dimension one as rows, average over dim 2:

it_aggd = 1; 
bl_row = 0; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row, ...
    cl_mp_datasetdesc);

xxx  Random 2D dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group       z         sum       mean        std      coefvari      min         max  
    _____    ________    ______    _______    _______    ________    ________    _______

      1            -1    2.4802    0.49605    0.22895     2.1666      0.22685    0.71947
      2      -0.33333    2.9617    0.59235    0.24524     2.4154      0.39212    0.98076
      3       0.33333    1.9685     0.3937    0.23907     1.6468     0.059678    0.72905
      4             1    2.1593    0.43186    0.24575     1.7573      0.17545      0.738

Sixth, dimension two as rows, average over dim 1:

ar_permute = [2,1];
it_aggd = 1; 
bl_row = 0; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row, ...
    cl_mp_datasetdesc, ar_permute);

xxx  Random 2D dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group     a       sum       mean        std      coefvari      min         max  
    _____    ____    ______    _______    _______    ________    ________    _______

      1         0    2.2007    0.55019    0.19636     2.8019      0.34318      0.738
      2      0.25    2.1784    0.54461    0.37514     1.4518      0.18249    0.98076
      3       0.5    1.5257    0.38143    0.23212     1.6432      0.17545    0.68483
      4      0.75    1.6235    0.40587    0.23269     1.7443     0.059678    0.55131
      5         1    2.0415    0.51036    0.15361     3.3226      0.39212    0.71947

3.1.3 Test FF_SUMM_ND_ARRAY with Random 6 Dimensional Matrix

Summarize over 6 dimensional array, iteratively change how many dimensions to group over.

First, generate matrix:

st_title = "Random ND dimensional Array Testing Summarizing";
rng(123)
mn_polval = rand(8,7,6,5,4,3);
bl_print_table = true;
ar_st_stats = ["mean"];

Second, summarize over the first four dimensions, row group others:

it_aggd = 4; 
bl_row = 0; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row);

xxx  Random ND dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    vardim5    vardim6     sum       mean        std      coefvari       min          max  
    _____    _______    _______    ______    _______    _______    ________    __________    _______

      1         1          1       836.78    0.49808    0.29255     1.7026     8.1888e-05    0.99964
      2         2          1       842.15    0.50128    0.28968     1.7305     6.7838e-05    0.99936
      3         3          1       831.45    0.49491    0.28851     1.7154     0.00091373    0.99989
      4         4          1        843.9    0.50232    0.28154     1.7842     0.00012471    0.99731
      5         1          2       838.99     0.4994     0.2911     1.7156     0.00029749    0.99938
      6         2          2       830.81    0.49453    0.28634     1.7271     0.00027113     0.9992
      7         3          2       832.59    0.49559    0.28682     1.7279     0.00035994    0.99936
      8         4          2       820.42    0.48835    0.29032     1.6821     0.00096259    0.99896
      9         1          3       870.56    0.51819    0.29111     1.7801      0.0010616    0.99951
     10         2          3       854.68    0.50874    0.28458     1.7877       0.001884    0.99965
     11         3          3       838.29    0.49898     0.2891      1.726      0.0019192    0.99945
     12         4          3       842.83    0.50169     0.2877     1.7438     0.00016871    0.99963

Third, summarize over the first four dimensions, column group 5th, and row group others:

it_aggd = 4; 
bl_row = 1; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ["sum"], it_aggd, bl_row);

xxx  Random ND dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    vardim6    sum_vardim5_1    sum_vardim5_2    sum_vardim5_3    sum_vardim5_4
    _____    _______    _____________    _____________    _____________    _____________

      1         1          836.78           842.15           831.45            843.9    
      2         2          838.99           830.81           832.59           820.42    
      3         3          870.56           854.68           838.29           842.83    

Fourth, summarize over the first five dimensions, column group 6th, no row groups:

it_aggd = 5;
bl_row = 1; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ["mean", "std"], it_aggd, bl_row);

xxx  Random ND dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    x    mean_vardim6_1    mean_vardim6_2    mean_vardim6_3    std_vardim6_1    std_vardim6_2    std_vardim6_3
    _____    _    ______________    ______________    ______________    _____________    _____________    _____________

      1      1       0.49915           0.49447            0.5069           0.28805          0.28862          0.28816   

Fifth, summarize over all six dimensions, summary statistics over the entire dataframe:

it_aggd = 6;
bl_row = 0; 
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row);

xxx  Random ND dimensional Array Testing Summarizing  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    x     sum      mean        std      coefvari       min          max  
    _____    _    _____    _______    _______    ________    __________    _______

      1      1    10083    0.50017    0.28831     1.7349     6.7838e-05    0.99989

3.1.4 Test FF_SUMM_ND_ARRAY with Random 7 Dimensional Matrix with All Parameters

Given a random seven dimensional matrix, average over the 2nd, 4th and 5th dimensionals. Show as row groups the 3, 6 and 7th dimensions, and row groups the 1st dimension. Show Coefficient of Variation only.

st_title = "avg VALUE 2+4+5th dims. groups 3+6+7th dims, and row groups the 1st dim.";
rng(123)
mn_polval = rand(3,10,2,10,10,2,3);
ar_permute = [2,4,5,1,3,6,7];
bl_print_table = true;
ar_st_stats = ["coefvari"];
it_aggd = 3; % mean over 3 dims
bl_row = 1; % one var for row group
cl_mp_datasetdesc = {};
cl_mp_datasetdesc{1} = containers.Map({'name', 'labval'}, ...
    {'age', [18, 19, 20]});
cl_mp_datasetdesc{2} = containers.Map({'name', 'labval'}, ...
    {'savings', linspace(0,1,10)});
cl_mp_datasetdesc{3} = containers.Map({'name', 'labval'}, ...
    {'borrsave', [-1,+1]});
cl_mp_datasetdesc{4} = containers.Map({'name', 'labval'}, ...
    {'shocka', linspace(-5,5,10)});
cl_mp_datasetdesc{5} = containers.Map({'name', 'labval'}, ...
    {'shockb', linspace(-5,5,10)});
cl_mp_datasetdesc{6} = containers.Map({'name', 'labval'}, ...
    {'marry', [0,1]});
cl_mp_datasetdesc{7} = containers.Map({'name', 'labval'}, ...
    {'region', [1,2,3]});
% call function
ff_summ_nd_array(st_title, mn_polval, bl_print_table, ar_st_stats, it_aggd, bl_row, cl_mp_datasetdesc, ar_permute);

xxx  avg VALUE 2+4+5th dims. groups 3+6+7th dims, and row groups the 1st dim.  xxxxxxxxxxxxxxxxxxxxxxxxxxx
    group    borrsave    marry    region    cv_age_18    cv_age_19    cv_age_20
    _____    ________    _____    ______    _________    _________    _________

      1         -1         0        1        1.7607       1.7534       1.7065  
      2          1         0        1        1.6566       1.7501       1.7042  
      3         -1         1        1        1.6608       1.7658       1.7291  
      4          1         1        1         1.756       1.7479       1.7606  
      5         -1         0        2        1.7314       1.7506        1.786  
      6          1         0        2        1.7347        1.728        1.738  
      7         -1         1        2        1.7811        1.755       1.7568  
      8          1         1        2        1.7445       1.7398       1.7746  
      9         -1         0        3        1.7025       1.7286         1.69  
     10          1         0        3          1.74       1.7549       1.7356  
     11         -1         1        3        1.7147       1.7287       1.7341  
     12          1         1        3        1.7919       1.7313       1.7452