<jupyter_start><jupyter_text>Link two computer models emulated by two 2-layered DGP models<jupyter_code>#First model
n=18
X1=np.linspace(0,1.,n)[:,None]
f1= lambda x: (np.sin(7.5*x)+1)/2
Y1=np.array([f1(x) for x in X1]).reshape(-1,1)
Xt=np.linspace(0,1.,200)[:,None]
Yt=np.array([f1(x) for x in Xt]).flatten()
plt.plot(Xt,Yt)
plt.scatter(X1,Y1,color='r')
#Second model
X2=Y1
f2= lambda x: 2/3*np.sin(2*(2*x-1))+4/3*np.exp(-30*(2*(2*x-1))**2)-1/3  
Y2=np.array([f2(x) for x in X2]).reshape(-1,1)
Xt=np.linspace(0,1.,200)[:,None]
Yt=np.array([f2(x) for x in Xt]).flatten()
plt.plot(Xt,Yt)
plt.scatter(X2,Y2,color='r')
#Overall model
Yt=np.array([f2(f1(x)) for x in Xt]).flatten()
plt.plot(Xt,Yt)
plt.scatter(X1,Y2,color='r')
#Construct 4 layers with first two layers as the DGP for the first model and the last two layers as the DGP of the second model
layer1=[kernel(length=np.array([1.]),name='matern2.5')]
layer2=[kernel(length=np.array([1.]),name='matern2.5',scale_est=1)]
layer3=[kernel(length=np.array([1.]),name='matern2.5')]
layer4=[kernel(length=np.array([1.]),name='matern2.5',scale_est=1)]
all_layer=combine(layer1,layer2,layer3,layer4)
Y=[np.full((len(Y1),1),np.nan),Y1,np.full((len(Y1),1),np.nan),Y2]
m=dgp(X1,Y,all_layer)
#Train model
m.train(N=500)
#Build emulator
final_layer_obj=m.estimate()
emu=emulator(final_layer_obj)
#Make predictions
z=np.linspace(0,1,300)[:,None].reshape(-1,1)
mu,var=emu.predict(z, N=50, method='mean_var')
#Plot output
s=np.sqrt(var)
u=mu+2*s
l=mu-2*s
p=plt.plot(z,mu,color='r',alpha=1,lw=1)
p1=plt.plot(z,u,'--',color='g',lw=1)
p1=plt.plot(z,l,'--',color='g',lw=1)
plt.plot(Xt,Yt)
plt.scatter(X1,Y2,color='r')<jupyter_output><empty_output><jupyter_text>Link two computer models emulated by two GP models<jupyter_code>#Construct two-layered structure
layer1=[kernel(length=np.array([0.1]),name='matern2.5',scale_est=1,input_dim=np.arange(1))]
layer2=[kernel(length=np.array([0.1]),name='matern2.5',scale_est=1,input_dim=np.arange(1))]
all_layer=combine(layer1,layer2)
#Create data for each GP
X=[[X1],[Y1]]
Y=[[Y1],[Y2]]
m0=lgp(X,Y,all_layer)
#Train model
m0.train()
#Make predictions
z=np.linspace(0,1,300)[:,None].reshape(-1,1)
mu,var=m0.predict(x=z,z=None)
#Draw outputs
s=np.sqrt(var)
u=mu+2*s
l=mu-2*s
p=plt.plot(z,mu,color='r',alpha=1,lw=1)
p1=plt.plot(z,u,'--',color='g',lw=1)
p1=plt.plot(z,l,'--',color='g',lw=1)
plt.plot(Xt,Yt)
plt.scatter(X1,Y2,color='r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24055829228243022, 'fraction_numerical': 0.06198686371100164, 'mean_word_length': 8.748, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '23285', 'license': 'MIT', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1199, 'n_words': 153, 'path': 'demo/model_linking.ipynb', 'repo_name': 'fentonscode/DGP', 'size': '2436'}