Hi Jerome,
As far as I see you are using matrices and some arrays of matrices. Is the number of rows of each matrix known in advance? n?
In that case, before the loop, write
mZ = new matrix[n][3 * numsim];
mZe = new array[numsim];
v1 = new matrix[n][numsim];
mXe = new array[numsim];
ye = new array[numsim];
yhe = new array[numsim];
so that all arrays and matrices are created with the correct sizes.
Further, in your code I notice a variable eps2 on the right hand side, which is never assigned to. Also U.
If you intend to make the for loop parallel then put the increment of f and u inside the for header and not in the block. Or compute them from t.
Regards,
Jaap
-----------------------------------------------------------------
dr. Jacob J. de Vries
Department of Spatial Economics
Vrije Universiteit
De Boelelaan 1105
NL 1081 HV Amsterdam
Room 9A-53 (parttime)
Phone +31 20 59 82248
[log in to unmask]
http://personal.vu.nl/j.j.de.vries/
-----------------------------------------------------------------
From: The ox-users list is aimed at all Ox users [mailto:[log in to unmask]] On Behalf Of Jerome Simons
Sent: Thursday, November 26, 2015 7:57 PM
To: [log in to unmask]
Subject: writing to and pre-allocating large arrays
Dear All,
I wish to pre-allocate my DGP in a large array so that I can parallelise the loop that will do the actual maximization of my objective function. However, I am having trouble preallocating and subsequently writing to those arrays. I could not find help in the Ox documentation.
I have a loop of the form:
for (t=0; t<numsim; ++t)
{
mZ[][f:f+2] = rann(samplesize,2)~ones(samplesize,1);
mZe[t] = mZ[][f:f+2];
v1[][t] = mZ[][f].*rann(samplesize,1); //here we are creating heteroskedasticity by element-wise multiplication with the first row of the instruments.
f = f + 3;
eps = rho*U[][u] + kappa*U[][u+1] + sqrt((1-rho^2)/(phi^2+(0.86)^4))*(phi*v1[][t]+v2[][t]); //this is the structural disturbance.
mXe[t] = mZe[t]*mPi + U[][u:u+1];
u = u + 2;
ye[t] = mXe[t]*delta + eps;
yhe[t] = mXe[t]*delta + eps2[][t];
}
where variable that have an 'e' behind them should be arrays that are filled with n by 2, n by 3, and n by 1 matrices respectively. The idea is that then a second parallel loop can simply access mX, mZ, and y as elements of an array with the same index 'i' even though the elements of that array may themselves have differing dimensions.
So I have two questions: how to pre-allocate appropriately-sized arrays? And, how do I write to them? The current syntax produces error messages.
Thank you so much!
Jerome
|