Skip to content

Instantly share code, notes, and snippets.

Avatar

Michy mick001

View GitHub Profile
@mick001
mick001 / Random_forest_example.py
Last active Aug 27, 2015
Example on how to start with scikit-learn and use a Random Forest classifier for a classification task
View Random_forest_example.py
from sklearn.ensemble import RandomForestClassifier
from sklearn.cross_validation import train_test_split
import pandas as pd
import os
# Loading data
data = pd.read_csv('data.csv')
# Encoding categorical features into numerical
buying_map = {'vhigh':4,'high':3,'med':2,'low':1}
@mick001
mick001 / Arduino_measurement_sketch.ino
Last active Aug 27, 2015
Arduino script for time measurement (to be used with Python and the Arduino class)
View Arduino_measurement_sketch.ino
//Initialize global variables
int ledRed = 8; //Red LED pin
int ledGreen = 9; //Green LED pin
int sensorCurrent = 10; //Sensor pin
int sensorRed = 0; //Red laser light sensor
int sensorGreen = 0; //Green laser light sensor
int measurements = 0; //Number of measurements done
int numberOfMeasurements = 1; //Measurements to be done (must be the same as nSamples in the Python script)
float t1; //Initial time (time at the start of the measurement)
float t2; //End time (time at the end of the measurement)
@mick001
mick001 / python_measuring_function.py
Last active Aug 27, 2015
Measuring function in Python, to be used with the Arduino script
View python_measuring_function.py
#------------------------------------------------------------------------------
# Measuring function. Makes nSamples measurements through an Arduino object
# Notes:
# The program loops until it collects the nSamples readings
#
# This function accepts an object of the Arduino class
def measure(nSamples,arduino,save=False):
try:
@mick001
mick001 / hidden_markov_models_example.py
Last active May 15, 2017
Example of hidden Markov models (HMM)
View hidden_markov_models_example.py
import numpy as np
import time
start = ['R','Su']
#Start probability
p_start = [0.2,0.8]
#t1 = [['R|R','Su|R'],['R|Su','Su|Su']]
#Transition probability
t1 = ['R','Su']
@mick001
mick001 / option_pricing_normal.py
Last active Feb 20, 2020
Call option pricing in Python assuming normally distributed returns
View option_pricing_normal.py
"""
MONTE CARLO PLAIN VANILLA OPTION PRICING
This script is used to estimate the price of a plain vanilla
option using the Monte Carlo method and assuming normally
distributed returns using a parametric normal distribution
approach.
Call option quotations are available at:
http://www.google.com/finance/option_chain?q=NASDAQ%3AAAPL&ei=fNHBVaicDsbtsAHa7K-QDQ
@mick001
mick001 / Option_pricing_KDE_estimate.py
Last active Apr 1, 2016
Call option pricing using KDE estimate instead of the normality assumption
View Option_pricing_KDE_estimate.py
"""
MONTE CARLO PLAIN VANILLA OPTION PRICING
This script is used to estimate the price of a plain vanilla
option using the Monte Carlo method and assuming that returns
can be simulated using an estimated probability density (KDE estimate)
Call option quotations are available at:
http://www.google.com/finance/option_chain?q=NASDAQ%3AAAPL&ei=fNHBVaicDsbtsAHa7K-QDQ
@mick001
mick001 / option_pricing.cpp
Created Aug 27, 2015
Call option pricing C++ implementation. (Returns are assumed to be normally distributed)
View option_pricing.cpp
// Call option Monte Carlo evaluation
#include <iostream>
#include <random>
#include <math.h>
#include <chrono>
using namespace std;
/* double stoc_walk: returns simulated price after periods
@mick001
mick001 / gathering_data.R
Created Aug 28, 2015
Gathering data script (R)
View gathering_data.R
#-------------------------------------------------------------------------------
# Gathering the data
#-------------------------------------------------------------------------------
# Loading the entire mtcars dataset
data(mtcars)
# Subsetting the dataset for our use
dat <- subset(mtcars,select=c(mpg,disp,hp,drat,wt))
dat
@mick001
mick001 / linear_reg.R
Created Aug 28, 2015
Linear regression in R
View linear_reg.R
#-------------------------------------------------------------------------------
# Linear regression y = a + bx
#-------------------------------------------------------------------------------
# Fit the model
model1 <- lm(wt ~ disp)
# Parameters and information about the model
model1
coef(model1)
@mick001
mick001 / parabola_fit.R
Created Aug 28, 2015
Example of a quadratic model fit in R
View parabola_fit.R
#-------------------------------------------------------------------------------
# Parabola y = a + bx + cx^2
#-------------------------------------------------------------------------------
model2 <- lm(wt ~ disp+I(disp^2))
summary(model2)
coef(model2)
# Predicted vs original
predicted <- fitted(model2)