Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
Code style change part 1 (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
shssf authored and Vyacheslav-Smirnov committed Aug 7, 2019
1 parent 1a30e4f commit 6dae0b3
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 224 deletions.
5 changes: 3 additions & 2 deletions examples/accel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ def accel_infer(n):
df = pd.DataFrame({'X': X, 'Y': Y, 'Z': Z})

g = 9.81
df['accel'] = np.sqrt(df.X**2 + df.Y**2 + (df.Z-g)**2)
df['accel'] = np.sqrt(df.X**2 + df.Y**2 + (df.Z - g)**2)
threshold = df.accel.mean() + 5 * df.accel.std()
df['is_brake'] = (df.rolling(10)['accel'].mean() > threshold)

df.is_brake.fillna(False, inplace=True)
checksum = df.is_brake.sum()
t2 = time.time()
print("exec time:", t2-t1)
print("exec time:", t2 - t1)
return checksum


n = 10**8
accel_infer(n)
4 changes: 3 additions & 1 deletion examples/d4p_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import hpat
import numpy as np


@hpat.jit(nopython=True)
def kmeans(N, D, nClusters, maxit):
a = np.random.ranf((N,D)) # doesn't make much sense, but ok for now
a = np.random.ranf((N, D)) # doesn't make much sense, but ok for now
kmi = daal4py.kmeans_init(nClusters, method='plusPlusDense')
km = daal4py.kmeans(nClusters, maxit)
kmr = km.compute(a, kmi.compute(a).centroids)
return (kmr.centroids, kmr.assignments, kmr.objectiveFunction, kmr.goalFunction, kmr.nIterations)


print(kmeans(10000, 20, 2, 30))

hpat.distribution_report()
9 changes: 6 additions & 3 deletions examples/d4p_linreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import hpat
import numpy as np


@hpat.jit
def lr_predict(N, D, model):
data = np.random.ranf((N/2,D))
data = np.random.ranf((N / 2, D))
return daal4py.linear_regression_prediction().compute(data, model)


@hpat.jit
def lr_train(N, D):
data = np.random.ranf((N,D))
gt = np.random.ranf((N,2))
data = np.random.ranf((N, D))
gt = np.random.ranf((N, 2))
return daal4py.linear_regression_training(interceptFlag=True, method='qrDense').compute(data, gt)


t_res = lr_train(1000, 10)
p_res = lr_predict(1000, 10, t_res.model)

Expand Down
8 changes: 5 additions & 3 deletions examples/hiframes_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import numpy as np
import hpat


@hpat.jit
def concat_df(n):
df1 = pd.DataFrame({'key1': np.arange(n), 'A': np.arange(n)+1.0})
df2 = pd.DataFrame({'key2': n-np.arange(n), 'A': n+np.arange(n)+1.0})
df1 = pd.DataFrame({'key1': np.arange(n), 'A': np.arange(n) + 1.0})
df2 = pd.DataFrame({'key2': n - np.arange(n), 'A': n + np.arange(n) + 1.0})
df3 = pd.concat([df1, df2])
return df3.key2.sum()


n = 10
print(concat_df(n))
#hpat.distribution_report()
# hpat.distribution_report()
4 changes: 3 additions & 1 deletion examples/hiframes_cumsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import numpy as np
import hpat


@hpat.jit
def cumsum_df(n):
df = pd.DataFrame({'A': np.arange(n)+1.0, 'B': np.random.ranf(n)})
df = pd.DataFrame({'A': np.arange(n) + 1.0, 'B': np.random.ranf(n)})
Ac = df.A.cumsum()
return Ac.sum()


n = 10
print(cumsum_df(n))
hpat.distribution_report()
2 changes: 2 additions & 0 deletions examples/hiframes_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import numpy as np
import hpat


@hpat.jit
def filter_df(n):
df = pd.DataFrame({'A': np.random.ranf(n), 'B': np.random.ranf(n)})
df1 = df[df.A > .5]
return np.sum(df1.B)


n = 10
print(filter_df(n))
6 changes: 4 additions & 2 deletions examples/hiframes_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import numpy as np
import hpat


@hpat.jit
def merge_df(n):
df1 = pd.DataFrame({'key1': np.arange(n), 'A': np.arange(n)+1.0})
df2 = pd.DataFrame({'key2': n-np.arange(n), 'B': n+np.arange(n)+1.0})
df1 = pd.DataFrame({'key1': np.arange(n), 'A': np.arange(n) + 1.0})
df2 = pd.DataFrame({'key2': n - np.arange(n), 'B': n + np.arange(n) + 1.0})
df3 = pd.merge(df1, df2, left_on='key1', right_on='key2')
return df3.B.sum()


n = 10
print(merge_df(n))
17 changes: 9 additions & 8 deletions examples/hiframes_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import hpat


@hpat.jit(pivots={'pt': ['small', 'large']})
def df_pivot(df):
pt = df.pivot_table(index='A', columns='C', values='D', aggfunc='sum')
Expand All @@ -11,12 +12,12 @@ def df_pivot(df):


df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
"bar", "bar", "bar", "bar"],
"B": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"C": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"D": [1, 2, 2, 6, 3, 4, 5, 6, 9]})
"bar", "bar", "bar", "bar"],
"B": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"C": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"D": [1, 2, 2, 6, 3, 4, 5, 6, 9]})

df_pivot(df)
df_pivot(df)
6 changes: 5 additions & 1 deletion examples/hiframes_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
import numpy as np
import hpat


@hpat.jit
def rolling_df1(n):
df = pd.DataFrame({'A': np.arange(n), 'B': np.random.ranf(n)})
Ac = df.A.rolling(5).sum()
return Ac.sum()


@hpat.jit
def rolling_df2(n):
df = pd.DataFrame({'A': np.arange(n), 'B': np.random.ranf(n)})
df['moving average'] = df.A.rolling(window=5, center=True).mean()
return df['moving average'].sum()


@hpat.jit
def rolling_df3(n):
df = pd.DataFrame({'A': np.arange(n), 'B': np.random.ranf(n)})
Ac = df.A.rolling(3, center=True).apply(lambda a: a[0]+2*a[1]+a[2])
Ac = df.A.rolling(3, center=True).apply(lambda a: a[0] + 2 * a[1] + a[2])
return Ac.sum()


n = 10
print("sum left window:")
print(rolling_df1(n))
Expand Down
7 changes: 5 additions & 2 deletions examples/hiframes_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
import numpy as np
import hpat


@hpat.jit
def shift_df1(n):
df = pd.DataFrame({'A': np.arange(n)+1.0, 'B': np.random.ranf(n)})
df = pd.DataFrame({'A': np.arange(n) + 1.0, 'B': np.random.ranf(n)})
Ac = df.A.shift(1)
return Ac.sum()


@hpat.jit
def shift_df2(n):
df = pd.DataFrame({'A': np.arange(n)+1.0, 'B': np.random.ranf(n)})
df = pd.DataFrame({'A': np.arange(n) + 1.0, 'B': np.random.ranf(n)})
Ac = df.A.pct_change()
return Ac


n = 10
print("shift 1:")
print(shift_df1(n))
Expand Down
2 changes: 2 additions & 0 deletions examples/hiframes_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import numpy as np
import hpat


@hpat.jit
def df_sort(df):
df2 = df.sort_values('A')
print(df2.A.values)
print(df2.B.values)


n = 11
df = pd.DataFrame({'A': np.random.ranf(n), 'B': np.arange(n), 'C': np.random.ranf(n)})
# computation is sequential since df is passed in
Expand Down
38 changes: 20 additions & 18 deletions examples/intraday_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
# adopted from:
# http://www.pythonforfinance.net/2017/02/20/intraday-stock-mean-reversion-trading-backtest-in-python/


@hpat.jit(locals={'s_open': hpat.float64[:], 's_high': hpat.float64[:],
's_low': hpat.float64[:], 's_close': hpat.float64[:],
's_vol': hpat.float64[:]})
's_low': hpat.float64[:], 's_close': hpat.float64[:],
's_vol': hpat.float64[:]})
def intraday_mean_revert():
file_name = "stock_data_all_google.hdf5"
f = h5py.File(file_name, "r")
Expand All @@ -23,35 +24,35 @@ def intraday_mean_revert():
for i in prange(nsyms):
symbol = sym_list[i]

s_open = f[symbol+'/Open'][:]
s_high = f[symbol+'/High'][:]
s_low = f[symbol+'/Low'][:]
s_close = f[symbol+'/Close'][:]
s_vol = f[symbol+'/Volume'][:]
s_open = f[symbol + '/Open'][:]
s_high = f[symbol + '/High'][:]
s_low = f[symbol + '/Low'][:]
s_close = f[symbol + '/Close'][:]
s_vol = f[symbol + '/Volume'][:]
df = pd.DataFrame({'Open': s_open, 'High': s_high, 'Low': s_low,
'Close': s_close, 'Volume': s_vol,})
'Close': s_close, 'Volume': s_vol, })

#create column to hold our 90 day rolling standard deviation
# create column to hold our 90 day rolling standard deviation
df['Stdev'] = df['Close'].rolling(window=90).std()

#create a column to hold our 20 day moving average
# create a column to hold our 20 day moving average
df['Moving Average'] = df['Close'].rolling(window=20).mean()

#create a column which holds a TRUE value if the gap down from previous day's low to next
#day's open is larger than the 90 day rolling standard deviation
# create a column which holds a TRUE value if the gap down from previous day's low to next
# day's open is larger than the 90 day rolling standard deviation
df['Criteria1'] = (df['Open'] - df['Low'].shift(1)) < -df['Stdev']

#create a column which holds a TRUE value if the opening price of the stock is above the 20 day moving average
# create a column which holds a TRUE value if the opening price of the stock is above the 20 day moving average
df['Criteria2'] = df['Open'] > df['Moving Average']

#create a column that holds a TRUE value if both above criteria are also TRUE
# create a column that holds a TRUE value if both above criteria are also TRUE
df['BUY'] = df['Criteria1'] & df['Criteria2']

#calculate daily % return series for stock
# calculate daily % return series for stock
df['Pct Change'] = (df['Close'] - df['Open']) / df['Open']

#create a strategy return series by using the daily stock returns where the trade criteria above are met
df['Rets'] = df['Pct Change'][df['BUY'] == True]
# create a strategy return series by using the daily stock returns where the trade criteria above are met
df['Rets'] = df['Pct Change'][df['BUY']]

n_days = len(df['Rets'])
res = np.zeros(max_num_days)
Expand All @@ -61,6 +62,7 @@ def intraday_mean_revert():

f.close()
print(all_res.mean())
print("execution time:", time.time()-t1)
print("execution time:", time.time() - t1)


intraday_mean_revert()
26 changes: 14 additions & 12 deletions examples/intraday_mean_rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# adopted from:
# http://www.pythonforfinance.net/2017/02/20/intraday-stock-mean-reversion-trading-backtest-in-python/


@hpat.jit
def intraday_mean_revert():
nsyms = 1000
Expand All @@ -15,38 +16,39 @@ def intraday_mean_revert():

t1 = time.time()
for i in prange(nsyms):
#np.random.seed(0)
# np.random.seed(0)
s_open = 20 * np.random.randn(max_num_days)
s_low = 18 * np.random.randn(max_num_days)
s_close = 19 * np.random.randn(max_num_days)
df = pd.DataFrame({'Open': s_open, 'Low': s_low,
'Close': s_close})
'Close': s_close})

#create column to hold our 90 day rolling standard deviation
# create column to hold our 90 day rolling standard deviation
df['Stdev'] = df['Close'].rolling(window=90).std()

#create a column to hold our 20 day moving average
# create a column to hold our 20 day moving average
df['Moving Average'] = df['Close'].rolling(window=20).mean()

#create a column which holds a TRUE value if the gap down from previous day's low to next
#day's open is larger than the 90 day rolling standard deviation
# create a column which holds a TRUE value if the gap down from previous day's low to next
# day's open is larger than the 90 day rolling standard deviation
df['Criteria1'] = (df['Open'] - df['Low'].shift(1)) < -df['Stdev']

#create a column which holds a TRUE value if the opening price of the stock is above the 20 day moving average
# create a column which holds a TRUE value if the opening price of the stock is above the 20 day moving average
df['Criteria2'] = df['Open'] > df['Moving Average']

#create a column that holds a TRUE value if both above criteria are also TRUE
# create a column that holds a TRUE value if both above criteria are also TRUE
df['BUY'] = df['Criteria1'] & df['Criteria2']

#calculate daily % return series for stock
# calculate daily % return series for stock
df['Pct Change'] = (df['Close'] - df['Open']) / df['Open']

#create a strategy return series by using the daily stock returns where the trade criteria above are met
df['Rets'] = df['Pct Change'][df['BUY'] == True]
# create a strategy return series by using the daily stock returns where the trade criteria above are met
df['Rets'] = df['Pct Change'][df['BUY']]

all_res += df['Rets'].mean()

print(all_res)
print("execution time:", time.time()-t1)
print("execution time:", time.time() - t1)


intraday_mean_revert()
Loading

0 comments on commit 6dae0b3

Please sign in to comment.