+ Reply to Thread
Results 1 to 2 of 2

Covariance, VBA, Loops

  1. #1
    Registered User
    Join Date
    02-22-2011
    Location
    Copenhagen, Denmark
    MS-Off Ver
    MS Excel 2007
    Posts
    1

    Covariance, VBA, Loops

    Hi there,

    I am close to being as rookie as it comes wrt VBA programming although I more or less know how to read VBA codes.

    I want to write a code for the covariance between to returns (X and Y). I am aware that such a function already exists but have nonetheless determined to program such a one as i want to learn VBA.

    What i specifically want the function to include:
    - Dynamic arrays
    - All-in-one formula that only needs as input the returns for the assets.

    Below i have pasted what I have written so far but I am stuck with this and do not know what to write next.

    Could someone please advice on this? Thanks a million!

    PapaBear

    Option Explicit

    Function CoV(ReturnsX(), ReturnsY())
    Dim ExpRx, ExpRy As Double
    Dim ReturnsX(), ReturnsY() As Double
    Dim i As Integer

    ReDim ReturnsX(n)
    ReDim ReturnsY(n)

    ExpRx = Sum(ReturnsX(n) / Count(ReturnsX(n)))
    ExpRy = Sum(ReturnsX(n) / Count(ReturnsX(n)))

    DevX = 0: DevY = 0: DevXY = 0
    For i = 0 To n
    DevX = ReturnsX(i) - ExpRx: DevY = ReturnsY(i) - ExpRy
    DevXY = DevXY + (DevX * DevY)
    Next i

    CoV = DevXY

    End Function

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Covariance, VBA, Loops

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1