def wavg(datalist, fast=False, prior=None, **fitterargs):
if prior is not None:<EOL><INDENT>datalist = list(datalist) + [prior]<EOL>warnings.warn(<EOL>'<STR_LIT>',<EOL>DeprecationWarning<EOL>)<EOL><DEDENT>if len(datalist) <= <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>elif len(datalist) == <NUM_LIT:1>:<EOL><INDENT>if hasattr(datalist[<NUM_LIT:0>], '<STR_LIT>'):<EOL><INDENT>return BufferDictWAvg(datalist[<NUM_LIT:0>], None)<EOL><DEDENT>if numpy.shape(datalist[<NUM_LIT:0>]) == ():<EOL><INDENT>return GVarWAvg(datalist[<NUM_LIT:0>], None)<EOL><DEDENT>else:<EOL><INDENT>return ArrayWAvg(numpy.asarray(datalist[<NUM_LIT:0>]), None)<EOL><DEDENT><DEDENT>if fast:<EOL><INDENT>chi2 = dof = time = svdcorrection = <NUM_LIT:0><EOL>ans = datalist[<NUM_LIT:0>]<EOL>for i, di in enumerate(datalist[<NUM_LIT:1>:]):<EOL><INDENT>ans = wavg([ans, di], fast=False, **fitterargs)<EOL>chi2 += ans.chi2<EOL>dof += ans.dof<EOL>time += ans.time<EOL>svdcorrection += ans.svdcorrection<EOL><DEDENT>ans.fit.dof = dof<EOL>ans.fit.Q = _gammaQ(dof / <NUM_LIT>, chi2 / <NUM_LIT>)<EOL>ans.fit.chi2 = chi2<EOL>ans.fit.time = time<EOL>ans.fit.svdcorrection = svdcorrection<EOL>return ans<EOL><DEDENT>if hasattr(datalist[<NUM_LIT:0>], '<STR_LIT>'):<EOL><INDENT>datashape = None<EOL><DEDENT>else:<EOL><INDENT>datashape = numpy.shape(datalist[<NUM_LIT:0>])<EOL>datalist = [{None:di} for di in datalist]<EOL><DEDENT>p0shape = {}<EOL>p0index = {}<EOL>data = gvar.BufferDict()<EOL>for i, di in enumerate(datalist):<EOL><INDENT>for k in di:<EOL><INDENT>data[k, i] = di[k]<EOL>shape = numpy.shape(di[k])<EOL>p0index[k, i] = tuple(slice(<NUM_LIT:0>, j) for j in shape)<EOL>if k not in p0shape:<EOL><INDENT>p0shape[k] = shape<EOL><DEDENT>elif p0shape[k] != shape:<EOL><INDENT>p0shape[k] = tuple(<EOL>max(j1, j2) for j1, j2 in zip(shape, p0shape[k])<EOL>)<EOL><DEDENT><DEDENT><DEDENT>p0 = gvar.BufferDict()<EOL>p0count = {}<EOL>for k, i in data:<EOL><INDENT>if k not in p0:<EOL><INDENT>p0[k] = numpy.zeros(p0shape[k], float)<EOL>p0count[k] = numpy.zeros(p0shape[k], float)<EOL><DEDENT>if p0index[k, i] == ():<EOL><INDENT>p0[k] += data[k, i].mean<EOL>p0count[k] += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>p0[k][p0index[k, i]] += gvar.mean(data[k, i])<EOL>p0count[k][p0index[k, i]] += <NUM_LIT:1.><EOL><DEDENT><DEDENT>for k in p0:<EOL><INDENT>p0[k] /= p0count[k]<EOL><DEDENT>def fcn(p):<EOL><INDENT>ans = gvar.BufferDict()<EOL>for k, i in data:<EOL><INDENT>shape = data[k, i].shape<EOL>if shape == ():<EOL><INDENT>ans[k, i] = p[k]<EOL><DEDENT>else:<EOL><INDENT>ans[k, i] = p[k][p0index[k, i]]<EOL><DEDENT><DEDENT>return ans<EOL><DEDENT>fit = lsqfit.nonlinear_fit(data=data, fcn=fcn, p0=p0, **fitterargs)<EOL>if datashape is None:<EOL><INDENT>return BufferDictWAvg(fit.p, fit)<EOL><DEDENT>elif datashape == ():<EOL><INDENT>return GVarWAvg(fit.p[None], fit)<EOL><DEDENT>else:<EOL><INDENT>return ArrayWAvg(fit.p[None], fit)<EOL><DEDENT>
Weighted average of |GVar|\s or arrays/dicts of |GVar|\s.

    The weighted average of ``N`` |GVar|\s ::

        xavg = wavg([g1, g2 ... gN])

    is what one obtains from a weighted least-squares fit of the
    collection of |GVar|\s to the one-parameter fit function ::

        def f(p):
            return N * [p]

    The average is the best-fit value for fit parameter ``p``.  |GVar|\s
    with smaller standard deviations carry more weight than those with
    larger standard deviations; and the averages take account of
    correlations between the |GVar|\s.

    ``wavg`` also works when each ``gi`` is an array of |GVar|\s or a
    dictionary whose values are |GVar|\s or arrays of |GVar|\s.
    Corresponding arrays in different ``gi``\s must have the same dimension,
    but can have different shapes (the overlapping components are
    averaged).  When the ``gi`` are dictionaries, they need not all have
    the same keys.

    Weighted averages can become costly when the number of random samples
    being averaged is large (100s or more). In such cases it might be useful
    to set parameter ``fast=True``. This causes ``wavg`` to estimate the
    weighted average by incorporating the random samples one at a time into a
    running average::

        result = datalist[0]
        for di in datalist[1:]:
            result = wavg([result, di], ...)

    This method can be much faster when ``len(datalist)`` is large, and gives
    the exact result when there are no correlations between different elements
    of list ``datalist``. The results are approximately correct when
    ``datalist[i]`` and ``datalist[j]`` are correlated for ``i!=j``.

    Args:
        datalist (list): The |GVar|\s to be averaged. ``datalist`` is
            a one-dimensional sequence of |GVar|\s, or of arrays of |GVar|\s,
            or of dictionaries containing |GVar|\s  and/or arrays of |GVar|\s.
            Corresponding arrays in different ``datalist[i]``\s must have the
            same dimension.
        fast (bool): If ``fast=True``, ``wavg`` averages the ``datalist[i]``
            sequentially. This can be much faster when averaging a large
            number of sampes but is only approximate if the different
            elements of ``datalist`` are correlated. Default is ``False``.
        fitterargs (dict): Additional arguments (e.g., ``svdcut``) for the
            :class:`lsqfit.nonlinear_fit` fitter used to do the averaging.

    Returns:
        The weighted average is returned as a |GVar| or an array of
        |GVar|\s or a dictionary of |GVar|\s and arrays of |GVar|\s.
        Results have the following extra attributes:

        **chi2** - ``chi**2`` for weighted average.

        **dof** - Effective number of degrees of freedom.

        **Q** - Quality factor `Q` (or *p-value*) for fit:
            the probability that the ``chi**2`` could have been larger,
            by chance, assuming that the data are all Gaussian and consistent
            with each other. Values smaller than 0.1 or so suggest that the
            data are not Gaussian or are inconsistent with each other. Also
            called the *p-value*.

        **time** - Time required to do average.

        **svdcorrection** - The *svd* corrections made to the data
            when ``svdcut`` is not ``None``.

        **fit** - Fit returned by :class:`lsqfit.nonlinear_fit`.