# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

# # Priors: Theano Graph

# +
with gp.G3Model() as model:
    g3 = gp.G3GP(gp.Constant(), gp.SE(x,gp.ARD_L2), gp.Identity(y))
    g3.observed(x_obs,y_obs)
    mu, var, median, I_up, I_down, m1, m2 = g3.compilate_functions(model.vars,x)
    
model.use_prior()
gp.show_graph(model.logpt)
# -

gp.show_graph(model.fastlogp.f)

model.use_prior(False)
gp.show_graph(model.logpt)

gp.show_graph(model.fastlogp.f)

# # Transformation
# http://localhost:8888/notebooks/GaussianProcesses/BoxCoxGP/sunspots-GP-vs-TGP.ipynb

hidden_var = np.linspace(-10,5,501)
params = {'ARD_L2_log_': np.array(2.5, dtype=np.float32),
         'COV_SE_log_': np.array([ 2, -3 ], dtype=np.float32),
         'Mean_Constant': np.array(3.0, dtype=np.float32),
         'T_Shift': np.array(0.0, dtype=np.float32),
         'T_Power_log_': np.array(-0.4, dtype=np.float32),
       }
trans, dtrans, dist_gp, dist_tgp = g3.compile_distributions(model.point_to_In(params))
params

# +
grilla = np.linspace(-5,15,501)
gp.style_normal()
plt.figure(1,(10,10))

desfase = 5


yyy = np.zeros_like(grilla)
i = 0
for xxx in grilla:
    yyy[i] = dist_gp(np.array([xxx]))
    i+=1
plt.plot(grilla,yyy*50-desfase,label='GaussianDist')


plt.plot(trans(grilla),grilla,label='BoxCoxTrans')


yyy = np.zeros_like(grilla)
i = 0
for xxx in trans(grilla):
    yyy[i] = dist_gp(np.array([xxx]))
    i+=1
plt.plot(yyy*30-desfase,grilla,label='BoxCox(Gaussian)')


i = 0
for xxx in grilla:
    yyy[i] = dtrans(np.array([xxx]))
    i+=1
plt.plot((yyy-np.min(yyy))-desfase,grilla,label='Jacobian(BoxCox)')


i = 0
for xxx in grilla:
    yyy[i] = dist_tgp(np.array([xxx]))
    i+=1
plt.plot(yyy*40-desfase,grilla,label='BoxCox-GaussianDist')



#location_gp = grilla[np.argmax(yyy*100)]
#min_gp = np.min(yyy*100)
#max_gp = trans(np.array([location_gp]))
#plt.plot(np.array([location_gp,location_gp]),np.array([min_gp,max_gp]))



gp.plot('Box-Cox Change of Variable','GP','Box-Cox GP',loc=1,ncol=1)
plt.axis([-5,12,-5,12])
gp.save('change_of_variable.pdf')
# -

import theano.sandbox.linalg as sT
trans = tt.log(sT.det(tt.jacobian(g3.mapping.inv(value),value)))
ftrans = th.function([value]+model.point_to_In(init),trans,allow_input_downcast=True)
yyy = np.zeros_like(grilla)
i = 0
for xxx in grilla:
    yyy[i] = ftrans(np.array([xxx]))
    i+=1
plt.plot(grilla,yyy)

value,dist = gp.marginal_tgp(np.array([0.0]))
fdist = th.function([value]+gp_model.point_to_In(start_bfgs),dist,allow_input_downcast=True)
grilla = np.linspace(-200,200,301)
yyy = np.zeros_like(grilla)
i = 0
for xxx in grilla:
    yyy[i] = fdist(np.array([xxx]))
    i+=1
plt.plot(grilla,yyy)

# # Plot Cov

ker=LaplaceKernel(noise,h,np.array([l]))
plt.matshow(ker.cov(x_obs[:,np.newaxis]))
plt.matshow(sp.linalg.inv(ker.cov(x_obs[:,np.newaxis])))
plt.matshow(CovCholesky(ker.cov(x_obs[:,np.newaxis])))
plt.matshow(CovCholesky(ker.cov(x_obs[:,np.newaxis])).T)

# +
#<NAME>
plt.figure(1, figsize=(20,6))
Kxx=InfoMatrix(GaussKer,np.expand_dims(np.arange(-2,2,0.1),1))

plt.subplot(121)
v=np.max(np.abs(Kxx))
plt.imshow(Kxx,cmap=cm.seismic,vmax=v,vmin=-v)
plt.colorbar()

#Graficar Precision
plt.subplot(122)
Jxx=np.linalg.solve(Kxx,np.eye(len(Kxx)))
v=np.max(np.abs(Jxx))
plt.imshow(Jxx,cmap=cm.seismic,vmax=v,vmin=-v)
plt.colorbar()


plt.figure(2, figsize=(20,6))
plt.plot(Kxx[len(Kxx)/2,:],'b')
plt.plot(Jxx[len(Jxx)/2,:],'r')

#plt.axis([550,650,-0.1,14])
#show(np.linalg.norm(Jxx.dot(Kxx)- np.eye(len(Kxx))))
# -

# # GP 2D
# http://localhost:8888/notebooks/GaussianProcesses/BoxCoxGP/Presentation-TGP.ipynb

# +
# Parametros grilla
x = np.linspace(0, 50, 51)
y = np.linspace(0,200, 201)

# Parametros GP
l = 30
sigma = 10*np.log(1.5)
gamma_x = 0.0*np.log(2.0/l**2)
gamma_y = 0.8*np.log(2.0/l**2)


x2d, y2d = np.meshgrid(x, y)
xy = np.zeros((len(x)*len(y),2))
for i in range(len(x)):
    for j in range(len(y)):
        xy[i*len(y)+j,:] = x[i],y[j]
x2d = x2d.T
y2d = y2d.T


Kerxy = GaussianKernel(-np.inf,sigma,np.array([gamma_x,gamma_y]))
fxy2d_hidden = Kerxy.sample(xy).reshape((len(x),len(y)))

fig = plt.figure(1,[20,10])
ax = fig.gca(projection='3d')
ax.plot_surface(x2d, y2d, fxy2d_hidden, alpha=0.4,cmap=cm.RdBu_r)
cset = ax.contourf(x2d, y2d, fxy2d_hidden, zdir='z', offset=np.min(fxy2d_hidden), cmap=cm.RdBu_r)
# -

# # Sympy

# +
# %matplotlib inline
import sympy as sy
import seaborn as sb
import matplotlib.pyplot as plt
from IPython.display import display

from sympy import init_printing
from sympy import symbols,diff,log,sign,Abs
from sympy.plotting import plot

init_printing() 
plt.rcParams['figure.figsize'] = (20, 10)
#init_session(quiet=True)

x,y = symbols('x,y',real=True)


# -

phi = sign(x)*log(Abs(x))
dphi = diff(phi,x)
phi_1 = sy.solve(phi-y,x,domain=sy.S.Reals)

display(phi)
display(dphi)
display(phi_1)

plot(phi,(x,-2,2))

plot(dphi,(x,-2,2))





StarcoderdataJupyterScriptsDedupFiltered