Granger Test (Python) Error message - TypeError: unsupported operand type(s) for -: 'str' and 'int'
Question: I am trying to run a granger causality test on two currency pairs but I seem
to get this error message in Shell whenever I try and test it. Can anyone
please advise?

I am very new to programming and need this to run an analysis for my project.
In shell, I am putting -

import ats15 ats15.grangertest('EURUSD', 'EURGBP', 8)

What is going wrong? I have copied the script below.

Thanks in advance.

## Heading ##def grangertest(Y,X,maxlag):

    
    
    """
    Performs a Granger causality test on variables (vectors) Y and X.
    The null hypothese is: Does X cause Y ?
    Returned value: pvalue, F, df1, df2
    """
    # Create linear model involving Y lags only.
    n = len(Y)
    if n != len(X):
        raise ValueError, "grangertest: incompatible Y,X vectors"
    M = [ [0] * maxlag for i in range(n-maxlag)]
    for i in range(maxlag, n):
        for j in range(1, maxlag+1):
            M[i-maxlag][j-1] = Y[i-j]
    
    fit = ols(M, Y[maxlag:])
    RSSr = fit.RSS
    
    # Create linear model including X lags.
    for i in range(maxlag, n):
        xlagged = [X[i-j] for j in range(1, maxlag+1)]
        M[i-maxlag].extend(xlagged)
    fit = ols(M, Y[maxlag:])
    RSSu = fit.RSS
    df1 = maxlag
    df2 = n - 2 * maxlag - 1
    F = ((RSSr - RSSu)/df1)/(RSSu/df2)
    pvalue = 1.0 - stats.f.cdf(F,df1,df2)
    return pvalue, F, df1, df2, RSSr, RSSu
    


Answer: You didn't post the full traceback, but this error message:

    
    
    TypeError: unsupported operand type(s) for -: 'str' and 'int'
    

means what it says. There's an operand `-` \-- the subtraction operator -- and
it doesn't know how to handle subtracting an integer from a string. Why would
strings be involved? Well, you're calling the function with:

    
    
    ats15.grangertest('EURUSD', 'EURGBP', 8)
    

and so you're giving `grangertest` two strings and an integer. But it seems
like `grangertest` expects

    
    
    def grangertest(Y,X,maxlag):
    

two sequences (lists, arrays, whatever) of _numbers_ to use as Y and X, not
strings. If `EURUSD` and `EURGBP` are names you've given to lists beforehand,
then you don't need the quotes:

    
    
    ats15.grangertest(EURUSD, EURGBP, 8)
    

but if not, then you should pass `grangertest` the lists under whatever name
you've called them.

