I'm trying to create a bootstrap using arrays.
I have a dataset of two stocks with data in columns B and C:
16/08/2016 79.84 70.87 15/08/2016 80.26 71.79 12/08/2016 80.22 71.7 11/08/2016 80.56 71.98 10/08/2016 80.55 71.21 09/08/2016 81.5 73.05 08/08/2016 81.6 72.25 05/11/1990 17.5625 6.4011 02/11/1990 17.0938 6.4358 01/11/1990 17 6.5137 31/10/1990 16.8438 6.583 30/10/1990 17.3438 6.4444 29/10/1990 17.7813 6.3232
I need to pick randomly 10,000 times from column C, take an average and display in cell D1. Next, generate random 10,000 times and display in D2, etc. Similar for E1, E2, ..., En, but based on the data in column C.
Because data sets are large with two or more stocks and the methodology requires many runs, simple printing of the average every time to each cell, takes a long time. So I need to use arrays. I can do it "manually", but I am sure there is a better way.
The code takes a long time to run because it prints to the spreadsheet every time. I am storing simulated values in the simval()
array, but it is deleted upon Redim.
Ideally, I want to lift data into an array, simulate a new distribution and print results next to the original data.
Sub bstrap()
Dim miRange As Range
Dim avgsimvals() As Double, simval() As Double
Dim start As Double, secs As Double
start = Timer
r = Range("A1").CurrentRegion.Rows.Count
c = 4
Set miRange = Range(Cells(1, 2), Cells(r, 2))
For j = 1 To 100 '100 runs takes over 90 secs, approx 2.5 hrs for 10,000
ReDim simval(1 To r)
For i = 1 To r
simval(i) = WorksheetFunction.Index(miRange, r * Rnd() + 1)
Cells(i, c).Value = simval(i)
Next i
c = c + 1
Next j
secs = Round(Timer - start, 6)
MsgBox "run in " & secs, vbInformation
End Sub