from __future__ import division
import numpy as np
import sys

# X_train = np.genfromtxt(sys.argv[1], delimiter=",")
# Y_train = np.genfromtxt(sys.argv[2], delimiter=',')
# X_test = np.genfromtxt(sys.argv[3], delimiter=",")

X_train = np.loadtxt('X_train.csv', delimiter=",")
Y_train = np.loadtxt('y_train.csv', delimiter=",")
X_test = np.loadtxt('X_test.csv', delimiter=",")

def calc_parameters_likelihood_mle(_classes, _x_train, _y_train):
    """
    Calc likelihood MLE
    :param _classes: classes dataset
    :param _x_train: train set x
    :param _y_train: train label
    :return: mus, sigmas, sigma_dets
    """
    mus, sigmas, sigma_dets = {}, {}, {}
    for c in _classes:
        c_id = (_y_train == c)
        X = _x_train[c_id, :]
        mu = np.matrix(np.mean(X, axis=0)).T
        # calculate gaussian covariance mle
        n, _ = X.shape
        sigma = 0
        for i in range(n):
            x = np.matrix(X[i, :]).T
            sigma += (x - mu) * (x - mu).T / n
        sigma_det = np.linalg.det(sigma)
        mus[c], sigmas[c], sigma_dets[c] = mu, sigma, sigma_det

    return mus, sigmas, sigma_dets

def plugin_classifier(x_train, y_train, x_test):
    """
    Implements bayes classifier
    :param x_train:
    :param y_train:
    :param x_test:
    :return: prob_pred for test set
    """

    # calculate prior by MLE
    n = y_train.shape[0]
    classes = np.unique(y_train)

    priors = {}
    for c in classes:
        priors[c] = np.sum(y_train == c) / n

    # calculate parameters of likelihood by MLE
    mus, sigmas, sigma_dets = calc_parameters_likelihood_mle(classes, x_train, y_train)

    c = classes.shape[0]

    n, d = x_test.shape
    prob_pred = np.zeros((n, c))

    for i in range(n):
        x = np.matrix(x_test[i, :]).T

        i_prob = np.zeros(c)
        for j in range(c):
            # calculate Gaussian likelihood
            mu = mus[classes[j]]
            sigma = sigmas[classes[j]]
            sigma_det = sigma_dets[classes[j]]

            # calc multi gaussian prob and proportion of posterior
            i_prob[j] = priors[classes[j]] * np.sqrt(sigma_det) ** (-1) * np.exp(
                - 0.5 * (x - mu).T * sigma.I * (x - mu))

        prob_pred[i, :] = i_prob / np.sum(i_prob)

    return prob_pred

def main():
    # assuming final_outputs is returned from function
    final_outputs = plugin_classifier(X_train, Y_train, X_test)

    # write output to file
    np.savetxt("probs_test.csv", final_outputs, delimiter=",")

if __name__ == '__main__':
    main()


"""
bracketing.py

Returns the uncertainty interval containing the minimizer of the function
X0 - initial guess
func - anonimous function
func_1st_der - anonimous function 1st derivative
"""

import numpy as np

def bracketing(X0, func, func_1st_der):

    epsilon = 0.5;
    d = - func_1st_der(X0) / np.linalg.norm(func_1st_der(X0)); # directional unit vector
    X = np.empty(shape = (100));
    F_at_X = np.empty(shape = (100));
    X[0] = X0;
    F_at_X[0] = func(X[0]);
    X[1] = X0 + 2 * epsilon * d;
    F_at_X[1] = func(X[1]);

    for ii in range(2, 100, 1):
        X[ii] = X0 + 2 * ii * epsilon * d;
        F_at_X[ii] = func(X[ii]);
        if F_at_X[ii - 1] < F_at_X[ii - 2] and F_at_X[ii - 1] < F_at_X[ii] :
            break;

    if (d > 0):
        interval = (X[ii - 2], X[ii]);
    else:
        interval = (X[ii], X[ii - 2]);
    return interval;


import numpy as np
import scipy.spatial.distance

def kmeans(data, centers, delta=.0001, maxiter=500, metric="cityblock"):
    """
    Inputs:
        data    : observations x features
        centers : k x features: the initial centroids; e.g.
                  a random sample of rows from X. These could be found
                  using k++ initialization before calling kmeans.
        delta   : cutoff; iterate until the average distance of the
                  observations to the centeroids is within delta of the
                  previous average distance to the centroids; i.e,
                  prevavgdist*(1-delta) <= current avgdist <= prevavgdist
        maxiter : maximum iterations of the algorithm if the
                  delta cutoff isn't reached
        metric  : cityblock = l1 norm or manhattan distance

    Outputs:
        centroids : k x features; the final centroids
        xlbl      : codebook; maps each feature in data to its nearest centroid, k
    """
    N, dim = data.shape
    k = centers.shape[0]
    all_obs = np.arange(N)
    prevdist = 0
    for jth_iter in range(maxiter):
        D = scipy.spatial.distance.cdist(data, centers, metric=metric)
        xlbl = D.argmin(axis=1)
        distances = D[all_obs, xlbl]
        avdist = distances.mean()

        if ((1-delta)*prevdist <= avdist <= prevdist) or (jth_iter == maxiter):
            break

        prevdist = avdist

        # Update centers
        for jc in range(k):
            c = np.where(xlbl == jc)[0]
            if len(c) > 0:
                centers[jc] = data[c].mean(axis=0)
    return centers, xlbl, jth_iter


# for computing the distance between points.  Periodi
# Tim Tyree
# 6.8.2021

import numpy as np
from numba import njit, jit, float32

#############################################################
# Example Usage: L2 Distance using Periodic Boundary Conditions
# distance_L2_pbc = get_distance_L2_pbc(width=200,height=200)
# distance_L2_pbc(np.array([1.,1.]),np.array([199.,199.]))
#############################################################

@njit
def pbc_2D(x,y, width, height):
	'''
	(x, y) coordinates that go from 0 to width or height, respectively.
	tight boundary rounding is in use.'''
	if ( x < 0  ):				# // Left P.B.C.
		x = width - 1
	elif ( x > (width - 1) ):	# // Right P.B.C.
		x = 0
	if( y < 0 ):				# //  Bottom P.B.C.
		y = height - 1
	elif ( y > (height - 1)):	# // Top P.B.C.
		y = 0
	return x,y

def get_distance_L2_pbc(width=1,height=1):
    '''returns a function for the euclidean (L2) distance measure for a 2D rectangle with periodic boundary conditions.
    width, height are the shape of that 2D rectangle.'''
    @jit('f8(f8[:],f8[:])', nopython=True)
    def distance_L2_pbc(point_1, point_2):
        '''assumes getting shortest distance between two points with periodic boundary conditions in 2D.  point_1 and point_2 are iterables of length 2'''
        mesh_shape=np.array((width,height))
        dq2 = 0.
        #     for q1, q2, width in zip(point_1[:2], point_2[:2], mesh_shape):
        for q1, q2, wid in zip(point_1, point_2, mesh_shape):
            dq2 = dq2 + min(((q2 - q1)**2, (q2 + wid - q1 )**2, (q2 - wid - q1 )**2))
        return np.sqrt(dq2)
    return distance_L2_pbc


import numpy as np
from matplotlib import pyplot as plt
from optsvm.svm import SVM

x_neg = np.array([[3, 4], [1, 4], [2, 3]])
y_neg = np.array([-1, -1, -1])
x_pos = np.array([[6, -1], [7, -1], [5, -3], [2, 4]])
y_pos = np.array([1, 1, 1, 1])
x1 = np.linspace(-10, 10)
x = np.vstack((np.linspace(-10, 10), np.linspace(-10, 10)))

fig = plt.figure(figsize=(10, 10))
plt.scatter(x_neg[:, 0], x_neg[:, 1], marker="x", color="r", label="Negative -1")
plt.scatter(x_pos[:, 0], x_pos[:, 1], marker="o", color="b", label="Positive +1")
plt.plot(x1, x1 - 3, color="darkblue", alpha=0.6, label="Previous boundary")
plt.xlim(0, 10)
plt.ylim(-5, 5)
plt.xticks(np.arange(0, 10, step=1))
plt.yticks(np.arange(-5, 5, step=1))

# Lines
plt.axvline(0, color="black", alpha=0.5)
plt.axhline(0, color="black", alpha=0.5)

plt.xlabel("$x_1$")
plt.ylabel("$x_2$")

plt.legend(loc="lower right")
plt.show()

X = np.vstack((x_pos, x_neg))
y = np.concatenate((y_pos, y_neg))
svm = SVM(C=10)

svm.fit(X, y)

print("---Our results")
print("w = ", svm.w_.flatten())
print("b = ", svm.b_)

from sklearn.svm import SVC

clf = SVC(C=100, kernel="linear")
clf.fit(X, y.ravel())

print("---SVM library")
print("w = ", clf.coef_)
print("b = ", clf.intercept_)


import numpy as np
from scipy.sparse import csc_matrix, diags, vstack, hstack
from scipy.sparse.linalg import inv, spsolve

def simplex(A, c, b):

    nr = len(b)

    I = diags(np.ones(nr))

    # form tableau blocks
    t1 = vstack((c, A))
    t2 = vstack((np.zeros((1, nr)), I))
    t3 = vstack(())

def interior_point(A, c, x0, tol=1e-6, max_iter=100):
    """
    Interior point solver for linear systems
    Source: Computational methods for electric power systems (3rd Ed.) - Mariesa L- Crow  (CRC Press)
    Args:
        A: Restrictions matrix
        c: Objective function coefficients
        x0: Feasible solution
        tol: Tolerance
        max_iter: Maximum number of iterations
    Returns: Optimal solution x
    """
    n = len(c)

    e = np.ones(n)

    I = diags(e)

    x = x0.copy()

    x_prev = x0.copy()

    converged = False

    iterations = 0

    last_f = 1e20

    while not converged and iterations < max_iter:

        D = diags(x)

        Ap = A * D

        cp = D * c

        # Pp = I - Ap.transpose() * inv(Ap * Ap.transpose()) * Ap

        Pp = I - Ap.transpose() * spsolve(Ap * Ap.transpose(), Ap)  # avoid using matrix inversions...

        p0 = - Pp * cp

        alpha = 0.9

        theta = - p0.min()

        xp = e + (alpha / theta) * p0

        x = D * xp

        f = c.dot(x)

        # back track
        b_iter = 0
        while f > last_f and b_iter < 10:

            alpha *= 0.25

            xp = e + (alpha / theta) * p0

            x = D * xp

            f = c.dot(x)

            b_iter += 1

            print('\talpha:', alpha)

        error = np.linalg.norm(x - x_prev, np.inf)

        converged = error < tol

        x_prev = x.copy()

        last_f = f

        iterations += 1

        print('f:', f, '\terror:', error)

    return x

if __name__ == '__main__':

    """
    Problem:
    min: -6x1 - 14x2 + 0x3 + 0x4 + 0x5

    s.t:
    2x1 + 1x2 + 1x3          = 12
    2x1 + 3x2 +     1x4      = 15
    1x1 + 7x2 +          1x5 = 21

    x1>=0, x2>=0, x3>=0, x4>=0, x5>=0
    """

    c_ = np.array([-6, -14, 0, 0, 0])

    A_ = csc_matrix([[2, 1, 1, 0, 0],
                     [2, 3, 0, 1, 0],
                     [1, 7, 0, 0, 1]])

    b_ = np.array([12, 15, 21])

    x_0 = np.array([1, 1, 9, 10, 13])

    print('x0', x_0)

    # solve
    x = interior_point(A=A_, c=c_, x0=x_0, tol=1e-8)

    print()
    print('Solution:', x)
    print('Constraints mismatch:', A_ * x - b_)


# Author: Sylvain Lapeyrade (sylla801)
# File: TSKS11-Lab4: Task 2-3

import numpy as np
from scipy import sparse
import pandas as pd
import matplotlib.pyplot as plt

# Link with the path of the txt files
link0 = './task2/mini-network.edgelist.txt'
link1 = './task2/poisson.edgelist.txt'
link2 = './task2/poisson-R.edgelist.txt'
link3 = './task2/collaboration.edgelist.txt'
link4 = './task2/collaboration-R.edgelist.txt'
link5 = './task3/n1.edgelist.txt'
link6 = './task3/n2.edgelist.txt'
link7 = './task3/n3.edgelist.txt'
link8 = './task3/n4.edgelist.txt'
link9 = './task3/n5.edgelist.txt'

link_name = link4  # Name of current path
link_file = open(link_name, 'r')

edgelist = np.loadtxt(link_file, dtype=int)  # Loads all links
nr_nodes = max(map(max, edgelist))+1  # Number of nodes
nr_edges = len(edgelist)  # Number of Edges

j = (edgelist[:, 0]).tolist()  # From Node Id
i = (edgelist[:, 1]).tolist()  # To Node Id

# Sparse matrix used for computation efficiency
sparse_matrix = sparse.csr_matrix(arg1=(np.ones(nr_edges), (i, j)),
                                  shape=(nr_nodes, nr_nodes))

# The network is undirected so we add the transpose of the matrix
network = sparse_matrix + sparse_matrix.transpose()

# Initinalize degree and knns with 0 vectors
degrees = np.zeros(shape=nr_nodes)
knns = np.zeros(shape=nr_nodes)

# Calculate the degree and the knn of each node
for node in range(nr_nodes):
    degrees[node] = np.sum(network[node, :])
    knn = np.sum([np.sum(network[neighbor, :])
                  for neighbor in network[:, node].nonzero()[0]])
    knns[node] = knn / degrees[node]

# Use DataFrame to link degree and knn (and do the mean)
data = pd.DataFrame({'degree': degrees, 'knn': knns})
means = data.groupby('degree').mean()

# *** Coefficient Correlation ***
# Initilise 2 vectors x, y with 0
x, y = np.zeros(shape=(nr_edges)), np.zeros(shape=(nr_edges))

# Assign those vectors with the node degree
for i, edge in enumerate(edgelist):
    x[i], y[i] = degrees[edge[0]], degrees[edge[1]]

# Substract x and y with their mean
xm, ym = x - x.mean(), y - y.mean()

# Pearson Correlation Coefficient implementation
r = np.add.reduce(xm * ym) / np.sqrt(np.sum(xm**2) * np.sum(ym**2))
r = max(min(r, 1.0), -1.0)  # In case abs(r) > 1

print('Network:', link_name)
print('Correlation Coefficient r=', r)

# Plot of the KNNs and their Degree
plt.ylabel("KNN")
plt.xlabel("Degree")
plt.plot(means.index, means['knn'])
plt.show()

# *** TASK 2 ***
# Poisson & Collaboration Graphs hae undergone re-wiring through
#  Degree Preserving Randomization to assess whether the variations
#  in the graphs could simply be artifacts of the graph's inherent structural
#  properties rather than properties unique to the nodes

# Poisson = -0.0028966
# Poisson rewired = -0.00329
# The poisson network was pretty balanced so
#  the rewiring doesnt affect it much
# => This is due to the graph

# Collaboration = 0.134
# Collaboration rewired = -0.005783
# The collaboration network on the other hand is more affected,
#  while there is still some peaks it is more evened out
# => This is due to the nodes

# *** TASK 3 ***
# The Correlation Coefficient are very similar (i.e the knn and Degree also).
#  We can asssume that under Degree Preserving Randomization rewiring
#  we would obtain more or less the sames graphs. So we can deduce that the
#  graphs  share same properies. (Scale free ?)


#coding:utf-8
import numpy as np
from pylab import *

"""
カーネル回帰：カーネル法を利用した曲線フィッティング
"""

N = 10
BETA = 1

# カーネル関数
def kernel(x1, x2):
    return np.exp(-BETA * (x1-x2)**2)

def y(x, xlist, alpha):
    summation = 0
    for i in range(len(xlist)):
        summation += alpha[i] * kernel(xlist[i], x)
    return summation

if __name__ == "__main__":
    # 訓練データを作成
    xlist = np.linspace(0, 1, N)
    tlist = np.sin(2 * np.pi * xlist) + np.random.normal(0, 0.2, xlist.size)

    # 行列Kを作成
    K = zeros((N, N))
    for i in range(N):
        for j in range(N):
            K[i, j] = kernel(xlist[j], xlist[i])

    # 重み係数αを計算
    alpha = np.dot(np.linalg.inv(K), tlist)
    print alpha

    # 連続関数のプロット用X値
    xs = np.linspace(0, 1, 1000)
    ideal = np.sin(2 * np.pi * xs)    # 理想曲線
    model = [y(x, xlist, alpha) for x in xs]

    # グラフを描画
    plot(xlist, tlist, 'bo')  # 訓練データ
    plot(xs, ideal, 'g-')     # 理想曲線
    plot(xs, model, 'r-')     # 推定モデル
    xlim(0.0, 1.0)
    ylim(-1.5, 1.5)
    show()


import numpy
from sklearn.linear_model import LinearRegression

def clean_list(list_):
    '''
    This function clean the list from html format
    '''
    result=[]
    for item in list_:
        v1=item.find('>')
        v2=item.find('<',2)
        result.append(item[v1+1:v2])
    return result

def clean_list_url(list_):
    '''
    This function clean the list to to get the https
    '''
    result=[]
    for item in list_:
        v1=item.find('href=')
        v2=item.find('>')
        item=item[v1+5:v2]
        item=item.replace("\"","")
        result.append(item)
    return result

def generate_b1_b0(dataframe,column_x,column_y):
    '''
    This function generates the constant and coefficient from a linear regression of two columns x and y.
    '''
    try:
        y=dataframe[column_y][~dataframe[column_y].isna()]
        x=dataframe[column_x][~dataframe[column_y].isna()]
        x=numpy.array(x).reshape((-1, 1))
        y=numpy.array(y)
        model = LinearRegression()

        model.fit(x, y)
        model = LinearRegression().fit(x, y)
        return [model.coef_[0],model.intercept_]
    except:
        print(f'There were problems with {column_x} and {column_y}')
        return[numpy.nan,numpy.nan]

def fill_nas(vector_y,vector_x,b1_b0):
    '''
    This function replace NAs with a predicted value greater or equal than zero
    '''
    try:
        result=vector_y.copy()
        cl=vector_y.isna()
        result[cl]=b1_b0[0]*vector_x+b1_b0[1]
        result[result<0]=0

        return result
    except:
        print(f'It didnt work for {vector_y} and {vector_x}, b1= {b1_b0[0]}')


import numpy as np
from matplotlib import pyplot as plt
import pandas as pd

# Predicting Final Scores based on Number of Hours Study

def loadData(file_name = ''):
	data = pd.read_csv(file_name)
	return data

def fitData(data):
	x = data['Hour']
	y = data['Score']
	x_b = np.c_[np.ones((100, 1)), x]
	return x_b, y.T

def NormalEquation(x, y):

	# theta = (x^T.x).x^T.y
	theta_best = np.linalg.inv(x.T.dot(x)).dot(x.T).dot(y)
	return theta_best

def predict(m, x, b):
	# y = m*x + b
	# x = Number of Hours Study
	y = m*x + b
	return y

def run():
	if __name__ == '__main__':
		data = loadData('data.csv')
		x, y = fitData(data)
		m, b = NormalEquation(x, y)
		hours_of_study = input('Enter the Numbers of Hours Study: ')
		y = predict(b, int(hours_of_study), m)
		print('Predicted Test Scores: ', y)

run()


"""
Simulation for problem 09 of SP1.
"""
import math

import matplotlib.pyplot as plt
import numpy as np

g = 9.8  # Gravitational constant
r = 0.24 * 10 ** -6  # Radius of the resin-spheres
V = math.pi * (4 / 3) * r ** 3  # Volume of the resin-spheres
T = 300.2  # Kelvin

density_resin = 1.2 * 10 ** 3
density_water = 1.0 * 10 ** 3

depths = np.array([0, 18, 33, 46, 57, 71]) * 10 ** -6
spheres = np.array([131, 214, 312, 437, 625, 877])
log_spheres = np.log(spheres)

# noinspection PyTupleAssignmentBalance
fit, cov = np.polyfit(depths, log_spheres, 1, cov=True)
err = np.sqrt(np.diag(cov))

k = (density_resin - density_water) * g * V / (fit[0] * T)

print(f'k = {k:.2e}')
print(f'f(d) = {fit[0]:.2e}±{err[0]:.1e}d + {fit[1]:.2e}±{err[1]:.1e}')

fig, axs = plt.subplots(2, 2, sharex='col')
axs[0, 0].set_title('Original data')
axs[0, 0].plot(depths, spheres)
axs[0, 0].set_ylabel('# spheres')

axs[0, 1].set_title('Original data scaled')
axs[0, 1].plot(depths, log_spheres)
axs[0, 1].set_ylabel('log(# spheres)')

axs[1, 0].set_title('Linear fit on scaled data')
axs[1, 0].scatter(depths, log_spheres)
axs[1, 0].plot(depths, fit[0] * depths + fit[1])
axs[1, 0].set_xlabel('depth [m]')
axs[1, 0].set_ylabel('log(# spheres)')

axs[1, 1].set_title('Fit compared to original data')
axs[1, 1].scatter(depths, spheres)
axs[1, 1].plot(depths, math.e ** (fit[0] * depths + fit[1]))
axs[1, 1].set_xlabel('depth [m]')
axs[1, 1].set_ylabel('# spheres')

plt.savefig('export/problem09.svg')

plt.show()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress

def draw_plot():

    df = pd.read_csv("epa-sea-level.csv")  # Read data from file

    # Create scatter plot
    fig, ax = plt.subplots(figsize=(6, 6))
    ax.scatter(df["Year"], df["CSIRO Adjusted Sea Level"],
               marker='.', color='c', label="Data")

    # Create first line of best fit
    lreg1 = linregress(df["Year"], df["CSIRO Adjusted Sea Level"])
    x1 = np.array(list(range(1880, 2051)))
    ax.plot(x1, lreg1.intercept + lreg1.slope * x1, 'g',
            label="Linear trend from $1880$-$2013$")
    print(f"\nLinear regression from 1880-2013:\n{lreg1}")

    # Create second line of best fit
    lreg2 = linregress(df[df["Year"] >= 2000]["Year"],
                       df[df["Year"] >= 2000]["CSIRO Adjusted Sea Level"])
    x2 = np.array(list(range(2000, 2051)))
    ax.plot(x2, lreg2.intercept + lreg2.slope * x2, 'r',
            label="Linear trend from $2000$-$2013$")
    print(f"\nLinear regression from 2000-2013:\n{lreg2}")

    # Add labels and title
    ax.set(title="Rise in Sea Level (Extrapolated to $2050$)",
           xlabel="Year",
           ylabel="Sea Level (inches)",
           xlim=(1875, 2060))

    ax.legend()

    # Save plot and return data for testing (DO NOT MODIFY)
    plt.savefig('sea_level_plot.png')

    return ax

if __name__ == "__main__":
    draw_plot()


import matplotlib.pyplot as plt
import numpy
import pandas
import sklearn.cluster as cluster
import sklearn.metrics as metrics

trainData = pandas.read_excel('C:\\Users\\minlam\\Documents\\IIT\\Machine Learning\\Data\\MVN100.xlsx',
                              sheet_name = 'MVN100', usecols = ['X', 'Y'])

nObs = trainData.shape[0]

# Determine the number of clusters
maxNClusters = 10
nClusters = numpy.zeros(maxNClusters)
Elbow = numpy.zeros(maxNClusters)
Silhouette = numpy.zeros(maxNClusters)
TotalWCSS = numpy.zeros(maxNClusters)
Inertia = numpy.zeros(maxNClusters)

for c in range(maxNClusters):
   KClusters = c + 1
   nClusters[c] = KClusters

   kmeans = cluster.KMeans(n_clusters=KClusters, random_state=20191106).fit(trainData)

   # The Inertia value is the within cluster sum of squares deviation from the centroid
   Inertia[c] = kmeans.inertia_

   if (1 < KClusters):
       Silhouette[c] = metrics.silhouette_score(trainData, kmeans.labels_)
   else:
       Silhouette[c] = numpy.NaN

   WCSS = numpy.zeros(KClusters)
   nC = numpy.zeros(KClusters)

   for i in range(nObs):
      k = kmeans.labels_[i]
      nC[k] += 1
      diff = trainData.iloc[i] - kmeans.cluster_centers_[k]
      WCSS[k] += diff.dot(diff)

   Elbow[c] = 0
   for k in range(KClusters):
      Elbow[c] += WCSS[k] / nC[k]
      TotalWCSS[c] += WCSS[k]

print("N Clusters\t Inertia\t Total WCSS\t Elbow Value\t Silhouette Value:")
for c in range(maxNClusters):
   print('{:.0f} \t {:.4f} \t {:.4f} \t {:.4f} \t {:.4f}'
         .format(nClusters[c], Inertia[c], TotalWCSS[c], Elbow[c], Silhouette[c]))

plt.plot(nClusters, Elbow, linewidth = 2, marker = 'o')
plt.grid(True)
plt.xlabel("Number of Clusters")
plt.ylabel("Elbow Value")
plt.xticks(numpy.arange(1, maxNClusters+1, step = 1))
plt.show()

plt.plot(nClusters, Silhouette, linewidth = 2, marker = 'o')
plt.grid(True)
plt.xlabel("Number of Clusters")
plt.ylabel("Silhouette Value")
plt.xticks(numpy.arange(1, maxNClusters+1, step = 1))
plt.show()

# Fit the 2 cluster solution
kmeans = cluster.KMeans(n_clusters=2, random_state=20191106).fit(trainData)
centroid = kmeans.cluster_centers_
print('Centroids:\n', centroid)

trainData['Cluster ID'] = kmeans.labels_

carray = ['red', 'green']
plt.figure(figsize=(10,10))
for i in range(2):
    subData = trainData[trainData['Cluster ID'] == i]
    plt.scatter(x = subData['X'],
                y = subData['Y'], c = carray[i], label = i, s = 25)
plt.scatter(x = centroid[:,0], y = centroid[:,1], c = 'black', marker = 'X', s = 100)
plt.grid(True)
plt.title('2-KMeans Clustering')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend(title = 'Cluster ID', loc = 'best', bbox_to_anchor = (1, 1), fontsize = 14)
plt.show()


import numpy as np

def make_features_from_dt(df, cleanup=True):
    yeardays = max(df.datetime.dt.dayofyear.max(), 365)
    dayminutes = 24 * 60
    doy_arg = 2 * np.pi * df.datetime.dt.dayofyear / yeardays
    mod_arg = 2 * np.pi * (df.datetime.dt.hour * 60 + df.datetime.dt.minute) / dayminutes
    df['sin_doy'] = np.sin(doy_arg)
    df['cos_doy'] = np.cos(doy_arg)
    df['sin_minofday'] = np.sin(mod_arg)
    df['cos_minofday'] = np.cos(mod_arg)
    if cleanup:
        df.drop('datetime', axis=1, inplace=True)

def make_features_from_latlon(df, cleanup=True):
    df['x'] = np.cos(np.deg2rad(df.lat)) * np.cos(np.deg2rad(df.lon))
    df['y'] = np.cos(np.deg2rad(df.lat)) * np.sin(np.deg2rad(df.lon))
    df['z'] = np.sin(np.deg2rad(df.lat))
    if cleanup:
        df.drop(['lat', 'lon'], axis=1, inplace=True)

def log_transform_feature(df, feature, offset=1e-6, cleanup=True):
    try:
        df['log10_' + feature] = np.log10(df[feature]+offset)
    except TypeError:
        for feature_i in feature:
            df['log10_' + feature_i] = np.log10(df[feature_i]+offset)
    if cleanup:
        df.drop(feature, axis=1, inplace=True)


import numpy as np

def logdet(covariance):
    """
    This should be equivalent to the following:

    >>> covariance_logdet = numpy.linalg.slogdet(covariance)[1]

    so feel free to use either
    """
    L = np.linalg.cholesky(covariance)
    covariance_logdet = 2*np.sum(np.log(np.diagonal(L)))
    return covariance_logdet


import numpy as np
import matplotlib.pyplot as plt

class LinearRegression:
    """
    Architecture Constants:
        N: Total number of input samples of matrix X.
        D: Number of input features of matrix X.

    Hyperparameters:
        epochs: Number of training iterations.
        learn_rate: Rate of change used in gradient descent.
        reg_L1: Sets the penalty for L1 (Lasso) regularization.
        reg_L2: Sets the penalty for L2 (Ridge) regularization.

    Numpy Matrices:
        X [N x D]: Input samples and features.
        T [N x 1]: Output targets.
        Y [N x 1]: Output predictions.
        w [D x 1]: Weights for each input feature.

    To implement the LinearRegression class, insert code that looks
    something like the following:

        from ml_models import LinearRegression

        X = ...
        T = ...

        model = LinearRegression(X=X,
                                 T=T,
                                 epochs=300,
                                 learn_rate=0.001,
                                 reg_L1=0,
                                 reg_L2=0)

        for i in xrange(model.epochs):

            model.make_predictions()
            model.calculate_error()
            model.update_weights()

            if i % 10 == 0:
                print "Iteration: " + str(i) + "   Error:", model.errors[i]

        model.calculate_R2()

        model.plot_errors()
        model.plot_predictions()
    """

    def __init__(self, X=None, T=None, epochs=1000, learn_rate=0.001,
                 reg_L1=0, reg_L2=0):
        """
        Sets the input data X and targets T, sets the number of samples
        N and the number of features D, initalizes an empty list to
        store errors, sets the hyperparameters, and calls the init_weights
        method to initialize the weights.
        """

        self.X = X
        self.T = T
        self.N, self.D = self.X.shape

        self.errors = []

        self.epochs = epochs
        self.learn_rate = learn_rate
        self.reg_L1 = reg_L1
        self.reg_L2 = reg_L2

        self.init_weights()

    def init_weights(self):
        """
        Initializes the weights by randomly sampling from a normal
        distribution with mean equal to zero and standard deviation
        equal to 1 / sqrt(# of features).
        """

        self.w = np.random.randn(self.D) / np.sqrt(self.D)

    def make_predictions(self):
        """
        Makes predictions Y by taking the dot product of input matrix X
        and weight vector w.
        """

        self.Y = self.X.dot(self.w)

    def calculate_error(self):
        """
        Calculates the mean squared error between predictions Y and
        targets T according to the following equation:

            error = (Y - T)**2 / N

        Then, the error is formatted to two significant figures and
        appended to a list called 'self.errors' to store the errors
        over iterations for a plot.
        """

        delta = self.Y - self.T
        error = delta.dot(delta) / self.N
        error = format(error, '.5f')

        self.errors.append(error)

    def update_weights(self):
        """
        The model takes a gradient descent approach to optimizing the
        weights. The derivative of the cost function with respect to the
        weights dJ/dw is the transpose of X times the difference between
        targets T and predictions Y:

            [1]:   self.X.T.dot(self.T - self.Y)

        L1 (Lasso) regularization encourages sparsity of weights and
        helps to prevent overfitting. This is done by subtracting the
        sign of weights times the L1 regularization constant from the
        derivative of the cost function:

            [2]:   [1] - self.reg_L1 * np.sign(self.w)

        L2 (Ridge) regularization encourages small weights for all
        weights and also helps to prevent overfitting. This is done by
        subtracting the L2 regularization times two times the weights
        from the derivative of the cost function.

            [3]:   [2] - self.reg_L2 * 2*self.w

        Finally, the weights are updated by adding to each weight the
        learning rate times the derivative of the cost function minus
        the regularization terms:

            [4]:   self.w += self.learning_rate * [3]
        """

        self.w += self.learn_rate * (self.X.T.dot(self.T - self.Y)
                                     - self.reg_L1 * np.sign(self.w)
                                     - self.reg_L2 * 2*self.w)

    def calculate_R2(self):
        """
        Calculates the R-squared correlation between targets T and
        predictions Y to measure the fit of the model.
        """

        d1 = self.T - self.Y
        d2 = self.T - self.T.mean()

        self.r2 = 1 - d1.dot(d1) / d2.dot(d2)
        self.r2 = format(self.r2, '.3f')

        print ""
        print "R2:", self.r2

    def plot_errors(self):
        """
        Plots the MSE (mean squared error) against iteration i.
        """

        plt.title("Prediction Error")
        plt.plot(self.errors)
        plt.ylabel("MSE (Mean Squared Error)")
        plt.xlabel("Iteration")
        plt.show()

    def plot_predictions(self):
        """
        Plots the targets T and predictions Y to visualize how good of
        a fit the model is.
        """

        plt.title("Targets vs. Predictions")
        plt.plot(self.T, label="Targets")
        plt.plot(self.Y, label="Predictions")
        plt.xlabel("Sample number")
        plt.legend()
        plt.show()


import numpy as np
import matplotlib.pyplot as plt
from hyperopt import hp
from hyperopt.pyll.stochastic import sample
from hyperopt import rand, tpe
from hyperopt import Trials
from hyperopt import fmin

def objective_function(x):
    """Objective function to minimize, for example
    we have a polynomial 2(x^4)-2(x^3)-200(x^2)-5(x)-200"""

    ## Optimization Function
    # simple polynomial function
    f = np.poly1d([2,-2,-200,-5,-200])

    # returning a scaled value for the polynomial for certain value of x
    return f(x) * 0.05

# This part is to visualize the minimum
x = np.linspace(-10, 10, 10000)
y = objective_function(x)

# get the min value of y and corresponding x value
miny = min(y)
minx = x[np.argmin(y)]

# Visualize the function
plt.figure(figsize = (8, 6))
plt.style.use('fivethirtyeight')
plt.title('Objective Function')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.vlines(minx, min(y)- 50, max(y), linestyles = '-', colors = 'r')
plt.plot(x, y)

# Print out the minimum of the function and value
print('Minimum of %0.4f occurs at %0.4f' % (miny, minx))

# ------------------------

## Domain Space
space = hp.uniform('x', -10, 10)

samples = []

# Sample 10000 random values from the range, this is not need for the algorithm we just do this to visualize it.
for _ in range(10000):
    samples.append(sample(space))

# Histogram of the values
plt.hist(samples, bins = 30, edgecolor = 'black');
plt.xlabel('Domain Space Values'); plt.ylabel('Frequency of thr Values'); plt.title('Domain Space');

# Create objects for the algorithms
tpe_algo = tpe.suggest # Tree Structure Parzen Estimator
rand_algo = rand.suggest # Random Forest

# Create two trials objects to record your estimations
tpe_trials = Trials()
rand_trials = Trials()

# Run 2000 trials with the tpe algorithm with our objective function and domain space
tpe_best = fmin(fn=objective_function, space=space, algo=tpe_algo, trials=tpe_trials,
                max_evals=2000, rstate= np.random.RandomState(50))

# Run 2000 trials with the random algorithm
rand_best = fmin(fn=objective_function, space=space, algo=rand_algo, trials=rand_trials,
                 max_evals=2000, rstate= np.random.RandomState(50))

# Printing info on the trials
print('Minimum for TPE:        {:.4f}'.format(tpe_trials.best_trial['result']['loss']))
print('Minimum for random:     {:.4f}'.format(rand_trials.best_trial['result']['loss']))
print('Actual minimum of f(x): {:.4f}'.format(miny))

# Print out information about number of trials
print('\nNumber of trials needed to attain minimum with TPE:    {}'.format(tpe_trials.best_trial['misc']['idxs']['x'][0]))
print('Number of trials needed to attain minimum with random: {}'.format(rand_trials.best_trial['misc']['idxs']['x'][0]))

# Print out information about value of x
print('\nBest value of x from TPE:    {:.4f}'.format(tpe_best['x']))
print('Best value of x from random: {:.4f}'.format(rand_best['x']))
print('Actual best value of x:      {:.4f}'.format(minx))

# ---------------------------------------------------
# This is a more practical example of using hyperopt
# ---------------------------------------------------

import io
import requests
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import mean_squared_error
from math import sqrt

url="https://raw.githubusercontent.com/S-Mann/hyperparameter_optimization/master/dataset/dataset.csv"
s=requests.get(url).content
dataset=pd.read_csv(io.StringIO(s.decode('utf-8')))

excluded_columns = [x not in ['model','msrp','cost_per_unit','profit_per_unit'] for x in dataset.columns]

X = dataset.iloc[:,excluded_columns].values
y = dataset.iloc[:,dataset.columns == 'msrp'].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

# Create our imputer to replace missing values with the mean e.g.
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp = imp.fit(X_train)
X_train = imp.transform(X_train)
imp = imp.fit(X_test)
X_test = imp.transform(X_test)

scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

def knn_obj_func(n):
    classifier = KNeighborsClassifier(n_neighbors=int(n))
    classifier.fit(X_train, np.ravel(y_train, order='C'))

    y_pred = classifier.predict(X_test)

    rmse = sqrt(mean_squared_error(y_test, y_pred))
    # with k = 5 , rmse = 40507.76274692575
    return rmse

# This is a random k value and its RMSE
random_hyperparam = 7
random_loss = knn_obj_func(7)

space = hp.uniform('x', 2, 1000)
samples = []

for _ in range(30):
    samples.append(sample(space))

# Create objects for the algorithms
tpe_algo = tpe.suggest # Tree Structure Parzen Estimator
rand_algo = rand.suggest # Random Forest

# Create two trials objects to record your estimations
tpe_trials = Trials()
rand_trials = Trials()

# Run 2000 trials with the tpe algorithm with our objective function and domain space
tpe_best = fmin(fn=knn_obj_func, space=space, algo=tpe_algo, trials=tpe_trials, max_evals=100, rstate= np.random.RandomState(50))

# Run 2000 trials with the random algorithm with our objective function and domain space
rand_best = fmin(fn=knn_obj_func, space=space, algo=rand_algo, trials=rand_trials, max_evals=100, rstate= np.random.RandomState(50))

# Printing info on the trials
print('Minimum loss for TPE:        {:.4f}'.format(tpe_trials.best_trial['result']['loss']))
print('Minimum loss for random:     {:.4f}'.format(rand_trials.best_trial['result']['loss']))
print('Default k\'s loss:            {:.4f}'.format(random_loss))

# Print out information about number of trials
print('\nNumber of trials needed to attain minimum with TPE:     {}'.format(tpe_trials.best_trial['misc']['idxs']['x'][0]))
print('Number of trials needed to attain minimum with random:  {}'.format(rand_trials.best_trial['misc']['idxs']['x'][0]))

# Print out information about value of x
print('\nBest value of k from TPE:    {:.0f}'.format(tpe_best['x']))
print('Best value of k from random: {:.0f}'.format(rand_best['x']))
print('Default value of k:          {:.0f}'.format(random_hyperparam))

plt.bar(['Default','TPE','Random Forest'],
        [random_loss,tpe_trials.best_trial['result']['loss'],rand_trials.best_trial['result']['loss']],
        edgecolor='black')
plt.xlabel('Algorithms')
plt.ylabel('RMSE')
plt.title('Loss for each Approach(Lower is Better)')
plt.axhline(y=tpe_trials.best_trial['result']['loss'], color = 'r')
plt.plot()


# Calculate terminal velocity, ut, from drag coefficient, Cd, and particle sphericity
# Cd from Ganser1993, Eq 18
# Cd also referenced in Cui2007, Eq 2
# Cd also referenced in Chhabra1999, Eq 6

# according to Chhabra1999, the Ganser1993 Cd correlation is applicable for
# sphericity values from 0.09 to 1.0

# use Python 3 print function and division
from __future__ import print_function
from __future__ import division

# libraries
import numpy as np

#---- Parameters

# air properties at T = 300K, P = 1 atm
rhog = 1.17     # density (kg/m^3)
ug = 1.85e-5    # dynamic viscosity (kg/ms)
g = 9.81        # gravity (m/s^2)

# particle properties
dp = 0.000207   # diameter of particle (m)
rhos = 2500     # density of particle (kg/m^3)
sp = 0.8        # sphericity of the particle, perfect sphere = 1.0

#---- Calculations

# papers Cui2007 and Chhabra1999 leave out the -2.25*dv/D term
K1 = (1/3 + 2/3*(sp**-0.5))**(-1)           # Stokes' shape factor
K2 = 10**(1.8148*((-np.log(sp))**0.5743))   # Newton's shape factor

# guess range for terminal velocity, ut (m/s)
ut = np.arange(0.001, 20, 0.001)

# Re, Reynolds number (-)
# Cd, Ganser1993, drag coefficient as function of Re, K1, K2 (-)
# Cdd, drag coefficient as function of ut, etc. (-)
Re = (dp*rhog*ut)/ug
Cd = (24/(Re*K1))*(1 + 0.1118*((Re*K1*K2)**0.6567)) + (0.4305*K2)/(1+(3305/(Re*K1*K2)))
Cdd = (4*g*dp*(rhos-rhog))/(3*(ut**2)*rhog)

delta = np.abs(Cd-Cdd)  # compare difference between Cd and Cdd
idx = np.argmin(delta)  # find index of minimum value in delta

# print results to console
print('--- Ganser1993 ---')
print('ut =', ut[idx])
print('Re =', Re[idx])
print('Cd =', Cd[idx])
print('Cdd =', Cdd[idx])
print('delta =', delta[idx])


from scipy import stats
import numpy as np
from scipy.optimize import minimize
import pylab as plt

latencies = np.loadtxt('latencies.txt')

cc, bbRaw = np.histogram(latencies,bins=50,density=True)
bb = (bbRaw[:-1]+bbRaw[1:])/2.

#ydata = np.array([0.1,0.15,0.2,0.3,0.7,0.8,0.9, 0.9, 0.95])
#xdata = np.array(range(0,len(ydata),1))

def singleExponential(xdata,k):
    return np.exp(-xdata/k)

def doubleExponential(xdata,k1,k2,theta):
    return theta*np.exp(-xdata/k1) + (1.-theta)*np.exp(-xdata/k2)

def singleExponentialLLE(params):
    k = params[0]
    #x0 = params[1]
    sd = params[1]

    yPred = singleExponential(bb,k)
    #yPred = 1 / (1+ np.exp(-k*(xdata-x0)))

    # Calculate negative log likelihood
    LL = -np.sum( stats.norm.logpdf(cc, loc=yPred, scale=sd ) )

    return(LL)

def doubleExponentialLLE(params):
    k1 = params[0]
    k2 = params[1]
    theta = params[2]
    sd = params[3]

    yPred = doubleExponential(bb,k1,k2,theta)
    #yPred = 1 / (1+ np.exp(-k*(xdata-x0)))

    # Calculate negative log likelihood
    LL = -np.sum( stats.norm.logpdf(cc, loc=yPred, scale=sd ) )

    return(LL)

initParamsSE = [1., 0.2]

resultsSE = minimize(singleExponentialLLE, initParamsSE, method='Nelder-Mead')
print resultsSE.x
print 'single exponential :', singleExponentialLLE(resultsSE.x)

initParamsDE = [0.5, 3., 0.8, 0.1]

resultsDE = minimize(doubleExponentialLLE, initParamsDE, method='Nelder-Mead')
print resultsDE.x
print 'double exponential :', doubleExponentialLLE(resultsDE.x)

estParmsSE = resultsSE.x
yOutSE =  singleExponential(bb,estParmsSE[0]) # 1 / (1+ np.exp(-estParms[0]*(xdata-estParms[1])))

estParmsDE = resultsDE.x
yOutDE =  doubleExponential(bb,estParmsDE[0],estParmsDE[1],estParmsDE[2]) # 1 / (1+ np.exp(-estParms[0]*(xdata-estParms[1])))

plt.clf()
plt.plot(bb,cc, 'go')
plt.plot(bb, yOutSE)
plt.plot(bb, yOutDE)
#plt.hist(cc-yOutDE)
plt.show()


from sympy import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from DiffEqSolver import SolveOde

"""INITIATE THE SYMBOLS, FUNCTIONS, AND PRETTY PRINTING"""

x,y,z = symbols('x,y,z', positive = True)
init_printing(use_latex=True)
f,g = symbols('f,g', cls = Function, positive = True)

"""
    CREATE AN EXQUATION AND CONDITION LIKE THE FOLLOWING

    equation = Eq(f(x).diff(x,x,x) + f(x).diff(x),exp(x))
    conditions_dict = {f(0) :0,f(x).diff(x).subs(x,0):0, f(x).diff(x,x).subs(x,0):0}
"""

equation = Eq(f(x).diff(x,x,x) + f(x).diff(x),exp(x))
conditions_dict = {f(0) :0,f(x).diff(x).subs(x,0):0, f(x).diff(x,x).subs(x,0):0}

solve_ode = SolveOde(equation, conditions_dict)
solve_ode.compute_ode()


# type: ignore[arg-type]
from typing import Callable

import numpy as np

def simpson_13(
    func: Callable[..., float], a: float, b: float, eps: float = 0.0001, *args, **kwargs
) -> float:

    """
    Metode simpson 1/3 menggunakan
    kurva polinomial ordo 2 untuk melakukan
    approximasi terhadap fungsi yang akan
    diintegrasi

    Parameter:
    f   = fungsi input
    a   = batas bawah integrasi
    b   = batas atas integrasi
    eps = error relatif maksimal

    >>> simpson_13(lambda x: x**2, 0, 2)
    2.6666666666666665
    >>> simpson_13(lambda x: x**5, 0, 1)
    0.16666679687500002
    """
    try:
        # Iterasi pertama
        n = 10
        h = (b - a) / n
        fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
        L0 = h / 3 * (fx[0] + 4 * sum(fx[1:-1:2]) + 2 * sum(fx[2:-2:2]) + fx[-1])

        # Optimasi
        err = 1
        while err > eps:
            n *= 2
            h = (b - a) / n
            fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
            L1 = h / 3 * (fx[0] + 4 * sum(fx[1:-1:2]) + 2 * sum(fx[2:-2:2]) + fx[-1])
            err = np.abs(L1 - L0) / np.abs(L1)
            L0 = L1
    except Exception:
        raise RuntimeError("Integrasi gagal, pastikan fungsi anda benar!")
    return L1

def simpson_38(
    func: Callable[..., float], a: float, b: float, eps: float = 0.0001, *args, **kwargs
) -> float:

    """
    Metode simpson 3/8 menggunakan
    kurva polinomial ordo 3 untuk melakukan
    approximasi terhadap fungsi yang akan
    diintegrasi

    Parameter:
    f   = fungsi input
    a   = batas bawah integrasi
    b   = batas atas integrasi
    eps = error relatif maksimal

    >>> simpson_38(lambda x: x**2, 0, 2)
    2.666666666666667
    >>> simpson_38(lambda x: x**5, 0, 1)
    0.16666667809785096
    """
    try:
        # Iterasi pertama
        n = 10
        h = (b - a) / n
        fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
        L0 = (
            3
            / 8
            * h
            * (
                fx[0]
                + 3 * sum(fx[1:-2:3])
                + 3 * sum(fx[2:-1:3])
                + 2 * sum(fx[3:-3:3])
                + fx[-1]
            )
        )

        # Optimasi
        err = 1
        while err > eps:
            n *= 3
            h = (b - a) / n
            fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
            L1 = (
                3
                / 8
                * h
                * (
                    fx[0]
                    + 3 * sum(fx[1:-2:3])
                    + 3 * sum(fx[2:-1:3])
                    + 2 * sum(fx[3:-3:3])
                    + fx[-1]
                )
            )
            err = np.abs(L1 - L0) / np.abs(L1)
            L0 = L1
    except Exception:
        raise RuntimeError("Integrasi gagal, pastikan fungsi anda benar!")
    return L1

if __name__ == "__main__":

    def f(x: float) -> float:
        """
        Test Function
        """
        return x ** 4

    print(simpson_13(f, 0, 1))
    print(simpson_38(f, 0, 1))

    import doctest

    doctest.testmod()


# -*- coding: utf-8 -*-
"""
Created on Mon Jul 24 10:52:46 2017

@author: Jason Livingston
"""

import pandas as pd
import numpy as np
from sklearn import linear_model

##CLOSED FORM LINEAR REGRESSION
X = pd.DataFrame()
X['int'] = [1,1,1,1,1]
X['A'] = [1,2,3,4,5]
X['B'] = [3,4,4,6,7]

X = X.values
Y = [4,5,7,7,9]
#Y = Y.values

Xt=X.transpose()

XtX=np.dot(Xt,X)
XtY = np.dot(Xt,Y)
XtXinv = np.linalg.inv(XtX)
B = np.dot(XtXinv,XtY)

##SKLEARN LINEAR REGRESSION FOR COMPARISON
X = pd.DataFrame()
X['A'] = [1,2,3,4,5]
X['B'] = [3,4,4,6,7]

explin = linear_model.LinearRegression(fit_intercept = True)
explin.fit(X,Y)
explin.coef_

##ONLINE GRADIENT DESCENT
def predict(row,coefficients):
    row = row
    yhat = coefficients[0]
    for i in range(len(row) - 1):
        yhat = yhat+coefficients[i+1]*row[i]
    return yhat

def df_to_list_of_lists(df):
    df2 = []
    for i in range(len(df[list(df.columns.values)[0]])):
        templist = []
        for col in list(df.columns.values):
            x = df[col][i]
            templist.append(x)
        df2.append(templist)
    return df2

def coefficients_sgd(train,l_rate,n_epoch):
    if type(train) == pd.core.frame.DataFrame:
        train = df_to_list_of_lists(train)
    coefs = [0 for i in range(len(train[0]))]
    for epoch in range(n_epoch):
        for row in train:
            yhat = predict(row,coefs)
            error = yhat - row[-1]
            coefs[0] = coefs[0] - l_rate*error
            for i in range(len(row)-1):
                coefs[i+1] = coefs[i+1]-l_rate*error*row[i]
    return coefs

X = pd.DataFrame()
#X['int'] = [1,1,1,1,1]
X['A'] = [1,2,3,4,5]
X['B'] = [3,4,4,6,7]
X['Y'] = Y
coefficients = coefficients_sgd(train = X,l_rate = .001,n_epoch = 1000)
print("Intercept = %f Beta_A = %f Beta_B = %f"%tuple(coefficients) )

Linear Regression by OLGD and Closed Form.py


# -*- coding: utf-8 -*-

import numpy as np

def conv(a, b):
    N = len(a)
    M = len(b)
    YN = N + M - 1
    y = [0.0 for i in range(YN)]
    for n in range(YN):
        for m in range(M):
            if 0 <= n - m and n - m < N:
                y[n] += a[n - m] * b[m]
    return y

def conv2(a, b):
    N = len(a)
    M = len(b)
    YN = N + M - 1
    y = [0.0 for i in range(YN)]
    b = np.array(b)[::-1]       # Reverse
    for n in range(YN):
        for m in range(M):
            k = n - M + m + 1;
            if 0 <= k and k < N:
                y[n] += a[k] * b[m]
    return y

def convfft(a, b):
    N = len(a)
    M = len(b)
    YN = N + M - 1
    FFT_N = 2 ** (int(np.log2(YN)) + 1)
    print(FFT_N);
    afft = np.fft.fft(a, FFT_N)
    bfft = np.fft.fft(b, FFT_N)
    abfft = afft * bfft
    y = np.fft.ifft(abfft).real[:YN]
    return y

def time_test():
    import time
    import matplotlib.pyplot as plt

    def run(func, a, b):
        n = 1
        start = time.clock()
        for j in range(n):
            func(a, b)
        end = time.clock()
        run_time = end - start
        return run_time / n

    n_list = []
    t1_list = []
    t2_list = []
    t3_list = []
    for i in range(10):
        count = i * 1000 + 10
        print(count)
        a = np.ones(count)
        b = np.ones(count)
        t1 = run(conv, a, b)    # Direct calculation convolution
        t2 = run(conv2, a, b)
        t3 = run(convfft, a, b) # Fast convotution by FFT
        n_list.append(count)
        t1_list.append(t1)
        t2_list.append(t2)
        t3_list.append(t3)

    # plot
    plt.plot(n_list, t1_list, label='conv')
    plt.plot(n_list, t2_list, label='conv2')
    plt.plot(n_list, t3_list, label='convfft')
    plt.legend()
    plt.title(u"convolve times")
    plt.ylabel(u"run times(ms/point)")
    plt.xlabel(u"length")
    plt.show()

if __name__ == '__main__':
    a = [ 0, 1, 2, 3, 4, 5 ]
    b = [ 0, 1, 2 ]
    print(conv2(a, b))
    print(convfft(a, b))
    print(np.convolve(a, b))
    # time_test()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 10 13:18:53 2020

Copyright 2020 by Hadrien Montanelli.
"""
# Standard library imports:
import numpy as np

def mtcarlo(f, domain, N=1000):
    """Monte Carlo integrtion of a function on a rectangular domain.

    Input
    -----
    f : function
        The function to integrate.

    domain : numpy array
        A rectangluar domain as a (2*d)x1 array in dimenion d.

    N : int
        The number of sample points.

    Output
    ------
    output ; float
        The approximate value of the integral.

    Example
    -------
    This is an example in 2d.

        import numpy as np
        from learnpy.misc import montecarlo

        f = lambda x: x[0]**2*np.cos(x[1])
        dom = np.array([0, 2, -1, 1])
        I = montecarlo(f, dom)

    See also the 'example_montecarlo' file.

    """
    # Get the dimension:
    d = int(len(domain)/2)

    # Compute the volume of the domain:
    V = 1
    for j in range(d):
        V *= domain[2*j+1] - domain[2*j]

    # Sample the domain uniformly:
    points = np.zeros([N, d])
    for i in range(N):
        for j in range(d):
            a = domain[2*j]
            b = domain[2*j+1]
            points[i, j] = a + (b - a)*np.random.uniform()

    # Compute the integral:
    S = 0
    for point in points:
        S += f(point)
    I = V/N*S

    return I


import numpy as np

a = np.arange(5)
b = np.arange(5, 10)
print(a, b)

# 1.
print(np.add(a, b))

# 2.
print(np.sum([a, b]))

print(np.sum([a, b], axis=1))

print(np.sum([a, b], axis=0))

# 3.
print(np.cumsum(a))
print(np.cumsum(b))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 18 18:59:42 2021

@author: jonathan
"""

import numpy as np
from numpy.random import default_rng
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator

# compute the RODFT00 as defined in the FFTW reference manual
def rodft00(arr):
    n = len(arr)
    out = np.zeros([n])
    for k in range(n):
        for j in range(n):
            out[k] += 2.0 * arr[j] * np.sin(np.pi*(j+1)*(k+1)/(n+1))
    return out

# compute the RODFT10 as defined in the FFTW reference manual
def rodft10(arr):
    n = len(arr)
    out = np.zeros([n])
    for k in range(n):
        for j in range(n):
            out[k] += 2.0 * arr[j] * np.sin(np.pi*(j+0.5)*(k+1)/n)
    return out

# compute the RODFT01 as defined in the FFTW reference manual
def rodft01(arr):
    n = len(arr)
    out = np.zeros([n])
    for k in range(n):
        out[k] += (-1)**k * arr[-1]
        for j in range(n-1):
            out[k] += 2.0 * arr[j] * np.sin(np.pi*(j+1)*(k+0.5)/n)
    return out

# compute the RODFT11 as defined in the FFTW reference manual
def rodft11(arr):
    n = len(arr)
    out = np.zeros([n])
    for k in range(n):
        for j in range(n):
            out[k] += 2.0 * arr[j] * np.sin(np.pi*(j+0.5)*(k+0.5)/n)
    return out

# evaluate the RODFT00 at all points given in x
def eval_rodft00(arr, x):
    n = len(arr)
    y = np.zeros_like(x)
    for j in range(n):
        y += 2.0*arr[j]*np.sin(np.pi*(j+1)*(x+1/(n+1)))
    return y

# evaluate the RODFT10 at all points given in x
def eval_rodft10(arr, x):
    n = len(arr)
    y = np.zeros_like(x)
    for j in range(n):
        y += 2.0*arr[j]*np.sin(np.pi*(j+0.5)*(x+1/n))
    return y

# evaluate the RODFT01 at all points given in x
def eval_rodft01(arr, x):
    n = len(arr)
    y = np.zeros_like(x)
    for j in range(n-1):
        y += 2.0*arr[j]* np.sin(np.pi*(j+1)*(x+0.5/n))
    y += arr[-1]* np.sin(np.pi*n*(x+0.5/n))
    return y

# evaluate the RODFT11 at all points given in x
def eval_rodft11(arr, x):
    n = len(arr)
    y = np.zeros_like(x)
    for j in range(n):
        y += 2.0*arr[j]*np.sin(np.pi*(j+0.5)*(x+0.5/n))
    return y

#%% RODFT00

# input size for RODFT00
n = 3

# logical size of equivalent DFT (for RODFT00)
N = 2*(n+1)

# random Fourier coefficients
rng = default_rng(seed=42)
r2r_out = rng.uniform(size=n)

# compute input of RODFT00 by RODFT00 (the inverse of RODFT00)
r2r_in = rodft00(r2r_out)/n

# extend to left for highlighting symmetry at start of array
left_in_x = np.arange(-N+n, 0)
left_in_y = np.zeros([N-n])
for i in range(n):
    # odd symmetry around -1
    left_in_y[-i-2] = -r2r_in[i]

# extend to equivalent DFT size to highlight symmetry at end of array
full_in_x = np.arange(n, N)
full_in_y = np.zeros([N-n])
for i in range(n):
    # even symmetry around n-1
    full_in_y[i+1] = -r2r_in[-i-1]

# sample at finer intervals
nFine = 1024
x = np.linspace(-N/2, N, nFine)
y = eval_rodft00(r2r_out, x/(n+1))/n

plt.figure(figsize=(5,3))

plt.plot(x, y, '-', color='gray', linewidth=0.5)
plt.axhline(0, ls='-', color='k')
plt.axvline(0, ls='-', color='k')

# symmetry lines
plt.axvline(-1, ls='--', color='b')
plt.axvline(n, ls='--', color='b')

# highlight array contents
plt.axvspan(0-0.25, (n-1)+0.25, alpha=0.3, color='gray')

# plot actual RODFT00 input
plt.plot(r2r_in, 'bo')

# label individual points
for i in range(n):
    plt.text(i+0.2, r2r_in[i]-0.13, chr(ord("a")+i))
plt.text(-1+0.2, 0.0+0.05, "0")

plt.plot(left_in_x, left_in_y, 'bo')

# plot data extending REDFT00 to full DFT
plt.plot(full_in_x, full_in_y, 'bo')

plt.text(full_in_x[0]+0.2, full_in_y[0]+0.05, "0")
for i in range(1,n+1):
    plt.text(full_in_x[i]+0.2, full_in_y[i]+0.07, "-"+chr(ord("a")+(n-i)))
plt.text(full_in_x[-1]+0.2, full_in_y[-1]+0.05, "0")

# only integer xaxis ticks at intervals of 1
plt.gca().xaxis.set_major_locator(MaxNLocator(steps=(1,10), integer=True))
plt.xlim((-4.5, 8.5))

plt.grid(True)
plt.xlabel("j")
plt.title("RODFT00 N=%d n=%d"%(N, n))
plt.tight_layout()
plt.savefig("rodft00.png")

#%% RODFT10
n = 4

# logical size of equivalent DFT (for RODFT10)
N = 2*n

# random Fourier coefficients
rng = default_rng(seed=41)
r2r_out = rng.uniform(size=n)

# compute input of RODFT10 by using RODFT01 (the inverse of RODFT10)
r2r_in = rodft01(r2r_out)/n

# extend to left for highlighting symmetry at start of array
left_in_x = np.arange(-n, 0)
left_in_y = np.zeros([n])
for i in range(n):
    # odd symmetry around -0.5
    left_in_y[i] = -r2r_in[-i-1]

# extend to equivalent DFT size for highlighting symmetry at end of array
full_in_x = np.arange(n, N)
full_in_y = np.zeros([N-n])
for i in range(N-n):
    # odd symmetry around n-0.5
    full_in_y[i] = -r2r_in[-i-1]

# sample "inputs" at finer intervals
nFine = 1024
x = np.linspace(-n, N, nFine)
y = eval_rodft01(r2r_out, x/n)/n

plt.figure(figsize=(5,3))

plt.plot(x, y, '-', color='gray', linewidth=0.5)

plt.axhline(0, ls='-', color='k')
plt.axvline(0, ls='-', color='k')

# symmetry lines
plt.axvline(-0.5, ls='--', color='b')
plt.axvline(n-0.5, ls='--', color='b')

# highlight array contents
plt.axvspan(0-0.25, (n-1)+0.25, alpha=0.3, color='gray')

plt.plot(r2r_in, 'bo')

# label individual points
for i in range(n-1):
    plt.text(i+0.2, r2r_in[i]-0.03, chr(ord("a")+i))
plt.text(n-1-0.6, r2r_in[n-1]-0.03, chr(ord("a")+n-1))

plt.plot(left_in_x, left_in_y, 'bo')

plt.plot(full_in_x, full_in_y, 'bo')

for i in range(n):
    plt.text(full_in_x[i]+0.2, full_in_y[i]-0.03, "-"+chr(ord("a")+(n-1-i)))

# only integer xaxis ticks
plt.gca().xaxis.set_major_locator(MaxNLocator(steps=(1,10), integer=True))
plt.xlim((-4.5, 8.5))

plt.grid(True)
plt.xlabel("j")
plt.title("RODFT10 N=%d n=%d"%(N, n))
plt.tight_layout()
plt.savefig("rodft10.png")

#%% RODFT01
n = 4

# logical size of equivalent DFT (for RODFT01)
N = 2*n

# random Fourier coefficients
rng = default_rng(seed=42)
r2r_out = rng.uniform(size=n)

# compute input of RODFT01 by using RODFT10 (the inverse of RODFT01)
r2r_in = rodft10(r2r_out)/n

# extend to left for highlighting symmetry at start of array
left_in_x = np.arange(-n, 0)
left_in_y = np.zeros([n])
for i in range(n-1):
    # odd symmetry around -1
    left_in_y[-i-2] = -r2r_in[i]

# extend to equivalent DFT size for highlighting symmetry at end of array
full_in_x = np.arange(n, N)
full_in_y = np.zeros([N-n])
for i in range(N-n-1):
    # even symmetry around n-1
    full_in_y[i] = r2r_in[-i+2]

# sample "inputs" at finer intervals
nFine = 1024
x = np.linspace(-n, N, nFine)
y = eval_rodft10(r2r_out, x/n)/n

plt.figure(figsize=(5,3))

plt.plot(x, y, '-', color='gray', linewidth=0.5)

plt.axhline(0, ls='-', color='k')
plt.axvline(0, ls='-', color='k')

# symmetry lines
plt.axvline(-1, ls='--', color='b')
plt.axvline(n-1, ls='--', color='r')

# highlight array contents
plt.axvspan(0-0.25, (n-1)+0.25, alpha=0.3, color='gray')

plt.plot(r2r_in, 'bo')

# label individual points
i=0
plt.text(i+0.2, r2r_in[i]-0.03, chr(ord("a")+i))
for i in range(1,n):
    plt.text(i-0.5, r2r_in[i]-0.03, chr(ord("a")+i))

plt.plot(left_in_x, left_in_y, 'bo')

plt.plot(full_in_x, full_in_y, 'bo')

for i in range(n-1):
    plt.text(full_in_x[i]+0.2, full_in_y[i]-0.03, chr(ord("a")+(n-2-i)))
plt.text(full_in_x[-1]+0.2, full_in_y[-1]+0.03, "0")

# only integer xaxis ticks
plt.gca().xaxis.set_major_locator(MaxNLocator(steps=(1,10), integer=True))
plt.xlim((-4.5, 8.5))

plt.grid(True)
plt.xlabel("j")
plt.title("RODFT01 N=%d n=%d"%(N, n))
plt.tight_layout()
plt.savefig("rodft01.png")

#%% RODFT11

# input size for RODFT11
n = 4

# logical size of equivalent DFT (for RODFT11)
N = 2*n

# random Fourier coefficients
rng = default_rng(seed=40)
r2r_out = rng.uniform(size=n)

# compute input of RODFT11 by RODFT11 (the inverse of RODFT11)
r2r_in = rodft11(r2r_out)/n

# extend to left for highlighting symmetry at start of array
left_in_x = np.arange(-n, 0)
left_in_y = np.zeros([n])
for i in range(n):
    # odd symmetry around -0.5
    left_in_y[i] = -r2r_in[-i-1]

# extend to equivalent DFT size to highlight symmetry at end of array
full_in_x = np.arange(n, N)
full_in_y = np.zeros([N-n])
for i in range(N-n):
    # even symmetry around n-0.5
    full_in_y[i] = r2r_in[-i-1]

# sample at finer intervals
nFine = 1024
x = np.linspace(-n, N, nFine)
y = eval_rodft11(r2r_out, x/n)/n

plt.figure(figsize=(5,3))

plt.plot(x, y, '-', color='gray', linewidth=0.5)
plt.axhline(0, ls='-', color='k')
plt.axvline(0, ls='-', color='k')

# symmetry lines
plt.axvline(-0.5, ls='--', color='b')
plt.axvline(n-0.5, ls='--', color='r')

# highlight array contents
plt.axvspan(0-0.25, (n-1)+0.25, alpha=0.3, color='gray')

# plot actual RODFT11 input
plt.plot(r2r_in, 'bo')

# label individual points
for i in range(n-1):
    plt.text(i+0.2, r2r_in[i]-0.05, chr(ord("a")+i))
i=n-1
plt.text(i-0.5, r2r_in[i]-0.03, chr(ord("a")+i))

plt.plot(left_in_x, left_in_y, 'bo')

# plot data extending REDFT11 to full DFT
plt.plot(full_in_x, full_in_y, 'bo')

for i in range(N-n):
    plt.text(full_in_x[i]+0.2, full_in_y[i]-0.05, chr(ord("a")+(n-1-i)))

# only integer xaxis ticks at intervals of 1
plt.gca().xaxis.set_major_locator(MaxNLocator(steps=(1,10), integer=True))
plt.xlim((-4.5, 8.5))

plt.grid(True)
plt.xlabel("j")
plt.title("RODFT11 N=%d n=%d"%(N, n))
plt.tight_layout()
plt.savefig("rodft11.png")


"""
@file               gram_schmidt_test.py
@description        solutions for linear algebra textbook 6.4
@author             Hiukong Dan
@date               May 29, 2021
"""

from gram_schmidt import *
from qr_factorization import *
import numpy as np
from numpy import linalg

def gram_schmidt_modified(A):
    # Check if the matrix is linear independent
    assert np.linalg.matrix_rank(A) == A.shape[1]

    len = np.linalg.norm(A.T[0])
    Q = []
    Q.append(A.T[0] / len)
    #print(Q)

    for i in range(1, A.shape[1]):
        #print(Q.T)
        p = np.matmul(np.array(Q), A.T[i])
        p = np.matmul(np.array(Q).T, p)
        z = A.T[i] - p
        z = z / np.linalg.norm(z)
        Q.append(z)

    return np.array(Q).T

if __name__ == "__main__":
    # problem 24
    print("problem 24")
    A = np.array([[-10,13,7,-11],[2,1,-5,3],[-6,3,13,-3],[16,-16,-2,5],[2,1,-5,-7]])
    print(orthonormalize(gram_schmidt(A)))
    print("-" * 20)

    # problem 25
    print("problem 25")
    ret = qr_factorization(A)
    print("Q", ret[0])
    print("R", ret[1])
    print("-" * 20)

    # problem 26
    print("problem 26")
    ret = gram_schmidt_modified(A)
    print(ret)


# NUMPY MANIPULATIONS OF ARRAYS
# Santiago Garcia Arango
# -------------------------------------------------------------------------
import numpy as np

my_array = np.arange(1, 11)  # [1,2,..,8,9,10]
print("my_array=\n", my_array, "\n")

# -----------------CHECKING CONDITIONS IN ARRAY ITEMS----------------------
# FIRST WAY...
# This is how we show boolean result of a desired condition of an array
boolean_array = my_array > 5
print("my_array > 5 --> ", boolean_array, "\n")
# We can take advantage of the boolean_array, by calling the main array...
# ..."evaluated" in the True statements of the boolean_array.
# This will give us only the original array where the conditions are True
print("my_array[boolean_array] = ", my_array[boolean_array], "\n")

# SECOND WAY...
# This previous two step process is usually done in one step!!!
# Remark: This is the most common way to to this!!!
print("my_array[my_array>5] = ", my_array[my_array > 5], "\n")

# -----------------------CREATE MATRICES EASIER----------------------------
# Example: create this matrix:
# 1 1 1 1 1
# 1 0 0 0 1
# 1 0 9 0 1
# 1 0 0 0 1
# 1 1 1 1 1

cool_matrix = np.ones((5, 5))
cool_matrix[1:4, 1:4] = 0
cool_matrix[2, 2] = 9
print("cool_matrix:\n", cool_matrix, "\n")


# 勾配: すべての変数の偏微分をベクトルとしてまとめたもの
# 勾配降下法: 勾配法で最小値を探す
import numpy as np
import copy

# 勾配の対象とする数式
def function_2(x):
#    print("x -> " + str(x))
    return x[0]**2 + x[1]**2

# 勾配の計算
def numerical_gradient(f, x):
    h = 1e-4
    grad = np.zeros_like(x) # xと同じ形状の配列を生成

    # 変数ごとに数値微分の計算
    for idx in range(x.size):
        tmp_val = x[idx]

        # f(x+h)の計算
        x[idx] = tmp_val + h
        fxh1 = f(x)
#        print("fxh1 -> " + str(fxh1))

        # f(x-h)の計算
        x[idx] = tmp_val - h
        fxh2 = f(x)
#        print("fxh2 -> " + str(fxh2))

        grad[idx] = (fxh1 - fxh2) / (2*h)
#        print("fxh1 - fxh2 -> " + str(fxh1 - fxh2))
#        print("2*h -> " + str(2*h))
        x[idx] = tmp_val # 値を元に戻す

#        print("------------")
    return grad

# 勾配のテスト計算
print("----- 勾配のテスト計算 -----")
print(numerical_gradient(function_2, np.array([3.0, 4.0]))) # array([6., 8.])

# 勾配降下法
# - f : 最適化したい関数
# - init_x : 初期値
# - lr : Learning rate 学習率
# - step_num : 勾配法による繰り返しの数
def gradient_descent(f, init_x, lr=0.01, step_num=100):
    x = copy.deepcopy(init_x)
    for i in range(step_num):
        grad = numerical_gradient(f, x)
        x -= lr * grad

    print("init_x -> " + str(init_x) + ", lr -> " + str(lr) + ", step_num -> " + str(step_num))
    print("  result : " + str(x))
    return x

print("----- 勾配降下法のテスト計算 -----")
init_x = np.array([-3.0, 4.0])
gradient_descent(function_2, init_x=init_x, lr=0.1, step_num=100)

print("----- 学習率が大きすぎる例 -----")
init_x = np.array([-3.0, 4.0])
gradient_descent(function_2, init_x=init_x, lr=10.0, step_num=100)

print("----- 学習率が小さすぎる例 -----")
init_x = np.array([-3.0, 4.0])
gradient_descent(function_2, init_x=init_x, lr=1e-10, step_num=100)


from numpy import linalg
import numpy as np
import matplotlib.pyplot as plt

img = plt.imread('horse.jpg')

img_rescaled = img / 255

img_gray = img_rescaled @ [0.2126, 0.7152, 0.0722]

U, s, Vt = linalg.svd(img_gray)

Sigma = np.zeros((U.shape[1], Vt.shape[0]))
np.fill_diagonal(Sigma, s)

if __name__ == '__main__':
    # Print shape of the diagonal matrix:
    print(Sigma.shape)
    # Print the norm of the difference between img_gray and the reconstructed SVD product:
    print(linalg.norm(img_gray - U @ Sigma @ Vt))
    # Check if the reconstructed product is close to the original matrix
    print(np.allclose(img_gray, U @ Sigma @ Vt))


from pnuml.learner.utils import minkowski_distance
import math
import numpy as np

def test_minkowski_distance():
    assert minkowski_distance(np.array([0, 0, 0]),
                              np.array([1, 1, 1]), 2) == math.sqrt(3)


import numpy as np
import math
import random
import matplotlib.pyplot as plt
#%matplotlib inline
square_size = 1
points_inside_circle = 0
points_inside_square = 0
sample_size = 1000
arc = np.linspace(0, np.pi/2, 100)

def generate_points(size):
    x = random.random() * size
    y = random.random() * size
    return x, y

def is_inside_circle(point, size):
    return math.sqrt(point[0]**2 + point[1]**2) <= size

def compute_pi(points_inside_circle, points_inside_square):
    return 4 * (points_inside_circle / points_inside_square)

plt.axes().set_aspect('equal')
plt.plot(1*np.cos(arc), 1*np.sin(arc))

for i in range(sample_size):
    point = generate_points(square_size)
    plt.plot(point[0],point[1],'c.')
    points_inside_square+=1
    if is_inside_circle(point, square_size):
        points_inside_circle += 1

print(compute_pi(points_inside_circle,points_inside_square))
plt.show()


#!usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:yaoli
@file: 03_lin_reg_tensorflow_way.py
@time: 2019/01/01
"""

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
ops.reset_default_graph()

sess = tf.Session()

# Load the data 导入鸢尾花数据集
# iris data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])

# Declare batch size
batch_size = 25

# Initialize placeholders
x_data = tf.placeholder(shape=[None,1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None,1], dtype=tf.float32)

# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[1,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

# Declare model operations 声明模型操作
model_output = tf.add(tf.matmul(x_data, A), b)

# Declare loss function (L2 loss)
loss = tf.reduce_mean(tf.square(y_target - model_output))

# We have to tell TensorFlow how to optimize and back propagate the gradients. We do this with the standard Gradient Descent operator (tf.train.GradientDescentOptimizer), with the learning rate argument of  0.05
# Then we initialize all the model variables.

# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(loss)

# Initlialize variables
init = tf.global_variables_initializer()
sess.run(init)

# We start our training loop and run the optimizer for 100 iterations
# Training loop
loss_vec = []
for i in range(100):
    rand_index = np.random.choice(len(x_vals), size=batch_size)
    rand_x = np.transpose([x_vals[rand_index]])
    rand_y = np.transpose([y_vals[rand_index]])
    sess.run(train_step, feed_dict={x_data:rand_x, y_target:rand_y})
    temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
    loss_vec.append(temp_loss)
    if (i+1)%25==0:
        print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ', b = ' + str(sess.run(b)))
        print('Loss = ' + str(temp_loss))

# Get the optimal coefficients
[slope] = sess.run(A)
[y_intercept] = sess.run(b)

# Get the best fit line
best_fit = []
for i in x_vals:
    best_fit.append(slope*i + y_intercept)

# Plot the result
plt.plot(x_vals, y_vals, 'o', label='Data Points')
plt.plot(x_vals, best_fit, 'r-', label='Best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.title('Sepal Length vs Petal Width')
plt.xlabel('Petal Width')
plt.ylabel('Sepal Length')
plt.show()

# Plot loss over time
plt.plot(loss_vec, 'k-')
plt.title('L2 Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('L2 Loss')
plt.show()


from scipy import optimize as opt
import numpy as np

def rosen(x):
    #return (1-x)^2 + 100(y-x^2)^2
	return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

def rast(x):
    return 10*2 + sum(x[:-1]**2.0 - 10*np.cos(2*np.pi*x[:-1])+x[:1]**2.0 - 10*np.cos(2*np.pi*x[:1]))

##x= np.array([21,15])
x = [1.3,.7,.8,1.9,1.2]
##x = [-1.2, 3, 12]
f = opt.rosen
fprime = opt.rosen_der

##print opt.fmin(f, x)
##print opt.fmin_powell(rosen,x)
##print opt.fmin_cg(rosen,x)
##print opt.fmin_bfgs(rosen,x)
##print opt.fmin_ncg(rosen,x, fprime, fhess=opt.rosen_hess)

def g(x):
    return x**2 + 10*np.sin(x)
grid = (-1,2,.01)
print opt.brute(g,(grid,))
##
##
##x = [.5]
##print opt.fmin_l_bfgs_b(g,x)
##print opt.fmin_bfgs(g,x)


import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("machine-learning-ex1/ex1/ex1data1.txt", delimiter=",", unpack=True)  # Output is 1D. Vectors are 2D m x 1 dimension so need to change to 2D using reshape

y = data[-1].reshape((data.shape[1], 1))  # Makes y a vector with each new line different training set

theta = np.ones((data.shape[0], 1))

onesVector = np.ones((data.shape[1], 1))  # Creates an m dimensional vector of 1s
X = np.concatenate((onesVector, data[:-1].T), axis=1)  # Creates matrix X, where each new line is a different training example, where x0 on each line is 1

iterations = 10000

'''Function to calculate cost for current theta values'''

def calc_cost(X, y, theta):

    m = len(y)

    predictions = X.dot(theta)  # Vector is generated where each row is a predicted value for each training example
    cost = (1/(2*m)) * np.sum(np.square(predictions - y))  # Finds the cost associated with each row of y and predictions and adds them all together to give the total cost.
    return cost

'''Function to perform gradient descent, returns theta history, cost history, and most optimal theta values'''

def gradient_descent(X,y, theta, learning_rate, iterations):

    n = theta.shape[0] - 1  # Number of features
    m = len(y)  # Number of training examples

    cost_history = np.zeros(iterations)  # Array used to store cost associated with each iteration
    theta_history = np.zeros((iterations, n+1)) # Array used to store theta associated with each iteration

    for j in range(0, iterations):  # Each time this loop runs theta is updated (optimised a bit more).
        predictions = X.dot(theta)  # h values, m dimensional vector
        theta = theta - (1/m) * learning_rate * X.T.dot(predictions - y) # update theta
        theta_history[j, :] = theta.T  # Add new theta to theta_history
        cost_history[j] = calc_cost(X, y, theta)  # Add new cost to cost_history

    return cost_history, theta_history, theta

'''Function to scale features to allow for quicker gradient descent'''

def scale_features(X):
    x_scaled = X.copy()
    X_std = x_scaled[:, 1:].std(0, keepdims=True)  # find standard deviation of each feature
    X_mean = x_scaled[:, 1:].mean(0, keepdims=True)  # find mean of each feature
    x_scaled[:, 1:] = (x_scaled[:, 1:] - X_mean) / X_std  # Normalise x1, x2, ..., xn
    return x_scaled, X_std, X_mean  # X is normalised except x0 (1)

'''Function to unscale theta values after normalising (scaling) X'''

def unscale_theta(theta, X_std, X_mean):

    theta[0,0] = theta[0, 0] - (X_mean / X_std).dot(theta[1:, 0]) # Unscale theta0
    theta[1:, 0] = theta[1:, 0] / X_std  # Unscale theta1, theta2, ..., theta n
    return theta

X_scaled, X_std, X_mean = scale_features(X)
cost_history, theta_history, theta = gradient_descent(X_scaled, y, theta, 0.1, iterations)

theta = unscale_theta(theta, X_std, X_mean)

# Plot graph of cost vs iterations
iterationsX = np.arange(1, iterations + 1, dtype= int)
plt.plot(iterationsX, cost_history, '-r')  # need x and y values separate or pyplot autogenerates x values.
plt.xlabel('Iterations')
plt.ylabel('Cost Function')
plt.xlim((1, iterations))
plt.show()

Multivariate with feature scaling (more efficient).py


# By
# ████████╗██╗   ██╗ █████╗ ███╗   ██╗    ██╗  ██╗ ██████╗
# ╚══██╔══╝██║   ██║██╔══██╗████╗  ██║    ██║  ██║██╔═══██╗
#    ██║   ██║   ██║███████║██╔██╗ ██║    ███████║██║   ██║
#    ██║   ██║   ██║██╔══██║██║╚██╗██║    ██╔══██║██║   ██║
#    ██║   ╚██████╔╝██║  ██║██║ ╚████║    ██║  ██║╚██████╔╝
#    ╚═╝    ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═══╝    ╚═╝  ╚═╝ ╚═════╝

# Email: ttuan.ho@outlook.com

from scipy.stats import norm, binom, poisson, expon, uniform
import numpy as np

"""
Ex4
"""

# % matlab version:
# binomcdf(1,6,0.5)
binom.cdf(1,6,0.5)

# % matlab version:
# poisscdf(1,6,0.5)
poisson.cdf(1,3)

"""
Ex5
"""
# % matlab version:
# 1 - normcdf(0.29)
# 1 - normcdf(100,96,14)
1 - norm.cdf(0.29)
1 - norm.cdf(100,96,14)

# % matlab version:
# normcdf(80,96,14)-normcdf(50,96,14)
norm.cdf(80,96,14)-norm.cdf(50,96,14)

# % matlab version:
# norminv(0.95)
norm.ppf(0.95)

# Interval that gives 90% = [mean +- sd * tinv(prob+(1-prob)/2)]
96 + 14 * norm.ppf(0.95) * np.array([-1, 1])


import pandas as pd
import seaborn as sns
import numpy as np
import pytest
import eda_hw
import math

def get_data():
    dataset_df = sns.load_dataset('mpg').select_dtypes(include='number')
    return dataset_df.sample(axis=1).squeeze().dropna()

test_data = get_data()
print(test_data.head())

def test_average():
    """
    This function tests that you've implemented mean correctly
    """
    assert math.isclose(eda_hw.average(test_data),
                        test_data.mean(), abs_tol=0.001)

def test_standard_deviation():
    assert math.isclose(eda_hw.standard_deviation(test_data),
                        test_data.std(ddof=0), abs_tol=0.001)

def test_median():
    assert eda_hw.median(test_data) == test_data.median()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# =======================================
# File Name: utils.py
# Purpose  : implementation for generating
#            parameters
# =======================================

import math
import numpy as np
import csv
import sys
import os

np.random.seed(42) # fixed random seed

def l2_cost(x, y):
    """L2 cost
    """
    m, n = x.shape[0], y.shape[0]
    ind = np.indices((m, n))
    cost = ((x[ind[0]] - y[ind[1]]) ** 2).sum(axis=2)
    return cost

def random_cost(num):
    """random cost
    """
    x = np.random.rand(num * 2).reshape((num, 2))
    y = np.random.rand(num * 2).reshape((num, 2))
    return l2_cost(x, y)

def gen_ellipse_2d(num, x1_center=0, x2_center=0, r_x1=1.3, r_x2=0.9, eps=0.1):
    """generate ellipse 2d datapoint
    """
    r = np.random.uniform(0, 2. * math.pi, num)
    dx1 = np.cos(r) + eps / math.sqrt(2.) * np.random.randn(num)
    dx2 = np.sin(r) + eps / math.sqrt(2.) * np.random.randn(num)
    x1 = r_x1 * dx1 + x1_center
    x2 = r_x2 * dx2 + x2_center
    x = np.concatenate((x1.reshape(num, 1), x2.reshape(num, 1)), axis=1)
    return x

def ellipse_cost(num, x1_center=0, x2_center=0, sr_x1=1.3, sr_x2=0.9, tr_x1=0.9, tr_x2=1.1, eps=0.1):
    """ellipse cost
    """
    x = gen_ellipse_2d(num, x1_center, x2_center, sr_x1, sr_x2, eps)
    y = gen_ellipse_2d(num, x1_center, x2_center, tr_x1, tr_x2, eps)
    return l2_cost(x, y)

def gen_caffarelli_2d(num, x1_center=0, x2_center=0, r=1, shift=2):
    """generate caffarelli 2d datapoint
    """
    l = 0
    while l <= num:
        ox1 = np.random.uniform(-r, r, num)
        ox2 = np.random.uniform(-r, r, num)
        mask = ox1 ** 2 + ox2 ** 2 < r ** 2
        dx1, dx2 = ox1[mask], ox2[mask]
        dx1[dx1 < 0.] -= shift
        dx2[dx2 >= 0.] += shift
        n = dx1.size
        x1 = dx1 + x1_center
        x2 = dx2 + x2_center
        x = np.concatenate((x1.reshape(n, 1), x2.reshape(n, 1)), axis=1)
        if l == 0:
            pos = x
        else:
            pos = np.concatenate((pos, x), axis=0)
        l = len(pos)
    return pos[0:num, ]

def caffarelli_cost(num, x1_center=0, x2_center=0, r=1, shift=2):
    """caffarelli cost
    """
    x = gen_caffarelli_2d(num, x1_center, x2_center, r, 0)
    y = gen_caffarelli_2d(num, x1_center, x2_center, r, shift)
    return l2_cost(x, y)

def DOTmark_weight(num, ImageClass):
    """DOTmark dataset weight for mu and nu
    Schuhmacher, Dominic; Schrieber, Jörn; Gottschlich, Carsten (2016):
    DOTmark v1.0. figshare. Dataset.
    https://doi.org/10.6084/m9.figshare.4288466.v1
    """
    path = os.getcwd()
    index = np.random.choice(range(1, 10), 2, replace=None)
    w = []
    for i in index:
        with open(path + '/DOT/' + ImageClass + '/data' + str(int(math.sqrt(num)))
                  + '_100' + str(i) + '.csv') as csvfile:
            csv_reader = csv.reader(csvfile)
            for row in csv_reader:
                w.append(row)
        csvfile.close()
    w = np.array(w, np.float32).reshape((2, num))
    mu = w[0, :] / sum(w[0, :])
    nu = w[1, :] / sum(w[1, :])
    return mu, nu

def gen_DOTmark(start_x1, end_x1, start_x2, end_x2, size):
    """generate DOTmark 2d datapoint
    """
    step_x1 = (end_x1 - start_x1) / size
    x1 = np.linspace(start_x1 + step_x1 / 2.0, end_x1 - step_x1 / 2.0, size)
    step_x2 = (end_x2 - start_x2) / size
    x2 = np.linspace(start_x2 + step_x2 / 2.0, end_x2 - step_x2 / 2.0, size)
    x1p, x2p = np.meshgrid(x1, x2)
    x = np.concatenate((x1p.reshape((int(size * size), 1)), x2p.reshape((int(size * size), 1))), axis=1)
    return x

def DOTmark_cost(size, start_x1=0, end_x1=1, start_x2=0, end_x2=1):
    """DOTmark cost
    """
    x = gen_DOTmark(start_x1, end_x1, start_x2, end_x2, int(math.sqrt(size)))
    y = gen_DOTmark(start_x1, end_x1, start_x2, end_x2, int(math.sqrt(size)))
    return l2_cost(x, y)

def min_row(A, eps):
    """min row
    """
    return - eps * np.log(np.sum(np.exp(-A / eps), axis=1))

def min_col(A, eps):
    """min col
    """
    return - eps * np.log(np.sum(np.exp(-A / eps), axis=0))

def get_params(n, dataset, imageclass="WhiteNoise"):
    """ get mu, nu, c for Optimal Transport problem from given dataset
    """
    if dataset == 'random':
        mu = np.random.random(n)
        mu = mu / sum(mu)
        nu = np.random.random(n)
        nu = nu / sum(nu)
        c = random_cost(n)
    elif dataset == 'caffarelli':
        mu = np.ones(n) / n
        nu = np.ones(n) / n
        c = caffarelli_cost(n)
    elif dataset == 'ellipse':
        mu = np.ones(n) / n
        nu = np.ones(n) / n
        c = ellipse_cost(n, 0, 0, 1.3, 0.9, 0.9, 1.1, 0.1)
    elif dataset == 'DOTmark':
        if n != 1024 and n != 4096:
            print("Unsupported datasize, exit")
            sys.exit(1)
        mu, nu = DOTmark_weight(n, imageclass)
        c = DOTmark_cost(n)
    return mu, nu, c


import math
import numpy as np
import matplotlib.pyplot as plt

import openrtdynamics2.lang as dy
from openrtdynamics2.ORTDtoNumpy import ORTDtoNumpy

from .vehicle_lib import make_time

@ORTDtoNumpy()
def generate_one_dimensional_motion_trajectory(acceleration_input, Ts):

    # Delta_l_dotdot; the motion jerk is limited to +-1.0 m/s^3
    acceleration     = dy.rate_limit(acceleration_input, Ts, lower_limit = -1.0, upper_limit = 1.0)

    # Delta_l_dot
    velocity         = dy.euler_integrator(acceleration, Ts, 0)

    # Delta_l
    lateral_distance = dy.euler_integrator(velocity,     Ts, 0)

    return lateral_distance, velocity, acceleration

def generate_lateral_profile_1(Ts, T_phase1=1, T_phase2=3, T_phase3=1, T_phase4=3, T_phase5=1):
    """
        Generate a one-dimensional motion trajectory for a jerk-limited sidewards movement.

    """

    def ones_by_time(T):
        return np.ones( int(math.floor( T / Ts )) )

    lateral_distance, velocity, acceleration = generate_one_dimensional_motion_trajectory(
        np.concatenate((

            # phase 1: no movement
            +0.0 * ones_by_time(T_phase1),

            # phase 2: movement into the positive direction
            #          divided into an accelerating phase,
            #          a phase of constant velocity, and finally
            #          a braking phase. The velocity is then zero.
            +1.0 * ones_by_time(T_phase2/3),
            +0.0 * ones_by_time(T_phase2/3),
            -1.0 * ones_by_time(T_phase2/3),

            # phase 3: no movement
            +0.0 * ones_by_time(T_phase3),

            # phase 4: like phase 2, however, in the opposite direction
            -1.0 * ones_by_time(T_phase4/3),
            +0.0 * ones_by_time(T_phase4/3),
            +1.0 * ones_by_time(T_phase4/3),

            # phase 5: no movement
            +0.0 * ones_by_time(T_phase5)

        )),
        0.01
    )

    lateral_profile = {}

    lateral_profile['Ts'] = Ts

    lateral_profile['Delta_l_r_dotdot'] = acceleration
    lateral_profile['Delta_l_r_dot'] = velocity
    lateral_profile['Delta_l_r'] = lateral_distance

    # velocity of the vehicle
    lateral_profile['v'] = 5 * np.ones( len( lateral_profile['Delta_l_r'] ) )

    return lateral_profile

def plot_lateral_profile( lateral_profile ):
    """
        Show a profile for the lateral distance to the original path
    """

    time = make_time(lateral_profile['Ts'], lateral_profile['Delta_l_r'])
    plt.figure(figsize=(12,3), dpi=100)
    plt.plot(time, lateral_profile['Delta_l_r'] )
    plt.plot(time, lateral_profile['Delta_l_r_dot'] )
    plt.plot(time, lateral_profile['Delta_l_r_dotdot'] )
    plt.legend(['intended lateral distance [$m$]', '1st derivative [$m/s$]', '2nd derivative [$m/s^s$]'])
    plt.xlabel('time [s]')
    plt.ylabel('lateral distance, velocity, acceleration')
    plt.show()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
import numpy as np

def inner_product(x, y):
    """Inner product."""
    return x.dot(y)

def inner_product_d(x_d, y_d):
    """Inner product for sparse vectors."""
    inner_prod = 0.0
    for k, v in x_d.items():
        if k in y_d:
            inner_prod += v * y_d[k]
    return inner_prod

def correlation_coeff(x, y):
    """Correlation coefficient.

    Note: To speed up computation, we drop normalized term (n - 1)**(-1),
    since this term is redundant after division with sqrt.
    """
    x_ = x - x.mean()
    y_ = y - y.mean()
    cov = inner_product(x_, y_)
    var_x = inner_product(x_, x_)
    var_y = inner_product(y_, y_)
    return cov / math.sqrt(var_x * var_y)

def cosine_similarity(x, y):
    """Cosine similarity."""
    inner_prod = inner_product(x, y)
    norm2_x = inner_product(x, x)
    norm2_y = inner_product(y, y)
    return inner_prod / math.sqrt(norm2_x * norm2_y)

def cosine_similarity_d(x_d, y_d):
    """Cosine similarity for sparse vectors."""
    inner_prod = inner_product_d(x_d, y_d)
    x = np.array(list(x_d.values()))
    y = np.array(list(y_d.values()))
    norm2_x = inner_product(x, x)
    norm2_y = inner_product(y, y)
    return inner_prod / math.sqrt(norm2_x * norm2_y)

def accuracy(y_pred, y_true):
    """Accuracy."""
    return (y_pred == y_true.reshape(y_pred.shape)).mean()

def mean_squared_error(y_pred, y_true):
    """Mean squared error."""
    return ((y_pred - y_true.reshape(y_pred.shape)) ** 2).mean()


import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# import pandas_datareader as web
import pymc3 as pm
from pymc3.distributions.timeseries import GaussianRandomWalk
import seaborn as sns

import pickle
import warnings
warnings.filterwarnings('ignore')

def obtain_plot_amz_dataset(start_date, end_date):
    amzn = pd.read_csv('data/amzn.csv')
    print("Loading and plotting AMZN log returns...")
    amzn['returns'] = amzn['Adj Close']/amzn['Adj Close'].shift(1)
    amzn.dropna(inplace=True)
    amzn['log_returns'] = np.log(amzn['returns'])
    amzn['log_returns'].plot(linewidth=0.5)
    plt.ylabel('AMZN daily percentage returns')
    plt.show()
    return amzn
# Model Specification in PyMC3 ------------------------

def configure_sample_stoch_vol_model(log_returns, samples):
    print("Configuring stochastic volatility with PyMC3...")
    model = pm.Model()

    with model:
        sigma = pm.Exponential('sigma', 50.0, testval=0.1)
        nu = pm.Exponential('nu', 0.1)
        s = GaussianRandomWalk('s', sigma=(sigma**-2), shape=len(log_returns))
        logrets = pm.StudentT(
            'logrets', nu, lam=pm.math.exp(-2.0*s), observed=log_returns)

    # Fitting the Model with NUTS
    print('Fitting the stochastic volatility model')
    with model:
        trace = pm.sample(samples)

        with open('data\\trace.pickle', 'wb') as f:
            pickle.dump(trace, f, protocol=pickle.HIGHEST_PROTOCOL)

        # with open('trace.pickle', 'rb') as f:
        #     trace = pickle.load(f)

        # with open('trace2.pickle', 'rb') as f:
        #     trace = pickle.load(f)
    '''
    # print(model.vars[:-1])
    # error when call traceplot
    pm.traceplot(trace, model.vars)
    plt.show()
    '''

    print("Plotting the log volatility...")
    k = 10      # step-size
    opacity = 0.03

    plt.plot(trace[s][::k].T, 'b', alpha=opacity)       # 'b' mean blue
    plt.xlabel('Time')
    plt.ylabel('Log Volatility')
    plt.show()

    print("Plotting the absolute returns overlaid with vol...")
    plt.plot(np.abs(np.exp(log_returns))-1.0, linewidth=0.5)
    plt.plot(np.exp(trace[s][::k].T), 'r', alpha=opacity)

    plt.xlabel("Trading Days")
    plt.ylabel("Absolute Returns/Volatility")
    plt.show()

if __name__ == "__main__":
    start_date = datetime.datetime(2006, 1, 1)
    end_date = datetime.datetime(2015, 12, 31)

    # Obtain and plot the logarithmic returns of amz prices
    df = obtain_plot_amz_dataset(start_date, end_date)
    log_returns = np.array(df['log_returns'])

    samples = 500   # 2000
    configure_sample_stoch_vol_model(log_returns, samples)
    pass


import numpy as np

A=np.array([[10,2,1],[0,4,2],[1,2,2]])
b=np.array([3,2,1])

def my_back_solve(A,b) :
    n=len(A)
    x=np.zeros(n)
    x[n-1] = b[n-1] / A[n-1, n-1]
    for k in range(n-2, -1, -1):
        sums = b[k]
        for j in range(k+1, n):
            sums = sums - (A[k,j] * x[j])
        x[k] = sums / A[k,k]
    return x

x=my_back_solve(A,b)
print('x=',x)
print('A x=', np.dot(A,x))


"""
Python code for fractional differencing of pandas time series
illustrating the concepts of the article "Preserving Memory in Stationary Time Series"
by Simon Kuttruf

While this code is dedicated to the public domain for use without permission, the author disclaims any liability in connection with the use of this code.
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def getWeights(d,lags):
    # return the weights from the series expansion of the differencing operator
    # for real orders d and up to lags coefficients
    w=[1]
    for k in range(1,lags):
        w.append(-w[-1]*((d-k+1))/k)
    w=np.array(w).reshape(-1,1)
    return w

def plotWeights(dRange, lags, numberPlots):
    weights=pd.DataFrame(np.zeros((lags, numberPlots)))
    interval=np.linspace(dRange[0],dRange[1],numberPlots)
    for i, diff_order in enumerate(interval):
        weights[i]=getWeights(diff_order,lags)
    weights.columns = [round(x,2) for x in interval]
    fig=weights.plot()
    plt.legend(title='Order of differencing')
    plt.title('Lag coefficients for various orders of differencing')
    plt.xlabel('lag coefficients')
    #plt.grid(False)
    plt.show()

plotWeights([0,1],7,6)

def ts_differencing(series, order, lag_cutoff):
    # return the time series resulting from (fractional) differencing
    # for real orders order up to lag_cutoff coefficients

    weights=getWeights(order, lag_cutoff)
    res=0
    for k in range(lag_cutoff):
        res += weights[k]*series.shift(k).fillna(0)
    return res[lag_cutoff:]

def plotMemoryVsCorr(result, seriesName):
    fig, ax = plt.subplots()
    ax2 = ax.twinx()
    color1='xkcd:deep red'; color2='xkcd:cornflower blue'
    ax.plot(result.order,result['adf'],color=color1)
    ax.plot(result.order, result['5%'], color='xkcd:slate')
    ax2.plot(result.order,result['corr'], color=color2)
    ax.set_xlabel('order of differencing')
    ax.set_ylabel('adf', color=color1);ax.tick_params(axis='y', labelcolor=color1)
    ax2.set_ylabel('corr', color=color2); ax2.tick_params(axis='y', labelcolor=color2)
    plt.title('ADF test statistics and correlation for %s' % (seriesName))
    plt.show()

from statsmodels.tsa.stattools import adfuller
def MemoryVsCorr(series, dRange, numberPlots, lag_cutoff, seriesName):
    # return a data frame and plot comparing adf statistics and linear correlation
    # for numberPlots orders of differencing in the interval dRange up to a lag_cutoff coefficients

    interval=np.linspace(dRange[0], dRange[1],numberPlots)
    result=pd.DataFrame(np.zeros((len(interval),4)))
    result.columns = ['order','adf','corr', '5%']
    result['order']=interval
    for counter,order in enumerate(interval):
        seq_traf=ts_differencing(series,order,lag_cutoff)
        res=adfuller(seq_traf, maxlag=1, regression='c') #autolag='AIC'
        result.loc[counter,'adf']=res[0]
        result.loc[counter,'5%']=res[4]['5%']
        result.loc[counter,'corr']= np.corrcoef(series[lag_cutoff:].fillna(0),seq_traf)[0,1]
    plotMemoryVsCorr(result, seriesName)
    return result


import numpy as np
from scipy.misc import factorial

def loggamma_vec(x_vec, a, b):
    """
    sum_i log Gamma(x_vec[i], a[i], b[i])

    Parameters
    ----------
    x_vec : np.array
    a : np.array or float
    b : np.array or float

    Returns
    -------
    float
    """
    p_x = a * np.log(b) - np.log(factorial(a - 1.0)) + (a - 1.0) * np.log(x_vec) - b * x_vec
    return np.sum(p_x)

def logpoisson_vec(lam_vec, x_vec):
    """
    sum_i logPoisson(lam_vec[i], x_vec[i])

    Parameters
    ----------
    lam_vec : np.array
        1 dimensional (D,) size numpy array for the Poisson mean
    x_vec : np.array
        1 dimensional (D,) size numpy array

    Returns
    -------
    float

    """
    lam_vec = np.array(lam_vec)
    if np.prod(lam_vec) == 0:
        for i, l in enumerate(lam_vec):
            if l == 0:
                lam_vec[i] = 1e-7
    x_vec = np.array(x_vec)
    return np.sum(-lam_vec + x_vec * np.log(lam_vec) - np.log(factorial(x_vec)))

def logpoisson_vec_all(lam_vec, x_mat, gam_prior=False, a=1.0, b=1.0):
    """
    Get an array of log likelihood for each x_mat[i], using lam_vec
    loglik[i] = logpoisson_vec(lam_vec, x_mat[i])

    Parameters
    ----------
    lam_vec : np.array
        1 dimensional (D,) size numpy array for the Poisson mean
    x_mat : np.array
        2 dimensional (N, D) size numpy array

    Returns
    -------
    np.array
        (N,) size log probabilities
    """
    logprob= np.zeros(x_mat.shape[0])
    if gam_prior:
        for i in range(x_mat.shape[0]):
            xi = x_mat[i]
            const = np.log(factorial(a + xi - 1)) - np.log(factorial(a - 1)) \
                    - np \
                    .log ( factorial(xi)) + a* np.log(b) - (a + xi)* np.log(b+1)
            logprob[i] = loggamma_vec( lam_vec, a+xi, b+1) + np.sum(const)
    else:
        for i in range(x_mat.shape[0]):
            logprob[i] = logpoisson_vec(lam_vec, x_mat[i])
    return logprob


### The following code implements the traveling salesman probblem with a greedy heuristic approach.

from tqdm import tqdm
import numpy as np

def read_file(name):
    """Given the path/name of the file, return the graph (dictionary).
    """
    file = open(name,'r')
    data = file.readlines()

    # initialize the graph
    g = {}
    #num = int(data[0][0])
    for line in data[1:]:
        item = line.split()
        g[int(item[0])] = (float(item[1]), float(item[2]))

    return g

g = read_file('tsp_large.txt')

def euclidean_sq(a,b):
    """Return the square euclidean distance of a(x1,y1) and b(x2,y2)
    """
    dis = (a[0] - b[0])**2 + (a[1]- b[1])**2
    return dis

def tsp(g):
    """Given the graph, return the heuristic traveling salesman problem value.
    """
    # number of cities
    num = len(g)

    # Initialize visited and unvisited set
    visited = []
    visited.append(1)

    unvisited = list(range(2, len(g)+1))

    final_dis = 0
    # Main loop: stop when all the cities are visited.
    for i in tqdm(range(num-1)):

        current_city = visited[-1]
        next_city = unvisited[0]

        min_dis = euclidean_sq(g[current_city], g[next_city])

        if len(unvisited)>1:
            for j in unvisited[1:]:
                dis = euclidean_sq(g[current_city], g[j])
                if dis < min_dis:
                    min_dis = dis
                    next_city = j
                # break the tie
                elif dis == min_dis:
                    next_city = min(next_city, j)

        visited.append(next_city)
        unvisited.remove(next_city)
        final_dis += np.sqrt(min_dis)

    final_dis += np.sqrt(euclidean_sq(g[visited[-1]], g[1]))

    return final_dis

def main():
    g = read_file('tsp_large.txt')
    best_dis = tsp(g)

    return best_dis

if __name__ == '__main__':
    main()

C4: Shortest Path Revisited, NP-Complete/tsp_heuristic.py


import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image = mpimg.imread('cutout1.jpg')

# Define a function to compute color histogram features
def color_hist(img, nbins=32, bins_range=(0, 256)):
    # Compute the histogram of the RGB channels separately
    rhist = np.histogram(img[:,:,0], bins=nbins,range=bins_range)
    ghist = np.histogram(img[:,:,1], bins=nbins,range=bins_range)
    bhist = np.histogram(img[:,:,2], bins=nbins,range=bins_range)
    # Generating bin centers
    bin_edges = rhist[1]
    #print(bin_edges)
    #print(bin_edges[1:])
    #print(bin_edges[0:len(bin_edges)-1])
    bin_centers = (bin_edges[1:]  + bin_edges[0:len(bin_edges)-1])/2
    # Concatenate the histograms into a single feature vector
    hist_features = np.concatenate((rhist[0], ghist[0], bhist[0]))
    # Return the individual histograms, bin_centers and feature vector
    return rhist, ghist, bhist, bin_centers, hist_features

rh, gh, bh, bincen, feature_vec = color_hist(image, nbins=32, bins_range=(0, 256))

# Plot a figure with all three bar charts
if rh is not None:
    fig = plt.figure(figsize=(12,3))
    plt.subplot(131)
    plt.bar(bincen, rh[0])
    plt.xlim(0, 256)
    plt.title('R Histogram')
    plt.subplot(132)
    plt.bar(bincen, gh[0])
    plt.xlim(0, 256)
    plt.title('G Histogram')
    plt.subplot(133)
    plt.bar(bincen, bh[0])
    plt.xlim(0, 256)
    plt.title('B Histogram')
    fig.tight_layout()
else:
    print('Your function is returning None for at least one variable...')


import numpy as np
import matplotlib.pyplot as plt

class Brownian:
    """
    A Brownian motion class constructor
    """

    def __init__(self, x0=0):
        """
        Init class
        """
        assert (
            type(x0) == float or type(x0) == int or x0 is None
        ), "Expect a float or None for the initial value"

        self.x0 = float(x0)

    def gen_random_walk(self, n_step=100):
        """
        Generate motion by random walk

        Arguments:
            n_step: Number of steps

        Returns:
            A NumPy array with `n_steps` points
        """
        # Warning about the small number of steps
        if n_step < 30:
            print(
                "WARNING! The number of steps is small. It may not generate a good stochastic process sequence!"
            )

        w = np.ones(n_step) * self.x0

        for i in range(1, n_step):
            # Sampling from the Normal distribution with probability 1/2
            yi = np.random.choice([1, -1])
            # Weiner process
            w[i] = w[i - 1] + (yi / np.sqrt(n_step))

        return w

    def gen_normal(self, n_step=100):
        """
        Generate motion by drawing from the Normal distribution

        Arguments:
            n_step: Number of steps

        Returns:
            A NumPy array with `n_steps` points
        """
        if n_step < 30:
            print(
                "WARNING! The number of steps is small. It may not generate a good stochastic process sequence!"
            )

        w = np.ones(n_step) * self.x0

        for i in range(1, n_step):
            # Sampling from the Normal distribution
            yi = np.random.normal()
            # Weiner process
            w[i] = w[i - 1] + (yi / np.sqrt(n_step))

        return w

    def stock_price(self, s0=100, mu=0.2, sigma=0.68, deltaT=52, dt=0.1):
        """
        Models a stock price S(t) using the Weiner process W(t) as
        `S(t) = S(0).exp{(mu-(sigma^2/2).t)+sigma.W(t)}`

        Arguments:
            s0: Iniital stock price, default 100
            mu: 'Drift' of the stock (upwards or downwards), default 1
            sigma: 'Volatility' of the stock, default 1
            deltaT: The time period for which the future prices are computed, default 52 (as in 52 weeks)
            dt (optional): The granularity of the time-period, default 0.1

        Returns:
            s: A NumPy array with the simulated stock prices over the time-period deltaT
        """
        n_step = int(deltaT / dt)
        time_vector = np.linspace(0, deltaT, num=n_step)
        # Stock variation
        stock_var = (mu - (sigma**2 / 2)) * time_vector
        # Forcefully set the initial value to zero for the stock price simulation
        self.x0 = 0
        # Weiner process (calls the `gen_normal` method)
        weiner_process = sigma * self.gen_normal(n_step)
        # Add two time series, take exponent, and multiply by the initial stock price
        s = s0 * (np.exp(stock_var + weiner_process))

        return s

b = Brownian()
# brown = b.gen_random_walk(100)
plt.plot(brown, label="Price")
plt.axvline(x=100, color="black", ls="--", label="Now")
plt.axvline(x=80, color="red", ls="--", label="Horizon 20: sell!")
plt.axvline(x=60, color="green", ls="--", label="Horizon 40: buy!")
plt.axhline(y=brown[79], color="red", linestyle=":")
plt.axhline(y=brown[59], color="green", linestyle=":")
plt.axhline(y=brown[99], color="black", linestyle=":")
plt.title("A random stock price")
plt.xlabel("Time")
plt.ylabel("Stock price (Brownian Motion)")
plt.legend()
plt.savefig("etf.png", format="png", dpi=300)
plt.show()


import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress

def draw_plot():
    # Read data from file
    df = pd.read_csv("sea_level_predictor/epa-sea-level.csv")
    # Create scatter plot
    x = df["Year"]
    y = df["CSIRO Adjusted Sea Level"]

    plt.scatter(x, y, c="blue", s=8)

    # Create first line of best fit
    linregress_stats = linregress(x, y)
    slope = linregress_stats.slope
    intercept = linregress_stats.intercept

    x_predict_2050 = range(1880, 2050)

    plt.plot(
        x_predict_2050,
        intercept + slope * x_predict_2050,
        "r",
        label="first line of best fit",
    )

    # Create second line of best fit
    x_2000_recent = df[df["Year"] >= 2000]["Year"]
    y_2000_recent = df[df["Year"] >= 2000]["CSIRO Adjusted Sea Level"]

    linregress_stats = linregress(x_2000_recent, y_2000_recent)
    slope_2000_recent = linregress_stats.slope
    intercep_2000_recent = linregress_stats.intercept

    x_predict_2000_2050 = range(2000, 2050)

    plt.plot(
        x_predict_2000_2050,
        intercep_2000_recent + slope_2000_recent * x_predict_2000_2050,
        "g",
        label="second line of best fit",
    )

    # Add labels and title
    plt.xlabel("Year")
    plt.ylabel("Sea Level (inches)")
    plt.title("Rise in Sea Level")

    # Save plot and return data for testing (DO NOT MODIFY)
    plt.savefig("sea_level_plot.png")
    return plt.gca()