Results 1 to 6 of 6

Thread: Tricky -Loops - Out of Memory Problem !

  1. #1

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Tricky -Loops - Out of Memory Problem !

    Hi all,

    I have been asked by a user to look at some VBA code that he has in a spreadsheet for him as he keeps getting out of memory error's

    Now i have looked at the code and as far as i can see it's use of Massive nested loops is part of the problem.

    Unfotunatley i don't fully understand what he is trying to do, i believe that it is a system to predict future prices by taking an initial price and by using a calculation & a random factor produce 3 new numbers on the next level so you build up a tree like structure of prices. This continues on down 100 times until a future price can be predicted, through an averaging of values.

    The code in the attached spreadsheet (zipped up) will give you a clearer idea of what he is trying to achieve !

    Now can anybody think of a more memory efficient way of doing this ?? as i am a little lost.

    any suggestions would be apprieciated !!
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Tricky -Loops - Out of Memory Problem !

    Out of memory errors in loops are usually caused by an infinite loop, one that never reaches an end point. How much memory is in the user's machine?
    If the loops are basically doing the same thing, instead of having tons of nested loops, maybe recursion would work better. Just make sure it ends at some point.
    Tengo mas preguntas que contestas

  3. #3
    Lively Member
    Join Date
    Sep 2004
    Posts
    74

    Re: Tricky -Loops - Out of Memory Problem !

    An piece of the code you posted :

    VB Code:
    1. For d1 = 1 To 3
    2.                 For d2 = 1 To 3
    3.                     For d3 = 1 To 3
    4.                         For d4 = 1 To 3
    5.                             For d5 = 1 To 3
    6.                                 For d6 = 1 To 3
    7.                                     For d7 = 1 To 3
    8.                                         For d8 = 1 To 3
    9.                                             For d9 = 1 To 3
    10.                                                                    
    11.                                                 Randomize
    12.                                                 S(9, d1, d2, d3, d4, d5, d6, d7, d8, d9, 0) = S(8, d1, d2, d3, d4, d5, d6, d7, d8, 0, 0) * (Exp((Risk_Free_Rate - Var / 2) * Delta_t + Standard_Deviation * Application.WorksheetFunction.NormSInv(Rnd) * Sqr(Delta_t)))
    13.                                                                    
    14.                                             Next d9
    15.                                         Next d8
    16.                                     Next d7
    17.                                 Next d6
    18.                             Next d5
    19.                         Next d4
    20.                     Next d3
    21.                 Next d2
    22.             Next d1

    so this loop wil do this piece of code :

    VB Code:
    1. Randomize
    2.                                                 S(9, d1, d2, d3, d4, d5, d6, d7, d8, d9, 0) = S(8, d1, d2, d3, d4, d5, d6, d7, d8, 0, 0) * (Exp((Risk_Free_Rate - Var / 2) * Delta_t + Standard_Deviation * Application.WorksheetFunction.NormSInv(Rnd) * Sqr(Delta_t)))

    ((((((((3) x 3) x 3) x 3) x 3) x 3) x3) x3) x3 = 19683 x

    and this is just one of the many nested loops, i guess the many loops just cause the memory probs, there doesn't have to be an infinite loop

  4. #4

    Thread Starter
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Tricky -Loops - Out of Memory Problem !

    Calibra - My thoughts exactly! - to many loops !

    The loops go from 1 (for/next) loop to 2 nested (for/next) loops all the way to 10 and then back to 1 again

    e.g. -

    for n = 0 to 3

    next

    for n = 0 to 3
    for i = 0 3

    next i
    next n

    then 3 loops nested, then 4 then 5 then 6 then 7 then 8 then 9 then 10

    then 9 then 8 then 7 then 6 then 5 then 4 then 3 then 2 then 1

    and all this in a sub which is called inside another (for/next) loop which goes from 0 - 100

    Thats a whole lot of looping !

    I suppose what i am asking is thier another way of doing this with out so many loops ????

    ps - It will probabley help alot to look at the spreadsheet to understand exactley what i mean !

    pps -salvelinus recursion ?? An Example would help me understand what you mean !

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Tricky -Loops - Out of Memory Problem !

    Recursion is when a function or sub calls itself. An example (I didn't download your code, this is just an example) -
    VB Code:
    1. Private Sub RecursiveX(ByRef x as Integer)
    2.    If x > 0 Then
    3.       If x = 10 Then
    4.          x = x * 3.1415
    5.          Exit Sub
    6.       Else
    7.          x = x + 1
    8.          RecursiveX(x)   'recursive call
    9.       End If
    10.    Else
    11.       x = 0
    12.    End If
    13. End Sub
    14.  
    15. Private Sub CallRecursiveX()
    16.    dim i as Integer
    17.  
    18.    i = 0
    19.    RecursiveX(i)   'i = 0 - was set to 0 in RecursiveX
    20.    i = 3    
    21.    RecursiveX(i)   'i = 30.1415 - was incremented to 10 via recursive calls, then multiplied
    22.    i = 10
    23.    RecursiveX(i)   'i = 30.1415 - no recursive call
    24.    i = 20
    25.    RecursiveX(i)   'i = ?? - this will be an infinite loop till memory runs out,
    26.                  '           because there's nothing to end the recursive calls  
    27. End Sub
    That's not an exciting example, and recursive calls can have memory problems too (they keep adding to the stack), but it's easier to read than tons of nested functions.
    If there are functions nested more than 5 deep, you've got too many. 3 or 4 are better.
    Last edited by salvelinus; Jan 14th, 2005 at 11:20 AM.
    Tengo mas preguntas que contestas

  6. #6
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Tricky -Loops - Out of Memory Problem !

    Quote Originally Posted by NeedSomeAnswers
    Hi all,

    Unfotunatley i don't fully understand what he is trying to do, i believe that it is a system to predict future prices by taking an initial price and by using a calculation & a random factor produce 3 new numbers on the next level so you build up a tree like structure of prices. This continues on down 100 times until a future price can be predicted, through an averaging of values.
    LOL! OK, this is one case where more is not better. The link below describes what he is trying to do, and if I were you, I'd draw this part to his attention:

    Quote Originally Posted by www.riskglossary.com
    While increasing the sample size is one technique for reducing the standard error of a Monte Carlo analysis, doing so can be computationally expensive. A better solution is to employ some technique of variance reduction. These techniques incorporate additional information about the analysis directly into the estimator. This allows them to make the Monte Carlo estimator more deterministic, and hence have a lower standard error.
    If you really want a crack at coding this, this link will get you started:

    http://www.riskglossary.com/articles...rlo_method.htm

    If you don't, I'd forward this link to your co-worker:

    http://www.add-ins.com/analyzer/index.htm

Posting Permissions

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



Click Here to Expand Forum to Full Width