segmentation/bmoss/change_detec.py
def update(self, X, Y, basisfunc=None):
        """Update model with a single observation.

        When new input-output data is available, the model can be updated using
        this method. As more and more data are collected, the number of
        hypotheses grows, increasing the computational complexity. By default
        hypotheses are pruned at the end of each update (see
        :py:meth:`.trim`.). To disable hypotheses trimming, initialise the
        class with ``maxhypot`` set to ``None``.

        Args:
            X (numpy.array): Observed (1 x M) input data (predictor variable).
            Y (numpy.array): Observed (1 x N) output data (response variable).

        """

        # Initialise algorithm on first call to update. This allows the
        # algorithm to configure itself to the size of the first input/output
        # data if no hyper-parameters have been specified.
        if not self.__initialised:
            init_basis = self.__basisfunc if basisfunc is None else basisfunc
            x = init_basis(X)
            m = x.shape[1] if np.ndim(x) > 1 else X.size
            n = Y.shape[1] if np.ndim(Y) > 1 else Y.size
            self.__initialise_algorithm(m, n)
            self.__initialised = True

        # Get size of data.
        k = X.shape[0] if np.ndim(X) > 1 else X.size
        m, n = self.__m, self.__n

        # Allocate variables for the dynamic programming pass.
        loglik = -np.inf
        logmax = -np.inf
        logsum = -np.inf
        ind = np.nan

        # Update hypotheses by updating each matrix variate, normal inverse
        # gamma distribution over the linear models.
        for hypothesis in self.__hypotheses:

            # Update the sufficient statistics.
            hypothesis['distribution'].update(hypothesis['basisfunc'](X), Y)

            # Compute the log-normalization constant after the update
            # (posterior parameter distribution).
            # (Equation 8)
            n_o = hypothesis['log_constant']
            n_k = hypothesis['log_constant'] = hypothesis['distribution'].log_constant()

            # Evaluate the log-density of the predictive distribution.
            # (Equation 16)
            log_density = n_k - n_o - k * (0.5 * m * n) * np.log(2.0 * np.pi)

            # Increment the counter.
            hypothesis['count'] += 1

            # Accumulate the log-likelihood of the data.
            # (Equation 17)
            hazard = self.__ratefun(hypothesis['count'])
            aux = np.log(hazard) + log_density + hypothesis['log_probability']
            loglik = self.__soft_max(loglik, aux)

            # Keep track of the highest, log-likelihood.
            if aux > logmax:
                logmax, ind = aux, hypothesis['count']

            # Update and accumulate the log-probabilities.
            hypothesis['log_probability'] += np.log1p(-hazard) + log_density
            logsum = self.__soft_max(logsum, hypothesis['log_probability'])

        # In the max-product algorithm, keep track of the most likely
        # hypotheses.
        if self.__alg__ == 'maxprod':
            loglik = logmax
            self.__counts.append(ind)

        # Add a new hypothesis, which states that the next segment is about to
        # begin.
        self.__add_new_hypothesis(loglik, basisfunc)

        # Normalize the hypotheses so that their probabilities sum to one.
        logsum = self.__soft_max(logsum, loglik)
        for hypothesis in self.__hypotheses:
            hypothesis['log_probability'] -= logsum

        # Automatically trim hypothesis on each update if requested.
        if self.__maximum_hypotheses is not None:
            self.trim_hypotheses(minprob=self.__minimum_probability,
                                 maxhypot=self.__maximum_hypotheses)

        # In the sum-product algorithm, keep track of the probabilities.
        if self.__alg__ == 'sumprod':
            iteration = list()
            for hypothesis in self.__hypotheses:
                iteration.append((hypothesis['count'],
                                  hypothesis['log_probability']))

            self.__probabilities.append(iteration)
Update model with a single observation.

        When new input-output data is available, the model can be updated using
        this method. As more and more data are collected, the number of
        hypotheses grows, increasing the computational complexity. By default
        hypotheses are pruned at the end of each update (see
        :py:meth:`.trim`.). To disable hypotheses trimming, initialise the
        class with ``maxhypot`` set to ``None``.

        Args:
            X (numpy.array): Observed (1 x M) input data (predictor variable).
            Y (numpy.array): Observed (1 x N) output data (response variable).

        
Update model with a single observation.
When new input-output data is available, the model can be updated using
this method. As more and more data are collected, the number of
hypotheses grows, increasing the computational complexity. By default
hypotheses are pruned at the end of each update (see
:py:meth:`.trim`.).
Update model with a single observation . When new input - output data is available the model can be updated using this method . As more and more data are collected the number of hypotheses grows increasing the computational complexity . By default hypotheses are pruned at the end of each update ( see : py : meth : ` . trim ` . ) .
def update(self, X, Y, basisfunc=None):
        if not self.__initialised:
            init_basis = self.__basisfunc if basisfunc is None else basisfunc
            x = init_basis(X)
            m = x.shape[1] if np.ndim(x) > 1 else X.size
            n = Y.shape[1] if np.ndim(Y) > 1 else Y.size
            self.__initialise_algorithm(m, n)
            self.__initialised = True
        k = X.shape[0] if np.ndim(X) > 1 else X.size
        m, n = self.__m, self.__n
        loglik = -np.inf
        logmax = -np.inf
        logsum = -np.inf
        ind = np.nan
        for hypothesis in self.__hypotheses:
            hypothesis['distribution'].update(hypothesis['basisfunc'](X), Y)
            n_o = hypothesis['log_constant']
            n_k = hypothesis['log_constant'] = hypothesis['distribution'].log_constant()
            log_density = n_k - n_o - k * (0.5 * m * n) * np.log(2.0 * np.pi)
            hypothesis['count'] += 1
            hazard = self.__ratefun(hypothesis['count'])
            aux = np.log(hazard) + log_density + hypothesis['log_probability']
            loglik = self.__soft_max(loglik, aux)
            if aux > logmax:
                logmax, ind = aux, hypothesis['count']
            hypothesis['log_probability'] += np.log1p(-hazard) + log_density
            logsum = self.__soft_max(logsum, hypothesis['log_probability'])
        if self.__alg__ == 'maxprod':
            loglik = logmax
            self.__counts.append(ind)
        self.__add_new_hypothesis(loglik, basisfunc)
        logsum = self.__soft_max(logsum, loglik)
        for hypothesis in self.__hypotheses:
            hypothesis['log_probability'] -= logsum
        if self.__maximum_hypotheses is not None:
            self.trim_hypotheses(minprob=self.__minimum_probability,
                                 maxhypot=self.__maximum_hypotheses)
        if self.__alg__ == 'sumprod':
            iteration = list()
            for hypothesis in self.__hypotheses:
                iteration.append((hypothesis['count'],
                                  hypothesis['log_probability']))
            self.__probabilities.append(iteration)
def update ( self , X , Y , basisfunc = None ) : if not self . __initialised : init_basis = self . __basisfunc if basisfunc is None else basisfunc x = init_basis ( X ) m = x . shape [ 1 ] if np . ndim ( x ) > 1 else X . size n = Y . shape [ 1 ] if np . ndim ( Y ) > 1 else Y . size self . __initialise_algorithm ( m , n ) self . __initialised = True k = X . shape [ 0 ] if np . ndim ( X ) > 1 else X . size m , n = self . __m , self . __n loglik = - np . inf logmax = - np . inf logsum = - np . inf ind = np . nan for hypothesis in self . __hypotheses : hypothesis [ 'distribution' ] . update ( hypothesis [ 'basisfunc' ] ( X ) , Y ) n_o = hypothesis [ 'log_constant' ] n_k = hypothesis [ 'log_constant' ] = hypothesis [ 'distribution' ] . log_constant ( ) log_density = n_k - n_o - k * ( 0.5 * m * n ) * np . log ( 2.0 * np . pi ) hypothesis [ 'count' ] += 1 hazard = self . __ratefun ( hypothesis [ 'count' ] ) aux = np . log ( hazard ) + log_density + hypothesis [ 'log_probability' ] loglik = self . __soft_max ( loglik , aux ) if aux > logmax : logmax , ind = aux , hypothesis [ 'count' ] hypothesis [ 'log_probability' ] += np . log1p ( - hazard ) + log_density logsum = self . __soft_max ( logsum , hypothesis [ 'log_probability' ] ) if self . __alg__ == 'maxprod' : loglik = logmax self . __counts . append ( ind ) self . __add_new_hypothesis ( loglik , basisfunc ) logsum = self . __soft_max ( logsum , loglik ) for hypothesis in self . __hypotheses : hypothesis [ 'log_probability' ] -= logsum if self . __maximum_hypotheses is not None : self . trim_hypotheses ( minprob = self . __minimum_probability , maxhypot = self . __maximum_hypotheses ) if self . __alg__ == 'sumprod' : iteration = list ( ) for hypothesis in self . __hypotheses : iteration . append ( ( hypothesis [ 'count' ] , hypothesis [ 'log_probability' ] ) ) self . __probabilities . append ( iteration )
Update model with a single observation.
Update model with a single observation .
"""Update model with a single observation.

        When new input-output data is available, the model can be updated using
        this method. As more and more data are collected, the number of
        hypotheses grows, increasing the computational complexity. By default
        hypotheses are pruned at the end of each update (see
        :py:meth:`.trim`.). To disable hypotheses trimming, initialise the
        class with ``maxhypot`` set to ``None``.

        Args:
            X (numpy.array): Observed (1 x M) input data (predictor variable).
            Y (numpy.array): Observed (1 x N) output data (response variable).

        """ # Initialise algorithm on first call to update. This allows the # algorithm to configure itself to the size of the first input/output # data if no hyper-parameters have been specified. # Get size of data. # Allocate variables for the dynamic programming pass. # Update hypotheses by updating each matrix variate, normal inverse # gamma distribution over the linear models. # Update the sufficient statistics. # Compute the log-normalization constant after the update # (posterior parameter distribution). # (Equation 8) # Evaluate the log-density of the predictive distribution. # (Equation 16) # Increment the counter. # Accumulate the log-likelihood of the data. # (Equation 17) # Keep track of the highest, log-likelihood. # Update and accumulate the log-probabilities. # In the max-product algorithm, keep track of the most likely # hypotheses. # Add a new hypothesis, which states that the next segment is about to # begin. # Normalize the hypotheses so that their probabilities sum to one. # Automatically trim hypothesis on each update if requested. # In the sum-product algorithm, keep track of the probabilities.
self
X
Observed (1 x M) input data (predictor variable).
Observed ( 1 x M ) input data ( predictor variable ) .
Y
Observed (1 x N) output data (response variable).
Observed ( 1 x N ) output data ( response variable ) .
basisfunc