+ Reply to Thread
Results 1 to 10 of 10

Matrix and Vector algebra in VBA

  1. #1
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Matrix and Vector algebra in VBA

    Hi all

    I have created a macro to make a Mean/variance frontier for stocks. It works but it is not dynamic in the sense that i can add more stocks to see how the frontier will change.

    Its uses an extensive amount of matrix algebra, which i found was only possible using the worksheetfunctions MMULT, TRANSPOSE and MINVERSE, using ranges instead of arrays.

    Is it not possible to do these computations on arrays instead ? This would make the code much nicer and i would not have to grab all my data from the spreadsheet.

    If it is not possible to do with arrays I just want it made dynamic, so it can take any size of range to make the graph. Its just pretty ugly code thats all.

    Would also be appreciated if someone could redirect me to any good "Linear algebra for VBA" threads.. Ive tried google without much luck...

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Matrix and Vector algebra in VBA

    Is it not possible to do these computations on arrays instead ?
    The code is doing the calculations on arrays.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Matrix and Vector algebra in VBA

    Instead of this ...

    Please Login or Register  to view this content.
    You could do this:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Matrix and Vector algebra in VBA

    To perform linear algebra in VBA you must operate on each element of the array individually. As VBA is an interpreted language (vs. compiled) it performs relatively poorly - particularly when working with large arrays. So, to answer your question: yes, it can be done, but depending upon the number of securities in your portfolio you may notice it takes some calculation time.

    A word of warning: MINVERSE will not work on a matrix of N > 52, so if you need to invert a larger matrix you'll have to write your our VBA routines anyway.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Matrix and Vector algebra in VBA

    MINVERSE will not work on a matrix of N > 52
    That limitation was removed in Excel 2007, PP.

  6. #6
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Matrix and Vector algebra in VBA

    Thx for the tip.
    I guess someone at MS needs to update their documentation: http://support.microsoft.com/kb/166342
    Last edited by PingPing; 04-16-2012 at 06:34 AM.

  7. #7
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Matrix and Vector algebra in VBA

    mrosendal, you may be interested in these:

    MV-Efficient-Frontier.xls

    MeanVariance.doc

  8. #8
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Re: Matrix and Vector algebra in VBA

    Thanks guys your help has been much appreciated. I have been doing some playing around on my own and it seems I can use my own dynamic arrays (not the ones taken from the worksheet) if I just write "application", Instead of Application.Worksheetfunction. Using this I should be able to make sth more Dynamic than what I have now...

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Re: Matrix and Vector algebra in VBA

    Pretty smooth thanks i knew there had to be a better way than mine.....

  10. #10
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Re: Matrix and Vector algebra in VBA

    Quote Originally Posted by shg View Post
    Instead of this ...

    Please Login or Register  to view this content.
    You could do this:

    Please Login or Register  to view this content.
    Pretty smooth thanks i knew there had to be a better way than mine.....

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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