# standard deviation python
# https://www.google.com/search?q=standard+deviation+python&oq=Standard+deviation+python&aqs=chrome.0.0l10.1896j0j7&sourceid=chrome&ie=UTF-8

'''
Standard deviation measures the spread of a data distribution.
The more spread out a data distribution is, the greater its
standard deviation. Interestingly, standard deviation cannot be negative.
A standard deviation close to 0 indicates that the data points tend to
be close to the mean
'''

import numpy as np
from statistics import stdev

a = np.array([[1, 2], [3, 4]])
# print(np.std(a))
# print(np.std(a, ddof=1))
# print(np.std(a, axis=0))
# print(np.std(a, axis=1))

a = [14, 8, 11, 10, 7, 9, 10, 11, 10, 15, 5, 10]
# print(stdev(a))

a = np.zeros((2, 512*512), dtype=np.float32)
a[0, :] = 1.0
a[1, :] = 0.1
# print(a)
# print(np.std(a, dtype=np.float64))

a = np.array([[14, 8, 11, 10], [7, 9, 10, 11], [10, 15, 5, 10]])
# print(np.std(a, ddof=1, dtype=np.float64))


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

# (a)
n = 100
rng = np.random.default_rng(42)

mu = 5
data = rng.normal(mu, 1, size=n)

# (b)
mu_lim = (3, 7)
k = 5000
mus = np.linspace(*mu_lim, k)
likelihood = np.ones(k)
for x in data:
    likelihood *= stats.norm.pdf(x, loc=mus)

mu_density = likelihood * 1
mu_density /= mu_density.sum()

fig_b, ax_b = plt.subplots()
ax_b.set(xlabel="Mu", ylabel="Posterior density")
ax_b.plot(mus, mu_density)
fig_b.savefig("11-02b.png", bbox_inches="tight")

# (c)
drawn_mus = rng.choice(mus, size=1000, p=mu_density)
fig_c, ax_c = plt.subplots()
ax_c.set(xlabel="Mu", ylabel="Frequency")
ax_c.hist(drawn_mus, bins=150, range=mu_lim)
fig_c.savefig("11-02c.png", bbox_inches="tight")

# (d)
drawn_thetas = np.exp(rng.choice(mus, size=10000, p=mu_density))

theta_lims = (75, 220)
fig_d, axs_d = plt.subplots(1, 2, figsize=(14, 6))
axs_d[0].set(xlabel="Theta", ylabel="Frequency")
axs_d[0].hist(drawn_thetas, bins="auto", range=theta_lims)

thetas = np.geomspace(*theta_lims, k)
log_thetas = np.log(thetas)
diff = np.tile(log_thetas, n).reshape((n, -1)).transpose() - data
propto = np.exp(-0.5 * np.sum(diff ** 2, axis=1))
theta_density = propto / propto.sum()
axs_d[1].set(xlabel="Theta", ylabel="Posterior density")
axs_d[1].plot(thetas, theta_density)

fig_d.savefig("11-02d.png", bbox_inches="tight")

# (e)
mu_cdf = mu_density.cumsum()
alpha = 0.05
mu_left_idx = np.argmax(mu_cdf >= alpha / 2)
mu_right_idx = np.argmax(mu_cdf >= 1 - alpha / 2)
mu_pi = (mus[mu_left_idx], mus[mu_right_idx])
print(
    "95% posterior interval for mu is given by",
    f"[{mu_pi[0]:.3f}, {mu_pi[1]:.3f}]",
)

# (f)
theta_cdf = theta_density.cumsum()
alpha = 0.05
theta_left_idx = np.argmax(theta_cdf >= alpha / 2)
theta_right_idx = np.argmax(theta_cdf >= 1 - alpha / 2)
theta_pi = (thetas[theta_left_idx], thetas[theta_right_idx])
print(
    "95% posterior interval for theta is given by",
    f"[{theta_pi[0]:.3f}, {theta_pi[1]:.3f}]",
)


'''' File with functions to perform common operations on fixed-income securities
(discounted cash-flows, computing durations, converting between different types of
 conventions for quoting interest rates, perform day counts depending on the convention
 chosen...), amortization table...
  Definitions:
• Maturity (T): date at which the last payment to the bondholders is due. The bond is redeemed at this date.
• Coupon (C): interest payment that is made by the issuer to each bondholder, at periodic dates.
• Face Value (N) (or par value): final payment which is made at maturity with the last coupon.
• Principal: Amount borrowed. Generally paid off when the bond matures.
• The Frequency (k) with which coupons are paid (e.g. once every year or once every semester).
• The Coupon Rate (c): by definition, the coupon rate is c = k * C / N.
• Price: How much the bond sells for (as opposed to its face value) '''

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

def coupon_freq_mat(c, T, ytm, fv=100, k=1, summary_table=True):
    '''
    Purpose: get information about a bond defined by (coupon rate, frequency of compounding,
            maturity, face value)
    :param fv: face value of bond
    :param c: coupon rate
    :param k: compounding freq. If None, is continuous
    :param T: maturity of the bond
    :param ytm: yield to maturity at all horizons (compounded k times per year)
    :param summary_table: true iif want to print table showing computation details
    '''
    coupon = c * fv / k # payment at each period, computed from the coupon rate
    print('The coupon is', coupon)
    dcf = 0
    conv = 0 # convexity of the bond, ie 2nd derivative of price wrt to y, divided by price
    d_mac = 0 # Macaulay duration
    for i in range(1, int(round(k * T))): # at each coupon payment period...
        payment_pv = coupon / ((1 + ytm / k)**i)
        dcf += payment_pv
        d_mac += (i / k) * payment_pv
        conv += (i/k) * (i/k + 1/k) * payment_pv
    pv_last_payment = (coupon + fv) / (1 + ytm / k)**(T*k) # add the principal payment
    dcf += pv_last_payment
    d_mac += T * pv_last_payment
    d_mac /= dcf
    d_mod = d_mac / (1 + ytm / k)
    conv += T * (T + 1/k) * pv_last_payment
    conv /= dcf * ((1 + ytm/k)**2)
    summary = {'DCF': dcf,
               'Maucaulay duration': d_mac,
               'Modified duration':d_mod,
               'Dollar duration': d_mod * dcf / 100,
               'Convexity': conv,
               'Dollar convexity':conv * dcf / 100}
    print(pd.DataFrame(summary, index=[0]))

def amortization_table(c, T, ytm, fv=100, k=1, summary_table=True):
    '''
    Print amorization table for the coupon
    :param c:
    :param T:
    :param ytm:
    :param fv:
    :param k:
    :param summary_table:
    '''

def coupon_continuous(c, T, ytm, fv=100):
    '''
    Purpose: get information about a continuously compounded bond
    :param c:
    :param T:
    :param ytm:
    :param fv:
    '''

def dcf(cf, ytm=None, r=None, times=None):
    '''
    :param cf: np array of cash-flows
    :param rates: the rates to take into account at diverse horizons
    :param ytm: yield to maturity, compounded annually
    :param times: np array of when when the cf are made. If None, assumes 1, 2, 3, ... (yearly cf)
    :return: the thus discounted cash-flow (= value)
    '''
    dcf = 0
    if r is None:
        r = np.repeat(ytm, cf.shape[0])
    if times is None:
        times = np.array(range(1, cf.shape[0] + 1))
    for rate, payment, t in zip(r, cf, times):
        dcf += payment / (1 + rate)**t
    return dcf


#!/usr/bin/env python

'''
Test for disctrete fourier transform (dft)
'''

# Python 2/3 compatibility
from __future__ import print_function

import cv2
import numpy as np
import sys

from tests_common import NewOpenCVTests

class dft_test(NewOpenCVTests):
    def test_dft(self):

        img = self.get_sample('samples/data/rubberwhale1.png', 0)
        eps = 0.001

        #test direct transform
        refDft = np.fft.fft2(img)
        refDftShift = np.fft.fftshift(refDft)
        refMagnitide = np.log(1.0 + np.abs(refDftShift))

        testDft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
        testDftShift = np.fft.fftshift(testDft)
        testMagnitude = np.log(1.0 + cv2.magnitude(testDftShift[:,:,0], testDftShift[:,:,1]))

        refMagnitide = cv2.normalize(refMagnitide, 0.0, 1.0, cv2.NORM_MINMAX)
        testMagnitude = cv2.normalize(testMagnitude, 0.0, 1.0, cv2.NORM_MINMAX)

        self.assertLess(cv2.norm(refMagnitide - testMagnitude), eps)

        #test inverse transform
        img_back = np.fft.ifft2(refDft)
        img_back = np.abs(img_back)

        img_backTest = cv2.idft(testDft)
        img_backTest = cv2.magnitude(img_backTest[:,:,0], img_backTest[:,:,1])

        img_backTest = cv2.normalize(img_backTest, 0.0, 1.0, cv2.NORM_MINMAX)
        img_back = cv2.normalize(img_back, 0.0, 1.0, cv2.NORM_MINMAX)

        self.assertLess(cv2.norm(img_back - img_backTest), eps)


import numpy as np
import matplotlib.pylab as plt
import scipy
import scipy.linalg

n = 100
xs = np.arange(0, n)

def construct_covariance(sigma1, sigma2):
    K = sigma1**2 * np.exp(
        -(xs[:, np.newaxis] - xs[:, np.newaxis].T)**2 / sigma2**2
    )
    K += 1e-5 * np.eye(n)
    return K

def sample_zero_mean_random_field(covariance_matrix):
    L, _ = scipy.linalg.cho_factor(covariance_matrix, lower=True)
    indep_random_sample = np.random.normal(size=n)
    return np.dot(np.tril(L), indep_random_sample)

sigma1 = 1.0
sigma2 = 10.0
K1 = sigma1**2 * np.eye(n)
K2 = construct_covariance(sigma1, sigma2)

plt.plot(sample_zero_mean_random_field(K1))
plt.plot(sample_zero_mean_random_field(K2))
plt.show()

def log_likelihood(sigma1, sigma2, x):
    K = construct_covariance(sigma1, sigma2)
    L, lower = scipy.linalg.cho_factor(K, lower=True)

    logdet = 2 * np.sum(np.log(np.diag(L)))
    xinvSx = x.dot(scipy.linalg.cho_solve((L, lower), x))

    return -0.5 * (logdet + xinvSx)

sigma1 = np.linspace(0.5, 1.5, 100)
sigma2 = np.linspace(5.0, 15.0, 100)
Sigma1, Sigma2 = np.meshgrid(sigma1, sigma2)
x = sample_zero_mean_random_field(K2)

L = np.vectorize(log_likelihood, excluded=['x'])(Sigma1, Sigma2, x=x)
max_log_likelihood = log_likelihood(1, 10, x)
levels = np.linspace(0.9 * max_log_likelihood, max_log_likelihood, 5)

plt.clf()
contours = plt.contour(Sigma1, Sigma2, L, levels=levels, colors='black')
plt.clabel(contours, inline=True, fontsize=8)

plt.imshow(L, extent=[0.5, 1.5, 5.0, 15.0], origin='lower',
           cmap='RdGy', alpha=0.5, vmin=levels[0], aspect='auto')
c = plt.colorbar();
c.set_label(r'$\mathcal{L}$')
plt.xlabel(r'$\sigma_1$')
plt.ylabel(r'$\sigma_2$')
plt.show()


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

def descenso(f, x0, dx=0.001, error=0.00001, maxiter=100000):    # introducir maxtol?

    xpost = x0 + dx
    xpre = x0 - dx

    f0 = f(x0)
    fpost = f(xpost)
    fpre = f(xpre)
    step = np.min([f0 - fpre, fpost - f0])

    contador = 0

    while np.abs(step) > error and contador <= maxiter:
        if fpost > f0:
            if fpre > f0:
                return x0, f0, contador
            else:
                contador += 1
                x0 = xpre
                xpre = x0 - dx
                f0 = f(x0)
                fpre = f(xpre)
                step = f0 - fpre

        elif fpost < f0 and f0 < fpre:
            contador += 1
            x0 = xpost
            xpost = x0 + dx
            f0 = f(x0)
            fpost = f(xpost)
            step = fpost - f0

        else:
            print('No se puede encontrar mínimo')
            return [], [], []

    return x0, f0, contador

def newtonoptim(f, x0, dx=0.01, error=0.0001, max_iter=100000):

    xn = x0

    for n in range(0, max_iter):

        dfn = (f(xn + dx) - f(xn - dx)) / (2 * dx)

        if abs(dfn) < error:
            print('Solución tras ', n, ' iteraciones.')
            return xn, f(xn), dfn

        d2fn = (f(xn + dx) - 2 * f(xn) + f(xn - dx)) / (dx ** 2)

        if d2fn == 0:
            print('Método se indefine (segunda derivada se anula).')
            return None, None, None

        xn = xn - dfn / d2fn

    print('Método no converge (se supera el máximo de iteraciones).')
    return None, None, None


import random
import numpy as np

class My_Perceptron(object):
    def __init__(self):
        self.weight_vector = []

    def function(self, features):
        '''Activation function

        z is the input vector Z
        weights is the vector of weights
        '''
        total_sum = self.weight_vector[0]
        for i in range(1, len(features)):
            # f(x) = wx + b
            total_sum += features[i] * self.weight_vector[i + 1]

        return 1.0 if total_sum >= 0.0 else 0.0

    def train(self, features, labels, r=0.01, max_iter=5):
        '''Train the weights

        dataset is the input dataset
        '''
        # initialize all weights to 0
        self.weight_vector = [0 for i in range(len(features[0]) + 1)]

        total_sum_dataset = len(features)

        for each_iter in range(max_iter):
            error_count = 0
            for instance_features, label in zip(features, labels):
                # Calculate the actual output
                actual_output = self.function(instance_features)
                if actual_output != label:
                    # wrong output
                    error_count += 1
                    # update the weights
                    diff = r * (label - actual_output)

                    for w in range(1, len(self.weight_vector)):
                        self.weight_vector[w] += diff * instance_features[w - 1]

                    # update the bias
                    self.weight_vector[0] += diff

            print('Number of Errors: ' + str(error_count))
            print('Score: ' + str(self.score(error_count, total_sum_dataset)))
            # random.shuffle(dataset)
        return

    def score(self, errors, total_number):
        return (total_number - errors) / total_number

my_perceptron = My_Perceptron()
list_of_lists = []
with open('bankdata.txt', 'r') as txt_file:
    list_of_lists = [[float(item) for item in line.strip().split(',')] for line in txt_file]

all_features = [item[:-1] for item in list_of_lists]
all_labels = [item[-1] for item in list_of_lists]

my_perceptron.train(all_features, all_labels, r=0.001, max_iter=20)

# from sklearn.datasets import load_digits
# from sklearn.linear_model import Perceptron

# X_array = []
# for item in float_list:
#     X_array.append(item[:len(item) - 2])

# y_array = []
# for final_output in float_list:
#     y_array.append(item[len(item) - 1])

# sklearn_perceptron = Perceptron(tol=1e-3, random_state=0)
# sklearn_perceptron.fit(X_array, y_array)
# sklearn_perceptron.score(X_array, y_array)


import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print ('Original array is:')
print (a)
print ('\n')

print ('Modified array is:')
for x in np.nditer(a):
   print (x,)

#print ('\n')

   import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print ('Original array is:')
print (a)
print ('\n')

print ('Transpose of the original array is:')
b = a.T
print (b)
print ('\n')

print ('Modified array is:')
for x in np.nditer(b):
   print (x,)

# Iteration Order
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')

print ('Transpose of the original array is:')
b = a.T
print (b)
print ('\n')

print ('Sorted in C-style order:')
c = b.copy(order = 'C')
print (c)
for x in np.nditer(c):
   print (x,)

print ('\n')

print ('Sorted in F-style order:')
c = b.copy(order = 'F')
print (c)
for x in np.nditer(c):
   print (x,)

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print ('Original array is:')
print (a)
print ('\n')

print ('Sorted in C-style order:')
for x in np.nditer(a, order = 'C'):
   print (x,)
print ('\n')

print ('Sorted in F-style order:')
for x in np.nditer(a, order = 'F'):
   print (x,)

# Modifying Array Values
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')

for x in np.nditer(a, op_flags = ['readwrite']):
   x[...] = 2*x
print ('Modified array is:')
print (a)

# External Loop
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print ('Original array is:')
print (a)
print ('\n')

print ('Modified array is:')
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
   print (x,)

# Broadcasting Iteration
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print ('First array is:')
print (a)
print ('\n')

print ('Second array is:')
b = np.array([1, 2, 3, 4], dtype = int)
print (b)
print ('\n')

print ('Modified array is:')
for x,y in np.nditer([a,b]):
   print ("%d:%d" % (x,y),)


"""Data Preprocessors."""

import numpy as np

from typing import Optional

class Normalization(object):
    """Rescale the data via a normalization.

    Produce:
    - Bring all values into the range [0, 1]
    """

    def __call__(self, X, axis: int = 0):
        min_x = np.amin(X, axis=axis)
        max_x = np.amax(X, axis=axis)
        return (X - min_x) / (max_x - min_x)

class Standardization(object):
    """Rescale the data via a standardization

    Produce:
    - mean(Xstandardized) = 0
    - std(Xstandardized) = 1
    """

    def __init__(self, mu: Optional[int] = None, sigma: Optional[int] = None):
        self.mu = mu
        self.sigma = sigma

    def __call__(self, X, axis: int = 0):
        if self.mu is None or self.sigma is None:
            self.mu = np.mean(X, axis=axis)
            self.sigma = np.std(X, axis=axis)

            if not np.any(self.sigma):
                self.sigma = np.ones_like(self.sigma)

        return np.divide(X - self.mu, self.sigma)


import numpy as np
import numpy.linalg as linalg
import scipy.linalg

import matplotlib.pyplot as plt

#Could be useful if you don't have a lot of base_functions and a lot of data
#Build the coefficient matrix of the linear equation system for which we want to minimize the residue.
def buildMatrix (base_functions, evaluation_points):
    #The matrix A
    A = np.zeros((evaluation_points.size,base_functions.size))

    #Iterate over points where the base functions will be evaluated. Corresponds to a row in A.
    for i in range(0, evaluation_points.size):
        ev_point = evaluation_points[i]
        #Iterate over the base functions, evaluate them and save the result in the matrix. Corresponds to a column in A.
        for j in range(0, base_functions.size):
            A[i,i] = base_functions[j](ev_point)
    return A

#Use this if the mxn matrix is big and sparse
#Uses the fact that the Jacobian matrix / Gradient of ||Ax-b||^2 should be zero if it is a minimum
def minimize_ata(A, b):
    ATA=np.dot(A.T,A)
    ATb=np.dot(A.T,b)
    x=np.linalg.solve(ATA,ATb)
    return x

#Possible to use this with a non fulll rank matrix, however SVD is better (according to the script).
def minimize_qr(A, b):
    Q, R = scipy.linalg.qr(A,mode="economic") #economic mode returns a square matrix for R (last rows will all be 0)
    btilde = np.dot(Q.T, b)
    x= scipy.linalg.solve_triangular(R, btilde) #R is an upper triagonal matrix
    return x

def minimize_svd(A, b):
    U,s,Vh = scipy.linalg.svd(A)
    r = np.linalg.matrix_rank(np.diag(s)) #rank
    x = np.dot(Vh[:r,:].T, np.dot(U[:,:r].T,b)/s[:r] )
    return x

#Implement assignemnt 8.2 (radioactive Decomposition)

def exactActivity (t, lambdas , m0):
    activity=0
    for i in range (m0.size):
        activity += m0[i]* lambdas[i]* np.exp (-lambdas[i]*t)
    return activity

def test():
    n=4
    number_of_measurements=10**2
    m0=np.floor(np.random.rand(n)*1000)
    lambdas=np.linspace(10**-2,10**-1,n)

    times = np.linspace(0.1 , 7, number_of_measurements)
    activities = np.zeros(times.shape)
    for i in range(number_of_measurements):
        activities[i] = exactActivity(times[i],lambdas,m0)

    noise_amplitude = 1E-7
    noise = 1 + (noise_amplitude *np.random.rand (number_of_measurements) - noise_amplitude )
    noisy_activities = activities*noise #hihihi
    plt.plot (times, activities, "k-", label="Exact Activity")
    plt.plot (times, noisy_activities, "go", alpha=0.5 , label="Noisy")
    plt.grid(True)
    plt.legend()
    plt.show()

    A=np.zeros((number_of_measurements, n))
    for i in range(n):
        A[:,i] = lambdas[i]* np.exp (-lambdas[i]*times)

    #Approximate the m's with least squares methods
    x1=minimize_ATA(A, activities)
    x2=minimize_qr(A,activities)
    x3=minimize_svd(A,activities);
    print("Exact")
    print("Exact M's: ",m0)
    print("Normal M's: ",x1)
    print("QR M's: ",x2)
    print("SVD M's: ",x3)

    x1=minimize_ATA(A, noisy_activities)
    x2=minimize_qr(A,noisy_activities)
    x3=minimize_svd(A,noisy_activities);
    print("Noisy: ")
    print("Exact M's: ",m0)
    print("Normal M's: ",x1)
    print("QR M's: ",x2)
    print("SVD M's: ",x3)

    return

if __name__ == "__main__":
    test()


import numpy as np

_ft2m = 0.3048
_m2ft = 3.2808

def lin_to_db(lin_value, eps=1e-99):
    """
    Convert inputs from linear units to dB, via the simple equation
         db = 10 * log10 (lin)

    Nicholas O'Donoughue
    7 May 2021

    :param lin_value: scalar or numpy array of linear values to convert
    :param eps: minimum precision, any inputs < eps will be capped, to prevent a divide by zero runtime error
    :return: scalar or numpy array of db values
    """

    # Use the optional eps argument as a minimum allowable precision, to prevent divide by zero errors if we take the
    # logarithm of 0.
    lin_value = np.where(lin_value >= eps, lin_value, eps)

    return 10. * np.log10(lin_value)

def db_to_lin(db_value):
    """
    Convert input from db units to linear, via the simple equation
        lin = 10^(db/10)

    Nicholas O'Donoughue
    7 May 2021

    :param db_value: scalar or numpy array of db values to convert
    :return: scalar or numpy array of linear values
    """

    return np.power(10., db_value/10.)

def kft_to_km(kft_value):
    """
    Convert altitude from kft (thousands of feet) to km.

    :param kft_value:
    :return:
    """
    return kft_value * _ft2m

def km_to_kft(km_value):
    """
    Convert altitude from km to kft (thousands of feet)

    :param km_value:
    :return:
    """

    return km_value * _m2ft


# This example shows calculation of integral value for PI using cuRAND library.

import pycuda.autoinit
import pycuda.driver as drv
from pycuda import gpuarray
from pycuda.compiler import SourceModule
import numpy as np
from sympy import Rational

ker = SourceModule(
no_extern_c=True,
source='''
#include <curand_kernel.h>
#define _PYTHAG(a,b) (a*a+b*b)
#define ULL unsigned long long

extern "C" {
    __global__ void estimate_pi(ULL iters, ULL *hits)
    {
        curandState cr_state;
        int tid = blockIdx.x * blockDim.x + threadIdx.x;
        curand_init((ULL) clock() + (ULL) tid, (ULL) 0, (ULL) 0, &cr_state);
        float x, y;

        for(ULL i = 0; i < iters; i++)
        {
            x = curand_uniform(&cr_state);
            y = curand_uniform(&cr_state);

            if(_PYTHAG(x,y) <= 1.0f)
                hits[tid]++;
        }

        return;
    }
}
''')

pi_ker = ker.get_function("estimate_pi")
threads_per_block = 32
blocks_per_grid = 512
total_threads = threads_per_block * blocks_per_grid
hits_d = gpuarray.zeros((total_threads,), dtype=np.uint64)

iters = 2**24

pi_ker(np.uint64(iters), hits_d, grid=(blocks_per_grid, 1, 1), block=(threads_per_block, 1, 1))

total_hits = np.sum(hits_d.get())
total = np.uint64(total_threads) * np.uint64(iters)

est_pi_symbolic = Rational(4) * Rational(int(total_hits), int(total))

est_pi = np.float(est_pi_symbolic.evalf())

print("Our Monte Carlo estimate of PI is: {}".format(est_pi))
print("NumPy's PI constant is: {}".format(np.pi))
print("Our estimate passes NumPy's 'allclose': {}".format(np.allclose(est_pi, np.pi)))

import numpy,from sympy,import pycuda,from pycuda


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

# # Question No 1

# In[1]:

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

# In[2]:

df1=pd.read_csv('delivery_time.csv')
df1.head()

# In[3]:

df1.iloc[:, 1]

# In[4]:

df1.columns

# In[5]:

df1.info()

# In[6]:

df1=df1.rename(columns={'Delivery Time': 'dt','Sorting Time': 'st' })
df1.head()

# In[7]:

sns.distplot(df1['dt'])

# In[8]:

sns.distplot(df1['st'])

# In[9]:

plt.scatter(x='st', y='dt',data=df1, color='red')
plt.xlabel("Sorting time")
plt.ylabel("Delivery time")

# In[10]:

sns.regplot(x="st", y="dt", data=df1,color='red')

# In[11]:

plt.boxplot('st',data=df1)

# In[12]:

plt.boxplot('dt',data=df1)

# In[13]:

df1.corr()

# In[14]:

import statsmodels.formula.api as smf
model1 = smf.ols("dt~st",data = df1).fit()

# In[15]:

model1.params

# In[16]:

print(model1.tvalues, '\n', model1.pvalues)

# In[17]:

(model1.rsquared,model1.rsquared_adj)

# In[18]:

newdata=pd.Series([10,25])

# In[19]:

data_pred=pd.DataFrame(newdata,columns=['st'])

# In[20]:

model1.predict(data_pred)

# In[ ]:

# # Question No 2

# In[21]:

df=pd.read_csv('Salary_Data.csv')
df.head()

# In[22]:

df.info()

# In[23]:

df=df.rename(columns={'YearsExperience': 'ye','Salary': 's' })
df.head()

# In[24]:

sns.distplot(df['ye'])

# In[25]:

sns.distplot(df['s'])

# In[26]:

plt.scatter(x='s', y='s',data=df, color='red')
plt.xlabel("Salary")
plt.ylabel("Years of Experience")

# In[27]:

sns.regplot(x="s", y="ye", data=df,color='red')
plt.xlabel("Salary")
plt.ylabel("Years of Experience")

# In[28]:

plt.boxplot('s',data=df)

# In[29]:

plt.boxplot('ye',data=df)

# In[30]:

df.corr()

# In[31]:

import statsmodels.formula.api as smf
model = smf.ols("s~ye",data = df).fit()

# In[32]:

model.params

# In[33]:

print(model.tvalues, '\n', model.pvalues)

# In[34]:

(model.rsquared,model.rsquared_adj)

# In[35]:

newdata=pd.Series([5,7])

# In[36]:

data_pred=pd.DataFrame(newdata,columns=['ye'])

# In[37]:

model.predict(data_pred)

# In[ ]:

Assignment on Simple Linear Regression .py


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

N = 200                           # Number of points in each direction
xStart,xEnd = -4.0,4.0            # x-direction boundaries
yStart,yEnd = -2.0,2.0            # y-direction boundaries
x = np.linspace(xStart,xEnd,N)    # x 1D-array
y = np.linspace(yStart,yEnd,N)    # y 1D-array
X,Y = np.meshgrid(x,y)            # generation of the mesh grid

Uinf = 1.0        # freestream speed
alphaInDegrees = 0.0       # angle of attack (in degrees)
alpha = alphaInDegrees*pi/180

# computing the velocity components on the mesh grid
uFreestream = Uinf*cos(alpha)
vFreestream = Uinf*sin(alpha)

# computing the stream-function on the mesh grid
psiFreestream = + Uinf*cos(alpha)*Y - Uinf*sin(alpha)*X

def flowproperties(strength,xs,ys,X,Y):
    u = strength/(2*pi)*(X-xs)/((X-xs)**2+(Y-ys)**2)
    v = strength/(2*pi)*(Y-ys)/((X-xs)**2+(Y-ys)**2)
    psi = strength/(2*pi)*np.arctan2((Y-ys),(X-xs))
    return u,v,psi
def flowdoublet(mu,xs,ys,X,Y):
    u = - mu/(2*pi)*((X-xd)**2-(Y-yd)**2)/((X-xd)**2+(Y-yd)**2)**2
    v = - mu/(2*pi)*2*(X-xd)*(Y-yd)/((X-xd)**2+(Y-yd)**2)**2
    psi = -mu/(2*pi)*(Y-yd)/((X-xd)**2+(Y-yd)**2)
    return u,v,psi

def flowvortex(ga,xv,yv,X,Y):
    u = +ga/(2*pi)*(Y-yv)/((X-xv)**2+(Y-yv)**2)
    v = -ga/(2*pi)*(X-xv)/((X-xv)**2+(Y-yv)**2)
    psi = ga/(4*pi)*np.log((X-xv)**2+(Y-yv)**2)
    return u,v,psi

""" ssource=5.0

xsource,ysource=-1.0,0.0

usource,vsource,psisource=flowproperties(ssource,xsource,ysource,X,Y)

ssink=-5.0
xsink,ysink=1.0,0.0
usink,vsink,psisink=flowproperties(ssink,xsink,ysink,X,Y)

u = uFreestream + usource + usink
v = vFreestream + vsource + vsink
psi = psiFreestream + psisource +psisink """

xd,yd=0.0,0.0
mu=5
ga=5
xv,yv,=0.0,0.0
udoublet,vdoublet,psidoublet=flowdoublet(mu,xd,yd,X,Y)
uvortex,vvortex,psivortex=flowvortex(ga,xv,yv,X,Y)

u=udoublet+uFreestream+uvortex
v=vdoublet+vFreestream+vvortex
psi=psidoublet+psiFreestream+psivortex

# plotting

size = 10
plt.figure(figsize=(size,(yEnd-yStart)/(xEnd-xStart)*size))
plt.grid(True)
plt.xlabel('x',fontsize=16)
plt.ylabel('y',fontsize=16)
plt.xlim(xStart,xEnd)
plt.ylim(yStart,yEnd)
plt.streamplot(X,Y,u,v,density=2.0,linewidth=1,arrowsize=1,arrowstyle='->')
plt.scatter(xd,yd,c='#CD2305',s=80,marker='o')

""" # computing the stagnation point
xStagnation = xsource - ssource/(2*pi*Uinf)*cos(alpha)
yStagnation = ysource - ssource/(2*pi*Uinf)*sin(alpha)

xstag=xsink - ssink/(2*pi*Uinf)*cos(alpha)
ystag=ysink - ssink/(2*pi*Uinf)*sin(alpha)

# adding the stagnation point to the figure
plt.scatter([xStagnation,xstag],[yStagnation,ystag],c='b',s=80,marker='o') """

# adding the dividing line to the figure
#if (alpha==0.0):
plt.contour(X,Y,psi,linewidths=2,linestyles='--')

plt.show()

Cp = 1.0-(u**2+v**2)/Uinf**2

plt.figure(figsize=(size,(yEnd-yStart)/(xEnd-xStart)*size))
plt.grid(True)
plt.xlabel('x',fontsize=16)
plt.ylabel('y',fontsize=16)
plt.xlim(xStart,xEnd)
plt.ylim(yStart,yEnd)
plt.contourf(X,Y,Cp,levels=np.linspace(-2.0,1.0,100),extend='both')
"""plt.scatter([xsource,xsink],[ysource,ysink],c='#CD2305',s=80,marker='o')

plt.scatter([xStagnation,xstag],[yStagnation,ystag],c='b',s=80,marker='o') """

# adding the dividing line to the figure
#if (alpha==0.0):
plt.contour(X,Y,psi,levels=[0.0],colors='#CD2305',linewidths=2,linestyles='--')
plt.show()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

def vectors_uniform(k):
    """Uniformly generates k vectors."""
    vectors = []
    for a in np.linspace(0, 2 * np.pi, k, endpoint=False):
        vectors.append(2 * np.array([np.sin(a), np.cos(a)]))
    return vectors

def visualize_transformation(A, vectors):
    """Plots original and transformed vectors for a given 2x2 transformation matrix A and a list of 2D vectors."""
    for i, v in enumerate(vectors):
        # Plot original vector.
        plt.quiver(0.0, 0.0, v[0], v[1], width=0.008, color="blue", scale_units='xy', angles='xy', scale=1,
                   zorder=4)
        plt.text(v[0]/2 + 0.25, v[1]/2, "v{0}".format(i), color="blue")

        # Plot transformed vector.
        tv = A.dot(v)
        plt.quiver(0.0, 0.0, tv[0], tv[1], width=0.005, color="magenta", scale_units='xy', angles='xy', scale=1,
                   zorder=4)
        plt.text(tv[0] / 2 + 0.25, tv[1] / 2, "v{0}'".format(i), color="magenta")
    plt.xlim([-6, 6])
    plt.ylim([-6, 6])
    plt.margins(0.05)

    # Plot eigenvectors
    plot_eigenvectors(A)
    plt.show()

def visualize_vectors(vectors, color="green", text="eigv"):
    """Plots all vectors in the list."""
    for i, v in enumerate(vectors):
        plt.quiver(0.0, 0.0, v[0], v[1], width=0.006, color=color, scale_units='xy', angles='xy', scale=1,
                   zorder=4)
        if text is not None:
            plt.text(v[0] / 2 + 0.25, v[1] / 2, "{}{}".format(text, i), color=color)

def plot_eigenvectors(A, **kwargs):
    """Plots all eigenvectors of the given 2x2 matrix A."""

    # DONE: Zad. 4.1. Oblicz wektory własne A. Możesz wykorzystać funkcję np.linalg.eig
    eigval, eigvec = np.linalg.eig(A)

    # DONE: Zad. 4.1. Upewnij się poprzez analizę wykresów, że rysowane są poprawne wektory własne (łatwo tu o pomyłkę).
    visualize_vectors(eigvec.T, **kwargs)

def EVD_decomposition(A):
    # DONE: Zad. 4.2. Uzupełnij funkcję tak by obliczała rozkład EVD zgodnie z zadaniem.

    eigval, eigvec = np.linalg.eig(A)

    K = eigvec
    L = np.diag(eigval)
    iK = np.linalg.inv(K)

    print('---')
    print('Obliczona macierz K:')
    print(K)

    print('Obliczona macierz L:')
    print(L)

    print('Obliczona macierz K^-1:')
    print(iK)

    print('Wynik mnozenia KLK^-1:')
    print(np.matmul(K, np.matmul(L, iK)))

    print('Oryginalna macierz:')
    print(A)

def plot_attractors(A, vectors, num_vectors: int = 20, num_iterations: int = 32):

    # DONE: Zad. 4.3. Uzupełnij funkcję tak by generowała wykres z atraktorami.

    # Helpers
    def normalize(v: np.array) -> np.array:
        """ Normalizes the vector (L2) """
        return v / np.linalg.norm(v, axis=0)

    def get_similarity_matrix(vectors_a: np.array, vectors_b: np.array) -> np.array:
        """ Returns cosine similarity matrix.
            Arguments should be column vectors.
            (vector_a -> colums, vector_b -> rows) """
        return np.array([[np.dot(v_a, v_b.T) for v_a in vectors_a.T] for v_b in vectors_b.T])

    def remove_similar_vectors(vectors: np.array) -> np.array:
        """ Removes very similar vectors from an array """
        to_delete = []
        for idx_a in range(vectors.shape[1] - 1):
            for idx_b in range(idx_a + 1, vectors.shape[1]):
                if np.isclose(vectors[:, idx_a], vectors[:, idx_b]).all():
                    to_delete.append(idx_b)
        return np.delete(vectors, to_delete, axis=1)

    def generate_oposed_vectors(vectors: np.array) -> np.array:
        """ Arguments should be column vectors. """
        output = []
        for vector in vectors.T:
            output.extend([vector, -vector])
        return np.array(output).T

    # Ground truth
    _, eigvec = np.linalg.eig(A)
    eigvec = remove_similar_vectors(eigvec)
    eigvec_and_opposed = generate_oposed_vectors(eigvec)

    # Define colors
    colors = []
    colors_mapping = ['red', 'orange', 'green', 'blue', 'magenta', 'cyan']

    # Plot properties
    plt.figure()
    plt.xlim([-2, 2])
    plt.ylim([-2, 2])
    plt.margins(0.05)
    plt.grid()

    # Generate vectors
    vectors = normalize(np.array(vectors_uniform(k=num_vectors)).T)
    initial_vectors = vectors.copy()

    # AAAA... Av
    for iter_idx in range(num_iterations):
        vectors = normalize(np.matmul(A, vectors))

    # Determine the the nearest attractor
    conv_sim_matrix = get_similarity_matrix(eigvec_and_opposed, vectors)
    colors = conv_sim_matrix.argmax(axis=1)

    # Plot initial vectors as quivers
    for v, color_idx in zip(initial_vectors.T, colors):
        color = colors_mapping[color_idx % len(colors)]
        plt.quiver(0.0, 0.0, v[0], v[1], width=0.003, color=color,
                scale_units='xy', angles='xy', scale=1, zorder=4)

    # Plot eigenvectors
    for color_idx, v in enumerate(eigvec_and_opposed.T):
        color = colors_mapping[color_idx % len(colors)]
        plt.quiver(0.0, 0.0, v[0], v[1], headwidth=5, headlength=8, width=0.008,
                color=color, scale_units='xy', angles='xy', scale=1, zorder=4)

    if conv_sim_matrix.max(axis=1).mean() < 0.99:
        # Plot black arrows
        for v in vectors.T:
            plt.quiver(0.0, 0.0, v[0], v[1], headwidth=5, headlength=8, width=0.008,
                    color='black', scale_units='xy', angles='xy', scale=1, zorder=4)

    plt.show()

def show_eigen_info(A, vectors):
    EVD_decomposition(A)
    visualize_transformation(A, vectors)
    plot_attractors(A, vectors)

if __name__ == "__main__":
    vectors = vectors_uniform(k=8)

    A = np.array([[2, 0],
                  [0, 2]])
    show_eigen_info(A, vectors)

    A = np.array([[-1, 2],
                  [2, 1]])
    show_eigen_info(A, vectors)

    A = np.array([[3, 1],
                  [0, 2]])
    show_eigen_info(A, vectors)

    A = np.array([[2, -1],
                  [1, 4]])
    show_eigen_info(A, vectors)


# coding: utf-8
'''
Created on 2015/09/14

@author: Kaoru
'''
import numpy as np
import matplotlib.pyplot as plt
##Gillespie Algorithm:
##Solve the simple case of a promoter expressing mRNA
##at a constant rate where the mRNA can in turn decay.

##Defining some parameters:
##new mRNA is produced approximately every 3.3 seconds
r = 20 #probability of mRNA production per unit time: [min^-1]

##life time of 1.5 minutes measured for the mRNA
gamma = 1/1.5 #probability of mRNA decay per mRNA and unit time: [min^-1]

##Iterations of the algorithm: for each iteration we will flip a coin.
MaxT = 500;

##Simulations start with no mRNA
m = np.empty(MaxT+1)
tau = np.empty(MaxT) #Time to the next reaction
m[0] = 0 #initial condition = 0

for i in range(MaxT):
  k1 = r
  k2 = m[i]*gamma
  k0  = k1 + k2 #Sum of all probabilities per unit time

  ##Determine the vector tau
  tau[i] = 1/k0 * np.log(1/np.random.rand()) #Uniform probability between 0 and 1

  ##Flip a coin to determine which one of the reactions will take place
  CoinFlip = np.random.rand()

  ##mRNA production / mRNA decay
  if CoinFlip <= k1/k0:
    m[i+1] = m[i]+1
  else:
    m[i+1] = m[i]-1

##Calculate the time axis
T = np.empty(MaxT+1)
T[0] = 0

for i in range(MaxT):
  T[i+1] = np.sum(tau[0:i])

##Plot results with the deterministic solution
plt.figure()
plt.plot(T,m)
plt.plot(T,r/gamma*(1-np.exp(-T*gamma)), "-k")
plt.xlabel("time (min)")
plt.ylabel("number of mRNA molecules")

plt.show(block=False)

##Distribution in steady state
r = 20
gamma = 1/1.5
MaxT = 10000

##Simulations start with no mRNA
m = np.empty(MaxT+1)
tau = np.empty(MaxT) #Time to the next reaction
m[0] = np.round(r/gamma) #Notice the different initial condition

for i in range(MaxT):
  k1 = r
  k2 = m[i]*gamma
  k0  = k1 + k2 #Sum of all probabilities per unit time

  ##Determine the vector tau
  tau[i] = 1/k0 #Average

  ##Flip a coin to determine which one of the reactions will take place
  CoinFlip = np.random.rand()

  ##mRNA production / mRNA decay
  if CoinFlip <= k1/k0:
    m[i+1] = m[i]+1
  else:
    m[i+1] = m[i]-1

##Check simulation to see if it follows a Poisson distribution
##Variance should be equal to the mean
##Weighting each mRNA occurence using the time over which it existed
MeanM = np.sum(m[0:-1]*tau)/np.sum(tau)
SecondMoment = np.sum(np.power(m[0:-1],2)*tau)/np.sum(tau)

VarianceM = SecondMoment - MeanM**2

##Compute the probability distribution
##Finding a certain number of mRNA molecules is related to the period of time
MaxmRNA = int(np.max(m))

p = np.zeros(MaxmRNA) #Observed number of mRNA molecules with length MaxmRNA

for i in range(MaxT): #Time that number existed until next reaction
  p[m[i]-1] = p[m[i]-1] + tau[i]
##Normalize the distribution
p = p/np.sum(p)

##Plot histogram of p with expected Poisson distribution
plt.figure()
plt.bar(range(1,MaxmRNA+1), p)
from scipy.stats import poisson
X = range(MaxmRNA)
Y = poisson.pmf(X, r/gamma)
plt.plot(X,Y,"-r")
plt.xlabel("number of mRNA molecules")
plt.ylabel("probability")
plt.legend(["Poisson distribution", "Simulation"])

plt.show()

"""
The agreement might not be perfect. At least, it is probably not as good
as the agreement in the figure associated with this computational
exploration in the chapter. The main reason for the disagreement is the
fact that we did not perform a coin flip to determine the values of tau
(unlike what we actually did to generate the figure). Instead we only took
its average for each iteration. A second cause of
disagreement is the number of iterations employed in our simulation. Both
of these limitations are easily solvable and are, in fact, taken into
account when this algorithm is used in the context of research.
"""


# noqa: D100
import numpy as np

def max_index(X):
    """Return the index of the maximum in a numpy array.

    Parameters
    ----------
    X : ndarray of shape (n_samples, n_features)
        The input array.

    Returns
    -------
    i : int
        The row index of the maximum.

    j : int
        The column index of the maximum.

    Raises
    ------
    ValueError
        If the input is not a numpy error or
        if the shape is not 2D.
    """
    i = 0
    j = 0

    # TODO
    if not isinstance(X, np.ndarray):
        raise ValueError("X should be a numpy array")
    if X.ndim != 2:
        raise ValueError(" The input shape of X should be 2D")
    L, C = np.shape(X)
    for line in range(0, L):
        for colonne in range(0, C):
            if X[line][colonne] > X[i][j]:
                i = line
                j = colonne
    return i, j

def wallis_product(n_terms):
    """Implement the Wallis product to compute an approximation of pi.

    See:
    https://en.wikipedia.org/wiki/Wallis_product

    XXX : write Parameters and Returns sections as above.

    """
    # XXX : The n_terms is an int that corresponds to the number of
    # terms in the product. For example 10000.
    pi = 2
    if n_terms > 0:
        for n in range(1, n_terms+1):
            pi *= (4*n**2)/(4*n**2 - 1)
    return pi


from typing import Tuple
import math

import numpy as np
import matplotlib.pyplot as plt

def rotate_point(
    alpha: float, x: float, y: float, cx: float, cy: float
) -> Tuple[float, float]:
    c = math.cos(alpha)
    s = math.sin(alpha)

    x -= cx
    y -= cy

    x_s = x * c - y * s
    y_s = x * s + y * c

    x_s += cx
    y_s += cy

    return (x_s, y_s)

def main() -> int:
    deg = 45.0
    rad = math.radians(deg)

    xs = [23.774, 28.774]
    ys1 = [-2.213, -2.213]
    ys2 = [-3.213, -3.213]

    plt.scatter(xs, ys1, c="blue")
    plt.scatter(xs, ys2, c="blue")

    cx = np.mean(xs)
    cy = np.mean([ys1[0], ys2[0]])

    _, ys1[0] = rotate_point(rad, xs[0], ys1[0], cx, cy)
    _, ys1[1] = rotate_point(rad, xs[1], ys1[1], cx, cy)
    xs[0], ys2[0] = rotate_point(rad, xs[0], ys2[0], cx, cy)
    xs[1], ys2[1] = rotate_point(rad, xs[1], ys2[1], cx, cy)
    plt.scatter(xs[0], ys1[0], c="red")
    plt.scatter(xs[1], ys1[1], c="red")
    plt.scatter(xs[0], ys2[0], c="red")
    plt.scatter(xs[1], ys2[1], c="red")

    plt.show()

    return 0

if __name__ == "__main__":
    main()


import numpy as np

"""
(x,y,z) = (r cos(phi) cos(theta), r cos(phi) sin(phi), r sin(phi))
r: fixed
theta in [0, 2 pi) <-- [0, 2)
phi in [-0.5 pi, 0.5 pi] <-- [-0.5, 0.5]
"""

pi=3.14159265358979

def make_sequence(init, div_num, delta):
    num = 0
    sequence = np.asarray([])
    value = init + delta
    sequence = np.append(sequence, value)
    while(num < div_num):
        num += 1
        value += 2.0 * delta
        sequence = np.append(sequence, value)

    return sequence

def make_lattice(lower_theta, upper_theta, divide_num_theta, \
                 lower_phi, upper_phi, divide_num_phi):
    delta_theta = (upper_theta - lower_theta) / (2.0 * divide_num_theta)
    sequence_theta = make_sequence(lower_theta, divide_num_theta, delta_theta)
    delta_phi = (upper_phi - lower_phi) / (2.0 * divide_num_phi)
    sequence_phi = make_sequence(lower_phi, divide_num_phi, delta_phi)

    return sequence_theta, sequence_phi

def parametrize_sphere(r, arg_theta, arg_phi):
    theta = arg_theta * pi
    phi = arg_phi * pi
    x = r * np.cos(phi) * np.cos(theta)
    y = r * np.cos(phi) * np.sin(theta)
    z = r * np.sin(phi)

    return x, y, z

def plot_3Dgraph(xs, ys, zs):
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D

    fig = plt.figure()
    # ax = fig.gca(projection='3d')
    # ax.plot(xs, ys, zs, label='sphere lattice')
    ax = Axes3D(fig)
    ax.plot(xs, ys, zs, "x", label='sphere lattice')
    ax.legend()
    plt.show()

def write_data_file(xs, ys, zs):
    import csv

    with open("sphere_plot.dat", "w", newline="") as f:
        writer = csv.writer(f, delimiter="\t")
        for x, y, z, in zip(xs, ys, zs):
            writer.writerow([x, y, z])

if __name__ == "__main__":
    r = 100.0
    lower_theta = 0.0
    upper_theta = 2.0
    divide_num_theta = 50
    lower_phi = -0.5
    upper_phi = 0.5
    divide_num_phi = 25

    thetaseq, phiseq = \
    make_lattice(lower_theta, upper_theta, divide_num_theta, \
                 lower_phi, upper_phi, divide_num_phi)

    xseq = np.asarray([])
    yseq = np.asarray([])
    zseq = np.asarray([])

    x = 0.0
    y = 0.0
    z = 0.0

    for theta in thetaseq:
        for phi in phiseq:
            x, y, z = parametrize_sphere(r, theta, phi)
            xseq = np.append(xseq, x)
            yseq = np.append(yseq, y)
            zseq = np.append(zseq, z)

    # plot_3Dgraph(xseq, yseq, zseq)
    write_data_file(xseq, yseq, zseq)


import math
import numpy as np
try:
  import cupy
  import optimized_gpu_tools
  import Q_inner_product
  import SphericalHarmonics_gpu
  xpy_default=cupy
except:
  print(' no cupy')
  import numpy as cupy
  optimized_gpu_tools=None
  Q_inner_product=None
  xpy_default=np

_coeff2m2 = math.sqrt(5.0 / (64.0 * math.pi))
_coeff2m1 = math.sqrt(5.0 / (16.0 * math.pi))
_coeff20 = math.sqrt(15.0 / (32.0 * math.pi))
_coeff2p1 = math.sqrt(5.0 / (16.0 * math.pi))
_coeff2p2 = math.sqrt(5.0 / (64.0 * math.pi))

def SphericalHarmonicsVectorized_orig(
        lm,
        theta, phi,
        xpy=cupy, dtype=np.complex128,
    ):
    """
    Compute spherical harmonics Y_{lm}(theta, phi) for a given set of lm's,
    thetas, and phis.

    Parameters
    ----------
    lm : array_like, shape = (n_indices, 2)

    theta : array_like, shape = (n_params,)

    phi : array_like, shape = (n_params,)

    Returns
    -------
    Ylm : array_like, shape = (n_params, n_indices)
    """
    l, m = lm.T

    n_indices = l.size
    n_params = theta.size

    Ylm = xpy.empty((n_params, n_indices), dtype=dtype)

    # Ensure coefficients are on the board if using cupy.
    c2m2 = xpy.asarray(_coeff2m2)
    c2m1 = xpy.asarray(_coeff2m1)
    c20 = xpy.asarray(_coeff20)
    c2p1 = xpy.asarray(_coeff2p1)
    c2p2 = xpy.asarray(_coeff2p2)
    imag = xpy.asarray(1.0j)

    # Precompute 1 +/- cos(theta).
    cos_theta = xpy.cos(theta)
    one_minus_cos_theta = (1.0 - cos_theta)
    one_plus_cos_theta = xpy.add(1.0, cos_theta, out=cos_theta)
    del cos_theta

    # Precompute sin(theta).
    sin_theta = xpy.sin(theta)

    for i, m_i in enumerate(m):
        if m_i == -2:
            Ylm[...,i] = (
                c2m2 *
                xpy.square(one_minus_cos_theta) *
                xpy.exp(imag * m_i * phi)
            )
        elif m_i == -1:
            Ylm[...,i] = (
                c2m1 *
                xpy.multiply(sin_theta, one_minus_cos_theta) *
                xpy.exp(imag * m_i * phi)
            )
        elif m_i == 0:
            Ylm[...,i] = (
                c20 *
                xpy.square(sin_theta)
            )
        elif m_i == +1:
            Ylm[...,i] = (
                c2p1 *
                xpy.multiply(sin_theta, one_plus_cos_theta) *
                xpy.exp(imag * m_i * phi)
            )
        elif m_i == +2:
            Ylm[...,i] = (
                c2p2 *
                xpy.square(one_plus_cos_theta) *
                xpy.exp(imag * m_i * phi)
            )
        else:
            Ylm[...,i] = xpy.nan

    return Ylm


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

def my_conv2d(g_kernel, image, stride, padding):
        g_kernel = np.flipud(np.fliplr(g_kernel))
        output_img_wshape = int((image.shape[0]-g_kernel.shape[0]+2*padding)/stride + 1)
        output_img_hshape = int((image.shape[1]-g_kernel.shape[1]+2*padding)/stride + 1)
        output = np.zeros([output_img_wshape, output_img_hshape, image.shape[2]])
        # Add zero padding to the input image
        image_padded = np.zeros((
                image.shape[0] + padding*2,
                image.shape[1] + padding*2,
                image.shape[2]))
        # import pdb; pdb.set_trace()
        image_padded[padding:-padding, padding:-padding, :] = image  # fencing
        for ch in range(0, image.shape[2]):
            print(f"In progress: convolution of channel no {ch+1}")
            for y in range(image.shape[1]):
                if stride*y <= image.shape[1]:
                    for x in range(image.shape[0]):
                        if stride*x <= image.shape[0]:
                            output[x, y, ch] = (
                                g_kernel*image_padded[(stride*x):(stride*x+g_kernel.shape[0]),
                                                      (stride*y):(stride*y+g_kernel.shape[1]),
                                                      ch]).sum()
                        continue
                continue
        return output

def str2bool(v):
    if v.lower() in ("yes", "true", "t", "y", "1"):
        return True
    elif v.lower() in ("no", "false", "f", "n", "0"):
        return False
    else:
        raise argparse.ArgumentTypeError("Boolean value expected.")

def cmd_arguments(parser):
    """"
    Add model specific args to the parse
    Args:
        - parser : argparse.ArgumentParser() object
    """ ""
    group = parser.add_argument_group(
        title="Cmd Arguments",
        description="""
          These include an image filename, a padding, a stride
          and a desired  convolution kernel.
          They will be passed in as normal command line flags.
        """,
    )

    group.add_argument("--image", type=str, required=True)
    group.add_argument("--padding", type=int, required=True)
    group.add_argument("--stride", type=int, required=True)
    group.add_argument("--kernel", type=str, required=True)

    return group

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    cmd_arguments(parser)
    args = parser.parse_args()

    # read image
    im = plt.imread(args.image).astype(float)
    im = im/255   # normalise to 0-1, it's easier to work in float space
    print(f"Shape of the original image: {im.shape}")

    # set kernel type
    g_kernel = np.squeeze(np.asarray(np.matrix(args.kernel)))

    # conv it!
    output_img = my_conv2d(g_kernel, im, args.stride, args.padding)

    # show plots
    print(f"Shape of the conv image: {im.shape}")

    plt.subplot(1, 2, 1)
    plt.imshow(im)
    plt.title('Original Image')

    plt.subplot(1, 2, 2)
    plt.imshow(output_img)
    plt.title('Conv image')

    plt.show()


import numpy as np
import keras
from sklearn import datasets
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical

n_pts = 500
centers = [[-1, 1], [-1, -1], [1, -1], [1, 1], [0, 0]]
X, y = datasets.make_blobs(n_samples=n_pts, random_state = 123, centers=centers, cluster_std=0.4)

plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
plt.scatter(X[y==3, 0], X[y==3, 1])
plt.scatter(X[y==4, 0], X[y==4, 1])

print(y)
y_cat = to_categorical(y, 5)
print(y_cat)

model = Sequential()
model.add(Dense(5, input_shape=(2,), activation='softmax'))
model.compile(Adam(lr=0.1), 'categorical_crossentropy', metrics=['accuracy'])

#one hot encode output
history = model.fit(X, y_cat, verbose=1, batch_size = 50, epochs=100)

def plot_multiclass_decision_boundary(X, y, model):
    x_span = np.linspace(min(X[:,0]) - 1, max(X[:,0]) + 1)
    y_span = np.linspace(min(X[:,1]) - 1, max(X[:,1]) + 1)
    xx, yy = np.meshgrid(x_span, y_span)
    grid = np.c_[xx.ravel(), yy.ravel()]
    pred_func = model.predict_classes(grid)
    z = pred_func.reshape(xx.shape)
    plt.contourf(xx, yy, z)

plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
plt.scatter(X[y==3, 0], X[y==3, 1])
plt.scatter(X[y==4, 0], X[y==4, 1])

plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
plt.scatter(X[y==3, 0], X[y==3, 1])
plt.scatter(X[y==4, 0], X[y==4, 1])

x = -0.5
y = -0.5

point = np.array([[x, y]])
prediction = model.predict_classes(point)
plt.plot([x], [y], marker='o', markersize=10, color="yellow")
print("Prediction is: ", prediction)


import numpy as np
import cupy as cp
from cupy.linalg import det
from cupy.linalg import inv

class numerical_basics():

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

    def matrix_mult(self, matrix1,matrix2):
        """
        The matrix multiplication is perhaps the basic of Linear algebra.
        A simple rule which must be observed is that the number columns of Mat1 should be equal
        to number of rows Mat2.

        matrix1 = numpy array
        matrix2 = numpy array
        """
        cp.cuda.Device(self.device).use()

        if matrix1.shape[1]==matrix2.shape[0]:
            matrix1 = cp.array(matrix1)
            matrix2 = cp.array(matrix2)
            return matrix1.dot(matrix2)
        else:
            print ("matrix multiplication not possible as 'matrix1' columns is not same as rows of\
            'matrix2'")
            return None

    def cross_prod(self,v,w):
        """
        The cross product between two vectors, v and w, is written v×w.
        It is defined by v×w=∥v∥2∥w∥2sin(θ)n, where θ is the angle between the
        v and w (which can be computed from the dot product) and n is a vector
        perpendicular to both v and w with unit length (i.e., the length is one).
        The geometric interpretation of the cross product is a vector perpendicular
        to both v and w with length equal to the area
        enclosed by the parallelogram created by the two vectors.
        """
        cp.cuda.Device(self.device).use()
        if len(v)==len(w):
            v = cp.array(v)
            w = cp.array(w)
            return cp.cross(v, w)
        else:
            print ("Non equal in length vectors, cross product not possible")
            return None

    def calc_determinant(self,matrix1):
        """
        A square matrix is an n×n matrix; that is, it has the same number of rows as columns.
        The determinant is an important property of square matrices and is scalar in nature
        The determinant is denoted by det(M)
        matrix1 = numpy array
        """
        cp.cuda.Device(self.device).use()
        if matrix1.shape[0]==matrix1.shape[1]:
            matrix1 = cp.array(matrix1)
            return det(matrix1)
        else:
            print ("The matrix is non-square")
            return None

    def calc_inverse(self,matrix1):
        """
        The inverse of a square matrix M is a matrix of the same size, N, such that M⋅N=I.
        To find the inverse of the matrix the determinat should be non-zero
        """
        cp.cuda.Device(self.device).use()
        matrix1 = cp.array(matrix1)

        return inv(matrix1)


import math
import numpy as np
import torch
from scipy.spatial.distance import cdist

__all__ = ("naive_gaussian_kernel", "naive_sigmoid_kernel", "naive_laplacian_kernel",
           "naive_linear_kernel", "naive_polynomial_kernel", "naive_matern_kernel",
           "naive_diff_gaussian_kernel", "naive_diff_sigmoid_kernel", "naive_diff_laplacian_kernel",
           "naive_diff_linear_kernel", "naive_diff_polynomial_kernel", "naive_diff_matern_kernel",)

def naive_diff_gaussian_kernel(X1, X2, sigma):
    pairwise_dists = torch.cdist(X1 / sigma, X2 / sigma, p=2).square()
    return torch.exp(-0.5 * pairwise_dists)

def naive_diff_laplacian_kernel(X1, X2, sigma):
    # http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/#laplacian
    pairwise_dists = torch.cdist(X1 / sigma, X2 / sigma, p=2)
    return torch.exp(-pairwise_dists)

def naive_diff_linear_kernel(X1, X2, beta, gamma):
    return naive_linear_kernel(X1, X2, beta, gamma)

def naive_diff_sigmoid_kernel(X1, X2, gamma, beta):
    out = X1 @ X2.T
    return torch.tanh(out * gamma + beta)

def naive_diff_polynomial_kernel(X1, X2, gamma, beta, degree):
    out = X1 @ X2.T
    return torch.pow(out * gamma + beta, degree)

def naive_diff_matern_kernel(X1, X2, sigma, nu):
    pairwise_dists = torch.cdist(X1 / sigma, X2 / sigma, p=2)

    if nu == 0.5:
        K = torch.exp(-pairwise_dists)
    elif nu == 1.5:
        K = pairwise_dists * math.sqrt(3)
        K = (1. + K) * torch.exp(-K)
    elif nu == 2.5:
        K = pairwise_dists * math.sqrt(5)
        K = (1. + K + K ** 2 / 3.0) * torch.exp(-K)
    elif nu == np.inf:
        K = torch.exp(-pairwise_dists ** 2 / 2.0)
    return K

def naive_gaussian_kernel(X1, X2, sigma):
    pairwise_dists = cdist(X1, X2, 'sqeuclidean')
    return np.exp(-pairwise_dists / (2 * sigma ** 2))

def naive_laplacian_kernel(X1, X2, sigma):
    pairwise_dists = cdist(X1, X2, 'euclidean')
    return np.exp(-pairwise_dists / sigma)

def naive_linear_kernel(X1, X2, beta, gamma):
    return beta + gamma * X1 @ X2.T

def naive_sigmoid_kernel(X1, X2, alpha, beta):
    out = X1 @ X2.T
    return np.tanh(out * alpha + beta)

def naive_polynomial_kernel(X1, X2, alpha, beta, degree):
    out = X1 @ X2.T
    return np.power(out * alpha + beta, degree)

def naive_matern_kernel(X1, X2, sigma, nu):
    pairwise_dists = cdist(X1 / sigma, X2 / sigma, 'euclidean')

    if nu == 0.5:
        K = np.exp(-pairwise_dists)
    elif nu == 1.5:
        K = pairwise_dists * math.sqrt(3)
        K = (1. + K) * np.exp(-K)
    elif nu == 2.5:
        K = pairwise_dists * math.sqrt(5)
        K = (1. + K + K ** 2 / 3.0) * np.exp(-K)
    elif nu == np.inf:
        K = np.exp(-pairwise_dists ** 2 / 2.0)
    return K


import math, numpy as np
from sympy import sympify, solve, solve_poly_system, symbols
from re import sub
import grtoolkit.Math.sympyInterface
from grtoolkit.Math.sympyInterface import preSympifySub

def magnitude(*args):
    '''Square root of sum'''
    square_sum = 0
    for arg in args:
        square_sum += arg**2
    return math.sqrt(square_sum)

def roundSig(x, sig=5):
    '''Round numbers to specified significant figures'''
    if x == 0:
        return 0
    return round(x, sig - int(math.floor(math.log10(abs(x)))) - 1)

def delta(p1,p2):
    '''Returns absolute difference between two numbers'''
    return abs(p2-p1)

def softmax(L):
    '''
    Function that takes as input a list of numbers,
    and returns the list of values given by the softmax function.

    Example:
    Input [1,2,3]
    Output [e**1/(e**1 + e**2 + e**3),
            e**2/(e**1 + e**2 + e**3)
            e**3/(e**1 + e**2 + e**3)]'''
    return [np.exp(x)/sum(np.exp(L)) for x in L]

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

def cross_entropy(Y, P):
    '''
    Sample inputs: Y=[1,1,0] and P=[0.8,0.7,0.1] Equal 0.69
    Where,
    Y is list of whether events are on or off
    P is list of probabilities if events are on

    Formula is sum of all cases of entropy if event is active plus entropy if event is inactive

    Events with high probabilities have low cross entopy
    Events with low proabilities have high cross entropy

    OR

    High cross entropy means low probability
    Low cross entropy means high probability
    '''

    Y = np.float_(Y)
    P = np.float_(P)
    return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))

def algebraSolve(expr, solve_for,**kwargs):
    """
    Solves single algebraic string equation.

    Usage: algebraSolve("x**2 + 3*x - 1/2 + y", "x", y=24)

    Returns: [ans]
    """
    expr = preSympifySub(expr,**kwargs)
    expr=sympify(expr)
    #GENERATE VARIABLE SYMBOLS FROM EXPRESSION
    # if expr.free_symbols: # if expr.freesymbols is not empty
    if not isinstance(expr,bool):
        for sym in expr.free_symbols:
            exec(f"{sym.name}=symbols('{sym.name}')")
            #MAKE solve_for STRING EQUAL REQUIRED EXPRESSION VARIABLE SYMBOL
            if solve_for == sym.name:
                solve_for = sym
            # SUBSTITUTE KARGs FOR EXPRESSION SYMBOLS
            for k, v in kwargs.items():
                if k == sym.name:
                    expr=expr.subs(k,v)
        return solve(expr, solve_for)
    else:
        return expr #if expression not possible, just return expression

def solveEqs(eq, find, printEq=True, **kwargs):
    """
    Solves multiple equations independently in search for variable.
    Allows substitution via kwargs.
    In other words, algebraSolve() on multiple equations at once.

    Usage:
        eq = list()
        eq.append("Eq(a_tan, dv/dt)")
        eq.append("Eq(a_tan, r*dw/dt)")
        eq.append("Eq(a_tan, r*alpha)")
        solveEqs(eq, "a_tan", printEq=printEq, **kwargs)

    Returns:
        [
            ans1,
            ans2,
            ans3
        ]
    """
    #CREATE SET OF ARGUMENTS SUPPLIED TO FUNCTION
    original_eq_ref = eq.copy()
    availSet = {find}
    for k,v in kwargs.items():
        availSet.add(k)
        v=str(v)
    eq = [preSympifySub(expr,**kwargs) for expr in eq]
    eq = [sympify(expr) for expr in eq]
    freesym = [expr.free_symbols for expr in eq]
    freevar = []
    solution = list()
    #CONVERT LIST OF SETS OF SYMBOLS INTO LIST OF SETS OF STR
    for freeset in freesym:
        tempset = set()
        for sym in freeset:
            tempset.add(sym.name)
        freevar.append(tempset)
    unknowns = [freeset-availSet for freeset in freevar]            # Eliminate variables requested in function arguments: find and knowns
    unknowns_count = [len(unknownset) for unknownset in unknowns]   # How many unknowns are left in each equation?
    find_avail = [find in freevarset for freevarset in freevar]     # Is the variable in question available in this equation?
    index = list(range(len(unknowns_count)))
    zipper = list(zip(index, unknowns_count, find_avail))

    for zippedList in zipper:
        solution.append(algebraSolve(eq[zippedList[0]],find, **kwargs))

    printEquationsSolution(original_eq_ref, solution, find, printEq=printEq) #print Equations and Solutions to console
    return solution

def printEquations(eq):
    # print("Equations:\n")
    for equation in eq:
        print(equation)

def printEquationsSolution(eq, solution, find, printEq=True):
    print("\n")
    if printEq:
        for i in range(len(eq)):
            print(
f"""
OPTION {i}
From Equation: {eq[i]}:
{find} = {str(solution[i])}"""
)
        # print("Equations"), printEquations(eq), print("\n")
        # print("Solutions"), printEquations(solution), print("\n")

def solveSimultaneousEqs(eq):
    '''
    Solve Simultaneous Equations
    Finite solutions only. No expression based answers.

    Usage:
    eq = list()
    eq.append("-x/10-x/5-6-(x-y)/2")
    eq.append("y/4-3-6-(x-y)/2")
    solveSimultaneousEqs(eq)
    '''

    eq = [sympify(expr) for expr in eq]
    freesym = [expr.free_symbols for expr in eq]
    freevar = list()
    for freeset in freesym:
        for sym in freeset:
            freevar.append(sym)
    freevar=list(set(freevar))
    for sym in freevar:
        exec(f"{sym.name}=symbols('{sym.name}')")

    solutions = solve_poly_system(eq, freevar)
    solutionList = list()

    freevar = [str(var) for var in freevar]

    for possibility in solutions:
        possibility = [float(num) for num in possibility]
        solutionList.append(dict(zip(freevar,possibility)))
        return solutionList

def sourceFreeSeriesEquivalentCircuit(R, C, L, find="i", printEq=True, **kwargs):
    alpha = R / (2 * L)
    w0 = 1 / (L*C)**(1/2)
    s1 = -alpha + (alpha**2 - w0**2)**(1/2)
    s2 = -alpha - (alpha**2 - w0**2)**(1/2)
    # j = (-1)**(1/2)
    # j = complex(1)

    eq = list()
    if alpha > w0:
        eq.append("Eq(i, A1*exp(s1*t) + A2*exp(s2*t))")
    if alpha == w0: #C=4*L/R**2
        eq.append("Eq(i, (A2 + A1*t)*exp(-alpha*t))")
    if alpha < w0:
        # wd = (w0**2 - alpha**2)**(1/2)
        # B1 = A1 + A2
        # B2 = j*(A1 - A2)
        eq.append("Eq(i, exp(-alpha*t)*(B1*cos(wd*t) + B2*sin(wd*t)))")

    kwargs.update({"wd": "(w0**2 - alpha**2)**(1/2)", "alpha": alpha, "w0":w0, "s1":s1, "s2":s2, "B1": "A1 + A2", "B2": "j*(A1 - A2)"})

    return solveEqs(eq,find="i", printEq=printEq, **kwargs)

if __name__ == "__main__":
    sourceFreeSeriesEquivalentCircuit(R=5,C=4*1/5**2,L=1)

    # Eq(i, exp(-(2.5)*t)*((A1 + A2)*cos((((10.0)**2 - (2.5)**2)**(1/2))*t) + (j*(A1 - A2))*sin((((10.0)**2 - (2.5)**2)**(1/2))*t))


import os, sys
sys.path.insert(0, os.path.join(os.pardir, 'src'))
from varform1D import solver
import sympy as sym
x, b = sym.symbols('x b')
f = 1

# Compute basis functions and their derivatives
N = 4
psi = {0: [x**(i+1) for i in range(N+1)]}
psi[1] = [sym.diff(psi_i, x) for psi_i in psi[0]]

# Galerkin

def integrand_lhs(psi, i, j):
    return psi[1][i]*psi[1][j]

def integrand_rhs(psi, i):
    return -f*psi[0][i]

Omega = [0, 1]

u, c = solver(integrand_lhs, integrand_rhs, psi, Omega,
              verbose=True, symbolic=True)
print('Galerkin solution u:', sym.simplify(sym.expand(u)))

# Least squares
psi = {0: [x**(i+2) for i in range(N+1)]}
psi[1] = [sym.diff(psi_i, x) for psi_i in psi[0]]
psi[2] = [sym.diff(psi_i, x) for psi_i in psi[1]]

def integrand_lhs(psi, i, j):
    return psi[2][i]*psi[2][j]

def integrand_rhs(psi, i):
    return -f*psi[2][i]

Omega = [0, 1]

u, c = solver(integrand_lhs, integrand_rhs, psi, Omega,
              verbose=True, symbolic=True)
print('solution u:', sym.simplify(sym.expand(u)))


# function
# This function calculates buckling load using a pre integration formulation

import sympy as sp
import numpy as np
from scipy import linalg
import matplotlib.pyplot as plt
from ._plot_surface_function import _plot_surface
from sympy import sin, cos, pi, Piecewise, Ne
from ._pre_integrated_component import integration_KGx_component, integration_K_component

def buckling_load(a, b, D, n = 3, shape_plot = False , eig = False):
    '''
    ===========================================
    The function

    This function calculates the critical buckling load of a composite plate.
    Some formulation characteristics are:
        - The plate is considered simple supported
        - Distributed load entry in the X direction
        - Laminate must be symmetric
        - It uses Rayleigh Ritz formulation and shape function as series of sine waves
        - Pre integration of the KGx and K matrices was added, so the function is much much faster
        - Available after release 0.1.0
    ===========================================

    Use

    buckling_load(a, b, D, n = 3, shape_plot = False , eig = False)
    It returns the Nx load of the plate (float)

        - a ==> is plate lengh, paralell to the X direction and paralel to the load
        - b ==> is the plate width, paralel to the Y direction, where the load is distributed
        - D ==> bending matrix of the laminate
        - n ==> number of shape functions to be used (default = 3)
        - shape_plot ==> display the critical buckling shape of the plate (default = False)
        - eig ==> the function a dictionary with Nx, eigen values and eigen vectors  (default = False)

    ===========================================

    References:
        - JONES, M. Robert. Mechanics of Composite Materials. Taylor & Francis: 2nd ed 1999.
        - Analysis and Design of composite structures. Class notes. ITA 2020.
        - CASTRO, G. P. Saullo. Semi-Analytical Tools for the Analysis of Laminated Composite Cylindrical
    and Conical Imperfect Shells under Various Loading and Boundary Conditions. Clausthal University of Technology, 2014.

    '''
    #========================= KGX MATRIX CONSTRUCTION ============================
    # Symbolic values and variables
    i,j,k,l = sp.symbols(['i','j','k','l'],positive=True)
    x,y = sp.symbols(['x','y'])

    # Shape function components
    component1 = sp.sin((i)*sp.pi*x/a)*sp.sin((j)*sp.pi*y/b)
    component2 = sp.sin((k)*sp.pi*x/a)*sp.sin((l)*sp.pi*y/b)
    #component1 = x**(i)*y**(j)*(x-a)*(y-b)
    #component2 = x**(k)*y**(l)*(x-a)*(y-b)
    dx_component1 = sp.diff(component1,x)
    dx_component2 = sp.diff(component2,x)
    #integrated_component =  sp.integrate(sp.integrate(dx_component1*dx_component2,(x,0,a)),(y,0,b))
    integrated_component = eval(integration_KGx_component)

    # Construction of KGx
    component_list = []
    KGx = sp.zeros(n**2)

    for i_index in range(n):
        for j_index in range(n):
            for k_index in range(n):
                for l_index in range(n):
                    #print(str(i_index+1)+str(j_index+1)+str(k_index+1)+str(l_index+1))
                    cur_comp = integrated_component.subs(i,1+i_index).subs(j,1+j_index).subs(k,1+k_index).subs(l,1+l_index)
                    component_list.append(cur_comp)

    KGx = np.array(component_list).reshape(n**2,n**2).astype(float)

    #ToDo
    #Built KGy and KGxy functions

    #========================= K MATRIX CONSTRUCTION ============================
    # Unpack D
    [D11,D12,D13],\
    [D21,D22,D23],\
    [D31,D32,D33] = D

    # Shape function components (used in Nww constructions)
    dxx_component1 = sp.diff(component1,x,x)
    dxx_component2 = sp.diff(component2,x,x)
    dyy_component1 = sp.diff(component1,y,y)
    dyy_component2 = sp.diff(component2,y,y)
    _2dxy_component1 = 2*sp.diff(component1,x,y)
    _2dxy_component2 = 2*sp.diff(component2,x,y)
    component_K = _2dxy_component2*(D33*_2dxy_component1 + D13*dxx_component1 + D23*dyy_component1)\
                + dxx_component2*(D31*_2dxy_component1 + D11*dxx_component1 + D21*dyy_component1) \
                + dyy_component2*(D32*_2dxy_component1 + D12*dxx_component1 + D22*dyy_component1)
    #integrated_component = sp.integrate(sp.integrate(component_K,(x,0,a)),(y,0,b))
    integrated_component = eval(integration_K_component)

    #Construction of K
    component_list_K = []
    K = sp.zeros(n**2)

    for i_index in range(n):
        for j_index in range(n):
            for k_index in range(n):
                for l_index in range(n):
                    #print(str(i_index+1)+str(j_index+1)+str(k_index+1)+str(l_index+1))
                    cur_comp = integrated_component.subs(i,1+i_index).subs(j,1+j_index).subs(k,1+k_index).subs(l,1+l_index)
                    component_list_K.append(cur_comp)

    K = np.array(component_list_K).reshape(n**2,n**2).astype(float)

    #==================SOLVING THE EIGENPROBLEM AND FINDING THE RESULTS ============
    #eigenproblem
    eig_values, eig_vectors = linalg.eig(K,KGx)

    #finding the min eigenvalue
    min_eigen_value = min((eig_values.real**2)**(1/2))
    Nx = min_eigen_value*b

    #Finding the corresponding C vector
    i = np.where(eig_values == min_eigen_value)
    C_vector = eig_vectors[::,i[0]].real

    #complete shape function
    Nw = [[]]
    for i in range(n):
        for j in range(n):
            Nw[0].append(sp.sin((i+1)*sp.pi*x/a)*sp.sin((j+1)*sp.pi*y/b))
    Nw_values = sp.Matrix(Nw)

    #ploting function
    if shape_plot:
        _plot_surface(a, b, C_vector, Nw_values)

    #Builting the result dictionary
    result = {'Nx':Nx}
    if eig:
        result['eigen values'] = eig_values
        result['eigen vectors'] = eig_vectors

    return result

import numpy,from scipy,import sympy,from sympy


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

#http://www.johnwittenauer.net/machine-learning-exercises-in-python-part-1/
#şimdi burada tek değişken üzerinden linear_regression problemini çözecez

alpha=0.01
iters=1000

#aşağısı
def costFunction (x,y,theta):
    inner =np.power(((x*theta.T)-y),2)
    return np.sum(inner)/(2*len(x))

def gradientDescent(x,y,theta,alpha,iters):
    temp=np.matrix(np.zeros(theta.shape))
    parameters=int(theta.ravel().shape[1])
    cost=np.zeros(iters)

    for i in range(iters):
        error=(x*theta.T)-y
        for j in range(parameters):
            term=np.multiply(error,x[:,j])
            temp[0,j]=theta[0,j]-((alpha/len(x))*np.sum(term))
        theta=temp
        cost[i]=costFunction(x,y,theta)
    return theta,cost

path=os.getcwd()+'/LinearRegression/ex1data1.txt'
data =pd.read_csv(path,header=None,names=['Population','Profit'])
head =data.head()
describe=data.describe()

print(describe)

#data.plot(kind='scatter',x='Population',y='Profit',figsize=(12,8))

data.insert(0,'Ones',1)

cols=data.shape[1]
x=data.iloc[:,0:cols-1]
y=data.iloc[:,cols-1:cols]

x=np.matrix(x.values)
y=np.matrix(y.values)
theta=np.matrix(np.array([0,0]))

g,cost=gradientDescent(x,y,theta,alpha,iters)

print("Cost:%1.5f"%costFunction(x,y,theta))
print(g)

print(costFunction(x,y,g))

#yaptığımız işi vizualize edeyoz
x=np.linspace(data.Population.min(),data.Population.max(),100)
f=g[0,0]+(g[0,1]*x)

fig,ax=plt.subplots(figsize=(12,8))
ax.plot(x,f,'r',label='Preditcion')
ax.scatter(data.Population,data.Profit,label='Training Data')
ax.legend(loc=2)
ax.set_xlabel('Population')
ax.set_ylabel('Profit')
ax.set_title('Predicted Profit vs. Population Size')

plt.show()

fig,ax=plt.subplots(figsize=(12,8))
ax.plot(np.arange(iters),cost,'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()


from johansen_test import coint_johansen
import pandas as pd
import matplotlib.pyplot as plt
from functions import *
from numpy.matlib import repmat
import numpy as np

# Implementation of the second part of example 3.1 in Ernest Chang's
# book Algorithmic trading : winning strategies and their rationale.
#
# log(Price) Spread

if __name__ == "__main__":

    #import data from CSV file
    root_path = 'C:/Users/javgar119/Documents/Python/Data/'
    # the paths
    # MAC: '/Users/Javi/Documents/MarketData/'
    # WIN: 'C:/Users/javgar119/Documents/Python/Data'
    filename = 'GLD_USO_daily2.csv'
    full_path = root_path + filename
    data = pd.read_csv(full_path, index_col='Date')

    y_ticket = 'USO'
    x_ticket = 'GLD'

    y = data[y_ticket]
    x = data[x_ticket]

    # lookback period for calculating the dynamically changing
    lookback = 20
    modelo = pd.ols(y=np.log(y), x=np.log(x), window_type='rolling', window=lookback)
    data = data[lookback-1:]
    betas = modelo.beta

    # calculate the number of units for the strategy in the form
    # y-beta*x
    yport = pd.DataFrame(np.log(data[y_ticket]) - (betas['x'] * np.log(data[x_ticket])))

    moving_mean = pd.rolling_mean(yport, window=lookback)
    moving_std = pd.rolling_std(yport,window=lookback)
    # the number of units of the syntetic portfolio is given by the
    # negative value of z-score
    numunits = pd.DataFrame(-(yport - moving_mean) / moving_std)

    # compute the $ position for each asset
    AA = pd.DataFrame(repmat(numunits,1,2))
    BB = pd.DataFrame(-betas['x'])
    BB['ones'] = np.ones((len(betas)))
    position = multiply(multiply(AA, BB), data)

    # compute the daily pnl in $$
    pnl = sum(multiply(position[:-1], divide(diff(data,axis = 0), data[:-1])),1)

    # gross market value of portfolio
    mrk_val = pd.DataFrame.sum(abs(position), axis=1)
    mrk_val = mrk_val[:-1]

    # return is P&L divided by gross market value of portfolio
    rtn = pnl / mrk_val

    # compute performance statistics
    sharpe = (np.sqrt(252)*np.mean(rtn)) / np.std(rtn)
    APR = np.prod(1+rtn)**(252/len(rtn))-1

    ##################################################
    # print the results
    ##################################################
    print('Price spread Sharpe: {:.4}'.format(sharpe))
    print('Price Spread APR: {:.4%}'.format(APR))

    #*************************************************
    # plotting the chart
    #*************************************************
    #plot of numunits
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(cumsum(rtn))
    ax.set_title('{}-{} log(Price) Spread Acum Return'.format(x_ticket, y_ticket))
    ax.set_xlabel('Data points')
    ax.set_ylabel('acumm rtn')
    ax.text(1000, 0, 'Sharpe: {:.4}'.format(sharpe))
    ax.text(1000, -0.03, 'APR: {:.4%}'.format(APR))

    plt.show()


from __future__ import division
from sympy import *

init_printing()
x, y = symbols('x y') #define x e y como variáveis simbólicas.

def f(x):
  return ((pow(x, 3) - 5 * pow(x, 2) + x + 3))

def drv_f(a):
  return(diff(f(x), x).subs(x, a)) #calculando a derivada da funcao definida no ponto a

def NewtonRephson(a, b):

  while((f(a)) * (f(b)) > 0):
    print(f'\nErro!\nCom os valores {a} e {b}, nao tem-se raizes para a funcao!')
    a = float(input('\nDigite outro valor para a: '))
    b = float(input('Digite outro valor para b: '))

  #der_func = der_f(a) calculando a derivada da funcao
  #print(der_func)

  erro = 0.01 #Margem de tolerancia
  i = 0

  while True:
    i += 1
    x = a - (f(a) / drv_f(a))
    xn = abs(f(x))
    if xn < erro:
      break
    a = x

  print(f'\n\nA raiz da funcao f(x) = x^3 - 5x^2 + x + 3 eh {x:.4f} com tolerancia de {erro:.2f} em {i} iteracao(oes)')


"""
Finds sum of all primes below limit_value using the prime sieve
Author: Juan Rios
"""
import math
import numpy as np

# Finds sum of prime factors below limit_value
def prime_factors_sum(limit_value):
    primes = [2]
    values = np.ones(shape=(1,limit_value))
    values[0,0] = 0
    values[0,1] = 0
    values[0,0::2] = 0

    sum_primes = 2
    current_value = 3
    while (current_value<limit_value):
        if values[0,current_value]==1:
            values[0,0::current_value] = 0
            sum_primes += current_value
        current_value += 2
    return sum_primes

if __name__ == "__main__":
    limit_value = 2000000
    print('The sum of primes below {0} is {1}'.format(limit_value, prime_factors_sum(limit_value)))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np

def batch_softmax(x):

    return np.exp(x) / np.sum(np.exp(x), axis=1).reshape(-1,1)

def cross_entropy_loss(logits, labels):
    '''
    logits:[B，N]
    labels:[B，N]

    '''
    logits = batch_softmax(logits)

    loss = -labels * np.log(logits)

    return loss.sum() / logits.shape[0]

if __name__=="__main__":

    x = np.random.randn(3,4)
    y = np.zeros_like(x)

    y[0][0] = 1
    y[1][1] = 1
    y[2][2] = 1
    loss = cross_entropy_loss(x, y)
    print(loss)


import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft

def GCD(A, B):
    """ Greatest common divisor """
    while B:
        A, B = B, A % B
    return A

def LCM(A, B):
    """ Lowest common denominator """
    return A * B / GCD(A, B)

Fs = 1200   # Sample frequency
f1 = 130    # Frequency of 1st component
f2 = 288    # Frequency of 2nd component

# Number of sample points
N = int(LCM(Fs / GCD(Fs, f1), Fs / GCD(Fs, f2)))    # 600

T = 1.0 / Fs    # Sample spacing
x = np.linspace(T, N * T, N)

# I Component
y1 = np.sin(f1 * 2.0 * np.pi * x)

# II Component
y2 = np.sin(f2 * 2.0 * np.pi * x)

# Complete Signal
y = y1 + y2
yf = fft(y)

xf = np.linspace(0.0, 1.0 / (2.0 * T), N // 2)

# Display FFT
plt.figure()
plt.stem(xf, 2.0 / N * np.abs(yf[0:N//2]))
plt.title('FFT Spectrum\n$x(t) = \sin(2\pi130t) + \sin(2\pi288t)$')
plt.xlabel('Frequency [Hz]')
plt.grid()
plt.show()


import numpy as np
from scipy.stats import norm

def delta(flag="C", s, k, t, r, v):
    d1 = (np.log(s/k) + (r+v*v/2)*t) / (v*np.sqrt(t))

    if flag == "C":
        return norm.cdf(d1)

    return norm.cdf(-d1)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from numpy.random import seed

class perceptron(object):
    ''' perceptron classifier.
    Parameters
    -----------------------
    eta: float
        learning rate (between 0.0 and 1.0)
    n_iter: int
        passes over the training dataset.

    Attributes
    -----------------------
    w_: ld-array
        weights after fitting.
    errors_: list
        number of misclassifications in every epoch.
    '''
    def __init__(self, eta = 0.01, n_iter = 10):
        self.eta = eta
        self.n_iter = n_iter

    def fit(self, X, y):
        '''Fit training data.

        Parameters
        -----------------------
        X: {array-like}, shape = [n_samples, n_features]
            training vectors, where
            n_samples is the number of samples and
            n_features is the number of features.
        y: array-like, shape = [n_samples]
            target values.

        Returns
        -----------------------
        self: object
        '''

        self.w_ = np.zeros(1 + X.shape[1])
        self.errors_ = []

        for _ in range(self.n_iter):
            errors = 0
            for xi, target in zip(X, y):
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] += update * xi
                self.w_[0] += update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self

    def net_input(self, X):
        '''Calculate net input'''
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def predict(self, X):
        '''return class label after unit step'''
        return np.where(self.net_input(X) >= 0.0, 1, -1)

class adalineGD(object):
    '''adaptive linear neuron classifier.

    Parameters
    -----------------------
    eta: float
        learning rate (between 0.0 and 1.0)
    n_iter: int
        passes over the training dataset.

    Attributes
    -----------------------
    w_: 1d-array
        weights after fitting.
    errors_: list
        number of misclassifications in every epoch.

    '''
    def __init__(self, eta = .01, n_iter = 50):
        self.eta = eta
        self.n_iter = n_iter

    def fit(self, X, y):
        ''' fit training data.

        Parameters
        -----------------------
        X: {array - like}, shape = [n_samples, n_features]
            training vectors,
            where n_samples is the number of samples and
            n_features is the number of features.
        y: array-like, shape = [n_samples]
            target values.

        Returns
        -----------------------
        self: object
        '''
        self.w_ = np.zeros(1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            output = self.net_input(X)
            errors = (y - output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta * errors.sum()
            cost = (errors ** 2).sum() / 2.0
            self.cost_.append(cost)
        return self

    def net_input(self, X):
        ''' calculate net input '''
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def activation(self, X):
        ''' compute linear activation '''
        return self.net_input(X)

    def predict(self, X):
        ''' return class label after unit step'''
        return np.where(self.activation(X) >= 0.0, 1, -1)

class adalineSGD(object):
    '''adaptive linear neuron classifier.

    Parameters
    -----------------------
    eta: float
        learning rate (between 0.0 and 1.0)
    n_iter: int
        passes over the training dataset.

    Attributes
    -----------------------
    w_: 1d-array
        weights after fitting.
    errors_: list
        number of misclassifications in every epoch.
    shuffle: bool (default: True)
        shuffles training data every epoch
        if True to prevent cycles.
    random_state: int (default: None)
        set random state for shuffling
        and initializing the weights.
    '''
    def __init__(self, eta = .01, n_iter = 10, shuffle = True, random_state = None):
        self.eta = eta
        self.n_iter = n_iter
        self.w_initialized = False
        self.shuffle = shuffle
        if random_state: seed(random_state)

    def fit(self, X, y):
        '''fit training data.

        Parameters
        -----------------------
        X: {array-like}, shape = [n_samples, n_features]
            training vectors, where n_samples is the number of samples and n_features is the number of features.

        y: array-like, shape = [n_samples]
            target values.

        Returns
        -----------------------
        self: object
        '''
        self._initialize_weights(X.shape[1])
        self.cost_ = []
        for i in range(self.n_iter):
            if self.shuffle:
                X, y = self._shuffle(X, y)
            cost = []
            for xi, target in zip(X, y):
                cost.append(self._update_weights(xi, target))
            avg_cost = sum(cost)/len(y)
            self.cost_.append(avg_cost)
        return self

    def partial_fit(self, X, y):
        ''' fit training data without reinitializing the weights'''
        if not self.w_initialized:
            self._initialize_weights(X.shape[1])
        if y.ravel().shape[0] > 1:
            for xi, target in zip(X, y):
                self._update_weights(xi, target)
        else:
            self._update_weights(X, y)
        return self

    def _shuffle(self, X, y):
        ''' shuffle training data'''
        r = np.random.permutation(len(y))
        return X[r], y[r]

    def _initialize_weights(self, m):
        ''' initialize weights to zeors'''
        self.w_ = np.zeros(1 + m)
        self.w_initialized = True

    def _update_weights(self, xi, target):
        ''' apply adaline learning rule to update the weights'''
        output = self.net_input(xi)
        error = (target - output)
        self.w_[1:] += self.eta * xi.dot(error)
        self.w_[0] += self.eta * error
        cost = .5 * error ** 2
        return cost

    def net_input(self, X):
        '''calculate net input'''
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def activation(self, X):
        ''' compute linear activation'''
        return self.net_input(X)

    def predict(self, X):
        ''' return class label after unit step'''
        return np.where(self.activation(X) >= 0.0, 1, -1)

def plot_decision_regions(X, y, classifier, resolution = .02):
    '''decision edge's visualization'''
    '''wendy has not totally understand this part'''

    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha = .4, cmap = cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    # plot class samples
    for idx, c1 in enumerate(np.unique(y)):
        plt.scatter(x = X[y == c1, 0], y = X[y == c1, 1],
                    alpha = .8, c = cmap(idx),
                    marker = markers[idx], label = c1)

def iris_example():
    '''get and scatter iris data'''
    df = pd.read_csv('iris.data', header = None)
    y = df.iloc[:100, 4].values
    y = np.where(y == 'Iris-setosa', -1, 1)
    X = df.iloc[:100, [0, 2]].values

    print (df.head())

    X_std = np.copy(X)
    X_std[:, 0] = (X_std[:, 0] - X_std[:, 0].mean()) / X_std[:, 0].std()
    X_std[:, 1] = (X_std[:, 1] - X_std[:, 1].mean()) / X_std[:, 1].std()

    # plt.scatter(X[0:50, 0], X[0:50, 1], color = 'red', marker = 'o', label = 'setosa')
    # plt.scatter(X[51:100, 0], X[51:100, 1], color = 'blue', marker = 'x', label = 'versicolor')
    # plt.xlabel('petal length (cm)')
    # plt.ylabel('sepal length (cm)')
    # plt.legend(loc = 'upper left')
    # plt.show()

    '''make classifier on the iris data'''
    # ppn = perceptron()
    # ppn.fit(X, y)
    # plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker = 'o')
    # plt.xlabel('Epochs')
    # plt.ylabel('Number of misclassifications')
    # plt.show()

    '''make 2-D dataset decision edge visulization'''
    # plot_decision_regions(X, y, classifier = ppn)
    # plt.xlabel('sepal length [cm]')
    # plt.ylabel('petal length [cm]')
    # plt.legend(loc = 'upper left')
    # plt.show()

    '''make plots of adalineGD between two different learning speed'''
    # fig, ax = plt.subplots(nrows = 1, ncols = 2, figsize = (8, 4))

    # ada1 = adalineGD(n_iter = 10, eta = .01).fit(X, y)
    # ax[0].plot(range(1, len(ada1.cost_) + 1),
    #             np.log10(ada1.cost_), marker = 'o')
    # ax[0].set_xlabel('Epochs')
    # ax[0].set_ylabel('log(sum-squared-error)')
    # ax[0].set_title('Adaline - Learning rate .01')

    # ada2 = adalineGD(n_iter = 10, eta = .0001).fit(X, y)
    # ax[1].plot(range(1, len(ada2.cost_) + 1),
    #             ada2.cost_, marker = 'o')
    # ax[1].set_xlabel('Epochs')
    # ax[1].set_ylabel('Sum-squared-error')
    # ax[1].set_title('Adaline - Learning rate .0001')
    # plt.show()

    '''make plots again and deal the data to standard norm distribution first'''
    # ada = adalineGD(n_iter = 15, eta = .01)
    # ada.fit(X_std, y)
    # plot_decision_regions(X_std, y, classifier = ada)
    # plt.title('Adaline - Gradient Descent')
    # plt.xlabel('sepal length [standardized]')
    # plt.ylabel('petal length [standardized]')
    # plt.legend(loc = 'upper left')
    # plt.show()
    # plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker = 'o')
    # plt.xlabel('Epochs')
    # plt.ylabel('Sum - squared - error')
    # plt.show()

    ''''''
    # ada = adalineSGD(n_iter = 15, eta = .01, random_state = 1)
    # ada.fit(X_std, y)
    # plot_decision_regions(X_std, y, classifier = ada)
    # plt.title('Adaline - Stochastic Gradient Descent')
    # plt.xlabel('sepal length [standardized]')
    # plt.ylabel('petal length [standardized]')
    # plt.legend(loc = 'upper left')
    # # plt.show()
    # plt.plot(range(1, len(ada.cost_) + 1), ada.cost_, marker = 'o')
    # plt.xlabel('Epochs')
    # plt.ylabel('Average Cost')
    # # plt.show()
    # ada.partial_fit(X_std[0, :], y[0])

if __name__ == '__main__':
    iris_example()


# 3.12
# Average versus median
#You now know how to use numpy functions to get a better feeling for your data. It basically comes down to importing numpy and then calling several simple functions on the numpy arrays:

#import numpy as np
#x = [1, 4, 8, 10, 12]
#np.mean(x)
#np.median(x)
#The baseball data is available as a 2D numpy array with 3 columns (height, weight, age) and 1015 rows. The name of this numpy array is np_baseball. After restructuring the data, however, you notice that some height values are abnormally high. Follow the instructions and discover which summary statistic is best suited if you're dealing with so-called outliers.

#INSTRUCTIONS
#100 XP
#Create numpy array np_height that is equal to first column of np_baseball.
#Print out the mean of np_height.
#Print out the median of np_height.

# np_baseball is available

# Import numpy
import numpy as np

# Create np_height from np_baseball
np_height = np_baseball[:,0]

# Print out the mean of np_height
print(np.mean(np_height))

# Print out the median of np_height
print(np.median(np_height))

Python/Datacamp/0.0_Intro to Python for Data Science/3.12_Average-versus-median.py


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

# Data preprocessing
data = pd.read_csv("RealEstate.csv")

# Converting Pandas dataframe to numpy array
X = data.Size.values.reshape(-1, 1)
Y = data.Price.values.reshape(-1, 1)

m = X.shape[0]  #  number of samples

# Model Intialization
reg = LinearRegression()

# Data Fitting to LinearRegression Model
reg = reg.fit(X, Y)

# Printing coefficients
print(f"Coefficients theta0 = {reg.intercept_}, theta1 = {reg.coef_} ")

# Predicted Values
Y_pred = reg.predict(X)

# Model Evaluation
def rmse(Y, Y_pred):
    rmse = np.sqrt(sum((Y - Y_pred) ** 2) / Y.shape[0])
    return rmse

def r2_score(Y, Y_pred):
    mean_y = np.mean(Y)
    ss_tot = sum((Y - mean_y) ** 2)
    ss_res = sum((Y - Y_pred) ** 2)
    r2 = 1 - (ss_res / ss_tot)
    return r2

# Print Scores
print("RMSE = ", rmse(Y, Y_pred))
print("R2 Score = ", r2_score(Y, Y_pred))

# Visualization
# Ploting Line
plt.plot(X, Y_pred, color='#c93e4e', label='Regression Line')
# Ploting Scatter Points
plt.scatter(X, Y, c='#54a774', label='Scatter Plot')
plt.xlabel('Size')
plt.ylabel('Price')
plt.legend()
plt.show()


#!/usr/bin/python
#
# Eigenvalues of the Gradient Structure Tensor
#
# Calculates the eigenvalues of the gradient structure tensor.
#
# The eigenvalues are numbered in decreasing order of their magnitude.
#
import sys,os
import numpy as np
#
# Import the module with the I/O scaffolding of the External Attribute
#
sys.path.insert(0, os.path.join(sys.path[0], '..', '..'))
import extattrib as xa
import extlib as xl
#
# These are the attribute parameters
#
xa.params = {
	'Inputs': ['In-line gradient', 'Cross-line gradient', 'Z gradient'],
	'Output': ['e1', 'e2', 'e3'],
	'ZSampMargin' : {'Value':[-3,3], 'Symmetric': True},
	'StepOut' : {'Value': [3,3], 'Symmetric': True},
	'Help': 'http://waynegm.github.io/OpendTect-Plugin-Docs/External_Attributes/GST_Attributes/'
}
#
# Define the compute function
#
def doCompute():
	xs = xa.SI['nrinl']
	ys = xa.SI['nrcrl']
	zs = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin']['Value'][0] + 1
	kernel = xl.getGaussian(xs, ys, zs)
	while True:
		xa.doInput()

		gx = xa.Input['In-line gradient']
		gy = xa.Input['Cross-line gradient']
		gz = xa.Input['Z gradient']
#
#	Inner product of  gradients
		gx2 = gx * gx
		gy2 = gy * gy
		gz2 = gz * gz
		gxgy = gx * gy
		gxgz = gx * gz
		gygz = gy * gz
#
#	Outer gaussian smoothing
		rgx2 = xl.sconvolve(gx2, kernel)
		rgy2 = xl.sconvolve(gy2, kernel)
		rgz2 = xl.sconvolve(gz2, kernel)
		rgxgy = xl.sconvolve(gxgy, kernel)
		rgxgz = xl.sconvolve(gxgz, kernel)
		rgygz = xl.sconvolve(gygz, kernel)
#
#	Form the structure tensor
		T = np.rollaxis(np.array([	[rgx2,  rgxgy, rgxgz],
									[rgxgy, rgy2,  rgygz],
									[rgxgz, rgygz, rgz2 ]]), 2)
#
#	Get the eigenvalues
		w = np.linalg.eigvalsh(T)
		v = np.rollaxis(np.sort(w),1)
		xa.Output['e1'] = v[2,:]
		xa.Output['e2'] = v[1,:]
		xa.Output['e3'] = v[0,:]
		xa.doOutput()

#
# Assign the compute function to the attribute
#
xa.doCompute = doCompute
#
# Do it
#
xa.run(sys.argv[1:])


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

dat=pd.read_csv("c:/users/jliv/downloads/qsar_aquatic_toxicity.csv",header=None,sep=";")
dat = dat.rename(mapper = {0:'TSPA',1:'SAaac',2:'H-050',3:'MLOGP',4:'RDCHI',5:'GATS1p',6:'nN',7:'C-040',8:'LC50'},axis='columns')

#EUC
dat.corr()['LC50']

variables = ['MLOGP', 'RDCHI', 'GATS1p']

train = dat.sample(int(len(dat)*.8)).copy()
test = dat[~dat.index.isin(train.index)]

X = train[variables].copy()
X['int']=1

Y=train['LC50']

testx = test[variables].copy()
testx['int']=1

testy=test['LC50']

#LINEAR REGRESSION
'''
Model: Y = X@B
Cost Function: C = .5*(X@B-Y)**2
Gradient: grad_C=X.T@(X@B-Y)
Hessian: hess_C=X.T@X

Explicit Solution:
    X.T@(X@B-Y)=0
    X.T@(X@B)-X.T@Y=0
    X.T@(X@B)=X.T@Y
    (X.T@X)@B=X.T@Y
    B=(X.T@X)**-1@(X.T@Y)
'''

#Explicit Solution

explicit_coefs = np.linalg.inv(X.T@X)@(X.T@Y)

#Newtonian Descent, random initial coefs, update B_n+1 = B_n - hess**-1@grad

newt_coefs = np.random.normal(0,.5,4)
grad = X.T@(X@newt_coefs-Y)
hess = X.T@X
newt_coefs=newt_coefs-np.linalg.inv(hess)@grad

#Gradient Descent with constant learning rate

grad_coefs = np.random.normal(0,.5,4)
lr = .00001
for i in range(50000):
    grad_coefs=grad_coefs-lr*(X.T@(X@grad_coefs-Y))

# LOGISTIC REGRESSION Data Prep

dat = pd.read_csv("c:/users/jliv/downloads/adult.data",header=None)

dat=dat.rename(mapper = {0:'age',
                          1:'workclass',
                          2:'fnlwgt',
                          3:'education',
                          4:'education_num',
                          5:'marital_status',
                          6:'occupation',
                          7:'relationship',
                          8:'race',
                          9:'sex',
                          10:'capital_gain',
                          11:'capital_loss',
                          12:'hours_per_week',
                          13:'native_country',
                          14:'income'},axis='columns')

dat['income']=np.where(dat['income']==" >50K",1,0)

onehot_cols = ['workclass','education','marital_status','occupation','relationship','race','sex']
matchdf = []
for j in onehot_cols:
    for i in dat[j].unique():
        dat[j+"_"+i]=np.where(dat[j]==i,1,0)

        match=max([sum(np.where(dat[j+"_"+i]==dat['income'],1,0)),sum(np.where(dat[j+"_"+i]!=dat['income'],1,0))])
        matchdf.append([j+"_"+i,match])

matchdf=pd.DataFrame(matchdf,columns=['dim','match'])
matchdf.sort_values(by='match',ascending=False,inplace=True)
matchdf.reset_index(inplace=True,drop=True)
plt.plot(matchdf['match'])

variables=  list(matchdf['dim'][:14])

X = dat[variables].copy()
X['int']=1
Y=dat['income']

#Explicit Solution
#Calculate log-odds from label
#Linear model for log odds

LO = Y/(1-Y)
LO = np.log(np.where(LO == np.inf,1e14,1e-14))

logistic_explicit_coefs = np.linalg.inv(X.T@X)@(X.T@LO)

Opred = np.exp(X@logistic_explicit_coefs)

Ypred = Opred/(1+Opred)
Ypred = np.where(Ypred>.5,1,0)

err = np.where(Ypred==Y,1,0)

acc = np.mean(err)

#Descent solution

logistic_coefs = np.random.normal(0,.5,len(X.columns.values))

def sigmoid(x):
    return 1/(1+np.e**(-x))

def del_sig(x):
    return sigmoid(x)*(1-sigmoid(x))

def predict(X,B):
    return sigmoid(X@B)

def activation(x):
    return np.where(x>.5,1,0)

lr=.001
grad0 = np.array([1000 for i in logistic_coefs])
tol = 1e-8
for i in range(500):
    grad = (X.T@((predict(X,logistic_coefs)-Y)*del_sig(predict(X,logistic_coefs))))
    logistic_coefs=logistic_coefs-lr*grad

    print(i,round(np.mean(np.where(activation(predict(X,logistic_coefs))==Y,1,0)),5))
    if max(grad/grad0-1)<=tol:
        break
    else:
        grad0=grad

pred = np.where(predict(X,logistic_coefs)>.5,1,0)

err = np.where(pred==Y,1,0)

acc = np.mean(err)

randompred=np.where(np.random.normal(0,1,len(pred))>0,1,0)

randerr = np.where(randompred==Y,1,0)
randacc = np.mean(randerr)

tp = sum(np.where((pred==1)&(Y==1),1,0))
tn = sum(np.where((pred==0)&(Y==0),1,0))

fp = sum(np.where((pred==1)&(Y==0),1,0))
fn = sum(np.where((pred==0)&(Y==1),1,0))

precision=tp/(tp+fp)

recall=tp/(tp+fn)

accuracy=(tp+tn)/(tp+tn+fp+fn)

#Kmeans
d1 = np.random.normal(3,.5,(10,3))
d2=np.random.normal(5,.5,(8,3))

d3=np.random.normal(7,.5,(8,3))

d = np.vstack((d1,d2,d3))

centroids=3

c=np.random.normal(np.mean(d),np.std(d),(centroids,d.shape[1]))

def kmeans(dat,centroids,max_iter):

    d = dat
    c=np.random.normal(np.mean(d),np.std(d),(centroids,d.shape[1]))

    def mydist(d,c):
        distarray=[]
        for i in range(c.shape[0]):
            distarray.append(np.sum((d-c[i])**2,axis=1)**.5)

        distarray=np.array(distarray)
        return distarray

    for j in range(16):
        dists = mydist(d,c).T
        clusts=np.argmin(dists,axis=1)
        for i in range(centroids):
            c[i]=np.mean(d[clusts==i],axis=0)

    return clusts

kmeans(d,3,16)


import numpy as np
# TODO add separate class for this functionality
def get_euler_angles_from_rot(R):
    """Compute Euler angles from rotation matrix.
    yaw, pitch, roll: 3, 2, 1 rot sequence
    Note frame relationship: e^b = e^v R^{vb}
    """
    psi = np.arctan2(R[1, 0], R[0, 0]) # yaw angle
    theta = np.arcsin(-R[2, 0])        # pitch angle
    phi = np.arctan2(R[2, 1], R[2, 2]) # roll angle
    return (psi, theta, phi)

def skew(a):
    """Returns skew symmetric matrix, given a 3-vector"""
    #a = np.flatten(a) # convert to 3-array
    a = a.flatten()
    return np.array([
        [    0,  -a[2],  a[1]],
        [ a[2],     0,  -a[0]],
        [-a[1],   a[0],     0]
        ])

def quat_prod(p, q):
    p0 = p[0]; p = p[1:4]
    P = np.zeros((4,4))
    P[0, 0] = p0; P[0, 1:] = -p.T
    P[1:, 0] = p.flatten()
    P[1:, 1:] = -skew(p) + p0*np.eye(3)
    return P @ q

def Euler2Quaternion(y, p, r):
    psi2 = y/2
    theta2 = p/2
    phi2 = r/2
    return np.array([
        [np.sin(phi2)*np.sin(psi2)*np.sin(theta2) + np.cos(phi2)*np.cos(psi2)*np.cos(theta2)],
        [np.sin(phi2)*np.cos(psi2)*np.cos(theta2) - np.sin(psi2)*np.sin(theta2)*np.cos(phi2)],
        [np.sin(phi2)*np.sin(psi2)*np.cos(theta2) + np.sin(theta2)*np.cos(phi2)*np.cos(psi2)],
        [-np.sin(phi2)*np.sin(theta2)*np.cos(psi2) + np.sin(psi2)*np.cos(phi2)*np.cos(theta2)]
        ])

def Quaternion2Rotation(q):
    """Compute rotation matrix from quaternion.
    quaternion must be provided in form [q0, q]
    """
    q = q.flatten()
    q0 = q[0]
    q = q[1:]
    return (q0**2 - np.dot(q, q))*np.eye(3) + 2*np.outer(q,q) + 2*q0*skew(q)

def Quaternion2Euler(q):
    R = Quaternion2Rotation(q)
    psi,theta,phi = get_euler_angles_from_rot(R)
    return (psi, theta, phi)