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


#! /usr/bin/env python3
# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

import numpy as np
from sklearn.metrics import roc_auc_score

def group_auc(preds, labels):
    group_auc = np.mean(
        [
            roc_auc_score(each_labels, each_preds)
            for each_labels, each_preds in zip(labels, preds)
        ]
    )
    return round(group_auc, 4)

def dcg_score(y_true, y_score, k=10):
    k = min(np.shape(y_true)[-1], k)
    order = np.argsort(y_score)[::-1]
    y_true = np.take(y_true, order[:k])
    gains = 2 ** y_true - 1
    discounts = np.log2(np.arange(len(y_true)) + 2)
    return np.sum(gains / discounts)

def ndcg_score(y_true, y_score, k=10):
    best = dcg_score(y_true, y_true, k)
    actual = dcg_score(y_true, y_score, k)
    return actual / best

def ndcg(preds, labels, k):
    ndcg_temp = np.mean(
        [
            ndcg_score(each_labels, each_preds, k)
            for each_labels, each_preds in zip(labels, preds)
        ]
    )
    return round(ndcg_temp, 4)

def mrr_score(y_true, y_score):
    order = np.argsort(y_score)[::-1]
    y_true = np.take(y_true, order)
    rr_score = y_true / (np.arange(len(y_true)) + 1)
    return np.sum(rr_score) / np.sum(y_true)

def mrr(preds, labels):
    mean_mrr = np.mean(
        [
            mrr_score(each_labels, each_preds)
            for each_labels, each_preds in zip(labels, preds)
        ]
    )
    return round(mean_mrr, 4)


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

# Import helper functions
from mlfromscratch.utils.data_operation import mean_squared_error
from mlfromscratch.utils.data_manipulation import train_test_split, polynomial_features
from mlfromscratch.supervised_learning import BayesianRegression

def main():

    # Load temperature data
    data = pd.read_csv('mlfromscratch/data/TempLinkoping2016.txt', sep="\t")

    time = np.atleast_2d(data["time"].as_matrix()).T
    temp = np.atleast_2d(data["temp"].as_matrix()).T

    X = time # fraction of the year [0, 1]
    y = temp

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

    n_samples, n_features = np.shape(X)

    # Prior parameters
    # - Weights are assumed distr. according to a Normal distribution
    # - The variance of the weights are assumed distributed according to
    #   a scaled inverse chi-squared distribution.
    # High prior uncertainty!
    # Normal
    mu0 = np.array([0] * n_features)
    omega0 = np.diag([.0001] * n_features)
    # Scaled inverse chi-squared
    nu0 = 1
    sigma_sq0 = 100

    # The credible interval
    cred_int = 10

    clf = BayesianRegression(n_draws=2000,
        poly_degree=4,
        mu0=mu0,
        omega0=omega0,
        nu0=nu0,
        sigma_sq0=sigma_sq0,
        cred_int=cred_int)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    mse = mean_squared_error(y_test, y_pred)

    # Get prediction line
    y_pred_, y_lower_, y_upper_ = clf.predict(X=X, eti=True)

    # Print the mean squared error
    print ("Mean Squared Error:", mse)

    # Color map
    cmap = plt.get_cmap('viridis')

    # Plot the results
    m1 = plt.scatter(366 * X_train, y_train, color=cmap(0.9), s=10)
    m2 = plt.scatter(366 * X_test, y_test, color=cmap(0.5), s=10)
    p1 = plt.plot(366 * X, y_pred_, color="black", linewidth=2, label="Prediction")
    p2 = plt.plot(366 * X, y_lower_, color="gray", linewidth=2, label="{0}% Credible Interval".format(cred_int))
    p3 = plt.plot(366 * X, y_upper_, color="gray", linewidth=2)
    plt.axis((0, 366, -20, 25))
    plt.suptitle("Bayesian Regression")
    plt.title("MSE: %.2f" % mse, fontsize=10)
    plt.xlabel('Day')
    plt.ylabel('Temperature in Celcius')
    plt.legend(loc='lower right')
    # plt.legend((m1, m2), ("Training data", "Test data"), loc='lower right')
    plt.legend(loc='lower right')

    plt.show()

if __name__ == "__main__":
    main()


"""
    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 torch
from torch import nn
import numpy as np

seq_len = 20

time_steps = np.linspace(0, np.pi, seq_len + 1) # (21,)
data = np.sin(time_steps)  # (21,)
data.resize((seq_len+1, 1)) # (21, 1)

x = data[:-1] # all but the last piece of data
y = data[1:] # all but the first, y is one step ahead of x

print("x: ", x.shape) #(20, 1) (0,1,2,3,...,19)
print("y: ", y.shape) #(20, 1) (1,2,3,4,...,20)

class RNN(nn.Module):
    def __init__(self, input_size, output_size, hidden_dim, n_layers):
        super(RNN, self).__init__()
        self.hidden_dim = hidden_dim
        self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)
        self.fc = nn.Linear(hidden_dim, output_size)

    def forward(self, x, hidden):
        batch_size = x.size(0)
        r_out, hidden = self.rnn(x, hidden)
        r_out = r_out.view(-1, self.hidden_dim)
        output = self.fc(r_out)
        return output, hidden

# test_rnn = RNN(input_size=1, output_size=1, hidden_dim=10, n_layers=2)

# time_steps = np.linspace(0, np.pi, seq_len)
# data = np.sin(time_steps)
# data.resize((seq_len, 1))

# test_input = torch.Tensor(data).unsqueeze(0)
# print('Input size: ', test_input.size()) # [1, 20, 1]) (batch_size, seq_len, input_size (#sequence))
# test_out, test_h = test_rnn(test_input, None)

# print('Output size: ', test_out.size()) # [20, 1]
# print('Hidden State size: ', test_h.size()) #[2, 1, 10] (#layers, batch_size, hidden_size)

input_size = 1
output_size  = 1
hidden_dim = 32
n_layers = 1

rnn = RNN(input_size, output_size, hidden_dim, n_layers)
print(rnn)

 # This is a regression problem: can we train an RNN to accurately predict the next data point
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.01)

def train(rnn, n_steps, print_every):
    hidden = None
    for batch_i, step in enumerate(range(n_steps)):
        time_steps = np.linspace(step * np.pi, (step+1)*np.pi, seq_len+1)
        data = np.sin(time_steps)
        data.resize((seq_len+1, 1))
        x = data[:-1]
        y = data[1:]
        x_tensor = torch.Tensor(x).unsqueeze(0)
        y_tensor = torch.Tensor(y)

        prediction, hidden = rnn(x_tensor, hidden)

        hidden = hidden.data

        loss = criterion(prediction, y_tensor)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if batch_i % print_every == 0:
            print("Loss: ", loss.item())

    return rnn

n_steps = 75
print_every = 15
trained_rnn = train(rnn, n_steps, print_every)


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


import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.utils import resample

np.random.seed(2018)

n = 40
n_boostraps = 100
maxdegree = 14

# Make data set.
x = np.linspace(-3, 3, n).reshape(-1, 1)
#y = x**8+ np.random.normal(0, 0.1, x.shape)
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
error = np.zeros(maxdegree)
bias = np.zeros(maxdegree)
variance = np.zeros(maxdegree)
polydegree = np.zeros(maxdegree)
# Hold out some test data that is never used in training.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

for degree in range(maxdegree):
    model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
    y_pred = np.empty((y_test.shape[0], n_boostraps))
    for i in range(n_boostraps):
        x_, y_ = resample(x_train, y_train)
        y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()

    polydegree[degree] = degree
    error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
    bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
    variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
    print('Polynomial degree:', degree)
    print('Error:', error[degree])
    print('Bias^2:', bias[degree])
    print('Var:', variance[degree])
    print('{} >= {} + {} = {}'.format(error[degree], bias[degree], variance[degree], bias[degree]+variance[degree]))

#plt.plot(polydegree, np.log10(error), label='Error')
plt.plot(polydegree, bias, label='bias')
plt.plot(polydegree, variance, label='Variance')
plt.legend()
plt.show()


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


# wavelet library

def wavelet(Y,dt,pad=0.,dj=0.25,s0=-1,J1=-1,mother="MORLET",param=-1):
    """
This function is the translation of wavelet.m by Torrence and Compo

import wave_bases from wave_bases.py

The following is the original comment in wavelet.m

#WAVELET  1D Wavelet transform with optional singificance testing
%
%   [WAVE,PERIOD,SCALE,COI] = wavelet(Y,DT,PAD,DJ,S0,J1,MOTHER,PARAM)
%
%   Computes the wavelet transform of the vector Y (length N),
%   with sampling rate DT.
%
%   By default, the Morlet wavelet (k0=6) is used.
%   The wavelet basis is normalized to have total energy=1 at all scales.
%
%
% INPUTS:
%
%    Y = the time series of length N.
%    DT = amount of time between each Y value, i.e. the sampling time.
%
% OUTPUTS:
%
%    WAVE is the WAVELET transform of Y. This is a complex array
%    of dimensions (N,J1+1). FLOAT(WAVE) gives the WAVELET amplitude,
%    ATAN(IMAGINARY(WAVE),FLOAT(WAVE) gives the WAVELET phase.
%    The WAVELET power spectrum is ABS(WAVE)^2.
%    Its units are sigma^2 (the time series variance).
%
%
% OPTIONAL INPUTS:
%
% *** Note *** setting any of the following to -1 will cause the default
%               value to be used.
%
%    PAD = if set to 1 (default is 0), pad time series with enough zeroes to get
%         N up to the next higher power of 2. This prevents wraparound
%         from the end of the time series to the beginning, and also
%         speeds up the FFT's used to do the wavelet transform.
%         This will not eliminate all edge effects (see COI below).
%
%    DJ = the spacing between discrete scales. Default is 0.25.
%         A smaller # will give better scale resolution, but be slower to plot.
%
%    S0 = the smallest scale of the wavelet.  Default is 2*DT.
%
%    J1 = the # of scales minus one. Scales range from S0 up to S0*2^(J1*DJ),
%        to give a total of (J1+1) scales. Default is J1 = (LOG2(N DT/S0))/DJ.
%
%    MOTHER = the mother wavelet function.
%             The choices are 'MORLET', 'PAUL', or 'DOG'
%
%    PARAM = the mother wavelet parameter.
%            For 'MORLET' this is k0 (wavenumber), default is 6.
%            For 'PAUL' this is m (order), default is 4.
%            For 'DOG' this is m (m-th derivative), default is 2.
%
%
% OPTIONAL OUTPUTS:
%
%    PERIOD = the vector of "Fourier" periods (in time units) that corresponds
%           to the SCALEs.
%
%    SCALE = the vector of scale indices, given by S0*2^(j*DJ), j=0...J1
%            where J1+1 is the total # of scales.
%
%    COI = if specified, then return the Cone-of-Influence, which is a vector
%        of N points that contains the maximum period of useful information
%        at that particular time.
%        Periods greater than this are subject to edge effects.
%        This can be used to plot COI lines on a contour plot by doing:
%
%              contour(time,log(period),log(power))
%              plot(time,log(coi),'k')
%
%----------------------------------------------------------------------------
%   Copyright (C) 1995-2004, Christopher Torrence and Gilbert P. Compo
%
%   This software may be used, copied, or redistributed as long as it is not
%   sold and this copyright notice is reproduced on each copy made. This
%   routine is provided as is without any express or implied warranties
%   whatsoever.
%
% Notice: Please acknowledge the use of the above software in any publications:
%    ``Wavelet software was provided by C. Torrence and G. Compo,
%      and is available at URL: http://paos.colorado.edu/research/wavelets/''.
%
% Reference: Torrence, C. and G. P. Compo, 1998: A Practical Guide to
%            Wavelet Analysis. <I>Bull. Amer. Meteor. Soc.</I>, 79, 61-78.
%
% Please send a copy of such publications to either C. Torrence or G. Compo:
%  Dr. Christopher Torrence               Dr. Gilbert P. Compo
%  Research Systems, Inc.                 Climate Diagnostics Center
%  4990 Pearl East Circle                 325 Broadway R/CDC1
%  Boulder, CO 80301, USA                 Boulder, CO 80305-3328, USA
%  E-mail: chris[AT]rsinc[DOT]com         E-mail: compo[AT]colorado[DOT]edu
%----------------------------------------------------------------------------"""
    #modules
    import numpy as np

    #set default
    n1 = len(Y)
    if (s0 == -1): s0=2.*dt
    if (dj == -1): dj = 1./4.
    if (J1 == -1): J1=np.fix((np.log(n1*dt/s0)/np.log(2))/dj)
    if (mother == -1): mother = 'MORLET'
    #print "s0=",s0
    #print "J1=",J1

    #....construct time series to analyze, pad if necessary
    x = Y - np.mean(Y);
    if (pad == 1):
        base2 = np.fix(np.log(n1)/np.log(2) + 0.4999)   # power of 2 nearest to N
        temp=np.zeros((2**(base2+1)-n1,))
        x=np.concatenate((x,temp))

    n = len(x)

    #....construct wavenumber array used in transform [Eqn(5)]
    k = np.arange(1,np.fix(n/2)+1)
    k = k*(2.*np.pi)/(n*dt)
    k = np.concatenate((np.zeros((1,)),k, -k[-2::-1]));

    #....compute FFT of the (padded) time series
    f = np.fft.fft(x)    # [Eqn(3)]

    #....construct SCALE array & empty PERIOD & WAVE arrays
    scale=np.array([s0*2**(i*dj) for i in range(0,int(J1)+1)])
    period = scale.copy()
    wave = np.zeros((int(J1)+1,n),dtype=np.complex)  # define the wavelet array  # make it complex
    # loop through all scales and compute transform
    for a1 in range(0,int(J1)+1):
        daughter,fourier_factor,coi,dofmin=wave_bases(mother,k,scale[a1],param)
        wave[a1,:] = np.fft.ifft(f*daughter)  # wavelet transform[Eqn(4)]
    period = fourier_factor*scale
    coi=coi*dt*np.concatenate(([1.E-5],np.arange(1.,(n1+1.)/2.-1),np.flipud(np.arange(1,n1/2.)),[1.E-5])) # COI [Sec.3g]
    wave = wave[:,:n1]  # get rid of padding before returning
    return wave,period,scale,coi
    # end of code

def wave_bases(mother,k,scale,param):
    """
    This is translation of wave_bases.m by Torrence and Gilbert P. Compo

    The folloing is the original README

%    WAVE_BASES  1D Wavelet functions Morlet, Paul, or DOG
%
%  [DAUGHTER,FOURIER_FACTOR,COI,DOFMIN] = ...
%      wave_bases(MOTHER,K,SCALE,PARAM);
%
%   Computes the wavelet function as a function of Fourier frequency,
%   used for the wavelet transform in Fourier space.
%   (This program is called automatically by WAVELET)
%
% INPUTS:
%
%    MOTHER = a string, equal to 'MORLET' or 'PAUL' or 'DOG'
%    K = a vector, the Fourier frequencies at which to calculate the wavelet
%    SCALE = a number, the wavelet scale
%    PARAM = the nondimensional parameter for the wavelet function
%
% OUTPUTS:
%
%    DAUGHTER = a vector, the wavelet function
%    FOURIER_FACTOR = the ratio of Fourier period to scale
%    COI = a number, the cone-of-influence size at the scale
%    DOFMIN = a number, degrees of freedom for each point in the wavelet power
%             (either 2 for Morlet and Paul, or 1 for the DOG)
%
%----------------------------------------------------------------------------
%   Copyright (C) 1995-1998, Christopher Torrence and Gilbert P. Compo
%   University of Colorado, Program in Atmospheric and Oceanic Sciences.
%   This software may be used, copied, or redistributed as long as it is not
%   sold and this copyright notice is reproduced on each copy made.  This
%   routine is provided as is without any express or implied warranties
%   whatsoever.
%----------------------------------------------------------------------------
    """
    #import modules
    import numpy as np

    #
    mother = mother.upper()
    n = len(k)
    # define Heaviside step function
    def ksign(x):
        y=np.zeros_like(x)
        y[x>0]=1
        return y
    #
    if mother=='MORLET':  #-----------------------------------  Morlet
        if (param == -1): param = 6.
        k0 = param
        expnt = -(scale*k - k0)**2/2. *ksign(k)
        norm = np.sqrt(scale*k[1])*(np.pi**(-0.25))*np.sqrt(n)    # total energy=N   [Eqn(7)]
        daughter = norm*np.exp(expnt)
        daughter = daughter*ksign(k)  # Heaviside step function
        fourier_factor = (4.*np.pi)/(k0 + np.sqrt(2. + k0**2)) # Scale-->Fourier [Sec.3h]
        coi = fourier_factor/np.sqrt(2)            # Cone-of-influence [Sec.3g]
        dofmin = 2.                          # Degrees of freedom
    elif mother=='PAUL': #--------------------------------  Paul
        if (param == -1): param = 4.
        m = param
        expnt = -(scale*k)*ksign(k)
        norm = np.sqrt(scale*k[1])*(2.**m/np.sqrt(m*np.prod(np.arange(2,2*m))))*np.sqrt(n)
        daughter = norm*((scale*k)**m)*np.exp(expnt)
        daughter = daughter*ksign(k)      # Heaviside step function
        fourier_factor = 4*np.pi/(2.*m+1.)
        coi = fourier_factor*np.sqrt(2)
        dofmin = 2.
    elif mother=='DOG':  #--------------------------------  DOG
        if (param == -1): param = 2.
        m = param
        expnt = -(scale*k)**2 / 2.0
        from scipy.special import gamma
        norm = np.sqrt(scale*k[1]/gamma(m+0.5))*np.sqrt(n)
        daughter = -norm*(1j**m)*((scale*k)**m)*np.exp(expnt);
        fourier_factor = 2.*np.pi*np.sqrt(2./(2.*m+1.))
        coi = fourier_factor/np.sqrt(2)
        dofmin = 1.
    else:
        raise Exception("Mother must be one of MORLET,PAUL,DOG")

    return daughter,fourier_factor,coi,dofmin

    # end of code

def wave_signif(Y,dt,scale1,sigtest=-1,lag1=-1,siglvl=-1,dof=-1,mother=-1,param=-1):
    """
This function is the translation of wave_signif.m by Torrence and Compo
use scipy function "chi2" instead of  chisquare_inv

The following is the original comment in wave_signif.m

%WAVE_SIGNIF  Significance testing for the 1D Wavelet transform WAVELET
%
%   [SIGNIF,FFT_THEOR] = ...
%      wave_signif(Y,DT,SCALE,SIGTEST,LAG1,SIGLVL,DOF,MOTHER,PARAM)
%
% INPUTS:
%
%    Y = the time series, or, the VARIANCE of the time series.
%        (If this is a single number, it is assumed to be the variance...)
%    DT = amount of time between each Y value, i.e. the sampling time.
%    SCALE = the vector of scale indices, from previous call to WAVELET.
%
%
% OUTPUTS:
%
%    SIGNIF = significance levels as a function of SCALE
%    FFT_THEOR = output theoretical red-noise spectrum as fn of PERIOD
%
%
% OPTIONAL INPUTS:
% *** Note *** setting any of the following to -1 will cause the default
%               value to be used.
%
%    SIGTEST = 0, 1, or 2.    If omitted, then assume 0.
%
%         If 0 (the default), then just do a regular chi-square test,
%             i.e. Eqn (18) from Torrence & Compo.
%         If 1, then do a "time-average" test, i.e. Eqn (23).
%             In this case, DOF should be set to NA, the number
%             of local wavelet spectra that were averaged together.
%             For the Global Wavelet Spectrum, this would be NA=N,
%             where N is the number of points in your time series.
%         If 2, then do a "scale-average" test, i.e. Eqns (25)-(28).
%             In this case, DOF should be set to a
%             two-element vector [S1,S2], which gives the scale
%             range that was averaged together.
%             e.g. if one scale-averaged scales between 2 and 8,
%             then DOF=[2,8].
%
%    LAG1 = LAG 1 Autocorrelation, used for SIGNIF levels. Default is 0.0
%
%    SIGLVL = significance level to use. Default is 0.95
%
%    DOF = degrees-of-freedom for signif test.
%         IF SIGTEST=0, then (automatically) DOF = 2 (or 1 for MOTHER='DOG')
%         IF SIGTEST=1, then DOF = NA, the number of times averaged together.
%         IF SIGTEST=2, then DOF = [S1,S2], the range of scales averaged.
%
%       Note: IF SIGTEST=1, then DOF can be a vector (same length as SCALEs),
%            in which case NA is assumed to vary with SCALE.
%            This allows one to average different numbers of times
%            together at different scales, or to take into account
%            things like the Cone of Influence.
%            See discussion following Eqn (23) in Torrence & Compo.
%
%
%----------------------------------------------------------------------------
%   Copyright (C) 1995-1998, Christopher Torrence and Gilbert P. Compo
%   University of Colorado, Program in Atmospheric and Oceanic Sciences.
%   This software may be used, copied, or redistributed as long as it is not
%   sold and this copyright notice is reproduced on each copy made.  This
%   routine is provided as is without any express or implied warranties
%   whatsoever.
%----------------------------------------------------------------------------
    """
    from scipy.stats import chi2
    import numpy as np

    try:
        n1=len(Y)
    except:
        n1=1
    J1 = len(scale1) - 1
    scale = scale1
    s0 = np.min(scale)
    dj = np.log(scale[1]/scale[0])/np.log(2.)

    if (n1 == 1):
        variance = Y
    else:
        variance = np.std(Y)**2

    if (sigtest == -1): sigtest = 0
    if (lag1 == -1): lag1 = 0.0
    if (siglvl == -1): siglvl = 0.95
    if (mother == -1): mother = 'MORLET'

    mother = mother.upper()

    # get the appropriate parameters [see Table(2)]
    if (mother=='MORLET'):  #----------------------------------  Morlet
        if (param == -1): param = 6.
        k0 = param
        fourier_factor = (4.*np.pi)/(k0 + np.sqrt(2. + k0**2)) # Scale-->Fourier [Sec.3h]
        empir = [2.,-1,-1,-1]
        if (k0 == 6): empir[1:4]=[0.776,2.32,0.60]
    elif (mother=='PAUL'):  #--------------------------------  Paul
        if (param == -1): param = 4.
        m = param
        fourier_factor = 4.*np.pi/(2.*m+1.)
        empir = [2.,-1,-1,-1]
        if (m == 4): empir[1:4]=[1.132,1.17,1.5]
    elif (mother=='DOG'):  #---------------------------------  DOG
        if (param == -1): param = 2.
        m = param
        fourier_factor = 2.*np.pi*np.sqrt(2./(2.*m+1.))
        empir = [1.,-1,-1,-1]
        if (m == 2): empir[1:4] = [3.541,1.43,1.4]
        if (m == 6): empir[1:4] = [1.966,1.37,0.97]
    else:
        raise Exception("Mother must be one of MORLET,PAUL,DOG")

    period = scale*fourier_factor
    dofmin = empir[0]     # Degrees of freedom with no smoothing
    Cdelta = empir[1]     # reconstruction factor
    gamma_fac = empir[2]  # time-decorrelation factor
    dj0 = empir[3]        # scale-decorrelation factor

    freq = dt / period   # normalized frequency
    fft_theor = (1.-lag1**2) / (1.-2.*lag1*np.cos(freq*2.*np.pi)+lag1**2)  # [Eqn(16)]
    fft_theor = variance*fft_theor  # include time-series variance
    signif = fft_theor
    try:
        test=len(dof)
    except:
        if (dof == -1):
            dof = dofmin
        else:
            pass
    #
    if (sigtest == 0):    # no smoothing, DOF=dofmin [Sec.4]
        dof = dofmin
        chisquare = chi2.ppf(siglvl,dof)/dof
        signif = fft_theor*chisquare   # [Eqn(18)]
    elif (sigtest == 1):  # time-averaged significance
        try:
            test=len(dof)
        except:
            dof=np.zeros((J1+1,))+dof
        truncate = dof < 1
        dof[truncate] = 1.
        dof = dofmin*np.sqrt(1. + (dof*dt/gamma_fac / scale)**2 )   # [Eqn(23)]
        truncate = dof < dofmin
        dof[truncate] = dofmin   # minimum DOF is dofmin
        for a1 in range(J1+1):
            chisquare = chi2.ppf(siglvl,dof[a1])/dof[a1]
            signif[a1] = fft_theor[a1]*chisquare
    elif (sigtest == 2):  # time-averaged significance
        if not (len(dof) == 2):
            raise Exception("DOF must be set to [S1,S2], the range of scale-averages'")
        if (Cdelta == -1):
            raise Exception('Cdelta & dj0 not defined for '+mother+' with param = '+str(param))
        s1 = dof[0];
        s2 = dof[1];
        avg = (scale >= s1) & (scale <= s2)  # scales between S1 & S2
        navg=np.sum(avg)
        if navg==0:
            raise Exception('No valid scales between '+str(s1)+' and '+str(s2))
        Savg = 1./np.sum(1 / scale[avg])    # [Eqn(25)]
        Smid = np.exp((np.log(s1)+np.log(s2))/2.)     # power-of-two midpoint
        dof = (dofmin*navg*Savg/Smid)*np.sqrt(1. + (navg*dj/dj0)**2)  # [Eqn(28)]
        fft_theor = Savg*np.sum(fft_theor[avg] / scale[avg])  # [Eqn(27)]#
        chisquare = chi2.ppf(siglvl,dof)/dof
        signif = (dj*dt/Cdelta/Savg)*fft_theor*chisquare    # [Eqn(26)]
    else:
        raise Exception('sigtest must be either 0, 1, or 2')

    return signif,fft_theor

# end of code

def wavelet_inverse(wave, scale, dt, dj=0.25, mother="MORLET",param=-1):
    """Inverse continuous wavelet transform
    Torrence and Compo (1998), eq. (11)

    INPUTS
        waves (array like):
          WAVE is the WAVELET transform. This is a complex array.

        scale (array like):
           the vector of scale indices
        dt (float) :
            amount of time between each original value, i.e. the sampling time.
        dj (float, optional) :
            the spacing between discrete scales. Default is 0.25.
           A smaller # will give better scale resolution, but be slower to plot.
        mother (string, optional) :
            the mother wavelet function.
             The choices are 'MORLET', 'PAUL', or 'DOG'
         PARAM = the mother wavelet parameter.
            For 'MORLET' this is k0 (wavenumber), default is 6.
            For 'PAUL' this is m (order), default is 4.
            For 'DOG' this is m (m-th derivative), default is 2.

    OUTPUTS
        iwave (array like) :
            Inverse wavelet transform.
    """
    import numpy as np

    j1, n = wave.shape
    J1 = len(scale)
    if not j1 == J1:
        print j1,n,J1
        raise Exception("Input array are inconsistent")
    sj = np.dot(scale.reshape(len(scale),1),np.ones((1,n)))
    #
    mother = mother.upper()

    # psi0 comes from Table 1,2 Torrence and Compo (1998)
    # Cdelta comes from Table 2 Torrence and Compo (1998)
    if mother=='MORLET':  #-----------------------------------  Morlet
        if (param == -1): param = 6.
        psi0=np.pi**(-0.25)
        if param==6.:
            Cdelta = 0.776
    elif mother=='PAUL': #--------------------------------  Paul
        if (param == -1): param = 4.
        m = param
        psi0=np.real(2.**m*1j**m*np.prod(np.arange(2, m + 1))/np.sqrt(np.pi*np.prod(np.arange(2,2*m+1)))*(1**(-(m+1))))
        if m==4.:
           Cdelta = 1.132
    elif mother=='DOG':  #--------------------------------  DOG
        if (param == -1): param = 2.
        m = param
        from scipy.special import gamma
        from numpy.lib.polynomial import polyval
        from scipy.special.orthogonal import hermitenorm
        p = hermitenorm(m)
        psi0=(-1)**(m+1)/np.sqrt(gamma(m+0.5))*polyval(p, 0)
        print psi0
        if m==2.:
            Cdelta=3.541
        if m==6.:
            Cdelta=1.966
    else:
        raise Exception("Mother must be one of MORLET,PAUL,DOG")

    #eq. (11) in Torrence and Compo (1998)
    iwave = dj * np.sqrt(dt) / Cdelta /psi0 * (np.real(wave) / sj**0.5).sum(axis=0)
    return iwave


#ref: Sreenivas Sarwar Anik

# Filters work by convolution with a moving window called a kernel.
#Convolution is nothing but multiplication of two arrays of different sizes.
#The image will be of one size and the kernel with be of a different size,
# #usually much smaller than image
# The input pixel is at the centre of the kernel.
# The convolution is performed by sliding the kernel over the image,
# $usually from top left of image.
# Linear filters and non-linear filters.
# Gaussian is an example of linear filter.
#Non-linear filters preserve edges.
#Median filter is an example of non-linear filter.
#The algorithm selects the median value of all the pixels in the selected window
#NLM: https://scikit-image.org/docs/dev/auto_examples/filters/plot_nonlocal_means.html

### What is gaussian Kernel #####
import numpy
from matplotlib import pyplot as plt

def gaussian_kernel(size, size_y=None):
    size = int(size)
    if not size_y:
        size_y = size
    else:
        size_y = int(size_y)
    x, y = numpy.mgrid[-size:size+1, -size_y:size_y+1]
    g = numpy.exp(-(x**2/float(size)+y**2/float(size_y)))
    return g / g.sum()

gaussian_kernel_array = gaussian_kernel(3)
print(gaussian_kernel_array)
plt.imshow(gaussian_kernel_array, cmap=plt.get_cmap('jet'), interpolation='nearest')
plt.colorbar()
plt.show()

############################ Denoising filters ###############
from skimage.restoration import denoise_nl_means, estimate_sigma
from skimage import img_as_ubyte, img_as_float
from matplotlib import pyplot as plt
from skimage import io
import numpy as np

img = img_as_float(io.imread("images/denoising/noisy_img.jpg"))
#Need to convert to float as we will be doing math on the array

from scipy import ndimage as nd
gaussian_img = nd.gaussian_filter(img, sigma=3)
plt.imsave("images/gaussian.jpg", gaussian_img)

median_img = nd.median_filter(img, size=3)
plt.imsave("images/median.jpg", median_img)

gaussian_img = nd.gaussian_filter(img, sigma=3)
plt.imsave("images/gaussian.jpg", gaussian_img)

##### NLM#####

sigma_est = np.mean(estimate_sigma(img, multichannel=True))

patch_kw = dict(patch_size=5,
                patch_distance=3,
                multichannel=True)

denoise_img = denoise_nl_means(img, h=1.15 * sigma_est, fast_mode=False,
                               patch_size=5, patch_distance=3, multichannel=True)
"""
denoise_img = denoise_nl_means(img, h=1.15 * sigma_est, fast_mode=False,
                           **patch_kw)
"""
denoise_img_as_8byte = img_as_ubyte(denoise_img)

plt.imshow(denoise_img)
#plt.imshow(denoise_img_as_8byte, cmap=plt.cm.gray, interpolation='nearest')
plt.imsave("images/NLM.jpg",denoise_img)


import numpy as np
import taylor as ta

def f(x):
    # vector function depending nonlinearly on input vector x
    if (x.shape[0] != 3):
        print(f'ERROR: Jacobian example requires vector of length 3')
    f    = np.empty(4)
    f[0] = x[0]*np.power(x[2],2)
    f[1] = np.sin(x[1]*x[2])
    f[2] = np.exp(np.pi*x[0]) + 10
    f[3] = np.sum(x)
    return f

def J_f_exact(x):
    # computes the exact Jacobian of f(x)
    J = np.empty((4,3))
    # set Jacobian row by row
    J[0,:] = np.array([np.power(x[2],2), 0.0, 2*x[0]*x[2]])
    J[1,:] = np.array([0.0, np.cos(x[1]*x[2])*x[2],
                       np.cos(x[1]*x[2])*x[1]])
    J[2,:] = np.array([np.exp(np.pi*x[0])*np.pi,0.0,0.0])
    J[3,:] = np.array([1.0,1.0,1.0])
    return J

if (__name__ == "__main__"):
    print()
    print(f'--------------------- JACOBIAN TEST ---------------------')
    print(f'    f(x) = [ x_0*(x_2)^2,')
    print(f'             sin(x_1*x_2),')
    print(f'             exp(pi*x_0) + 10,')
    print(f'             x_1 + x_2 + x_3 ]\n')

    # set current point x and compute exact Jacobian at point
    x       = np.array([1.5,20,2.718])
    J_exact = J_f_exact(x)

    # print x and exact Jacobian at x
    print(f'x : {x}\n')
    print(f'J_f(x) (exact) :\n{J_exact}\n')

    # compute Jacobian of nonlinear f with respect to x numerically
    deriv_nonlinear_vec = ta.diff(f,x,1)
    print(f'J_f(x) (approximate) :\n{deriv_nonlinear_vec}\n')


import numpy as np
import math

# -------------------------------------------------------------------------
'''
    Problem 0: Graph Clustering (Spectral Clustering)
    In this problem, you will implement a version of the spectral clustering method to cluster the nodes in a graph into two groups.
    You could test the correctness of your code by typing `nosetests -v test0.py` in the terminal.

    Notations:
            ---------- input data ------------------------
            n: the number of nodes in the network, an integer scalar.
            A:  the adjacency matrix, a float numpy matrix of shape n by n.
                If there is a link between node i an node j, then A[i][j] = A[j][i] = 1.
            ---------- computed data ------------------------
            D:  the degree matrix, a numpy float matrix of shape n by n.
                All off-diagonal elements are 0. Each diagonal element represents the degree of the node (number of links).
            L:  the Laplacian matrix, a numpy float matrix of shape n by n.
                L = D-A
            E:  the eigen vectors of matrix L, a numpy float matrix of shape n by n. Each column of E corresponds to an eigen vector of matrix L.
            v:  the eigen values of matrix L, a numpy float array of length n. Each element corresponds to an eigen value of matrix L. E and v are paired.
            e2:  the eigen vector corresponding to the smallest non-zero eigen value, a numpy float matrix of shape (n by 1).
            tol: the tolerance threshold for eigen values, a float scalar. A very small positive value. If an eigen value is smaller than tol, then we consider the eigen value as being 0.
            x:  the binary vector of shape (n by 1), a numpy float vector of (0/1) values.
                It indicates a binary partition on the graph, such as [1.,1.,1., 0.,0.,0.].
            --------------------------------------------------
'''

# --------------------------
def compute_D(A):
    '''
        Compute the degree matrix D.
        Input:
            A:  the adjacency matrix, a float numpy matrix of shape n by n. Here n is the number of nodes in the network.
                If there is a link between node i an node j, then A[i][j] = A[j][i] = 1.
        Output:
            D:  the degree matrix, a numpy float matrix of shape n by n.
                All off-diagonal elements are 0. Each diagonal element represents the degree of the node (number of links).
    '''

    #########################################
    ## INSERT YOUR CODE HERE
    D = np.zeros(A.shape)
    # degree vector of the nodes

    for i in range(A.shape[0]):
        for j in range(A.shape[1]):
            if A[i, j] == 1:
                D[i, i] += 1

    # diagonal matrix
    D = np.asmatrix(D)
    #########################################
    return D

# --------------------------
def compute_L(D, A):
    '''
        Compute the Laplacian matrix L.
        Input:
            D:  the degree matrix, a numpy float matrix of shape n by n.
                All off-diagonal elements are 0. Each diagonal element represents the degree of the node (number of links).
            A:  the adjacency matrix, a float numpy matrix of shape n by n. Here n is the number of nodes in the network.
                If there is a link between node i an node j, then A[i][j] = A[j][i] = 1.
        Output:
            L:  the Laplacian matrix, a numpy float matrix of shape n by n.
    '''

    #########################################
    ## INSERT YOUR CODE HERE
    L = D - A
    #########################################
    return L

# --------------------------
def compute_eigen_pairs(L):
    '''
        Compute the eigen vectors and eigen values of L.
        Input:
            L:  the Laplacian matrix, a numpy float matrix of shape n by n.
        Output:
            E:  the eigen vectors of matrix L, a numpy float matrix of shape p by p. Each column of E corresponds to an eigen vector of matrix L.
            v:  the eigen values of matrix L, a numpy float array of length p. Each element corresponds to an eigen value of matrix L. E and v are paired.
        Hint: you could use np.linalg.eig() to compute the eigen vectors of a matrix.
        Note: in the returned eigen vectors "eig_vecs", each column (not row) is considered as an eigen vector.
    '''

    #########################################
    ## INSERT YOUR CODE HERE
    v, E = np.linalg.eig(L)
    #########################################
    return E, v

# --------------------------
def find_e2(E, v, tol=1e-4):
    '''
        find the eigen vector that corresponds to the smallest non-zeros eigen values.
        For example, if the eigen values are [0.5, 0.000001, 2.], the first eigen vector is the answer.
        For example, if the eigen values are [2., 0.000001, 0.3], the last eigen vector is the answer.
        For example, if the eigen values are [0.00003, 0.000001, 0.3], the last eigen vector is the answer.
        Input:
            E:  the eigen vectors of matrix L, a numpy float matrix of shape n by n. Each column of E corresponds to an eigen vector of matrix L.
            v:  the eigen values of matrix L, a numpy float array of length n. Each element corresponds to an eigen value of matrix L. E and v are paired.
            tol: the tolerance threshold for eigen values, a float scalar. A very small positive value. If an eigen value is smaller than tol, then we consider the eigen value as being 0.
        Output:
            e2:  the eigen vector corresponding to the smallest non-zero eigen value, a numpy float matrix of shape (n by 1).
    '''

    #########################################
    ## INSERT YOUR CODE HERE
    i = 0
    e2 = np.asmatrix(np.zeros(E.shape[0]))
    # sort the eigen values
    idx = np.argsort(v)
    while v[idx[i]] < tol:
        i += 1

    e2 = E[:, idx[i]]

    #########################################
    return e2

# --------------------------
def compute_x(e2):
    '''
        Compute the partition on the graph from the thresholding an eigen vector with 0 threshold.
        Input:
            e2:  the eigen vector corresponding to the smallest non-zero eigen value, a numpy float matrix of shape (n by 1).
        Output:
            x:  the binary vector of shape (n by 1), a numpy float vector of (0/1) values.
                It indicates a binary partition on the graph, such as [1.,1.,1., 0.,0.,0.].
    '''

    #########################################
    ## INSERT YOUR CODE HERE
    x = e2
    threshold = 0
    x[x < threshold] = 0
    x[x > threshold] = 1
    #########################################
    return x

# --------------------------
def spectral_clustering(A):
    '''
        Spectral clustering of a graph.
        Input:
            A:  the adjacency matrix, a float numpy matrix of shape n by n. Here n is the number of nodes in the network.
                If there is a link between node i an node j, then A[i][j] = A[j][i] = 1.
        Output:
            x:  the binary vector of shape (n by 1), a numpy float vector of (0/1) values.
                It indicates a binary partition on the graph, such as [1.,1.,1., 0.,0.,0.].
        Note: you cannot use any existing python package for spectral clustering, such as scikit-learn.
        Hint: x is related to the eigen vector of L with the smallest positive eigen values.
              For example, if the eigen vector is [0.2,-0.1, -0.2], the values larger than zero will be 1, so x=[1,0,0] in this example.
              Note: if the eigen value is small enough (say smaller than 0.000001), we can treat it as being zero.
    (5 points)
    '''

    #########################################
    ## INSERT YOUR CODE HERE

    # compute degree matrix
    D = compute_D(A)
    # compute laplacian matrix
    L = compute_L(D, A)
    # compute eigen pairs of L
    E, v = compute_eigen_pairs(L)
    # find the eigen vector with the smallest non-zero eigen value
    e2 = find_e2(E, v, tol=1e-6)
    # compute the graph partition
    x = compute_x(e2)
    #########################################
    return x


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

from sklearn.datasets import make_blobs
from sklearn.mixture import GaussianMixture

# Set random seed for reproducibility
np.random.seed(1000)

nb_samples = 1000

if __name__ == '__main__':
    # Create the dataset
    X, Y = make_blobs(n_samples=nb_samples,
                      n_features=2,
                      centers=3, cluster_std=1.5,
                      random_state=1000)

    # Show the unclustered dataset
    sns.set()

    fig, ax = plt.subplots(figsize=(15, 8))

    ax.scatter(X[:, 0], X[:, 1], s=40)
    ax.grid(True)
    ax.set_xlabel(r'$x_0$', fontsize=22)
    ax.set_ylabel(r'$x_1$', fontsize=22)

    plt.show()

    # Find the optimal number of components
    nb_components = [2, 3, 4, 5, 6, 7, 8]

    aics = []
    bics = []

    for n in nb_components:
        gm = GaussianMixture(n_components=n,
                             max_iter=1000,
                             random_state=1000)
        gm.fit(X)
        aics.append(gm.aic(X))
        bics.append(gm.bic(X))

    fig, ax = plt.subplots(2, 1, figsize=(15, 8))

    ax[0].plot(nb_components, aics)
    ax[0].set_ylabel('AIC', fontsize=22)
    ax[0].grid(True)

    ax[1].plot(nb_components, bics)
    ax[1].set_xlabel('Number of components', fontsize=22)
    ax[1].set_ylabel('BIC', fontsize=22)
    ax[1].grid(True)

    plt.show()

    # Create and fit a Gaussian Mixture
    gm = GaussianMixture(n_components=3)
    gm.fit(X)

    # Show the Gaussian parameters
    print('Weights:')
    print(gm.weights_)

    print('\nMeans:')
    print(gm.means_)

    print('\nCovariances:')
    print(gm.covariances_)

    # Show the clustered dataset
    Yp = gm.predict(X)

    fig, ax = plt.subplots(figsize=(15, 8))

    ax.scatter(X[Yp == 0, 0], X[Yp == 0, 1], c='red', marker='o', s=80, label="Gaussian 1")
    ax.scatter(X[Yp == 1, 0], X[Yp == 1, 1], c='blue', marker='x', s=80, label="Gaussian 2")
    ax.scatter(X[Yp == 2, 0], X[Yp == 2, 1], c='green', marker='d', s=80, label="Gaussian 3")
    ax.grid(True)
    ax.set_xlabel(r'$x_0$', fontsize=22)
    ax.set_ylabel(r'$x_1$', fontsize=22)
    ax.legend(fontsize=22)

    plt.show()


from sklearn.metrics import *
import numpy as np

EPSILON = 1e-10

def MeanAbsoErr(y_true, y_pred):
    """
    Mean absolute error regression loss. smaller is better.

    interpretation: smaller is better.(Best value is 0.0)
    - MAE value is between 0 to inf.
    - MAE treats larger and small errors equally. Not sensitive to outliers in the data.
    - MAE is Robust to outliers compared to RMSE.
    -

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean absolute error
    """
    return mean_absolute_error(y_true, y_pred)

def MeanSqrtErr(y_true, y_pred):
    """
    Mean squared error regression loss.

    interpretation: smaller is better.(Best value is 0.0)
    - MSE = 0.0, indicates that model is perfectly fitting the data.
    - MSE = inf, indicates that model is overfitting the data.
    - MSE = NaN, indicates that model is not fitting the data.
    - MSE value is between 0 to inf.
    - MSE treats Sensitive to outliers, punishes larger error more.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean squared error
    """
    return mean_squared_error(y_true, y_pred)

def RootMeanSqrtErr(y_true, y_pred):
    """
    Root Mean Square Error regression loss.

    interpretation: smaller is better.(Best value is 0.0)
    - RMSE = 0.0, indicates that model is perfectly fitting the data.
    - RMSE = inf, indicates that model is overfitting the data.
    - RMSE = NaN, indicates that model is not fitting the data.
    - RMSE value is between 0 to inf.
    - RMSE treats Sensitive to outliers, punishes larger error more.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean squared error
    """
    return np.sqrt(mean_squared_error(y_true, y_pred))

def RootMeanSqrtLogErr(y_true, y_pred):
    """
    Root Mean Squared Logarithm Error regression loss.

    interpretation: smaller is better.(Best value is 0.0)
    - RMSLE = 0.0, indicates that model is perfectly fitting the data.
    - RMSLE = inf, indicates that model is overfitting the data.
    - RMSLE = NaN, indicates that model is not fitting the data.
    - RMSLE is usually used when you don't want to penalize the large differences in the predicted and the actual values when the predicted
    and the actual values are big numbers.
    - RMSLE is used when y has long tail distribution, or we are interested in the ratio of true value and predicted value.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: root mean squared logarithm error
    """
    for i in range(len(y_true)):
        if y_true[i] < 0 or y_pred[i] < 0:
            continue

    return np.sqrt(mean_squared_log_error(y_true, y_pred))

def RootMeanSqrtLogErrNeg(y_true, y_pred):
    """
    Root Mean Squared Logarithmic Error with negative values regression loss.

    interpretation: smaller is better.(Best value is 0.0)

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: root mean squared logarithm error
    """
    sum=0.0
    for x in range(len(y_pred)):
        if y_pred[x]<0 or y_true[x]<0: #check for negative values
            continue
        p = np.log(y_pred[x]+1)
        r = np.log(y_true[x]+1)
        sum = sum + (p - r)**2

    RMSLE = (sum/len(y_pred))**0.5

    return RMSLE

def R2CoefScore(y_true, y_pred):
    """
    :math:`R^2` (coefficient of determination) regression score function.

    interpretation: Best possible score is 1.0(perfect prediction), lower values are worse.
    - 0.0 <= r2<= 1.0 is standard value but worst cases are -1.0 and -2.0 or any negative values.
    - r2 > 0.9 is good.
    - 0.7 < r2 < 0.9 indicates high level of correlation.
    - 0.4 < r2 < 0.7 indicates medium level of correlation.
    - r2 < 0.4 indicates low level of correlation.
    -
    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: R2
    """
    return r2_score(y_true, y_pred)

def AdjR2CoefScore(y_true, y_pred):
    """
    Adjusted R2 regression score function.(Best value is 1.0)

    interpretation: Best possible score is 1.0, lower values are worse.
    - adj_r2 < 1.0 indicates that at least some variability in the data is explained by the model.
    - adj_r2 =0.5 indicates that the variability in the outcome data cannot be explained by the model.
    - adj_r2 < 0.0 indicates that model fitted wrong.
    - Adjusted R-squared to compare the goodness-of-fit for regression models that contain differing numbers of independent variables.
    - Adjusted R2 is the better model when you compare models that have a different amount of variables.
    -

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: adjusted R2
    """
    return r2_score(y_true, y_pred) - ((1 - r2_score(y_true, y_pred)) * (len(y_true) - 1) / (len(y_true) - len(y_pred) - 1))

def MeanAbsPercErr(y_true, y_pred):
    """
    Mean absolute percentage error regression loss.

    interpretation: smaller is better.(Best value is 0.0)
    - MAPE < 10 % Very Good
    - 10 % < MAPE < 20% Good
    - 20 % < MAPE < 50% Average
    - MAPE > 50% Not Good
    - MAPE widely used measure for checking forecast accuracy.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean absolute percentage error
    """
    return mean_absolute_percentage_error(y_true, y_pred)

def MeanSqrtLogErr(y_true, y_pred):
    """
    Mean squared logarithmic error regression loss.

    interpretation: smaller is better.(Best value is 0.0)
    - MSLE = 0.0, indicates that model is perfectly fitting the data.
    - MSLE = inf, indicates that model is overfitting the data.
    - MSLE used when target, conditioned on the input, is normally distributed, and
    you don’t want large errors to be significantly more penalized than small ones,
    in those cases where the range of the target value is large.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean squared logarithmic error
    """
    return mean_squared_log_error(y_true, y_pred)

def SymMeanAbsPercErr(y_true: np.ndarray, y_pred: np.ndarray):
    """
    :math:`SMAPE` Symmetric mean absolute percentage error regression loss.

    interpretation: smaller is better.(Best value is 0.0)

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: SMAPE
    """
    return np.mean(2.0 * np.abs(y_true - y_pred) / ((np.abs(y_true) + np.abs(y_pred)) + EPSILON))

def NormRootMeanSqrtErr(y_true, y_pred, type = "minmax"):
    """
    Normalized Root Mean Square Error.

    interpretation: smaller is better.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples
        type (str): type of normalization. default is "minmax"
        - sd (standard deviation) : it divides the value by the standard deviation of the data.
        - mean (mean) : it divides the value by the mean of the data.
        - minmax (min-max) : it divides the value by the range of the data.
        - iqr (interquartile range) : it divides the value by the interquartile range of the data.

    Returns:
        [float]: normalized root mean square error
    """
    if type=="sd":
        return np.sqrt(mean_squared_error(y_true, y_pred))/np.std(y_true)
    elif type=="mean":
        return np.sqrt(mean_squared_error(y_true, y_pred))/np.mean(y_true)
    elif type=="minmax":
        return np.sqrt(mean_squared_error(y_true, y_pred))/(np.max(y_true) - np.min(y_true))
    elif type=="iqr":
        return np.sqrt(mean_squared_error(y_true, y_pred))/(np.quantile(y_true, 0.75) - np.quantile(y_true, 0.25))
    elif type!="":
        raise ValueError("type must be either 'sd', 'mean', 'minmax', or 'iqr'")

def NormRootMeanSqrtLogErr(y_true, y_pred):
    """
    Normalized Root Mean Squared Logarithmic Error.

    interrpretation: smaller is better.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples
        type (str): type of normalization. default is "minmax"
        - sd (standard deviation) : it divides the value by the standard deviation of the data.
        - mean (mean) : it divides the value by the mean of the data.
        - minmax (min-max) : it divides the value by the range of the data.
        - iqr (interquartile range) : it divides the value by the interquartile range of the data.

    Returns:
        [float]: normalized root mean squared logarithm error
    """
    if type=="sd":
            return np.sqrt(mean_squared_log_error(y_true, y_pred))/np.std(y_true)
    elif type=="mean":
        return np.sqrt(mean_squared_log_error(y_true, y_pred))/np.mean(y_true)
    elif type=="minmax":
        return np.sqrt(mean_squared_log_error(y_true, y_pred))/(np.max(y_true) - np.min(y_true))
    elif type=="iqr":
        return np.sqrt(mean_squared_log_error(y_true, y_pred))/(np.quantile(y_true, 0.75) - np.quantile(y_true, 0.25))
    elif type!="":
        raise ValueError("type must be either 'sd', 'mean', 'minmax', or 'iqr'")

def MedianAbsErr(y_true, y_pred):
    """
    Median absolute error regression loss. (Best value is 0.0)

    interpretation: smaller is better.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: median absolute error
    """
    return median_absolute_error(y_true, y_pred)

def MediaRelErr(y_true, y_pred):
    """
    Mean relative error regression loss.

    interpretation: smaller is better.(Best value is 0.0)

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean relative error
    """
    return np.mean(np.abs(y_true - y_pred) / (y_true + EPSILON))

def MeanArcAbsPercErr(y_true, y_pred):
    """
    Mean Arctangent Absolute Percentage Error regression loss.

    interpretation: smaller is better.(Best value is 0.0)

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean arctangent absolute percentage error
    """
    return np.mean(np.abs(np.arctan(y_true) - np.arctan(y_pred)))

def MeanAbsScaErr(y_true, y_pred, y_train):
    """
    Mean absolute scale error regression loss.

    Reference : R.J. Hyndman, A.B. Koehler, Another look at measures of forecast accuracy, International Journal of Forecasting, 22 (2006), pp. 679-688

    interpretation: smaller is better.(Best value is 0.0)
    - MASE = 0.5 means model has doubled the prediction accurracy.
    - MASE > 1.0 means model has overpredicted.
    - MASE < 1.0 means model has underpredicted.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: mean absolute scale error
    """
    e_t = np.mean(np.abs(y_true - y_train))
    scale = mean_absolute_error(y_true[1:] - y_pred[:-1])
    return np.mean(np.abs(e_t/ scale))

def NashSutCoeff(y_true, y_pred):
    """
    The Nash-Sutcliffe efficiency (NSE) is a normalized statistic that determines the relative magnitude of the residual variance compared to the measured data variance (Nash and Sutcliffe, 1970).

    Reference: McCuen, R.H., Knight, Z. and Cutter, A.G., 2006. Evaluation of the Nash–Sutcliffe efficiency index. Journal of hydrologic engineering, 11(6), pp.597-602.

    interpretation: Larger is better.(Best possible score is 1.0, lower values are worse.)
    - NSE = 1, corresponds to a perfect match of the model to the observed data.
    - NSE = 0, indicates that the model predictions are as accurate as the mean of the observed data.
    - Inf < NSE < 0, indicates that the observed mean is a better predictor than the model.

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: Nash-Sutcliffe efficiency coefficient
    """
    return 1 - ((np.sum((y_true - y_pred)**2)) / (np.sum((y_true - np.mean(y_true))**2)))

def WillMottIndexAgreeMent(y_true, y_pred):
    """
    Willmott (1981) proposed an index of agreement (d) as a standardized measure of the degree of model prediction error which varies between 0 and 1.

    References : Willmott, C.J., 1981. On the validation of models. Physical geography, 2(2), pp.184-194.

    interpretation: Larger is better (Best value is 1.0)
    - d = 1, indicates that the model prediction is perfect match.
    - d = 0, indicates that no agreement at all

    Args:
        y_true ([np.array]): test samples
        y_pred ([np.array]): predicted samples

    Returns:
        [float]: Willmott index of agreement
    """
    residuals = y_true - y_pred
    abs_diff_pred = np.abs(y_pred - np.nanmean(y_true))
    abs_diff_obs  = np.abs(y_true  - np.nanmean(y_true))
    return 1 - np.nansum(residuals**2) / np.nansum((abs_diff_pred * abs_diff_obs)**2)


import numpy as np
from sklearn.utils import check_random_state
from scipy.linalg import block_diag

from fclsp.reshaping_utils import fill_hollow_sym

def complete_graph(n_nodes=100, edges='ones', pos=True, random_state=None):
    """

    Parameters
    ----------
    n_nodes: int

    edges: str
        How the edges are set. Must be one of ['ones', 'normal', 'uniform'].
        'ones': edges are symmetric Bernoulli random variables. If pos=True, then all edges are one.

        'normal': edges are N(0, 1) or |N(0, 1)| depending on pos

        'uniform': edges U(-1, 1) or U(0, 1) depending on pos

    pos: bool
        Make edges positive.

    random_state: None, int

    Output
    ------
    A: array-like, (n_nodes, n_nodes)
    """
    rng = check_random_state(random_state)

    assert edges in ['ones', 'normal', 'uniform']

    n_edges = int(n_nodes * (n_nodes - 1) / 2)

    if edges == 'ones':
        if pos:
            a = np.ones(n_edges)
        else:
            a = rng.choice([-1, 1], size=n_edges, p=[.5, .5])

    elif edges == 'normal':
        a = rng.normal(size=n_edges)

        if pos:
            a = np.abs(a)

    elif edges == 'uniform':
        a = rng.uniform(low=-1, high=1, size=n_edges)

        if pos:
            a = np.abs(a)

    A = fill_hollow_sym(a)

    return A

def block_diag_complete(block_sizes=[10, 20, 30],
                        edges='ones', pos=True, random_state=None):

    rng = check_random_state(random_state)

    return block_diag(*[complete_graph(n_nodes=s, edges=edges,
                                       pos=pos, random_state=rng)
                        for s in block_sizes])


"""
Lattice calcs

Authors/Modifications:
-----------------------
* Tom Trainor (tptrainor@alaska.edu)
* convert matlab to python, Kunal Tanwar
* Convert classes, TPT

Notes on Convention and Generalized Transforms
----------------------------------------------
The set of basis vectors that defines the real lattice are
covarient quantities, while the set of indicies that define
real space vectors are contravarient:
                 |a|
    v = (x,y,z)* |b| = x*a + y*b + z*c
                 |c|
If A is an arbitrary matrix that transforms the contravarient
vector indicies, its assumed that the indicies left multiply
    (x',y',z') = (x,y,z) * A

Note that the usual convention is to express covarient quantities
as column vectors and contravarient quantities as row vectors
(subscripts and superscripts respectivley in tensor notation)
However, in the notes below we'll just take 1-D arrays to be
column vectors to simplify the notation.  Therefore, if A is defined
as above, the matrix vector multiplications will need to be changed to:
    |x'|                 |x|
    |y'| = transpose(A)* |y|
    |z'|                 |z|
We can express the same in numpy given a vector v and matrix A
    dot(v,A) = dot(transpose(A),v)
where v is multiplied as a row vector on the lhs and a column
vector on the rhs; ie the transpose of v bewteen column and
row vecor is implied by its position.

Assume there is a matrix that transforms the covarient basis
(via rotation and/or dilation) to a new set of basis vectors:
    |a'|      |a|        a' = (F11*a + F12*b + F13*c)
    |b'| = F* |b|   or   b' = (F21*a + F22*b + F23*c)
    |c'|      |c|        c' = (F31*a + F32*b + F33*c)
with a well defined inverse:
    |a|           |a'|
    |b| = inv(F)* |b'|
    |c|           |c'|

The covarient/contravarient relationship bewteen basis vectors
and the vector indicies implies that the indicies of a (stationary)
vector in the new (') basis are obtained via the inverse of the
F matrix:
    |x'|      |x|
    |y'| = M* |y|
    |z'|      |z|
where
    M = transpose(inv(F)) = inv(transpose(F))

The reciprocal relation is:
    |x|      |x'|
    |y| = N* |y'|
    |z|      |z'|
where
    N = inv(M) = transpose(F)

With respect to the real lattice the recip lattice basis vectors
are contravarient and the vector indicies are covarient:
                     |h|
    vr = (ar,br,cr)* |k| = h*ar + k*br + l*cr
                     |l|

Given the generalized basis transform F above, the covarient recip
lattice indicies transform in the same way as the lattice.
Therefore:
     |h'|      |h|
     |k'| = F* |k|
     |l'|      |l|

The reciprocal relation is:
    |h|      |k'|
    |k| = G* |k'|
    |l|      |l'|
where
    G = inv(F) = transpose(M)

The metric tensor
------------------
The metric tensor is defined in terms of the vector dot product
for a general real space system
  dot(u,v) = u*g*v
where
       |a*a, a*b, a*c|
  g =  |b*a, b*b, b*c|
       |c*a, c*b, c*c|

Therefore, (with a,b,c, correspoding to vectors magnitudes):

g = [ [ a*a, a*b*cosd(gam), a*c*cosd(bet) ],
      [ b*a*cosd(gam), b*b, b*c*cosd(alp) ],
      [ c*a*cosd(bet), c*b*cosd(alp), c*c ] ]

The metric tensor may also be defined in terms of a lattice transform
ie the real space basis vectors can be expressed in terms of the
recip space basis vectors via
      |a|      |ar|
      |b| = g* |br|
      |c|      |cr|
therefore
      |ar|       |a|
      |br| = gr* |b|
      |cr|       |c|
where gr = inv(g).  Hence the recip lattice vectors
can be plotted in the real lattice via
      ar = gr11*a + gr12*b + gr13*c
      br = gr21*a + gr22*b + gr23*c
      cr = gr31*a + gr32*b + gr33*c
and visa versa

Using the above relations for lattice transforms the
indicies of real lattice vectors can be calc in the recip
lattice and visa-versa via:
   |h|                             |x|      |x|
   |k| = (x,y,z)*g = transpose(g)* |y| = M* |y|
   |l|                             |z|      |z|
and
   |x|                      |h|      |h|
   |y| = inv(transpose(g))* |k| = N* |k|
   |z|                      |l|      |l|

e.g. in above we take recip lattice as primed basis and
real lattice as the unprimed basis, therefore equate:
  F = gr
  G = inv(F) = inv(gr) = g
  M = transpose(inv(F)) = transpose(g)
  N = inv(M) = transpose(F) = inv(transpose(g)) = transpose(gr)

Note that g and gr are always symmetric, therefore
  g = transpose(g) and gr = transpose(gr).
  F = N = gr
  G = M = g

"""
##########################################################################

import numpy as num
from mathutil import cosd, sind, tand
from mathutil import arccosd, arcsind, arctand

##########################################################################
class Lattice:
    """
    Class that defines a lattice and various
    operations within the lattice

    """
    def __init__(self,a=10.,b=10.,c=10.,alpha=90.,beta=90.,gamma=90.,lam=1.5406):
        """
        Initialize

        Parameters:
        -----------
        * a,b,c in angstroms
        * alpha, beta, gamma in degrees,
        * lambda in angstroms (default lambda is Cu Ka1)
        """
        self.update(a=a,b=b,c=c,alpha=alpha,beta=beta,gamma=gamma,lam=lam)

    def __repr__(self,):
        """ display """
        lout = "a=%6.5f, b=%6.5f, c=%6.5f" % (self.a, self.b, self.c)
        lout = "%s, alpha=%6.5f,beta=%6.5f,gamma=%6.5f\n" % (lout,
                                                             self.alpha,
                                                             self.beta,
                                                             self.gamma)
        lout = "%sar=%6.5f, br=%6.5f, cr=%6.5f" % (lout,self.ar, self.br, self.cr)
        lout = "%s, alphar=%6.5f,betar=%6.5f,gammar=%6.5f\n" % (lout,
                                                                self.alphar,
                                                                self.betar,
                                                                self.gammar)
        lout = "%sDefault wavelength for angle calculations=%6.5f\n" % (lout,
                                                                      self.lam)
        return lout

    def update(self,a=None,b=None,c=None,alpha=None,beta=None,gamma=None,lam=None):
        """
        Update lattice parameters
        """
        if a != None:     self.a = float(a)
        if b != None:     self.b = float(b)
        if c != None:     self.c = float(c)
        if alpha != None: self.alpha = float(alpha)
        if beta  != None: self.beta  = float(beta)
        if gamma != None: self.gamma = float(gamma)
        if lam != None:   self.lam = float(lam)
        # update calc quantities
        self._calc_g()

    def cell(self):
        """
        Return array of real lattice cell parameters
        """
        return num.array([self.a,self.b,self.c,
                          self.alpha,self.beta,self.gamma],dtype=float)

    def rcell(self):
        """
        Return array of reciprocal lattice cell parameters
        """
        return num.array([self.ar,self.br,self.cr,
                          self.alphar,self.betar,self.gammar],dtype=float)

    def _calc_g(self):
        """
        Calculate the metric tensors and recip lattice params
        self.g  = real space metric tensor
        self.gr = recip space metric tensor
        """
        (a,b,c,alp,bet,gam) = self.cell()
        # real metric tensor
        self.g = num.array([ [ a*a, a*b*cosd(gam), a*c*cosd(bet) ],
                             [ b*a*cosd(gam), b*b, b*c*cosd(alp) ],
                             [ c*a*cosd(bet), c*b*cosd(alp), c*c ] ])
        # recip lattice metric tensor
        # and recip lattice params
        self.gr     = num.linalg.inv(self.g)
        self.ar     = num.sqrt(self.gr[0,0])
        self.br     = num.sqrt(self.gr[1,1])
        self.cr     = num.sqrt(self.gr[2,2])
        self.alphar = arccosd(self.gr[1,2]/(self.br*self.cr))
        self.betar  = arccosd(self.gr[0,2]/(self.ar*self.cr))
        self.gammar = arccosd(self.gr[0,1]/(self.ar*self.br))

    def vol(self,recip=False):
        """
        Calculate the cell volume.

        Parameters:
        -----------
        * If recip == False, this is the real space vol in ang**3
          If recip == True, this is the recip space vol in ang**-3

        Notes:
        ------
        V_real  = sqrt(determiant(g))
        V_recip = sqrt(determiant(inv(g)))

        """
        if recip == True: g = self.gr
        else: g = self.g
        det = num.linalg.det(g)
        if det > 0:
            return num.sqrt(det)
        else:
            return 0.0

    def dot(self,u,v,recip=False):
        """
        Calculate dot product of two vectors (u,v)

        Notes:
        ------
        * If u and v are real space vectors, use recip = False
        * If u and v are recip space vectors, use recip = True

        Note u and v are assumed to be normal numpy arrays
        (ie not matrix objects)

        The generalized dot product for real space vectors
        may be defined in terms of the metric tensor (g):
           dot(u,v) = u*g*v
        If u and v are recip lattice vectors replace g with
        gr = inv(g)
        """
        if recip == True: g = self.gr
        else: g = self.g
        u = num.array(u,dtype=float)
        v = num.array(v,dtype=float)
        return num.dot(u,num.dot(g,v))

    def mag(self,v,recip=False):
        """
        Calculate the norm of a vector (v)

        Notes:
        ------
        * If v is real space vector, use recip = False
        * If v is recip space vector, use recip = True

        Note v is assumed to be normal numpy array
        (ie not matrix objects)
        """
        m = num.sqrt(self.dot(v,v,recip=recip))
        return m

    def angle(self,u,v,recip=False):
        """
        Calculate the angle between two vectors (u,v)

        Notes:
        ------
        * If u and v are real space vectors, use recip = False
        * If u and v are recip space vectors, use recip = True

        Note u and v are assumed to be normal numpy arrays
        (ie not matrix objects)
        """
        uv = self.dot(u,v,recip=recip)
        um = self.mag(u,recip=recip)
        vm = self.mag(v,recip=recip)
        arg = uv/(um*vm)
        if num.fabs(arg) > 1.0:
            arg = arg / num.fabs(arg)
        alpha = arccosd(arg)
        return alpha

    def angle_rr(self,x,h):
        """
        Calculate the angle between a real vector x = [x,y,z]
        and a recip vector h = [h,k,l]
        """
        x = num.array(x,dtype=float)
        h = num.array(h,dtype=float)
        hx = num.sum(x*h)
        xm = self.mag(x,recip=False)
        hm = self.mag(h,recip=True)
        arg = hx/(hm*xm)
        if num.fabs(arg) > 1.0:
            arg = arg / num.fabs(arg)
        alpha = arccosd(arg)
        return alpha

    def d(self,hkl):
        """
        Calculate d space for given [h,k,l]
        """
        if len(hkl)!=3:
            print "Error need an array of [h,k,l]"
            return 0.
        h = self.mag(hkl,recip=True)
        if h == 0.:
            #print "Error [h,k,l] magnitude is zero:", hkl
            return 0.0
        d = 1./h
        return d

    def tth(self,hkl,lam=None):
        """
        Calculate 2Theta for given [h,k,l] and wavelength

        Notes:
        ------
        If lam is None the default lambda will be used (Cu Ka1)
        If you pass in lam, this will change the default for
        subsequent calls
        """
        if lam != None: self.lam = float(lam)
        d = self.d(hkl)
        if d == 0.: return 0.
        r = self.lam/(2.*d)
        if num.fabs(r) > 1.0:
            r = r/num.fabs(r)
        tth = 2.*arcsind(r)
        return tth

    def dvec(self,hkl):
        """
        Calculate the real space vector d
        which has a magnitude of the d spacing
        and is normal to the plane hkl = [h,k,l]
        """
        # convert hkl vector to real space indicies
        #hkl  = num.array(hkl,dtype=float)
        #dvec = num.dot(self.gr,hkl)
        dvec = self.recip_to_real(hkl)
        dspc = self.d(hkl)
        if dspc == 0: return num.array([0.,0.,0.])
        dvec = (dspc**2.)*dvec
        return dvec

    def recip_to_real(self,hkl):
        """
        Given recip vector hkl = [h,k,l] calculate the
        vectors indicies in the real lattice
        """
        hkl = num.array(hkl,dtype=float)
        #v   = num.dot(self.gr.transpose(),hkl)
        v   = num.dot(self.gr,hkl)
        return v

    def real_to_recip(self,v):
        """
        Given real vector v = [x,y,z] calculate the
        vectors indicies in the reciprocal lattice
        """
        v   = num.array(v,dtype=float)
        hkl = num.dot(v,self.g)
        return hkl

##########################################################################
class LatticeTransform:
    """
    Generalized lattice transformations
    """
    def __init__(self,lattice,Va=None,Vb=None,Vc=None,shift=None):
        """
        Initialize

        Parameters:
        ----------
        * lattice is a Lattice instance.
          All transforms are defined with respect to this
          lattice, ie this is the original unprimed lattice
        * Va, Vb, Vc are the lattice vectors that define the
          a',b',c' coordinate system (rotation and contraction)
        * shift is the lattice shift vector
        * (V's and shift are defined in the unprimed system)
        """
        self.lattice = lattice
        self.Va    = num.array([1.,0.,0.])
        self.Vb    = num.array([0.,1.,0.])
        self.Vc    = num.array([0.,0.,1.])
        self.shift = num.array([0.,0.,0.])
        self._update(Va=Va,Vb=Vb,Vc=Vc,shift=shift)

    def basis(self,Va=None,Vb=None,Vc=None,shift=None):
        """
        Define new basis vectors.

        Parameters:
        ----------
        * Va, Vb and Vc should define the a',b',c' lattice
          vectors of the new basis (rotation/dialation part).

        * Shift describes an origin shift of the new lattice
          (ie take the new basis defined by Va,Vb,Vc then
          apply the shift vector)

        Notes:
        ------
        All the vectors should be defined in terms of fractional
        coordinate indicies in the original basis, for example:
           a' = x1*a + y1*b + z1*c
           b' = x2*a + y2*b + z2*c
           c' = x3*a + y3*b + z3*c
           shift = x4*a + y4*b + z4*c
        and
           Va = [x1,y1,y1]
           Vb = [x2,y2,y2]
           Vc = [x3,y3,y3]
           shift = [x4,y4,z4]
        """
        self._update(Va=Va,Vb=Vb,Vc=Vc,shift=shift)

    def _update(self,Va=None,Vb=None,Vc=None,shift=None):
        """ update """
        if Va != None: self.Va = num.array(Va,dtype=float)
        if Vb != None: self.Vb = num.array(Vb,dtype=float)
        if Vc != None: self.Vc = num.array(Vc,dtype=float)
        if shift != None: self.shift = num.array(shift,dtype=float)
        F = [self.Va,self.Vb,self.Vc]
        self.F = num.array(F, dtype=float)
        self.G = num.linalg.inv(self.F)
        self.M = self.G.transpose()
        self.N = self.F.transpose()

    def cartesian(self,shift=[0.,0.,0.]):
        """
        Calculates a cartesian basis using:
          Va = a' is parallel to a
          Vb = b' is perpendicular to a' and in the a/b plane
          Vc = c' is perpendicular to the a'/c' plane
        A shift vector may be specified to shift the origin
        of the cartesian lattice relative to the original lattice
        origin (specify shift in fractional coordinates of
        the original lattice)
        """
        (a,b,c,alp,bet,gam) = self.lattice.cell()
        (ar,br,cr,alpr,betr,gamr) = self.lattice.rcell()
        Va = [1./a,                 0. ,             0.]
        Vb = [-1./(a*tand(gam)), 1./(b*sind(gam)),   0.]
        Vc = [ar*cosd(betr),     br*cosd(alpr),      cr]
        self.basis(Va=Va,Vb=Vb,Vc=Vc,shift=shift)

    def xp(self,x):
        """
        Given x = [x,y,z] in the original lattice
        compute the indicies of the vector in the new basis
           xp = M*(x - shift)
        """
        x  = num.array(x,dtype=float)
        if self.shift.sum() != 0.:
            x = x - self.shift
        xp = num.dot(self.M,x)
        return xp

    def x(self,xp):
        """
        Given xp = [x',y',z'] in the primed basis
        compute the indicies of the vector in the original basis
            x = N*(xp + shiftp) = N*xp + shift
        """
        xp  = num.array(xp,dtype=float)
        x = num.dot(self.N,xp)
        if self.shift.sum() != 0.:
            x = x + self.shift
        return x

    def hp(self,h):
        """
        Given h = [h,k,l] in the original (recip) basis
        compute the indicies of the vector in the primed
        (recip) basis
        """
        h  = num.array(h,dtype=float)
        return num.dot(self.F,h)

    def h(self,hp):
        """
        Given hp = [h',k',l'] in the primed (recip) basis
        compute the indicies of the vector in the original
        (recip) basis
        """
        hp  = num.array(hp,dtype=float)
        return num.dot(self.G,hp)

    def plat(self):
        """
        Return a Lattice instance for the primed basis
        """
        a = self.lattice.mag(self.Va)
        b = self.lattice.mag(self.Vb)
        c = self.lattice.mag(self.Vc)
        alp = self.lattice.angle(self.Vb,self.Vc)
        bet = self.lattice.angle(self.Va,self.Vc)
        gam = self.lattice.angle(self.Va,self.Vb)
        return Lattice(a,b,c,alp,bet,gam)

##########################################################################
##########################################################################
def test_lattice():
    # create a new lattice instance
    cell = Lattice(5.6,5.6,13,90,90,120)

    # show lattice parameters and real
    # recip lattice cell volumes
    print cell
    print cell.vol()
    print cell.vol(recip=True)

    # what the hkl = [0,0,1] dspace
    # and 2 theta value for lambda = 1 ang.
    print cell.d([0,0,1])
    print cell.tth([0,0,1],lam=1.)

    # create a real space vector perpendicular
    # to hkl = [0,0,1], with magnitude = dspace(001)
    dv = cell.dvec([0,0,1])
    print dv
    print cell.mag(dv)

    # compute the angle between some vectors
    # recip lattice [1,0,0] and [0,1,0]
    print cell.angle([1,0,0],[0,1,0],recip=True)
    # recip lattice [1,0,0] and [0,0,1]
    print cell.angle([1,0,0],[0,0,1],recip=True)
    # recip lattice [0,0,1] and [1,1,1]
    print cell.angle([0,0,1],[1,1,1],recip=True)

    # real lattice [1,0,0] and [0,1,0]
    print cell.angle([1,0,0],[0,1,0])
    # real lattice [1,0,0] and [0,0,1]
    print cell.angle([1,0,0],[0,0,1])
    # real lattice [0,0,1] and [1,1,1]
    print cell.angle([0,0,1],[1,1,1])

    # real [0,0,1] and recip [0,0,1]
    print cell.angle_rr([0,0,1],[0,0,1])
    # real [1,1,1] and recip [0,0,1]
    print cell.angle_rr([1,1,1],[0,0,1])
    # real dv and recip [0,0,1]
    print cell.angle_rr(dv,[0,0,1])
    # real dv and recip [1,1,0]
    print cell.angle_rr(dv,[1,1,0])

    return cell

def test_transform():
    # create a new hexagonal lattice instance
    cell = Lattice(5.6,5.6,13,90,90,120)

    # test lattice transform
    # below defines rhombohedral basis vectors
    # in terms of the heaxagonal basis vectors
    Va_rhom = [0.6667, 0.3333, 0.3333]
    Vb_rhom = [-0.3333, 0.3333, 0.3333]
    Vc_rhom = [-0.3333, -0.6667, 0.3333]
    t = LatticeTransform(cell,Va=Va_rhom,Vb=Vb_rhom,Vc=Vc_rhom)

    # create an instance of the rhombohedral lattice
    # shows the rhomb lattice params
    print t.plat()

    # given the hkl = [001] in hex calc hkl in rhom
    print t.hp([0,0,1])

    # given the hkl = [111] in rhom calc hkl in hex
    print t.h([1,1,1])

    # create a cartesian representation
    t.cartesian()
    cart = t.plat()
    print cart

    # convert a [1,1,0] vector in hex lattice to cartesian
    vc = t.xp([1,1,0])
    print vc

    # check that the vector is the same length
    print cell.mag([1,1,0])
    print cart.mag(vc)

    return t

##########################################################################
##########################################################################
if __name__ == "__main__":
    """
    test
    """
    test_lattice()
    test_transform()


import numpy as np

def congruent_linear_generator(a : int, m : int, c : int, x0 : int):
    """
    :def: Linear Congruent Generator Technique
    : Method to generate pseudo-randoms
    :
    :param a:  Random Parameter
    :type a:   int
    :param c:  Constant used to create bigger variation of generated numbers
    :type c:   int
    :param m:  Parameter which determines the biggest value generated
    :type m:   int
    :param x0: Determines initial seed and stop trigger
    :type x0:  int
    :return:   Pseudo-random list of numbers
    :rtype:    list[int]
    """
    return (a*x0 + c)%m

def give_me_n_random(RANGE:int, SEED:int, a:int=44, m:int=95784, c:int=4565):
    pseudo_random = [SEED]
    for _ in range(RANGE):
        pseudo_random.append(congruent_linear_generator(a, m, c, pseudo_random[-1]))

    MIN = min(pseudo_random)
    MAX = max(pseudo_random)

    def percentage(MIN, MAX, value):
        return round(((value - MIN)/(MAX - MIN)),2)

    return [percentage(MIN, MAX, x) for x in pseudo_random][1:]


import numpy as np
from nptyping import Array

def lms(d: Array, x: Array, w: Array, mu: float, _) -> Array:
    """
    lms Least-Mean-Square filtering.

    Args:
        d (Array): Desired values.
        x (Array): Input(recorded) values.
        w (Array): Filter-weight matrix.
        mu (float): Step size.
        _ ([dummy argument]): DO NOT SET ANY VALUE.

    Returns:
        Array: Modified value array of w.
    """
    N = len(x)

    y = np.zeros_like(d)
    e = np.zeros_like(d)

    # w_delta: 係数行列wの更新量
    w_delta = np.zeros_like(w)

    for k in range(N):
        y[k] = np.dot(w.T, x[k])
        e[k] = d[k] - y[k]
        # 2 * J = e ** 2
        diff_J_for_w_ = e[k] * x[k]
        w_delta = mu * diff_J_for_w_
    return w_delta

def nlms(d: Array, x: Array, w: Array, mu: float, lambda_: float = 1.0) -> Array:
    """
    Normalized-LMS filtering.

    Args:
        d (Array): Desired values.
        x (Array): Input(recorded) values.
        w (Array): Filter-weight matrix.
        mu (float): Step size.
        lambda_ (float, optional): Learning rate. Defaults to 1.0.

    Returns:
        Array: Modified value array of w.
    """

    N = len(x)

    y = np.zeros_like(d)
    e = np.zeros_like(d)

    # w_delta: 係数行列wの更新量
    w_delta = np.zeros_like(w)

    for k in range(N):
        y[k] = np.dot(w.T, x[k])
        e[k] = d[k] - y[k]
        # 2 * J = e ** 2 - sum(w[i] ** 2 for i in range(N))
        diff_J_for_w_ = e[k] * x[k]
        w_delta = mu / (np.dot(x[k], x[k]) + lambda_) * diff_J_for_w_

    return w_delta


### Example script showing how to perform a Total-Variation filtering with proxTV
import prox_tv as ptv
import numpy as np
import matplotlib.pyplot as plt
import time

def _blockysignal():
    """Generates a blocky signal for the demo"""
    N = 1000
    s = np.zeros((N,1))
    s[int(N/4):int(N/2)] = 1
    s[int(N/2):int(3*N/4)] = -1
    s[int(3*N/4):int(-N/8)] = 2
    return s

### TV-L1 filtering

# Generate impulse (blocky) signal
s = _blockysignal()

# Introduce noise
n = s + 0.5*np.random.rand(*np.shape(s))

# Filter using TV-L1
lam=20
print('Filtering signal with TV-L1...')
start = time.time()
f = ptv.tv1_1d(n,lam)
end = time.time()
print('Elapsed time ' + str(end-start))

# Plot results
plt.subplot(3, 1, 1)
plt.title('TVL1 filtering')
plt.plot(s)
plt.ylabel('Original')
plt.grid(True)

plt.subplot(3, 1, 2)
plt.plot(n)
plt.ylabel('Noisy')
plt.grid(True)

plt.subplot(3, 1, 3)
plt.plot(f)
plt.ylabel('Filtered')
plt.grid(True)

plt.show()

### TV-L2 filtering

# Generate sinusoidal signal
N = 1000
s = np.sin(np.arange(1,N+1)/10.0) + np.sin(np.arange(1,N+1)/100.0)

# Introduce noise
n = s + 0.5*np.random.randn(*np.shape(s))

# Filter using TV-L2
lam=100;
print('Filtering signal with TV-L2...')
start = time.time()
f = ptv.tv2_1d(n,lam);
end = time.time()
print('Elapsed time ' + str(end-start))

# Plot results
plt.subplot(3, 1, 1)
plt.title('TVL2 filtering')
plt.plot(s)
plt.ylabel('Original')
plt.grid(True)

plt.subplot(3, 1, 2)
plt.plot(n)
plt.ylabel('Noisy')
plt.grid(True)

plt.subplot(3, 1, 3)
plt.plot(f)
plt.ylabel('Filtered')
plt.grid(True)

plt.show()

### Weighted TV-L1 filtering

# Generate impulse (blocky) signal
s = _blockysignal()

# Introduce noise
n = s + 0.5*np.random.randn(*np.shape(s))

# Generate weights
lam = np.linspace(0,2,N-1)

# Filter using weighted TV-L1
print('Filtering signal with weighted TV-L1...')
start = time.time()
f = ptv.tv1w_1d(n, lam)
end = time.time()
print('Elapsed time ' + str(end-start))

# Plot results
plt.subplot(4, 1, 1)
plt.title('Weighted TVL1 filtering')
plt.plot(s)
plt.ylabel('Original')
plt.grid(True)

plt.subplot(4, 1, 2)
plt.plot(n)
plt.ylabel('Noisy')
plt.grid(True)

plt.subplot(4, 1, 3)
plt.plot(f)
plt.ylabel('Filtered')
plt.grid(True)

plt.subplot(4, 1, 4)
plt.fill_between(np.arange(1,N), 0, lam)
plt.ylabel('Weights')
plt.grid(True)

plt.show()