In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import HTML
from numpy import linalg as LA
import matplotlib.animation as animation

Problem 1

In [88]:
### Create data
x = np.linspace(-10,10,1000)
y = np.exp(x)

n = 1
a1,M1 = fitpade(x,y,n)
out1 = getpade(a1,x,n)

n = 2
a2,M2 = fitpade(x,y,n)
out2 = getpade(a2,x,n)

n = 3
a3,M3 = fitpade(x,y,n)
out3 = getpade(a3,x,n)

n = 4
a4,M4 = fitpade(x,y,n)
out4 = getpade(a4,x,n)

n = 5
a5,M5 = fitpade(x,y,n)
out5 = getpade(a5,x,n)
In [87]:
def getpade(a,x,n):
    for i in range(0,n):
        num = a[i]*(x**i)  
    for i in range(1,n+1):    
        den = a[i+n]*x**(i+n-n)
        
    return(num/(1+den))
In [89]:
fig=plt.figure(figsize=(8, 5), dpi= 180, facecolor='w', edgecolor='k')
ax = fig.gca()

ax.plot(x,y,'k',linewidth = 2,label = 'e^x')
ax.plot(x,out1,label = 'order 1,1')
ax.plot(x,out2,label = 'order 2,2')
ax.plot(x,out3,label = 'order 3,3')
ax.plot(x,out4,label = 'order 4,4')
ax.plot(x,out5,label = 'order 5,5')

handles, labels = ax.get_legend_handles_labels()
plt.legend(handles, labels)
plt.ylim(-10, 10)
plt.xlim(-10, 10)
Out[89]:
(-10, 10)
In [19]:
def fitpade(x,y,n):
    # create pade matrix
    m = x.shape[0]
    M = np.ones((m,2*n+1))

    for i in range(0,n+1):
        M[:,i] = x**i

    for i in range(1,n+1):
        M[:,n+i] = -(x**i)*y
        
    #invert matrix
    u,s,v = LA.svd(M,full_matrices=False)
    s = np.diag(s)

    # fit data
    a = np.dot(np.dot(v.T,np.dot(LA.inv(s),u.T)),y)
    return(a,M)

Problem 2

In [3]:
def RBF(x,n,knots):
    # create vandermonde
    m = x.shape[0]
    M = np.ones((m,n))

    for i in range(1,n):
        M[:,i] = np.abs(x-knots[i])**3
    return(M) 
    
def vandermonde(x,n):
    # create vandermonde
    m = x.shape[0]
    M = np.ones((m,n))

    for i in range(1,n):
        M[:,i] = x**i
    return(M)

def fitvandermonde(x,y,n):
    # create vandermonde
    m = x.shape[0]
    M = np.ones((m,n))

    for i in range(1,n):
        M[:,i] = x**i

    #invert matrix
    u,s,v = LA.svd(M,full_matrices=False)
    s = np.diag(s)

    # fit data
    a = np.dot(np.dot(v.T,np.dot(LA.inv(s),u.T)),y)
    return(a,M)

def fitRBF(x,y,n,knots):
    # create vandermonde
    m = x.shape[0]
    M = np.ones((m,n))

    for i in range(1,n):
        M[:,i] = np.abs(x-knots[i])**3

    #invert matrix
    u,s,v = LA.svd(M,full_matrices=False)
    s = np.diag(s)

    # fit data
    a = np.dot(np.dot(v.T,np.dot(LA.inv(s),u.T)),y)
    return(a,M)
In [5]:
# create data
x = np.linspace(-10,10,20)
y = np.sign(x)

# fit data
av5, Mv5 = fitvandermonde(x,y,5)
av10, Mv10 = fitvandermonde(x,y,10)
av15, Mv15 = fitvandermonde(x,y,15)
In [6]:
fig=plt.figure(figsize=(8, 5), dpi= 180, facecolor='w', edgecolor='k')
ax = fig.gca()

ax.plot(y,'k',linewidth = 4)
ax.plot(np.dot(Mv5,av5),'b',linewidth = 3,alpha = 0.6,label = '5')
ax.plot(np.dot(Mv10,av10),'orange',linewidth = 3,alpha = 0.7,label = '10')
ax.plot(np.dot(Mv15,av15),'r',linewidth = 3,alpha = 0.9, label = '15')
handles, labels = ax.get_legend_handles_labels()
plt.legend(handles, labels)
plt.ylim(-2, 2)
plt.title('fitting step function with vandermonde matrix')
Out[6]:
<matplotlib.text.Text at 0x112753080>
In [9]:
# create data
x = np.linspace(-10,10,20)
y = np.sign(x)
c5 = np.linspace(-10,10,5)
c10 = np.linspace(-10,10,10)
c15 = np.linspace(-10,10,15)

# fit data
ar5, Mr5 = fitRBF(x,y,5,c5)
ar10, Mr10 = fitRBF(x,y,10,c10)
ar15, Mr15 = fitRBF(x,y,15,c15)
In [10]:
fig=plt.figure(figsize=(8, 5), dpi= 180, facecolor='w', edgecolor='k')
ax = fig.gca()

ax.plot(y,'k',linewidth = 4)
ax.plot(np.dot(Mr5,ar5),'b',linewidth = 3,alpha = 0.6,label = '5')
ax.plot(np.dot(Mr10,ar10),'orange',linewidth = 3,alpha = 0.7,label = '10')
ax.plot(np.dot(Mr15,ar15),'r',linewidth = 3,alpha = 0.9, label = '15')
handles, labels = ax.get_legend_handles_labels()
plt.legend(handles, labels)
plt.ylim(-2, 2)
plt.title('fitting step function with RBF matrix')
Out[10]:
<matplotlib.text.Text at 0x1131379b0>
In [11]:
Mr5 = RBF(np.linspace(-10.5,10.5,20),5,np.linspace(-10.5,10.5,5))
Mr10 = RBF(np.linspace(-10.5,10.5,20),10,np.linspace(-10.5,10.5,10))
Mr15 = RBF(np.linspace(-10.5,10.5,20),15,np.linspace(-10.5,10.5,15))

Mv5 = vandermonde(np.linspace(-10.5,10.5,20),5)
Mv10 = vandermonde(np.linspace(-10.5,10.5,20),10)
Mv15 = vandermonde(np.linspace(-10.5,10.5,20),15)


fig=plt.figure(figsize=(8, 5), dpi= 180, facecolor='w', edgecolor='k')
ax = fig.gca()

ax.plot(y,'k',linewidth = 4)
ax.plot(np.dot(Mr5,ar5),'b',linewidth = 2,alpha = 0.6,label = 'rbf 5')
ax.plot(np.dot(Mr10,ar10),'orange',linewidth = 2,alpha = 0.7,label = 'rbf 10')
ax.plot(np.dot(Mr15,ar15),'r',linewidth = 2,alpha = 0.9, label = 'rbf 15')

ax.plot(np.dot(Mv5,av5),'b',linewidth = 2,alpha = 1,label = 'vand 5',linestyle = '--')
ax.plot(np.dot(Mv10,av10),'orange',linewidth = 2,alpha = 1,label = 'vand 10',linestyle = '--')
ax.plot(np.dot(Mv15,av15),'r',linewidth = 2,alpha = 1, label = 'vand 15',linestyle = '--')

handles, labels = ax.get_legend_handles_labels()
plt.legend(handles, labels)
plt.ylim(-2, 2)
plt.title('fitting step function with RBF matrix')
Out[11]:
<matplotlib.text.Text at 0x113bcb278>
In [37]:
errorV = np.array((np.sum((y-np.dot(Mv5,av5))**2),np.sum((y-np.dot(Mv10,av10))**2)
,np.sum((y-np.dot(Mv15,av15))**2)))
errorR = np.array((np.sum((y-np.dot(Mr5,ar5))**2),np.sum((y-np.dot(Mr10,ar10))**2)
,np.sum((y-np.dot(Mr15,ar15))**2)))

fig=plt.figure(figsize=(8, 5), dpi= 180, facecolor='w', edgecolor='k')
ax = fig.gca()
ax.semilogy([5,10,15],errorV,'ko-',label = 'vandermonde fit')
ax.semilogy([5,10,15],errorR,'ro-',label = 'RBF fit')
plt.xlabel('order of fit')
plt.ylabel('sum squared error')
handles, labels = ax.get_legend_handles_labels()
plt.legend(handles, labels)
plt.title('sum squared error for generalization')
Out[37]:
<matplotlib.text.Text at 0x1176ea550>

Problem 3

In [90]:
def LFSR(x):
    #outbit = (np.sum([x[1],x[4]])%2)
    outbit = (x[1]+x[4])%2
    y = ([outbit,x[0],x[1],x[2],x[3]])
    return(outbit,y)
In [91]:
outbits = np.zeros(100)
inbits = ([0,1,1,1,1])
bits = ([0,1,1,1,1])
for i in range(0,100):
    outbits[i],inbits = LFSR(inbits)
    bits = np.vstack([bits,inbits])
In [2]:
import tensorflow
from keras.models import Sequential
from keras.layers import Dense, Activation
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-5e53d6d3cedd> in <module>()
      1 import tensorflow
----> 2 from keras.models import Sequential
      3 from keras.layers import Dense, Activation

/Users/andrewbahle/anaconda/lib/python3.6/site-packages/keras/__init__.py in <module>()
      1 from __future__ import absolute_import
      2 
----> 3 from . import activations
      4 from . import applications
      5 from . import backend

/Users/andrewbahle/anaconda/lib/python3.6/site-packages/keras/activations.py in <module>()
      4 from . import backend as K
      5 from .utils.generic_utils import deserialize_keras_object
----> 6 from .engine import Layer
      7 
      8 

/Users/andrewbahle/anaconda/lib/python3.6/site-packages/keras/engine/__init__.py in <module>()
      6 from .topology import Layer
      7 from .topology import get_source_inputs
----> 8 from .training import Model

/Users/andrewbahle/anaconda/lib/python3.6/site-packages/keras/engine/training.py in <module>()
     22 from .. import metrics as metrics_module
     23 from ..utils.generic_utils import Progbar
---> 24 from .. import callbacks as cbks
     25 from ..legacy import interfaces
     26 

/Users/andrewbahle/anaconda/lib/python3.6/site-packages/keras/callbacks.py in <module>()
     24 if K.backend() == 'tensorflow':
     25     import tensorflow as tf
---> 26     from tensorflow.contrib.tensorboard.plugins import projector
     27 
     28 

ModuleNotFoundError: No module named 'tensorflow.contrib.tensorboard'