Ejemplo n.º 1
0
def ds_hurst(list_time_series, list_columns):
    ds = pd.Series({
        list_columns[0]: compute_Hc(list_time_series[0])[0],
        list_columns[1]: compute_Hc(list_time_series[1])[0],
        list_columns[2]: compute_Hc(list_time_series[2])[0],
        list_columns[3]: compute_Hc(list_time_series[3])[0],
    })

    return ds
Ejemplo n.º 2
0
    def mostraStat(self, pointsToPlot, mediaView, desvioView, tempoTotalView,
                   hurstView):
        medidaTempo = [' segundos']
        medidaTaxa = [' bytes/segundo']
        # self.uiMainWindow.analiseEstatIOT.setVisible(True)

        mediaView.setText('{0:.2f} '.format(mean(pointsToPlot)) +
                          medidaTaxa[0])
        # if len( pointsToPlot ) > 1:
        #     self.uiMainWindow.desvioViewIOT.setText('{0:.2f} '.format(stdev(pointsToPlot)) + medidaTaxa[self.uiMainWindow.escalaIOT.currentIndex()])
        # else:
        desvioView.setText('{0:.2f}'.format(stdev(pointsToPlot)) +
                           medidaTaxa[0])
        tempoTotalView.setText(str(len(pointsToPlot)) + medidaTempo[0])
        if len(pointsToPlot) > 100:
            # diferences = [0]*len(points)
            # # diferences[0] = points[0]
            # for i in range(len(points)):
            #     diferences[i] = points[i] - points[i-1]
            try:
                H, c, _ = compute_Hc(pointsToPlot,
                                     kind='change',
                                     simplified=False)
                if H > 1:
                    hurstView.setText('H > 1 não é um valor válido')
                    return
                hurstView.setText('H={:.4f}, c={:.4f}'.format(H, c))
            except FloatingPointError as fpe:
                hurstView.setText(
                    'Unable to calculate Hurst value with current series')
                print('Erro no calculo do valor de hurst: ' + fpe.__str__())
        else:
            hurstView.setText('Sample com menos de 101 amostras')
Ejemplo n.º 3
0
    def mostraIOTStat(self, pointsToPlot):
        medidaTempo = [' segundos', ' minutos']
        medidaTaxa = [' bytes/segundo', ' bytes/minuto' ]
        self.uiMainWindow.analiseEstatIOT.setVisible(True)
        # numberOfPoints = len(pointsToPlot[0])

        # allPoints = [0]*(pointsToPlot[0][numberOfPoints - 1] + 1)
        # for i in pointsToPlot[0]:
        #     allPoints[int(i)] += pointsToPlot[1]

        self.uiMainWindow.mediaViewIOT.setText('{0:.2f} '.format(mean(pointsToPlot)) + medidaTaxa[self.uiMainWindow.escalaIOT.currentIndex()] )
        # if len( pointsToPlot ) > 1:
        #     self.uiMainWindow.desvioViewIOT.setText('{0:.2f} '.format(stdev(pointsToPlot)) + medidaTaxa[self.uiMainWindow.escalaIOT.currentIndex()])
        # else:
        self.uiMainWindow.desvioViewIOT.setText('{0:.2f}'.format(stdev(pointsToPlot)) + medidaTaxa[self.uiMainWindow.escalaIOT.currentIndex()] )
        self.uiMainWindow.tempoTotalIOT.setText( str(len(pointsToPlot)) + medidaTempo[self.uiMainWindow.escalaIOT.currentIndex()])
        if len(pointsToPlot) > 100:
            # diferences = [0]*len(points)
            # # diferences[0] = points[0]
            # for i in range(len(points)):
            #     diferences[i] = points[i] - points[i-1]
                
            H, c, data = compute_Hc(pointsToPlot, kind='change', simplified=False)
            self.uiMainWindow.hurstParametroIOT.setText('H={:.4f}, c={:.4f}'.format(H,c))
        else:
            self.uiMainWindow.hurstParametroIOT.setText('Sample com menos de 101 amostras')
Ejemplo n.º 4
0
    def mostraStreamODEstat(self, pointsToPlot):
        medidaTempo = [' milisegundos', ' segundos', ' minutos']
        medidaTaxa = [
            ' bits/100 milisegundos', ' bits/segundo', ' bits/minuto'
        ]
        self.uiMainWindow.analiseEstatStreamOD.setVisible(True)
        self.uiMainWindow.mediaViewStreamOD.setText(
            '{0:.2f} '.format(mean(pointsToPlot)) +
            medidaTaxa[self.uiMainWindow.escalaStreamOD.currentIndex()])
        if len(pointsToPlot) > 1:
            self.uiMainWindow.desvioViewStreamOD.setText(
                '{0:.2f} '.format(stdev(pointsToPlot)) +
                medidaTaxa[self.uiMainWindow.escalaStreamOD.currentIndex()])
        else:
            self.uiMainWindow.desvioViewStreamOD.setText(
                'Não há desvio com apenas 1 ponto')
        self.uiMainWindow.tempoTotalViewStreamOD.setText(
            str(len(pointsToPlot) - 1) +
            medidaTempo[self.uiMainWindow.escalaStreamOD.currentIndex()])

        if len(pointsToPlot) > 100:
            # diferences = [0]*len(pointsToPlot)
            # # diferences[0] = points[0]
            # for i in range(len(pointsToPlot)):
            #     diferences[i] = pointsToPlot[i] - pointsToPlot[i-1]

            H, c, data = compute_Hc(pointsToPlot,
                                    kind='change',
                                    simplified=False)
            self.uiMainWindow.hurstParameterStreamOD.setText(
                'H={:.4f}, c={:.4f}'.format(H, c))
        else:
            self.uiMainWindow.hurstParameterStreamOD.setText(
                'Sample com menos de 100 amostras')
Ejemplo n.º 5
0
    def get_features(self):
        hursts = []
        for window in self.windows:
            H, _, _ = compute_Hc(self.prices[-window:], kind='price', simplified=True)
            hursts.append(H)

        return np.array(hursts)
Ejemplo n.º 6
0
 def mostraStatWEB(self, points):
     medidaTempo = [' milisegundos', ' segundos', ' minutos']
     medidaTaxa = [
         ' byts/100 milisegundos', ' bytes/segundo', 'byts/minuto'
     ]
     self.uiMainWindow.analiseEstatWeb.setVisible(True)
     self.uiMainWindow.mediaViewWEB.setText(
         '{0:.2f} '.format(mean(points)) + medidaTaxa[1])
     if len(points) > 1:
         self.uiMainWindow.desvioViewWEB.setText(
             '{0:.2f} '.format(stdev(points)) + medidaTaxa[1])
     else:
         self.uiMainWindow.desvioViewWEB.setText(
             'Não há desvio para um único tempo')
     self.uiMainWindow.tempoTotalViewWEB.setText(
         str(len(points)) +
         medidaTempo[self.uiMainWindow.comboBox_2.currentIndex()])
     if len(points) > 100:
         # diferences = [0]*len(points)
         # # diferences[0] = points[0]
         # for i in range(len(points)):
         #     diferences[i] = points[i] - points[i-1]
         H, c, data = compute_Hc(points, kind='change', simplified=False)
         self.uiMainWindow.HurstViewWEB.setText('H={:.4f}, c={:.4f}'.format(
             H, c))
     else:
         self.uiMainWindow.HurstViewWEB.setText(
             'Sample com menos de 101 amostras')
Ejemplo n.º 7
0
def get_rolling_hurst(dataframe):
    period = 24 * 30
    df['H'] = 0
    for i in range(period, len(dataframe)):
        dataframe.H.iloc[i] = compute_Hc(dataframe.close.iloc[i - period:i -
                                                              1],
                                         kind='price')[0]
    return dataframe['H']
Ejemplo n.º 8
0
 def _calculate_hurst_exponent(self):
     hurst_values = {}
     for name, row in self.cointegration_result.iterrows():
         pair_ts = self.spreads[name].values
         H, _, _ = compute_Hc(pair_ts)
         hurst_values[name] = H
     hurst_values = pd.Series(hurst_values).rename("hurst")
     self.cointegration_result = self.cointegration_result.join(
         hurst_values)
Ejemplo n.º 9
0
def test_hurst():
    np.random.seed(42)
    random_increments = np.random.randn(99999)
    series = np.cumsum(
        random_increments)  # create a random walk from random increments

    # Evaluate Hurst equation
    H, c, data = compute_Hc(series)
    assert H < 0.6 and H > 0.4
Ejemplo n.º 10
0
def summary_stats_extra(x):
    """outputs additional summary statistics: skewness, kurtosis, Hurst"""

    try:
        H, c, data = compute_Hc(x, kind='price', simplified=True)
    except Exception as e:
        H = 0.25
        print(e)

    return {"skew": x.skew(), "kurt": x.kurt(), "hurst": H}
def simulate_a_seed(seed_params):
    """Simulates the model for a single seed and outputs the associated cost"""
    seed = seed_params[0]
    params = seed_params[1]

    obs = []
    # run model with parameters
    traders, central_bank, orderbook = init_objects(params, seed)
    traders, central_bank, orderbook = qe_model(traders,
                                                central_bank,
                                                orderbook,
                                                params,
                                                scenario='None',
                                                seed=seed)
    obs.append(orderbook)

    # store simulated stylized facts
    mc_prices, mc_returns, mc_autocorr_returns, mc_autocorr_abs_returns, mc_volatility, mc_volume, mc_fundamentals = organise_data(
        obs)

    autocor = []
    autocor_abs = []
    kurtosis = []
    hursts = []

    for col in mc_returns:
        autocor.append(autocorrelation_returns(mc_returns[col][1:], 25).mean())
        autocor_abs.append(
            autocorrelation_abs_returns(mc_returns[col][1:], 25).mean())
        kurtosis.append(mc_returns[col][1:].kurtosis())
        H, c, data = compute_Hc(mc_prices[col].dropna(),
                                kind='price',
                                simplified=True)
        hursts.append(H)

    stylized_facts_sim = np.array([
        np.mean(autocor),
        np.mean(autocor_abs),
        np.mean(kurtosis),
        np.mean(hursts)
    ])

    W = np.load(
        'distr_weighting_matrix.npy'
    )  # if this doesn't work, use: np.identity(len(stylized_facts_sim))

    empirical_moments = np.array(
        [0.05034916, 0.06925489, 4.16055312, 0.71581425])

    # calculate the cost
    cost = quadratic_loss_function(stylized_facts_sim, empirical_moments, W)
    return cost
Ejemplo n.º 12
0
def cal_hurst(feats):
    n = feats.size(0)
    # H, c, data = compute_Hc(series, kind='price', simplified=True)
    H = []
    for i in range(n):
        # feats是tensor张量,函数的参数是numpy矩阵
        # .detach().numpy() 将张量转化为numpy数组
        h, _, _ = compute_Hc(feats[i].detach().numpy(), kind='price')
        H.append(h)

    print(H)
    print(len(H))
    return H
def get_hurst_exp(signal: np.ndarray):
    """
    Extracts Hurst coefficients from the raw signal

    Parameters:
    signal: np.ndarray - input 1D signal

    Returns:
    features: np.ndarray - extracted stat. features
    feature_names: list - names of extracted features
    """

    H, c, _ = compute_Hc(signal, kind='random_walk', simplified=True)
    features = np.array([H, c])
    feature_names = ['hurst_H', 'hurst_c']

    return features, feature_names
def FD(price_data):
    #iterate in the list, set i as a index
    for i in range(len(price_data)):
        # check whether there is enough data (100 float numbers) to process
        # the later calculation
        if len(price_data) - i >= 100:
            # store the list with 100 element
            eurusd_price_2 = price_data[i:i + 100]
            # use compute_Hc function to get the hurst
            # set kind='price' and simplified=True
            hurst = compute_Hc(eurusd_price_2, kind='price', simplified=True)
            # caculate Fractal Dimension use 2 minus the first element in hurst
            FD = 2 - hurst[0]
            # store the Fractal Dimension into a list
            fd_list.append(FD)
    # return the list of Fractal Dimension
    return fd_list
def simulate_a_seed(seed_params):
    """Simulates the model for a single seed and outputs the associated cost"""
    seed = seed_params[0]
    params = seed_params[1]

    obs = []
    # run model with parameters
    traders, orderbook = init_objects(params, seed)
    traders, orderbook = exuberance_inequality_model(traders, orderbook,
                                                     params, seed)
    obs.append(orderbook)

    # store simulated stylized facts
    mc_prices, mc_returns, mc_autocorr_returns, mc_autocorr_abs_returns, mc_volatility, mc_volume, mc_fundamentals = organise_data(
        obs, burn_in_period=BURN_IN)

    rets_autocor = []
    rets_abs_autocors = []
    kurts = []
    hursts = []
    for col in mc_returns:
        rets_autocor.append(autocorrelation_returns(mc_returns[col][1:], 25))
        rets_abs_autocors.append(
            autocorrelation_returns(mc_returns[col][1:].abs(), 25))
        kurts.append(mc_returns[col][1:].kurtosis())
        hursts.append(
            compute_Hc(mc_prices[col][1:], kind='price', simplified=True)[0])

    stylized_facts_sim = np.array([
        np.mean(rets_autocor),
        np.mean(rets_abs_autocors),
        np.mean(kurts),
        np.mean(hursts)
    ])

    W = np.load(
        'distr_weighting_matrix.npy'
    )  # if this doesn't work, use: np.identity(len(stylized_facts_sim))

    empirical_moments = np.load('emp_moments.npy')

    # calculate the cost
    cost = quadratic_loss_function(stylized_facts_sim, empirical_moments, W)
    return cost
Ejemplo n.º 16
0
    def hurst(self, ts, plot=False):
        '''
        ts: input a list you want to find the hurst value of
        plot: default=False, plot the lags on a log scale, this value must be a boolean
        '''
        H, c, val = compute_Hc(ts)
        axes = plt.subplots()[1]

        if plot == True:
            axes.plot(val[0], c * val[0]**H, color="blue")
            axes.scatter(val[0], val[1], color="red")
            axes.set_xscale('log')
            axes.set_yscale('log')
            axes.set_xlabel('Time interval')
            axes.set_ylabel('R/S ratio')
            axes.grid(True)
            plt.show
            return H
        elif plot == False:
            return H
        else:
            raise ValueError('Plot must be set to True or False')
Ejemplo n.º 17
0
	def hurst_matrix(self):
		"""Create a matrix that prints Hurst exponents."""
		n = len(symbols)
		matrix = np.ones((n, n))
		np.seterr(divide='ignore')
		for i in range(n):
			for j in range(n):
				if symbols[i] == symbols[j]:
					matrix[i, j] = None
				else:
					S1 = Data(symbols[i], self.start, self.end).logrets().dropna()
					S2 = Data(symbols[j], self.start, self.end).logrets().dropna()
					S1 = S1[1:]
					S2 = S2[1:]
					ratio = S1/S2
					spread = S1 - (np.mean(ratio))*S2
					result = compute_Hc(spread)
					hurst = result[0]
					matrix[i, j] = hurst 
		seaborn.heatmap(matrix, xticklabels=symbols, yticklabels=symbols, cmap='RdYlGn_r', vmin=0.4, vmax=0.6, center=0.5, annot=True)
		plt.title('Matrix of Hurst Exponents for Given Stocks')
		plt.show()
Ejemplo n.º 18
0
def metrics(prices_series):
    tickers = list(prices_series)
    data = {}
    m = {
        "mu":{},
        "std":{},
        "skew":{},
        "kurt":{},
        "ADF":{},
        "H":{}
    }
    for t in tickers:
        stats = {}
        dat = {}
        d = prices_series[t].values
        mask = ~np.isnan(d)
        logr = np.log((d[1:] / d[:-1]))
        logr = logr[~np.isnan(logr)]
        logr = logr[~np.isinf(logr)]
        dat["data"] = logr.tolist()
        m["mu"][t] = np.mean(logr[-30:])
        m["std"][t] = np.std(logr[-30:])
        m["skew"][t] = skew(logr[-30:])
        m["kurt"][t] = kurtosis(logr[-30:])
        ac = pacf(logr, nlags=min(int(10 * np.log10(len(logr))), len(logr) // 2 - 1) )
        dat["autocorrelation"] = ac
        fuller = adfuller(logr)
        m["ADF"][t] = fuller[0]
        # calculate hurst exponential
        try:
            H, c, _ = compute_Hc(d[mask], kind='price', simplified=True)
        except Exception as e:
            H, c = np.nan, np.nan
        m["H"][t] = H
        data[t] = dat
    return m, data
Ejemplo n.º 19
0
    w['High'].apply(find_high_date), w['Close'].apply(get_close_date)
])
orderedBar = np.sort(pricesBar.values)
#orderedBar

#Date	price	barType
data = pd.DataFrame(orderedBar.tolist(), columns=['Date', 'price', 'barType'])
data['Sym'] = s
data.head()

data.set_index('Date')['price'].iplot(title='{}, ({};{})'.format(
    data['Sym'].unique(), df.index[0], df.index[-1]))
hurst(data['price'])

# Evaluate Hurst equation
H, c, hdata = compute_Hc(data['price'], kind='price', simplified=True)

# Plot
f, ax = plt.subplots()
ax.plot(hdata[0], c * hdata[0]**H, color="deepskyblue")
ax.scatter(hdata[0], hdata[1], color="purple")
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Time interval')
ax.set_ylabel('R/S ratio')
ax.grid(True)
plt.show()

print("H={:.4f}, c={:.4f}".format(H, c))

# In[ ]:
Ejemplo n.º 20
0
def plot_agr_load():
    series_total = 86400 * [0]
    series_stream = 86400 * [0]
    series_web = 86400 * [0]
    series_voip = 86400 * [0]
    # Streaming
    for i in range(40):
        try:
            with open('./Charges/stream_charge{}.json'.format(i)) as json_file:
                data = json.load(json_file)
                init_time = data['init_time']
                message_size_list = data['packet_size']
                time_between_message = data['time_between_packets']
                for k, j in zip(message_size_list, time_between_message):
                    try:
                        series_stream[int(init_time + j)] += k
                        init_time += j
                    except IndexError:
                        print('Diferenca entre tamanho de listas')

        except IOError:
            print('No file named "./Charges/stream_charge{}.json"'.format(i))

    H, c, data = compute_Hc(series_stream, kind='change', simplified=False)
    print("Streaming de vídeo => H={:.4f}, c={:.4f}".format(H, c))

    plt.subplot(2, 2, 1)
    plt.plot(series_stream, color='darkred')
    plt.title('Streaming de vídeo sob demanda')
    plt.xlabel('Tempo (segundos)')
    plt.ylabel('Trafego agregado (bytes)')

    # WEB
    for i in range(85):
        try:
            with open('./Charges/web_charge{}.json'.format(i)) as json_file:
                data = json.load(json_file)
                init_time = data['init_time']
                message_size_list = data['packet_size']
                time_between_message = data['time_between_packets']
                for k, j in zip(message_size_list, time_between_message):
                    try:
                        if init_time + j > 86400:
                            break
                        series_web[int(init_time + j)] += k
                        init_time += j
                    except IndexError:
                        print('Diferenca entre tamanho de listas')

        except IOError:
            print('No file named "./Charges/stream_charge{}.json"'.format(i))

    plt.subplot(2, 2, 2)
    plt.plot(series_web, color='darkred')
    plt.title('WEB')
    plt.xlabel('Tempo (segundos)')
    plt.ylabel('Trafego agregado (bytes)')

    H, c, data = compute_Hc(series_web, kind='change', simplified=False)
    print("WEB => H={:.4f}, c={:.4f}".format(H, c))

    # VoIP
    for i in range(554):
        try:
            with open('./Charges/voip_charge{}.json'.format(i)) as json_file:
                data = json.load(json_file)
                init_time = data['init_time']
                message_size = data['packet_size']
                time_between_message = data['time_between_packets']
                for k in time_between_message:
                    try:
                        if init_time + k > 86400:
                            break
                        series_voip[int(init_time + k)] += message_size
                        init_time += k
                    except IndexError:
                        print('Diferenca entre tamanho de listas')

        except IOError:
            print('No file named "./Charges/stream_charge{}.json"'.format(i))

    H, c, data = compute_Hc(series_voip, kind='change', simplified=False)
    print("VoIP => H={:.4f}, c={:.4f}".format(H, c))

    plt.subplot(2, 2, 3)
    plt.plot(series_voip, color='darkred')
    plt.title('VoIP')
    plt.xlabel('Tempo (segundos)')
    plt.ylabel('Trafego agregado (bytes)')

    z = 0
    for i, j, k in zip(series_stream, series_web, series_voip):
        series_total[z] += i + j + k
        z += 1

    H, c, data = compute_Hc(series_total, kind='change', simplified=False)
    print("Total => H={:.4f}, c={:.4f}".format(H, c))

    plt.subplot(2, 2, 4)
    plt.plot(series_total, color='darkred')
    plt.title('Tráfego Agregado')
    plt.xlabel('Tempo (segundos)')
    plt.ylabel('Trafego agregado (bytes)')
    plt.subplots_adjust(hspace=0.3)

    plt.show()
Ejemplo n.º 21
0
df = p[[4]]
df['changes'] = df.pct_change()
changevec = df['changes'].dropna() + 1
changevec = changevec.dropna()

random_changes = np.array(changevec)
series = np.cumprod(random_changes)

H_l = []
c_l = []
data_l = []
mse_l = []
series_splits = np.array_split(series, 100)

for currseries in series_splits:
    H, c, data = compute_Hc(currseries, kind='price', simplified=True)
    H_l.append(H)
    c_l.append(c)
    data_l.append(data)
    mse_l.append(ar(currseries - 1))  # Get back to simple returns around 0

# Evaluate Hurst equation for complete data set
H, c, data = compute_Hc(series, kind='price', simplified=True)

# Use Autoregressive Model to predict price for periods of differing H.
# Would expect high H periods (e.g. > 0.5) to have a smaller Mean Squared Error than other periods
# Then use some hypothesis test on the mean difference.

# Use t-test for mse_l small when H_l large
cutoff_int = 0.5
idx = np.where(np.array(H_l) > cutoff_int)
Ejemplo n.º 22
0
def distr_model_performance(input_parameters):
    """
    Simple function calibrate uncertain model parameters
    :param input_parameters: list of input parameters
    :return: cost
    """
    # set fixed parameters of integer variables & variable names
    n_runs = 1
    variable_names = [
        'std_noise', "w_random", "strat_share_chartists", "base_risk_aversion",
        "fundamentalist_horizon_multiplier", "mutation_intensity",
        "average_learning_ability"
    ]

    # update params
    uncertain_parameters = dict(zip(variable_names, input_parameters))
    params = {
        "fundamental_value": 166,
        "trader_sample_size": 22,
        "n_traders": 1000,
        "ticks": 600,
        "std_fundamental": 0.053,
        "init_assets": 740,
        'spread_max': 0.004,
        'money_multiplier': 2.2,
        "horizon": 200,
        "std_noise": 0.049,
        "w_random": 0.08,
        "strat_share_chartists": 0.08,
        "base_risk_aversion": 1.051,
        "fundamentalist_horizon_multiplier": 3.8,
        "trades_per_tick": 1,
        "mutation_intensity": 0.0477,
        "average_learning_ability": 0.05,
        "bond_mean_reversion": 0.0,
        'cb_pf_range': 0.05,
        "qe_perc_size": 0.16,
        "cb_size": 0.02,
        "qe_asset_index": 0,
        "qe_start": 2,
        "qe_end": 598
    }
    params.update(uncertain_parameters)

    empirical_moments = np.array(
        [0.05034916, 0.06925489, 4.16055312, 0.71581425])

    traders = []
    obs = []
    # run model with parameters
    for seed in range(n_runs):
        traders, central_bank, orderbook = init_objects(params, seed)
        traders, central_bank, orderbook = qe_model(traders,
                                                    central_bank,
                                                    orderbook,
                                                    params,
                                                    scenario='None',
                                                    seed=seed)
        traders.append(traders)
        obs.append(orderbook)

    # store simulated stylized facts
    mc_prices, mc_returns, mc_autocorr_returns, mc_autocorr_abs_returns, mc_volatility, mc_volume, mc_fundamentals = organise_data(
        obs)

    autocor = []
    autocor_abs = []
    kurtosis = []
    hursts = []

    for col in mc_returns:
        autocor.append(autocorrelation_returns(mc_returns[col][1:], 25).mean())
        autocor_abs.append(
            autocorrelation_returns(mc_returns[col][1:], 25).abs().mean())
        kurtosis.append(mc_returns[col][1:].kurtosis())
        H, c, data = compute_Hc(mc_prices[col].dropna(),
                                kind='price',
                                simplified=True)
        hursts.append(H)

    stylized_facts_sim = np.array([
        np.mean(autocor),
        np.mean(autocor_abs),
        np.mean(kurtosis),
        np.mean(hursts)
    ])

    W = np.load(
        'distr_weighting_matrix.npy'
    )  #if this doesn't work, use: np.identity(len(stylized_facts_sim))

    # calculate the cost
    cost = quadratic_loss_function(stylized_facts_sim, empirical_moments, W)
    return cost
Ejemplo n.º 23
0
df = pd.concat(vwaplist)

# get the dates in the right format and set it as index
df['datetime'] = pd.to_datetime(df['date'])
df.set_index('datetime')

# calculate the total period vwap
df['vwap'] = (df['volume'] *
              (df['high'] + df['low']) / 2).cumsum() / df['volume'].cumsum()

# calculate the SAR
df['sar'] = ta.SAR(df, acceleration=0.01, maximum=0.02)

# calculate the Hurst Exponent and print it
H, c, data = compute_Hc(df['close'], kind='price')
alt_Hurst = hurst(df['close'].values)
print("Hurst Exponent:")
print(H)
print(alt_Hurst)


# calculate the rolling hurst Exponent for each week
def get_rolling_hurst(dataframe):
    period = 24 * 30
    df['H'] = 0
    for i in range(period, len(dataframe)):
        dataframe.H.iloc[i] = compute_Hc(dataframe.close.iloc[i - period:i -
                                                              1],
                                         kind='price')[0]
    return dataframe['H']
Ejemplo n.º 24
0
def hurst_coeff(x):
    h, C, data = hurst.compute_Hc(x)
    result = fracdiff.fracdiff(x, 0, 0)
    _hurst_coeff = result.rx2('d')[0] + 0.5
    return _hurst_coeff
Ejemplo n.º 25
0
def calculate_hurst(dfseries):
    """if Hurst < 0.5 favors mean reversion, if Hurst >0.5 favors trend, Hurst ~ 0.5 means a random walk"""
    hurst, constant, vals = compute_Hc(dfseries, kind='price')
    print('Hurst Exponent value is : ', hurst)
    return hurst
Ejemplo n.º 26
0
def calculate_hurst_exponent(traffic):
    hurst_exponent, _, _ = compute_Hc(traffic)
    return hurst_exponent
Ejemplo n.º 27
0
import datetime
from numpy import *
from pylab import plot, show
start = datetime.datetime(2019, 1, 1)
end = datetime.datetime(2020, 5, 1)
spy = web.DataReader("SPY", 'yahoo', start, end)
closes = spy['Adj Close'][:]

# Use random_walk() function or generate a random walk series manually:
# series = random_walk(99999, cumprod=True)
np.random.seed(42)
random_changes = 1. + np.random.randn(99999) / 1000.
series = np.cumprod(random_changes)  # create a random walk from random changes

# Evaluate Hurst equation
H, c, data = compute_Hc(closes, kind='price', simplified=True)

# Plot
f, ax = plt.subplots()
ax.plot(data[0], c*data[0]**H, color="deepskyblue")
ax.scatter(data[0], data[1], color="purple")
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Time interval')
ax.set_ylabel('R/S ratio')
ax.grid(True)
plt.show()

print("H={:.4f}, c={:.4f}".format(H,c))

Ejemplo n.º 28
0
for index, symbol in enumerate(btc_pairlist):
    close_list[index] = get_all_binance(symbol, '1h', save = False)['close']
    length_list[index] = len(close_list[index])

# create a dataframe with all the closes so we can get a correlation matrix
close_df = pd.concat(close_list, axis=1, sort=False)
close_df.columns = btc_pairlist
close_df = close_df.apply( pd.to_numeric, errors='coerce' )

#calculate the correlation matrix
corr_matrix = close_df.corr()
print(corr_matrix)

corr_filename = 'correlation_matrix.csv'
corr_matrix.to_csv(corr_filename)

# calculate the hurst exponent for each symbol and store it in our list
for index, elem in enumerate(close_list):
    closes = pd.to_numeric(close_list[index]).values
    hurst_list[index]= compute_Hc(closes,kind='price')[0]
    alt_hurst_list[index] = hurst(closes)

# create the hurst exponent dataframe
df = pd.DataFrame(list(zip(btc_pairlist,hurst_list,alt_hurst_list,length_list)),
              columns=['ticker','hurst_exponent','althurstlst','number_of_hours'])

print(df)
filename = 'hurst_data.csv'

df.to_csv(filename)
Ejemplo n.º 29
0
def split_hurst(df):
    df['split_hurst'] = compute_Hc(df['close'], kind='price')[0]
    df['alt_split_hurst'] = hurst(df['close'].values)
    return df
Ejemplo n.º 30
0
 def h(l):
     s = compute_Hc(l)
     return s[0]