Skip to content

Instantly share code, notes, and snippets.

@kangeugine
kangeugine / sample.py
Created August 25, 2017 03:28
sample python code
import numpy as np
def ols(x,y):
x_mean = x.mean()
y_mean = y.mean()
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum()
beta_0 = y_mean - (beta_1 * x_mean)
def lin_model(new_x, slope=beta_1, intercept=beta_0):
return new_x * slope + intercept
@kangeugine
kangeugine / ols.py
Created August 25, 2017 03:31
Ordinary Least Square
def ols(x,y):
x_mean = x.mean()
y_mean = y.mean()
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum()
beta_0 = y_mean - (beta_1 * x_mean)
def lin_model(new_x, slope=beta_1, intercept=beta_0):
return new_x * slope + intercept
return lin_model
@kangeugine
kangeugine / palindrome_test.py
Created August 25, 2017 04:28
Unit Testing Example Palindrome
def is_palindrome(sequence):
return sequence == sequence[::-1]
# test when sequence IS palindrome
def test_true():
seq = 'bob'
assert is_palindrome(seq) == True
def test_true_list():
seq = [1, 0, 1]
import pytest
@pytest.fixture
def default_list():
return [1, 2]
def test_fixture_1(default_list):
del default_list[0]
assert default_list == [2]
@kangeugine
kangeugine / pytest_param_test.py
Created August 25, 2017 05:06
Pytest Mark Parametrize
import pytest
def is_int(age):
return isinstance(age, int)
@pytest.mark.parametrize("age", [
"five",
1.5,
{2: 3},
[2, 5],
@kangeugine
kangeugine / test_ols.py
Created August 25, 2017 17:35
Unit Testing with OLS
import numpy as np
def ols(x,y):
x_mean = x.mean()
y_mean = y.mean()
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum()
beta_0 = y_mean - (beta_1 * x_mean)
def lin_model(new_x, slope=beta_1, intercept=beta_0):
return new_x * slope + intercept
@kangeugine
kangeugine / nnls-piecewise.py
Created August 25, 2017 23:29
Non-linear least square for piecewise linear function
from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15], dtype=float)
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 90, 80, 70, 60])
def piecewise_linear(x, y0, y1, b0, b1, b2):
x0 = 6
x1 = 12
@kangeugine
kangeugine / arima_1.py
Created August 26, 2017 15:21
ARIMA 1
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
def parser(x):
return datetime.strptime('190'+x, '%Y-%m')
series = pd.read_csv('data/sales-of-shampoo-over-a-three-ye.csv', \
header=0, \
parse_dates=[0], \
@kangeugine
kangeugine / arima_2.py
Created August 26, 2017 15:24
ARIMA autocorrelation
from pandas.tools.plotting import autocorrelation_plot
autocorrelation_plot(series)
plt.show()
@kangeugine
kangeugine / arima_3.py
Created August 26, 2017 15:38
ARIMA fit model and residuals
from statsmodels.tsa.arima_model import ARIMA
# fit model
model = ARIMA(series, order=(5,1,0))
model_fit = model.fit(disp=0)
# print(model_fit.summary())
# plot residual erros
residuals = pd.DataFrame(model_fit.resid)
residuals.plot()