在 Excel 中加速蒙特卡罗

如何解决在 Excel 中加速蒙特卡罗

我正在 Excel 中运行蒙特卡罗。为了表示每个月每个产品 SKU 的需求分布,我使用以下公式超过 500 次:

=IFERROR(BETA.INV(RAND(),(1+4*(D35-D31)/(D27-D31)),(1+4*(D27-D35)/(D27-D31)),D31,D27),0)

因此,一次超过 500 个 RAND() 易失性函数。

公式每次迭代的输出都存储在表中。我有一个 VBA 来处理:

Sub QARLO_Products()
'runs on QARLO_P!,for forecasting product
    Range("Q7:OC100000").ClearContents
    For Iteration = 1 To Range("G1").Value
        Range("I1").Value = Range("G1").Value - Iteration
        'For QMIN Zinc-Bulk
        Cells(6 + Iteration,17) = Iteration
        Cells(6 + Iteration,18) = Cells(39,4)
        Cells(6 + Iteration,19) = Cells(39,5)
        Cells(6 + Iteration,20) = Cells(39,6)
        Cells(6 + Iteration,21) = Cells(39,7)
        Cells(6 + Iteration,22) = Cells(39,8)
        Cells(6 + Iteration,23) = Cells(39,9)
        Cells(6 + Iteration,24) = Cells(39,10)
        Cells(6 + Iteration,25) = Cells(39,11)
        Cells(6 + Iteration,26) = Cells(39,12)
        Cells(6 + Iteration,27) = Cells(39,13)
        Cells(6 + Iteration,28) = Cells(39,14)
        Cells(6 + Iteration,29) = Cells(39,15)
        'For QMIN Zinc-Jugs
        Cells(6 + Iteration,30) = Iteration
        Cells(6 + Iteration,31) = Cells(40,4)
.... blah,blah,blah and so on for all products....
        Cells(6 + Iteration,444) = Cells(561,445) = Cells(561,15)
   Next Iteration
End Sub

这些语句的左侧代表记录输出数据的表位置,右侧是上述公式的输出。

我注意到这个 VBA 贯穿每一行。每行都会导致重新计算所有 500 个易失性 RAND() 函数。在 Core i7/32GB RAM 的所有 8 个内核上,此 VBA 的一次迭代需要 30 秒。我想定期运行 5,000 次迭代。

你能提出一些方法让这个模型更有效地运行吗?

  • 将一个单元格设为 RAND(),然后让所有 500 个单元格都引用它?
  • 重构 VBA?

为了让 Excel 运行更高效,我已经完成了所有基本/一般的事情。

解决方法

“我已经完成了所有基本/一般的事情以使 Excel 运行更有效”是什么意思?您是否尝试关闭屏幕更新等?

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

'Place your macro code here

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
,

一个小小的改变可能会产生很大的不同。

通过一次调用从数组中读取和写入大范围。 我现在无法检查确切的代码,但您可以说类似

   Range(Cells(6 + Iteration,18),Cells(6+Iteration,455).Value = Range(Cells(39,4),Cells(561,15).Value

或者通过拆分读写来检查调试器中的内容

   Dim TempValues As Variant ' Really a 2D array of variants

   TempValues = Range(Cells(39,15)).Value
   Range(Cells(6 + Iteration,Cells(6 + Iteration,455)).Value = TempValues

通过这样做,您将一次性读取/写入所有内容,并且仅写入一次,您将强制执行一次易失性计算,而不是每次写入时进行。

这看起来是一篇关于将范围作为数组读取和写入以及在调试器下检查内容的好文章:https://bettersolutions.com/excel/cells-ranges/vba-working-with-arrays.htm

其他一些想法:

  • 在 VBA 中用您自己的 rand 函数替换 volatile 函数(它可能只是在内部调用 Rnd)。然后,当您明确重新计算所有值时,if 只会获得现在的值(并重新计算)。
  • 在“记录”循环期间将工作表切换为手动计算,以防止在写入工作表时计算不稳定的单元格。
,

如果您想让 Excel 更高效,您必须考虑完全在 VBA 中运行您的蒙特卡罗模拟。将结果存储在一个数组中,并在所有模拟结束时将这些结果绘制在 excel 表中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-