예제 #1
0
class Autocorrelation(Metric):
    def __init__(self, lag=1, window=30):
        self.lag = lag
        self.window = window
        self.mr = MinutelyReturns()
        super().__init__()

    def compute(self, df):
        df = pd.Series(self.mr.compute(df))
        df = df.rolling(self.window,
                        center=True).apply(lambda x: x.autocorr(lag=self.lag),
                                           raw=False)
        return df.dropna().tolist()

    def visualize(self, simulated, real, plot_real=True):
        min_sim = min([len(x) for x in simulated.values()])
        min_size = min(min_sim, len(real))
        random.shuffle(real)
        real = real[:min_size]
        for k, v in simulated.items():
            random.shuffle(v)
            simulated[k] = v[:min_size]
        self.hist(simulated,
                  real,
                  title="Autocorrelation (lag={}, window={})".format(
                      self.lag, self.window),
                  xlabel="Correlation coefficient",
                  log=False,
                  plot_real=plot_real)
예제 #2
0
class VolatilityClustering(Metric):
    def __init__(self, lags=10, mode="abs"):
        self.lags = lags
        modes = ["abs", "square"]
        if mode not in modes:
            raise ValueError("`mode` must be one of " + str(modes) + ".")
        self.mode = mode
        self.mr = MinutelyReturns()
        super().__init__()

    def compute(self, df):
        df = pd.Series(self.mr.compute(df))
        if self.mode == "abs":
            df = abs(df)
        elif self.mode == "square":
            df = df**2
        return [[df.autocorr(lag) for lag in range(1, self.lags + 1)]]

    def visualize(self, simulated, real, plot_real=True):
        self.line(simulated,
                  real,
                  "Volatility Clustering/Long Range Dependence",
                  "Lag",
                  "Correlation coefficient",
                  plot_real=plot_real)
class ReturnsVolatilityCorrelation(Metric):

    def __init__(self, intervals=4):
        self.mr = MinutelyReturns()

    def compute(self, df):
        returns = np.array(self.mr.compute(df))
        volatility = abs(returns)
        return [np.corrcoef(returns, volatility)[0,1]]

    def visualize(self, simulated):
        self.hist(simulated, title="Returns/Volatility Correlation", xlabel="Correlation coefficient", bins=50)
예제 #4
0
class VolumeVolatilityCorrelation(Metric):
    def __init__(self, intervals=4):
        self.mr = MinutelyReturns()

    def compute(self, df):
        volatility = abs(np.array(self.mr.compute(df)))
        volume = df["volume"].iloc[1:].values
        return [np.corrcoef(volume, volatility)[0, 1]]

    def visualize(self, simulated):
        self.hist(simulated,
                  title="Volume/Volatility Correlation",
                  xlabel="Correlation coefficient")
예제 #5
0
class AggregationNormality(Metric):
    def __init__(self):
        self.mr = MinutelyReturns()

    def compute(self, df):
        df = df[["close"]].resample("10T").last()
        return self.mr.compute(df)

    def visualize(self, simulated):
        self.hist(simulated,
                  "Aggregation Normality (10 minutes)",
                  "Log Returns",
                  log=True,
                  clip=.05)
예제 #6
0
class Kurtosis(Metric):

    def __init__(self, intervals=4):
        self.intervals = intervals
        self.mr = MinutelyReturns()

    def compute(self, df):
        ks = []
        for i in range(1,self.intervals+1):
            temp = df[["close"]].resample("{}T".format(i)).last()
            rets = self.mr.compute(temp)
            ks.append(kurtosis(rets))
        return [ks]

    def visualize(self, simulated):
        self.line(simulated, title="Kurtosis", xlabel="Time scale (min)", ylabel="Average kurtosis", logy=True)