Пример #1
0
    def y_from_x(self, x, s_label=None, y_label=None):
        """Return an uncertain number ``y`` that predicts the response to ``x``

        :arg x: a real number, or an uncertain real number
        :arg s_label: a label for an elementary uncertain number associated with observation variability  
        :arg y_label: a label for the return uncertain number `y` 

        This is a prediction of a single future response ``y`` to a stimulus ``x``
        
        The variability in observations is based on residuals obtained during regression.
        
        An uncertain real number can be used for ``x``, in which
        case the associated uncertainty will also be propagated into ``y``.

        .. note::
            When ``y_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)
        
        """
        a, b = self._a_b
        df = self._N - 2

        u = math.sqrt(self._ssr / df)

        noise = ureal(0, u, df, label=s_label, independent=False)

        append_real_ensemble(a, noise)

        if y_label is None:
            y = a + b * x + noise
        else:
            y = result(a + b * x + noise, label=y_label)

        return y
Пример #2
0
    def y_from_x(self, x, s_y, s_label=None, y_label=None):
        """Return an uncertain number ``y`` that predicts the response to ``x``

        :arg x: a real number, or an uncertain real number
        :arg s_y: response variability uncertainty
        :arg s_label: a label for an elementary uncertain number associated with response variability  
        :arg y_label: a label for the return uncertain number `y` 

        Returns a single future response ``y`` predicted for a stimulus ``x``.

        The standard uncertainty ``s_y`` is used to create an additive
        component of uncertainty associated with variability in the ``y`` value.
        
        An uncertain real number can be used for ``x``, in which
        case the associated uncertainty is also propagated into ``y``.
        
        .. note::
            When ``y_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        """
        a, b = self._a_b
        df = self.N - 2

        noise = ureal(0, s_y, df, label=s_label)

        append_real_ensemble(a, noise)

        if y_label is None:
            y = a + b * x + noise
        else:
            y = result(a + b * x + noise, label=y_label)

        return y
Пример #3
0
    def x_from_y(self, yseq, u_yseq, x_label=None, y_label=None):
        """Estimate the stimulus ``x`` corresponding to the responses in ``yseq``

        :arg yseq: a sequence of further observations of ``y``
        :arg u_yseq: the standard uncertainty of the ``yseq`` data
        :arg x_label: a label for the return uncertain number `x` 
        :arg y_label: a label for the estimate of `y` based on ``yseq``

        The variations in ``yseq`` values are assumed to result from 
        independent random effects.
        
        ..note::
            When ``x_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        """
        a, b = self._a_b

        p = len(yseq)
        y = math.fsum(yseq) / p

        y = ureal(y,
                  u_yseq / math.sqrt(p),
                  inf,
                  label=y_label,
                  independent=False)

        append_real_ensemble(a, y)

        if x_label is None:
            x = (y - a) / b
        else:
            x = result((y - a) / b, label=x_label)

        return x
Пример #4
0
    def x_from_y(self, yseq, s_y, x_label=None, y_label=None):
        """Estimate the stimulus ``x`` corresponding to the responses in ``yseq``

        :arg yseq: a sequence of further observations of ``y``
        :arg s_y: a scale factor for the uncertainty of the ``yseq``
        :arg x_label: a label for the return uncertain number `x` 
        :arg y_label: a label for the estimate of `y` based on ``yseq``

        ..note::
            When ``x_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        """
        df = self._N - 2
        a, b = self._a_b

        p = len(yseq)
        y = math.fsum(yseq) / p

        y = ureal(y,
                  s_y * math.sqrt(self._ssr / df / p),
                  df,
                  label=y_label,
                  independent=False)

        append_real_ensemble(a, y)

        if x_label is None:
            x = (y - a) / b
        else:
            x = result((y - a) / b, label=x_label)

        return x
Пример #5
0
    def x_from_y(self, yseq, x_label=None, y_label=None):
        """Estimate the stimulus ``x`` corresponding to the responses in ``yseq``

        :arg yseq: a sequence of ``y`` observations 
        :arg x_label: a label for the return uncertain number `x` 
        :arg y_label: a label for the estimate of `y` based on ``yseq`` 

        .. note::
            When ``x_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        **Example** ::
        
            >>> x_data = [0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.5, 0.5, 0.5,
            ...                 0.7, 0.7, 0.7, 0.9, 0.9, 0.9]
            >>> y_data = [0.028, 0.029, 0.029, 0.084, 0.083, 0.081, 0.135, 0.131,
            ...                 0.133, 0.180, 0.181, 0.183, 0.215, 0.230, 0.216]

            >>> fit = type_a.line_fit(x_data,y_data)
            
            >>> x0 = fit.x_from_y( [0.0712, 0.0716] )            
            >>> x0
            ureal(0.2601659751037...,0.01784461112558...,13.0)

        """
        df = self._N - 2
        a, b = self._a_b

        p = len(yseq)
        y = math.fsum(yseq) / p

        y = ureal(y,
                  math.sqrt(self._ssr / df / p),
                  df,
                  label=y_label,
                  independent=False)

        append_real_ensemble(a, y)

        if abs(b) < 1E-15:
            # When b == 0, the best-fit line is horizontal
            # so no use of new y data is made
            x = a
        else:
            x = (y - a) / b

        if x_label is not None:
            x = result(x, label=x_label)

        return x
Пример #6
0
    def x_from_y(self, y_data, u_y_data, x_label=None, y_label=None):
        """Estimate the stimulus ``x`` corresponding to the responses in ``y_data``

        :arg y_data: a sequence of further observations of ``y``
        :arg u_y_data: the standard uncertainty of the ``y_data`` elements
        :arg x_label: a label for the return uncertain number `x` 
        :arg y_label: a label for the estimate of `y` based on ``y_data``

        The variations in ``y_data`` values are assumed to result from 
        independent random effects.
        
        .. note::
            When ``x_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        """
        a, b = self._a_b

        p = len(y_data)
        y = math.fsum(y_data) / p

        y = ureal(y,
                  u_y_data / math.sqrt(p),
                  inf,
                  label=y_label,
                  independent=False)

        append_real_ensemble(a, y)

        if abs(b) < 1E-15:
            # When b == 0, the best-fit line is horizontal
            # so no use of new y data is made
            x = a
        else:
            x = (y - a) / b

        if x_label is not None:
            x = result(x, label=x_label)

        return x
Пример #7
0
    def y_from_x(self, x, s_y, s_label=None, y_label=None):
        """Return an uncertain number ``y`` that predicts the response to ``x``

        :arg x: a real number, or an uncertain real number
        :arg s_y: a scale factor for the response uncertainty
        :arg s_label: a label for an elementary uncertain number associated with observation variability  
        :arg y_label: a label for the return uncertain number `y` 

        Returns a single future response ``y`` predicted for a stimulus ``x``.

        Because there is different variability in 
        the response to different stimuli, the
        scale factor ``s_y`` is required. It is assumed 
        that the standard deviation in the ``y`` value is 
        proportional to ``s_y``.
        
        An uncertain real number can be used for ``x``, in which
        case the associated uncertainty is also propagated into ``y``.
        
        .. note::
            When ``y_label`` is defined, the uncertain number returned will be 
            declared an intermediate result (using :func:`~.result`)

        """
        a, b = self._a_b
        df = self._N - 2

        u = math.sqrt(s_y * self._ssr / df)

        noise = ureal(0, u, df, label=s_label)

        append_real_ensemble(a, noise)

        if y_label is None:
            y = a + b * x + noise
        else:
            y = result(a + b * x + noise, label=y_label)

        return y