Skip to content

Commit 0a3bbaf

Browse files
committed
Improve Readability
Signed-off-by: Ryan Russell <[email protected]>
1 parent fe63b48 commit 0a3bbaf

27 files changed

+53
-53
lines changed

backtest/hist_downloader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def run(args):
207207
pickle.dump(dict_all_stocks, f, pickle.HIGHEST_PROTOCOL)
208208
print(f'Fundamentals {field} downloaded')
209209

210-
# This is adatped from https://towardsdatascience.com/sentiment-analysis-of-stocks-from-financial-news-using-python-82ebdcefb638
210+
# This is adapted from https://towardsdatascience.com/sentiment-analysis-of-stocks-from-financial-news-using-python-82ebdcefb638
211211
if args.sentiment:
212212
print('sentiment downloading .............')
213213
finwiz_url = 'https://finviz.com/quote.ashx?t='
@@ -246,7 +246,7 @@ def run(args):
246246
# read the text from each tr tag into text
247247
# get text from a only
248248
text = x.a.get_text()
249-
# splite text in the td tag into a list
249+
# split text in the td tag into a list
250250
date_scrape = x.td.text.split()
251251
# if the length of 'date_scrape' is 1, load 'time' as the only element
252252
if len(date_scrape) == 1:

backtest/ma_cross.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
To use pyfolio 0.9.2
1616
1. line 893 in pyfolio/timeseries.py from: valley = np.argmin(underwater) # end of the period to valley = underwater.idxmin() # end of the period
1717
2. line 133 and 137 in pyfolio/round_trips.py: groupby uses list not tuple. ['block_dir', 'block_time']
18-
3. line 77 in pyfolio/roud_trips.py: doesn't support agg(stats_dict) and rename_axis ==> rename
18+
3. line 77 in pyfolio/round_trips.py: doesn't support agg(stats_dict) and rename_axis ==> rename
1919
ss = round_trips.assign(ones=1).groupby('ones')[col].agg(list(stats_dict.values()))
2020
ss.columns = list(stats_dict.keys())
2121
stats_all = (ss.T.rename({1.0: 'All trades'}, axis='columns'))

backtest/volume_factor_alphalens.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"source": [
2424
"### Factor Analysis\n",
2525
"\n",
26-
"The predictive power of volume on future returns has been evaluted in the Vector Autoregression notebook. Here we use Alphalens to assess it directly."
26+
"The predictive power of volume on future returns has been evaluated in the Vector Autoregression notebook. Here we use Alphalens to assess it directly."
2727
]
2828
},
2929
{
@@ -275,7 +275,7 @@
275275
"\n",
276276
"3. Information Analysis; function alphalens.tears.create_information_tear_sheet;\n",
277277
"\n",
278-
"Fundamental law of investment defines $IR = IC \\cdot \\sqrt({breadth})$. We expect IC to be significantly different from zero. And it is not distorted by skewness. Therefore Alphalens also prvoides distribution and QQ plot.\n",
278+
"Fundamental law of investment defines $IR = IC \\cdot \\sqrt({breadth})$. We expect IC to be significantly different from zero. And it is not distorted by skewness. Therefore Alphalens also provides distribution and QQ plot.\n",
279279
"\n",
280280
"4. Turnover Analysis; function alphalens.tears.create_turnover_tear_sheet \n",
281281
"\n",
@@ -828,7 +828,7 @@
828828
"\n",
829829
"Information coefficient (IC) measures correlations between factor values and forward returns. It is positive but not very significant. \n",
830830
"\n",
831-
"On the other hand, mean return by quantile looks promising; so does the cumulative return by quantile. It suggests higher volumne leads to higher returns."
831+
"On the other hand, mean return by quantile looks promising; so does the cumulative return by quantile. It suggests higher volume leads to higher returns."
832832
]
833833
},
834834
{

eod/eod_run.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
-- USDT from treasury gov to misc
99
-- Option Stats from CBOE to misc
1010
-- VIX Index from CBOE to stocks
11-
-- VIX futurs from CBOE to futures
11+
-- VIX futures from CBOE to futures
1212
##################################################
1313
## {License_info}
1414
##################################################
@@ -17,7 +17,7 @@
1717
## Credits: [{credit_list}]
1818
## License: {license}
1919
## Version: {mayor}.{minor}.{rel}
20-
## Mmaintainer: {maintainer}
20+
## Maintainer: {maintainer}
2121
## Email: {contact_email}
2222
## Status: {dev_status}
2323
##################################################
@@ -170,15 +170,15 @@ def main(args):
170170
construct_inter_commodity_spreads()
171171
logging.info('-------- inter-commodity spread updated --------')
172172
except:
173-
logging.error('-------- inter-commdity spread failed --------')
173+
logging.error('-------- inter-commodity spread failed --------')
174174
time.sleep(3)
175175

176176
try:
177177
logging.info('-------- Construct ICS generic --------')
178178
construct_inter_comdty_generic_hist_prices()
179179
logging.info('inter-commodity generic spread updated.')
180180
except:
181-
logging.error('inter-commoidty generic spread failed.')
181+
logging.error('inter-commodity generic spread failed.')
182182
time.sleep(3)
183183

184184
if args.curve:

ml/a3c.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"\n",
3939
"[OpenAI Gym CartPole](https://gym.openai.com/envs/CartPole-v0/) has four states, cart position, cart speed, pole angle, and pole speed. The actions are either going left or right. The objective is to keep pole from falling. Every move that doesn't lead to a fall gets reward 1.\n",
4040
"\n",
41-
"A3C (Asynchronous Advantage Actor-Critic) is the asynchronous version of A2C proposed by DeepMind. Each worker pulls model parameters from glboal network and then feed updated parameters back after local training. Actor learns policy network while critic learns value network, and\n",
41+
"A3C (Asynchronous Advantage Actor-Critic) is the asynchronous version of A2C proposed by DeepMind. Each worker pulls model parameters from global network and then feed updated parameters back after local training. Actor learns policy network while critic learns value network, and\n",
4242
"\n",
4343
"$$\n",
4444
"\\begin{aligned}\n",

ml/american_option.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"\n",
4343
"2. Gym and Monte Carlo simulation: to create OpenAI gym environment.\n",
4444
"\n",
45-
"3. TF-Agent and Optimal stopping policy: to training a reinforement learning agent and policy."
45+
"3. TF-Agent and Optimal stopping policy: to training a reinforcement learning agent and policy."
4646
]
4747
},
4848
{

ml/atari_space_invaders.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@
473473
"source": [
474474
"In the Atari Space_Invaders environment, the agent learns to control a laser cannon to fire at descending aliens. The goal is to defeat all the aliens while avoiding to be destroyed by them.\n",
475475
"\n",
476-
"* Observation has a shape of (210, 160, 3), which stands for 210 pixels long, 160 pixels wide, and RGB color chanels. Each chanel is an integer value from 0 to 255. For example, a black pixel is (0,0,0), a white pixel is (255, 255, 255).\n",
476+
"* Observation has a shape of (210, 160, 3), which stands for 210 pixels long, 160 pixels wide, and RGB color channels. Each chanel is an integer value from 0 to 255. For example, a black pixel is (0,0,0), a white pixel is (255, 255, 255).\n",
477477
"* Reward is a scalar float value. Total rewards is the game score. No discount.\n",
478478
"* Action is a scalar integer with six possible values:\n",
479479
" * 0 — Stand still\n",
@@ -647,7 +647,7 @@
647647
"id": "S7W1VfUHqjgz"
648648
},
649649
"source": [
650-
"CNN is widely used in image recognition. Here the game screen has a shape of (210, 160, 3), which stands for 210 pixels long, 160 pixels wide, and RGB color chanels. DQN downsamples the pixel to 84 x 84 and convert the RGB colors to grayscale. Then it stacks 4 frames together in order to tell the direction and velocity of moving objects. Therefore the input is 84x84x4."
650+
"CNN is widely used in image recognition. Here the game screen has a shape of (210, 160, 3), which stands for 210 pixels long, 160 pixels wide, and RGB color channels. DQN downsamples the pixel to 84 x 84 and convert the RGB colors to grayscale. Then it stacks 4 frames together in order to tell the direction and velocity of moving objects. Therefore the input is 84x84x4."
651651
]
652652
},
653653
{
@@ -688,7 +688,7 @@
688688
"num_actions = action_tensor_spec.maximum - action_tensor_spec.minimum + 1\n",
689689
"observation_spec = tensor_spec.from_spec(train_env.observation_spec()) # (84, 84, 4) four gray frames stacking \n",
690690
"\n",
691-
"# proprocessing from uint8 color code between 0 and 255 to a float32 between 0 and 1.\n",
691+
"# preprocessing from uint8 color code between 0 and 255 to a float32 between 0 and 1.\n",
692692
"layer0 = tf.keras.layers.Lambda(lambda obs: tf.cast(obs, np.float32) / 255.)\n",
693693
"layer1 = tf.keras.layers.Conv2D(filters=32, kernel_size=(8, 8), strides = (4, 4), activation='relu')\n",
694694
"layer2 = tf.keras.layers.Conv2D(filters=64, kernel_size=(4, 4), strides = (2, 2), activation='relu')\n",
@@ -893,7 +893,7 @@
893893
"1. driver -- explores the environment using a collect policy\n",
894894
"2. collect policy -- used by driver to interact with the environment\n",
895895
"3. observer -- receives trajectories or experiences from driver and save to replay buffer\n",
896-
"4. agent -- randomly pull experience from replay buffer and use the exprience to train\n",
896+
"4. agent -- randomly pull experience from replay buffer and use the experience to train\n",
897897
"\n"
898898
]
899899
},
@@ -957,7 +957,7 @@
957957
"id": "2iY2lGo3EaSr"
958958
},
959959
"source": [
960-
"For information purpusoe, sample a batch of trajectories from the replay buffer and examine how they look like.\n",
960+
"For information purposes, sample a batch of trajectories from the replay buffer and examine how they look like.\n",
961961
"\n",
962962
"Below it samples 2 trajectories; each has three steps; each step contains a 84x84x4 observation."
963963
]

ml/dqn.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"\n",
3939
"[OpenAI Gym CartPole](https://gym.openai.com/envs/CartPole-v0/) has four states, cart position, cart speed, pole angle, and pole speed. The actions are either going left or right. The objective is to keep pole from falling. Every move that doesn't lead to a fall gets reward 1.\n",
4040
"\n",
41-
"DQN is off policy, value based, TD method to solve control problems. In 2015, DeepMind used DQN to play 50 Atari games and was able to achieve human-levle performance. It uses a DNN to model the q-value functions whose loss function is\n",
41+
"DQN is off policy, value based, TD method to solve control problems. In 2015, DeepMind used DQN to play 50 Atari games and was able to achieve human-level performance. It uses a DNN to model the q-value functions whose loss function is\n",
4242
"\n",
4343
"$$\n",
4444
"L = \\left( r+\\gamma \\underset{a' \\in A}{\\max}Q(s',a') - Q(s,a) \\right)^2 \\tag{1}\n",
@@ -233,7 +233,7 @@
233233
"for n_epi in range(episodes):\n",
234234
" # epsilon decay\n",
235235
" epsilon = max(0.01, 0.08 - 0.01 * (n_epi / 200))\n",
236-
" s = env.reset() # s is intial obs after reset\n",
236+
" s = env.reset() # s is initial obs after reset\n",
237237
" for t in range(600): # run one episode\n",
238238
" a = q.get_action(s, epsilon)\n",
239239
" s_prime, r, done, info = env.step(a)\n",
@@ -244,7 +244,7 @@
244244
" if done:\n",
245245
" break\n",
246246
"\n",
247-
" if memory.size() > 2000: # train as soon as there are 2000 expriences\n",
247+
" if memory.size() > 2000: # train as soon as there are 2000 experiences\n",
248248
" train(q, q_target, memory, optimizer)\n",
249249
"\n",
250250
" if n_epi % print_interval == 0 and n_epi != 0:\n",

ml/mc_control.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@
154154
" # Update all (state, action) pairs we've visited in this episode\n",
155155
" sa_in_episode = set([(x[0], x[1]) for x in s_a_r_episode])\n",
156156
" for state, action in sa_in_episode:\n",
157-
" first_occurence_idx = next(i for i,x in enumerate(s_a_r_episode) if x[0] == state and x[1] == action)\n",
158-
" G = sum([x[2]*(gamma**i) for i,x in enumerate(s_a_r_episode[first_occurence_idx:])])\n",
157+
" first_occurrence_idx = next(i for i,x in enumerate(s_a_r_episode) if x[0] == state and x[1] == action)\n",
158+
" G = sum([x[2]*(gamma**i) for i,x in enumerate(s_a_r_episode[first_occurrence_idx:])])\n",
159159
" N[state, action] += 1.0\n",
160160
" alpha = 1.0 / N[state, action]\n",
161161
" # update q values\n",

ml/ppo.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
"returns = [] # average returns over 20 episodes\n",
267267
"total = 0 # total returns\n",
268268
"for i_epoch in range(episodes):\n",
269-
" state = env.reset() # reset to intial state\n",
269+
" state = env.reset() # reset to initial state\n",
270270
" for t in range(500): # maximum 500 steps\n",
271271
" action, action_prob = agent.select_action(state)\n",
272272
" next_state, reward, done, _ = env.step(action)\n",

ml/reinforce.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"\n",
6565
"Below it uses the former and code structure follows closely [here](https://github.com/dragen1860/Deep-Learning-with-TensorFlow-book/blob/master/ch14-%E5%BC%BA%E5%8C%96%E5%AD%A6%E4%B9%A0/REINFORCE_tf.py).\n",
6666
"\n",
67-
"REINFORCE is usually adjusted by a basline to reduce variance.\n",
67+
"REINFORCE is usually adjusted by a baseline to reduce variance.\n",
6868
"\n",
6969
"\n",
7070
"__Reference__\n",

ml/reinforcement_trader.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
"id": "escS5QYOGuOO"
1717
},
1818
"source": [
19-
"(Use Open in Colab button above to see trading vidoes)\n",
19+
"(Use Open in Colab button above to see trading videos)\n",
2020
"\n",
2121
"## Introduction\n",
2222
"\n",
2323
"From reinforcement gamer to reinforcement trader. Part II.\n",
2424
"\n",
25-
"For reinforcement gamer, Part I, check out [the previous notebook](https://github.com/letianzj/QuantResearch/blob/master/ml/atari_space_invaders.ipynb). This noteboook shares a lot of resemblance to the previous one.\n",
25+
"For reinforcement gamer, Part I, check out [the previous notebook](https://github.com/letianzj/QuantResearch/blob/master/ml/atari_space_invaders.ipynb). This notebook shares a lot of resemblance to the previous one.\n",
2626
"\n",
2727
"As illustrated in the figure below, investing bears a clear resemblance to game playing. In fact, some good poke players, such as Edward Thorp, also stand out in the stock markets.\n",
2828
"\n",

notebooks/bayesian_linear_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
sigma_beta_0 = sigma_beta_1
4141
beta_recorder.append(beta_0)
4242

43-
print('pamameters: %.7f, %.7f' %(beta_0[0], beta_0[1]))
43+
print('parameters: %.7f, %.7f' %(beta_0[0], beta_0[1]))
4444

4545
# plot the Beyesian dynamics
4646
xfit = np.linspace(0, 10, sample_size)

notebooks/ch1_pca_relative_value.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"There are three basic movements in yield curve: \n",
5151
"1. level or a parallel shift; \n",
5252
"2. slope, i.e., a flattening or steepening; and \n",
53-
"3. curvature, i.e., hump or butterlfy.\n",
53+
"3. curvature, i.e., hump or butterfly.\n",
5454
"\n",
5555
"PCA formalizes this viewpoint.\n",
5656
"\n",
@@ -1232,7 +1232,7 @@
12321232
"id": "0TapnAwfi-nz"
12331233
},
12341234
"source": [
1235-
"SVD has p singluar values; covariance matrix is pxp. $W^T$ is pca.components_, which is pxp"
1235+
"SVD has p singular values; covariance matrix is pxp. $W^T$ is pca.components_, which is pxp"
12361236
]
12371237
},
12381238
{
@@ -1523,7 +1523,7 @@
15231523
"\n",
15241524
"Second PC is spread. It suggests that short tenors move downward while long tenors move upward, or steepening. \n",
15251525
"\n",
1526-
"Third PC is buterfly or curvature. The belly rises 40bps while the wings fall 40bps."
1526+
"Third PC is butterfly or curvature. The belly rises 40bps while the wings fall 40bps."
15271527
]
15281528
},
15291529
{

notebooks/classical_linear_regression.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
# normal equation to estimate the model parameters
1717
X = np.vstack((np.ones(sample_size), x)).T
1818
params_closed_form = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
19-
print('pamameters: %.7f, %.7f' %(params_closed_form[0], params_closed_form[1]))
19+
print('parameters: %.7f, %.7f' %(params_closed_form[0], params_closed_form[1]))
2020

2121
from sklearn.linear_model import LinearRegression
2222
# The next two lines does the regression
2323
lm_model = LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
2424
lm_model.fit(x.reshape(-1,1), y) # fit() expects 2D array
25-
print('pamameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
25+
print('parameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
2626

2727
# present the graph
2828
xfit = np.linspace(0, 10, sample_size)

notebooks/cointegration_pairs_trading.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# The next two lines does the regression
2626
lm_model = LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
2727
lm_model.fit(data['EWA US Equity'].values.reshape(-1,1), data['EWC US Equity'].values) # fit() expects 2D array
28-
print('pamameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
28+
print('parameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
2929

3030
# present the graph
3131
fig, ax = plt.subplots(nrows=1, ncols=2)
@@ -53,7 +53,7 @@
5353

5454
lm_model = LinearRegression(copy_X=True, fit_intercept=True, normalize=False)
5555
lm_model.fit(data['EWC US Equity'].values.reshape(-1,1), data['EWA US Equity'].values) # fit() expects 2D array
56-
print('pamameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
56+
print('parameters: %.7f, %.7f' %(lm_model.intercept_, lm_model.coef_))
5757
yfit = lm_model.coef_ * data['EWC US Equity'] + lm_model.intercept_
5858
y_residual = data['EWA US Equity'] - yfit
5959
ts.adfuller(y_residual, 1) # lag = 1

notebooks/hidden_markov_chain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from datetime import datetime, date
1010
from hmmlearn.hmm import GaussianHMM
1111

12-
#################################################### Viberti #####################################################
12+
#################################################### Viterbi #####################################################
1313
# https://en.wikipedia.org/wiki/Viterbi_algorithm
1414
obs = ('happy', 'happy', 'happy')
1515
states = ('Up', 'Down')

notebooks/mcmc_linear_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# don't forget to generate the 500 random samples as in the previous post
1717
sigma_e = 3.0
1818

19-
# Similar to last post, let's initially believe that a, b follow Normal distribution with mean 0.5 and stadndard deviation 0.5
19+
# Similar to last post, let's initially believe that a, b follow Normal distribution with mean 0.5 and standard deviation 0.5
2020
# it returns the probability of seeing beta under this belief
2121
def prior_probability(beta):
2222
a = beta[0] # intercept

notebooks/pairs_trading_kalman_filter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
obs_mat_F = np.transpose(np.vstack([data[sym_a].values, np.ones(data.shape[0])])).reshape(-1, 1, 2)
4343

4444
kf = KalmanFilter(n_dim_obs=1, # y is 1-dimensional
45-
n_dim_state=2, # states (alpha, beta) is 2-dimensinal
45+
n_dim_state=2, # states (alpha, beta) is 2-dimensional
4646
initial_state_mean=np.ones(2), # initial value of intercept and slope theta0|0
4747
initial_state_covariance=np.ones((2, 2)), # initial cov matrix between intercept and slope P0|0
4848
transition_matrices=np.eye(2), # G, constant

notebooks/portfolio_management_one.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
ef_right = np.asscalar(max(hist_mean.as_matrix())) # maximum return
107107
target_returns = np.linspace(ef_left, ef_right, N) # N target returns
108108
optimal_weights = [ solvers.qp(P, q, A=A, b=matrix([t,1]))['x'] for t in target_returns ] # QP solver
109-
ef_returns = [ np.asscalar(np.dot(w.T, hist_mean.as_matrix())*250) for w in optimal_weights ] #a nnualized
109+
ef_returns = [ np.asscalar(np.dot(w.T, hist_mean.as_matrix())*250) for w in optimal_weights ] # annualized
110110
ef_risks = [ np.asscalar(np.sqrt(np.dot(w.T, np.dot(hist_cov, w)) * 250)) for w in optimal_weights ]
111111

112112
plt.plot(port_stdevs, port_returns, 'o', markersize=6, label='Candidate Market Portfolio')

notebooks/python.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@
733733
"source": [
734734
"### Grouping\n",
735735
"\n",
736-
"groupby: split/appply/combine"
736+
"groupby: split/apply/combine"
737737
]
738738
},
739739
{

notebooks/tensorflow_linear_regression.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"colab_type": "text"
101101
},
102102
"source": [
103-
"### 1. Low-Level Implementattion\n",
103+
"### 1. Low-Level Implementation\n",
104104
"\n",
105105
"The convergence depends on initial states of w and b, batch_size, learning rate, and loss function (mse or rmse), etc."
106106
]

0 commit comments

Comments
 (0)