VBA: Sumproductc return 空值(矩阵乘法)

VBA: Sumproductc return empty value (Matrix Multiplication)

我正在尝试在 VBA 中进行一些矩阵乘法运算,但我一直收到错误消息。我尝试定义所有向量和矩阵并一次停止计算,看起来 =MMult 函数工作正常,但使用 =SumProduct 时返回的值为 0。

这是我的子代码(我省略了一些格式):

Sub StockAnalysis()
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim Stocks As Integer
Dim TableStart As Integer
Dim TableEnd As Integer
Dim CorrTableStart
Dim CovTableStart As Integer
Dim InvCovTableStart As Integer

Dim AuxiliaryA As Integer
Dim AuxiliaryB As Integer
Dim AuxiliaryC As Integer
Dim AuxiliaryD As Integer

'Count the amount of stocks
Stocks = 0
For i = 1 To 20
    If Worksheets("MainSheet").Cells(i + 2, 2) <> 0 Then
        Stocks = Stocks + 1
    Else
        Stocks = Stocks
    End If
Next

'Deleting previous TempSheet
For Each Worksheet In Application.ActiveWorkbook.Worksheets
    If Worksheet.Name = "TempSheet" Then
        Worksheet.Delete
    End If
Next

'Defining start and end points of the tables
TableStart = Stocks + 3
TableEnd = TableStart + Stocks
CorrTableStart = 9
CovTableStart = CorrTableStart + Stocks + 2
InvCovTableStart = 13 + (Stocks * 2)
RiskFreeRateStart = InvCovTableStart + 2 + Stocks

'Transposing Means are creating One Vector 
ActiveWorkbook.Sheets.Add.Name = "TempSheet"
Worksheets("TempSheet").Visible = xlSheetHidden
Worksheets("TempSheet").Cells(1, 1) = "Stocks"
Worksheets("TempSheet").Cells(1, 2) = "Mean"
Worksheets("TempSheet").Cells(1, 3) = "Vector"
For j = 1 To Stocks
    Symbol = Worksheets("All stocks").Cells(1, 1 + j)
    Worksheets("TempSheet").Cells(j + 1, 1) = Symbol

    Mean = Worksheets("All stocks").Cells(2, TableStart + j)
    Worksheets("TempSheet").Cells(j + 1, 2) = Mean

    Worksheets("TempSheet").Cells(j + 1, 3) = 1
Next

'--------------------Calculations--------------------

MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(1 + Stocks, 2))
OneVector = Worksheets("TempSheet").Range(Cells(2, 3), Cells(1 + Stocks, 3))

ReDim MatrixInvCov(1 To Stocks, 1 To Stocks)
For i = 1 To Stocks
    For j = 1 To Stocks
        MatrixInvCov(i, j) = Worksheets("All stocks") _
            .Cells(InvCovTableStart + i, TableStart + j).Value
    Next
Next

AuxiliaryMeanMM = Application.WorksheetFunction.MMult(MatrixInvCov, MeanVector)

AuxiliaryOneMM = Application.WorksheetFunction.MMult(MatrixInvCov, OneVector)

AuxiliaryA = Application.WorksheetFunction.SumProduct(MeanVector, AuxiliaryMeanMM)
AuxiliaryB = Application.WorksheetFunction.SumProduct(MeanVector, AuxiliaryMeanMM)
AuxiliaryC = Application.WorksheetFunction.SumProduct(MeanVector, AuxiliaryOneMM)
AuxiliaryD = AuxiliaryA - AuxiliaryC - AuxiliaryB ^ 2



Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

在Excel中"A"会这样写:

=Sumproduct(MeanVector;MMULT(MatrixInvCov;MeanVector))

矩阵和向量的维度是正确的,因为 AuxiliaryOneMM、AuxiliaryMeanMM、MeanVector 和 OneVectore 是 [3x1],而 MatrixInvCov 是 [3x3] 矩阵

这是一张当地人的照片window,看起来向量和矩阵是正确的,但是A、B、C、D都是0。

TempSheet 和所有库存图片(如果需要):

Dim AuxiliaryA As Integer
Dim AuxiliaryB As Integer
Dim AuxiliaryC As Integer
Dim AuxiliaryD As Integer

您正在调暗结果 As Integer,无论结果是否小于 1,它们都将为 0。查看手表中的值 window,它们似乎是很小的值,所以这个是最有可能的原因。

Dim AuxiliaryA As Double, AuxiliaryB As Double, AuxiliaryC As Double, AuxiliaryD As Double

对于科学计算,我认为你应该调暗所有变量 As Double 除非你有一些合理的理由。

您还可以评估 Excel 公式(未测试):

MatrixInvCovAddr = Cells(InvCovTableStart + 1, TableStart + 1).Resize(stocks,stocks).Address

AuxiliaryMeanMM = Sheet("TempSheet").Evaluate("Sumproduct(B2:B" & 1 + Stocks & _ 
           ",MMULT('All stocks'!" & MatrixInvCovAddr & ", B2:B" & 1 + Stocks & "))")