0
votes

I have a array (option strikes):

 550  600  620  640  650  660  680  700  710  720  730  740  750  760  770  780  790  800  810  820  830  840  850  860  870 880  890  900  910  920  930  940  950  960  970  980  990 1000 1010 1020 1030 1040 1050 1060 1070 1080 1090 1100 1110 1120 1130 1140 1150 1160 1170 1180 1190 1200 1210 1220 1230 1240 1250 1260 1270 1280 1290 1300 1310 1320 1330 1340 1350 1360 1370 1380 1390 1400 1410 1420 1430 1440 1450 1460 1470 1480 1490 1500 1520

It has uneven increments sometimes in the beginning or the end. For example, the first number is 550 and it goes to 600 right away from there, a increase of 50 points.

I know that at around a central number (at the money) there exists a subset of the above vector which increments by 10 points. I want to somehow extract it. The central number is 1090 for this vector. It can change continuously for different vectors like the one above - but should be around the center.

So for the above array, I want to extract:

 700  710  720  730  740  750  760  770  780  790  800  810  820  830  840  850  860  870 880  890  900  910  920  930  940  950  960  970  980  990 1000 1010 1020 1030 1040 1050 1060 1070 1080 1090 1100 1110 1120 1130 1140 1150 1160 1170 1180 1190 1200 1210 1220 1230 1240 1250 1260 1270 1280 1290 1300 1310 1320 1330 1340 1350 1360 1370 1380 1390 1400 1410 1420 1430 1440 1450 1460 1470 1480 1490 1500

In this vector all numbers increment at a constant rate of 10 points. So essentially I want to be able to subset the first vector for a subset that increments constantly at 10 points. How can I do that?

One way would be too loop. Take the difference between and subset for 10 point differences. But this way is not very elegant as I must set a number to stop looping at. Love to hear if there are better ways.

Thanks

edit:

for the following sequence, julius' answer doesnt seem to work..

700  710  720  730  740  750  760  770  780  790  800  810  820  830  840  850  860  870  880  890  900  910  920  930  940  950  960  970  980  990 1000
1010 1020 1030 1040 1050 1060 1070 1080 1090 1100 1110 1120 1130 1140 1150 1160 1170 1180 1190 1200 1210 1220 1230 1240 1250 1260 1270 1280 1290 1300 1310
1320 1330 1340 1350 1360 1370 1380 1390 1400 1450 1500

I get when I use Julius' answer: 1410 1420 1430 1440 1450 1460 1470 1480 1490 1500 1510 1520 1530 1540 1550 1560 1570 1580 1590 1600 1610 1620 1630 1640 1650 1660 1670 1680 1690 1700 1710 1720 1730 1740 1750 1760 1770 1780 1790 1800 1810 1820 1830 1840 1850 1860 1870 1880 1890 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000 2010 2020 2030 2040 2050 2060 2070 2080 2090 2100 2110

2
"Around the center" is not very precise. Can one instead assume that you are looking for the longest subsequence with a constant increment? - Julius Vainora
Something like rle(diff(your_vector)) should be helpful. - Gregor Thomas
And, just to be clear, you mean vector right, not an object of class array? - Gregor Thomas

2 Answers

1
votes

The following lines give the longest subsequence with a constant increment 10:

v <- rle(x - 1:length(x) * 10)
v$v[which.max(v$l)] + (sum(v$l[seq_len(which.max(v$l) - 1)]) + 1:max(v$l)) * 10
# [1]  700  710  720  730 ... 1500
1
votes

Use this block of code:

#Subtracting each value by the successor in the vector
b <- a[2:length(a)]-a[1:(length(a)-1)]
#Which difference value has the longest run length
maxpos <- which(rle(b)$length==max(rle(b)$length))
# Extracting the starting and ending indices
start <- sum(rle(b)$length[1:maxpos-1]) + 1
end <- start + rle(b)$length[maxpos]

#Extracted values
a[start:end]

The output:

[1]  700  710  720  730  740  750  760  770  780 ...