所以我想知道如何计算相关度在20%(LB)和70%(UB)之间的股票之间的平均相关性。
我应该使用此VBA函数AvgRhoBounded(Ret, LB, UB)
LB代表下限,UB代表上限。 Ret是我已经拥有的返回数据。到目前为止,这是我所做的,并且不确定是否正确。
代码:
Function AvgRhoBounded(RET, LB, UB)
' get number of assets
n = data.Columns.Count
' add up all returns
total_rho = 0
n_rho = 0
For i = 1 To n
For j = i + 1 To n
rho_ij = Application.WorksheetFunction.Correl(data.Columns(i), data.Columns(j))
total_rho = total_rho + rho_ij
n_rho = n_rho + 1
Next j
Next i
' return average correlations
AvgRho = total_rho / n_rho
'calculating the average correlation between stocks whose correlation are between 20% and 70%
Dim LB As String
Dim Lowerbound As String
Dim RET As String
Dim UB As String
Dim Upperbound As String
If LB = 20 Then
Lowerbound = 20
ElseIf UB = 70 Then
Upperbound = 70
End If
End Function