"""
Filename: riccati.py

Authors: Thomas Sargent, John Stachurski

Solves the discrete-time algebraic Riccati equation

"""

import numpy as np
from numpy import dot
from numpy.linalg import solve

def dare(A, B, R, Q, tolerance=1e-10, max_iter=150):
    """
    Solves the discrete-time algebraic Riccati equation

        X = A'XA - A'XB(B'XB + R)^{-1}B'XA + Q

    via the doubling algorithm.  An explanation of the algorithm can be
    found in "Optimal Filtering" by B.D.O. Anderson and J.B. Moore
    (Dover Publications, 2005, p. 159).

    Parameters
    ----------
    A : array_like(float, ndim=2)
        k x k array.
    B : array_like(float, ndim=2)
        k x n array
    R : array_like(float, ndim=2)
        n x n, should be symmetric and positive definite
    Q : array_like(float, ndim=2)
        k x k, should be symmetric and non-negative definite
    tolerance : scalar(int), optional(default=1e-10)
        The tolerance level for convergence
    max_iter : scalar(float), optional(default=150)
        The maximum number of iterations allowed

    Returns
    -------
    X : array_like(float, ndim=2)
        The fixed point of the Riccati equation; a  k x k array
        representing the approximate solution

    """
    # == Set up == #
    error = tolerance + 1
    fail_msg = "Convergence failed after {} iterations."
    # == Make sure that all arrays are two-dimensional == #
    A, B, Q, R = list(map(np.atleast_2d, (A, B, Q, R)))
    k = Q.shape[0]
    I = np.identity(k)

    # == Initial conditions == #
    a0 = A
    b0 = dot(B, solve(R, B.T))
    g0 = Q
    i = 1

    # == Main loop == #
    while error > tolerance:

        if i > max_iter:
            raise ValueError(fail_msg.format(i))

        else:

            a1 = dot(a0, solve(I + dot(b0, g0), a0))
            b1 = b0 + dot(a0, solve(I + dot(b0, g0), dot(b0, a0.T)))
            g1 = g0 + dot(dot(a0.T, g0), solve(I + dot(b0, g0), a0))

            error = np.max(np.abs(g1 - g0))

            a0 = a1
            b0 = b1
            g0 = g1

            i += 1

    return g1  # Return X

if __name__ == '__main__': ## Example of useage

    a = np.array([[0.1, 0.1, 0.0],
                  [0.1, 0.0, 0.1],
                  [0.0, 0.4, 0.0]])

    b = np.array([[1.0, 0.0],
                  [0.0, 0.0],
                  [0.0, 1.0]])

    r = np.array([[0.5, 0.0],
                  [0.0, 1.0]])

    q = np.array([[1, 0.0, 0.0],
                  [0.0, 1, 0.0],
                  [0.0, 0.0, 10.0]])

    x = dare(a, b, r, q)
    print(x)


import numpy as np

A = np.array([[3, -9], [2, 4]])
print(A)

B = np.array([4, 9])

Z = np.linalg.solve(A, B)

print(Z)


import numpy as np
import math
import matplotlib.pyplot as plt
'''
PART A:
Bernoulli distribution: We can generate samples from a bernoulli distribution with parameter p
by using uniform random variable generator in python by outputting 1 if number generated through
np.random.uniform() is less than parameter p and 0 if greater.
The above argument works because then the probability of generating 1 is (p-0)/1 = p and probability
of generating 0 is (1-p)/1 = 1-p
'''

def Bernoulli(p):
    z = np.random.uniform()
    if(z<=p):
        k = 1
    else:
        k = 0;
    return k

'''
PART B
Binomial distribution:We follow a similar approach in this, except that we need to have n inputs for this.
We shall store those n numbers in a list.
Binomial parameters : n, p
No. of sample : N
'''

def probab(n,r,p):
    x = nCr(n,r) * math.pow(p,r) * math.pow((1-p),n-r)
    return x

def nCr(n,r):
    f = math.factorial
    return (f(n) / (f(r)*f(n-r)))

def Binomial(N,n,p):
    l =[]
    Nl = []
    for i in range(0,N):
        l = []
        suml = 0
        for j in range(0,n):
            m = Bernoulli(p)
            l.append(m)
        suml = sum(l)
        Nl.append(suml)
    return Nl

# Generating random value based on bernoulli distribution
p = input("Enter the bernoulli parameter p:")
k = Bernoulli(p)
print("The random bernoulli value generated using uniform distribution is: " + str(k))

N = input("Enter the no. of samples:")
n = input("Enter the binomial parameter n:")
p = input("Enter the binomial parameter p:")

#Generating N random samples from binomial distribution
binom = Binomial(N,n,p)

# Plotting graph here
plt.figure(1)
al = list(xrange(n+1))
alp = []

for i in range(0,n+1):
    temp = probab(n,i,p)
    alp.append(temp*N)
# Red line indicates the expected probability distribution
plt.plot(al,alp,'r')
# Blue bars represent frequency of values obtained through experiment/sampling
plt.hist(binom,bins=n)
plt.title("Binomial distribution")
plt.xlabel("Values")
plt.ylabel("Frequency")

plt.show()


# coding: utf-8

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.constants import g

#
# Fandenpendel:
# $$ \ddot{\theta} = -k^2 \sin(\theta) $$
#
# Als Näherung:
# Harmonischer Oszillator:
# $$ \ddot{\theta} = -k^2 \theta  $$
#
# mit
# $$
# \theta(0) = \theta_0, \, \dot{\theta} = 0, \, k^2 = \frac{g}{l}
# $$

# Allgemeine Lösung des harmonischen Oszillators:
# $$
# \theta(t) = A e^{ikt} + B e^{-ikt}
# $$
# Anfandsbedinungen einsetzen:
# $$
# \theta(t) = \theta_0 \cos(kt)
# $$
# und
# $$
# \dot{\theta}(t) = - \theta_0 k \sin(kt)
# $$

steps = 1000 # [1]
T = 8.0 # [s]
t = np.linspace(0, T, steps) # [s]
v0 = 0.0 # [m/s]
l = 1.0 # [m]
k_sq = g / l
k = np.sqrt(k_sq)

def rhs(y, t):
    theta, v = y[0], y[1]
    a = - k_sq * np.sin(theta)
    return np.array([v, a])

def compare_harm_osc_and_pendulum(theta0_deg):
    theta0 = np.deg2rad(theta0_deg)

    theta_harm_osc = theta0 * np.cos(k * t)
    v_harm_osc = - theta0 * k * np.sin(k * t)

    y0 = np.array([theta0, v0])
    ys_pendel = odeint(rhs, y0, t)
    theta_pendel = ys_pendel[:, 0]
    v_pendel  = ys_pendel[:, 1]

    plt.subplot(2, 1, 1)
    plt.title(r"Harmonic Oscillator vs. Pendulum ($\theta_0 = %.2f^\circ$)" % theta0_deg)
    plt.plot(t, theta_harm_osc, "k", label="Harmonic Oscillator")
    plt.plot(t, theta_pendel, "--r", label="Pendulum")
    plt.plot(t, (theta_harm_osc - theta_pendel), "b", label="Signed Error")
    plt.grid()
    plt.xlabel("time t [s]")
    plt.ylabel(r"angle $\theta$ [rad]")
    plt.legend()

    plt.subplot(2, 1, 2)
    plt.plot(theta_harm_osc, v_harm_osc, "k", label="Harmonic Oscillator")
    plt.plot(theta_pendel, v_pendel, "r", label="Pendulum")
    plt.xlabel(r"$\theta$")
    plt.ylabel(r"$\dot{\theta}$")
    plt.grid()
    plt.legend()

    plt.tight_layout()

plt.figure(1, figsize=(8,8))
compare_harm_osc_and_pendulum(5)

plt.figure(2, figsize=(8,8))
compare_harm_osc_and_pendulum(60)

plt.figure(3, figsize=(8,8))
compare_harm_osc_and_pendulum(170)
plt.show()


#!/usr/bin/python3

"""
    The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""

import numpy as np

def isprime(num: int) -> bool:
    for i in range(2, int(np.sqrt(num))+1):
        if num%i == 0:
            return False
    return True

def euler10() -> int:
    tot = 2
    i = 3
    while i < 2000000:
        if isprime(i):
            tot = tot + i
        i = i + 2
    return tot

tot = euler10()
print(tot)


import numpy as np

def f(x):

     s=x**2+x+1

     return s

def F(x):

   s=x**3/3+x**2/2+x

   return s

x=[0,0.5,1]

quad=(1/6)*(f(x[0])+4*f(x[1])+f(x[2]))

print('approximation with Trapezontial rule :', quad)
print('real solution of integration : ', F(1)-F(0))


'''
# Defining the sigmoid function for activations
def sigmoid(x):
    return 1/(1+np.exp(-x))

# Derivative of the sigmoid function
def sigmoid_prime(x):
    return sigmoid(x) * (1 - sigmoid(x))

# Input data
x = np.array([0.1, 0.3])
# Target
y = 0.2
# Input to output weights
weights = np.array([-0.8, 0.5])

# The learning rate, eta in the weight step equation
learnrate = 0.5

# The neural network output (y-hat)
nn_output = sigmoid(x[0]*weights[0] + x[1]*weights[1])
# or nn_output = sigmoid(np.dot(x, weights))

# output error (y - y-hat)
error = y - nn_output

# error term (lowercase delta)
error_term = error * sigmoid_prime(np.dot(x,weights))

# Gradient descent step
del_w = [ learnrate * error_term * x[0],
                 learnrate * error_term * x[1]]
# or del_w = learnrate * error_term * x
'''
import numpy as np

def sigmoid(x):
    """
    Calculate sigmoid
    """
    return 1/(1+np.exp(-x))

def sigmoid_prime(x):
    '''
    Derivative of the sigmoid function
    '''
    return sigmoid(x) * (1 - sigmoid(x))

learnrate = 0.5
x = np.array([1, 2])
y = np.array(0.5)

# Initial weights
w = np.array([0.5, -0.5])

# Calculate one gradient descent step for each weight
# TODO: Calculate output of neural network
nn_output = sigmoid(w.dot(x))

# TODO: Calculate error of neural network
error = y - nn_output

# TODO: Calculate change in weights
del_w = learnrate * error * x * sigmoid_prime(w.dot(x))

print('Neural Network output:')
print(nn_output)
print('Amount of Error:')
print(error)
print('Change in Weights:')
print(del_w)


from __future__ import print_function
from fenics import *
from mshr import *
import matplotlib.pyplot as plt
import numpy as np

# Create mesh
R = 1. # radius
N = 20 # mesh resolution
domain = Circle(Point(0., 0.), R)
mesh = generate_mesh(domain, N)
plot(mesh,linewidth=0.3)
plt.show()

#define function space
degreeElements = 1
FS = FunctionSpace(mesh, 'Lagrange', degreeElements)

#impose Dirichlet boundary conditions
def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(FS, Constant(0.), boundary)

#define function u and test function v
u = Function(FS)
v = TestFunction(FS)

# weak formulation of the problem
Res = -dot(grad(u), grad(v))*dx + v*dx

# solve the problem
solve(Res == 0, u, bc)

# plot solution
c = plot(u,mode='color',title='$u$')
plt.colorbar(c)
plot(mesh,linewidth=0.3)
plt.show()

# exact solution
uExact=Expression('(1-x[0]*x[0]-x[1]*x[1])/4',degree=2)

# Compute error (L2 norm)
error_L2 = errornorm(uExact, u, 'L2')
print("Error = ",error_L2)

# evaluate function at several points
print("u(0,0) = ",u(0,0))
print("u(0.5,0.5) = ",u(0.5,0.5))
#print("u(2,0) = ",u(2,0)) #point outside domain
print("u(1,0) = ",u(1,0))
# print("u(0,1) = ",u(0,1)) #point outside the discretized domain

# plot solution
tol=1e-2;
x=np.linspace(-1+tol,1-tol,40)
points = [(x_, 0) for x_ in x]
u_line = np.array([u(point) for point in points])
plt.plot(x,u_line,'k-')
plt.plot(x,(1-x*x)/4,'r--')
plt.xlabel('$x$')
plt.ylabel('$u(x,0)$')
plt.legend(['FEM','exact'])
plt.show()


import numpy as np

class FuzzyKmeans():
    """
    Fuzzy Kmeans calculates cluster probabilities in regards to euclidian distance
    Equivalent to vanilla Kmeans if we assign a given point to the cluster with the highest fuzzy probability
    Parameters:
        X: numpy array() data matrix
        U: cluster probabilities
        centers: center values
        m: numeric, the degree of uncertainty, (fuzziness of cluster)
    """
    def __init__(self,X,m=2):
        self.X = X
        self.m = m
        self.U = None
        self.centers = None

    def calculate_centers(self,U,X):
        """
        Recalculates centers using a linear combination of fuzzy probs and X values
        Normalized by the sum of all the fuzzy probs.
        """
        m = self.m
        ones = np.ones(X.shape[0]).reshape((X.shape[0],1))
        denom = (1/(U**m).dot(ones)).reshape(U.shape[0],)
        diagonal = np.diag(denom)
        centers = diagonal.dot((U**m).dot(X))
        return centers

    def calculate_fuzzy(self,X,centers):
        m = self.m
        m = 2/(m-1)
        us = np.ones(shape=(centers.shape[0],1))
        ones = np.ones(shape=(1,centers.shape[0]))
        for i in range(X.shape[0]):
            row = X[i].reshape(X.shape[1],1)
            row = row.dot(ones).T - centers
            norms = np.linalg.norm(row,2,axis=1).reshape(centers.shape[0],1)
            norms = (norms.dot(1/norms.T))**m
            sums = 1/np.sum(norms,axis=1).reshape(norms.shape[0],1)
            us = np.column_stack((us,sums))

        return us[:,1:]

    def fit(self,k,exit=0.01,seed=1,maxiterations=100,argmax=False):
        """
        Main clustering function
            k is the number of clusters
            exit is the exit criteria
            set argmax = True for normal K-means
        """
        X = self.X
        np.random.seed(seed)
        U = np.random.uniform(0,1,size=(k,X.shape[0])) #initialize cluster probabilities
        centers = self.calculate_centers(U,X)
        newcenters = 2*centers
        count = 0
        while np.linalg.norm((centers - newcenters),2) >= exit and count <= maxiterations:
            newcenters = centers
            U = self.calculate_fuzzy(X,centers)
            centers = self.calculate_centers(U,X)
            count += 1
        self.U = U
        self.centers = centers

    def predict(self,X,argmax=False):

        U = self.calculate_fuzzy(X,self.centers)
        if argmax:
            return (np.argmax(U,axis=0).T).reshape(X.shape[0],)
        return U.T


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

def doPCA():
	from sklearn.decomposition import PCA
	pca=PCA(n_components=2)
	pca.fit(array)
	return pca

df = pd.read_excel('defra.xlsx')
#print df

array=df.as_matrix()
#print array

cov_mat = np.cov([array[:,0],array[:,1],array[:,2]],array[:,3])
print('Covariance Matrix:\n', cov_mat)

mean_x = np.mean(array[:,0])
mean_y = np.mean(array[:,1])
mean_z = np.mean(array[:,2])
mean_k = np.mean(array[:,3])

mean_vector = np.array([[mean_x],[mean_y],[mean_z],[mean_k]])

#print('Mean Vector:\n', mean_vector)

#The scatter matrix is computed by the following equation:

scatter_matrix = np.zeros((4,4))
for i in range(array.shape[1]):
    scatter_matrix += (array[i,:].reshape(4,1) - mean_vector).dot((array[i,:].reshape(4,1) - mean_vector).T)

# eigenvectors and eigenvalues for the from the scatter matrix
eig_val_sc, eig_vec_sc = np.linalg.eig(scatter_matrix)

# eigenvectors and eigenvalues for the from the covariance matrix
eig_val_cov, eig_vec_cov = np.linalg.eig(cov_mat)

print eig_vec_cov
diagon=np.diagonal(cov_mat)

sum=np.sum(diagon)
print sum

print "From covariance matrix S, we know followings. \n\n 1. The total variance (trace of matrix) is %d " %  (sum)
for i in range(len(diagon)):
	pososto=(diagon[i]/float(sum))*100
	print " Variable X%d contributes %d/%d=%f%% " % (i+1,diagon[i],sum,pososto)

sum=np.sum(eig_val_cov)
print "\n\n 2.From Eigenvalues matrix L, we know followings."
for i in range(len(eig_val_cov)):
	pososto=(eig_val_cov[i]/float(sum))*100
	print "  The %d principal axis contains  %d/%d=%f%% " % (i+1,eig_val_cov[i],sum,pososto)

pca=doPCA()
print pca.explained_variance_ratio_
first_pc=pca.components_[0]
second_pc=pca.components_[1]
#third_pc=pca.components_[2]
#fourth_pc=pca.components_[3]

transformed_data=pca.transform(array)
for ii,jj in zip(transformed_data,array):
	plt.scatter(first_pc[0]*ii[0],first_pc[1]**ii[0],color="r")
	plt.scatter(second_pc[0]*ii[1],second_pc[1]**ii[1],color="c")
	#plt.scatter(third_pc[0]*ii[0],third_pc[1]**ii[0],color="g")
	#plt.scatter(fourth_pc[0]*ii[0],fourth_pc[1]**ii[0],color="k")
	plt.scatter (jj[0],jj[1],color="b")

plt.xlabel("bonus")
plt.ylabel("long-term incentive")
plt.show()

print transformed_data


import numpy as np
import matplotlib.pyplot as plt

def LagrangeInterpolation(xi, yi, points):
	m = points.size
	l = np.ones(xi.shape, dtype='double')
	ans = np.zeros(m)

	for j in range (0, m):
		for k in range(0, len(xi)):
			for i in range(0, len(xi)):
				if i != k:
					l[k] *= (points[j]-xi[i])/(xi[k]-xi[i])
		ans[j] = yi.dot(l)
		l = np.ones(xi.shape)

	return ans

values_x = np.array([-2,0,3,5], dtype='double')
values_y = np.array([3,-2,4,2], dtype='double')
points_x = np.linspace(-2, 5, num=41, endpoint=True)
points_y = LagrangeInterpolation(values_x, values_y, points_x)

plt.figure(figsize=(10,6),facecolor='white')
plt.plot(points_x,points_y,label = 'Interpolação de Lagrange',linewidth = 2)
plt.scatter(values_x,values_y,label = 'Pontos',linewidth = 2)
plt.xlabel('x',fontsize='large')
plt.ylabel('y',fontsize='large')
plt.title('')
plt.legend()
plt.show()


import numpy as np
from sklearn.datasets import load_iris
from sklearn import linear_model

def softmax(X_train,W,k):
#    print(W)
#    print(X_train.dot(W))
#    print(np.sum(X_train.dot(W),axis=1))
    return np.exp(X_train.dot(W[:,k]))/(np.sum(np.exp(X_train.dot(W))))

class LogisticRegression():
    def __init__(self,epsilon=10**-2,lamb=10**-2):
        self.epsilon=epsilon
        self.lamb=lamb
    def train(self,X_train,y_train):
        train_no,feature_no=X_train.shape
        self.n_cat=len(set(y_train))
        W=np.random.randn(feature_no+1,self.n_cat)
        W[:,-1]=np.zeros((feature_no+1))
        X_cat=np.ones((train_no,feature_no+1))
        X_cat[:,:feature_no]=X_train
        dw=np.zeros((feature_no+1,self.n_cat))

        old_W=np.full((W.shape),np.inf)
#        print(old_W-W)
#        print(abs(old_W-W)<self.epsilon)
        while np.any(abs(old_W-W)>self.epsilon):
#            print('pass')
            old_W=W

            for k in range(self.n_cat-1):
                summ=np.zeros((feature_no+1,1))
                for i in range(train_no):
#                    print(X_cat[i].T.shape)
#                    print(y_train[i]==k)
#                    print(softmax(X_cat[i],W,k,self.n_cat))
                    summ+=X_cat[i].reshape(feature_no+1,1)*(int(y_train[i]==k)-softmax(X_cat[i],W,k))

                dw[:,k]=summ.ravel()
#            print(dw)
#            print(W)

            W=W+self.lamb*dw
        self.W=W
        print(W)

    def predict(self,X_test):
        test_no,feature_no=X_test.shape
        X_cat=np.ones((test_no,feature_no+1))
        X_cat[:,:feature_no]=X_test
        result=np.zeros((test_no,self.n_cat))
        for i in range(test_no):
            for k in range(self.n_cat):
                result[i,k]=softmax(X_cat[i],self.W,k)
        y_pred=np.argmax(result,axis=1)

        return y_pred

if __name__=='__main__':
    iris=load_iris()
    X_train=iris.data
#    print(iris.data)
    y_train=iris.target
    index=np.random.permutation(np.arange(len(X_train)))
    X_train=X_train[index]
    y_train=y_train[index]
    X_test=X_train

    model=LogisticRegression()
    model.train(X_train,y_train)
    y_pred=model.predict(X_test)
    print('Logistic Regression',y_pred)
    print(sum(y_train==y_pred))

    model=linear_model.LogisticRegression(solver='sag',multi_class='multinomial')
    model.fit(X_train,y_train)
    y_pred=model.predict(X_test)
    print('sklearn',y_pred)
    print(sum(y_train==y_pred))
    print(model.coef_)

Supervised Machine Learning Algorithms/Logistic_Regression.py


import numpy as np
import drawPlot as dp

def linearRegression(x, y):

    avgX, avgY = np.mean(x), np.mean(y)

    a = np.sum( y * (x - avgX) ) / np.sum((x - avgX) * (x - avgX))

    b = avgY - a * avgX

    return(a, b)

def main():
    x = np.array([40,  43,  139, 182,  240])
    y = np.array([149, 157, 350, 367,  423])

    f = linearRegression(x, y)
    print("f(x) = {}x  {}".format(f[0], f[1]))

    price = float(input("Price $: "))

    size = (price-f[1]) / f[0]
    print("Size {}".format(size))

    dp.drawPlot(x, y, f)

if __name__ == "__main__":
    main()


from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')

def derivatives(t, y, vaccine_rate, birth_rate=0.01):
    """Defines the system of differential equations that
    describe the epidemiology model.

    Args:
        t: a positive float
        y: a tuple of three integers
        vaccine_rate: a positive float <= 1
        birth_rate: a positive float <= 1

    Returns:
        A tuple containing dS, dI, and dR
    """
    infection_rate = 0.3
    recovery_rate = 0.02
    death_rate = 0.01
    S, I, R = y
    N = S + I + R
    dSdt = (
        -((infection_rate * S * I) / N)
        + ((1 - vaccine_rate) * birth_rate * N)
        - (death_rate * S)
    )
    dIdt = (
        ((infection_rate * S * I) / N)
        - (recovery_rate * I)
        - (death_rate * I)
    )
    dRdt = (
        (recovery_rate * I)
        - (death_rate * R)
        + (vaccine_rate * birth_rate * N)
    )
    return dSdt, dIdt, dRdt

def integrate_ode(
    derivative_function,
    t_span,
    y0=(2999, 1, 0),
    vaccine_rate=0.85,
    birth_rate=0.01,
):
    """Numerically solve the system of differential equations.

    Args:
        derivative_function: a function returning a tuple
                             of three floats
        t_span: endpoints oif the time range to integrate over
        y0: a tuple of three integers (default: (2999, 1, 0))
        vaccine_rate: a positive float <= 1 (default: 0.85)
        birth_rate: a positive float <= 1 (default: 0.01)

    Returns:
        A tuple of three arrays
    """
    sol = solve_ivp(
        derivative_function,
        t_span,
        y0,
        args=(vaccine_rate, birth_rate),
    )
    ts, S, I, R = sol.t, sol.y[0], sol.y[1], sol.y[2]
    return ts, S, I, R

t_span = [0, 730]
t, S, I, R = integrate_ode(derivatives, t_span, vaccine_rate=0.0)

fig, ax = plt.subplots(1, figsize=(10, 5))
ax.plot(t, S, label='Susceptible', c='black', linestyle='solid', linewidth=1.75)
ax.plot(t, I, label='Infected', c='black', linestyle='dotted', linewidth=1.75)
ax.plot(t, R, label='Recovered', c='black', linestyle='dashed', linewidth=1.75)
ax.legend(fontsize=14, frameon=True, ncol=3, bbox_to_anchor=(0.85, 1.13))
ax.set_xlabel('Time', fontsize=14)
ax.set_ylabel('People', fontsize=14)
fig.savefig("plot_no_vaccine.pdf")

t, S, I, R = integrate_ode(derivatives, t_span, vaccine_rate=0.85)

fig, ax = plt.subplots(1, figsize=(10, 5))
ax.plot(t, S, label='Susceptible', c='black', linestyle='solid', linewidth=1.75)
ax.plot(t, I, label='Infected', c='black', linestyle='dotted', linewidth=1.75)
ax.plot(t, R, label='Recovered', c='black', linestyle='dashed', linewidth=1.75)
ax.legend(fontsize=14, frameon=True, ncol=3, bbox_to_anchor=(0.85, 1.13))
ax.set_xlabel('Time', fontsize=14)
ax.set_ylabel('People', fontsize=14)
fig.savefig("plot_with_vaccine.pdf")


import numpy as np

def gini_coefficient(x: np.ndarray) -> float:
    """
    Calculate the Gini coefficient of a distribution.

    Parameters
    ----------
    x : np.ndarray
        Array of values.

    Returns
    -------
    float
        Gini coefficient.
    """
     # Mean absolute difference
    mad = np.abs(np.subtract.outer(x, x)).mean()
    # Relative mean absolute difference
    rmad = mad/np.mean(x)
    # Gini coefficient
    g = 0.5 * rmad
    return g


from scipy import stats
import numpy as np

def kendall_tau(r1, r2):
    # Calculates the kendall tau between two ranking vectors
    tau, _ = stats.kendalltau(np.argsort(r1), np.argsort(r2))
    return tau

def spearman_r(r1, r2):
    # Calculates the spearman r between two ranking vectors
    rho, _ = stats.spearmanr(np.argsort(r1), np.argsort(r2))
    return rho


samples = 200

r = 0.83
r2 = 0.33
r3 = 0.56

# Generate pearson correlated data with approximately cor(X, Y) = r

import numpy as np

data = np.random.multivariate_normal([0, 0, 0 ], [[1, r, r2], [r, 1, r3], [r2, r3, 1] ], size=samples)

#X, Y = data[:,0], data[:,1]

X,Y,Z = data[:,0], data[:,1], data[:,2]

#X, Y = data[:,0], data[:,1]

# That's it! Now let's take a look at the actual correlation:

import scipy.stats as stats

print 'r=', stats.pearsonr(X, Y)[0]
print 'r2=', stats.pearsonr(X, Z)[0]
print 'r3=', stats.pearsonr(Y, Z)[0]


from math import gcd

from sage.all import GF
from sage.all import is_prime_power

def attack(outputs, modulus=None, multiplier=None, increment=None):
    """
    Recovers the parameters from a linear congruential generator.
    If no modulus is provided, attempts to recover the modulus from the outputs (may require many outputs).
    If no multiplier is provided, attempts to recover the multiplier from the outputs (requires at least 3 outputs).
    If no increment is provided, attempts to recover the increment from the outputs (requires at least 2 outputs).
    :param outputs: the sequential output values obtained from the LCG
    :param modulus: the modulus of the LCG (can be None)
    :param multiplier: the multiplier of the LCG (can be None)
    :param increment: the increment of the LCG (can be None)
    :return: a tuple containing the modulus, multiplier, and the increment
    """
    if modulus is None:
        assert len(outputs) >= 4, "At least 4 outputs are required to recover the modulus"
        for i in range(len(outputs) - 3):
            d0 = outputs[i + 1] - outputs[i]
            d1 = outputs[i + 2] - outputs[i + 1]
            d2 = outputs[i + 3] - outputs[i + 2]
            g = d2 * d0 - d1 * d1
            modulus = g if modulus is None else gcd(g, modulus)

        assert is_prime_power(modulus), "Modulus must be a prime power, try providing more outputs"

    gf = GF(modulus)
    if multiplier is None:
        assert len(outputs) >= 3, "At least 3 outputs are required to recover the multiplier"
        x0 = gf(outputs[0])
        x1 = gf(outputs[1])
        x2 = gf(outputs[2])
        multiplier = int((x2 - x1) / (x1 - x0))

    if increment is None:
        assert len(outputs) >= 2, "At least 2 outputs are required to recover the multiplier"
        x0 = gf(outputs[0])
        x1 = gf(outputs[1])
        increment = int(x1 - multiplier * x0)

    return modulus, multiplier, increment


import yfinance as yf
import numpy as np

start_date = '2015-01-01'
end_date = '2021-12-31'
stock_data = yf.download('GOOGL', start_date, end_date)

#######   Annual Volatility   #######
def annualized_volatility(data):
    df = data.copy()
    df["daily_return"] = df["Adj Close"].pct_change()
    vol = df["daily_return"].std() * np.sqrt(252)
    return vol

print("Annualized Volatility: " + str(annualized_volatility(stock_data) * 100) + '%')


# episode 28 unsupervised Machine Learning - Hierarchical Clustering with Mean Shift Scikit-learn and Python

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.cluster import MeanShift
from sklearn.datasets.samples_generator import make_blobs  # to generate random data samples

sns.set_style("darkgrid")

# Around those center we create our samples
centers = [[1, 1], [5, 5], [3, 10]]

# "y" are the real labels
X, y = make_blobs(n_samples = 200, centers = centers, cluster_std = 1)

# plotting our data with seaborn
sns.scatterplot(X[:,0], X[:,1])
plt.show()

# training our model
ms = MeanShift()
ms.fit(X)

# dont confuse those labels with "y". labels are the "predicted" labels. y are the real labels
labels = ms.labels_
cluster_centers = ms.cluster_centers_  # estimated centers of our data sets

# estimated number of cluster
n_clusters = len(np.unique(labels))

# Communication is the key
print("Number of estimated cluster: ", n_clusters)
# print("Labels: ", labels)
print("Cluster Centeres: ")
print(np.round(cluster_centers, 2))

plt.figure(figsize=(8,7))
sns.scatterplot(X[:, 0], X[:, 1], hue=labels, palette="summer", legend="full")
sns.scatterplot(cluster_centers[:, 0], cluster_centers [:, 1], marker="x", s=200, color="magenta", linewidth=4)
plt.show()


import numpy as np
import torch

def cart2polar(x):
    """Transform Cartesian to Polar Coordinates

    x: torch.Tensor, [dims] x 2
    return rho, theta
    """
    rho = torch.norm(x, p=2, dim=-1).unsqueeze(-1)
    theta = torch.atan2(x[..., 1], x[..., 0]).unsqueeze(-1)
    theta = theta + (theta < 0).type_as(theta) * (2 * np.pi)

    return rho, theta


import numpy as np

#Series temporais como listas
s1 = [168, 398, 451, 337, 186, 232, 262, 349, 189, 204, 220, 220, 207, 239, 259, 258,242, 331, 251, 323, 106, 1055, 170]
s2= [168, 400, 451, 300, 186, 200, 262, 349, 189, 204, 220, 220, 207, 239, 259, 258,242, 331, 251, 180, 106, 1055, 200]

#criando arrays a partir das listas
a = np.array(s1)
b = np.array(s2)

#calculo distancia euclidiana

#subtraçao
subtracao = a - b
quadrado = subtracao**2
somatorio = np.sum(quadrado)
distancia = np.sqrt(somatorio)

print("A distância euclidiana entre as séries é:\n",distancia)

print()

#valores medios
media = (a+b) / 2
print("A matriz com os valores médios \n",media)

print()

#valores maximos
max =np.maximum(a, b)
print("Matriz com valores máximos:\n",max )

print()

#valores minimos
min =np.minimum(a, b)
print("Matriz com valores mínimos:\n",min )


# Elastic Net Regression
#----------------------------------
#
# This function shows how to use TensorFlow to
# solve elastic net regression.
# y = Ax + b
#
# We will use the iris data, specifically:
#  y = Sepal Length
#  x = Pedal Length, Petal Width, Sepal Width

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()

# Create 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[1], x[2], x[3]] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])

# Declare batch size
batch_size = 50

# Initialize placeholders
x_data = tf.placeholder(shape=[None, 3], 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=[3,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

# Declare model operations
model_output = tf.add(tf.matmul(x_data, A), b)

# Declare the elastic net loss function
elastic_param1 = tf.constant(1.)
elastic_param2 = tf.constant(1.)
l1_a_loss = tf.reduce_mean(tf.abs(A))
l2_a_loss = tf.reduce_mean(tf.square(A))
e1_term = tf.multiply(elastic_param1, l1_a_loss)
e2_term = tf.multiply(elastic_param2, l2_a_loss)
loss = tf.expand_dims(tf.add(tf.add(tf.reduce_mean(tf.square(y_target - model_output)), e1_term), e2_term), 0)

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

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

# Training loop
loss_vec = []
for i in range(1000):
    rand_index = np.random.choice(len(x_vals), size=batch_size)
    rand_x = 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[0])
    if (i+1)%250==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
[[sw_coef], [pl_coef], [pw_ceof]] = sess.run(A)
[y_intercept] = sess.run(b)

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


import numpy as np

class Quaternion:

    def __init__(self, s=0, x=0, y=0, z=0):
        self.s = s
        self.x = x
        self.y = y
        self.z = z

    def __add__(self, other):
        res = Quaternion()
        res.x = self.x + other.x
        res.y = self.y + other.y
        res.z = self.z + other.z
        res.s = self.s + other.s
        return res

    def __sub__(self, other):
        res = Quaternion()
        res.x = self.x - other.x
        res.y = self.y - other.y
        res.z = self.z - other.z
        res.s = self.s - other.s
        return res

    def __mul__(self, other):
        res = Quaternion()
        if not isinstance(other, Quaternion):
            res.s = self.s * other
            res.x = self.x * other
            res.y = self.y * other
            res.z = self.z * other
        else:
            # doesn't satisfy commutative law.
            res.s = self.s * other.s - self.x * other.x - self.y * other.y - self.z * other.z
            res.x = self.s * other.x + self.x * other.s + self.y * other.z - self.z * other.y
            res.y = self.s * other.y - self.x * other.z + self.y * other.s + self.z * other.x
            res.z = self.s * other.z + self.x * other.y - self.y * other.x + self.z * other.s
        return res

    def __truediv__(self, other):
        if other == 0:
            raise ZeroDivisionError
        return self * (1. / other)

    def conjugate(self):
        res = Quaternion()
        res.s = self.s
        res.x = -self.x
        res.y = -self.y
        res.z = -self.z
        return res

    def normalize(self):
        return self / self.norm()

    def norm(self):
        return np.sqrt(self.s ** 2 + self.x ** 2 + self.y ** 2 + self.z ** 2)

    def inv(self):
        return self.conjugate() / self.norm() ** 2

    def dot(self, other):
        res = Quaternion()
        res.s = self.s * other.s
        res.x = self.x * other.x
        res.y = self.y * other.y
        res.z = self.z * other.z
        return res

    def rotate(self, p):
        return self * p * self.inv()

    def convert_to_rotation_matrix(self):
        R = np.array([
            [1 - 2 * self.y * self.y - 2 * self.z * self.z, 2 * self.x * self.y - 2 * self.s * self.z,
             2 * self.x * self.z + 2 * self.s * self.y],
            [2 * self.x * self.y + 2 * self.s * self.z, 1 - 2 * self.x * self.x - 2 * self.z * self.z,
             2 * self.y * self.z - 2 * self.s * self.x],
            [2 * self.x * self.z - 2 * self.s * self.y, 2 * self.y * self.z + 2 * self.s * self.x,
             1 - 2 * self.x * self.x - 2 * self.y * self.y]
        ])
        return R

    def load_from_rotation_matrix(self, R):
        self.s = np.sqrt(R.trace() + 1) / 2
        self.x = (R[1, 2] - R[2, 1]) / (self.s * 4)
        self.y = (R[2, 0] - R[0, 2]) / (self.s * 4)
        self.z = (R[0, 1] - R[1, 0]) / (self.s * 4)

    def load_from_axis_angle(self, theta, n):
        self.s = np.cos(theta / 2)
        n = n * np.sin(theta / 2)
        n = n.ravel()
        self.x = n[0]
        self.y = n[1]
        self.z = n[2]

    def convert_to_axis_angle(self):
        theta = 2 * np.arccos(self.s)
        n = np.array([self.x, self.y, self.z]) / np.sin(theta / 2)
        return theta, n

    def __str__(self):
        return f'[{self.s} {self.x} {self.y} {self.z}]'

if __name__ == '__main__':
    # "identity" quaternion can represent any rotation.
    R = np.array([
        [np.sqrt(2) / 2, -np.sqrt(2) / 2, 0.],
        [np.sqrt(2) / 2, np.sqrt(2) / 2, 0.],
        [0., 0., 1.]]
    )
    q = Quaternion()
    q.load_from_rotation_matrix(R)
    print('convert R to quaternion:', q)
    print('the norm of q:', q.norm())
    print('****take care：identity quaternion represent rotation, if norm(q)!=1, pls use norm.')
    res = q * q  # two time rotation, each time rotate 45 degree.
    theta, n = res.convert_to_axis_angle()
    print('q * q to axis angle:', np.rad2deg(theta), n)
    q2 = Quaternion()
    q2.load_from_axis_angle(theta, n)
    R2 = q2.convert_to_rotation_matrix()
    print('R2:\n', R2)
    print('q * inv(q):', q * q.inv())


import numpy as np

array = np.array([1, 2, 3])

print(array)
print(array.dtype)
print(type(array))
print(array.shape)
print(array.nbytes)
print()

print(np.zeros(5))
print(np.ones(5))
print(np.zeros_like(5))
print()

print(np.arange(1, 16, 4))
print(np.arange(1, 10))
print(np.arange(5))
print()

print(np.linspace(1, 15, 4))
print()

a = np.linspace(3, 33, 11)
b = np.linspace(-2, -22, 11)
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(5 * a)
print(10 + b)
print()

print(np.sin(a))
print(np.cos(a))
print(np.log(a))
print(a > b)
print()

c = np.arange(20)
print(np.any(c))
print(np.all(c))
print()

print(np.pi)
print(np.e)
print()

print(b.cumsum())
print()

print(np.sort(a))
print(a)
print()

c = np.hstack((a, b, 5 * b))
print(c)
print()

x1, x2, x3 = np.hsplit(a, [3, 5])
print(x1, x2, x3)
print()

print(np.append(a, [1, 2, 3, 4, 5]))
print(np.insert(1, 2, [-1, -1]))
print(a[::-1])
print(a[0:3])
print()

print(np.ones(3, 3))
print(np.eye(3))
print(np.diag(np.array([1, 2, 3, 4])))
print()

is_prime = np.ones(100, dtype=bool)
is_prime[:2] = False
n_max = int(np.sqrt(len(is_prime)))
for i in range(2, n_max):
    is_prime[2*i::i] = False
print(is_prime)


import math
import matplotlib.pyplot as plt
import scipy.optimize as op
import numpy as np
np.set_printoptions(suppress=True)

from ex2 import hypothesis, plot_cost_history

def plot_classification(positive, negative, theta, title):
	#from https://towardsdatascience.com/andrew-ngs-machine-learning-course-in-python-regularized-logistic-regression-lasso-regression-721f311130fb
	def mapFeaturePlot(x1,x2,degree):
		"""
		take in numpy array of x1 and x2, return all polynomial terms up to the given degree
		"""
		out = np.ones(1)
		for i in range(1,degree+1):
			for j in range(i+1):
				terms= (x1**(i-j) * x2**j)
				out= np.hstack((out,terms))
		return out

	plt.clf()
	plt.title(title)
	plt.plot(positive[:,0], positive[:,1], 'b+', label = 'y = 1')
	plt.plot(negative[:,0], negative[:,1], 'rx', label = 'y = 0')

	# Plotting decision boundary
	u_vals = np.linspace(-1,1.5,50)
	v_vals = np.linspace(-1,1.5,50)
	z = np.zeros((len(u_vals),len(v_vals)))

	for i in range(len(u_vals)):
		for j in range(len(v_vals)):
			z[i,j] = mapFeaturePlot(u_vals[i],v_vals[j],6) @ theta

	plt.contour(u_vals,v_vals,z.T,0)
	plt.ylabel('Microchip Test 2')
	plt.xlabel('Microchip Test 1')
	plt.legend()
	plt.show()

# Import data
data = np.loadtxt("ex2data2.txt", delimiter=",")

positive = data[data[:,2] == 1][:,0:2]
negative = data[data[:,2] == 0][:,0:2]

# 2.1 Visualizing the data
plt.ylabel('Microchip Test 2')
plt.xlabel('Microchip Test 1')

plt.plot(positive[:,0], positive[:,1], 'b+', label = 'y = 1')
plt.plot(negative[:,0], negative[:,1], 'rx', label = 'y = 0')

plt.legend()
plt.show()

# 2.2 Feature mapping
def map_feature(x1, x2):
	degree = 6
	out = np.ones(x1.shape[0])

	for i in range(1, degree + 1):
		for j in range(i + 1):
			out = np.c_[out, (x1 ** (i - j)) * (x2 ** j)]

	return out

X = map_feature(data[:, 0], data[:, 1])

# 2.3 Cost function and gradient
def cost_function_reg(theta, X, y, Lambda):
	m = X.shape[0]

	h = hypothesis(theta, X)
	error = np.sum(-y * np.log(h) - (1 - y) * np.log(1 - h))

	error = error / m

	return error + ((Lambda / (2*m)) * np.sum(theta ** 2))

# Evaluation
theta = np.zeros(X.shape[1])
y = data[:, 2]

# Set regularization parameter lambda to 1
Lambda = 1

# You should see that the cost is about 0.693.
assert round(cost_function_reg(theta, X, y, Lambda), 3) == 0.693

def gradient(theta, X, y, Lambda):
	m = X.shape[0]

	h = hypothesis(theta, X)

	# for j = 0
	theta[0] = np.sum((h - y) * X[:, 0]) / m

	# for j ≥ 1
	for j in range(1, theta.shape[0]):
		theta[j] = (np.sum((h - y) * X[:, j]) / m) + ((Lambda / m) * theta[j])

	return theta

# There is no fminunc in scipy, so I have used gradient descent with manually tuned alpha and iterations.
for iterations in [100, 1000]:
	for Lambda in [0, 0.5]:
		theta = np.zeros(X.shape[1])
		alpha = 0.1
		cost_history = []

		m = X.shape[0]

		for i in range(iterations):
			m = X.shape[0]

			h = hypothesis(theta, X)

			# for j = 0
			theta[0] = theta[0] - alpha * np.sum((h - y) * X[:, 0]) / m

			# for j ≥ 1
			for j in range(1, theta.shape[0]):
				theta[j] = theta[j] - alpha * (np.sum((h - y) * X[:, j]) / m) + ((Lambda / m) * theta[j])

			cost_history.append(cost_function_reg(theta, X, y, Lambda))

		#plot_cost_history(cost_history)
		cost = cost_function_reg(theta, X, y, Lambda)

		# The predict function will produce “1” or “0” predictions given a dataset
		# and a learned parameter vector θ.
		accuracy = np.mean((hypothesis(theta, X) >= 0.5).astype(float) == y) * 100

		result = 'Iterations = %d, lambda = %.1f, Accuracy = %.2f%%' % (iterations, Lambda, accuracy)

		print(result)

		#plot_classification(positive, negative, theta, result)


import numpy as np

'''
教材第45页
已知正实例点x1=(3，3)T,x2=[4,3]T,负实例点x3=(1,1)T
用感知机学习算法的对偶形式求感知机模型f(x)=sign(w·x+b)。
这里w=(w(1),w(2))T,x=(x(1),x(2))T
'''

def perceptron_antithesis():
    x = np.array([[3, 3], [4, 3], [1, 1]])
    y = np.array([1, 1, -1])
    aa = np.array([0, 0, 0])
    b = 0
    G = np.array([[18, 21, 6], [21, 25, 7], [6, 7, 2]])
    # print(np.matmul(x, x[0].T))
    for a in range(5):
        print('-----------第{}轮------------'.format(a + 1))
        for i in range(3):
            L = y[i] * (np.sum(aa * y * np.matmul(x, x[i].T)) + b)
            if L <= 0:
                print("x{},L={}".format(i + 1, L))
                aa[i] = aa[i] + 1
                print("a={}".format(aa))
                b = b + y[i]
                print("b={}".format(b))

if __name__ == "__main__":
    perceptron_antithesis()


import numpy as np

# Whiteboard: https://wbd.ms/share/v2/aHR0cHM6Ly93aGl0ZWJvYXJkLm1pY3Jvc29mdC5jb20vYXBpL3YxLjAvd2hpdGVib2FyZHMvcmVkZWVtLzk4ZjBjYzM3OTc1MzQ3YjY5ZTQ4OTA3MjYxODIxN2Y0X0JCQTcxNzYyLTEyRTAtNDJFMS1CMzI0LTVCMTMxRjQyNEUzRA==

def print_paths(n, start, end, arr):
    """
    We leverage the bijection between Catalan paths
    and valid parenthesizations. start and end are the
    coordinates of the starting and ending points in the
    Catalan paths.
    Note that end never changes, so passing it to the function
    is redundant. Also, n never changes.
    """
    # one less on the x-coordiate and one more on
    # the y-coordinate, means we're one step away
    # from end point. We could also check for start[1]-end[1]==1
    # but the abs makes it not specific to paths above the grid
    if start[0]==end[0]-1 and abs(start[1]-end[1])==1:
        arr.append(')')
        print(''.join(arr))
    ## This means we're at the peak (n,n).
    # We can never let the sum of the x and y-coordinates
    # exceed 2n. This ensures we stay within the square boundary
    # of the grid. We can never cross the line x=y by design.
    # the condition ensure we also never cross
    # x+y=2n, the other boundary.
    elif sum(start)==2*n:
        # Since we're at the peak, must move down.
        arr.append(')')
        start+=np.array([1,-1])
        print_paths(n,start,end,arr)
    # If we've touched the x-axis, we move up.
    ## this is specific to Catalan paths.
    elif start[1] == 0:
        # Must move up now.
        arr.append('(')
        start+=np.array([1,1])
        print_paths(n,start,end,arr)
    ## If within all boundaries, move up and also move down.
    else:
        arr_up = arr.copy()
        arr_down=arr.copy()
        arr_up.append('(')
        start1=start+np.array([1,1])
        print_paths(n,start1,end,arr_up)
        arr_down.append(')')
        start2=start+np.array([1,-1])
        print_paths(n,start2,end,arr_down)

def valid_parenth(n):
    start=np.array([0,0])
    end=np.array([2*n,0])
    arr=[]
    print_paths(n,start,end,arr)


from scipy import stats
import numpy as np
import plotly.graph_objs as go
import plotly.io as pio

def get_pdf(x_range, store_data):
    """ The function will return the pdf for a given beta distribution.

    Args:
        x_range: Array of x values for the plot.
        store_data: Array of values for a store. A value of 1 represents a positive customer
                    result while an value of 0 represents a negative result.

    Returns:
        A numpy array representing the PDF for the given beta distribution.
    """
    alpha = sum(store_data)
    beta = len(store_data) - alpha
    return stats.beta(a=alpha, b=beta).pdf(x_range)

def generate_plotly_area_fig(x_range, x_min, x_max, y_min, y_max, pdf_a, pdf_b, n_customers):
    """ The function will return a Plotly figure.

    Args:
        x_range: Array of x values for the plot.
        x_min: Lower limit to display for the x-axis
        x_max: Upper limit to display for the x-axis
        y_min: Lower limit to display for the x-axis
        y_max: Upper limit to display for the x-axis
        pdf_a: The PDF generated by the data from store A
        pdf_b: The PDF generated by the data from store B

    Returns:
        A Plotly figure
    """
    trace1 = go.Scatter(
        x=x_range,
        y=pdf_a,
        name='Store A',
        fill='tozeroy',
        line = dict(
          color = '#006b3c'
            )
        )

    trace2 = go.Scatter(
        x=x_range,
        y=pdf_b,
        name='Store B',
        fill='tozeroy',
        line = dict(
            color = '#c41e3a'
            )
        )

    layout = go.Layout(
        xaxis=dict(
            title='Conversion Rate',
            range=[x_min, x_max]
        ),
        yaxis=dict(
            range=[y_min, y_max]
        ),
        title=f'Conversion Rate Posterior Distributions After {n_customers} Users'
    )

    data = [trace1, trace2]
    fig = go.Figure(data=data, layout=layout)
    return fig

# n_customers = [200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000]
def generate_plotly_frames(data_store_a, data_store_b, n_customers):
    """ The function will return a list of figures for Plotly to animate.

    Args:
        data_store_a: An array of zeros and ones, where ones represent a "success" at a given store/display.
                      In this case, a success could be defined as a person deciding to walk into the store.
        data_store_b: An array at zeros and ones like data_store_a but for a different store/display.
        n_customers:  A list with the number of customers

    Returns:
        A list of plotly figures that can be used to plot sequential images
    """
    frames = []
    for n in n_customers:
        x_range = np.arange(0, 1.001, 0.001)
        pdf_a = get_pdf(x_range, data_store_a[:n])
        pdf_b = get_pdf(x_range, data_store_b[:n])
        fig = generate_plotly_area_fig(x_range, 0, .1, 0, 125, pdf_a, pdf_b, n)
        frames.append(fig)

    return frames

def export_frame_images(frames, base_fig_name):
    """ The function will return a list of figures for Plotly to animate.

    Args:
        frames: A list of Plotly figures
        base_fig_name: The base name that you want the output files saved as. If the base name is  "flagshipStore" then
                       the files will be output as flagshipStore0.png, flagshipStore1.png, etc.

    Returns:
        This function exports a sequence of PNG files that can then be used to turn into a gif.
    """
    for i, frame in enumerate(frames):
        pio.write_image(frame, base_fig_name + str(i) + '.png')


import numpy as np

class DHs:
    def __init__(self, **kwargs):
        self.alpha = kwargs['alpha']
        self.theta = kwargs['theta']
        self.d = kwargs['d']
        self.a = kwargs['a']

    def DH(self):

        wholeDT = []

        for i in range(len(self.theta)): # you can use others it doesnt matter
            aco = 0 if ((np.cos(np.deg2rad(self.alpha[i] % 360)) < 1e-10) & (np.cos(np.deg2rad(self.alpha[i] % 360)) > -1e-10)) else np.cos(np.deg2rad(self.alpha[i] % 360))
            asi = 0 if ((np.sin(np.deg2rad(self.alpha[i] % 360)) < 1e-10) & (np.sin(np.deg2rad(self.alpha[i] % 360)) > -1e-10)) else np.sin(np.deg2rad(self.alpha[i] % 360))
            tco = 0 if ((np.cos(np.deg2rad(self.theta[i] % 360)) < 1e-10) & (np.cos(np.deg2rad(self.theta[i] % 360)) > -1e-10)) else np.cos(np.deg2rad(self.theta[i] % 360))
            tsi = 0 if ((np.sin(np.deg2rad(self.theta[i] % 360)) < 1e-10) & (np.sin(np.deg2rad(self.theta[i] % 360)) > -1e-10)) else np.sin(np.deg2rad(self.theta[i] % 360))

            DT = [
                [tco, -tsi, 0, self.a[i]],
                [tsi*aco, tco*aco, -asi, -self.d[i]*asi],
                [asi*tsi, asi*tco, aco, self.d[i]*aco],
                [0, 0, 0, 1]
                ]

            wholeDT.append(DT)

        return np.reshape(wholeDT, [len(self.theta), 4, 4]), len(self.theta)

    def multDT(self):

        multipliedDTs = np.matmul(
            self.DH()[0][self.DH()[1] - 2],
            self.DH()[0][self.DH()[1] - 1]
            )

        if self.DH()[1] > 2:
            for i in range(self.DH()[1] - 3, -1, -1):
                multipliedDTs = np.matmul(
                    self.DH()[0][i],
                    multipliedDTs
                    )
        return multipliedDTs

    def pointInPlane(self, nokta, **axis):
        if axis['axis'] == 'global':
            return np.matmul(self.multDT(), np.reshape(np.append(nokta, [1]), [4, 1]))
        else:
            return np.matmul(np.linalg.inv(self.multDT()), np.reshape(np.append(nokta, [1]), [4, 1]))


# The Nelson-Siegel-Svensson is a popular extension of the 4-parameter Nelson-Siegel method to 6 parameters. It is an algorithm for interpolatin/extrapolating the yield curve among other uses.
# The Scennson introduces two extra parameters to better fit the variety of shapes of either the instantaneous forward rate or yield curves that are observed in practice.
# A desirable property of the model is that it produces a smooth and well behaved forward rate curve.
# Another desirable property is the intuitive explanation of the parameters. beta0 is the long term interest rate and beta0+beta1 is the instantaneous short-term rate.
# To find the optimal value of the parameters, the Nelder-Mead simplex algorithm is used (Already implemented in the scipy package). The link to the optimization algorithm is
# Gao, F. and Han, L. Implementing the Nelder-Mead simplex algorithm with adaptive parameters. 2012. Computational Optimization and Applications. 51:1, pp. 259-277

from nelsonsiegelsvensson import *
import numpy as np

## Inputs
#   - Observed yield rates (YieldVec)
#   - Maturity of each observed yield (TimeVec)
#   - Initial guess for parameters (beta0, beta1, beta2,beta3, labda0 , and lambda1)
#   - Target maturities (TimeResultVec)

TimeVec = np.array([1,2,5,10,25])
YieldVec = np.array([0.0039, 0.0061, 0.0166, 0.0258, 0.0332])
beta0   = 0.1 # initial guess
beta1   = 0.1 # initial guess
beta2   = 0.1 # initial guess
beta3   = 0.1 # initial guess
lambda0 = 1 # initial guess
lambda1 = 1 # initial guess

TimeResultVec = np.array([1,2,5,10,25,30,31]) # Maturities for yields that we are interested in

## Implementation
OptiParam = NSSMinimize(beta0, beta1, beta2, beta3, lambda0, lambda1, TimeVec, YieldVec) # The Nelder-Mead simplex algorithem is used to find the parameters that result in a curve with the minimum residuals compared to the market data.

# Print the yield curve with optimal parameter to compare with the data provided
print(NelsonSiegelSvansson(TimeResultVec, OptiParam[0], OptiParam[1], OptiParam[2], OptiParam[3], OptiParam[4], OptiParam[5]))


'''
Reference
[1] Dabhade S A ,  Bewoor M S ,  Dabhade S A , et al. Face Recognition using Principle Component Analysis[J]. Core.kmi.open.ac.uk, 2013, ETCSIT(100):45 - 50.

The variable names used in this source code are consistent with this paper.
'''
import numpy as np
import sys

from settings import EIGENIMAGES_CONTRAST
from settings import FACE_COUNT
from settings import IMAGE_COUNT_PER_FACE
from settings import TRAIN_COUNT_PER_FACE
from image_parser import read_all_images
from image_parser import write_an_image
from math_utils import get_dist

def get_omegas_of_training_set(X, M_):
    '''
    m represents the mean image.
    W represents the mean centered images.
    '''
    m = np.array([np.mean(X, axis=1)]).T
    W = X-m
    '''
    C = np.dot(W,W.T), which The size of C is N × N which could be enormous.
    The eigenvalues and eigenvectors of C are obtained by eigen decomposition of C_ (C').
    The first (M − 1) (λi, ei) of C are given by (μi, Wdi) of C_, respectively.
    Wdi needs to be normalized in order to be equal to ei.
    The dimension of C_ is much smaller than that of C, reducing operations greatly.
    '''
    C_ = np.dot(W.T, W)
    eigenvalues_C, eigenvectors_C_ = np.linalg.eig(C_)
    eigenvectors_C = np.dot(W, eigenvectors_C_)
    for i in range(len(eigenvectors_C[0])):
        eigenvectors_C[:, i] /= np.linalg.norm(eigenvectors_C[:, i])
    '''
    {Wdi} corresponding to nonzero {μi} of C_ produce an orthonormal basis for the subspace,
    within which most image data can be represented with a small amount of error.
    {Wdi} are sorted from high to low according to their corresponding {μi}.
    The 90% of the total variance is contained in the first 5% - 10% of the dimensions.
    Only the M_ eigenvectors with the largest eigenvalues are examined.
    '''
    assert M_ < len(eigenvalues_C)
    sorted_eigenvalues_rank = np.argsort(eigenvalues_C)
    '''
    The vectors ei are also images, so called, eigenimages, or eigenfaces.
    '''
    E = []
    max_rank = len(eigenvalues_C)-1
    for i in range(M_):
        rank = sorted_eigenvalues_rank[max_rank-i]
        E.append(eigenvectors_C[:, rank])
    E = np.array(E).T
    '''
    A facial image can be projected onto M_ dimensions by computing omega.
    '''
    omegas_of_training_set = np.dot(E.T, W)
    return m, E, omegas_of_training_set

def recognize_face(m, E, omegas_of_training_set, face):
    W = face-m
    omega_of_face = np.dot(E.T, W)
    assert len(omegas_of_training_set[0]) == TRAIN_COUNT_PER_FACE*FACE_COUNT
    min_dist = get_dist(
        np.array([omegas_of_training_set[:, 0]]).T, omega_of_face)
    face_recognized = 0
    for i in range(FACE_COUNT):
        for j in range(TRAIN_COUNT_PER_FACE):
            if i == 0 and j == 0:
                continue
            dist = get_dist(
                np.array([omegas_of_training_set[:, TRAIN_COUNT_PER_FACE*i+j]]).T, omega_of_face)
            if dist < min_dist:
                min_dist = dist
                face_recognized = i
    return face_recognized

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print('Please specify a series of `M_` via command line parameters.')
        print(f'0 < `M_` < {TRAIN_COUNT_PER_FACE*FACE_COUNT}')
        print('E.g. python pca_recognizer.py 199 (...)')
        sys.exit()

    print('###### Principal Component Analysis (PCA) Face Recognition ######')
    print('The face recognition results of each person are listed in the following format:')
    print('Face Number | Image Number | Face Recognized | Right or Wrong (T/F)\n...\nError Rate')
    training_images, test_images = read_all_images()
    for i in range(1, len(sys.argv)):
        M_ = int(sys.argv[i])
        print('################################################################################')
        print(f'The dimension of omega is set to {M_}.')
        m, E, omegas_of_training_set = get_omegas_of_training_set(
            training_images, M_)
        for j in range(len(E[0])):
            write_an_image(E[:, j]*EIGENIMAGES_CONTRAST, 'eigenimages', j+1)
        print('The eigenimages of training set are as follows:')
        print(E)
        print(f'(Its shape: {len(E)} * {len(E[0])})')
        print('You can view them at file://./output/eigenimages/\n')
        error_count = 0
        for j in range(FACE_COUNT):
            error_count_j = 0
            for k in range(IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE):
                face = np.array(
                    [test_images[:, (IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE)*j+k]]).T
                face_recognized = recognize_face(
                    m, E, omegas_of_training_set, face)
                t_or_f = 'T'
                if face_recognized != j:
                    t_or_f = 'F'
                    error_count_j += 1
                print(f'{j+1}\t{k+1}\t{face_recognized+1}\t{t_or_f}')
            error_count += error_count_j
            print(f'{error_count_j} / {IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE} = {error_count_j/(IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE)}\n')
        print('Total Error Rate:')
        print(f'{error_count} / {(IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE)*FACE_COUNT} = {error_count/((IMAGE_COUNT_PER_FACE-TRAIN_COUNT_PER_FACE)*FACE_COUNT)}')
        print('################################################################################')


import numpy as np
import sympy as sp
from sympy.utilities.lambdify import lambdify, implemented_function

def Weierstrass_Enneper(f, g, z, imag_unit=1j):
    """
    Compute the Weierstrass Enneper parametrization for given 'Weierstrass data'.
    :param sympy expression f: h'/g, with h the height function
    :param sympy expression g: Gauss map.
    :param sympy variable z: Complex variable.
    :param imag_unit: Representation of imaginary unit, e.g. 1j (numpy) or sympy.I.
    :return: Weierstrass Enneper parametrization as a tuple of sympy expressions.
    """
    psi1 = sp.integrate(f*(1-g**2)/2, z)
    psi2 = imag_unit*sp.integrate(f*(1+g**2)/2, z)
    psi3 = sp.integrate(f*g, z)

    return psi1, psi2, psi3

def Enneper_parametrization(k, z, verbose=True):
    """
    Compute a parametrization of the higher order Enneper surface of order k.
    :param int k: Order of the Enneper surface
    :param sympy variable z: Complex variable.
    :param bool verbose: Whether to output auxilliary information.
    :return: function s parametrizing the higher order Enneper surface of order k
    """
    f = 2
    g = z**k

    psi = Weierstrass_Enneper(f, g, z)
    if verbose:
        print(f"Minimal curve psi_{k} = {psi}")

    psi_func = lambdify(z, psi)
    polar = lambda r, phi: r*np.cos(phi) + 1j*r*np.sin(phi)

    return lambda u, v: map(np.real, psi_func(polar(u, v)))


import numpy as np
from scipy.integrate import simps
from pwtools.num import norm_int

def test_norm_int():
    # simps(y, x) == 2.0
    x = np.linspace(0, np.pi, 100)
    y = np.sin(x)

    for scale in [True, False]:
        yy = norm_int(y, x, area=10.0, scale=scale)
        assert np.allclose(simps(yy,x), 10.0)


import math

import numpy as np
import pandas as pd
import reader
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold

# file path constants
FILE_PATH_TRAIN = "train1b.csv"

# feature transformation functions
functions = [lambda x: float(x), lambda x: math.pow(x, 2), lambda x: math.exp(x), lambda x: math.cos(x), lambda x: float(1)]

# read input data
data = reader.read_csv(FILE_PATH_TRAIN, False)

# drop the first column
data = pd.DataFrame.drop(data, columns='Id')

# splitting the data set into x & y values
# y-values
Y = pd.DataFrame(data, columns=['y'], copy=True)
Y = Y['y'].values

# x-values
X = pd.DataFrame(data, columns=['x1', 'x2', 'x3', 'x4', 'x5'], copy=True)

# new data frames for feature transformations
quad = pd.DataFrame(data, columns=['x6', 'x7', 'x8', 'x9', 'x10'])
exp = pd.DataFrame(data, columns=['x11', 'x12', 'x13', 'x14', 'x15'])
cos = pd.DataFrame(data, columns=['x16', 'x17', 'x18', 'x19', 'x20'])
const = pd.DataFrame(data, columns=['x21'])

# performing the feature transformations
for index, row in X.iterrows():
    # 1. transformation: quadratic
    quad['x6'][index] = functions[1](X['x1'][index])
    quad['x7'][index] = functions[1](X['x2'][index])
    quad['x8'][index] = functions[1](X['x3'][index])
    quad['x9'][index] = functions[1](X['x4'][index])
    quad['x10'][index] = functions[1](X['x5'][index])

    # 2. transformation: exponential
    exp['x11'][index] = functions[2](X['x1'][index])
    exp['x12'][index] = functions[2](X['x2'][index])
    exp['x13'][index] = functions[2](X['x3'][index])
    exp['x14'][index] = functions[2](X['x4'][index])
    exp['x15'][index] = functions[2](X['x5'][index])

    # 3. transformation: cosine
    cos['x16'][index] = functions[3](X['x1'][index])
    cos['x17'][index] = functions[3](X['x2'][index])
    cos['x18'][index] = functions[3](X['x3'][index])
    cos['x19'][index] = functions[3](X['x4'][index])
    cos['x20'][index] = functions[3](X['x5'][index])

    # 4. transformation: constant
    const['x21'][index] = functions[4](1)

# concatenating all data frames
X_transformed = pd.concat([X, quad, exp, cos, const], axis=1)
X_transformed = X_transformed.values

# storing the evaluation scores
eval = []

# storing the coefficients
av_coeffs = []

# alpha parameter values
alphas = [10.0e-4, 10.0e-3, 10.0e-2, 10.0e-1, 10.0e0, 10.0, 20.0, 100.0]

for entry in alphas:
    # creating the 10-fold split
    ten_fold = KFold(n_splits=10, shuffle=False)

    # fold counter
    i = 1

    # create model
    ridge = Ridge(alpha=entry, fit_intercept=True, normalize=False,
                  copy_X=True, max_iter=None, tol=1e-10, solver='auto')

    # errors per alpha value
    errors = []

    # coefficients per alpha value
    coeffs = []

    # training the ridge cv model
    for train_ind, val_ind in ten_fold.split(X_transformed, Y):
        # x & y training set (no separated test set)
        x_train = X_transformed[train_ind, :]
        y_train = Y[train_ind]

        # x & y validation set (no separated test set)
        x_val = X_transformed[val_ind, :]
        y_val = Y[val_ind]

        # train the model
        ridge.fit(x_train, y_train)

        # remember the coefficients
        coeffs.append(ridge.coef_)

        # evaluate with validation set
        y_val_predictions = ridge.predict(x_val)

        # compute root mean square error
        rmse = math.sqrt(mean_squared_error(y_val, y_val_predictions))
        errors.append(rmse)

        # printing alpha, fold number and root mean squared error
        # print("Alpha:\t", str(entry)+"\t", "Fold:", str(i)+"\t", "RMSE:\t", rmse)
        # print("Coefficients:\t", ridge.coef_)

        # increment fold counter
        i += 1

    # compute mean errors for rmse of each alpha
    eval.append(np.mean(errors))

    # compute the averaged coefficients
    av_coeffs.append(np.mean(coeffs, axis=0, dtype=np.float64))

# print out the rmse averages for all lambdas
min_rmse = 10000000
min_rmse_ind = None

# printing a summary
print("Summary of all alpha values tested:")

for ind in range(0, len(alphas)):
    print("Alpha:\t", alphas[ind], "\t\t", "Avg RMSE:\t", eval[ind])

    if eval[ind] < min_rmse:
        min_rmse = eval[ind]
        min_rmse_ind = ind

# preparing to write the coefficients to file
out = {"Avg Coeffs": av_coeffs[min_rmse_ind]}

# output data frame
best_coeffs = pd.DataFrame(data=out)

# printing test output
print()
print("Result Written To File:")
print("Alpha:\t", alphas[min_rmse_ind], "\tRMSE:\t", min_rmse)
print()
print(best_coeffs.head(5))

# # write to csv-file
# best_coeffs.to_csv("task1b_ridge_10-fold-cv_Nr4.csv", sep=',', index=False, header=False)


from math import sin,cos,exp,sqrt,log
from numpy import arange
import sys
import matplotlib.pyplot as plt

# Parâmetros da exibição dos gráficos
plt.rcParams['xtick.labelsize'] = 24
plt.rcParams['ytick.labelsize'] = 24
plt.rcParams['axes.labelsize'] = 28

RC = float(sys.argv[1])

def vinquad(t):
    vin = 1.
    if int(2*t)%2 == 1:
        vin = -1.
    return vin

def fquad(x,t):
    return (vinquad(t) - x)/RC

def passo_rk4(f,x,t,h):            # Calcula um passo no método de RK4
    k1 = h*f(x,t)
    k2 = h*f(x+0.5*k1,t+0.5*h)
    k3 = h*f(x+0.5*k2,t+0.5*h)
    k4 = h*f(x+k3,t+h)
    return (k1+2.0*(k2+k3)+k4)/6.0

def sol_exata(t,a,xa):             # Válida somente para fquad
    n = int((t-a)/0.5)
    x0 = xa
    t0 = a
    sinal = 1.
    vin = sinal
    for i in range(n):
        xf = vin - (vin - x0)*exp(-0.5/RC)
        t0 = t0 + 0.5
        x0 = xf
        sinal = -sinal
        vin = sinal
    xf = vin - (vin - x0)*exp(-(t-t0)/RC)
    return xf

def main():

    a = 0.00         # Início do intervalo
    b = 10.0         # Final do intervalo
    xa = 0.0         # Condição inicial, ou seja, x(a)

    N_exato = 1000    # Número de pontos para o gráfico da solução exata
    h_exato = (b-a)/N_exato
    t_exato = arange(a,b,h_exato)
    x_exato = []

    for t in t_exato:  # Criando as listas para o gráfico da função
        x_exato.append(sol_exata(t,a,xa))

    N = 1000           # Número de passos da integração numérica
    h = (b-a)/N        # Tamanho do passo da integração

    xrk4 = xa
    t_rk4 = arange(a,b,h)
    x_rk4 = []

    for t in t_rk4:   # Realizando a integração numérica
        x_rk4.append(xrk4)
        xrk4 += passo_rk4(fquad,xrk4,t,h)

    plt.figure(figsize=(12,9))
    plt.plot(t_rk4,x_rk4,'r-',t_exato,x_exato,'bo')
    plt.xlabel("$t$")
    plt.ylabel("$V_{out}(t)$")
    plt.tight_layout()
    plt.show()

if __name__ == '__main__':
    main()


"""
  Name     : c8_12_normality_test_for_IBM.py
  Book     : Python for Finance (2nd ed.)
  Publisher: Packt Publishing Ltd.
  Author   : Yuxing Yan
  Date     : 6/6/2017
  email    : yany@canisius.edu
             paulyxy@hotmail.com
"""

from scipy import stats
from matplotlib.finance import quotes_historical_yahoo_ochl as getData
import numpy as np

ticker='IBM'
begdate=(2012,1,1)
enddate=(2016,12,31)

p =getData(ticker, begdate, enddate,asobject=True, adjusted=True)
ret = (p.aclose[1:] - p.aclose[:-1])/p.aclose[1:]
print 'ticker=',ticker,'W-test, and P-value'
print(stats.shapiro(ret))

print( stats.anderson(ret) )


# Copyright 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
A simple implementation of PCA (Principle Component Analysis)
using numpy. Written to apply to neural network activations.

"""

import numpy as np

def get_pca(acts, compute_dirns=False):
    """ Takes in neuron activations acts and number of components.
    Returns principle components and associated eigenvalues.

    Args:
            acts: numpy array, shape=(num neurons, num datapoints)
            n_components: integer, number of pca components to reduce
                          to

    """

    assert acts.shape[0] < acts.shape[1], ("input must be number of neurons"
                                           "by datapoints")

    # center activations
    means = np.mean(acts, axis=1, keepdims=True)
    cacts = acts - means

    # compute PCA using SVD
    U, S, V = np.linalg.svd(cacts, full_matrices=False)

    return_dict = {}
    return_dict["eigenvals"]  = S
    return_dict["neuron_coefs"] = U.T
    if compute_dirns:
        return_dict["pca_dirns"] = np.dot(U.T, cacts) + means

    return return_dict


from util import *
import numpy as np
import cv2

def questao9A(img):
	'''
	aplicar o filtro
	  0  -1	   0
		+	+
	 -1	  5   -1
		+	+
	  0	  -1   0

	'''
	kernel = np.array(([0,-1,0],[-1, 5, -1],[ 0, -1, 0]))
	return applyFilter3x3(img, kernel)

def questao9B(img):
	'''
	aplicar o filtro
	  0   0	   0
		+	+
	  0	  1    0
		+	+
	  0	   0   -1
	'''
	kernel = np.array(([0, 0, 0], [0, 1, 0], [0, 0, -1]))
	return applyFilter3x3(img, kernel)

def questao9ATest(img):
	'''
	Usa a funcao da propria biblioteca prar observar o resultado
	'''
	kernel = np.array(([0,-1,0],[-1, 5, -1],[ 0, -1, 0]))
	resultImage = img # pegar o mesmo tamanho da imaem original
	cv2.filter2D(src=img, ddepth=-1, kernel=kernel, dst=resultImage)

	return resultImage

def questao9BTest(img):
	kernel = np.array(([0, 0, 0], [0, 1, 0], [0, 0, -1]))
	resultImage = img # pegar o mesmo tamanho da imaem original
	cv2.filter2D(src=img, ddepth=-1, kernel=kernel, dst=resultImage)

	return resultImage


import numpy as np
import matplotlib.pyplot as plt

from scipy.spatial.distance import pdist, squareform
from scipy import exp
from scipy.linalg import eigh
from sklearn.datasets import make_circles
from sklearn.decomposition import PCA
from matplotlib.ticker import FormatStrFormatter

def rbf_kernel_pca(x, gamma, n_comps):
    """Radial-Basis Kernel PCA"""

    # Calc pairwise squared Euclidian dists in mxn dimm dataset
    sq_dists = pdist(x,'sqeuclidean')

    # Convert pairwise squared Eculidian dist into a square matrix
    mat_sq_dists = squareform(sq_dists)

    # Compute the symetric kernel matrix
    k = exp(-gamma * mat_sq_dists)

    # Center the kernel matrix
    n = k.shape[0]
    one_n = np.ones((n, n)) / n
    k = k - one_n.dot(k) - k.dot(one_n) + one_n.dot(k).dot(one_n)

    # Obtain eigenpairs from the centered kernel matrix
    eigvals, eigvecs = eigh(k)

    # projected ds
    x_pc = np.column_stack((eigvecs[:, -i]
                            for i in range(1, n_comps + 1)))

    return x_pc

x, y = make_circles(n_samples=1000, random_state=123, noise=0.1, factor=0.2)

plt.scatter(x[y == 0, 0], x[y == 0, 1],
            color='red', marker='^', alpha=0.5)
plt.scatter(x[y == 1, 0], x[y == 1, 1],
            color='blue', marker='o', alpha=0.5)
plt.show()

scikit_pca = PCA(n_components=2)
x_spca = scikit_pca.fit_transform(x)
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(7, 3))

ax[0].scatter(x_spca[y == 0, 0], x_spca[y == 0, 1],
              color='red', marker='^', alpha=0.5)
ax[0].scatter(x_spca[y == 1, 0], x_spca[y == 1, 1],
              color='blue', marker='o', alpha=0.5)
ax[1].scatter(x_spca[y == 0, 0], np.zeros((500, 1)) + 0.02,
              color='red', marker='^', alpha=0.5)
ax[1].scatter(x_spca[y == 1, 0], np.zeros((500, 1)) - 0.02,
              color='blue', marker='o', alpha=0.5)

ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')
plt.show()

x_kpca = rbf_kernel_pca(x, gamma=15, n_comps=2)
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(7, 3))
ax[0].scatter(x_kpca[y == 0, 0], x_kpca[y == 0, 1],
              color='red', marker='^', alpha=0.5)
ax[0].scatter(x_kpca[y == 1, 0], x_kpca[y == 1, 1],
              color='blue', marker='o', alpha=0.5)
ax[1].scatter(x_kpca[y == 0, 0], np.zeros((500, 1)) + 0.02,
              color='red', marker='^', alpha=0.5)
ax[1].scatter(x_kpca[y == 1, 0], np.zeros((500, 1)) - 0.02,
              color='blue', marker='o', alpha=0.5)

ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')
ax[0].xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))
ax[1].xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import time
import scipy.integrate as inte

def system_of_ode(x,U):
    u , v = U
    du = v
    dv = 2*v/(1-x) - u/((1-x)**4)
    return np.array([du , dv])

# f(x)
def exact(x):
    y = np.sin(1/(1-x))
    return y

def main():

    # u(0) , v(0)
    u0 , v0 = np.sin(1) , np.cos(1)
    system_of_ode_0 = [u0 , v0]

    # LSODA method is an implicit method. Note that although it is a faster method, advanced explicit methods such as 'DOP853' are generally more precise.
    method = 'LSODA'

    start_time = time.clock()
    s = inte.solve_ivp(fun=system_of_ode,t_span=(0,0.99),y0=system_of_ode_0,method=method,rtol=2.3e-14,atol=1e-20)
    end_time = round(time.clock() - start_time, 3)

    # u(x) , v(x)
    u , v  = s.y
    x = s.t
    exact_f = exact(x)

    print('Method:', method)
    print('Number of data points:', len(u))
    print('Execution time:', end_time, 'seconds')

    # Difference of u(x) and f(x) ( Difference of numerical solution and exact solution; computed in the origianl c++ program )
    difference = exact_f-u

    # Plot of u(x)
    plt.plot(x, u,'b')
    plt.xlabel(r'$x$',fontsize=16)
    plt.ylabel(r'$u(x)$',fontsize=16)
    plt.tight_layout()
    plt.show()

    # Plot of v(x)
    plt.plot(x, v,'g')
    plt.xlabel(r'$x$',fontsize=16)
    plt.ylabel(r'$v(x)$',fontsize=16)
    plt.tight_layout()
    plt.show()

    # Plot of difference, using symmetric log
    plt.plot(x, difference,'.-r')
    plt.yscale('symlog',linthresh=1e-15)
    plt.xlabel(r'$x$',fontsize=16)
    plt.ylabel(r'$difference$',fontsize=16)
    plt.tight_layout()
    plt.show()

if __name__ == '__main__':
    main()


import numpy as np
import matplotlib.pyplot as plt

info_list = []
def InverseShift(A, mu, converge_range):

    r, c = A.shape

    if r != c:
        raise Exception("not a square matrix")

    #initialize eigenvectors
    vec_list = []
    lambda_list = []
    diff_list = []

    idx = 0
    vec_init = np.zeros(r)
    vec_init[-1] = 1
    vec_list.append(vec_init)

    #initialize eigenvalues
    lambda_init = vec_init.dot(A.dot(vec_init))
    lambda_list.append(lambda_init)

    diff_init = float("inf")
    diff_list.append(diff_init)

    I = np.identity(r)

    while diff_list[idx] > converge_range:
        mat = A - (mu*I)
        #solve the (A-(mu)I)w = v^(k-1) for w=vec_new
        vec_new = np.linalg.solve(mat, vec_list[idx])
        #normalize the newly found vec_new=w
        vec_new = vec_new / np.linalg.norm(vec_new)
        #add this to the vector list
        vec_list.append(vec_new)
        lambda_new = vec_new.dot(A.dot(vec_new))
        lambda_list.append(lambda_new)
        diff = np.abs(lambda_new - lambda_list[idx])
        diff_list.append(diff)
        idx = idx + 1

    print_log(idx, vec_list, lambda_list, diff_list)

    #plot lambda_list
    x = [i for i in range(idx+1)]
    if len(x) > 20:
        x = x[:21]
        diff_list = diff_list[:21]

    plt.plot(x, diff_list)
    plt.show()
    plt.savefig("IwS_difference_list_plot")
    return vec_list[-1], lambda_list[-1]

def print_log(idx, vec_list, lambda_list, diff_list):
    print("Inverse Iteration with Shift:")
    print('Number of Iterations:', idx)
    cor_eig_vec = vec_list[idx]
    sm_eig_val = round(lambda_list[idx], 4)
    inv_eig_val = np.reciprocal(sm_eig_val)
    print('Inverse Eigenvalue:', inv_eig_val) #
    print('Eigenvalue:', sm_eig_val)
    print('Corresponding Eigenvector:', cor_eig_vec)

#Examples to run on
D = np.array([[2, 2, -1], [-5, 9, -3], [-4, 4, 1]]) # eigenvals of D are 3, 4, and 5
E = np.array([[-4, 1, 1], [0, 3, 1], [-2, 0, 15]]) # eigenvals of E are ~ -3.9095, 3.0243 and 14.8852
InverseShift(E, 10, 0.00001)


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

def modelo(x):
    return 2 * x ** 1.345 + 0.75 * random.random()

x0, x1 = 0, 10
n = 100
x = x0 + (x1 - x0) * np.random.rand(n)
x.sort()
y = [modelo(xi) for xi in x]

def best_pow(x, y):

    new_x = [math.log(xi) for xi in x]
    new_y = [math.log(yi) for yi in y]
    sum_x = sum(new_x)
    sum_x2 = sum(xi ** 2 for xi in new_x)
    A = [[len(new_x), sum_x], [sum_x, sum_x2]]
    sum_new_y = sum(new_y)
    sum_new_xy = sum(xi * yi for xi, yi in zip(new_x, new_y))
    B = [sum_new_y, sum_new_xy]
    a0, a1 = np.linalg.solve(A, B)
    a, b = math.exp(a0), a1
    return a, b

a, b = best_pow(x, y)

def bpow(x):
    return a * x ** b

if __name__ == '__main__':

    print(a, b)
    t = np.linspace(min(x),max(x), 100)
    bpowt = [bpow(i) for i in t]
    plt.plot(t, bpowt, color='orange', linewidth=3, zorder=10)
    plt.scatter(x, y, zorder=1)
    plt.savefig('best_pow.png')


# encoding='utf-8'

'''
/**
 * This is the solution of No.62 problem in the LeetCode,
 * the website of the problem is as follow:
 * https://leetcode-cn.com/problems/unique-paths/
 *
 * The description of problem is as follow:
 * ==========================================================================================================
 * 一个机器人位于一个 m x n 网格的左上角 （起始点在下图中标记为“Start” ）。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角（在下图中标记为“Finish”）。
 * 问总共有多少条不同的路径？
 * 例如，上图是一个7 x 3 的网格。有多少可能的路径？
 *
 * 示例 1:
 * 输入: m = 3, n = 2
 * 输出: 3
 * 解释:
 * 从左上角开始，总共有 3 条路径可以到达右下角。
 * 1. 向右 -> 向右 -> 向下
 * 2. 向右 -> 向下 -> 向右
 * 3. 向下 -> 向右 -> 向右
 *
 * 来源：力扣（LeetCode）
 * ==========================================================================================================
 *
 * @author zhangyu (zhangyuyu417@gmail.com)
 */
'''
import numpy as np

class Solution:
    def unique_paths(self, m: int, n: int) -> int:
        '''
            独特的路径
        Args:
            m:数字m
            n:数字n
        Returns:
            返回数量
        '''
        if m == 0 or n == 0:
            return 0
        if m == 1 or n == 1:
            return 1
        dp = np.zeros((m + 1, n + 1))
        for i in range(m + 1):
            dp[i][0] = 1
        for j in range(n + 1):
            dp[0][j] = 1
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        return dp[m - 1][n - 1]

if __name__ == '__main__':
    m, n = 7, 3
    solution = Solution()
    paths = solution.unique_paths(m, n)
    assert paths == 28


#Pell number
import numpy as np
def __pow(x, n, I):
    if n==0:
        return I
    elif n == 1:
        return x
    else:
        y = __pow(x,n//2,I)
        y = y.dot(y)
        if n % 2:
            y = x.dot(y)
        return y

def PellNumber(index):
    F = __pow(np.array(((2,1),(1,0)),dtype=object),index,np.eye(2))
    return F[0][1]

def Binetformula(index=1,roundi=True):
    from sympy import Pow as mmmpow
    from sympy import Integer as mmmint
    from sympy import N as mmmn
    q = 2**(1/2)
    r = (mmmpow(1+q,index)-mmmpow(1-q,index))/(2*q)
    if(roundi):
        return mmmint(r)
    else:
        return mmmn(r)

#Pell–Lucas
def CompanionPellLucasNumber(Pell,roundi=True):
    from sympy import Integer as mmmint
    from sympy import N as mmmn
    r = Pell*(2**(1/2))
    if(roundi):
        return mmmint(r)
    else:
        return mmmn(r)

def CompanionPellNumber(index=1,roundi=True):
    from sympy import Pow as mmmpow
    from sympy import Integer as mmmint
    from sympy import N as mmmn
    q = 2**(1/2)
    r = (mmmpow(1+q,index)-mmmpow(1-q,index))/(2)
    if(roundi):
        return mmmint(r)
    else:
        return mmmn(r)

def PellNumbers(toEnd):
    Pells=[1,2,5]
    i=3
    while i<toEnd:
        num=Pells[i-1]*2+Pells[i-2]
        Pells.append(num)
        i+=1
    return Pells

def doTest(toPrint=False,toProgress=False,start=0,toEnd=1000,algo="s"):
    s = set()
    KK = 10000
    from IPython.display import clear_output
    for i in range(start,toEnd+1):
        if(toProgress and (i<KK or (i>=KK and i%(KK/100)==0))):
            clear_output(wait=True)
            print(i,end="\t")
        if(algo=="s"):
            pell = PellNumber(i)
        elif(algo=="binet"):
            pell = Binetformula(i)
        elif(algo=="companion"):
            pell = CompanionPellNumber(i)
        elif(algo=="pelllucas"):
            pell = CompanionPellLucasNumber(i)
        if(pell):
            s.add(pell)
            if(toPrint and not toProgress):
                print(pell,end=", ")
        if(toProgress and (i<KK or (i>=KK and i%(KK/100)==0))):
            print(s)
    if(not toPrint):
        return s

#print(Binetformula(6))
#print(PellNumber(6))
#doTest(True,False,0,10000)


#Acessar elementos
import numpy as np

x = np.linspace(start = 10, stop = 100, num = 10)
print("x: ", x)
print("Shape: ",x.shape)
print('''
Primeiro numero é: {}
Segundo numero é: {}
Terceiro numero é: {}
Ultimo numero é: {}
'''.format(x[0],x[1],x[2],x[9]))

#Slicing
print("x: ",x)
print("Os cincos primeiros mumeros: ", x[0:5])
print("Os dois primeiros numeros: ", x[:2])
print("Os dois ultimos numeros: ", x[-2: ])

print('')

#Acessando elementos D2
x = x.reshape(2, 5) # reshape(remodelar) de x para 2 linhas e 5 colunas
print("x:\n ", x)
print(f'''
Primeira linha, segunda coluna: {x[0,1]}
Segunda lina, penultima coluna: {x[1,-2]}
Ultima linha, ultima coluna: {x[1,-4]}
Ultima linha, ultima coluna: {x[-1, -1]}
''')

#Slicing
print("x:\n",x)
print(f'''
Primeira linha inteira: {x[0,:]}
Primeira linha, segunda ate a quarta coluna: {x[0,1:4]}
Ultima coluna inteira:\n {x[:,[-1]]}
''')

#Compartilhamento de memoria
a = np.array([1,2,3,4])
print("A:", a)
z = a[0:4].copy()
z[0] = -90
print("Z:",z)
print("A:",a)
 #Se não for usado o metodo copy(), o array principal(a) será alterado.


#!/usr/bin/env python
# coding: utf-8

# # Week 01 Example Statistics in Python 3
# This Notebook introduces basic statistical analysis from week 01 using Python 3.x.
#
# Jupyter notebooks blend Markdown (rich formatted text) with software code and output to ease the learning process.
#

# Version: 01
#
# Author: Chris Kennedy

# In[1]:

import pandas as pd
from scipy import stats

# In[2]:

dfWine = pd.read_excel(r'W1 - Wine Quality.xlsx')

# In[3]:

dfWine.describe()

# ## Practice Question 1
# What is the 99% confidence interval for the average alcohol level of a bottle of wine?

# In[4]:

n = dfWine['alcohol'].count()
print("# Wines: %6.2f" % n)

# In[5]:

avg = dfWine['alcohol'].mean()
print("Average Alcohol: %6.4f" % avg)

# In[6]:

stderr = dfWine['alcohol'].sem()
print("Standard Error: %6.4f" % stderr)

# In[7]:

t_CI = stats.t.ppf(0.995,df = n - 1)
print("T-statistic: %6.4f" % t_CI)

# In[8]:

moe = t_CI * stderr
print("Margin of error: %6.4f" % moe)

# In[9]:

print("Lower: %6.4f" % (avg - moe))
print("Upper: %6.4f" % (avg + moe))

# In[10]:

# Concise using stats package directly:
stats.t.interval(0.99, loc=avg, scale=stderr, df = n-1)

# ## Practice Question 2
# What is the 90% confidence interval around the proportion of white wines that are rated very good quality (7 or higher)?
#

# In[11]:

filteredDF = dfWine[dfWine['type'] == 'white']
n = filteredDF['type'].count()

# In[12]:

qfilteredDF = filteredDF[filteredDF['quality'] >= 7]
nq = qfilteredDF['type'].count()

# In[13]:

proportion  = nq / n
print("Proportion: %5.2f%%" % (proportion*100))

# In[14]:

stderr = (proportion * (1 - proportion) / n)**0.50
print("Standard error: %5.2f%%" % (stderr*100))

# In[15]:

z_CI = stats.norm.ppf(0.95)

# In[16]:

print ("Z for 90%%: %6.4f" % z_CI)

# In[17]:

stats.norm.interval(0.90, loc=proportion*100, scale=stderr*100)

# ## Question 3
# Can you conclude (at the 5% significance level) that the average fixed acid level for all wines is above 7.2?

# In[18]:

def getStatistic(x, H0, stderr):
    return (x - H0) / stderr

# H$_0$: $\mu \le 7.2$
#
# H$_a$: $\mu > 7.2$
#

# In[19]:

H0 = 7.20
avg = dfWine['fixed acidity'].mean()
stderr = dfWine['fixed acidity'].sem()
tStatistic = getStatistic(avg, H0, stderr)
nWines = dfWine['fixed acidity'].count()
print("T-stat: %7.4f" %tStatistic)
dfWine['fixed acidity'].describe()

# Right tail test due to alternative hypothesis.

# In[20]:

prob_value = 1 - stats.t.cdf(tStatistic, df=nWines-1)
print(prob_value)

# In[21]:

stats.norm.interval(0.9, loc=5,scale=2
                )

# ### End of Notebook!


import tensorflow as tf
import numpy as np

def sigmoid(x):
    return 1.0/(1+np.exp(-x))
#
# 计算方式：对输入的logits先通过sigmoid函数计算，再计算它们的交叉熵，但是它对交叉熵的计算方式进行了优化，使得的结果不至于溢出。
# 适用：每个类别相互独立但互不排斥的情况：例如一幅图可以同时包含一条狗和一只大象。
# output不是一个数，而是一个batch中每个样本的loss,所以一般配合tf.reduce_mean(loss)使用。
#计算公式图片 https://upload-images.jianshu.io/upload_images/5877934-fb698517859fea13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/720/format/webp
# 5个样本三分类问题，且一个样本可以同时拥有多类
y = np.array([[1,0,0],[0,1,0],[0,0,1],[1,1,0],[0,1,0]])
logits = np.array([[12,3,2],[3,10,1],[1,2,5],[4,6.5,1.2],[3,6,1]])
# 按计算公式计算
y_pred = sigmoid(logits)
E1 = -y*np.log(y_pred)-(1-y)*np.log(1-y_pred)
print(E1)     # 按计算公式计算的结果
# 按封装方法计算
sess =tf.Session()
y = np.array(y).astype(np.float64) # labels是float64的数据类型
E2 = sess.run(tf.nn.sigmoid_cross_entropy_with_logits(labels=y,logits=logits))
print(E2)     # 按 tf 封装方法计算

if E1.all() == E2.all():
    print("True")
else:
    print("False")
# 输出的E1，E2结果相同


'''
Tests for the measure module
'''
# import
import numpy as np
import pytest

import molecool

# unit testing function names should start with 'test'
def test_calculate_distance():

    r1 = np.array([0,0,0])
    r2 = np.array([0,1,0])

    expected_distance = 1

    calculated_distance = molecool.calculate_distance(r1,r2)

    # test: compare expected result and calculated result (from your code)

    assert expected_distance == calculated_distance

# unit testing function names should start with 'test'
def test_calculate_angle():

    rA = np.array([0,0,-1])
    rB = np.array([0,0,0])
    rC = np.array([1,0,0])

    expected_angle = 90

    calculated_angle = molecool.calculate_angle(rA,rB,rC,degrees=True)

    # test: compare expected result and calculated result (from your code)

    assert pytest.approx(expected_angle) == calculated_angle

@pytest.mark.parametrize("p1, p2, p3, expected_angle",[
    (np.array([np.sqrt(2)/2, np.sqrt(2)/2, 0]), np.array([0, 0, 0]), np.array([1, 0, 0]), 45),
    (np.array([0, 0, -1]), np.array([0, 1, 0]), np.array([1, 0, 0]), 60),
])
def test_calculate_angle_many(p1, p2, p3, expected_angle):
    calculated_value = molecool.calculate_angle(p1, p2, p3, degrees=True)

    assert pytest.approx(expected_angle) == calculated_value


"""
% -------------------------------------------------------------
%                                          Matlab code
% -------------------------------------------------------------
% grpah partition using the eigenvector corresponding to the second
% smallest  eigenvalue
% grpah partition using the eigenvector corresponding to the second
% smallest  eigenvalue
t=[randn(500,2)+repmat([-2,-2],500,1) ;randn(500,2)+repmat([2,2],500,1)];
scatter(t(:,1),t(:,2))
W=squareform(pdist(t));
A=W<3;      % create adjacency matrix (set connected notes equal to one)
D       = sum(A,1);
L       = diag(D)-A;
Lsym    = diag(D.^-0.5)*L*diag(D.^-0.5);
[u,s,v] = svd(Lsym);

figure; plot(u(:, (end-1)))
F       = u(:, (end-1));
plot(F);title('Second smallest non-zero eigenvalue eigenvector');
scatter(t(F<0,1),t(F<0,2),'bo','filled');hold on
scatter(t(F>0,1),t(F>0,2),'go','filled');
"""
# Pytorch equivalent code
import torch
from torch.autograd import Variable

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

import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib as mpl

color_map =  plt.get_cmap('jet')

def distance_matrix(mat):
    d= ((mat.unsqueeze (0)-mat.unsqueeze (1))**2).sum (2)**0.5
    return d

# Generate Clusters
mat = torch.cat([torch.randn(500,2)+torch.Tensor([-2,-3]),   torch.randn(500,2)+torch.Tensor([2,1])])
plt.scatter(mat[:,0].numpy(),mat[:,1].numpy())
plt.show(block=False)
##-------------------------------------------
#         Compute distance matrix and then the Laplacian
##-------------------------------------------
d= distance_matrix(mat);
da=d<2;
plt.figure()
plt.imshow(da.numpy())
plt.show(block=False)

D= ((da.float()).sum(1)).diag()
L = D -da.float()
plt.figure()
plt.title("Laplacian")
plt.imshow(L.numpy())
plt.show(block=False)

Lsym=torch.mm(torch.mm(torch.diag(torch.pow(torch.diag(D),-0.5)),L),torch.diag(torch.pow(torch.diag(D),-0.5)));
plt.figure()
plt.imshow(Lsym.numpy())
plt.title("Symmetric Laplacian")
plt.show(block=False)

[u,s,v]=torch.svd(Lsym)

# plot fiedler vector

plt.figure()
plt.title('Fiedler vector')
plt.plot(u[:,-2].numpy());
plt.show(block=False)
norm  = colors.Normalize(vmin=-1, vmax=1)

scalarMap = cm.ScalarMappable( norm=norm , cmap=color_map)

plt.figure()
plt.title('clusters')
for i in range(len(u[:,-2])):
	if u[i,-2]<0:
		color = scalarMap.to_rgba(-1)
		plt.scatter(mat[i,0],mat[i,1], color=color,marker='o')
	else:
		color = scalarMap.to_rgba(1)
		plt.scatter(mat[i,0],mat[i,1], color=color,marker='*')

plt.show(block=False)

raw_input("Press Enter to exit..")
plt.close('all')