Compute geometric average returns from a returns time serie
773042
def average_returns(ts, **kwargs):
    ''' '''
    average_type = kwargs.get('type', 'net')
    if average_type == 'net':
        relative = 0
    else:
        relative = -1  # gross
    #start = kwargs.get('start', ts.index[0])
    #end = kwargs.get('end', ts.index[len(ts.index) - 1])
    #delta = kwargs.get('delta', ts.index[1] - ts.index[0])
    period = kwargs.get('period', None)
    if isinstance(period, int):
        pass
    #else:
        #ts = reIndexDF(ts, start=start, end=end, delta=delta)
        #period = 1
    avg_ret = 1
    for idx in range(len(ts.index)):
        if idx % period == 0:
            avg_ret *= (1 + ts[idx] + relative)
    return avg_ret - 1
553588
def average(self, rho):
        """
        def marginal(f, rho):
            remaining = len(f.shape)
            if remaining == 0:
                return rho
            rho = sum([f[i]*rho[i] for i in range(rho.shape[0])])
            f = np.sum(f, 0)
            return marginal(f, rho)

        return marginal(self.distribution, rho)
1145255
def avg(self):
        """"""
        # XXX rename this method
        if len(self.values) > 0:
            return sum(self.values) / float(len(self.values))
        else:
            return None
457397
def moving_average(self, window, method=SIMPLE):
        ''''''
        if len(self.points) < window:
            raise ArithmeticError('Not enough points for moving average')
        numpy = LazyImport.numpy()
        if method == TimeSeries.SIMPLE:
            weights = numpy.ones(window) / float(window)
        ma_x = self.timestamps[window-1:]
        ma_y = numpy.convolve(self.values, weights)[window-1:-(window-1)].tolist()
        return TimeSeries(zip(ma_x, ma_y))
411508
def function(data, maxt=None):
    
    data = np.atleast_1d(data)
    assert len(np.shape(data)) == 1, \
        "The autocorrelation function can only by computed " \
        + "on a 1D time series."
    if maxt is None:
        maxt = len(data)
    result = np.zeros(maxt, dtype=float)
    _acor.function(np.array(data, dtype=float), result)
    return result / result[0]
266100
def sphere_average(dset,x,y,z,radius=1):
    ''''''
    return_list = []
    if isinstance(dset,basestring):
        dset = [dset]
    for d in dset:
        return_list += [float(a) for a in subprocess.check_output(['3dmaskave','-q','-dball',str(x),str(y),str(z),str(radius),d],stderr=subprocess.PIPE).split()]
    return return_list
763014
def Kdiag(self, X):
        """"""
        ret = np.empty(X.shape[0])
        ret[:] = self.variance
        return ret
42461
def compute():
    """"""
    if what == "numpy":
        y = eval(expr)
    else:
        y = ne.evaluate(expr)
    return len(y)
1052097
function p_t(df, t) {
        var c2, s, p, i;

        c2 = df / (df + t * t);
        s = Math.sqrt(1.0 - c2);

        if (t < 0.0) { s = -s; }

        p = 0.0;
        i = df % 2 + 2;
        while (i <= df) {
            p = p + s;
            s = s * ((i - 1) * c2 / i);
            i = i + 2;
        }

        if ((df & 1) !== 0) {
            return 0.5 + (p * Math.sqrt(c2) + Math.atan(t / Math.sqrt(df))) / Math.PI;
        }

        return (1.0 + p) / 2.0;
    }
815221
def __calculate_moving_average(self, date, row):
        
        cal_data = self.__serial_price(row)
        result = []
        for dummy in range(len(cal_data) - int(date) + 1):
            result.append(round(sum(cal_data[-date:]) / date, 2))
            cal_data.pop()
        result.reverse()
        cont = self.__cal_continue(result)
        return result, cont
281814
def triangular_moving_average(data, period):
    
    catch_errors.check_for_period_error(data, period)

    tma = sma(sma(data, period), period)
    return tma
954632
def hull_moving_average(data, period):
    
    catch_errors.check_for_period_error(data, period)
    hma = wma(
        2 * wma(data, int(period/2)) - wma(data, period), int(np.sqrt(period))
        )
    return hma
1373946
def _compute(self):
        """"""
        x_len = self._len
        y_len = self._order
        self._box.xmax = x_len
        self._box.ymax = y_len

        self._x_pos = [n / 2 for n in range(1, 2 * x_len, 2)]
        self._y_pos = [n / 2 for n in reversed(range(1, 2 * y_len, 2))]

        for j, serie in enumerate(self.series):
            serie.points = [(self._x_pos[i], self._y_pos[j])
                            for i in range(x_len)]
141349
def compute(self, x_arr, y_arr):
        '''
        
        '''
        y_arr += 1e-08
        return np.sum(x_arr * np.log(x_arr / y_arr), axis=-1)
278413
def _compute(self):
        
        for serie in self.series:
            serie.points, serie.outliers = \
                self._box_points(serie.values, self.box_mode)

        self._x_pos = [(i + .5) / self._order for i in range(self._order)]

        if self._min:
            self._box.ymin = min(self._min, self.zero)
        if self._max:
            self._box.ymax = max(self._max, self.zero)
314302
def _calculate_average(self, points):
        
        assert len(self.theta) == len(points), \
            "points has length %i, but should have length %i" % \
            (len(points), len(self.theta))
        new_point = {'x': 0, 'y': 0, 'time': 0}
        for key in new_point:
            new_point[key] = self.theta[0] * points[0][key] + \
                self.theta[1] * points[1][key] + \
                self.theta[2] * points[2][key]
        return new_point
166322
def compute_avg_adj_deg(G):
    
    """
    return np.sum(np.dot(G.A, G.A), axis=1) / (np.sum(G.A, axis=1) + 1.)