def chained_lsqfit(<EOL>self, data=None, pdata=None, prior=None, p0=None, add_priornoise=None,<EOL>**kargs<EOL>):
if prior is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if add_priornoise:<EOL><INDENT>prior = prior + (gvar.sample(prior) - gvar.mean(prior))<EOL><DEDENT>kargs, oldargs = self.set(**kargs)<EOL>fitter_args_kargs = (<EOL>self.chained_lsqfit,<EOL>dict(data=data, prior=prior, pdata=pdata, models=self.models),<EOL>dict(kargs),<EOL>)<EOL>if self.fast:<EOL><INDENT>prior = self.buildprior(prior)<EOL><DEDENT>else:<EOL><INDENT>prior = gvar.BufferDict(prior)<EOL><DEDENT>chained_fits = collections.OrderedDict()<EOL>all_fnames = []<EOL>all_fitp = []<EOL>for tasktype, taskdata in self.tasklist:<EOL><INDENT>if tasktype == '<STR_LIT>':<EOL><INDENT>fitter = self.__class__(models=taskdata, **kargs)<EOL>fit = fitter.lsqfit(<EOL>data=data, pdata=pdata, prior=prior, p0=p0<EOL>)<EOL>fname = list(fit.chained_fits.keys())[<NUM_LIT:0>]<EOL>if fname in chained_fits:<EOL><INDENT>raise ValueError('<STR_LIT>' + str(fname))<EOL><DEDENT>elif fname[:<NUM_LIT:5>] == '<STR_LIT>' and fname[-<NUM_LIT:1>] == '<STR_LIT:)>':<EOL><INDENT>raise ValueError('<STR_LIT>' + fname)<EOL><DEDENT>else:<EOL><INDENT>all_fnames.append(fname)<EOL>chained_fits[fname] = fit<EOL>all_fitp.append(fit.p)<EOL><DEDENT><DEDENT>elif tasktype == '<STR_LIT>':<EOL><INDENT>lastfit = chained_fits[all_fnames[-<NUM_LIT:1>]]<EOL>lastfit_p = lastfit.p<EOL>for k in lastfit_p:<EOL><INDENT>idx = tuple(<EOL>slice(None, i) for i in numpy.shape(lastfit.p[k])<EOL>)<EOL>if idx != ():<EOL><INDENT>prior[k][idx] = lastfit.p[k]<EOL><DEDENT>else:<EOL><INDENT>prior[k] = lastfit.p[k]<EOL><DEDENT><DEDENT><DEDENT>elif tasktype == '<STR_LIT>':<EOL><INDENT>nlist = all_fnames[-taskdata:]<EOL>plist = [chained_fits[k].p for k in nlist]<EOL>fit = lsqfit.wavg(plist, **self.wavg_kargs).fit<EOL>fname = self.fitname('<STR_LIT>'.format('<STR_LIT:U+002C>'.join(nlist)))<EOL>all_fnames.append(fname)<EOL>chained_fits[fname] = fit<EOL><DEDENT>elif tasktype == '<STR_LIT>':<EOL><INDENT>kargs.update(taskdata)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>' + tasktype)<EOL><DEDENT><DEDENT>if self.fast and self.wavg_all:<EOL><INDENT>fit = lsqfit.wavg(all_fitp, **self.wavg_kargs).fit<EOL>fname = self.fitname('<STR_LIT>')<EOL>chained_fits[fname] = fit<EOL>prior = fit.p<EOL><DEDENT>self.fit = chained_nonlinear_fit(<EOL>p=prior, chained_fits=chained_fits,<EOL>multifitter=self, prior=fitter_args_kargs[<NUM_LIT:1>]['<STR_LIT>'],<EOL>)<EOL>fitter_args_kargs[<NUM_LIT:1>]['<STR_LIT>'] = self.fit.pmean<EOL>def _bstrap_iter(<EOL>n=None, datalist=None, pdatalist=None, **kargs<EOL>):<EOL><INDENT>return MultiFitter._bootstrapped_fit_iter(<EOL>fitter_args_kargs,<EOL>n=n, datalist=datalist, pdatalist=pdatalist, **kargs<EOL>)<EOL><DEDENT>self.fit.bootstrapped_fit_iter = _bstrap_iter<EOL>self.set(**oldargs)<EOL>return self.fit<EOL>
Compute chained least-squares fit of models to data.

        In a chained fit to models ``[s1, s2, ...]``, the models are fit one
        at a time, with the fit output from one being fed into the prior for
        the next. This can be much faster than  fitting the models together,
        simultaneously. The final result comes from the last fit in the chain,
        and includes parameters from all of the models.

        The most general chain has the structure ``[s1, s2, s3 ...]``
        where each ``sn`` is one of:

            1) A model (derived from :class:`multifitter.MultiFitterModel`).

            2) A tuple ``(m1, m2, m3)`` of models, to be fit together in
                a single fit (i.e., simultaneously). Simultaneous fits
                are useful for closely related models.

            3) A list ``[p1, p2, p3 ...]`` where each ``pn`` is either
                a model or a tuple of models (see #2). The ``pn`` are fit
                separately: the fit output from one fit is *not* fed into the
                prior of the next (i.e., the fits are effectively in
                parallel). Results from the separate fits are averaged at the
                end to provide a single composite result for the collection of
                fits. Parallel fits are effective (and fast) when the
                different fits have few or no fit parameters in common.

            4) A dictionary that (temporarily) resets default values for
                fitter keywords. The new values, specified in the dictionary,
                apply to subsequent fits in the chain. Any number of such
                dictionaries can be included in the model chain.


        Fit results are returned in a :class:`lsqfit.MultiFitter.chained_fit`
        object ``fit``, which is very similar to a :class:`nonlinear_fit`
        object (see documentation for more information). Object ``fit`` has an
        extra attribute ``fit.chained_fits`` which is an ordered dictionary
        containing fit results for each link in the chain of fits, indexed by
        fit names built from the corresponding data tags.

        To list results from all of the fits in the chain, use ::

            print(fit.formatall())

        This method has optional keyword arguments ``maxline``,
        ``pstyle``, and ``nline``; see the documentation for
        :meth:`lsqfit.nonlinear_fit.format` for more
        information.

        To view plots of each fit use

            fit.show_plots()

        This method has optional keyword arguments ``save`` and ``view``;
        see documentation for :class:`lsqfit.MultiFitter.show_plots`
        for more information. Plotting requires module :mod:`matplotlib`.

        To bootstrap a fit, use ``fit.bootstrapped_fit_iter(...)``;
        see :meth:`lsqfit.nonlinear_fit.bootstrapped_fit_iter` for more
        information.

        Args:
            data: Input data. One of ``data`` or ``pdata`` must be
                specified but not both. ``pdata`` is obtained from ``data``
                by collecting the output from ``m.builddata(data)``
                for each model ``m`` and storing it in a dictionary
                with key ``m.datatag``.
            pdata: Input data that has been processed by the
                models using :meth:`MultiFitter.process_data` or
                :meth:`MultiFitter.process_dataset`. One of
                ``data`` or ``pdata`` must be  specified but not both.
            prior: Bayesian prior for fit parameters used by the models.
            p0: Dictionary , indexed by parameter labels, containing
                initial values for the parameters in the fit. Setting
                ``p0=None`` implies that initial values are extracted from the
                prior. Setting ``p0="filename"`` causes the fitter to look in
                the file with name ``"filename"`` for initial values and to
                write out best-fit parameter values after the fit (for the
                next call to ``self.lsqfit()``).
            kargs: Arguments that override parameters specified when
                the :class:`MultiFitter` was created. Can also include
                additional arguments to be passed through to
                the :mod:`lsqfit` fitter.