import tkinter as tk
from tkinter.constants import LEFT, RIGHT
import numpy as np

def quadratic_bezier(t, p0, p1, p2):
    """
    :return: Quadratic bezier formular according to https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Quadratic_B%C3%A9zier_curves
    """
    return (1 - t) * ((1 - t) * p0 + t * p1) + t * ((1 - t) * p1 + t * p2)

def cubic_bezier(t, p0, p1, p2, p3):
    """
    :return: Cubic bezier formular according to https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B%C3%A9zier_curves
    """
    return (1 - t) * quadratic_bezier(t, p0, p1, p2) + t * quadratic_bezier(t, p1, p2, p3)

def draw_bezier_curve_quadratic(p, line_color, resolution=20):
    """
    Draw a quadratic bezier curve.
    :param p: control points of the spline
    :line_color: color of the drawn line
    :resolution: number of segments (each segment is a tiny straight line)
    """
    for i in range(len(p)):
        i = i * 2
        if i + 2 < len(p):
            b_prev = p[i]
            for res_index in range(resolution):
                t = float(res_index) / resolution
                b = quadratic_bezier(t, p[i], p[i + 1], p[i + 2])
                can.create_line(b_prev[0], b_prev[1], b[0], b[1], fill=line_color)
                b_prev = b

def draw_bezier_curve_cubic(p, line_color, resolution=20):
    """
    Draw a cubic bezier curve.
    :param p: control points of the spline
    :line_color: color of the drawn line
    :resolution: number of segments (each segment is a tiny straight line)
    """
    for i in range(len(p)):
        i = i * 3
        if i + 3 < len(p):
            b_prev = p[i]
            for res_index in range(resolution + 1):
                t = float(res_index) / resolution
                b = cubic_bezier(t, p[i], p[i + 1], p[i + 2], p[i + 3])
                can.create_line(b_prev[0], b_prev[1], b[0], b[1], fill=line_color)
                b_prev = b

def dot(p0, p1):
    """
    :return: dot product of the given 2d vectors
    """
    return p0[0] * p1[0] + p0[1] * p1[1]

def norm(p):
    """
    :return: norm of the given 2d vector
    """
    return np.sqrt(p[0] * p[0] + p[1] * p[1])

def normalize(p):
    """
    :return: normalized 2d vector
    """
    return 1 / norm(p) * p

def project_point_on_line(p, p0, p1):
    """
    :return: the projected point p on the line that is defined by (p0, p1). (p0, p1) must be normalized.
    """
    d = (p1 - p0)
    return p0 + dot(d, p - p0) * d

def project_point_on_line_fac(p, p0, p1):
    """
    :return: the factor that needs to be multiplied to get the projected point p on the line that is defined by (p0, p1). (p0, p1) must be normalized.
    """
    d = (p1 - p0)
    return dot(d, p - p0)

def draw_points(points, line_color):
    """
    Draw spheres for each given point.
    :param points: list of points to draw.
    """
    radius = 5
    for p in points:
        can.create_oval(p[0] - radius, p[1] - radius, p[0] + radius, p[1] + radius, outline=line_color)

def draw_connected_points(p, line_color):
    """
    Draw a dashed lines that connectes the given points p
    :param p: - list/array of points
    """
    for i in range(len(p) - 1):
        can.create_line(p[i][0], p[i][1], p[i + 1][0], p[i + 1][1], dash=(10,10), fill=line_color)

def connect_line(points, connect_ends, factor):
    """
    Creates a cubic bezier curve that connects the given points.
    Control points are calculated in a way that the resulting curve intersects the given points.
    :param connect_ends: bool if the start and end of the curve should be connected.
    :param factor: defines the amount of "curvature". 0 makes the corners edges while larger values (> 400) makes them completely round.
    :return: list of the cubic bezier control points
    """
    if len(points) < 3:
        return ()

    if connect_ends:
        bezier_points = np.zeros((3 * len(points), 2))
        for i in range(len(points)):
            iPrev = (i - 1) % len(points)
            iNext = (i + 1) % len(points)
            # factor_used = min(factor, 0.5 * norm(points[i] - points[i - 1]), 0.5 * norm(points[i + 1] - points[i]))
            factor_used = min(factor, 0.5 * norm(points[i] - points[iPrev]))
            dir = normalize(points[iNext] - points[iPrev])
            bezier_points[(3 * i - 1) % len(bezier_points)] = points[i] - factor_used * dir
            bezier_points[3 * i] = points[i]

            factor_used = min(factor, 0.5 * norm(points[iNext] - points[i]))
            bezier_points[(3 * i + 1) % len(bezier_points)] = points[i] + factor_used * dir
        bezier_points = np.append(bezier_points, np.array([bezier_points[0]]), axis=0)
    else:
        bezier_points = np.zeros((3 * len(points) - 2, 2))
        bezier_points[0] = points[0]
        factor_used = min(factor, 0.5 * norm(points[1] - points[0]))
        bezier_points[1] = points[0] + factor_used * normalize(points[1] - points[0])
        factor_used = min(factor, 0.5 * norm(points[len(points) - 1] - points[len(points) - 2]))
        bezier_points[len(bezier_points) - 2] = points[len(points) - 1] - factor_used * normalize(points[len(points) - 1] - points[len(points) - 2])
        bezier_points[len(bezier_points) - 1] = points[len(points) - 1]
        for i in range(len(points) - 1):
            if i == 0:
                continue
            factor_used = min(factor, 0.5 * norm(points[i] - points[i - 1]))
            dir = normalize(points[i + 1] - points[i - 1])
            bezier_points[3 * i - 1] = points[i] - factor_used * dir
            bezier_points[3 * i] = points[i]

            factor_used = min(factor, 0.5 * norm(points[i + 1] - points[i]))
            bezier_points[3 * i + 1] = points[i] + factor_used * dir

    draw_bezier_curve_cubic(bezier_points, line_color)
    return bezier_points

# List of list of points. Each list represents it's own curve that is separated from the others.
points_list = [np.array([[100, 100],
                    [200, 200],
                    [100, 300],
                    [400, 400],
                    [500, 350],
                    [350, 200]])]

# main window
window = tk.Tk()
window.title("Bezier Demo")
window.config(height=1000, width=1000)

# canvas
canvas_width = 1200
canvas_height = 800
can = tk.Canvas(window, bg = 'white', height=canvas_height, width=canvas_width)
can.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

line_color = "#476042"

def redraw2(show_control_points, show_connected_points, connect_ends, factor):
    """
    Redraws the canvas.
    :param show_control_points: show the cubic bezier control points.
    :param show_connect_points: draw a circle for all points that should be connected.
    :param connect_ends: connect the ends of the cubic bezier splines
    :param factor: "curvature" factor. Defines how round the corners are.
    """
    can.delete("all")

    for points in points_list:
        if show_connected_points:
            draw_points(points, 'blue')
        bezier_points = connect_line(points, connect_ends, factor)
        if len(bezier_points) > 0 and show_control_points:
            draw_connected_points(bezier_points, 'red')

def redraw():
    redraw2(show_control_points_var.get() == 1, show_connected_points_var.get() == 1, connect_ends_var.get(), factorSlider.get())

def append_new_curve():
    """
    Add a new curve that will be considered in future mouse clicks.
    """
    points_list.append(np.zeros((0,2)))

# Event handlers

# Mouse left click
def mouse_left_clicked(event):
    global points_list

    if (event.state & 0x4) != 0: # pressing ctrl while left clicking, automatically sets a new curve
        append_new_curve()

    points_list[len(points_list) - 1] = np.append(points_list[len(points_list) - 1], np.array([[event.x, event.y]]), axis=0)
    redraw()
can.bind("<Button-1>", mouse_left_clicked)

# Mouse right click
def mouse_right_clicked(event):
    global points_list
    points_list[len(points_list) - 1] = points_list[len(points_list) - 1][:-1]
    redraw()
can.bind("<Button-2>", mouse_right_clicked)

# add other controls to the window
settingsFrame = tk.Frame()
settingsFrame.pack(side=RIGHT)

def var_changed():
    redraw()

# show control points check box
show_control_points_var = tk.IntVar()
show_control_points_var.set(1)
c1 = tk.Checkbutton(settingsFrame, text='Control points', variable=show_control_points_var, onvalue=1, offvalue=0, command=var_changed)
c1.pack(fill="x")

# show connected points check box
show_connected_points_var = tk.IntVar()
show_connected_points_var.set(1)
c2 = tk.Checkbutton(settingsFrame, text='Connected points', variable=show_connected_points_var, onvalue=1, offvalue=0, command=var_changed)
c2.pack(fill="x")

# connect ends check box
connect_ends_var = tk.IntVar()
connect_ends_var.set(1)
connect_ends_checkbox = tk.Checkbutton(settingsFrame, text='Connect ends', variable=connect_ends_var, onvalue=1, offvalue=0, command=var_changed)
connect_ends_checkbox.pack(fill="x")

# curvature factor slider
def slider_changed(value):
    redraw()
factorSlider = tk.Scale(settingsFrame, from_=0, to=400, orient=tk.HORIZONTAL, command=slider_changed)
factorSlider.set(50)
factorSlider.pack()
def clear_command():
    global points_list
    points_list = ([np.zeros((0,2))])
    redraw()
clearButton = tk.Button(settingsFrame, text="Clear", command=clear_command)
clearButton.pack()

# new curve button
def new_curve_command():
    global points_list
    append_new_curve()
    redraw()
newCurveButton = tk.Button(settingsFrame, text="New curve", command=new_curve_command)
newCurveButton.pack()
can.pack()

# Initial redraw of the canvas.
redraw()

# center window
window.eval('tk::PlaceWindow . center')

window.mainloop()


r"""
Solve Poisson equation in 1D with mixed Dirichlet and Neumann bcs

    \nabla^2 u = f,

The equation to solve is

     (\nabla^2 u, v) = (f, v)

Use any combination of Dirichlet and Neumann boundary conditions.

"""
import os
import sympy as sp
import numpy as np
from shenfun import inner, div, grad, TestFunction, TrialFunction, \
    Array, Function, FunctionSpace, dx, legendre, extract_bc_matrices

# Use sympy to compute a rhs, given an analytical solution
# Choose a solution with non-zero values

domain = (-2, 1)
x = sp.symbols("x", real=True)
ue = sp.cos(5*sp.pi*(x+0.1)/2)
fe = ue.diff(x, 2)

# The pure Neumann requires the value of the mean
x_map = -1 + (x-domain[0])*2/(domain[1]-domain[0])
mean = {
    'c': sp.integrate(ue/sp.sqrt(1-x_map**2), (x, domain[0], domain[1])).evalf(),
    'l': sp.integrate(ue, (x, domain[0], domain[1])).evalf()
}

# 5 different types of boundary conditions
bcs = [
    {'left': ('N', ue.diff(x, 1).subs(x, domain[0])), 'right': ('N', ue.diff(x, 1).subs(x, domain[1]))},
    {'left': ('D', ue.subs(x, domain[0])), 'right': ('D', ue.subs(x, domain[1]))},
    {'left': ('N', ue.diff(x, 1).subs(x, domain[0])), 'right': ('D', ue.subs(x, domain[1]))},
    {'left': ('D', ue.subs(x, domain[0])), 'right': ('N', ue.diff(x, 1).subs(x, domain[1]))},
    {'right': (('D', ue.subs(x, domain[1])), ('N', ue.diff(x, 1).subs(x, domain[1])))}
]

def main(N, family, bci):
    bc = bcs[bci]
    if bci == 0:
        SD = FunctionSpace(N, family=family, bc=bc, domain=domain, mean=mean[family.lower()])
    else:
        SD = FunctionSpace(N, family=family, bc=bc, domain=domain)

    u = TrialFunction(SD)
    v = TestFunction(SD)

    # Get f on quad points
    fj = Array(SD, buffer=fe)

    # Compute right hand side of Poisson equation
    f_hat = Function(SD)
    f_hat = inner(v, fj, output_array=f_hat)

    # Get left hand side of Poisson equation
    A = inner(v, div(grad(u)))

    u_hat = Function(SD).set_boundary_dofs()
    if isinstance(A, list):
        bc_mat = extract_bc_matrices([A])
        A = A[0]
        f_hat -= bc_mat[0].matvec(u_hat, Function(SD))

    u_hat = A.solve(f_hat, u_hat)
    uj = u_hat.backward()
    uh = uj.forward()

    # Compare with analytical solution
    ua = Array(SD, buffer=ue)
    assert np.allclose(uj, ua), np.linalg.norm(uj-ua)
    if 'pytest' not in os.environ:
        print("Error=%2.16e" %(np.sqrt(dx((uj-ua)**2))))
        import matplotlib.pyplot as plt
        plt.plot(SD.mesh(), uj, 'b', SD.mesh(), ua, 'r')
        #plt.show()

if __name__ == '__main__':
    import sys
    N = int(sys.argv[-1]) if len(sys.argv) == 2 else 36
    for family in ('C', 'L'):
        for bci in range(5):
            main(N, family, bci)


import numpy as np
from numpy.fft import fft, fftshift, ifft, fftfreq
import matplotlib.pyplot as plt

def ideal_lowpass_filtering( x, cutoff, fs ):
	X = fft( x )
	H = np.zeros( fs )
	for i in range( -cutoff, cutoff + 1 ):
		H[i] = 1
	Y = H * X
	y = ifft( Y )
	y = y.real
	return y

def ideal_highpass_filtering( x, cutoff, fs ):
	X = fft( x )
	H = np.zeros( fs )
	for i in range( -cutoff, cutoff + 1 ):
		H[i] = 1
	H = 1 - H
	Y = H * X
	y = ifft( Y )
	y = y.real
	return y

def ideal_bandpass_filtering( x, f1, f2, fs ):
	X = fft( x )
	H = np.zeros( fs )
	for i in range( f1, f2 + 1 ):
		H[i] = 1
	for i in range( -f1, -f2 - 1, -1 ):
		H[i] = 1
	Y = H * X
	y = ifft( Y )
	y = y.real
	return y

def ideal_bandstop_filtering( x, f1, f2, fs ):
	X = fft( x )
	H = np.zeros( fs )
	for i in range( f1, f2 + 1 ):
		H[i] = 1
	for i in range( -f1, -f2 - 1, -1 ):
		H[i] = 1
	H = 1 - H
	Y = H * X
	y = ifft( Y )
	y = y.real
	return y

def ideal_allpass_filtering( x ):
	X = fft( x )
	Y = X
	y = ifft( Y )
	y = y.real
	return y

def main( ):
	print( "DSP in Frequency Domain" )
	print( "(1) Ideal Lowpass Filtering" )
	print( "(2) Ideal Highpass Filtering" )
	print( "(3) Ideal Bandpass Filtering" )
	print( "(4) Ideal Bandstop Filtering" )
	print( "(5) Ideal Allpass Filtering" )

	choice = eval( input( "Please enter your choice: " ) )

	if choice == 1 or choice == 2:
		fc = eval( input( "Please enter cutoff frequency(Hz): " ) )

	if choice == 3 or choice == 4:
		f1 = eval( input( "Please enter frequency f1(Hz): " ) )
		f2 = eval( input( "Please enter frequency f2(Hz): " ) )

	fs = 500
	t = np.linspace( 0, 1, fs, endpoint = False )
	x = np.cos( 2 * np.pi * 10 * t ) + np.cos( 2 * np.pi * 20 * t ) + np.cos( 2 * np.pi * 30 * t )

	if choice == 1:
		y = ideal_lowpass_filtering( x, fc, fs )
	elif choice == 2:
		y = ideal_highpass_filtering( x, fc, fs )
	elif choice == 3:
		y = ideal_bandpass_filtering( x, f1, f2, fs )
	elif choice == 4:
		y = ideal_bandstop_filtering( x, f1, f2, fs )
	else:
		y = ideal_allpass_filtering( x )

	f = fftshift( fftfreq( fs, 1 / fs ) )
	Xm = abs( fftshift( fft( x ) ) )
	Ym = abs( fftshift( fft( y ) ) )

	plt.figure( 1 )
	plt.plot( x )
	plt.xlabel( 't (second)' )
	plt.ylabel( 'Amplitude' )

	plt.figure( 2 )
	plt.plot( f, Xm )
	plt.xlabel( 'f' )
	plt.ylabel( 'Magnitude' )

	plt.figure( 3 )
	plt.plot( y )
	plt.xlabel( 't (second)' )
	plt.ylabel( 'Amplitude' )

	plt.figure( 4 )
	plt.plot( f, Ym )
	plt.xlabel( 'f' )
	plt.ylabel( 'Magnitude' )

	plt.show( )

main( )


import numpy as np

def mean_squared_error(y, t):
  return 0.5 * np.sum((y - t) ** 2)

t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]

y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]
print(mean_squared_error(np.array(y), np.array(t)))

y = [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]
print(mean_squared_error(np.array(y), np.array(t)))


import numpy as np

##### DESCRIPTION #####
# This part of the module handles the mathematic
# formulation for channel coefficients estimation

# Compute the first and second coefficients of the fft considering that the third one is 0
def get_fft_01(rss):                         # Vector with the 4 rrs-like values corresponding to the required measurements [paper]
    n_nan = sum(np.isnan(rss))               # Number of NaNs in rss (maximum of 1)
    if n_nan > 1:                            # If there's more than 1, return a NaN
        return np.nan
    if n_nan == 1:
        ii = np.argwhere(np.isnan(rss))[0]   # If there's only one, then solve it using that the third coefficient
        fft_3_coefs = np.array([1, -1, 1, -1])
        if ii%2 == 0:
            rss[ii] = -np.nansum(rss*fft_3_coefs)
        else:
            rss[ii] = np.nansum(rss*fft_3_coefs)
    fft_0 = np.mean(rss)                     # First coefficient
    fft_1 = np.dot(rss, [1, 1j, -1, -1j])/4  # Second coefficient
    return fft_0, fft_1

# Compute the channe coefficient from the measures corresponding to that antenna assuming the antenna is off
def get_coef_off(rss):                       # Vector with the 4 rrs-like values corresponding to the required measurements [paper]
    fft_0, fft_1 = get_fft_01(rss)           # Get first and second fft coefficients
    a = 2*np.abs(fft_1)                      # Amplitude of the wave
    b = np.max((fft_0, a))                   # Constant offset of the wave
    top = np.sqrt(b+a)                       # High point of the wave (undoing the square)
    bot = np.sqrt(b-a)                       # Low point of the wave  (undoing the square)
    amplitude = (top - bot)/2                # The amplitude of the coefficient is half the difference between top and bot
    coef = amplitude*fft_1/np.abs(fft_1)     # The coefficient has the same phase as fft_1
    return coef

# Compute the channel coefficient from the measures corresponding to that antenna assuming the antenna is on
def get_coef_on(rss, phase_0):               # Vector with the 4 rrs-like values corresponding to the required measurements [paper] and phase of the antenna when it was on
    fft_0, fft_1 = get_fft_01(rss)
    a = 2*np.abs(fft_1)
    b = np.max((fft_0, a))
    top = np.sqrt(b+a)
    bot = np.sqrt(b-a)
    amplitude = (top - bot)/2                # So far its the same as for get_coef_off
    phase = np.angle(fft_1)                  # This time we compute the phase to later correct it
    amplitude_0 = (top + bot)/2              # The amplitude of the array response of all the other antennas is the middle point between top and bot
    phase -= np.angle(amplitude_0 + amplitude*np.exp((phase - phase_0)*1j)) # Phase correction described in the paper
    return amplitude*np.exp(1j*phase)


import numpy as np
from numpy import atleast_2d as vec
from scipy.stats import multivariate_normal

def gaussian_mix(query):
    # Assign multivariate gaussians to be present in the space.
    gaussians = [multivariate_normal(mean = [0.9, 0.1], cov = [[.05, 0], [0, .05]])]
    gaussians.append(multivariate_normal(mean = [0.9, 0.9], cov = [[0.07, 0.01], [0.01, .07]]))
    gaussians.append(multivariate_normal(mean = [0.15, 0.7], cov = [[.03, 0], [0, .03]]))
    # Initialize initial value.
    value = 0.0
    # Iterate through each gaussian in the space.
    for j in xrange(len(gaussians)):
        value += gaussians[j].pdf(query)

    # Take the average.
    gaussian_function = value / len(gaussians)
    return vec(gaussian_function) # vec(np.array([query.ravel(), gaussian_function]).ravel())

if __name__ == "__main__":
    X = gaussian_mix(np.array([0.5, 0.5]))
    print X
    print X.shape


#連立常微分方程式を解く
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#1階常微分方程式（放射性崩壊）
def func_dydt(y, t):
    dydt = -y

    return dydt

#1階連立常微分方程式（Lorenz方程式）
def func_lorenz(var, t, p, r, b):
    dxdt = -p*var[0] +p*var[1]
    dydt = -var[0]*var[2] +r*var[0] -var[1]
    dzdt = var[0]*var[1] -b*var[2]

    return [dxdt, dydt, dzdt]

#2階常微分方程式（運動方程式、自由落下）
def func_motion(var, t):
    dxdt = var[1]
    dvdt = -gravity

    return [dxdt, dvdt]

#2d可視化
def plot2d(t_list, y_list, t_label, y_label):
    plt.xlabel(t_label)  #x軸の名前
    plt.ylabel(y_label)  #y軸の名前
    plt.grid()  #点線の目盛りを表示
    plt.plot(t_list, y_list)

    plt.show()

#3d可視化
def plot3d(t_list, var_list):
    fig = plt.figure()
    ax = fig.gca(projection='3d')

    ax.set_xlabel("$x$")  #x軸の名前
    ax.set_ylabel("$y$")  #y軸の名前
    ax.set_zlabel("$z$")  #z軸の名前
    ax.plot(var_list[:, 0], var_list[:, 1], var_list[:, 2])

    plt.show()

#メイン実行部
if (__name__ == '__main__'):
    #1階常微分方程式（放射性崩壊）
    t_list = np.linspace(0.0, 10.0, 1000)
    y_init = 1.0  #初期値
    y_list = odeint(func_dydt, y_init, t_list)
    print(y_list)

    #可視化
    plot2d(t_list, y_list[:, 0], "$t$", "$y(t)$")

    #1階連立常微分方程式（Lorenz方程式）
    t_list = np.linspace(0.0, 100.0, 10000)
    p = 10
    r = 28
    b = 8/3
    var_init = [0.1, 0.1, 0.1]  #3次元座標上での初期値
    var_list = odeint(func_lorenz, var_init, t_list, args=(p, r, b))
    print(var_list)

    #可視化
    plot3d(t_list, var_list)

    #2階常微分方程式（運動方程式、自由落下）
    t_list = np.linspace(0.0, 5.0, 1000)
    v0 = 0.0  #初速度
    gravity = 9.80665  #重力加速度
    m_init = [100.0, 0.0]  #高さと速度の初期値
    m_list = odeint(func_motion, m_init, t_list)
    print(m_list)

    #可視化
    plot2d(t_list, m_list[:, 0], "$t$", "$x(t)$")
    plot2d(t_list, m_list[:, 1], "$t$", "$v(t)$")


from math import e, sin, cos
import pandas as pd
from sympy import Symbol, integrate, lambdify

x = Symbol('x')

h = 0.1
n = 10
x0 = 0

def def_int(func, a, b):
    int_ = lambdify(x, integrate(func, x))
    return int_(b) - int_(a)

def formula(f_, arr, A, B):
    x_ = list(arr['x'])
    y_ = list(arr['Tn(x)'])
    omega = (x - x_[0]) * (x - x_[1]) * (x - x_[2]) * (x - x_[3]) * (x - x_[4])
    d_omega = lambda x: (5 * x ** 4 -
                         4 * x ** 3 * (sum(x_)) +
                         3 * x ** 2 * (x_[0] * x_[1] + x_[0] * x_[2] + x_[0] * x_[3] + x_[0] * x_[4] + x_[1] * x_[2] +
                                       x_[1] * x_[3] + x_[1] * x_[4] + x_[2] * x_[3] + x_[2] * x_[4] + x_[3] * x_[4]) -
                         2 * x ** 1 * (x_[0] * x_[1] * x_[2] + x_[0] * x_[1] * x_[3] + x_[0] * x_[1] * x_[4] +
                                       x_[0] * x_[2] * x_[3] + x_[0] * x_[2] * x_[4] + x_[0] * x_[3] * x_[4] +
                                       x_[1] * x_[2] * x_[3] + x_[1] * x_[2] * x_[4] + x_[1] * x_[3] * x_[4] +
                                       x_[2] * x_[3] * x_[4]) +
                         1 * x ** 0 * (
                                 x_[0] * x_[1] * x_[2] * x_[3] + x_[0] * x_[1] * x_[2] * x_[4] + x_[0] * x_[1] * x_[4] *
                                 x_[3] +
                                 x_[0] * x_[4] * x_[2] * x_[3] + x_[4] * x_[1] * x_[2] * x_[3])
                         )

    gauss_type = sum(def_int(omega / (x - x_[i]) / d_omega(x_[i]), A, B) * f_(x_[i], y_[i]) for i in range(5))
    return y_[4] + gauss_type

dy = lambda x, y: - y + sin(x)
y = lambda x: 1 / 2 * (3 * e ** (-x) - cos(x) + sin(x))

exact = pd.DataFrame([(x0 + k * h, y(x0 + k * h)) for k in range(-2, n + 1)])
exact = exact.rename(columns={0: 'x', 1: 'y(x)'})
print("Таблица значений y(x)")
print(exact)

x0 = 0
taylor_formula = lambda x_: 1 - (x_ - x0) + (x_ - x0) ** 2 - 1 / 3 * (x_ - x0) ** 3 + 1 / 24 * (
            x_ - x0) ** 4 - 1 / 120 * (x_ - x0) ** 5

taylor_table = pd.DataFrame(
    [(x0 + k * h, taylor_formula(x0 + k * h), abs(taylor_formula(x0 + k * h) - y(x0 + k * h))) for k in
     range(-2, n + 1)])
taylor_table = taylor_table.rename(columns={0: 'x', 1: 'Tn(x)', 2: '|y(x) - Tn(x)|'})
print(taylor_table[:5])

f = lambda x, y: - y + sin(x)
adams_table = taylor_table[:5]
adams_table = adams_table.drop('|y(x) - Tn(x)|', axis=1)
# adams_table = adams_table.rename(columns={'Tn(x)': 'f(x)'})

for k in range(3, n + 1):
    xi = float(adams_table.tail(1)['x'])
    d = formula(f, adams_table[-5:], xi, xi + h)
    adams_table = adams_table.append({'x': xi + h, 'Tn(x)': d}, ignore_index=True)
    # print([xi + h, d])
# print(adams_table['f(x)'][-
adams_table = adams_table.rename(columns={'Tn(x)': 'y(x)'})
print(abs(list(adams_table['y(x)'])[-1] - list(exact['y(x)'])[-1]))
# print(adams_table[5:])

yi = 1
xi = 0
runge_kutta_table = pd.DataFrame(columns=['x', 'y(x)'])
for k in range(n):
    k1 = h * dy(xi, yi)
    k2 = h * dy(xi + h / 2, yi + k1 / 2)
    k3 = h * dy(xi + h / 2, yi + k2 / 2)
    k4 = h * dy(xi + h, yi + k3)
    xi += h
    yi += 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    runge_kutta_table = runge_kutta_table.append({'x': xi, 'y(x)': yi}, ignore_index=True)
    # print(xi, yi)
print(abs(list(runge_kutta_table['y(x)'])[-1] - list(exact['y(x)'])[-1]))
print(runge_kutta_table)

yi = 1
xi = 0
euler_table = pd.DataFrame(columns=['x', 'y(x)'])
for k in range(n):
    yi += h * dy(xi, yi)
    xi += h
    euler_table = euler_table.append({'x': xi, 'y(x)': yi}, ignore_index=True)
    # print(xi, yi)
print(abs(list(euler_table['y(x)'])[-1] - list(exact['y(x)'])[-1]))
print(euler_table)

yi = 1
xi = 0
modified_euler_table = pd.DataFrame(columns=['x', 'y(x)'])
for k in range(n):
    yi_2 = yi + h / 2 * dy(xi, yi)
    yi += h * dy(xi + h / 2, yi_2)
    xi += h
    modified_euler_table = modified_euler_table.append({'x': xi, 'y(x)': yi}, ignore_index=True)
    # print(xi, yi)
print(abs(list(modified_euler_table['y(x)'])[-1] - list(exact['y(x)'])[-1]))
print(modified_euler_table)

yi = 1
xi = 0
euler_cauchy_table = pd.DataFrame(columns=['x', 'y(x)'])
for k in range(n):
    Yi = yi + h * dy(xi, yi)
    yi += h / 2 * (dy(xi, yi) + dy(xi + h, Yi))
    xi += h
    euler_cauchy_table = euler_cauchy_table.append({'x': xi, 'y(x)': yi}, ignore_index=True)
    # print(xi, yi)
print(abs(list(euler_cauchy_table['y(x)'])[-1] - list(exact['y(x)'])[-1]))
print(euler_cauchy_table)


import numpy as np

def ackley(params, diff):
    """Ackley function, which is:
    f(x) = -a exp(-b\sqrt{\frac{1}{d} \sum_{i=1}^n x_i^2})
            - exp(\frac{1}{d} \sum_{i=1}^n cos(c x_i)) + a + e
    link to the function: https://tinyurl.com/qmds7dh

    Args:
        params(np.array): 1d numpy array of function arguments
        diff(np.array): 1d np.array of difficulty parameters
            First value(a) defines the 'height' of the figure, larger (a) makes the 'cone'
            in the figure higher. Second value(b) generates a 'hole', the larger (b) is the
            wider  is the hole. Third value(c) defines the wiggliness of the function. The
            higer (c) gets, fluctuations become more frequent, so it gets more difficult to
            take numerical derivative.

    Returns:
           res(float): value of the function evaluated at `params` and `diff`

    """
    a = np.array(diff[0])
    b = np.array(diff[1])
    c = np.array(diff[2])
    sum1 = np.sum(params**2)
    sum2 = np.sum(np.cos(c*params))
    d = int(len(params))
    res = -a*np.exp(-b*np.sqrt(sum1/d)) - np.exp(sum2/d) + a + np.e
    return res

def grad_ackley(params, diff):
    """Gradient vector of Ackley function.

    Args:
        params(np.array): 1d numpy array of function arguments
        diff(np.array): 1d np.array of difficulty parameters

    Returns:
        grad(np.array): 1d numpy array of Ackley function derivative for each
            argument, evaluated at `params` and `diff`
    """

    a = np.atleast_1d(diff[0])
    b = np.atleast_1d(diff[1])
    c = np.atleast_1d(diff[2])
    sum1 = np.sum(params**2)
    sum2 = np.sum(np.cos(c*params))
    d = int(len(params))
    der_param = np.zeros_like(params)
    for i in range(d):
        der_param[i] = a * b / d * params[i] * np.exp(-b*np.sqrt(sum1/d))/np.sqrt(sum1/d)
        + c/d * np.exp(1/d*sum2) * np.sin(c*params[i])

    grad = np.array([der_param])
    return grad

def hessian_ackley(params, diff):
    """Return Hessian matrix of Ackley function."""
    """Hessioan matrix of Ackley function.

    Args:
        params(np.array): 1d numpy array of function arguments
        diff(np.array): 1d np.array of difficulty parameters

    Returns:
        hess_mat(np.array): 2d numpy array with the Ackley function hessian,
            evaluated at `params` and `diff`

    """
    a = diff[0]
    b = diff[1]
    c = diff[2]
    sum1 = np.sum(params**2)
    sum2 = np.sum(np.cos(c*params))
    d = int(len(params))
    hess_mat = np.zeros((d,d))
    for i in range(d):
        for j in range(d):
            if i == j:
                hess_mat[i,j] = (a * b / d * np.exp(-b*np.sqrt(sum1/d)) * (1/np.sqrt(sum1/d) -
                           b * params[i]**2 / sum1 - params[i]**2/d * (1/d*sum1)**(-3/2)) +
                            c**2/d*np.exp(1/d*sum2)*(np.cos(c*params[i]) - 1/d*(np.sin(c*params[i]))**2))
            else:
                hess_mat[i,j] =( -a*b / d * params[i] * params[j] * np.exp(-b*np.sqrt(sum1/d)) * (b/sum1 + 1/d * (1/d*sum1)**(-3/2)) -
                            c**2/d**2 * np.exp(1/d*sum2) * np.sin(c*params[i]) * np.sin(c*params[j]))

    return hess_mat


import numpy as np
from math import factorial
from numpy.ma import masked_array
import scipy
from scipy.special import factorial
import scipy.stats as stat
import pdb

#Define Zernike radial polynomials
def rnm(n,m,rho):
    """
    Return an array with the zernike Rnm polynomial calculated at rho points.

    **ARGUMENTS:**

        === ==========================================
        n    n order of the Zernike polynomial
        m    m order of the Zernike polynomial
        rho  Matrix containing the radial coordinates.
        === ==========================================

    .. note:: For rho>1 the returned value is 0

    .. note:: Values for rho<0 are silently returned as rho=0

    """

    if(type(n) is not int):
        raise Exception("n must be integer")
    if(type(m) is not int):
        raise Exception("m must be integer")
    if (n-m)%2!=0:
        raise Exception("n-m must be even")
    if abs(m)>n:
        raise Exception("The following must be true |m|<=n")
    mask=np.where(rho<=1,False,True)

    if(n==0 and m==0):
        return  masked_array(data=np.ones(np.shape(rho)), mask=mask)
    rho=np.where(rho<0,0,rho)
    Rnm=np.zeros(rho.shape)
    S=(n-abs(m))/2
    for s in range (0,S+1):
        CR=pow(-1,s)*factorial(n-s)/ \
            (factorial(s)*factorial(-s+(n+abs(m))/2)* \
            factorial(-s+(n-abs(m))/2))
        p=CR*pow(rho,n-2*s)
        Rnm=Rnm+p
    return masked_array(data=Rnm, mask=mask)

def zernike(n,m,rho,theta):
    """
    Returns the an array with the Zernike polynomial evaluated in the rho and
    theta.

    **ARGUMENTS:**

    ===== ==========================================
    n     n order of the Zernike polynomial
    m     m order of the Zernike polynomial
    rho   Matrix containing the radial coordinates.
    theta Matrix containing the angular coordinates.
    ===== ==========================================

    .. note:: For rho>1 the returned value is 0

    .. note:: Values for rho<0 are silently returned as rho=0
    """

    Rnm=rnm(n,m,rho)

    NC=np.sqrt(2*(n+1))
    S=(n-abs(m))/2

    if m>0:
        Zmn=NC*Rnm*np.cos(m*theta)
    #las funciones cos() y sin() de scipy tienen problemas cuando la grilla
    # tiene dimension cero

    elif m<0:
        Zmn=NC*Rnm*np.sin(m*theta)
    else:
        Zmn=np.sqrt(0.5)*NC*Rnm
    return Zmn

def zmodes(N):
    """
    Construct Zernike mode vectors in standard ordering
    Includes all modes up to radial order N
    """
    r = 0 #Starting radial mode
    radial = []
    azimuthal = []
    z = []
    while np.size(radial) < N:
        if r % 2 == 0:
            m = 0 #Set starting azimuthal mode to 0
        else:
            m = 1 #Set starting azimuthal mode to 1
        while m <= r and np.size(radial) < N:
            #Get current z number
            z = np.size(azimuthal) + 1
            #If z is odd, append sine first
            #Append negative and positive m
            if z % 2 == 1:
                azimuthal.append(-m)
            else:
                azimuthal.append(m)
            radial.append(r)
            if m > 0:
                if z % 2 == 1:
                    azimuthal.append(m)
                else:
                    azimuthal.append(-m)
                radial.append(r)
            m = m + 2
        r = r + 1 #Increment radial order
    return np.array(radial[:N],order='F').astype('int'),\
           np.array(azimuthal[:N],order='F').astype('int')

def zmatrix(rho,theta,N,r=None,m=None):
    """
    Formulate Zernike least squares fitting matrix
    Requires rho and theta vectors, normalized to rhomax=1
    """
    #Create mode vectors
    if r is None:
        r,m = zmodes(N)

    #Form matrix
    A = np.zeros((np.size(rho),np.size(r)))

    #Populate matrix columns
    for i in range(np.size(r)):
        A[:,i] = zernike(int(r[i]),int(m[i]),rho,theta)

    return A

def carttopolar(x,y,cx,cy,rad):
    #Convert to polar
    r = (sqrt((x-cx)**2+(y-cy)**2))/rad
    #Remove invalid points
    mask = r <= 1
    r = r[mask]
    x = x[mask]
    y = y[mask]
    theta = arctan2((y-cy),(x-cx))

    return r, theta, mask

#Reconstruct surface on unique x and y vectors for zernike coefficients
def zernsurf(x,y,cx,cy,rad,coeff,r=None,m=None):
    if np.size(np.unique(x)) == np.size(x):
        x,y = np.meshgrid(x,y)
    rho = np.sqrt((x-cx)**2+(y-cy)**2)/rad
    theta = np.arctan2((y-cy),(x-cx))
    heights = np.zeros(np.shape(x))

    if r is None:
        r,m = zmodes(np.size(coeff))

    for i in range(np.size(r)):
        heights = heights + coeff[i]*zernike(int(r[i]),int(m[i]),rho,theta)

    #Set outside pixels to NaN
    heights[np.where(rho>1.)] = np.NaN

    return heights.data

def fitimg(img,N=20,r=None,m=None):
    """
    Perform Zernike fit on an image.
    Zernike domain is defined over the full image array.
    """
    #Construct rho and theta vectors
    x,y = np.meshgrid(np.linspace(-1.,1.,np.shape(img)[0]),\
                      np.linspace(-1.,1.,np.shape(img)[1]))
    rho = np.sqrt(x**2+y**2)
    theta = np.arctan2(y,x)

    #Flatten and remove NaNs
    rho = rho.flatten()
    theta = theta.flatten()

    #Get b vector
    b = img.flatten()

    #Get A matrix
    A = zmatrix(rho[~np.isnan(b)],theta[~np.isnan(b)],N,r=r,m=m)

    #Solve for coefficients
    c = scipy.linalg.lstsq(A,b[~np.isnan(b)])

    #Reconstruct fit image
    coeff = c[0]
    A = zmatrix(rho,theta,N,r=r,m=m)
    fit = np.dot(A,coeff)
    fit = fit.reshape(np.shape(x))
    fit[np.isnan(img)] = np.nan

    return c,fit.reshape(np.shape(x))

def fitvec(x,y,z,N=20,r=None,m=None):
    """
    Perform Zernike fit on an image.
    Zernike domain is defined over the full image array.
    """
    #Construct rho and theta vectors
    rho = np.sqrt(x**2+y**2)
    theta = np.arctan2(y,x)
    rho = rho/rho.max()

    #Get A matrix
    A = zmatrix(rho,theta,N,r=r,m=m)

    #Solve for coefficients
    c = scipy.linalg.lstsq(A,z)

    return c

#Function to output Zernike coefficients and RMS fit error for x,y,z txt file
def zcoeff(filename,save=False,cx=0.,cy=0.,rad=1.,order=20,r=None,m=None,**kwags):
    #Read in surface data
    if type(filename) is str:
        d = genfromtxt(filename,**kwags)
    else:
        d = filename

    if shape(d)[0]==3:
        sagx, sagy, sagz = d[0],d[1],d[2]
    else:
##        #Strip NaN rows/columns
##        while sum(isnan(d[0]))==shape(d)[1]:
##            d = d[1:]
##        while sum(isnan(d[-1]))==shape(d)[1]:
##            d = d[:-1]
##        newsize = shape(d)[0]
##        while sum(isnan(d[:,0]))==newsize:
##            d = d[:,1:]
##        while sum(isnan(d[:,-1]))==newsize:
##            d = d[:,:-1]
        x,y=meshgrid(arange(shape(d)[0],dtype='float'),\
                     arange(shape(d)[1],dtype='float'))
##        ind = invert(isnan(d))
##        d2 = d[ind]
##        x = x[ind]
##        y = y[ind]
        x = x.flatten()
        y = y.flatten()
        d2 = d.flatten()
        sagx = []
        sagy = []
        sagz = []
        for i in range(size(d2)):
            if invert(isnan(d2[i])):
                sagx.append(x[i])
                sagy.append(y[i])
                sagz.append(d2[i])
        sagx = array(sagx)
        sagy = array(sagy)
        sagz = array(sagz)

    #Convert to normalized polar coordinates for Zernike fitting
    rho, theta, mask = carttopolar(sagx,sagy,cx,cy,rad)

    #Create Zernike polynomial matrix for least squares fit
    #Using all Zernike polynomials up to radial order 20
    pdb.set_trace()
    A = zmatrix(rho,theta,order,r=r,m=m)

    #Perform least squares fit
    #0th element is the coefficient matrix
    #1st element is sum of squared residuals
    #2nd element is rank of matrix A
    #3rd element is singular values of A
    fit = scipy.linalg.lstsq(A,sagz[mask])

    #Compute fitted surface from Zernike coefficients
    if shape(d)[0] != 3:
        y,x=meshgrid(arange(shape(d)[0],dtype='float'),\
                         arange(shape(d)[1],dtype='float'))
    else:
        x,y = d[0],d[1]
    fitsurf = zernsurf(x.flatten(),y.flatten(),cx,cy,rad,fit[0],r=r,m=m)
##
##    #Do residuals match up with those from fit?
##    print sum((fitsurf-sagz)**2)
##    print fit[1]

    rms = sqrt(fit[1]/size(sagz))

    #If save=True, save coefficients to a txt file
    #First line is number of coefficients
    if save==True:
        savetxt(filename.split('.')[0]+'Coeff.txt'\
                ,insert(fit[0],0,size(fit[0])))

    return fit[0],fit[1],rms,fitsurf

import numpy,from numpy,import scipy,from scipy


'''
Created on Jun 1, 2011
@author: Peter Harrington
'''
from numpy import *
import matplotlib.pyplot as plt

def loadDataSet(fileName, delim='\t'):
    fr = open(fileName)
    stringArr = [line.strip().split(delim) for line in fr.readlines()]
    datArr = [map(float, line) for line in stringArr]
    return mat(datArr)

def pca(dataMat, topNfeat=9999999):
    meanVals = mean(dataMat, axis=0)
    meanRemoved = dataMat - meanVals  # remove mean
    covMat = cov(meanRemoved, rowvar=0)
    eigVals, eigVects = linalg.eig(mat(covMat))
    eigValInd = argsort(eigVals)  # sort, sort goes smallest to largest
    eigValInd = eigValInd[:-(topNfeat + 1):-1]  # cut off unwanted dimensions
    redEigVects = eigVects[:, eigValInd]  # reorganize eig vects largest to smallest
    lowDDataMat = meanRemoved * redEigVects  # transform data into new dimensions
    reconMat = (lowDDataMat * redEigVects.T) + meanVals
    return lowDDataMat, reconMat

def replaceNanWithMean():
    datMat = loadDataSet('secom.data',' ')  # 解析数据
    print(datMat.shape)
    numFeat = shape(datMat)[1]      # 获取特征维度
    for i in range(numFeat):
        meanVal = mean(datMat[nonzero(~isnan(datMat[:,i].A))[0],i]) # 利用该维度所有非NaN特征求取均值
        datMat[nonzero(isnan(datMat[:,i].A))[0],i] = meanVal # 将该维度中所有NaN特征全部用均值替换
    return datMat

datMat = loadDataSet('secom.data',' ')  # 解析数据
print(datMat.shape)

datMat = replaceNanWithMean()
meanVals = mean(datMat, axis=0)
meanRemoved = datMat - meanVals
covMat = cov(meanRemoved, rowvar=0)
eigVals, eigVects = linalg.eig(mat(covMat))
print(sum(eigVals)*0.9)     # 计算90%的主成分方差总和
print(sum(eigVals[:6]))     # 计算前6个主成分所占的方差
plt.plot(eigVals[:20])      # 对前20个画图观察
plt.show()


import numpy as np
import math

shouldNormalize = True

def normalize(X, axis=-1, order=2):
    if not shouldNormalize :
        return X

    l2 = np.atleast_1d(np.linalg.norm(X, order, axis))
    l2[l2 == 0] = 1
    return X / np.expand_dims(l2, axis)

class NaiveBayes():
    def fit(self, X, y):
        X = normalize(X)
        # print(X)
        self.X, self.y = X, y
        self.classes = np.unique(y)
        self.parameters = []
        for i, c in enumerate(self.classes):
            X_where_c = X[np.where(y == c)]
            self.parameters.append([])
            for col in X_where_c.T:
                parameters = {"mean": col.mean(), "var": col.var()}
                self.parameters[i].append(parameters)

    def _calculate_likelihood(self, mean, var, x):
        eps = 1e-4
        coeff = 1.0 / math.sqrt(2.0 * math.pi * var + eps)
        exponent = math.exp(-(math.pow(x - mean, 2) / (2 * var + eps)))
        return coeff * exponent

    def _calculate_prior(self, c):
        frequency = np.mean(self.y == c)
        return frequency

    def _classify(self, sample):
        posteriors = []
        for i, c in enumerate(self.classes):
            posterior = self._calculate_prior(c)
            for feature_value, params in zip(sample, self.parameters[i]):
                likelihood = self._calculate_likelihood(params["mean"], params["var"], feature_value)
                posterior *= likelihood
            posteriors.append(posterior)

        return self.classes[np.argmax(posteriors)]

    def predict(self, X):
        X = normalize(X)
        y_pred = [self._classify(sample) for sample in X]
        return y_pred


import pandas as pd
import numpy as np
import math
class Node:
    def __init__(self, l):
        self.label = l
        self.branch = {}
def entropy(data):
    total_ex = len(data)
    p_ex = len(data.loc[data['PlayTennis']=='Yes'])
    n_ex = len(data.loc[data['PlayTennis']=='No'])
    en = 0
    if(p_ex>0):
        en = -(p_ex/float(total_ex)) * (math.log(p_ex,2)-math.log(total_ex,2))
    if(n_ex>0):
        en += -(n_ex/float(total_ex)) * (math.log(n_ex,2)-math.log(total_ex,2))
    return en
def gain(en_s,data_s,attrib):
    values = set(data_s[attrib])
    #print(values)
    gain = en_s
    for value in values:
        gain -= len(data_s.loc[data_s[attrib]==value])/float(len(data_s)) * entropy(data_s.loc[data_s[attrib]==value])
    return gain
def get_attr(data):
    en_s = entropy(data)
    attribute = ""
    max_gain = 0
    for attr in data.columns[:len(data.columns)-1]:
        g = gain(en_s, data, attr)
        if g > max_gain:
            max_gain = g
            attribute = attr
    return attribute
def decision_tree(data):
    root = Node("NULL")
    if(entropy(data)==0):
        if(len(data.loc[data[data.columns[-1]]=="Yes"]) == len(data)):
            root.label = "Yes"
        else:
            root.label = "No"
        return root
    if(len(data.columns)==1):
        return
    else:
        attr = get_attr(data)
        root.label = attr
        values = set(data[attr])
        for value in values:
            root.branch[value] = decision_tree(data.loc[data[attr]==value].drop(attr,axis=1))
        return root
def get_rules(root, rule, rules):
    if not root.branch:
        rules.append(rule[:-1]+"=>"+root.label)
        return rules
    for val in root.branch:
        get_rules(root.branch[val], rule+root.label+"="+str(val)+"^", rules)
    return rules
def test(tree, test_str):
    if not tree.branch:
        return tree.label
    return test(tree.branch[str(test_str[tree.label])], test_str)

data = pd.read_csv("tennis_Program_4.csv")
tree = decision_tree(data)
rules = get_rules(tree," ",[])
for rule in rules:
    print(rule)
test_str = {}
print("Enter the test case input: ")
for attr in data.columns[:-1]:
    test_str[attr] = input(attr+": ")
print(test_str)
print(test(tree, test_str))


""" The typical Lotka-Volterra Model simulated using scipy """

import scipy as sc
import scipy.integrate as integrate
import pylab as p #Contains matplotlib for plotting

# import matplotlib.pylab as p #Some people might need to do this

def dCR_dt(pops, t=0):
    """ Returns the growth rate of predator and prey populations at any given time step """

    R = pops[0]
    C = pops[1]
    dRdt = r*R - a*R*C
    dCdt = -z*C + e*a*R*C

    return sc.array([dRdt, dCdt])

# Define parameters:
r = 1. # Resource growth rate
a = 0.1 # Consumer search rate (determines consumption rate)
z = 1.5 # Consumer mortality rate
e = 0.75 # Consumer production efficiency

# Now define time -- integrate from 0 to 15, using 1000 points:
t = sc.linspace(0, 15,  1000)

x0 = 10
y0 = 5
xy0 = sc.array([x0, y0]) # initial conditions: 10 prey and 5 predators per unit area

pops, infodict = integrate.odeint(dCR_dt, xy0, t, full_output=True)

infodict['message']     # >>> 'Integration successful.'

prey, predators = pops.T # What's this for?
f1 = p.figure() #Open empty figure object
p.plot(t, prey, 'g-', label='Resource density') # Plot
p.plot(t, predators  , 'b-', label='Consumer density')
p.grid()
p.legend(loc='best')
p.xlabel('Time')
p.ylabel('Population')
p.title('Consumer-Resource population dynamics')
p.show()
f1.savefig('../Results/prey_and_predators_1.pdf') #Save figure


# MA 322, Lab Quiz
# AB Satyaprakash, 180123062
# Question 3
# imports
from math import pow
import numpy as np
import matplotlib.pyplot as plt

# functions

# take the function to be x^2 (2 fixed points in R, i.e, 0 and 1)
def f(x):
    return pow(x, 2)

# program body
# we can make a quick guess that {xn} will converge to 0 and {yn} will diverge from 0
# pointing out the first 3 terms of the sequeneces
x0, y0 = 1/2, 2
x_list, y_list = [x0], [y0]
for i in range(2):
    x0 = f(x0)
    x_list.append(x0)
    y0 = f(y0)
    y_list.append(y0)
fx_list = [f(x) for x in x_list]
fy_list = [f(y) for y in y_list]

# plotting the graph of x^2 for 500 evenly spaced values
X = np.linspace(0, 25, 500)
Y = [f(x) for x in X]

plt.title('Graph of x^2 - function with 2 fixed points')
plt.plot(X, Y, label='Function f(x) = x^2')
plt.scatter(x_list, fx_list, label='Converging Sequence (xn) (x0 = 0.5)')
plt.scatter(y_list, fy_list, label='Diverging Sequence (y) (y0 = 2)')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()

Semester 6/MA 322 (Scientific Computing Theory)/Exams/Lab Quiz/180123062_q3.py


# for details see : https://nbviewer.jupyter.org/github/gpeyre/numerical-tours/blob/master/python/optimaltransp_6_entropic_adv.ipynb

import numpy as np

normalise = lambda x : x/np.sum(np.abs(x))

def mina_u(H,a,epsilon):
    tmp=np.sum(a * np.exp(-H/epsilon),0)
    tmp=np.where(np.abs(tmp)<10e-15, 10e-15, tmp)
    return -epsilon*np.log( tmp )
def minb_u(H,b,epsilon):
    tmp=np.sum(b * np.exp(-H/epsilon),1)
    tmp=np.where(np.abs(tmp)<10e-15, 10e-15, tmp)
    return -epsilon*np.log( tmp )

def mina(H,a,epsilon):
    #print(mina_u(H-np.min(H,0),a,epsilon) + np.min(H,0))
    return mina_u(H-np.min(H,0),a,epsilon) + np.min(H,0)
def minb(H,b,epsilon): return minb_u(H-np.min(H,1)[:,None],b,epsilon) + np.min(H,1)

def Sinkhorn(C,a,b,epsilon,f,niter = 500):#cost matrix, histograms, regularization strength, initial f, number of iterations
    Err = np.zeros(niter)
    for it in range(niter):
        g = mina(C-f[:,None],a,epsilon)
        f = minb(C-g[None,:],b,epsilon)
        # generate the coupling
        P = a * np.exp((f[:,None]+g[None,:]-C)/epsilon) * b
        # check conservation of mass
        Err[it] = np.linalg.norm(np.sum(P,0)-b,1)
    return (P,Err)

def unb_Sinkhorn(C,a,b,epsilon,rho,f,niter=500):#same as above and rho is the weight of the divergence terms in unbalanced OT
    kappa = rho/(rho+epsilon)
    for it in range(niter):
        g = kappa*mina(C-f[:,None],a,epsilon)
        f = kappa*minb(C-g[None,:],b,epsilon)
    P = a * np.exp((f[:,None]+g[None,:]-C)/epsilon) * b
    return P

#Not relevant anymore
def L2_cost(source,sink):
    C=np.empty((len(source),len(sink)))
    for i,pos1 in enumerate(source):
        for j, pos2 in enumerate(sink):
            C[i,j]=np.sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2)
    return C

#Not relevant either
def sink_L2_cost(sp,ep,cr_cost=1,del_cost=1,INF=1e7):
    sub_cost=L2_cost(sp,ep)
    left_col=np.hstack((np.ones((len(sp),1)) * INF ,del_cost*np.ones((len(sp),1))))
    top_row=np.vstack((cr_cost*np.ones((1,len(ep))),INF*np.ones((1,len(ep)))))
    corner=np.array([[0,0],[INF,0]])
    top=np.hstack((corner,top_row))
    bottom=np.hstack((left_col,sub_cost))
    end_cost=np.vstack((top,bottom))

    return end_cost/np.average(end_cost)


"""
File contains different state transition models
"""
import numpy as np
import math
from numpy import concatenate as cat

def __generate_linear_row(T, depth, zeros, dim):
    """Generates linear state transition matrix rows"""
    core = np.eye(dim)
    if depth == 1:
        return cat((np.zeros((dim, dim * zeros)), core), axis=1)
    depth = depth - 1
    return cat((__generate_linear_row(T, depth, zeros, dim),
                T ** depth / math.factorial(depth) * core,), axis=1)

def __generate_linear_cols(T, depth, target, dim):
    """Carefully concatenates linear state transition matrix rows"""
    if depth == 1:
        return __generate_linear_row(T, depth, target, dim)
    return cat((__generate_linear_row(T, depth, target, dim),
                __generate_linear_cols(T, depth - 1, target + 1, dim)), axis=0)

def linear(T, depth, dim):
    """Returns transition state matrix for linear model"""
    return __generate_linear_cols(T, depth, 0, dim)

def constant_turn_2d(T, wk):
    """Returns 2d state transition matrix with constant turn wk [rad/sec]"""
    return np.array([
        [1, 0, np.sin(wk * T) / wk, (np.cos(wk * T) - 1) / wk],
        [0, 1, (1 - np.cos(wk * T)) / wk, np.sin(wk * T) / wk],
        [0, 0, np.cos(wk * T), -np.sin(wk * T)],
        [0, 0, np.sin(wk * T), np.cos(wk * T)],
    ])


import numpy as np

# TODO: finish the stupid ass gradients

'''
grad() returns ∂Hi/∂y where Hi is the inside of the error function
it's the part that is commmon in all of the derivative, caused/
which shows up because of the chain rule
# https://stats.stackexchange.com/questions/154879/a-list-of-cost-functions-used-in-neural-networks-alongside-applications
'''
class ErrorFunction:

	def __init__(self, name):
		self.name = name

class MSE(ErrorFunction):

	def __init__(self):
		super().__init__('Mean Squared Error')

	def __call__(self, Y, predictions):
		return np.sum((predictions-Y)**2)/len(Y)

	def grad(self, Y, predictions):
		return 2*(predictions-Y)/len(Y)

# Mean Absolute Error (or Huber loss)
class MAE(ErrorFunction):

	def __init__(self):
		super().__init__('Mean Absolute Error')

	def __call__(self, Y, predictions):
		return np.sum(np.abs(predictions-Y))/len(Y)

	def grad(self, Y, predictions):
		return np.sign(predictions-Y)/len(Y)

# Mean Bias Error (not recommended)
class MBE(ErrorFunction):

	def __init__(self):
		super().__init__('Mean Bias Error')

	def __call__(self, Y, predictions):
		return np.sum(Y-predictions)/len(Y)

	def grad(self, Y, predictions):
		return -np.ones(Y.shape)

class CrossEntropy(ErrorFunction):

	def __init__(self):
		super().__init__('Cross Entropy')

	def __call__(self, Y, predictions):
		return -np.sum(Y*np.log(predictions)+(1-Y)*np.log(1-predictions))

	def grad(self, Y, predictions):
		return (predictions-Y)/(len(Y)*(1-predictions)*predictions)

class CategoricalCrossEntropy(ErrorFunction):

	def __init__(self):
		super().__init__('Categorical Cross Entropy')

	def __call__(self, Y, predictions):
		return -np.sum(Y*np.log(predictions))

	def grad(self, Y, predictions):
		return -Y/predictions

class KullbackLeiblerDivergence(ErrorFunction):

	def __init__(self):
		super().__init__('Kullback-Leibler divergence')

	def __call__(self, Y, predictions):
		return np.sum(Y*np.log(Y/predictions))

	def grad(self, Y, predictions):
		return -Y/predictions

class Exponential(ErrorFunction):

	def __init__(self, t=0.1):
		super().__init__('Exponential')
		self.t = t

	def __call__(self, Y, predictions):
		return self.t*np.exp(np.sum((Y-predictions)**2)/self.t)

	def grad(self, Y, predictions):
		return 2/self.t*self.__call__(Y, predictions)*(predictions-Y)

class HellingerDistance(ErrorFunction):

	def __init__(self):
		super().__init__('Hellinger Distance')

	def __call__(self, Y, predictions):
		return np.sum((np.sqrt(Y)-np.sqrt(predictions))**2)/np.sqrt(2)

	def grad(self, Y, predictions):
		return (np.sqrt(predictions)-np.sqrt(Y))/(np.sqrt(2*predictions))


import sys
import numpy
from scipy.special import binom

def help():
    print("Usage: python nirvana.py NdK P")
    print("\tN: number of dice, e.g. 3")
    print("\tK: number of sides for each die, e.g. 6")
    print("\tP: probability of success, e.g. 15")

def computeArguments(arg):
    [die, sides] = arg[1].split("d")
    prob = arg[2]
    return (int(die), int(sides), float(prob))

def signedK(dieNumber, dieSides, s):
    return int(numpy.floor((s - dieNumber) / dieSides))

def specialSum(n, k, s):
    accumulator = 0
    signedk = signedK(n, k, s)
    for j in range(signedk + 1):
        partial = ((-1)**j) * binom(n, j) * binom(s - k*j - 1, s - k*j - n)
        accumulator += partial
    return accumulator

def computeDifficulty(die, sides, prob):
    if prob >= 1:
        prob /= 100
    difficulty = die * sides
    currentProbability = 0
    while (currentProbability < prob) & (difficulty > 1):
        probToRollDiff = (1 / (sides ** die)) * specialSum(die, sides, difficulty)
        currentProbability += probToRollDiff
        difficulty -= 1
    return difficulty

if __name__ == "__main__":

    if (len(sys.argv)) == 1:
        for die in ["1d2", "2d4", "3d6", "4d8", "5d12", "6d24", "7d30", "8d60", "9d100"]:
            for P in ["1", "15", "55", "60", "70", "80", "85", "90", "95", "97", "99"]:
                (d, side) = die.split("d")
                if (int(d) > 5) and (P == "55"):
                    print(die, "30", computeDifficulty(int(d), int(side), 30))
                else:
                    print(die, P, computeDifficulty(int(d), int(side), int(P)))
        exit()

    if (len(sys.argv)) != 3:
        help()
        exit()
    (die, sides, probability) = computeArguments(sys.argv)
    print(computeDifficulty(die, sides, probability))


import numpy as np

features=[[1,2,3],
          [1,4,9],
          [1,5,0]]
X = np.array(features)

prices = [5,
          13,
          5]
y=np.array(prices)

theta = [0]*len(features[0])
theta = np.transpose(np.array(theta))

LEARNING_RATE=0.01
NO_TRAINING_EXAMPLES = len(features)
EPSILON=0.000000001

def predict(X,theta):
    y=np.matmul(np.transpose(theta),X)
    return y

def cost(X,y,theta):
    return (1/(2*NO_TRAINING_EXAMPLES))*np.transpose((X@theta - y))@(X@theta - y)

def gradient_descent(X,y,theta):
    old_cost = cost(X, y, theta)
    while True:
        sum=0
        for i in range(NO_TRAINING_EXAMPLES):
            sum += (predict(X[i],theta)-y[i])*X[i]
        delta = sum/NO_TRAINING_EXAMPLES
        theta=theta-LEARNING_RATE*np.transpose(delta)

        new_cost=cost(X,y,theta)

        # test for convergence
        if abs(old_cost - new_cost) < EPSILON:
            break
        else:
            old_cost = new_cost
    return theta
print(gradient_descent(X,y,theta))


'''
Compare different window functions to estimate spectrum
Key: the coefficient to normalize the spectrum

XiaoCY 2021-02-02
'''

#%%
import numpy as np
import matplotlib.pyplot as plt

#%%
fs = 1000                           # sampling frequency
fsig = 100                          # signal frequency

t = np.arange(0,100/fsig,1/fs)      # time series
N = len(t)                          # number of samples

x = np.cos(2*np.pi*fsig*t)

Nf = int(np.ceil(N/2))
f = np.arange(0,Nf)*fs/N

#%%
# rectangular window
X = np.fft.fft(x,axis=0)
amp = np.abs(X[0:Nf])*2/N

plt.figure(1)
plt.plot(f,amp,label='rectwin')

# hanning window
win = np.hanning(N)
X = np.fft.fft(x*win,axis=0)
amp = np.abs(X[0:Nf])*2/np.sum(win)
plt.plot(f,amp,label='hanning')

# hamming window
win = np.hamming(N)
X = np.fft.fft(x*win,axis=0)
amp = np.abs(X[0:Nf])*2/np.sum(win)
plt.plot(f,amp,label='hamming')

# blackman window
win = np.blackman(N)
X = np.fft.fft(x*win,axis=0)
amp = np.abs(X[0:Nf])*2/np.sum(win)
plt.plot(f,amp,label='blackman')

plt.grid()
plt.legend()
plt.xlabel('Frequency (Hz)')
plt.ylabel('Spectrum')
plt.xlim(80,120)


import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

def save_fig(fig_id):
    plt.savefig(image_path(fig_id) + ".png", format='png')

def R2(y_data, y_model):
    return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2)
def MSE(y_data,y_model):
    n = np.size(y_model)
    return np.sum((y_data-y_model)**2)/n

x = np.random.rand(100)
y = 2.0+5*x*x+0.1*np.random.randn(100)

#  The design matrix now as function of a given polynomial
X = np.zeros((len(x),3))
X[:,0] = 1.0
X[:,1] = x
X[:,2] = x**2
# We split the data in test and training data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# matrix inversion to find beta_ols
beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train
print(beta)

reg = LinearRegression().fit(X_train, y_train)
beta_sci = reg.coef_
beta_sci[0] = reg.intercept_
print(beta_sci)

# and then make the prediction
ytilde = X_train @ beta
print("Training R2")
print(R2(y_train,ytilde))
print("Training MSE")
print(MSE(y_train,ytilde))
ypredict = X_test @ beta
print("Test R2")
print(R2(y_test,ypredict))
print("Test MSE")
print(MSE(y_test,ypredict))

plt.plot(x, y ,'ro')
#plt.plot(x, ytilde) #not of same dimensions?

x_plot = np.linspace(0,2,1000)

X_plot = np.zeros((len(x_plot),3))
X_plot[:,0] = 1.0
X_plot[:,1] = x_plot
X_plot[:,2] = x_plot**2

plt.plot(x_plot, X_plot @ beta)

plt.show()


import numpy as np
from numpy.lib.function_base import angle

# Returns a circle with unary diameter
def generate_circle_points_func(n: int) -> np.ndarray:
    # List of angles
    angles: np.ndarray = np.linspace(0, 2 * np.pi, num=n)

    # Create circle
    circle: np.ndarray = np.zeros((n, 2))
    circle[:, 0] = 0.5 * np.cos(angles)
    circle[:, 1] = 0.5 * np.sin(angles)

    return np.copy(circle)


# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

import numpy as np
import matplotlib.pyplot as plt

def target_function(x):
    y = x*x
    return y

def derivative_function(x):
    return 2*x

def draw_function():
    x = np.linspace(-1.2,1.2)
    y = target_function(x)
    plt.plot(x,y)

def draw_gd(X):
    Y = []
    for i in range(len(X)):
        Y.append(target_function(X[i]))

    plt.plot(X,Y)

if __name__ == '__main__':
    x = 1.2
    eta = 0.3
    error = 1e-3
    X = []
    X.append(x)
    y = target_function(x)
    while y > error:
        x = x - eta * derivative_function(x)
        X.append(x)
        y = target_function(x)
        print("x=%f, y=%f" %(x,y))

    draw_function()
    draw_gd(X)
    plt.show()


# Polynomial Regression

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

from sklearn.linear_model import LinearRegression
lin_reg  = LinearRegression()
lin_reg.fit(X,y)

from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)

lin_reg2 = LinearRegression()
lin_reg2.fit(X_poly,y)

plt.scatter(X,y,color = 'red')
plt.plot(X,lin_reg.predict(X),color = 'blue')
plt.title('truth or bluff - linear reg')
plt.xlabel('POsition level')
plt.ylabel('salary')
plt.show()

plt.scatter(X,y,color = 'red')
plt.plot(X,lin_reg2.predict(poly_reg.fit_transform(X)),color = 'blue')
plt.title('truth or bluff - linear reg')
plt.xlabel('POsition level')
plt.ylabel('salary')
plt.show()

lin_reg2.predict(poly_reg.fit_transform([[6.5]]))


# -*- coding: utf-8 -*-
"""
Created on Sat Oct 27 06:55:00 2018

@author: PCLee

Demonstration of PCA for feature extraction
"""

import pandas as pd
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression
from matplotlib import pyplot as plt
import numpy as np

def PlotVarExplanation(eigen_vals):
    plt.figure()
    eigen_sum = np.sum( eigen_vals)
    eigen_ratio = [ i / eigen_sum for i in eigen_vals ]
    eigen_ratio_accum = np.cumsum( eigen_ratio )

    plt.bar(range(1, cov_mat.shape[0]+1), eigen_ratio, alpha=0.5, align='center',
            label='individual explained variance')
    plt.step(range(1, cov_mat.shape[0]+1), eigen_ratio_accum, where='mid',
             label='cumulative explained variance')
    plt.ylabel('Explained variance ratio')
    plt.xlabel('Principal components')
    plt.legend(loc='best')
    plt.tight_layout()
    plt.show()

df_wine = pd.read_csv('../Data/winequality-red.csv', sep=';')
df_shape = df_wine.shape
X, y = df_wine.iloc[:, 0:df_shape[1]-1],  df_wine.iloc[:, df_shape[1]-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=0)

stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(X_train)
X_test_std = stdsc.transform(X_test)

'''
calculate covariance matrix of input data
Note1. the input dimensions for numpy.cov is 'featurex x data size'
Note2. Be caution of the order of the reported eigenvalues from
       linalg.eig and linalg.eigh
'''

cov_mat = np.cov( X_train_std.T )
eigen_vals, eigen_vecs = np.linalg.eigh(cov_mat)
eigen_vals = np.flip( eigen_vals, axis=0) #reverse the order of eigenvalues
eigen_vecs = np.flip( eigen_vecs, axis=0) #reverse the order of eigenvectors

PlotVarExplanation(eigen_vals)

'''
Choose the top-2 explaining factors andconstruct the transformation
matrix, W.
'''

eigen_pairs = [ (np.abs(eigen_vals[i]), eigen_vecs[:,i]) for i in range( len(eigen_vals)) ]
W = np.hstack((eigen_pairs[0][1][:, np.newaxis],
               eigen_pairs[1][1][:, np.newaxis]
               ))

'''
Transformation
'''

X_train_pca = np.dot(X_train_std, W)
X_test_pca = np.dot(X_test_std, W)

'''
Let's see how good it is
'''
lr = LogisticRegression(penalty='l1', C=0.1)
train_accu = []
test_accu = []

for i in range(2, X_train.shape[1], 1):
    W = np.hstack( ( eigen_pairs[k][1][:,np.newaxis] ) for k in range( i)  )
    X_train_pca = np.dot(X_train_std, W)
    X_test_pca = np.dot(X_test_std, W)
    lr.fit(X_train_pca, y_train)
    train_score = lr.score(X_train_pca, y_train)
    test_score = lr.score(X_test_pca, y_test)
    train_accu.append( train_score)
    test_accu.append( test_score)
    print( 'Trainning accuracy: {}'.format(train_score))
    print( 'Test accuracy: {}'.format(test_score))

plt.figure()
plt.xlabel('feature number')
plt.ylabel('Accuraccy')
plt.ylim((0.4, 1.0))
plt.plot(np.arange(2, 2+len(train_accu)), train_accu, label='Train')
plt.plot(np.arange(2, 2+len(test_accu)), test_accu, label='Test')
plt.legend(loc='best')
plt.show()

lr.fit(X_train_std, y_train)
print('Standard accuracy: {}'.format( lr.score(X_test_std, y_test) ) )


"""This file contains code used in "Think Stats",
by Allen B. Downey, available from greenteapress.com

Copyright 2015 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import numpy
import thinkbayes
import thinkplot

"""
This problem presents a solution to the "Bayesian Billiards Problem",
presented in this video:

https://www.youtube.com/watch?v=KhAUfqhLakw

Based on the formulation in this paper:

http://www.nature.com/nbt/journal/v22/n9/full/nbt0904-1177.html

Of a problem originally posed by Bayes himself.
"""

class Billiards(thinkbayes.Suite):

    def Likelihood(self, data, hypo):
        """Computes the likelihood of the data under the hypothesis.

        data: tuple (#wins, #losses)
        hypo: float probability of win
        """
        p = hypo
        win, lose = data
        like = p**win * (1-p)**lose
        return like

def ProbWinMatch(pmf):
    total = 0
    for p, prob in pmf.Items():
        total += prob * (1-p)**3
    return total

def main():
    ps = numpy.linspace(0, 1, 101)
    bill = Billiards(ps)
    bill.Update((5, 3))
    thinkplot.Pdf(bill)
    thinkplot.Save(root='billiards1',
                   xlabel='probability of win',
                   ylabel='PDF',
                   formats=['png'])

    bayes_result = ProbWinMatch(bill)
    print(thinkbayes.Odds(1-bayes_result))

    mle = 5 / 8
    freq_result = (1-mle)**3
    print(thinkbayes.Odds(1-freq_result))

if __name__ == '__main__':
    main()


"""
descent.py

Author: Daniel Lefevre

12/11/2020

Implementation of 1-dimensional gradient descent for linear regression

"""

from collections import deque

import numpy as np
import matplotlib.pyplot as plt

def hypothesis(feature_matrix, theta_matrix):
    """Compute the linear hypothesis function"""
    return np.matmul(feature_matrix, theta_matrix)

def cost_function(m, feature_matrix, theta_matrix, labels):
    """Compute the error from the hypothesis and y values"""
    return (1 / (2 * m)) * np.sum(np.square(hypothesis(feature_matrix, theta_matrix) - labels))

def update_theta(alpha, m, theta_matrix, feature_matrix, labels):
    """Simultaneously update theta_0 and theta_1 according to the algorithm"""
    new_theta = np.zeros((2, 1))
    new_theta[0] = theta_matrix[0] - (alpha/m)*np.sum(hypothesis(feature_matrix, theta_matrix) - labels)
    new_theta[1] = theta_matrix[1] \
                  - (alpha/m)*np.matmul(np.array([feature_matrix[:, 1]]),
                                        (hypothesis(feature_matrix, theta_matrix) - labels))
    return new_theta

def gradient_descent(alpha, m, theta_matrix, feature_matrix, labels):
    """Compute and store the cost function for current theta guess,
       update theta, and repeat until the cost function no longer decreases.

       Return the last computed value of theta"""
    track_cost = deque(maxlen=2)
    J = cost_function(m, feature_matrix, theta_matrix, labels)
    track_cost.append(J)
    while True:
        theta_matrix = update_theta(alpha, m, theta_matrix, feature_matrix, labels)
        new_cost = cost_function(m, feature_matrix, theta_matrix, labels)
        if new_cost < track_cost[0]:
            track_cost.append(new_cost)
        else:
            return theta_matrix

def add_line(axis, feature_matrix, theta_matrix):
    """Plot a line given the slope and intercept (theta_matrix)"""
    intercept = theta_matrix[0]
    slope = theta_matrix[1]
    x_values = np.arange(min(feature_matrix[:, 1]), max(feature_matrix[:, 1]) + 1)
    y_values = intercept + slope * x_values
    axis.plot(x_values, y_values)

# Create some initial data and add noise to y values
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
noise = np.random.random(5) - 0.5
y = y + noise

# Plot the initial data
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y)

# Gradient Descent
# learning rate
learning_rate = 0.01
# m = number of examples
num_examples = x.shape[0]
# Reshape X, y into column vectors
# Append a column of 1's in front of X for the theta_nought coefficients
features = np.transpose([x])
X = np.hstack((np.ones_like(features), features))
y = np.transpose([y])
# initialize theta to [[0], [0]]
theta = np.zeros((2, 1))

problem_data = {"m": num_examples,
                "alpha": learning_rate,
                "feature_matrix": X,
                "theta_matrix": theta,
                "labels": y}

# perform gradient descent to find optimal theta
optimized_theta = gradient_descent(**problem_data)

# plot the result along with the initial data
result_plot_data = {"axis": ax,
                    "feature_matrix": X,
                    "theta_matrix": optimized_theta}
add_line(**result_plot_data)

plt.show()


from numpy.random import normal
import numpy as np

def cNoise(beta,shape=(1024,),std=0.001):
    '''
       Wrote by: Rubens Andreas Sautter (2021)

       Approximating spectral decay by the squared root of the 1/(f^n).
       Frequency are measured in multidimensional space by the frequency euclidian distance

       The gaussian standard deviation

       Based on paper:
      http://articles.adsabs.harvard.edu//full/1995A%26A...300..707T/0000707.000.html
    '''
    dimension = []
    for index,dsize in enumerate(shape):
        dimension.append(np.fft.fftfreq(dsize).tolist())
    dimension = tuple(dimension)
    d = float(len(dimension))

    freqs = np.power(np.sum(np.array(np.meshgrid(*dimension,indexing='ij'))**2,axis=0),1/2)

    #Sampling gaussian with sandard deviation varying according to frequency
    ftSample = normal(loc=0,scale=std,size=shape) + 1j*normal(loc=0,scale=std,size=shape)

    # Setting the scale [0,2pi]
    freqs = np.pi*freqs
    not0Freq = (np.abs(freqs)>1e-15)

    decayCorrection = np.sqrt(2)**(d-1)

    scaling = (freqs[not0Freq]+0j)**(-(beta*decayCorrection )/2)

    ftSample[not0Freq] = (ftSample[not0Freq]*scaling)

    not0Freq = (np.abs(freqs)>1e-15)

    spsd = np.sum(np.abs(ftSample))

    # zero avg
    ftSample[0] = 0.0

    out = np.fft.ifftn(ftSample*spsd).real

    return out


import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize

# Metodo con fit
def funzione(x, a, b) :
    return a + b*x

def derivata(x, a, b) :
    return b

def chi2(x, popt, y, dy) :
    return np.sum(((y-funzione(x, *popt))/dy)**2)

# Dati
Rj, dRj = np.genfromtxt("digitalizzate.txt", skip_header=0, unpack=True)
Ij, dIj = np.genfromtxt("ddp.txt", skip_header=0, unpack=True)

# Fit
ndof = 10
dI_eff = dIj
popt, pcov = scipy.optimize.curve_fit(funzione, Rj, Ij, [4, 18], dI_eff, absolute_sigma=False)
chi2_new = chi2(Rj, popt, Ij, dI_eff)
chi2_old = -1
print(chi2_new)
for i in range (10) :
    dI_eff = ((derivata(Rj, *popt)*dRj)**2 + dIj**2)**0.5
    popt, pcov = scipy.optimize.curve_fit(funzione, Rj, Ij, [4, 18], dI_eff, absolute_sigma=False)
    chi2_old = chi2_new
    chi2_new = chi2(Rj, popt, Ij, dI_eff)
    print(chi2_new)

print("\nintercetta", popt[0], pcov[0][0]**0.5)
print("coeff angolare", popt[1], pcov[1][1]**0.5)
print("chi2 ", chi2_new)
print("chi2/ndof {}/{}={}".format(chi2_new, ndof, chi2_new/ndof))
print("cov normalizzata", pcov[1][0]/(pcov[0][0]*pcov[1][1])**0.5, "\n")

# Grafici
# Plot funzione
x = np.linspace(0, 1000, 4000)
plt.figure()
plt.subplot(211)
plt.errorbar(Rj, Ij, yerr=dIj, xerr=dRj, fmt='.', label="Misure")
plt.plot(x, funzione(x, *popt), label="Fit")
plt.xlabel('Valori digitalizzati X [digit]')
plt.ylabel('ddp [V]')
plt.legend()

# Plot residui normalizzati
plt.subplot(212)
plt.errorbar(Rj, (Ij-funzione(Rj, *popt))/dI_eff, fmt='.', label="Residui normalizzati")
plt.plot(x, x*0)
plt.xlabel('Valori digitalizzati X [digit]')
plt.legend()
plt.show()


import numpy as np
import math

def rotation_matrix(angles):
    """
    Calculates rotation matrix in ZYX form

    :param angles: [z axis(psi), y axis(theta), x axis(phi)]
    :return: Rotation matrix
    """

    cs, ct, cp = np.cos(angles)
    ss, st, sp = np.sin(angles)

    R_x = np.array([[1, 0, 0],
                    [0, cp, -sp],
                    [0, sp, cp]])
    R_y = np.array([[ct, 0, st],
                    [0, 1, 0],
                    [-st, 0, ct]])
    R_z = np.array([[cs, -ss, 0],
                    [ss, cs, 0],
                    [0, 0, 1]])
    return R_z @ R_y @ R_x

def wrap_angle(angle):
    """
    Constrain angle between [-pi, pi]
    :param angle: angle, in radians
    :return: Constrained angle
    """
    return (angle + np.pi) % (2 * np.pi) - np.pi

def thrust(speed, pitch, diameter):
    """
    From http://www.electricrcaircraftguy.com/2013/09/propeller-static-dynamic-thrust-equation.html
    :param speed: RPM of the motor
    :param pitch: Propeller pitch (tilt), in inches
    :param diameter: Propeller diameter, in inches
    :return: Thrust of the propeller, in N
    """
    return 4.392e-8 * speed * math.pow(diameter, 3.5) / (math.sqrt(pitch)) * 4.23e-4 * speed * pitch


import numpy as np
from numpy.linalg import svd
from itertools import product
import matplotlib.pyplot as plt

# Rank-3 TensorNetwork

def TRG_3_rank(K, beta , D, Dcut, no_iter):
    T = np.empty([D, D, D])

    inds = np.arange(D)

    for i, j, k in product(inds, repeat=3):
        T[i][j][k] = 0.5*(1 + (2*i - 1)*(2*j - 1)*(2*k - 1)) * \
            np.exp(beta*K*(i+j+k - 2))

    for n in range(no_iter):
        D_new = min(D**2, Dcut)
        inds = np.arange(D)
        inds_new = np.arange(D_new)

        M = np.zeros(shape=(D**2, D**2))
        for i, j, k, l in product(inds, repeat=4):
            for m in inds:
                M[i+ l*D][k + j*D] += T[i][j][m]*T[k][l][m]

        S_a, S_b = [np.zeros([D, D, D_new ])]*2

        U, L, V = svd(M)
        L = np.sort(L)[::-1][0: D_new]

        for x, y, m in product(inds, inds, inds_new):
            S_a[x, y, m] = np.sqrt(L[m])*U[x + D*y][m]
            S_b[x, y, m] = np.sqrt(L[m])*V[m][x + D*y]

        T_new_a,T_new_b = [np.zeros([D_new, D_new, D_new])]*2

        for i, j, k in product(inds_new, repeat=3):
            for r, u, l in product(inds, repeat=3):
                T_new_a[i][j][k] += S_a[r, u, k]*S_a[u, l, j]*S_a[l, r, i]
                T_new_b[i][j][k] += S_b[r, u, k]*S_b[u, l, j]*S_b[l, r, i]

        D = D_new
        inds = inds_new
        T_a = T_new_a
        T_b = T_new_b

        Z = 0
        for i, j, k in product (inds , inds , inds):
            Z += T_a[i][j][k]*T_b[i][j][k]

        return Z

if __name__ == "__main__":

    B = np.linspace(0,1.6,100)
    A = np.array([])
    for i in B:
        Z = TRG_3_rank(K=i, beta=2, D=2, Dcut=6, no_iter=3)
        A = np.append(A,Z)

    plt.figure(figsize=(8,5))
    plt.plot(B,np.log10(A),'-o',label='Tensor renormalization group')
#    plt.xlim([0,1.3])
    plt.legend(fontsize=16)
    plt.xlabel('Coupling constant',fontsize=18)
    plt.ylabel('Free energy per unit site',fontsize=18)
    plt.savefig('TRG_Honeycomb.png')


import numpy as np

# log norm distribution with 1 peak
def log_norm_func1(data,center1,sigma1,scale1):
    return scale1*(1/(data*np.sqrt(2*np.pi*sigma1**2))*np.exp(-(((np.log(data)-center1)**2)/(2*sigma1**2))))

# log norm distribution with 2 peak
def log_norm_func2(data,center1,sigma1,scale1, center2,sigma2,scale2):
    return scale1*(1/(data*sigma1*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center1)**2)/(2*sigma1**2)))) +\
            scale2*(1/(data*sigma2*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center2)**2)/(2*sigma2**2))))

# log norm distribution with 3 peak
def log_norm_func3(data,center1,sigma1,scale1, center2,sigma2,scale2, center3,sigma3,scale3):
    return scale1*(1/(data*sigma1*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center1)**2)/(2*sigma1**2)))) +\
            scale2*(1/(data*sigma2*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center2)**2)/(2*sigma2**2)))) +\
            scale3*(1/(data*sigma3*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center3)**2)/(2*sigma3**2))))

#log norm distribution with 4 peaks
def log_norm_func4(data,center1,sigma1,scale1, center2,sigma2,scale2, center3,sigma3,scale3, center4,sigma4,scale4):
    return scale1*(1/(data*sigma1*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center1)**2)/(2*sigma1**2)))) +\
            scale2*(1/(data*sigma2*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center2)**2)/(2*sigma2**2)))) +\
            scale3*(1/(data*sigma3*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center3)**2)/(2*sigma3**2)))) +\
            scale4*(1/(data*sigma4*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center4)**2)/(2*sigma4**2))))

#log norm distribution with 5 peaks
def log_norm_func5(data,center1,sigma1,scale1, center2,sigma2,scale2, center3,sigma3,scale3, center4,sigma4,scale4, center5,sigma5,scale5):
    return scale1*(1/(data*sigma1*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center1)**2)/(2*sigma1**2)))) +\
            scale2*(1/(data*sigma2*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center2)**2)/(2*sigma2**2)))) +\
            scale3*(1/(data*sigma3*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center3)**2)/(2*sigma3**2)))) +\
            scale4*(1/(data*sigma4*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center4)**2)/(2*sigma4**2)))) +\
            scale5*(1/(data*sigma5*np.sqrt(2*np.pi))*np.exp(-(((np.log(data)-center5)**2)/(2*sigma5**2))))


import numpy as np

def dft_factor(k, n, n_tot):
    return np.exp(2j * np.pi * k * n / n_tot)

def dft_matrix(n_tot):
    ret_mat = np.zeros((n_tot, n_tot), dtype=np.complex128)
    for k in range(0, n_tot):
        for n in range(0, n_tot):
            ret_mat[k, n] = dft_factor(k, n, n_tot)
    return ret_mat

def idft_matrix(n_tot):
    ret_mat = np.zeros((n_tot, n_tot), dtype=np.complex128)
    for k in range(0, n_tot):
        for n in range(0, n_tot):
            ret_mat[k, n] = dft_factor(k, n, n_tot).conjugate()
    return ret_mat

# gives angular frequency -> 2 pi nu = omega
def dft_frequencies(n_tot, sampling_rate):
    tmp = np.linspace(0, np.pi * (n_tot - 1) * sampling_rate / n_tot, int(n_tot / 2) + 1, dtype=np.float64)
    tmp2 = np.linspace(-np.pi * (n_tot - 1) * sampling_rate / n_tot, 0, int(n_tot / 2) + 1, dtype=np.float64)
    return np.append(tmp, tmp2[:-1])

def idft_times(n_tot, sampling_rate):
    return np.linspace(0, (n_tot - 1) / sampling_rate, n_tot, dtype=np.float64)

def dft(f_array, sampling_rate):
    return np.column_stack((dft_frequencies(len(f_array), sampling_rate),
                            np.dot(dft_matrix(len(f_array)), f_array) / np.sqrt(len(f_array))))

def idft(f_array, sampling_rate):
    return np.column_stack((idft_times(len(f_array), sampling_rate),
                            idft_matrix(len(f_array)).dot(f_array) / np.sqrt(len(f_array))))

def rearrange(f_array):
    return np.concatenate((f_array[int(len(f_array)/2) + 1:], f_array[:int(len(f_array)/2) + 1]))


# coding:utf8

from numpy import *
import matplotlib
import matplotlib.pyplot as plt

'''
Description:PCA新闻数据降维并可视化分析
Author：伏草惟存
Prompt: code in Python3 env
'''

'''加载数据集'''
def loadDataSet(fileName, delim='\t'):
    fr = open(fileName)
    stringArr = [line.strip().split(delim) for line in fr.readlines()]
    datArr = [list(map(float, line)) for line in stringArr]
    return mat(datArr)

# 将NaN替换成平均值函数：secom.data 为1567篇文章，每篇文章590个单词
def replaceNanWithMean():
    datMat = loadDataSet('./secom.data', ' ')
    numFeat = shape(datMat)[1]
    for i in range(numFeat):
        meanVal = mean(datMat[nonzero(~isnan(datMat[:, i].A))[0], i])
        datMat[nonzero(isnan(datMat[:, i].A))[0],i] = meanVal
    return datMat

# 主成分降维
def pca(dataMat, topNfeat=9999999):
    # 1 计算每一列的均值
    meanVals = mean(dataMat, axis=0) # axis=0表示列，axis=1表示行

    # 2 去平均值，每个向量同时都减去均值
    meanRemoved = dataMat - meanVals

    # 3 计算协方差矩阵的特征值与特征向量
    covMat = cov(meanRemoved, rowvar=0)
    eigVals, eigVects = linalg.eig(mat(covMat))
    print('特征值:\n', eigVals,'\n特征向量:\n', eigVects) # 很多特征值为0

    # 4 将特征值排序
    eigValInd = argsort(eigVals)

    # 5 保留前N个特征
    eigValInd = eigValInd[:-(topNfeat+1):-1]
    redEigVects = eigVects[:, eigValInd]
    # print('重组n特征向量最大到最小:\n', redEigVects.T)

    # 6 将数据转换到新空间
    lowDDataMat = meanRemoved * redEigVects
    reconMat = (lowDDataMat * redEigVects.T) + meanVals
    return lowDDataMat, reconMat

'''降维后的数据和原始数据可视化'''
def show_picture(dataMat, reconMat):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(dataMat[:, 0].flatten().A[0], dataMat[:, 1].flatten().A[0], marker='^', s=5,c='green')
    ax.scatter(reconMat[:, 0].flatten().A[0], reconMat[:, 1].flatten().A[0], marker='o', s=5, c='red')
    plt.show()

if __name__ == "__main__":
    dataMat = replaceNanWithMean()
    # 分析数据
    lowDmat, reconMat = pca(dataMat,17)
    print(shape(lowDmat)) # 1567篇文章，提取前20个单词
    show_picture(dataMat, reconMat)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Plot 2 dimension data (version 2)
"""

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import math

# Build datas ###############

x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)

xx,yy = np.meshgrid(x, y)

z = np.zeros(xx.shape)

for xi in range(z.shape[0]):
    for yi in range(z.shape[1]):
        z[xi, yi] = math.sin(math.sqrt(xx[xi, yi]**2 + yy[xi, yi]**2))

# Plot data #################

fig = plt.figure()
ax = axes3d.Axes3D(fig)
ax.plot_wireframe(xx, yy, z)

# SAVE FILES ######################
plt.savefig("demo1b_mplot3d.png")

plt.show()


"""
    Single Neuron Linear Regression with data normalization
    N = #Samples
    i = [0, ... ,N]
    M = Dimension of samples +1(offset b)
    Output a_i
    a_i = w.transpose()*x_i
    Cost C
    C = sum(0.5*(a_i-y_i)**2)
"""

import numpy as np
from dataset1_linreg import DataSet

def get_norm_params(x_arr):
    mean_x = np.sum(x_arr, axis=1)/x_arr.shape[1]
    mean_x = np.array([mean_x]).transpose()
    mx = np.tile(mean_x, (1, x_arr.shape[1]))
    xmm = (x_arr - mx)**2
    stdd_x = np.sqrt(np.sum(xmm, axis=1)/x_arr.shape[1])
    stdd_x = np.array([stdd_x]).transpose()
    return mean_x, stdd_x

def predict_y1(x_arr):
    # Normalize Input Samples
    if normalize_data:
        x_arr = (x_arr - mean) / stdd
    a = w[1][0] * x_arr + w[0][0]
    return a

def l2_cost(x_arr, y_arr, predict_f):
    cost = np.sum((y_arr - predict_f(x_arr)) ** 2) * 0.5
    return cost

# Compute gradient of cost function
def gradient_w(x_arr, y_arr, predict_f):
    amy = predict_f(np.array([x_arr[0, :]])) - y_arr
    ones = np.ones((1, x_arr.shape[1]))
    X = np.vstack((ones, x_arr))
    grad_w = amy.dot(X.transpose())
    return grad_w.transpose()

if __name__ == '__main__':
    ##########################
    normalize_data = True
    ##########################
    print("Normalize data: {}".format(normalize_data))
    y_D, x_D = DataSet.get_data()
    DataSet.plot_data()
    # Initialize weights
    w = np.array([[0.1], [0.1]])

    mean, stdd = get_norm_params(x_D)
    DataSet.plot_model(predict_y1)

    print('Initial cost: %.3f' % l2_cost(x_D, y_D, predict_y1))
    grad = gradient_w(x_D, y_D, predict_y1)
    print("Initial gradient:{}".format(grad))

    # Weight tuning
    i = 0
    while l2_cost(x_D, y_D, predict_y1) > 0.9:
        w = w - 0.01 * gradient_w(x_D, y_D, predict_y1)
        i += 1

    DataSet.plot_model(predict_y1)
    print('Final Cost: %f' % l2_cost(x_D, y_D, predict_y1))
    print("Iterations needed: {}".format(i))


import numpy as np
from scipy import stats
import statistics
import matplotlib.pyplot as plt
plt.style.use('ggplot')

# Scores obtained for the interaction appreciation by the control group
# The penalties due to late reaction have already been applied.
test = np.array([[4.0, 4, 2, 5, 4],
                    [2,   4, 3, 4, 5],
                    [2,   2, 1, 5, 5],
                    [4,   4, 2, 5, 5],
                    [4,   4, 3, 4, 2]])

# Scores obtained for the interaction appreciation by the test group
# The penalties due to late reaction have already been applied.
control    = np.array([[1.0, 3, 2, 5, 1],
                    [4  , 3, 1, 5, 3],
                    [5  , 3, 5, 2, 4],
                    [3  , 4, 1, 3, 2],
                    [2  , 1, 4, 3, 5],
                    [5  , 4, 2, 3, 2]])

# get the mean score of each participant
control_mean = np.nanmean(control, axis = 1)
test_mean    = np.nanmean(test, axis = 1)
print(control_mean)
print(test_mean)
# display both results
fig, ax = plt.subplots()
ax.boxplot((control_mean, test_mean), vert=True, showmeans=False,meanline=True,
           labels=('Control group score','Test group score'),patch_artist=True,
           medianprops={'linewidth': 2, 'color': 'purple'},
           meanprops={'linewidth': 2, 'color': 'red'})
plt.show()

# compute the p-value with T-test
_,p_value=stats.ttest_ind(control_mean,test_mean)
alpha = 0.05

print("===========================================")
print(" Variance of control group:", round(statistics.variance(control_mean), 3))
print(" Variance of test group:   ", round(statistics.variance(test_mean), 3))
print(' P-value for two tailed test is %f'%p_value)
if p_value<=alpha:
    print(' Null hypothesis can be rejected')
else :
    print(' Null hypothesis cannot be rejected')
print("===========================================")


import numpy as np
import torch

from scipy.special import comb

##### Auditory filters #####

class GaussianFilter:
	'''
	class for Gaussian filters. Main methods are just a recall of some formulas.
	The model mainly uses the class method 'int_standard_sq'
	'''
	def __init__(self, sig_f, f_c):
		self.sig_f=sig_f
		self.f_c=f_c

	def __call__(self, f):
		'''
		Args:
			f: float or numpy array of frequency values
		Returns:
			The frequency response at f (amplitude spectrum).
			'''
		return 1/np.sqrt(np.sqrt(2*np.pi)*self.sig_f)*torch.exp(-1/4*(f-self.f_c)**2/self.sig_f**2)

	@classmethod
	def givenQ10(cls, f_c, Q10):
		'''
		Alternative constructor with f and Q10 values
		'''
		BW10=f_c/Q10
		sig_f=BW10/(2*np.sqrt(2*np.log(10)))
		return cls(sig_f, f_c)

	def BW10(self):
		'''
		Returns:
			The 10dB bandwidth
		'''
		return 2*np.sqrt(2*np.log(10))*self.sig_f #2 x 2.14 sig_f

	@classmethod
	def int_standard_sq(cls, x):
		'''integrates frequency amplitude squared x=f/bw_10'''
		cte=2*np.sqrt(2*np.log(10)) #sig_f=bw10/(2*2.14...)
		return 1/2+1/2*torch.erf(x*cte/np.sqrt(2))

class GammatoneFilter:
	'''
	class for Gammatone filters. Main methods are just a recall of some formulas.
	The model mainly uses the class method 'int_standard_sq'
	'''

	def __init__(self, k, tau, f_c):
		self.k=k
		self.tau=tau
		self.f_c=f_c

	def __call__(self, f):
		omega=2*np.pi*f
		k=self.k
		tau=self.tau
		res_sq=1/comb(2*k-2, k-1)*np.power(2, 2*k-1)*tau*torch.pow( 1+tau**2*omega**2 , -k)
		return torch.sqrt(res_sq)

	def BW10(self):
		return 1/(self.tau*np.pi)*np.sqrt(10**(1/self.k)-1)

	@classmethod
	def int_standard_sq(cls, k, x):
		'''integrates frequency amplitude squared x=f/bw_10'''

		def prim_cos_2k(k, x):
			res=0
			li=[]
			for l in range(k):
				coeff=2*comb(2*k, l)*1/(2*k-2*l)
				res+=coeff*torch.sin((2*k-2*l)*x)
			res+=comb(2*k, k)*x
			res/=2**(2*k)
			return res

		tau=1/(np.pi)*np.sqrt(10**(1/k)-1)  #(*1/bw_10)
		return 0.5+1/comb(2*k-2, k-1)*np.power(2, 2*k-2)/np.pi*prim_cos_2k(k-1, torch.arctan(2*np.pi*tau*x))


import numpy as np

def zscore(x):
	'''
	x m*1
	'''
	mean = np.mean(x)
	std = np.std(x)
	x_ = np.zeros_like(x,dtype=float)
	for i in range(x.shape[0]):
		x_[i] = (x[i] - mean) / std
	return x_

if __name__ == "__main__":
	X = np.array([0, 15, -9, 7, 12, 3, -21])
	print(zscore(X))


"""
Inferring two binomial proportions via grid approximation.
"""
from __future__ import division
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
from mpl_toolkits.mplot3d.axes3d import Axes3D
from scipy.stats import beta
from HDI_of_grid import HDI_of_grid
import numpy as np

# Specify the grid on theta1,theta2 parameter space.
n_int = 500  # arbitrary number of intervals for grid on theta.
theta1 = np.linspace(0, 1, n_int)
theta2 = theta1

theta1_grid, theta2_grid = np.meshgrid(theta1, theta2)

# Specify the prior probability _masses_ on the grid.
prior_name = ("Beta","Ripples","Null","Alt")[0]  # or define your own.
if prior_name == "Beta":
    a1, b1, a2, b2 = 3, 3, 3, 3
    prior1 = beta.pdf(theta1_grid, a1, b1)
    prior2 = beta.pdf(theta2_grid, a1, b1)
    prior = prior1 * prior2
    prior = prior / np.sum(prior)

if prior_name == "Ripples":
    m1, m2, k = 0, 1, 0.75 * np.pi
    prior = np.sin((k*(theta1_grid-m1))**2 + (k*(theta2_grid-m2))**2)**2
    prior = prior / np.sum(prior)

if prior_name == "Null":
    # 1's at theta1=theta2, 0's everywhere else:
    prior = np.eye(len(theta1_grid), len(theta2_grid))
    prior = prior / np.sum(prior)

if prior_name == "Alt":
#    # Uniform:
    prior = np.ones((len(theta1_grid), len(theta2_grid)))
    prior = prior / np.sum(prior)

# Specify likelihood
z1, N1, z2, N2 = 5, 7, 2, 7  # data are specified here
likelihood = theta1_grid**z1 * (1-theta1_grid)**(N1-z1) * theta2_grid**z2 * (1-theta2_grid)**(N2-z2)

# Compute posterior from point-by-point multiplication and normalizing:
p_data = np.sum(prior * likelihood)
posterior = (prior * likelihood) / p_data

# Specify the probability mass for the HDI region
credib = .95
thin = 4
color = 'skyblue'

fig = plt.figure(figsize=(12,12))

# prior
ax = fig.add_subplot(3, 2, 1, projection='3d')
ax.plot_surface(theta1_grid[::thin,::thin], theta2_grid[::thin,::thin], prior[::thin,::thin], color=color)
ax.set_xlabel(r'$\theta1$')
ax.set_ylabel(r'$\theta1$')
ax.set_zlabel(r'$p(t1,t2)$')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])

plt.subplot(3, 2, 2)
plt.contour(theta1_grid, theta2_grid, prior, colors=color)
plt.xlabel(r'$\theta1$')
plt.ylabel(r'$\theta1$')

# likelihood
ax = fig.add_subplot(3, 2, 3, projection='3d')
ax.plot_surface(theta1_grid[::thin,::thin], theta2_grid[::thin,::thin], likelihood[::thin,::thin], color=color)
ax.set_xlabel(r'$\theta1$')
ax.set_ylabel(r'$\theta1$')
ax.set_zlabel(r'$p(D|t1,t2)$')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])

plt.subplot(3, 2, 4)
plt.contour(theta1_grid, theta2_grid, likelihood, colors=color)
plt.xlabel(r'$\theta1$')
plt.ylabel(r'$\theta1$')
plt.plot(0, label='z1,N1,z2,N2=%s,%s,%s,%s' % (z1, N1, z2, N2), alpha=0)
plt.legend(loc='upper left')

# posterior
ax = fig.add_subplot(3, 2, 5, projection='3d')
ax.plot_surface(theta1_grid[::thin,::thin], theta2_grid[::thin,::thin],posterior[::thin,::thin], color=color)
ax.set_xlabel(r'$\theta1$')
ax.set_ylabel(r'$\theta1$')
ax.set_zlabel(r'$p(t1,t2|D)$')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])

plt.subplot(3, 2, 6)
plt.contour(theta1_grid, theta2_grid, posterior, colors=color)
plt.xlabel(r'$\theta1$')
plt.ylabel(r'$\theta1$')
plt.plot(0, label='p(D) = %.3e' % p_data, alpha=0)
plt.legend(loc='upper left')

# Mark the highest posterior density region
HDI_height = HDI_of_grid(posterior)['height']
plt.contour(theta1_grid, theta2_grid, posterior, levels=[HDI_height], colors='k')

plt.tight_layout()
plt.savefig('BernTwoGrid_%s.png' % prior_name)
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Tue Oct  6 23:24:13 2020

@author: ninjaac
"""

"""
Objective
In this challenge, we practice calculating the Pearson correlation coefficient. Check out the Tutorial tab for learning materials!

Task
Given two n-element data sets, X and Y , calculate the value of the Pearson correlation coefficient.
"""
import numpy as np
n=int(input("enter the size"))
X=input().split()
Y=input().split()

X=np.array(X).astype('float64')
Y=np.array(Y).astype('float64')
x_mean=np.mean(X)
x_sd=np.std(X)

y_mean=np.mean(Y)
y_sd=np.std(Y)

mean_sum=0
for i in range(0,int(n)):
    mean_sum+=(X[i]-x_mean)*(Y[i]-y_mean)

coefficient=mean_sum/(n*x_sd*y_sd)
print('%.3f'%coefficient)