Results 1 to 10 of 10

Thread: Linear interpolation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    I think you were wiping out your data

    I added the Exit For to get out of your j loop once the gap is calculated and now everything looks reasonable

    (although my math is a little rusty)

    Option Explicit
    
    Function Interpolate(oldRates As Variant) As Variant
    Dim i As Long, j As Long, Maturity As Integer
    ' (8,1) in old rates data array highlighted in yellow, so maturity became 10
    Maturity = oldRates(oldRates.Rows.Count, 1)
    Dim Mat() As Double
    ReDim Mat(1 To Maturity, 1 To 2)   ' 1 to 10, 1 to 2:  the new array set up.
    'Fills in the years
    For i = 1 To Maturity
        Mat(i, 1) = i
        Mat(i, 2) = 0#
    Next i
    'copies over known Rates.
    For i = 1 To oldRates.Rows.Count    ' 1 to 8
       j = oldRates(i, 1).Value
      Mat(j, 2) = oldRates(i, 2)
    Next i
    For i = 2 To Maturity
        'find if missing
        If Mat(i, 2) = 0# Then
           For j = i + 1 To Maturity     'find the next Filled Rate to interpolate with
              If Mat(j, 2) <> 0 Then       'if statement
                 Mat(i, 2) = Mat(i - 1, 2) + (Mat(j, 2) - Mat(i - 1, 2)) / (Mat(j, 1) - Mat(i - 1, 1))
                 Exit For    '   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
              End If
          Next j
       End If
    Next i
    Interpolate = Mat
    End Function
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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