Skip to content

Instantly share code, notes, and snippets.

@accessnash
accessnash / portfolio_risk_ch1.py
Last active July 16, 2018 18:14
Intro to Portfolio Risk Management in Python - course on Datacamp - Chapter 1
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'])
@accessnash
accessnash / portfolio_risk_ch2.py
Last active July 16, 2018 20:10
Intro to Portfolio Risk Management in Python - course on Datacamp - Chapter 2
# 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)
@accessnash
accessnash / portfolio_risk_ch3.py
Created July 16, 2018 21:32
Intro to Portfolio Risk Management in Python - course on Datacamp - Chapter 3
# 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()
@accessnash
accessnash / portfolio_risk_ch4.py
Created July 17, 2018 13:26
Intro to Portfolio Risk Management in Python - course on Datacamp - Chapter 4
# 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
@accessnash
accessnash / iris_EDA.py
Last active July 18, 2018 18:29
exploratory data analysis in Python for the Iris dataset from UCI library
# Import plotting modules
import matplotlib.pyplot as plt
import seaborn as sns
# Import numpy
import numpy as np
# Set default Seaborn style
sns.set()
@accessnash
accessnash / knn_voting.py
Last active July 19, 2018 18:19
fit a k-Nearest Neighbors classifier to the Congressional voting record dataset (scikit learn)- Datacamp
# 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
@accessnash
accessnash / gapminder_reg.py
Last active March 15, 2020 19:45
Predict the life expectancy in a given country using Gapminder data - OLS Regression, cross-validation, Lasso & Ridge regression (Supervised Learning with scikit learn at Datacamp)
# 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']
@accessnash
accessnash / PIMA_Indians.py
Last active July 22, 2018 02:57
PIMA Indians dataset obtained from the UCI Machine Learning Repository - to predict whether or not a given female patient will contract diabetes based on features such as BMI, age, and number of pregnancies (Supervised Learning with scikit learn at Datacamp)
# 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
@accessnash
accessnash / gapminder_pipeline.py
Last active July 24, 2018 13:21
Preprocessing data using Gapminder data - Supervised Learning with scikit learn at Datacamp
# 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
@accessnash
accessnash / regression_seaborn.py
Created August 3, 2018 22:52
Visualizing regressions using Seaborn - Datacamp
# 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()