MCPcopy Create free account
hub / github.com/dask/dask / moment

Function moment

dask/array/reductions.py:560–633  ·  view source on GitHub ↗

Calculate the nth centralized moment. Parameters ---------- a : Array Data over which to compute moment order : int Order of the moment that is returned, must be >= 2. axis : int, optional Axis along which the central moment is computed. The default is to

(
    a, order, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None, out=None
)

Source from the content-addressed store, hash-verified

558
559
560def moment(
561 a, order, axis=None, dtype=None, keepdims=False, ddof=0, split_every=None, out=None
562):
563 """Calculate the nth centralized moment.
564
565 Parameters
566 ----------
567 a : Array
568 Data over which to compute moment
569 order : int
570 Order of the moment that is returned, must be >= 2.
571 axis : int, optional
572 Axis along which the central moment is computed. The default is to
573 compute the moment of the flattened array.
574 dtype : data-type, optional
575 Type to use in computing the moment. For arrays of integer type the
576 default is float64; for arrays of float types it is the same as the
577 array type.
578 keepdims : bool, optional
579 If this is set to True, the axes which are reduced are left in the
580 result as dimensions with size one. With this option, the result
581 will broadcast correctly against the original array.
582 ddof : int, optional
583 "Delta Degrees of Freedom": the divisor used in the calculation is
584 N - ddof, where N represents the number of elements. By default
585 ddof is zero.
586
587 Returns
588 -------
589 moment : Array
590
591 References
592 ----------
593 .. [1] Pebay, Philippe (2008), "Formulas for Robust, One-Pass Parallel
594 Computation of Covariances and Arbitrary-Order Statistical Moments",
595 Technical Report SAND2008-6212, Sandia National Laboratories.
596
597 """
598 if not isinstance(order, Integral) or order < 0:
599 raise ValueError("Order must be an integer >= 0")
600
601 if order < 2:
602 reduced = a.sum(axis=axis) # get reduced shape and chunks
603 if order == 0:
604 # When order equals 0, the result is 1, by definition.
605 return ones(
606 reduced.shape, chunks=reduced.chunks, dtype="f8", meta=reduced._meta
607 )
608 # By definition the first order about the mean is 0.
609 return zeros(
610 reduced.shape, chunks=reduced.chunks, dtype="f8", meta=reduced._meta
611 )
612
613 if dtype is not None:
614 dt = dtype
615 else:
616 dt = getattr(np.var(np.ones(shape=(1,), dtype=a.dtype)), "dtype", object)
617

Callers 1

momentMethod · 0.90

Calls 4

reductionFunction · 0.90
sumMethod · 0.45
varMethod · 0.45
onesMethod · 0.45

Tested by

no test coverage detected