Return standard deviations
485759
def _get_stddevs(self, C, stddev_types, num_sites, mag, c1_rrup,
                     log_phi_ss, mean_phi_ss):
        
        phi_ss = _compute_phi_ss(C, mag, c1_rrup, log_phi_ss, mean_phi_ss)

        stddevs = []
        for stddev_type in stddev_types:
            assert stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES

            if stddev_type == const.StdDev.TOTAL:
                stddevs.append(np.sqrt(
                               C['tau'] * C['tau'] +
                               phi_ss * phi_ss) +
                               np.zeros(num_sites))

            elif stddev_type == const.StdDev.INTRA_EVENT:
                stddevs.append(phi_ss + np.zeros(num_sites))

            elif stddev_type == const.StdDev.INTER_EVENT:
                stddevs.append(C['tau'] + np.zeros(num_sites))
        return stddevs
678750
def calc_deviation(values, average):
    
    size = len(values)
    if size < 2:
        return 0
    calc_sum = 0.0

    for number in range(0, size):
        calc_sum += math.sqrt((values[number] - average) ** 2)
    return math.sqrt((1.0 / (size - 1)) * (calc_sum / size))
189035
def _setup_standard_deviations(self, fle):
        
        # Load in total standard deviation
        self.stddevs = {}
        self.stddevs[const.StdDev.TOTAL] = hdf_arrays_to_dict(fle["Total"])
        # If other standard deviations
        self.DEFINED_FOR_STANDARD_DEVIATION_TYPES = set(
            self.DEFINED_FOR_STANDARD_DEVIATION_TYPES)
        for stddev_type in [const.StdDev.INTER_EVENT,
                            const.StdDev.INTRA_EVENT]:
            if stddev_type in fle:
                self.stddevs[stddev_type] = hdf_arrays_to_dict(
                    fle[stddev_type])
                self.DEFINED_FOR_STANDARD_DEVIATION_TYPES.add(stddev_type)
874304
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        
        R = (dists.rrup)
        M = rup.mag
        # get constants
        Ssr = self.get_Ssr_term(sites.vs30)
        Shr = self.get_Shr_term(sites.vs30)
        rake = rup.rake
        F = self.get_fault_term(rake)

        # compute mean
        mean = -3.512 + (0.904 * M) - (1.328 * np.log(np.sqrt(R**2
               + (0.149 * np.exp(0.647 * M))**2))) \
               + (1.125 - 0.112 * np.log(R) - 0.0957 * M) * F \
               + (0.440 - 0.171 * np.log(R)) * Ssr \
               + (0.405 - 0.222 * np.log(R)) * Shr
        stddevs = self.get_stddevs(mean, stddev_types)
        return mean, stddevs
938476
def get_deviations(self):
        
        bt = False
        if not self.istransformed:
            bt = True
            self._transform()
        mean_vec = self.mean()

        df = self.loc[:,:].copy()
        for col in df.columns:
            df.loc[:,col] -= mean_vec[col]
        if bt:
            self._back_transform()
        return type(self).from_dataframe(pst=self.pst,df=df)
960558
def _get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        
        # distances
        distsl = copy.copy(dists)
        distsl.rjb, distsl.rrup = \
            utils.get_equivalent_distances_east(rup.mag, dists.repi)
        #
        # Pezeshk et al. 2011 - Rrup
        mean1, stds1 = super().get_mean_and_stddevs(sites, rup, distsl, imt,
                                                    stddev_types)
        mean1 = self.apply_correction_to_BC(mean1, imt, distsl)
        #
        # Atkinson 2008 - Rjb
        gmpe = Atkinson2008prime()
        mean2, stds2 = gmpe.get_mean_and_stddevs(sites, rup, distsl, imt,
                                                 stddev_types)
        #
        # Silva et al. 2002 - Rjb
        gmpe = SilvaEtAl2002SingleCornerSaturation()
        mean4, stds4 = gmpe.get_mean_and_stddevs(sites, rup, distsl, imt,
                                                 stddev_types)
        mean4 = self.apply_correction_to_BC(mean4, imt, distsl)
        #
        # Silva et al. 2002 - Rjb
        gmpe = SilvaEtAl2002DoubleCornerSaturation()
        mean5, stds5 = gmpe.get_mean_and_stddevs(sites, rup, distsl, imt,
                                                 stddev_types)
        mean5 = self.apply_correction_to_BC(mean5, imt, distsl)
        #
        # distances
        distsl.rjb, distsl.rrup = \
            utils.get_equivalent_distances_east(rup.mag, dists.repi, ab06=True)
        #
        # Atkinson and Boore 2006 - Rrup
        gmpe = AtkinsonBoore2006Modified2011()
        mean3, stds3 = gmpe.get_mean_and_stddevs(sites, rup, distsl, imt,
                                                 stddev_types)
        # Computing adjusted mean and stds
        mean_adj = mean1*0.2 + mean2*0.2 + mean3*0.2 + mean4*0.2 + mean5*0.2

        # Note that in this case we do not apply a triangular smoothing on
        # distance as explained at page 996 of Atkinson and Adams (2013)
        # for the calculation of the standard deviation
        stds_adj = np.log(np.exp(stds1)*0.2 + np.exp(stds2)*0.2 +
                          np.exp(stds3)*0.2 + np.exp(stds4)*0.2 +
                          np.exp(stds5)*0.2)
        #
        return mean_adj, stds_adj
271528
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        
        mean, stddevs = super().get_mean_and_stddevs(sites, rup, dists, imt,
                                                     stddev_types)
        delta = self._get_delta(imt, rup.mag)
        return mean-delta, stddevs
1312455
def _sd_of_runs(stats, mean, key='runs'):
    

    num_runs = len(stats[key])
    first = stats[key][0]

    standard_deviation = {}
    for stat_key in first:
        # Skip non numberic attributes
        if isinstance(first[stat_key], numbers.Number):
            standard_deviation[stat_key] = math.sqrt(
                sum((run[stat_key] - mean[stat_key])**2
                    for run in stats[key]) / float(num_runs))

    return standard_deviation
575790
def variance(self) -> Optional[float]:  #, ddof: int = 0) -> float:
        
        # TODO: Add DOF
        # http://stats.stackexchange.com/questions/6534/how-do-i-calculate-a-weighted-standard-deviation-in-excel
        if self._stats:
            if self.total > 0:
                return (self._stats["sum2"] - self._stats["sum"] ** 2 / self.total) / self.total
            else:
                return np.nan
        else:
            return None
380648
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        

        dists_mod = copy.deepcopy(dists)
        dists_mod.rrup[dists.rrup <= 5.] = 5.

        return super().get_mean_and_stddevs(
            sites, rup, dists_mod, imt, stddev_types)
1085939
def _get_stddevs(self, C, stddev_types, stddev_shape):
        
        stddevs = []
        for stddev_type in stddev_types:
            assert stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES
            if stddev_type == const.StdDev.TOTAL:
                stddevs.append(C["sigtot"] + np.zeros(stddev_shape))
            elif stddev_type == const.StdDev.INTRA_EVENT:
                stddevs.append(C['sig2'] + np.zeros(stddev_shape))
            elif stddev_type == const.StdDev.INTER_EVENT:
                stddevs.append(C['sig1'] + np.zeros(stddev_shape))
        return stddevs
1194566
def _get_stddevs(self, C, stddev_types, num_sites):
        
        assert all(stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES
                   for stddev_type in stddev_types)

        # Using a frequency independent value of sigma as recommended
        # in the caption of Table 2 of Atkinson (2010)
        stddevs = [0.26/np.log10(np.e) + np.zeros(num_sites)]

        return stddevs
24797
def get_stddevs(self, mag, imt, stddev_types, num_sites):
        
        tau = self._get_tau(imt, mag)
        phi = self._get_phi(imt, mag)
        sigma = np.sqrt(tau ** 2. + phi ** 2.)
        stddevs = []
        for stddev_type in stddev_types:
            assert stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES
            if stddev_type == const.StdDev.TOTAL:
                stddevs.append(sigma + np.zeros(num_sites))
            elif stddev_type == const.StdDev.INTRA_EVENT:
                stddevs.append(phi + np.zeros(num_sites))
            elif stddev_type == const.StdDev.INTER_EVENT:
                stddevs.append(tau + np.zeros(num_sites))
        return stddevs
685535
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        

        sites.vs30 = 700 * np.ones(len(sites.vs30))

        mean, stddevs = super().get_mean_and_stddevs(
            sites, rup, dists, imt, stddev_types)

        C = CauzziFaccioli2008SWISS01.COEFFS

        tau_ss = 'tau'
        log_phi_ss = np.log(10)
        mean, stddevs = _apply_adjustments(
            C, self.COEFFS_FS_ROCK[imt], tau_ss,
            mean, stddevs, sites, rup, dists.rhypo, imt, stddev_types,
            log_phi_ss)

        return mean, np.log(10 ** np.array(stddevs))
2145
def moments(self):
        """"""
        moment1 = statstools.calc_mean_time(self.delays, self.coefs)
        moment2 = statstools.calc_mean_time_deviation(
            self.delays, self.coefs, moment1)
        return numpy.array([moment1, moment2])
458361
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        
        C = self.COEFFS[imt]

        imean = (self._get_magnitude_term(C, rup.mag) +
                 self._get_distance_term(C, dists.rhypo, rup.mag))
        # Convert mean from cm/s and cm/s/s
        if imt.name in "SA PGA":
            mean = np.log((10.0 ** (imean - 2.0)) / g)
        else:
            mean = np.log(10.0 ** imean)
        stddevs = self._get_stddevs(C, len(dists.rhypo), stddev_types)
        return mean, stddevs
669975
def get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types):
        
        assert all(stddev_type in self.DEFINED_FOR_STANDARD_DEVIATION_TYPES
                   for stddev_type in stddev_types)

        mean = np.zeros_like(sites.vs30)
        stddevs = [np.zeros_like(sites.vs30) for _ in stddev_types]

        idx_rock = sites.vs30 >= self.ROCK_VS30
        idx_soil = sites.vs30 < self.ROCK_VS30

        if idx_rock.any():
            C = self.COEFFS_ROCK[imt]
            self._compute_mean(C, self.CONSTS['A1_rock'],
                               self.CONSTS['A2_rock'], self.CONSTS['A3_rock'],
                               self.CONSTS['A4_rock'], self.CONSTS['A5_rock'],
                               self.CONSTS['A6_rock'], rup.mag, rup.hypo_depth,
                               dists.rrup, mean, idx_rock)
            self._compute_std(C, rup.mag, stddevs, idx_rock)

            if imt == SA(period=4.0, damping=5.0):
                mean = mean / 0.399

        if idx_soil.any():
            C = self.COEFFS_SOIL[imt]
            self._compute_mean(C, self.CONSTS['A1_soil'],
                               self.CONSTS['A2_soil'], self.CONSTS['A3_soil'],
                               self.CONSTS['A4_soil'], self.CONSTS['A5_soil'],
                               self.CONSTS['A6_soil'], rup.mag, rup.hypo_depth,
                               dists.rrup, mean, idx_soil)
            self._compute_std(C, rup.mag, stddevs, idx_soil)

        return mean, stddevs