This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import numpy as np | |
| from scipy.stats import skew | |
| from scipy.stats import kurtosis | |
| from scipy import stats | |
| from scipy.stats import shapiro | |
| # Chapter 1: Univariate Investment Risk and Returns | |
| StockPrices = pd.read_csv('StockData.csv', parse_dates=['Date']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Chapter 2 : Portfolio Investing | |
| portfolio_weights = np.array([0.12, 0.15, 0.08, 0.05, 0.09, 0.10, 0.11, 0.14, 0.16]) | |
| # Calculate the weighted stock returns | |
| WeightedReturns = StockReturns.mul(portfolio_weights, axis=1) | |
| # Calculate the portfolio returns | |
| StockReturns['Portfolio'] = WeightedReturns.sum(axis=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Chapter 3 : Factor investing | |
| # Calculate excess portfolio returns | |
| FamaFrenchData['Portfolio_Excess'] = FamaFrenchData['Portfolio'] - FamaFrenchData['RF'] | |
| # Plot returns vs excess returns | |
| CumulativeReturns = ((1+FamaFrenchData[['Portfolio','Portfolio_Excess']]).cumprod()-1) | |
| CumulativeReturns.plot() | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Chapter 4 : Value At Risk | |
| # Sub-topic : Estimating tail risk | |
| # Calculate the running maximum | |
| running_max = np.maximum.accumulate(cum_rets) | |
| # Ensure the value never drops below 1 | |
| running_max[running_max < 1] = 1 | |
| # Calculate the percentage drawdown |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import plotting modules | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| # Import numpy | |
| import numpy as np | |
| # Set default Seaborn style | |
| sns.set() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Supervised learning with scikit learn | |
| from sklearn import datasets | |
| import matplotlib.pyplot as plt | |
| # Import KNeighborsClassifier from sklearn.neighbors | |
| from sklearn.neighbors import KNeighborsClassifier | |
| # Create arrays for the features and the response variable | |
| y = df['party'].values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import numpy and pandas | |
| import numpy as np | |
| import pandas as pd | |
| # Read the CSV file into a DataFrame: df | |
| df = pd.read_csv('gapminder.csv') | |
| # Create arrays for features and target variable | |
| y = df['life'] | |
| X = df['fertility'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import necessary modules | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import classification_report | |
| from sklearn.metrics import confusion_matrix | |
| from sklearn.linear_model import LogisticRegression | |
| # Create training and test set | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42) | |
| # Instantiate a k-NN classifier: knn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import pandas | |
| import pandas as pd | |
| # Read 'gapminder.csv' into a DataFrame: df | |
| df = pd.read_csv('gapminder.csv') | |
| # Create a boxplot of life expectancy per region | |
| df.boxplot('life', 'Region', rot=60) | |
| # Show the plot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import plotting modules | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| # Plot a linear regression between 'weight' and 'hp' | |
| sns.lmplot(x='weight', y='hp', data=auto) | |
| # Display the plot | |
| plt.show() |