微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Julia 的中值绝对偏差

如何解决Julia 的中值绝对偏差

我使用蒙特卡洛方法近似了 pi。
我在计算结果的中值绝对偏差时遇到了麻烦(请参阅下面的代码)。
我的猜测是 Julia 中已经有一个函数可以做到这一点,但不幸的是我不知道如何在我的代码中实现它。

enter image description here

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

给出相同的结果

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