【问题标题】:Why does the matlab bootstrap procedure evaluate N+1 times?为什么 matlab 引导程序会评估 N+1 次?
【发布时间】:2016-03-09 14:22:56
【问题描述】:

我想使用内置命令“bootstrp”通过 matlab 进行引导。我注意到的是,当我只要求 N 次迭代时,该过程会进行 N+1 次迭代。这是为什么?当我构建一个手动循环来进行引导时,它实际上只运行了 N 次,那么它会更快。这是该问题的一个最小示例:

clear all

global iterationcounter

tic

iterationcounter=0;
data=unifrnd(0,1,1,1000); %draw vector of 1000 random numbers

bootstat = bootstrp(100,@testmean,data); %evaluate function for 100 bootstrap samples
toc

使用函数

function [ m ] = testmean( data )
global iterationcounter

m=mean(data);

iterationcounter=iterationcounter+1

end

该函数应该评估 100 个样本,但是当我运行脚本时,它将评估该函数 101 次:

...

迭代计数器 =

101

经过的时间是 0.102291 秒。

那么为什么要使用这个看起来很浪费时间的内置 Matlab 函数呢?

【问题讨论】:

    标签: matlab statistics-bootstrap


    【解决方案1】:

    bootstrp 调用 bootfun(函数参数)进行完整性检查(来自源代码,在 MATLAB 2015b 中,bootstrp.m,l.167 ff):

    % Sanity check bootfun call and determine dimension and type of result
    try
        % Get result of bootfun on actual data, force to a row.
        bootstat = feval(bootfun,bootargs{:});
        bootstat = bootstat(:)';
    catch ME
        m = message('stats:bootstrp:BadBootFun');
        MEboot =  MException(m.Identifier,'%s',getString(m));
        ME = addCause(ME,MEboot);
        rethrow(ME);
    end
    

    我认为在一个实际的应用程序中,N>>100,所以额外开销(远)小于总运行时间的百分之一(不考虑可能的并行化带来的速度增益),所以这无关紧要多少?

    【讨论】:

    • 这就解释了,谢谢!你是对的,随着 N 变大,差异不会那么重要。尽管如此,它还是让我感到困惑,如果您尝试测试代码进行一两次迭代,那么额外运行所需的时间是其一半或三倍,因此您开始注意到(一次迭代需要 1-3 分钟就我而言)。
    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2012-03-06
    • 2021-11-01
    相关资源
    最近更新 更多