朱莉娅的中位数绝对偏差

Median absolute deviation in Julia

我使用 Monte Carlo 方法近似圆周率。
我在计算结果的中值绝对偏差时遇到了麻烦(请参见下面的代码)。
我的猜测是 Julia 中已经有一个函数可以执行此操作,但不幸的是我不知道如何在我的代码中实现它。

for i in 1:5
   picircle(1000)
end
3.0964517741129436
3.152423788105947
3.1284357821089457
3.1404297851074463
3.0904547726136933

正如 MarcMush 在评论中提到的,StatsBase 导出了 mad 函数,我们可以从文档字符串中看到

help?> mad
search: mad mad! maxad muladd mapreduce meanad ismarked mapfoldr mapfoldl mean_and_var mean_and_std mean_and_cov macroexpand

  mad(x; center=median(x), normalize=true)

  Compute the median absolute deviation (MAD) of collection x around center (by default, around the median).

  If normalize is set to true, the MAD is multiplied by 1 / quantile(Normal(), 3/4) ≈ 1.4826, in order to obtain a consistent
  estimator of the standard deviation under the assumption that the data is normally distributed.

所以

julia> A = randn(10000);

julia> using StatsBase

julia> mad(A, normalize=false)
0.6701649037518176

或者,如果您不想要 StatsBase 依赖项,那么您可以直接使用(例如)

来计算它
julia> using Statistics

julia> median(abs.(A .- median(A)))
0.6701649037518176

给出相同的结果