Helper method for `extract_anomalies` method.
    Calculates the epsilon (threshold) for anomalies.
194328
def compute_threshold(smoothed_errors, error_buffer, sd_limit=12.0):
    
    mu = np.mean(smoothed_errors)
    sigma = np.std(smoothed_errors)

    max_epsilon = 0
    sd_threshold = sd_limit

    # The treshold is determined dynamically by testing multiple Zs.
    # z is drawn from an ordered set of positive values representing the
    # number of standard deviations above mean(smoothed_errors)

    # here we iterate in increments of 0.5 on the range that the NASA paper found to be good
    for z in np.arange(2.5, sd_limit, 0.5):
        epsilon = mu + (sigma * z)
        below_epsilon, below_indices, above_epsilon = [], [], []

        for i in range(len(smoothed_errors)):
            e = smoothed_errors[i]
            if e < epsilon:
                # save to compute delta mean and delta std
                # these are important for epsilon calculation
                below_epsilon.append(e)
                below_indices.append(i)

            if e > epsilon:
                # above_epsilon values are anomalies
                for j in range(0, error_buffer):
                    if (i + j) not in above_epsilon and (i + j) < len(smoothed_errors):
                        above_epsilon.append(i + j)

                    if (i - j) not in above_epsilon and (i - j) >= 0:
                        above_epsilon.append(i - j)

        if len(above_epsilon) == 0:
            continue

        # generate sequences
        above_epsilon = sorted(list(set(above_epsilon)))
        groups = [list(group) for group in mit.consecutive_groups(above_epsilon)]
        above_sequences = [(g[0], g[-1]) for g in groups if not g[0] == g[-1]]

        mean_perc_decrease = (mu - np.mean(below_epsilon)) / mu
        sd_perc_decrease = (sigma - np.std(below_epsilon)) / sigma
        epsilon = (mean_perc_decrease + sd_perc_decrease) /\
                  (len(above_sequences)**2 + len(above_epsilon))

        # update the largest epsilon we've seen so far
        if epsilon > max_epsilon:
            sd_threshold = z
            max_epsilon = epsilon

    # sd_threshold can be multiplied by sigma to get epsilon
    return max_epsilon, sd_threshold
1229846
def likelihood(self, samples):
        
        ret = np.ones(NUM_OF_INSTANCE)
        for i in range(NUM_OF_INSTANCE):
            for j in range(1, self.point_num + 1):
                ret[i] *= self.normal_distribution(j, samples[i])
        return ret
623726
def threshold(self):
        """"""
        if self._threshold is None:
            self._threshold = {
                'error': self.timer_warning,
                'warning': self.timer_ok,
            }[self.timer_fail]
        return self._threshold
589672
def calcinds(data, threshold, ignoret=None):
    

    inds = []
    for i in range(len(data['time'])):
        snr = data['snrs'][i]
        time = data['time'][i]
        if (threshold >= 0 and snr > threshold):
            if ignoret:
                incl = [t0 for (t0, t1) in ignoret if np.round(time).astype(int) in range(t0,t1)]
                logger.debug('{} {} {} {}'.format(np.round(time).astype(int), t0, t1, incl))
                if not incl:
                    inds.append(i)
            else:
                inds.append(i)
        elif threshold < 0 and snr < threshold:
            if ignoret:
                incl = [t0 for (t0, t1) in ignoret if np.round(time).astype(int) in range(t0,t1)]
                logger.debug('{} {} {} {}'.format(np.round(time).astype(int), t0, t1, incl))
                if not incl:
                    inds.append(i)
            else:
                inds.append(i)

    return inds
1019451
def _parse(self, threshold):
        
        match = re.search(r'^(@?)((~|\d*):)?(\d*)$', threshold)

        if not match:
            raise ValueError('Error parsing Threshold: {0}'.format(threshold))

        if match.group(1) == '@':
            self._inclusive = True

        if match.group(3) == '~':
            self._min = float('-inf')
        elif match.group(3):
            self._min = float(match.group(3))
        else:
            self._min = float(0)

        if match.group(4):
            self._max = float(match.group(4))
        else:
            self._max = float('inf')

        if self._max < self._min:
            raise ValueError('max must be superior to min')
286045
private double loglikelihoodAnomalous(DBIDs anomalousObjs) {
    return anomalousObjs.isEmpty() ? 0 : anomalousObjs.size() * -FastMath.log(anomalousObjs.size());
  }
103444
def _analyze_root_causes(self):
        
        causes = {}
        for a in self.anomalies:
            try:
                causes[a] = self.correlations[a][0]
            except IndexError:
                raise exceptions.InvalidDataFormat('luminol.luminol: dict correlations contains empty list.')
        self.causes = causes
1230136
def _sigma_ee_nonrel(self, gam, eps):
        
        s0 = 4 * r0 ** 2 * alpha / (15 * eps)
        x = 4 * eps / (gam ** 2 - 1)
        sigma_nonrel = s0 * self._F(x, gam)
        sigma_nonrel[np.where(eps >= 0.25 * (gam ** 2 - 1.0))] = 0.0
        sigma_nonrel[np.where(gam * np.ones_like(eps) < 1.0)] = 0.0
        return sigma_nonrel / mec2_unit
175371
def _std(self):
    
    variance = tf.cond(
        self._count > 1,
        lambda: self._var_sum / tf.cast(self._count - 1, tf.float32),
        lambda: tf.ones_like(self._var_sum) * float('nan'))
    # The epsilon corrects for small negative variance values caused by
    # the algorithm. It was empirically chosen to work with all environments
    # tested.
    return tf.sqrt(variance + 1e-4)
1291539
def eps_greedy(self, params):
        '''
        
        '''

        if params and type(params) == dict:
            eps = params.get('epsilon')
        else:
            eps = 0.1

        r = np.random.rand()

        if r < eps:
            return np.random.choice(list(set(range(len(self.wins))) -
                                    {self.max_mean()}))
        else:
            return self.max_mean()
1090675
def estimate_threshold(in1, edge_excl=0, int_excl=0):
    

    out1 = np.empty([in1.shape[0]])
    mid = in1.shape[1]/2

    if (edge_excl!=0) | (int_excl!=0):

        if edge_excl!=0:
            mask = np.zeros([in1.shape[1], in1.shape[2]])
            mask[edge_excl:-edge_excl, edge_excl:-edge_excl] = 1
        else:
            mask = np.ones([in1.shape[1], in1.shape[2]])

        if int_excl!=0:
            mask[mid-int_excl:mid+int_excl, mid-int_excl:mid+int_excl] = 0

    else:
        mask = np.ones([in1.shape[1], in1.shape[2]])

    for i in range(in1.shape[0]):
        out1[i] = np.median(np.abs(in1[i,mask==1]))/0.6745

    return out1
292303
private Metric predictAnomalies(Map<Long, Double> metricData) {
        Metric predictions = new Metric(getResultScopeName(), getResultMetricName());
        Map<Long, Double> predictionDatapoints = new HashMap<>();

        if (variance == 0.0) {
            /**
             * If variance is 0, there are no anomalies.
             * Also, using 0 for variance would cause divide by zero operations
             * in Gaussian anomaly formulas. This condition avoids such operations.
             */
            for (Entry<Long, Double> entry : metricData.entrySet()) {
                Long timestamp = entry.getKey();
                predictionDatapoints.put(timestamp, 0.0);
            }
        } else {
            for (Entry<Long, Double> entry : metricData.entrySet()) {
                Long timestamp = entry.getKey();
                double value = entry.getValue();
                try {
                    double anomalyScore = calculateAnomalyScore(value);
                    predictionDatapoints.put(timestamp, anomalyScore);
                } catch (ArithmeticException e) {
                    continue;
                }
            }
        }

        predictions.setDatapoints(predictionDatapoints);
        return predictions;
    }
902787
def get_below_threshold(umi_quals, quality_encoding,  quality_filter_threshold):
    ''''''
    umi_quals = [x - RANGES[quality_encoding][0] for x in map(ord, umi_quals)]
    below_threshold = [x < quality_filter_threshold for x in umi_quals]
    return below_threshold
467714
def _outlierDetection_threaded(inputs):
    '''
    
    '''
    [samples_idx, samples_x, samples_y_aggregation] = inputs
    sys.stderr.write("[%s] DEBUG: Evaluating %dth of %d samples\n"\
                        % (os.path.basename(__file__), samples_idx + 1, len(samples_x)))
    outlier = None

    # Create a diagnostic regression model which removes the sample that we want to evaluate
    diagnostic_regressor_gp = gp_create_model.create_model(\
                                    samples_x[0:samples_idx] + samples_x[samples_idx + 1:],\
                                    samples_y_aggregation[0:samples_idx] + samples_y_aggregation[samples_idx + 1:])
    mu, sigma = gp_prediction.predict(samples_x[samples_idx], diagnostic_regressor_gp['model'])

    # 2.33 is the z-score for 98% confidence level
    if abs(samples_y_aggregation[samples_idx] - mu) > (2.33 * sigma):
        outlier = {"samples_idx": samples_idx,
                   "expected_mu": mu,
                   "expected_sigma": sigma,
                   "difference": abs(samples_y_aggregation[samples_idx] - mu) - (2.33 * sigma)}
    return outlier
272212
private void calculateError(){
        int totalErrorCount = 0;
        int totalRedundancy = 0;
        int trueCoverage = 0;
        int totalCoverage = 0;

        int numNoise = 0;
        double errorNoise = 0;
        double errorNoiseMax = 0;

        double errorMissed = 0;
        double errorMissedMax = 0;

        double errorMisplaced = 0;
        double errorMisplacedMax = 0;

        double totalError = 0.0;
        double totalErrorMax = 0.0;

        /** mainly iterate over all points and find the right error value for the point.
         *  within the same run calculate various other stuff like coverage etc...
         */
        for (int p = 0; p < numPoints; p++) {
            CMMPoint cmdp = gtAnalysis.getPoint(p);
            double weight = cmdp.weight();
            //noise counter
            if(cmdp.isNoise()){
                numNoise++;
                //this is always 1
                errorNoiseMax+=cmdp.connectivity*weight;
            }
            else{
                errorMissedMax+=cmdp.connectivity*weight;
                errorMisplacedMax+=cmdp.connectivity*weight;
            }
            //sum up maxError as the individual errors are the quality weighted between 0-1
            totalErrorMax+=cmdp.connectivity*weight;


            double err = 0;
            int coverage = 0;

            //check every FCluster
            for (int c = 0; c < numFClusters; c++) {
                //contained in cluster c?
                if(pointInclusionProbFC[p][c] >= pointInclusionProbThreshold){
                    coverage++;

                    if(!cmdp.isNoise()){
                        //PLACED CORRECTLY
                        if(matchMap[c] == cmdp.workclass()){
                        }
                        //MISPLACED
                        else{
                            double errvalue = misplacedError(cmdp, c);
                            if(errvalue > err)
                                err = errvalue;
                        }
                    }
                    else{
                        //NOISE
                        double errvalue = noiseError(cmdp, c);
                        if(errvalue > err) err = errvalue;
                    }
                }
            }
            //not in any cluster
            if(coverage == 0){
                //MISSED
                if(!cmdp.isNoise()){
                    err = missedError(cmdp,true);
                    errorMissed+= weight*err;
                }
                //NOISE
                else{
                }
            }
            else{
                if(!cmdp.isNoise()){
                    errorMisplaced+= err*weight;
                }
                else{
                    errorNoise+= err*weight;
                }
            }

            /* processing of other evaluation values */
            totalError+= err*weight;
            if(err!=0)totalErrorCount++;
            if(coverage>0) totalCoverage++;  //points covered by clustering (incl. noise)
            if(coverage>0 && !cmdp.isNoise()) trueCoverage++; //points covered by clustering, don't count noise
            if(coverage>1) totalRedundancy++; //include noise

            cmdp.p.setMeasureValue("CMM",err);
            cmdp.p.setMeasureValue("Redundancy", coverage);
        }

        addValue("CMM", (totalErrorMax!=0)?1-totalError/totalErrorMax:1);
        addValue("CMM Missed", (errorMissedMax!=0)?1-errorMissed/errorMissedMax:1);
        addValue("CMM Misplaced", (errorMisplacedMax!=0)?1-errorMisplaced/errorMisplacedMax:1);
        addValue("CMM Noise", (errorNoiseMax!=0)?1-errorNoise/errorNoiseMax:1);
        addValue("CMM Basic", 1-((double)totalErrorCount/(double)numPoints));

        if(debug){
            System.out.println("-------------");
        }
    }
183294
function getThreshold(threshold) {
			if (min > threshold) {
				threshold = min;
			} else if (max < threshold) {
				threshold = max;
			}

			return translate(threshold, 0, 1);
		}
1158227
def m_probs(self):
        """"""
        log_m = self.kernel.feature_log_prob_[self._match_class_pos()]

        return self._prob_inverse_transform(numpy.exp(log_m))