首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google电子表格上运行Montecarlo分析

在Google电子表格上运行Montecarlo分析
EN

Stack Overflow用户
提问于 2018-05-03 17:46:11
回答 2查看 5.3K关注 0票数 3

一个复杂的谷歌电子表格有多个输入和一个输出,我需要对其运行MonteCarlo分析。在Excel中,我会使用"DYI“MonteCarlo方法,将如下公式

代码语言:javascript
复制
=norminv(rand(),expected_return,st_deviation)

在输入上,有一个带有循环的简单宏,通常运行1000次或更多,这会触发电子表格重新计算(相当于按F9键),并记录输出单元格的结果。

在谷歌电子表格上,我找不到一种从谷歌应用程序脚本触发电子表格重新计算的方法。

有没有办法,或者更好/更智能的架构来在谷歌电子表格中进行MonteCarlo分析(最好不要使用我不明白他们在对数据做什么的插件,或者需要无限制地访问我所有的文件)?

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2020-04-19 06:05:15

我刚刚开发了一个add-on,它可以让你在Google Sheets中运行蒙特卡洛模拟。请注意,它在Google Sheets中运行所有计算(这就是为什么它可能会有点慢),所以你不必担心隐私/安全问题。

票数 1
EN

Stack Overflow用户

发布于 2018-05-03 17:53:38

这似乎起到了作用(来自这个link)

代码语言:javascript
复制
/**
 * @OnlyCurrentDoc  Limits the script to only accessing the current spreadsheet.
 */


/**
 * Adds a custom menu with items to show the sidebar and dialog.
 *
 * @param {Object} e The event parameter for a simple onOpen trigger.
 */
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createAddonMenu()
      .addItem('Re-calculate selected cells', 'recalculate')
      .addToUi();
}


/**
 * Force Spreadsheet to re-calculate selected cells
 */
function recalculate(){
  var activeRange = SpreadsheetApp.getActiveRange();
  var originalFormulas = activeRange.getFormulas();
  var originalValues = activeRange.getValues();

  var valuesToEraseFormula = [];
  var valuesToRestoreFormula = [];

  originalFormulas.forEach(function(outerVal, outerIdx){
    valuesToEraseFormula[outerIdx] = [];
    valuesToRestoreFormula[outerIdx] = [];
    outerVal.forEach(function(innerVal, innerIdx){
      if('' === innerVal){
        //The cell doesn't have formula
        valuesToEraseFormula[outerIdx][innerIdx] = originalValues[outerIdx][innerIdx];
        valuesToRestoreFormula[outerIdx][innerIdx] = originalValues[outerIdx][innerIdx];
      }else{
        //The cell has a formula.
        valuesToEraseFormula[outerIdx][innerIdx] = '';
        valuesToRestoreFormula[outerIdx][innerIdx] = originalFormulas[outerIdx][innerIdx];
      }
    })
  })

  activeRange.setValues(valuesToEraseFormula);
  activeRange.setValues(valuesToRestoreFormula);
}


/**
 * Runs when the add-on is installed; calls onOpen() to ensure menu creation and
 * any other initializion work is done immediately.
 *
 * @param {Object} e The event parameter for a simple onInstall trigger.
 */
function onInstall(e) {
  onOpen(e);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50151906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档