如何在 Julia 中计算数组的方差?

log*_*ick 1 julia

我有以下数组,我需要找出它的方差:

\n
julia> a = [5, 6, 7, 8, 10, 12, 12, 17, 67, 68, 69, 72, 74, 74, 92, 93, 100, 105, 110, 120, 124]\n21-element Vector{Int64}:\n   5\n   6\n   7\n   8\n  10\n  12\n  12\n  17\n  67\n  68\n   \xe2\x8b\xae\n  74\n  74\n  92\n  93\n 100\n 105\n 110\n 120\n 124\n
Run Code Online (Sandbox Code Playgroud)\n

我怎样才能在朱莉娅中做到这一点?

\n

Bog*_*ski 6

Julia 的var功能内置于标准Statistics模块中。所以你可以这样做:

using Statistics
var(a)
Run Code Online (Sandbox Code Playgroud)

StatsBase.jl 包不会导出该var函数,因此您的代码在新的 Julia 会话中使用时将无法工作。您必须StatsBase.var(a)改为编写(或添加using Statistics)。

StatsBase.jl 向该var函数添加的内容是它定义了允许计算加权方差的附加方法。因此,例如以下内容适用于 StatsBase.jl(但目前没有它就无法工作):

julia> using Statistics

julia> using StatsBase

julia> var([1,2,3], Weights([1,2,3]))
0.5555555555555555
Run Code Online (Sandbox Code Playgroud)