473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using arrays to calculate medians

Hi, I'm fairly new to VBA and was looking to start using arrays to calculate medians of monte carlo simulations. I know how to set up an array as I have been reading through such websites as:

http://excelmontecarlo.com/24a_writing_arrays_to_worksheet.html

The problem is when I'm looking to input into an array the values of a share price it doesn't seem to work, for example my code is:

'----Setting Array Size----
ReDim Arr(0 To nSimulations)

'----Set GBM----
Shock = (Volatility * Sqr(TimePeriod))

'----Main Loop----
For i = 1 To nSimulations

'----Loop for the life of the option by days----
For J = 1 To mSimulations

'----Normal Random Number Generator----
U1 = genrand_real3()
Norm1 = Moro_NormSInv(U1)

'----Drift----
Drift = (RiskFreeRate - DividendYield - (0.5 * (Volatility * Volatility))) * TimePeriod
Shock = (Volatility * Sqr(TimePeriod))

'---Calculate NewPrice Using GBM----
NewPrice = NewPrice * Exp(Drift + (Shock * Norm1))

Next J

Arr(i) = NewPrice

Next i

The problem is that I don't know how to place the array so it remembers each output for the NewPrice, then how do I order the array numerically so I can calculate the median and thirdly how to output the median to an excel cell.

Thank you in advance for any help

James
Mar 17 '11 #1
1 2073
It looks like you need to look at your nested for loops, the newprice is changing in your j loop but isn't getting input into your array. Also if you want your information in your array to be kept declare it in the declaration section i.e. before all your functions, and subs. As for ordering your array numerically see below,

Expand|Select|Wrap|Line Numbers
  1. Dim arr1(), arr2() As Integer 'Declaration
  2. Public Sub calculate()
  3. Dim i, r, j, k As Integer
  4.  
  5. [A1].Select   'For a test i put some random #'s in col A
  6. ReDim arr1([A1].End(xlDown).Row)
  7.  
  8. For i = 0 To UBound(arr1) - 1 'Input data into array
  9.     arr1(i) = ActiveCell.Offset(i, 0)
  10. Next
  11.  
  12. ReDim arr2(UBound(arr1))  ' sort array numerically
  13. For i = 0 To UBound(arr1) - 1
  14.     arr2(i) = arr1(i)
  15.     j = arr2(i)
  16.     For r = k To UBound(arr1) - 1
  17.         If arr1(r) < arr2(i) Then
  18.             arr2(i) = arr1(r)
  19.         End If
  20.     Next
  21.     For r = 0 To UBound(arr1) - 1
  22.         If arr2(i) = arr1(r) Then arr1(r) = j
  23.     Next
  24.     k = k + 1
  25. Next
  26.  
  27. [B1].Select  'export data into worksheet
  28. For i = 0 To UBound(arr1) - 1
  29.     ActiveCell.Offset(i, 0) = arr2(i)
  30. Next
  31. End Sub
  32.  
you'll have to modify it to suit.
Hope this helps.

RRR
Mar 18 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Chamomile | last post by:
I have been happily using array members as id's in my html code (either hand coded or generated by server-side script-php ) for some time. eg < input type='text' id='arrayItem' >< input...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
2
by: arnuld | last post by:
i do not have any problem here. i solved the problem but i wanted to know the views of you. please look at it from a newbie's perspective: problem: define a table with names of months of the...
0
by: antique | last post by:
iam supposed to CREATE A FUNCTION INT ADJUSTEDSUM (INT,INT) 1. find the sum of the array of size 100 2. find the number of occurrence of a MIN and MAX number if its twice or more only use 1...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
4
by: canteyn | last post by:
Ok here is my problem. I am coding a program that uses 3 different functions, and the end result is that the program reads in (x,y) points from a file, and then outputs those points to another file,...
7
sammyboy78
by: sammyboy78 | last post by:
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like: public void methodName(...
3
Blackout
by: Blackout | last post by:
Hi, I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32 (2 to the power of 0, 2 to the power of 1, 2 to...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
2
by: theblissfulwizard | last post by:
FYI - New to ASP I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do. I have a page that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.