Back to Fan's Matlab Examples Table of Content
Cell Array Store U(C) to Avoid Duplicate Computation over Iteration: u(c) across iterations often shares many common values
During Iteration Solution Procedure, sometimes only a subset of rows/columns need to be updated for some core matrix after each iteration.
Potentially, there could be significant speed gains if one does not need to fully recompute based on some N by M matrix, but can compute based on some N-n by M-m matrix, and update values in the N by M matrix with new values. See this file for examples when we fully reuse matrixes.
As stated here, we will store existing calculations in cell arrays.
Cell array based index updating is time saving because no additional array copying during slicing is needed.
One should not store data in larger tensors or matrixes and slice subsets when needed, that will not lead to speed improvements as shown here.
Below I invoke the best subset replace function out of the ones tested with some different parameters and look at resulting speeds. The code below shows the core codes contained in the data subset updating file here.
% %% Full Replace Standard Function
% function ffs_full_replace(ar_coh, ar_kp, ar_bp, f_u, f_c, it_iter, it_z)
% for it_iter_n=1:1:it_iter
% for it_z_m=1:1:it_z
% mt_u = f_u(f_c(ar_coh, ar_kp, ar_bp));
% end
% end
% %% Partial Replace with Cell Indexing
% function ffs_cellpart_replace(ar_it_rows_replace, ar_coh, ar_kp, ar_bp, f_u, f_c, it_iter, it_z)
% % This is the most efficient method
% cl_u_store = cell([it_z, 1]);
% tic;
% for it_iter_n=1:1:it_iter
% for it_z_m=1:1:it_z
% if (it_iter_n == 1)
% mt_c = f_c(ar_coh, ar_kp, ar_bp);
% mt_u = f_u(mt_c);
% cl_u_store{it_z_m} = mt_u;
% else
% cl_u_store{it_z_m}(ar_it_rows_replace,:) = f_u(f_c(ar_coh, ar_kp(ar_it_rows_replace), ar_bp(ar_it_rows_replace)));
% mt_u = cl_u_store{it_z_m};
% end
% end
% end
% end
Shift Matrix Size
% Limited States
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 50;
param_map('it_rown_update') = 10;
param_map('it_coln') = 500;
ff_u_c_partrepeat(param_map)
% More states/choices
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_rown_update') = 60;
param_map('it_coln') = 3000;
ff_u_c_partrepeat(param_map)
Shift the number of values that requiring updating. During iteration solution, sometimes the proportion of u(c) values, for fixed grid one choice problem, is 0 percent.
For two dimensional choice problem converted to one dimensional problem, the proportion of u(c) that requires changing decreases with each iteration quickly.
% Update 99 percent.
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_coln') = 3000;
param_map('it_rown_update') = param_map('it_rown')-1;
ff_u_c_partrepeat(param_map)
% Update half
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_coln') = 3000;
param_map('it_rown_update') = param_map('it_rown')/2;
ff_u_c_partrepeat(param_map)
% Update 1/4
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_coln') = 3000;
param_map('it_rown_update') = round(param_map('it_rown')/4);
ff_u_c_partrepeat(param_map)
% Update 1/8
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_coln') = 3000;
param_map('it_rown_update') = round(param_map('it_rown')/8);
ff_u_c_partrepeat(param_map)
% Update 1
param_map = containers.Map('KeyType','char', 'ValueType','any');
param_map('it_rown') = 300;
param_map('it_coln') = 3000;
param_map('it_rown_update') = 1;
ff_u_c_partrepeat(param_map)