1

I am trying to implement a multiple regression in MathNet using that model function for a start (may add another predictor variable later) :

Y = a + bX1 + cX2 + dX1X2+ eX1^2 + fX2^2

    Dim impvol(IV(0).Count) As Double
    Dim predictors1 As Double()() = New Double(2)() {} ' MN,DTE
    Dim predictors2 As Double()() = New Double(2)() {} ' STRIKE,DTE 

    Try
        For i = 1 To MN(0).Count
            predictors1(i) = New Double() {CType(MN(0).Item(i), Double), CType(IV(0).Item(i), Double)}
        Next
        For i = 1 To Strike(0).Count
            predictors2(i) = New Double() {CType(Strike(0).Item(i), Double), CType(IV(0).Item(i), Double)}
        Next
        For i = 1 To UBound(impvol)
            impvol(i) = CType(IV(0).Item(i), Double)
        Next
    Catch exc As Exception
        MessageBox.Show("Conversion Error (Vol Surface)")
    End Try

    ' Model Surface is Y = a + bX1 + cX2 + dX1X2 + eX1^2 + fX2^2
    ' The following inline model functions are needed here to regress the 3D surface

    'Dim p0 As Func(Of Double, Double) = Function(x) 1
    'Dim p1 As Func(Of Double, Double) = Function(x) x
    'Dim p2 As Func(Of Double(), Double) = Function(x) x(1) * x(2)
    'Dim p3 As Func(Of Double, Double) = Function(x) x ^ 2
    Dim p0 = Function(x As Double) 1
    Dim p1 = Function(x As Double) x
    Dim p2 = Function(x As Double, y As Double) x * y
    Dim p3 = Function(x As Double) x ^ 2
    Dim regparams1() As Double = MathNet.Numerics.Fit.LinearMultiDim(predictors1, impvol,
                                 p0, p1(predictors1(0)), p1(predictors1(1)), p2(predictors1(0), predictors1(1)), p3(predictors1(0)), p3(predictors1(1)))

    Dim regparams2() As Double = MathNet.Numerics.Fit.LinearMultiDim(predictors2, impvol, False)

The second regression compiles OK (with False) but not the first one. This is more of a VB.NET question: How do I implement the function as it is described on the Regression page in C# ?

Thanks in advance

Talha Israr
  • 654
  • 6
  • 17
bruno
  • 68
  • 8
  • Where is this regression page that you speak of? – IAmNerd2000 Jan 23 '19 at 17:42
  • https://numerics.mathdotnet.com/Regression.html – bruno Jan 23 '19 at 17:48
  • 1
    You may need to make sure that you are using the array bounds correctly. For example on the first two loops you iterate from 1 to .count and on the last one you iterate from 1 to ubound(). they are not consistent. Array indecies in VB.net start at 0 and end at (.count-1) or ubound(). – IAmNerd2000 Jan 23 '19 at 18:43
  • yes i realised that after posting. thank you. would you know from the C# example on the Regression page how to do same in VB, i.e. the model functions ? – bruno Jan 23 '19 at 19:35

1 Answers1

0

You can try this (this is my guess as to what it requires):

    Dim f As Func(Of Double, Double) 

    f = Fit.LinearCombinationFunc(
                {61.0, 62.0, 63.0, 65.0}, 
                {3.6, 3.8, 4.8, 4.1}, 
                Function(x) 1.0, 
                Function(x) Math.Log(x)
        )

    Debug.Print(f(66.0))

    'And For Linearizing non-linear models by transformation...
    Dim xy = {{1.0, 4.0},
              {2.0, 5.0},
              {3.0, 2.0}}
    Dim z = {15.0, 20, 10}

    Dim z_hat = z.[Select](Function(r) Math.Log(r)).ToArray()

    Dim p_hat As Double() = Fit.LinearMultiDim(xy, z_hat, Function(d) 1.0,
                            Function(d) Math.Log(d(0)), Function(d) Math.Log(d(1)))

    Dim u As Double = Math.Exp(p_hat(0))

    Dim v As Double = p_hat(1)

    Dim w As Double = p_hat(2)
IAmNerd2000
  • 761
  • 1
  • 4
  • 12
  • Thanks. Will give it a try. – bruno Jan 23 '19 at 19:48
  • Sorry to bother you again... Your example looks correct for simple linear regression (1 predictor, 1 output). How would you now adapt your code for multiple regression e.g. 2 predictors X1 X2, 1 output Y, where one function would be Y=f(X1,X2) ? – bruno Jan 24 '19 at 10:06
  • I edited the code. I believe it should work. You have just never responded or upvoted or accepted this answer. – IAmNerd2000 Feb 02 '19 at 08:21
  • Sorry, my score is still too low to show my up vote publicly. – bruno Feb 08 '19 at 14:24
  • That's ok, you can accept it as your answer though. – IAmNerd2000 Feb 08 '19 at 15:08
  • I've never asked a question, but I would think it would be next to my answer on the top left next to the up/down arrows, or under the answer there are words that say: share, edit, etc. it may be around there. My first instinct would be that it is around the up/down arrows though. Thanks. – IAmNerd2000 Feb 08 '19 at 15:40
  • Got it! Thanks! – bruno Feb 08 '19 at 15:44