This repository provides Python code and Jupyter Notebooks accompanying the Machine Learning for Financial Risk Management with Python book published by O'Reilly.
Buy the book on Amazon.
License: Other
This repository provides Python code and Jupyter Notebooks accompanying the Machine Learning for Financial Risk Management with Python book published by O'Reilly.
Buy the book on Amazon.
cost_log = conf_mat_log[0][1] * cost_fp + conf_mat_boost[1][0] *
cost_fn.mean() + conf_mat_log[1][1] * cost_tp
cost_dt = conf_mat_dt[0][1] * cost_fp + conf_mat_boost[1][0] *
cost_fn.mean() + conf_mat_dt[1][1] * cost_tp
cost_rf = conf_mat_rf[0][1] * cost_fp + conf_mat_boost[1][0] *
cost_fn.mean() + conf_mat_rf[1][1] * cost_tp
Should all 3 cost variables be using conf_mat_boost[1][0] or should it be:
cost_log = conf_mat_log[0][1] * cost_fp + conf_mat_log[1][0] *
cost_fn.mean() + conf_mat_log[1][1] * cost_tp
cost_dt = conf_mat_dt[0][1] * cost_fp + conf_mat_dt[1][0] *
cost_fn.mean() + conf_mat_dt[1][1] * cost_tp
cost_rf = conf_mat_rf[0][1] * cost_fp + conf_mat_rf[1][0] *
cost_fn.mean() + conf_mat_rf[1][1] * cost_tp
I had to use a higher memory machine (4 vCPU and 32GB vs 4 vCPU and 16GB) to enable the cost sensitive models to complete. Otherwise a memory allocation error was generated or the kernel crashed.
Amended 'from keras import regularizers' to import from tensorflow as 'from tensorflow.keras import regularizers'
Autoencoder training takes a while to complete 100 epochs.
!pip install plotly
rand = np.random.rand(n_assets
3.. portfolio = np.array([port_return(np.random.randn(n_assets, i))
for i in range(1, 101)])
This appears to be varying the number of simulations from 1 to 100 for each of 100 experiments, should it be:
portfolio=np.array([port_return(np.random.randn(n_assets, n_simulation)) for i in range(1,101)])
The prediction appears to be generated against the last n_steps of the training data (22/11 - 11/12) and then plotted against the date range of the test set (12/12 - 31/12)
These are the same length because the prediction is generated over n_steps = 13 which is same as the last 5% of the original 251 records selected for the test set.
FraudTrain.txt file is missing
rolling_five.append(liq_data[liq_data.TICKER == j][i:i+5].agg({'BIDLO': 'min',
'ASKHI': 'max',
'VOL': 'sum',
'SHROUT': 'mean',
'PRC': 'mean'}))
This same bug seems to be present in subsequent sections as well.
The last 4 values of the rolling window have 4,3,2 and 1 rows to calculate over, such that the last row is simply the values of BIDLO,ASKHI, VOL, SHROUT & PRC. If the modification above is included this occurs at the end of each set of rows per TICKERs as opposed to just once at the end of the file.
For the liq_ratio calculation the numerator is a sum of 5 sets of 'price X volume' calculations whereas the denominator is a single difference of means. As such the numerator-denominator ratio comes out 5 times greater than perhaps it should. Alternatively means could be used in the numerator as well as:
liq_ratio.append((liq_vol_all[liq_data.TICKER == j]['PRC'][i+1:i+6].mean() *
liq_vol_all[liq_data.TICKER == j]['VOL'][i+1:i+6].mean())/
The only fix i can suggest is to skip calculating the roll value for last 5 rows for each TICKER but this doesn't seem very acceptable as it leaves missing values in the dataset:
for j in liq_vol_all.TICKER.unique():
for i in range(len(liq_vol_all[liq_vol_all.TICKER == j])-5):
(liq_vol_all[liq_data.TICKER == j]['VOL'][i:i+5].sum() /
As a suggestion to ensure the last few rows are of the same magnitude as the previous values as fewer rows are available as we come to the end of the dataset an option might be:
((liq_vol_all[liq_data.TICKER == j]['VOL'][i:i+5].mean()*5) /
I stopped here with this chapter based on my comments above
TypeError: init() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given
Suggest:
kde = KernelDensity(bandwidth=bandwidth, kernel='gaussian')
bid_ask = pd.read_csv('datasets/bid_ask.csv')
This section of code is not needed as crash_dataw is not referenced further:
std = crash_data.groupby('TICKER')['RET'].resample('W').std()
.reset_index()
crash_dataw['std'] = pd.DataFrame(std['RET'])
The count values are not used:
merge_all = merge_grouped.groupby('TICKER')
.resample('1Y').agg({'down':['sum', 'count'],
'up':['sum', 'count']})
.reset_index()
merge_all.head()
Hi Abdullah,
I am the Chinese translator of your book.
This is my LinkedIn profile:
https://www.linkedin.cn/injobs/in/weimin-ye-9203806
In order to get the best quality of the translation, could you add me via Linkedin and we can communicate more?
I am trying to rewrite the code and find that bs_v.3 and Fraudtrain data missing.
Does anyone know where to find the missing data? Thanks in advance
As below error message, ctgan library cannot import CTGANSynthesizer
This code was exercised in Google colab and the same error occurred in my Anaconda (python version = 3.8)
from ctgan import CTGANSynthesizer
ctgan = CTGANSynthesizer(epochs=10)
ctgan.fit(california_housing_df)
synt_sample = ctgan.sample(len(california_housing_df))
ImportError Traceback (most recent call last)
in <cell line: 1>()
----> 1 from ctgan import CTGANSynthesizer
2
3 ctgan = CTGANSynthesizer(epochs=10)
4 ctgan.fit(california_housing_df)
5 synt_sample = ctgan.sample(len(california_housing_df))
ImportError: cannot import name 'CTGANSynthesizer' from 'ctgan' (/usr/local/lib/python3.10/dist-packages/ctgan/init.py)
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
Had to install tensorflow and yfinance
Erroneous >
model.add(Flatten())>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.