In [1]:
import numpy as np
import matplotlib.pyplot as plt
import random
In [2]:
plt.rcParams['figure.figsize']=(12,8)
In [3]:
import graphPaths
In [4]:
# this function creates the adjancy list and the dictionary with the order of edges

def listTograph(grafo , dicionario , lista , initvalue=0):
    i = initvalue
    
    for arco in lista:
        grafo.addEdge(arco[0],arco[1])
        dicionario[arco[0],arco[1]] = i 
        i += 1

        
def invertList(lista):
    out = lista.copy()
    
    for i in range(len(lista)):
        out.append(lista[i][::-1])
    
    return out    
In [5]:
grafo01 = graphPaths.Graph(5)
lista01 = [[0,1],[0,2],[1,2],[2,3],[2,4],[3,4]]
dicio01 = {}
In [6]:
listTograph(grafo01 , dicio01 , lista01)
In [7]:
print(grafo01.graph)
print(dicio01)
defaultdict(<class 'list'>, {0: [1, 2], 1: [2], 2: [3, 4], 3: [4]})
{(0, 1): 0, (0, 2): 1, (1, 2): 2, (2, 3): 3, (2, 4): 4, (3, 4): 5}
In [8]:
caminhos01 = []
grafo01.printAllPaths(0,4,caminhos01)
[0, 1, 2, 3, 4]
[0, 1, 2, 4]
[0, 2, 3, 4]
[0, 2, 4]
In [9]:
print(caminhos01)
[[0, 1, 2, 3, 4], [0, 1, 2, 4], [0, 2, 3, 4], [0, 2, 4]]
In [10]:
narcos = len(lista01)
ncamin = len(caminhos01)
matrix = np.zeros(ncamin * narcos).reshape(ncamin , narcos)

for i in range(ncamin):
    for k in range(len(caminhos01[i]) - 1):
        matrix[i,dicio01[caminhos01[i][k],caminhos01[i][k+1]]] = 1

print(matrix)
[[1. 0. 1. 1. 0. 1.]
 [1. 0. 1. 0. 1. 0.]
 [0. 1. 0. 1. 0. 1.]
 [0. 1. 0. 0. 1. 0.]]
In [11]:
sigmas = np.linspace(10,3,num=narcos)
mus = np.linspace(1,10,num=narcos)

arcos_randomize = np.random.exponential

print(mus)
print(sigmas)
[ 1.   2.8  4.6  6.4  8.2 10. ]
[10.   8.6  7.2  5.8  4.4  3. ]
In [12]:
# Epaneshnikov Kernel 
xx10 = np.linspace(-1,1)
yy10 = np.array(list(map(lambda x : .75 * (1 - x**2),xx10)))

plt.plot(xx10,yy10)
plt.title('Epanechnikov Kernel')
plt.show()
In [13]:
# triweight kernel

xx20 = np.linspace(-1,1)
yy20 = np.array(list(map(lambda x : 35.0 *(1-x**2)**3/32.0, xx20)))

plt.plot(xx20,yy20)
plt.title('Triweight Kernel')
plt.show()
In [14]:
def triweightKfunc(x,x0,h):
    
    t = (x - x0)/h
    
    out = 35.0 *(1-t**2)**3/32.0
    
    return out/h 
In [15]:
triweightKfunc(0.0,1,3)
Out[15]:
0.2560585276634659
In [16]:
npts = 100

xx0= 10 * np.ones(npts)
hh = 3 * np.ones(npts)

xx30 = np.linspace(xx0[0] - hh[0] , xx0[0] + hh[0] , num = npts)

yy30 = np.array(list(map(lambda x , y , z: triweightKfunc(x,y,z),xx30, xx0 , hh)))

plt.plot(xx30,yy30)
plt.show()
In [17]:
# now we generate points for this random variable using the rejection acceptance method.

npts = 100

centralpt = 10 
hhi = 3

xx40 = np.linspace(centralpt - hhi, centralpt + hhi , num = npts)
yy40 = np.array(list(map(lambda x : triweightKfunc(x,centralpt,hhi),xx40)))

# now we are going to generate random points for this distribution
nrpts = 50000
contador = 0 

randpts = []

while (contador < nrpts):
    
    x = centralpt - hhi + 2.0 * hhi * random.uniform(0.0 , 1.0)
    y = random.random()
    t = triweightKfunc(x , centralpt , hhi)
    
    if (y < t):
        randpts.append(x)
        contador += 1
    

plt.plot(xx40,yy40)
plt.hist(np.array(randpts),bins = 60 ,density=True,rwidth=0.8)
plt.show()

deal with different initial and final edges

In [18]:
grafo02 = graphPaths.Graph(5)
lista02 = [[0,1],[0,2],[1,2],[2,3],[2,4],[3,4]]
dicio02 = {}

listTograph(grafo02,dicio02,lista02)
caminhos02 = []
grafo02.printAllPaths(4,2,caminhos02)

print(caminhos02)
[]
In [19]:
lista021 = [x[::-1] for x in lista02 if x[1]== 2]
lista021.append([x for x in lista02 if x[1]!= 2])
In [20]:
print(lista02)
print(lista021)
[[0, 1], [0, 2], [1, 2], [2, 3], [2, 4], [3, 4]]
[[2, 0], [2, 1], [[0, 1], [2, 3], [2, 4], [3, 4]]]
In [21]:
lista02[::]
Out[21]:
[[0, 1], [0, 2], [1, 2], [2, 3], [2, 4], [3, 4]]