I am having a dataset of prices recorded at every second for 10 days.I want to sample them at 10th,20th and 30th second using the count command and finding the corresponding rows.I wrote the code below to find the 10th sec,20th and 30th sec on each of the tenth day.But somehow I get the count data only for days 1 to 9 or 2 to 10 if I modify the expression in do while loops. Otherwise I get an error "G0058 :Index out of range".How do I rectify this error? Please help.
v={10,20,30};
nrows = 10;
ncols = 50;
newrows = zeros(nrows, ncols);
//Data- A column vector of seconds at which price is recorded
do while j <= ncols;
k=l;
do while Data[l] < j;
l=l+1;
endo;
dummy=price[k:l-1,1];
c=counts(temp,v);
newrows[1:10,j] = c;
j=j+1;
endo;
5 Answers
0
Would it be possible for you to post a few observations of Data
? That will make it simpler to help you.
0
The actual dataset has 3 columns. One with a day counter, the tick/seconds information and the corresponding index values.
Dayct, Tick(in seconds-named as prices in the code), S&P
1,3,2400
1,7,2400.1
1,9,2400.5
1,15,2400.34
1,22,2400.21
1,33,2400
.......
2,8,2399
2,17,2399.9
....
10,2300,2401
I then created 3 vectors containing each of the 3 coloumns. Then named, dayct(1st column) as "Data"and ticks(2nd column) as "price"(Should have named as ticks)
The first do while j <= ncols helps me to get in to each day and the second do while Data[l] < j is for counting the number of rows of data for each day to find the corresponding row index of the prices to be picked using the count command.
Thank you.
0
I think I am understanding what you want to do. If so, you do not need to do a loop, you can use element-by-element comparison operators. The soon to be released version, GAUSS 18, has a new function ismember
that would be helpful, but older versions can accomplish the task just fine.
I have made the following self-contained example based on the sample of data you provided with some extra made up examples. I would probably do almost all of this in one line, but to make it easier for you to look at the output of each intermediate steps, I have made the code much longer than it needs to be.
//First column is day (1:10)
//Second column is second
//Third column is index value
data = { 1 3 2400,
1 7 2400.1,
1 9 2400.5,
1 15 2400.34,
1 22 2400.21,
1 33 2400,
2 4 2401,
2 10 2400.6,
2 11 2400.75,
2 30 2400.67,
2 45 2400.59,
3 1 2400 };
//Binary vector with 1 where first column equals 2
mask_day = (data[.,1] .== 2);
//row vector with seconds we
//want to look for
sec = { 10 20 30 };
//Create Nx3 matrix with 1 where
//element of 'data' equals corresponding
//element of 'sec'
mask_sec = (data[.,2] .== sec);
//Sum down rows so we have non-zero
//when data[.,2] equals 10 or 20 or 30
mask_sec_2 = sumr(mask_sec);
//Final mask where both masks are non-zero
final_mask = mask_day .and mask_sec_2;
//Grab all rows that meet our criteria
data_select = selif(data, final_mask);
This will set data_select
to
2.00 10.00 2400.60 2.00 30.00 2400.67
Let us know if we misunderstood your question, or you have any questions about this answer.
0
Thank you very much for the code.
The problem is that, in most of the cases, we do not have the observation in the exact tenth second or the 20th and so on. So, in such cases, we take the one earlier, like in my dataset,(i.e) 9th sec(because 10th second data is not available),15th second(because 20th second data is not available) and so on. Counts command helps to find the number of observations between any 2 sampling interval,which can be translated into row index.
It would be great if I get to know why my code does not give me the results on the last day.
Thank you.
0
There are a few undefined symbols in the original code snippet as noted below. I think I can figure out some, but not all. If you post a code snippet that can reproduce the problem with either the data posted below the code snippet or another one you would prefer to use, we should be able to help you.
v={10,20,30};
nrows = 10;
ncols = 50;
newrows = zeros(nrows, ncols);
//Data- A column vector of seconds at which price is recorded
do while j <= ncols; //'j' is not initialized
//start as 1 before loop?
k=l;
do while Data[l] < j;
l=l+1; //'l' is not initialized
endo;
dummy=price[k:l-1,1]; //where does 'price' come from?
//'dummy' is not used
c=counts(temp,v); //where does 'temp' come from?
newrows[1:10,j] = c;
j=j+1;
endo;
//First column is day (1:10)
//Second column is second
//Third column is index value
data = { 1 3 2400,
1 7 2400.1,
1 9 2400.5,
1 15 2400.34,
1 22 2400.21,
1 33 2400,
2 4 2401,
2 10 2400.6,
2 11 2400.75,
2 30 2400.67,
2 45 2400.59,
3 1 2400,
3 10 2401,
3 11 2400.5,
3 21 2400.75 };
Your Answer
5 Answers
Would it be possible for you to post a few observations of Data
? That will make it simpler to help you.
The actual dataset has 3 columns. One with a day counter, the tick/seconds information and the corresponding index values.
Dayct, Tick(in seconds-named as prices in the code), S&P
1,3,2400
1,7,2400.1
1,9,2400.5
1,15,2400.34
1,22,2400.21
1,33,2400
.......
2,8,2399
2,17,2399.9
....
10,2300,2401
I then created 3 vectors containing each of the 3 coloumns. Then named, dayct(1st column) as "Data"and ticks(2nd column) as "price"(Should have named as ticks)
The first do while j <= ncols helps me to get in to each day and the second do while Data[l] < j is for counting the number of rows of data for each day to find the corresponding row index of the prices to be picked using the count command.
Thank you.
I think I am understanding what you want to do. If so, you do not need to do a loop, you can use element-by-element comparison operators. The soon to be released version, GAUSS 18, has a new function ismember
that would be helpful, but older versions can accomplish the task just fine.
I have made the following self-contained example based on the sample of data you provided with some extra made up examples. I would probably do almost all of this in one line, but to make it easier for you to look at the output of each intermediate steps, I have made the code much longer than it needs to be.
//First column is day (1:10)
//Second column is second
//Third column is index value
data = { 1 3 2400,
1 7 2400.1,
1 9 2400.5,
1 15 2400.34,
1 22 2400.21,
1 33 2400,
2 4 2401,
2 10 2400.6,
2 11 2400.75,
2 30 2400.67,
2 45 2400.59,
3 1 2400 };
//Binary vector with 1 where first column equals 2
mask_day = (data[.,1] .== 2);
//row vector with seconds we
//want to look for
sec = { 10 20 30 };
//Create Nx3 matrix with 1 where
//element of 'data' equals corresponding
//element of 'sec'
mask_sec = (data[.,2] .== sec);
//Sum down rows so we have non-zero
//when data[.,2] equals 10 or 20 or 30
mask_sec_2 = sumr(mask_sec);
//Final mask where both masks are non-zero
final_mask = mask_day .and mask_sec_2;
//Grab all rows that meet our criteria
data_select = selif(data, final_mask);
This will set data_select
to
2.00 10.00 2400.60 2.00 30.00 2400.67
Let us know if we misunderstood your question, or you have any questions about this answer.
Thank you very much for the code.
The problem is that, in most of the cases, we do not have the observation in the exact tenth second or the 20th and so on. So, in such cases, we take the one earlier, like in my dataset,(i.e) 9th sec(because 10th second data is not available),15th second(because 20th second data is not available) and so on. Counts command helps to find the number of observations between any 2 sampling interval,which can be translated into row index.
It would be great if I get to know why my code does not give me the results on the last day.
Thank you.
There are a few undefined symbols in the original code snippet as noted below. I think I can figure out some, but not all. If you post a code snippet that can reproduce the problem with either the data posted below the code snippet or another one you would prefer to use, we should be able to help you.
v={10,20,30};
nrows = 10;
ncols = 50;
newrows = zeros(nrows, ncols);
//Data- A column vector of seconds at which price is recorded
do while j <= ncols; //'j' is not initialized
//start as 1 before loop?
k=l;
do while Data[l] < j;
l=l+1; //'l' is not initialized
endo;
dummy=price[k:l-1,1]; //where does 'price' come from?
//'dummy' is not used
c=counts(temp,v); //where does 'temp' come from?
newrows[1:10,j] = c;
j=j+1;
endo;
//First column is day (1:10)
//Second column is second
//Third column is index value
data = { 1 3 2400,
1 7 2400.1,
1 9 2400.5,
1 15 2400.34,
1 22 2400.21,
1 33 2400,
2 4 2401,
2 10 2400.6,
2 11 2400.75,
2 30 2400.67,
2 45 2400.59,
3 1 2400,
3 10 2401,
3 11 2400.5,
3 21 2400.75 };