excel - 如何在 VBA 中调试平方根溢出(MacOS v.16.36 上的错误)
问题描述
我是 VBA 的菜鸟,在 VBA 中使用 sqr() 时遇到问题。每当我在 VBA 中使用 Sqr() 函数时,在 SigSqrdt = sigma * Sqr(dt) 行上会出现一个消息框“运行时错误'6'溢出”。我试图只模拟布朗运动。
Sub Simulating_Geometric_Brownian_Motion()
Dim T, S, mu, sigma, dt, SigSqrdt, LogS, drift, i, N
T = InputBox("Enter the length of the time period (T)")
N = InputBox("Enter the number of periods (N)")
S = InputBox("Enter the initial stock price (S)")
mu = InputBox("Enter the expected rate of return (mu)")
sigma = InputBox("Enter the volatility (sigma)")
dt = T / N
SigSqrdt = sigma * Sqr(dt)
drift = (mu - 0.5 * sigma * sigma) * dt
LogS = Log(S)
ActiveCell.Value = "Time"
ActiveCell.Offset(0, 1) = "stock price"
ActiveCell.Offset(1, 0) = 0 ' beginning time
ActiveCell.Offset(1, 1) = S 'beginning stock price
For i = 1 To N
ActiveCell.Offset(i + 1, 0) = i * dt
LogS = LogS + SigSqrdt * RandN()
ActiveCell.Offset(i + 1, 1) = Exp(LogS)
Next i
End Sub
Function RandN()
RandN = Application.NormSInv(Rnd())
End Function
样本输入:
T=1000, N=1000, S=10, mu=10, sigma=1
解决方案
推荐阅读
- closures - 重构和闭包
- rust - 如何为 solana-test-validator 设置日志记录?
- python - 无法使用 UI(带头)运行 scrapy-selenium 库
- scala - chiseltest 测试工具中 `initSource`、`initSink` 或 `setSinkClock` 的用途是什么?
- python - Power bi 中的 Python 可视化
- javascript - Javascript 中的 Hangman 问题
- git - 使用个人访问令牌克隆私有 Github 存储库 Netbeans
- c++ - 在 CLion 中构建 cmake 时找到 GLEW,但在命令行中构建时找不到 GLEW
- python - 创建或 isinstance 的 Python 泛型检查其类型参数
- r - R:预测时间序列的未来值