【问题标题】:I have a R formula for making a transition probability matrix that I want to implement in excel我有一个 R 公式,用于制作我想在 excel 中实现的转换概率矩阵
【发布时间】:2019-04-10 13:43:18
【问题描述】:

我有这段代码可以从一个从 1 到 12 的重复序列中生成转移概率矩阵,描述销售。

如何使用公式在 Excel 中实现此代码

我没有使用 VBA 的经验,我在 excel 中尝试过但失败了

x <- e$Range
p <- matrix(nrow = 12, ncol = 12, 0)
for (t in 1:(length(x) - 1)) p[x[t], x[t + 1]] <- p[x[t], x[t + 1]] + 1
for (i in 1:12) p[i, ] <- p[i, ] / sum(p[i, ])
p

这 12 个值的每个单元格中每个值的转移概率。

提前谢谢你, 问候

【问题讨论】:

    标签: r excel vba excel-formula markov-chains


    【解决方案1】:

    假设您的 Excel 工作表中有以下数据,从位置 A1 开始,对于 X:

    如果您想将两个表放在不同的位置,请使用新位置修改宏中的以下变量:startRowNumX, startColNumX, startRowNumP, startColNumP

    请注意,matix 用 0 和 1 填充,因为您没有在帖子中放置示例数据

    这里有一种方法来实现你想要用宏做的事情:

    Sub TransitonProbabilityMatrix()
        Dim startRowNumX, startColNumX, startRowNumP, startColNumP, sizeMatrix As Integer
        startRowNumX = 1
        startColNumX = 1
        startRowNumP = 3
        startColNumP = 1
    
        sizeMatrix = startColNumX
        While (Not IsEmpty(Cells(startRowNumX, sizeMatrix)))
            sizeMatrix = sizeMatrix + 1
        Wend
        sizeMatrix = sizeMatrix - startColNumX
    
        For i = startColNumX + 1 To startColNumX + sizeMatrix - 2
            Cells(Cells(startRowNumX, i) + startRowNumP, Cells(startRowNumX, i + 1) + startColNumP) = Cells(Cells(startRowNumX, i) + startRowNumP, Cells(startRowNumX, i + 1) + startColNumP) + 1
        Next
    
        For i = startRowNumP + 1 To startRowNumP + sizeMatrix - 1
            For j = startColNumP + 1 To startColNumP + sizeMatrix - 1
                Cells(i, j) = Cells(i, j) / WorksheetFunction.Sum(ActiveSheet.Range(Cells(i, startColNumP + 1), Cells(i, startColNumP + sizeMatrix)))
            Next
        Next
    End Sub
    

    这个Sub首先搜索矩阵的大小,然后应用转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2014-01-15
      • 1970-01-01
      • 2020-03-28
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多