forward step for HMM, assuming both the hidden state and noise
            have 1-step dependence on the previous value.
515222
def _forward_step(self, Y, T_X, Var_X, Var_dX, rho_e, sigma2_e, weight):
        
        # We currently only implement diagonal form
        # of covariance matrix for Var_X, Var_dX and T_X, which means
        # each dimension of X is independent and their innovation noise
        # are also independent. Note that log_p_data takes this assumption.
        if Var_X.ndim == 1:
            inv_Var_X = np.diag(1 / Var_X)
            half_log_det_Var_X = np.sum(np.log(Var_X)) / 2.0
            Var_X = np.diag(Var_X)
            # the marginal variance of X
        else:
            half_log_det_Var_X = self._half_log_det(Var_X)
            inv_Var_X = np.linalg.inv(Var_X)
        if Var_dX.ndim == 1:
            inv_Var_dX = np.diag(1 / Var_dX)
            half_log_det_Var_dX = np.sum(np.log(Var_dX)) / 2.0
            Var_dX = np.diag(Var_dX)
            # the marginal variance of Delta X (the change of X from
            # previous time point)
        else:
            inv_Var_dX = np.linalg.inv(Var_dX)
            half_log_det_Var_dX = self._half_log_det(Var_dX)
        if T_X.ndim == 1:
            T_X = np.diag(T_X)
            # Transfer function of X: the expected mean of X at t+1
            # time point is T_x * X
        [n_T, n_V] = np.shape(Y)
        # numbers of time points and voxels
        mu = [None] * n_T
        # posterior mean of X, conditioned on all data up till the current
        # time point
        Gamma_inv = [None] * n_T
        # inverse of poterior Gamma.
        mu_Gamma_inv = [None] * n_T
        # mu * inv(Gamma)
        log_p_data = - np.log(np.pi * 2) * (n_T * n_V) / 2 \
            - half_log_det_Var_X - np.sum(np.log(sigma2_e)) * n_T / 2.0\
            + np.sum(np.log(1 - rho_e**2)) / 2.0 - half_log_det_Var_dX \
            * (n_T - 1)
        # This is the term to be incremented by c_n at each time step.
        # We first add all the fixed terms to it.

        # The following are a few fixed terms.
        Lambda_0 = np.dot(T_X, np.dot(inv_Var_dX, T_X.T)) \
            + np.dot(weight * rho_e**2 / sigma2_e, weight.T)
        H = np.dot(inv_Var_dX, T_X.T) + np.dot(weight * rho_e / sigma2_e,
                                               weight.T)
        Lambda_1 = inv_Var_dX + np.dot(weight / sigma2_e, weight.T)

        Gamma_inv[0] = inv_Var_X + np.dot(
            weight * (1 - rho_e**2) / sigma2_e, weight.T)
        # We might not need this and only use linalg.solve for related terms.
        mu_Gamma_inv[0] = np.dot(
            Y[0, :] * (1 - rho_e**2) / sigma2_e, weight.T)
        mu[0] = np.linalg.solve(Gamma_inv[0], mu_Gamma_inv[0])
        log_p_data -= 0.5 * np.sum(Y[0, :]**2 * (1 - rho_e**2) / sigma2_e)
        # This is the term added for the first time point.

        deltaY = Y[1:, :] - rho_e * Y[:-1, :]
        deltaY_sigma2inv_rho_weightT = np.dot(
            deltaY / sigma2_e * rho_e, weight.T)
        for t in np.arange(1, n_T):
            Gamma_tilde_inv = Lambda_0 + Gamma_inv[t - 1]
            tmp = np.linalg.solve(Gamma_tilde_inv, H.T)
            Gamma_inv[t] = Lambda_1 - np.dot(H, tmp)
            mu_Gamma_inv[t] = np.dot(deltaY[t - 1, :] / sigma2_e, weight.T) \
                + np.dot(mu_Gamma_inv[t - 1]
                         - deltaY_sigma2inv_rho_weightT[t - 1, :], tmp)
            mu[t] = np.linalg.solve(Gamma_inv[t], mu_Gamma_inv[t])
            tmp2 = mu_Gamma_inv[t - 1] - deltaY_sigma2inv_rho_weightT[t - 1, :]
            log_p_data += -self._half_log_det(Gamma_tilde_inv) \
                + np.dot(tmp2, np.linalg.solve(Gamma_tilde_inv, tmp2)) / 2.0
        log_p_data += -self._half_log_det(Gamma_inv[-1]) \
            + np.dot(mu_Gamma_inv[-1], mu[-1]) / 2.0 \
            - np.sum(deltaY**2 / sigma2_e) / 2.0
        return mu, mu_Gamma_inv, Gamma_inv, log_p_data, Lambda_0, \
            Lambda_1, H, deltaY, deltaY_sigma2inv_rho_weightT
86001
def step1c(self):
        """"""
        if self.ends("y") and self.vowelinstem():
            self.b = self.b[: self.k] + "i" + self.b[self.k + 1 :]
7124
private double[][] _step(double[][] state, char ch1, char ch2) {
    double cost;
    _shift(state);
    state[2][0] = state[1][0] + deletionDistance;
    for (int i = 0; i < morseBase.length(); i++) {
      cost = (morseBase.charAt(i) == ch1) ? 0 : replaceDistance;
      state[2][i + 1] = Math.min(state[2][i] + insertionDistance,
          state[1][i] + cost);
      state[2][i + 1] = Math.min(state[2][i + 1],
          state[1][i + 1] + deletionDistance);
      if (i > 0 && ch2 != 0x00 && (morseBase.charAt(i - 1) == ch1)
          && (morseBase.charAt(i) == ch2)) {
        state[2][i + 1] = Math.min(state[2][i + 1],
            state[0][i - 1] + transpositionDistance);
      }
    }
    return state;
  }
145848
def two_gaussian(freq, freq0_1, freq0_2, sigma1, sigma2, amp1, amp2,
                   offset, drift):
   
   return (gaussian(freq, freq0_1, sigma1, amp1, offset, drift) +
           gaussian(freq, freq0_2, sigma2, amp2, offset, drift))
838518
private ForwardStepResult<S, O, D> forwardStep(O observation, Collection<S> prevCandidates,
            Collection<S> curCandidates, Map<S, Double> message,
            Map<S, Double> emissionLogProbabilities,
            Map<Transition<S>, Double> transitionLogProbabilities,
            Map<Transition<S>,D> transitionDescriptors) {
        final ForwardStepResult<S, O, D> result = new ForwardStepResult<>(curCandidates.size());
        assert !prevCandidates.isEmpty();

        for (S curState : curCandidates) {
            double maxLogProbability = Double.NEGATIVE_INFINITY;
            S maxPrevState = null;
            for (S prevState : prevCandidates) {
                final double logProbability = message.get(prevState) + transitionLogProbability(
                        prevState, curState, transitionLogProbabilities);
                if (logProbability > maxLogProbability) {
                    maxLogProbability = logProbability;
                    maxPrevState = prevState;
                }
            }
            // Throws NullPointerException if curState is not stored in the map.
            result.newMessage.put(curState, maxLogProbability
                    + emissionLogProbabilities.get(curState));

            // Note that maxPrevState == null if there is no transition with non-zero probability.
            // In this case curState has zero probability and will not be part of the most likely
            // sequence, so we don't need an ExtendedState.
            if (maxPrevState != null) {
                final Transition<S> transition = new Transition<>(maxPrevState, curState);
                final ExtendedState<S, O, D> extendedState = new ExtendedState<>(curState,
                        lastExtendedStates.get(maxPrevState), observation,
                        transitionDescriptors.get(transition));
                result.newExtendedStates.put(curState, extendedState);
            }
        }
        return result;
    }
175374
def _pre_train(self,
                   stop_param_updates,
                   num_epochs,
                   updates_epoch):
        """"""
        # Calculate the total number of updates given early stopping.
        updates = {k: stop_param_updates.get(k, num_epochs) * updates_epoch
                   for k, v in self.params.items()}

        # Calculate the value of a single step given the number of allowed
        # updates.
        single_steps = {k: np.exp(-((1.0 - (1.0 / v)))
                        * self.params[k]['factor'])
                        for k, v in updates.items()}

        # Calculate the factor given the true factor and the value of a
        # single step.
        constants = {k: np.exp(-self.params[k]['factor']) / v
                     for k, v in single_steps.items()}

        return constants
843587
def combination_step(self):
        

        # Update t step
        tprv = self.t
        self.t = 0.5 * float(1. + np.sqrt(1. + 4. * tprv**2))

        # Update Y
        if not self.opt['FastSolve']:
            self.Yprv = self.Y.copy()
        self.Y = self.X + ((tprv - 1.) / self.t) * (self.X - self.Xprv)
461528
def _MStep(self, l, K, x, z_h1, pi_h1, p_h1, p_h, delta_i_j_s):
        
        for k in range(K):
            normconst = 0
            if l == 0: # only need to compute pi at first MM iteration
                pi_h1[k] = np.sum(z_h1.T[k]) / len(z_h1)
            for j in range(self.m):
                omega_k_j = EMMMixPLAggregator.omega(k, j, z_h1, x) # numerator
                denom_sum = 0
                for i in range(self.n):
                    sum1 = 0
                    for t in range(len(x[i])):
                        sum2 = 0
                        sum3 = 0
                        for s in range(t, self.m):
                            sum2 += p_h[k][EMMMixPLAggregator.c(x[i], s)]
                        for s in range(t, self.m + 1):
                            sum3 += delta_i_j_s[i][j][s]
                        sum1 += z_h1[i][k] * (sum2 ** -1) * sum3
                    denom_sum += sum1
                p_h1[k][j] = omega_k_j / denom_sum
                normconst += p_h1[k][j]
            for j in range(self.m):
                p_h1[k][j] /= normconst
878446
def get_transitions_forward_steps(self, indexes, forward_steps, discount_factor):
        
        frame_batch_shape = (
                [indexes.shape[0], indexes.shape[1]]
                + list(self.state_buffer.shape[2:-1])
                + [self.state_buffer.shape[-1] * self.frame_history]
        )

        simple_batch_shape = [indexes.shape[0], indexes.shape[1]]

        past_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)
        future_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)

        reward_buffer = np.zeros(simple_batch_shape, dtype=np.float32)
        dones_buffer = np.zeros(simple_batch_shape, dtype=bool)

        for buffer_idx, frame_row in enumerate(indexes):
            for env_idx, frame_idx in enumerate(frame_row):
                past_frame, future_frame, reward, done = self.get_frame_with_future_forward_steps(
                    frame_idx, env_idx, forward_steps=forward_steps, discount_factor=discount_factor
                )

                past_frame_buffer[buffer_idx, env_idx] = past_frame
                future_frame_buffer[buffer_idx, env_idx] = future_frame
                reward_buffer[buffer_idx, env_idx] = reward
                dones_buffer[buffer_idx, env_idx] = done

        actions = take_along_axis(self.action_buffer, indexes)

        transition_tensors = {
            'observations': past_frame_buffer,
            'actions': actions,
            'rewards': reward_buffer,
            'observations_next': future_frame_buffer,
            'dones': dones_buffer.astype(np.float32),
        }

        for name in self.extra_data:
            transition_tensors[name] = take_along_axis(self.extra_data[name], indexes)

        return transition_tensors
332987
function step1b(token) {
    var r1 = getR1(token);

    if (!r1) {
        return token;
    }

    if (token.match(/(b|c|d|f|g|h|j|l|m|n|o|p|r|t|v|y|z)s$/)) {
        return token.slice(0, -1);
    }

    if (token.match(/([^aeiouyæåø]k)s$/)) {
        return token.slice(0, -1);
    }

    return token;
}
1065244
def forward_state(self, sequence, state=None):
        
        if state is None:
            state = self.zero_state(sequence.size(0))

        data = self.input_block(sequence)

        state_outputs = []

        # for layer_length, layer in zip(self.hidden_layers, self.recurrent_layers):
        for idx in range(len(self.recurrent_layers)):
            layer_length = self.recurrent_layers[idx].state_dim

            # Partition hidden state, for each layer we have layer_length of h state and layer_length of c state
            current_state = state[:, :, :layer_length]
            state = state[:, :, layer_length:]

            # Propagate through the GRU state
            data, new_h = self.recurrent_layers[idx](data, current_state)

            if self.dropout_layers:
                data = self.dropout_layers[idx](data)

            state_outputs.append(new_h)

        output_data = self.output_activation(self.output_layer(data))

        concatenated_hidden_output = torch.cat(state_outputs, dim=2)

        return output_data, concatenated_hidden_output
675434
def forward_backward(self, parameters):
        
        # If the features are sparse, we can use an optimization.
        # I'm not using scipy.sparse here because we want to avoid a scipy dependency and also scipy.sparse doesn't seem
        # to handle arrays of shape higher than 2.
        if isinstance(self.sparse_x, str) and self.sparse_x == 'uninitialized':
            if (self.x == 0).sum() * 1.0 / self.x.size > 0.6:
                self.sparse_x = self._construct_sparse_features(self.x)
            else:
                self.sparse_x = 'not sparse'

        I, J, K = self.x.shape
        if not isinstance(self.sparse_x, str):
            C = self.sparse_x[0].shape[2]
            S, _ = parameters.shape
            x_dot_parameters = np.zeros((I, J, S))
            sparse_multiply(x_dot_parameters, self.sparse_x[0], self.sparse_x[1], parameters.T, I, J, K, C, S)
        else:
            x_dot_parameters = np.dot(self.x, parameters.T)  # Pre-compute the dot product
        alpha = self._forward(x_dot_parameters)
        beta = self._backward(x_dot_parameters)
        classes_to_ints = {k: i for i, k in enumerate(set(self.states_to_classes.values()))}
        states_to_classes = np.array([classes_to_ints[self.states_to_classes[state]]
                                      for state in range(max(self.states_to_classes.keys()) + 1)], dtype='int64')
        if not isinstance(self.sparse_x, str):
            ll, deriv = gradient_sparse(alpha, beta, parameters, states_to_classes,
                                        self.sparse_x[0], self.sparse_x[1], classes_to_ints[self.y],
                                        I, J, self.sparse_x[0].shape[2])
        else:
            ll, deriv = gradient(alpha, beta, parameters, states_to_classes,
                                 self.x, classes_to_ints[self.y], I, J, K)
        return ll, deriv
167945
def sample_from_likelihood(self, n_timesteps=10):
        

        self.latent_state_sequences = lmap(
            lambda A: ltake(
                n_timesteps,
                iterate(
                    lambda s: pd.Series(A @ s.values, index=s.index), self.s0
                ),
            ),
            self.transition_matrix_collection,
        )

        self.observed_state_sequences = [
            [self.sample_observed_state(s) for s in latent_state_sequence]
            for latent_state_sequence in self.latent_state_sequences
        ]
85616
def inference(self, observed_arr):
        '''
        
        '''
        self.__pred_arr = self.__lstm_model.inference(observed_arr)
        return self.__pred_arr
779433
function next_state(mt) {
    var p = 0, _p = mt.state;
    var j;

    mt.left = N;
    mt.next = 0;

    for (j=N-M+1; --j; p++)
      _p[p] = _p[p+(M)] ^ TWIST(_p[p+(0)], _p[p+(1)]);

    for (j=M; --j; p++)
      _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[p+(1)]);

    _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[0]);
  }
555940
def hybrid_forward(self, F, x, sampled_values, label, w_all, b_all):
        """"""
        sampled_candidates, expected_count_sampled, expected_count_true = sampled_values
        # (num_sampled, in_unit)
        w_sampled = w_all.slice(begin=(0, 0), end=(self._num_sampled, None))
        w_true = w_all.slice(begin=(self._num_sampled, 0), end=(None, None))
        b_sampled = b_all.slice(begin=(0,), end=(self._num_sampled,))
        b_true = b_all.slice(begin=(self._num_sampled,), end=(None,))
        # true pred
        # (batch_size, 1)
        x = x.reshape((-1, self._in_unit))
        pred_true = (w_true * x).sum(axis=1) + b_true
        # samples pred
        # (batch_size, num_sampled)
        b_sampled = F.reshape(b_sampled, (-1,))
        pred_sampled = F.FullyConnected(x, weight=w_sampled, bias=b_sampled,
                                        num_hidden=self._num_sampled)

        # remove accidental hits
        if self._remove_accidental_hits:
            label_vec = F.reshape(label, (-1, 1))
            sample_vec = F.reshape(sampled_candidates, (1, -1))
            mask = F.broadcast_equal(label_vec, sample_vec) * -1e37
            pred_sampled = pred_sampled + mask

        # subtract log(q)
        expected_count_sampled = F.reshape(expected_count_sampled,
                                           shape=(1, self._num_sampled))
        expected_count_true = expected_count_true.reshape((-1,))
        pred_true = pred_true - F.log(expected_count_true)
        pred_true = pred_true.reshape((-1, 1))
        pred_sampled = F.broadcast_sub(pred_sampled, F.log(expected_count_sampled))

        # pred and new_labels
        # (batch_size, 1+num_sampled)
        pred = F.concat(pred_true, pred_sampled, dim=1)
        if self._sparse_label:
            new_label = F.zeros_like(label)
        else:
            label_vec = F.reshape(label, (-1, 1))
            new_label_true = F.ones_like(label_vec)
            new_label_sampled = F.zeros_like(pred_sampled)
            new_label = F.Concat(new_label_true, new_label_sampled, dim=1)
        return pred, new_label
749209
def keystroke_model():
    """"""
    model = Pohmm(n_hidden_states=2,
                  init_spread=2,
                  emissions=['lognormal', 'lognormal'],
                  smoothing='freq',
                  init_method='obs',
                  thresh=1)
    return model