Source

bubble-economy / stats.py

Full commit
#!/usr/bin/env python
# encoding: utf-8
"""
stats.py

do some stats on varying permutations on stacked matrices of velocity or
distance vectors. Traderset-specific statistics are here; the general/abstract
machineries of analysing vector MI are in mi.py

Copyright (c) 2010 Dan MacKinlay. All rights reserved.
"""

import numpy as np
from numpy import linalg
import scipy as sp

def nonparametric_bootstrap(data,
        statistic,
        iterations=200,
        return_dist_vector=False):
    """given a data vector or observation row vectors, and a statistic to
    analyse said vectors, return the statistic and variance estimate. This
    assumes a well-behaved, symmetric bootstrap distribution and no
    inter-sample dependencies."""
    est = statistic(data)
    indices = np.zeros(data.shape[0], dtype=int)
    bootstrap_ests = np.zeros(iterations)
    for i in xrange(iterations):
         indices[:] = np.random.randint(0, data.shape[0], size=data.shape[0])
         bootstrap_ests[i] = statistic(data[indices])
    variance = bootstrap_ests.var()
    ret = (est, variance)
    if return_dist_vector:
        rest += (bootstrap_ests,)
    return ret