1+ import matplotlib .pyplot as plt
2+ from scipy import stats
3+ import numpy as np
4+
5+
6+ # Set global plot settings
7+ plt .rcParams ['figure.figsize' ] = (8 , 6 )
8+ plt .rcParams ['figure.dpi' ] = 300
9+ plt .rcParams ['axes.labelsize' ] = 16
10+ plt .rcParams ['xtick.labelsize' ] = 15
11+ plt .rcParams ['ytick.labelsize' ] = 15
12+ plt .rcParams ['legend.fontsize' ] = 12
13+ plt .rcParams ['lines.linewidth' ] = 3
14+ plt .rcParams ['lines.markersize' ] = 8
15+ plt .rcParams ['axes.labelweight' ] = 'bold'
16+ plt .rcParams ['xtick.direction' ] = 'in'
17+ plt .rcParams ['ytick.direction' ] = 'in'
18+ plt .rcParams ['xtick.top' ] = True
19+ plt .rcParams ['ytick.right' ] = True
20+ plt .rcParams ['savefig.bbox' ] = 'tight'
21+
22+
23+ if False :
24+ U = np .linspace (- 4 ,4 , 200 )
25+ u = stats .norm .pdf (U , loc = 0 , scale = 1 )
26+ pi = 0.2
27+ z = pi * stats .norm .pdf (U , loc = - 2 , scale = 0.5 ) + (1 - pi ) * stats .norm .pdf (U , loc = 1 , scale = 0.5 )
28+
29+ plt .figure ()
30+ plt .plot (U ,u , linewidth = 3 )
31+ plt .fill_between (U , u * 0 , u , alpha = 0.5 )
32+ plt .savefig ("aiche_delete_l8r_blue" , dpi = 300 )
33+ plt .close ()
34+
35+ plt .figure ()
36+ plt .plot (U ,z , linewidth = 3 , color = "r" )
37+ plt .fill_between (U , u * 0 , z , alpha = 0.5 , color = "r" )
38+ plt .savefig ("aiche_delete_l8r_orange" , dpi = 300 )
39+ plt .close ()
40+
41+ if False :
42+ X = np .linspace (- 5 , 5 , 200 )
43+ shift = 6
44+ scale = 1
45+ x1 = stats .norm .pdf (X , 0 , 1 )
46+ x2 = 0.75 * stats .norm .pdf (X - shift , 0 - shift , 1.5 * scale )
47+ x3 = 0.9 * stats .norm .pdf (X + shift , 0 + shift , 1.25 * scale )
48+
49+ plt .figure (figsize = (10 , 4 ))
50+
51+
52+ plt .plot (X - shift , x2 , linewidth = 3 , color = "r" )
53+ plt .fill_between (X - shift , x2 * 0 , x2 , alpha = 0.3 , color = "r" )
54+
55+ plt .plot (X + shift , x3 , linewidth = 3 , color = "b" )
56+ plt .fill_between (X + shift , x3 * 0 , x3 , alpha = 0.3 , color = "b" )
57+
58+ plt .plot (X , x1 , linewidth = 3 , color = "g" )
59+ plt .fill_between (X , x1 * 0 , x1 , alpha = 0.3 , color = "g" )
60+
61+ plt .plot (X , x1 * 0 , linewidth = 3 , color = "k" )
62+ plt .plot (X - shift , x1 * 0 , linewidth = 3 , color = "k" )
63+ plt .plot (X + shift , x1 * 0 , linewidth = 3 , color = "k" )
64+ plt .savefig ("kl_divergence" , dpi = 300 )
65+
66+ import numpy as np
67+ import matplotlib .pyplot as plt
68+ from scipy .stats import norm
69+
70+ # Observed data
71+ np .random .seed (0 )
72+ x_observed = np .random .normal (loc = 2.5 , scale = 1.0 , size = 10 ) # Data with unknown mean and known variance
73+
74+ # Known parameters
75+ sigma = 1.0 # Known standard deviation of the observations
76+ n = len (x_observed ) # Number of observations
77+ sample_mean = np .mean (x_observed )
78+
79+ # Prior parameters
80+ mu_0 = 0.0 # Prior mean
81+ tau_0 = 1.0 # Prior standard deviation
82+
83+ # Posterior parameters
84+ tau_n_sq = 1 / (1 / tau_0 ** 2 + n / sigma ** 2 ) # Posterior variance
85+ mu_n = tau_n_sq * (mu_0 / tau_0 ** 2 + n * sample_mean / sigma ** 2 ) # Posterior mean
86+ tau_n = np .sqrt (tau_n_sq ) # Posterior standard deviation
87+
88+ # Range of mu values
89+ mu_values = np .linspace (1.5 , 4.5 , 200 )
90+
91+ # Calculate unnormalized posterior (prior * likelihood)
92+ prior = norm .pdf (mu_values , mu_0 , tau_0 )
93+ likelihood = norm .pdf (mu_values , sample_mean , sigma / np .sqrt (n ))
94+ unnormalized_posterior = prior * likelihood
95+
96+ # Scale up unnormalized posterior for visibility
97+ unnormalized_posterior_scaled = unnormalized_posterior * 200 # Adjust this factor as needed
98+
99+ # Calculate normalized posterior
100+ normalized_posterior = norm .pdf (mu_values , mu_n , tau_n )
101+
102+ # Plotting
103+ plt .figure (figsize = (5 , 5 ))
104+
105+ # Unnormalized posterior with shading (scaled up)
106+ plt .plot (mu_values , unnormalized_posterior_scaled , label = "Unnormalized" , linestyle = "--" , color = "blue" )
107+ plt .fill_between (mu_values , unnormalized_posterior_scaled , color = "blue" , alpha = 0.2 )
108+
109+ # Normalized posterior with shading
110+ plt .plot (mu_values , normalized_posterior , label = "Normalized" , color = "red" )
111+ plt .fill_between (mu_values , normalized_posterior , color = "red" , alpha = 0.2 )
112+
113+ # Annotation to highlight normalization importance
114+ plt .text (4.0 , 0.8 , "Area = 1" , color = "red" , fontsize = 16 , ha = "center" , backgroundcolor = "white" )
115+ plt .text (4.0 , 0.5 , "Area < 1" , color = "blue" , fontsize = 16 , ha = "center" , backgroundcolor = "white" )
116+
117+ # Labels and legend
118+ plt .xlabel (r"$\mu$" )
119+ plt .ylabel ("Density" )
120+ plt .legend (loc = "upper right" , fontsize = 14 )
121+ plt .ylim (0 ,1.4 )
122+
123+ plt .savefig ("normalized_density_fig" , dpi = 300 )
124+
125+ import os
126+ import torch
127+ from linfa .models .discrepancy_models import PhysChem
128+ import matplotlib .pyplot as plt
129+
130+
131+
132+ samples = np .loadtxt ('results/TP15_no_disc_error_estimation_aiche/TP15_no_disc_error_estimation_aiche_outputs_lf+noise_6000' )
133+ observations = np .loadtxt ("observations.csv" , skiprows = 1 , delimiter = ',' )
134+
135+
136+ # Set variable grid
137+ T = [50.0 , 400.0 , 450.0 ]
138+ P = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]
139+
140+ samples = samples .reshape (3 ,5 ,5000 )
141+
142+
143+ # Plot the samples
144+ for i in range (5000 ):
145+ plt .plot (P , samples [0 , :, i ], 'm-' , linewidth = 0.005 )
146+ plt .plot (P , samples [1 , :, i ], 'r-' , linewidth = 0.005 )
147+ plt .plot (P , samples [2 , :, i ], color = "orange" , linestyle = '-' , linewidth = 0.005 )
148+
149+ # Plot observations
150+ plt .plot (observations [:, 1 ], observations [:, 2 ], 'ko' )
151+
152+ # Add custom legend entries by plotting representative lines
153+ plt .plot ([], [], 'm-' , label = "350 K" ) # Magenta line
154+ plt .plot ([], [], 'r-' , label = "400 K" ) # Red line
155+ plt .plot ([], [], color = "orange" , linestyle = '-' , label = "450 K" ) # Orange line
156+ plt .plot ([], [], 'ko' , label = "Observations" ) # Black circles for observations
157+
158+ # Add legend, labels, and limits
159+ plt .legend ()
160+ plt .xlim (1 , 5 )
161+ plt .xlabel ("Pressure, $P$ [Pa]" )
162+ plt .ylabel ("Coverage" )
163+ plt .savefig ("function" , dpi = 300 )
164+
165+ # for i in range(5000):
166+ # plt.plot(P, samples[0,:,i],'m-', linewidth = 0.005)
167+ # plt.plot(P, samples[1,:,i],'r-', linewidth = 0.005)
168+ # plt.plot(P, samples[2,:,i],color = "orange", linestyle = '-', linewidth = 0.005)
169+ # plt.plot(observations[:,1], observations[:,2], 'ko')
170+ # plt.xlim(1,5)
171+ # plt.xlabel("Pressure, $P$ [Pa]")
172+ # plt.ylabel("Coverage")
173+ # plt.savefig("function", dpi = 300)
174+ # exit()
0 commit comments