使用VBA返回估计参数

时间:2012-11-20 20:10:27

标签: function vba

我有以下函数用于输入2个初始参数rho和V,并且基于最小平方误差总和,我希望它返回估计参数rho和V.

Public Function RhoAndV(params, MktStrike, MktVol, Vol, Fwd, Tau, Beta)
    Dim rho, V, Alpha As Double
    Dim i As Integer, L As Integer
    Dim sqdError() As Double, ModelVol() As Double
    rho = params(1)
    V = params(2)
    Alpha = AFn(Fwd, Fwd, Tau, Vol, Beta, rho, V)
    'MsgBox ("Alpha=" & Alpha)
    L = MktVol.Cells.Count
    ReDim ModelVol(L) As Double, sqdError(L) As Double
    For i = 1 To L
        ModelVol(i) = Vfn(Alpha, Beta, rho, V, Fwd, MktStrike(i), Tau)
        'MsgBox ("ModelVol(i)=" & ModelVol(i))
        sqdError(i) = (ModelVol(i) - MktVol(i)) ^ 2
    Next i
    RhoAndV = Application.SUM(sqdError)
    End Function

我在单元格A1:A2中有rho和V,其值为0.1和0.1,因此我选择A1:A2作为params。 在单元格C3:C6中,我有MktStrike,其值为

30
31
32
33

在单元格D3:D6中,我有MktVol值

0.23
0.24
0.25
0.26

此函数现在返回平方误差总和(我在Excel中将其作为=RhoAndV(A1:A3,C3:C6,D3:D6,0.25,10,1,0.5)运行,并返回.003。但是,我希望它返回估计的参数rho和{ {1}}。任何人都可以告诉我如何修复此功能吗?

1 个答案:

答案 0 :(得分:1)

您可以将原始功能修改为以下示例。如何返回值取决于是否要将它们返回到行(例如D3:E3)或列(例如D3:D4)

Function RhoAndV(params, MktStrike, MktVol, Vol, Fwd, Tau, Beta)

    'rest of your code

    'RhoAndV = Array(rho, V) 'returning to a row
    RhoAndV = Application.Transpose(Array(rho, V)) 'returning to a column
End Function