Ejemplo n.º 1
0
    def __init__(self, P, RHO, ma_weights, T=None, epsilon=None):        
        # If not provided, epsilon is an array of N(0, 1).
        if epsilon is None:
            epsilon = normal(0, 1, (T,))
        elif T is None:
            raise ValueError("You must provide T or epsilon.")
        self.epsilon = epsilon
        
        # If not provided, T will be len(epsilon)
        if T is None:
            T = shape(epsilon)[0]
        else:
            assert T == shape(epsilon)[0], "T and shape(epsilon) are incoherent."
        self.T = T

        # Generating the ARMA process
        Q = len(ma_weights)
        X = array(typecode=Float32, shape=(T,))
        for t in range(T):
            X[t] = epsilon[t]
        
            for k in range(1, min(t, Q)+1):
                alpha_k = ma_weights[k-1] # offset by 1 since lists start at 0 in Python 
                X[t] += alpha_k * epsilon[t-k]
        
            for p in range(1, min(t, P)+1):
                X[t] += RHO**p * X[t-p]
        
        self.x = X
        self.p = P
        self.q = Q
        self.rho = RHO 
        self.ma_weights = ma_weights
Ejemplo n.º 2
0
    def __init__(self, P, RHO, ma_weights, T=None, epsilon=None):
        # If not provided, epsilon is an array of N(0, 1).
        if epsilon is None:
            epsilon = normal(0, 1, (T, ))
        elif T is None:
            raise ValueError("You must provide T or epsilon.")
        self.epsilon = epsilon

        # If not provided, T will be len(epsilon)
        if T is None:
            T = shape(epsilon)[0]
        else:
            assert T == shape(
                epsilon)[0], "T and shape(epsilon) are incoherent."
        self.T = T

        # Generating the ARMA process
        Q = len(ma_weights)
        X = array(typecode=Float32, shape=(T, ))
        for t in range(T):
            X[t] = epsilon[t]

            for k in range(1, min(t, Q) + 1):
                alpha_k = ma_weights[
                    k - 1]  # offset by 1 since lists start at 0 in Python
                X[t] += alpha_k * epsilon[t - k]

            for p in range(1, min(t, P) + 1):
                X[t] += RHO**p * X[t - p]

        self.x = X
        self.p = P
        self.q = Q
        self.rho = RHO
        self.ma_weights = ma_weights
Ejemplo n.º 3
0
def _test_ols_regression(T, K, alpha=0.0, scale=10.0, plot=False):
    u = normal(0, 1, (T, ))
    beta = range(1, K + 1)

    X = scale * random((T, K))
    Y = alpha + mmult(X, beta) + u

    print "Beta:", beta

    print
    olsR = ols_regression(X, Y, False)
    print "Beta OLS (no intercept):", olsR[1], "(%.3f)" % matrix_distance(
        olsR[1], beta)

    print
    ols = ols_regression(X, Y, True)
    print "Beta OLS (with intercept %s):" % ols[0], ols[
        1], "(%.3f)" % matrix_distance(ols[1], beta)

    print
    if K == 1:
        X = X.ravel()
    scipy = stats.linregress(X, Y)
    print "Beta SciPy (with intercept %s):" % scipy[1],
    print scipy[0], "(%.3f)" % matrix_distance(array(scipy[0]), beta)

    if plot:
        import pylab
        pylab.hold(True)
        pylab.scatter(X, Y)
        xlims = pylab.xlim()
        pylab.plot([0, xlims[1]], [0, olsR[1][0] * xlims[1]],
                   label="No intercept")

        intercept = ols[0][0]
        line_end = intercept + ols[1][0] * xlims[1]
        pylab.plot([0, xlims[1]], [intercept, line_end],
                   label="With intercept")

        intercept = scipy[1]
        line_end = intercept + scipy[0] * xlims[1]
        pylab.plot([0, xlims[1]], [intercept, line_end], label="SciPy")

        pylab.legend(loc=0)
        pylab.show()

    return X, Y, olsR, ols, scipy
Ejemplo n.º 4
0
def _test_ols_regression(T, K, alpha=0.0, scale=10.0, plot=False):
    u = normal(0, 1, (T,))
    beta = range(1, K+1)
    
    X = scale * random((T, K))
    Y = alpha + mmult(X, beta) + u    

    
    print "Beta:", beta

    print 
    olsR = ols_regression(X, Y, False)
    print "Beta OLS (no intercept):", olsR[1], "(%.3f)"%matrix_distance(olsR[1],beta)

    print 
    ols = ols_regression(X, Y, True)
    print "Beta OLS (with intercept %s):"%ols[0], ols[1], "(%.3f)"%matrix_distance(ols[1],beta)

    print
    if K==1:
        X = X.ravel()
    scipy = stats.linregress(X, Y)    
    print "Beta SciPy (with intercept %s):"%scipy[1],
    print scipy[0], "(%.3f)"%matrix_distance(array(scipy[0]),beta)

    if plot:
        import pylab
        pylab.hold(True)
        pylab.scatter(X, Y)
        xlims = pylab.xlim()
        pylab.plot([0, xlims[1]], [0, olsR[1][0]*xlims[1]], label="No intercept")

        intercept = ols[0][0]
        line_end  = intercept + ols[1][0]*xlims[1]
        pylab.plot([0, xlims[1]], [intercept, line_end], label="With intercept")

        intercept = scipy[1]
        line_end  = intercept + scipy[0]*xlims[1]
        pylab.plot([0, xlims[1]], [intercept, line_end], label="SciPy")

        pylab.legend(loc=0)
        pylab.show()

    return X, Y, olsR, ols, scipy
Ejemplo n.º 5
0
 def __init__(self, T, n=1000):
     self.T = T
     self.n = n
     self.n_inverse      = 1.0/n
     self.increments     = normal(0, self.n_inverse, (n*T,))
     self.discretization = cumsum(self.increments)
Ejemplo n.º 6
0
 def __init__(self, T, n=1000):
     self.T = T
     self.n = n
     self.n_inverse = 1.0 / n
     self.increments = normal(0, self.n_inverse, (n * T, ))
     self.discretization = cumsum(self.increments)