Skip to content

Instantly share code, notes, and snippets.

@accessnash
accessnash / stratsampling.M
Last active June 11, 2018 19:20
Stratified sampling example for a call option
#Model Parameters
S0 = 100;
K =90;
r = 0.025;
sigma = 0.2;
T = 1;
#Simulation parameters
#B =  [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000]; #Intervals
#NB = [1000, 500, 200, 100, 50, 20, 10, 5, 2, 1]; # sample size
@accessnash
accessnash / asian.M
Last active June 12, 2018 03:53
Asian Options : Comparison of the Variance Ratios using 3 methods : Importance Sampling using GHS method to find µ , Stratification based on conditioning using the same µ vector, Combination of the Importance Sampling and Stratification
r = .05; % Set up parameters and arrays
sig = .10;
T = 1;
s0 = 50;
K = 45;
N = 16; %time steps along path
n = 1000000;
B=100; % number of bins for stratification
NB = n/B; %sample size per bin
del = T/N;
@accessnash
accessnash / class-size
Created June 13, 2018 01:44
Maimonides rules - Stata
reg avgmath classize
reg avgverb classize
reg avgmath classize ses
reg avgverb classize ses
gen maimrule = gradesize/(int((gradesize-1)/40)+1)
twoway (scatter classize gradesize, msize(small)) (line maimrule gradesize, lcolor(orange))
reg avgmath maimrule gradesize
@accessnash
accessnash / splitpersonality.py
Created June 13, 2018 23:37
A function to split names and keep only title and last names
Personalities = ['Mr. Clarke Kent', 'Dr. Victor Doom', 'Dr. Stephen Strange', 'Dr. Jonathan Osterman']
def split_names(superhero):
title = superhero.split()[0]
lastname = superhero.split()[-1]
return '{} {}'.format(title, lastname)
list(map(split_names, Personalities))
@accessnash
accessnash / pca_stockreturns.R
Created June 17, 2018 00:45
Principal component analysis and factor analysis for stock return data
findata <- read.table("C:/Users/DASA0/Desktop/MS Econ/Stat 524/wichern data/T8-4.dat", sep="\t", header=F)
colnames(findata) <- c("JP Morgan", "City bank", "Wells Fargo", "Royal Dutch", "Exxon")
Xbar <- colMeans(findata)
S <- cov(findata)
weight <- diag(1/sqrt(diag(cov(findata))))
#standardization
stdfindata <- (as.matrix(findata)-rep(1, dim(findata)[1])%*%t(apply(findata, 2, mean)))%*%weight
stdfindata <- as.data.frame(stdfindata)
colnames(stdfindata) <- c("JP Morgan", "City bank", "Wells Fargo", "Royal Dutch", "Exxon")
R <- cov(stdfindata)
@accessnash
accessnash / balancedbrackets.py
Created June 24, 2018 00:59
Balanced brackets problem from Hackerrank
def balanced( a_str):
if len(a_str) %2 !=0:
return False
open_bracks = ['(', '{', '[']
close_bracks = [')', '}', ']']
par_dict = {open_bracks[0]:close_bracks[0], open_bracks[1]:close_bracks[1], open_bracks[2]:close_bracks[2]}
if a_str[0] in close_bracks:
return False
@accessnash
accessnash / melonsboxes.py
Created June 24, 2018 01:01
Melons and boxes problem from Hackerrank (need to revise - all test cases do not pass)
def melon_count(boxes, melons):
boxes.sort()
melons.sort()
b= len(boxes)
m = len(melons)
noofmelons = 0
i = 0
j = 0
while(i != b and j != m):
@accessnash
accessnash / dfexample.py
Last active July 3, 2018 21:06
A simple example of Pandas and Dataframes
import pandas as pd
purchase_1 = pd.Series({'Name': 'Paul',
'Book Purchased': 'Monte Carlo Methods',
'Cost': 94.69})
purchase_2 = pd.Series({'Name': 'Steve',
'Book Purchased': 'Stochastic Calculus',
'Cost': 39.55})
purchase_3 = pd.Series({'Name': 'Brigo',
'Book Purchased': 'Interest Rate Models',
@accessnash
accessnash / binarysearch.py
Last active July 9, 2018 20:50
Binary search algorithm with a measurement of performance in terms of time taken
from time import time
def present(collection, num):
'''finds out if number of our interest is present in collection'''
return num in collection
def binsearch(ordered, num):
'''binary search function to show position of number of our interest in the collection'''
low = 0
high = len(ordered)-1
@accessnash
accessnash / chisquare.py
Last active July 7, 2018 03:31
Checking how skew in a chi-squared distribution decreases as the degrees of freedom increases
import numpy as np
import pandas as pd
import scipy.stats as stat
import matplotlib.pyplot as plt
chi_squared_df1 = np.random.chisquare(1, size = 100000)
chi_squared_df2 = np.random.chisquare(2, size = 100000)
chi_squared_df3 = np.random.chisquare(3, size = 100000)