(Auto)Correlations and Transition Probabilities (InfHorz)

EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz() is a new command for computing autocorrelations and transition probabilites.

Say you have FnsToEvaluate.A and FnsToEvaluate.E. ‘A’ is assets and takes a large number of possible values (hundreds of values), while ‘E’ is employment and takes two possible values.

If you run EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz() you will get the autocorrelations for both of these. By default the only thing computed is one-period autocorrelation; it also reports the Mean, Standard Deviation and AutoCovariance, because these anyway had to be calculated as intermediate steps to get the autcorrelations.

If you set simoptions.transprobs={'E'} then you will also get the (one-period) transition probabilities between the two values of E. This should be a cell containing the names of some of the FnsToEvaluate, e.g., simoptions.transprobs={'E','K'}.

By default the command produces all the one-period correlations (and transition probabilities). You can set, e.g., simoptions.timehorizons=[5,10] to get the 5-period and 10-period correlations (and transition probabilities), this can be any vector of integers.

By default the transition probabilities are reported across all possible values. Since in this example K takes hundreds of values the transition probabilities between each individual value of K is probably useless info. You can set, e.g., simoptions.transprobquantiles=5 to get the 5x5 matrix of transition probabilities between 5 quantiles (so the quintiles) of K. Note that using this option changes the interpretations from ‘transition probabilites’ to ‘rank transition probabilities’.

If you want, e.g., the transition probabilities for E as a 2x2, as well as the quantile transitions for K (a 5x5), then you have to run the command twice. Anything you set for simoptions.transprobquantiles is applied to everything in simoptions.transprobs.

[There used to be a function called RankTransitionProbabilities. I have deleted it because this new function has more options, the old was was also based on simulations so was less accurate.]

Currently this is only available for InfHorz models. It will eventually come for FHorz and TPaths, let me know if you need this and I will prioritize.

PS. There is a separate command called
EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz()
which does the covariances and correlations between all the pairs of the FnsToEvaluate.

1 Like

Nice! I will check this command in my repo on Pijoan-Mas (2006). In the paper he author computes the correlation between hours and productivity.

(1) When I run it there is an error: it does not recognize simoptions.inheritanceasset.

Unrecognized field name "inheritanceasset".

Error in CreatePTransitionMatrix (line 6)
elseif simoptions.inheritanceasset==1
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz (line 139)
P=CreatePTransitionMatrix(Policy,l_d,l_a,n_d,n_a,n_z,N_a,N_semiz,N_z,N_e,pi_semiz,pi_z,pi_e,Parameters,simoptions);
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in main (line 248)
CorrTransProbs=EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz(StatDist, Policy, FnsToEvaluateCorr, Params, [], n_d, n_a, n_z, d_grid, a_grid, z_grid, pi_z, simoptions);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I can easily fix it by setting the following line in my script:

simoptions.inheritanceasset = 0;

but it should be handled by the toolkit automatically.

(2) I added these lines in my script for Pijoan-Mas:

FnsToEvaluateCorr.hours = @(d, aprime, a, z) d;
FnsToEvaluateCorr.productivity = @(d, aprime, a, z) z;
CorrTransProbs=EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz(StatDist, Policy, FnsToEvaluateCorr, Params, [], n_d, n_a, n_z, d_grid, a_grid, z_grid, pi_z, simoptions);

The results make sense

CorrTransProbs.hours

ans = 

  struct with fields:

               Mean: 0.35695
       StdDeviation: 0.071505
     AutoCovariance: 0.0048761
    AutoCorrelation: 0.95367

K>> CorrTransProbs.productivity

ans = 

  struct with fields:

               Mean: 1.1632
       StdDeviation: 0.67793
     AutoCovariance: 0.41977
    AutoCorrelation: 0.91336

but they don’t give the (cross-sectional) correlation between hours of work and idiosyncratic productivity. This moment is denoted as corr(h,\varepsilon) in Pijoan-Mas (2006) and is used to pin down one of the preference parameters. I wrote my own function to compute the correlation, see fun_corr.

Does this new toolkit command compute also the correlation matrix for the variables in function to evaluate?

Reference: Script main.m in repository: GitHub - aledinola/PijoanMasTaxes: Aiyagari model with endogenous labor (as in Pijoan-Mas 2006), with VFI toolkit

Hmm, I’ve renamed the function to be ‘AutoCorr’ instead of ‘Corr’ so that it is more obvious (and edited my earlier post to reflect this).

There is a separate command called
EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz()
which does the covariances and correlations between all the pairs of the FnsToEvaluate.

I need to extend both of these to FHorz, and then I can add them into the calibration and GMM estimation commands so that you can include all of these as targets. Between the two of them it covers all of the obvious moments that the calibration and estimation commands cannot yet handle so would be cool to have them there. (This will take quite a while, no immediate plan to do this, just a general plan to do this in the future.)

1 Like

Nice, I didn’t know about this function

I tried out but it fails with error in line 248 of PijoanMasTaxes/main.m at main · aledinola/PijoanMasTaxes · GitHub

Unrecognized field name "CovarianceWith".

Error in EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz (line 132)
        CrossSectionCorr.(AggVarNames{ff2}).CovarianceWith.(AggVarNames{ff1})=CrossSectionCorr.(AggVarNames{ff1}).CovarianceWith.(AggVarNames{ff2});
                                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in main (line 248)
Corr=EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz(StatDist,Policy,FnsToEvaluateCorr, Params,[], n_d, n_a, n_z, d_grid, a_grid, z_grid,simoptions);
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Should be fixed now.

1 Like

It works well now, thanks.

In the Pijoan-Mas replication, I want to compute the correlation between hours and productivity, and the correlation b/w wealth and productivity. Here is the code:

FnsToEvaluateCorr.hours = @(d, aprime, a, z) d;
FnsToEvaluateCorr.productivity = @(d, aprime, a, z) z;
FnsToEvaluateCorr.wealth = @(d, aprime, a, z) a;
Corr=EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz(StatDist,Policy,FnsToEvaluateCorr, Params,[], n_d, n_a, n_z, d_grid, a_grid, z_grid,simoptions);
%CorrTransProbs=EvalFnOnAgentDist_AutoCorrTransProbs_InfHorz(StatDist, Policy, FnsToEvaluateCorr, Params, [], n_d, n_a, n_z, d_grid, a_grid, z_grid, pi_z, simoptions);

corr_h_z = Corr.hours.CorrelationWith.productivity;
corr_a_z = Corr.wealth.CorrelationWith.productivity;

Note that I have defined a specific structure FnsToEvaluateCorr since I want correlations to be computed only among hours, productivity and wealth.

A small comment: As output I was expecting the correlation matrix b/w the desired variables, but this way is also OK.

1 Like

Good point. I’ve added CovarianceMatrix and CorrelationMatrix to the outputs. This duplicates the pairwise-by-name outputs, but some users will prefer one or the other and is essentially costless to just produce both.

I think the pairwise-by-name will be easier to use, partly because you can then later go and add more FnsToEvaluate without breaking your codes (whereas if you are using CorrelationMatrix(2,3) this might break if you add/remove FnsToEvaluate). Partly just because it saves you having to remember the ordering of FnsToEvaluate. But equally some people will want the covariance matrix so they can compare it to data, so saves them having to construct it manually from the pairwise results.

1 Like

I’ve renamed it to EvalFnOnAgentDist_CrossSectionCovarCorr_InfHorz()
so that it is more obvious that it calculates covariance matrix and correlation matrix. Apologies for the minor inconvenience :folded_hands:

[edited all the posts above to use this new name]

1 Like