Skip to content

mean(X; dims=2) not the same as mean of each row #29883

@sebastianskejoe

Description

@sebastianskejoe

Not sure if this is expected and/or acceptable behaviour, but I feel that it should at least be documented that mean(X; dims=2) can differ from [mean(X[i,:]) for i in 1:size(X,1)] - at least if the means are very small. I believe this is due to mean(X; dims=n) using mean! to calculate the mean over a given dimension, where as taking the mean of row calculates a "regular" mean (sum of elements divided by number of elements). Example output:

julia> [mean(X[i,:]) for i in 1:size(X,1)]
12-element Array{Float64,1}:
 -8.116963891148366e-16
 -1.64991477270683e-16
  1.450999814128156e-16
 -1.8621292779346593e-16
 -3.096288657565714e-16
 -9.86864910777917e-18
 -9.945747928933694e-18
 -9.398346298736568e-17
 -1.5034270125132327e-17
  6.908054375445419e-17
 -1.7578531223231644e-17
 -4.271274691960672e-17

julia> mean(X; dims=2)
12×1 Array{Float64,2}:
 -8.388351741612295e-16
 -1.8472877548624133e-16
  1.5743579279753957e-16
 -1.973151580397175e-16
 -3.367676508029642e-16
 -2.4671622769447924e-18
 -1.117932906740609e-17
 -9.274988184889329e-17
 -1.5651060694368526e-17
  6.661338147750939e-17
 -2.3746436915593628e-17
 -4.394632805807912e-17

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentationfoldsum, maximum, reduce, foldl, etc.statisticsThe Statistics stdlib module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions