-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoise_plots.py
More file actions
174 lines (102 loc) · 4.38 KB
/
Copy pathnoise_plots.py
File metadata and controls
174 lines (102 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""
Created on May 16, 2016
@author: Inom Mirzaev
Plots the generated data from noise_effect.py.
"""
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import cPickle, os
import model_rates as mr
#read all the files in the folder
fnames = []
for file in os.listdir("data_files"):
if file.endswith("noise.pkl"):
fnames.append(file)
#Open the specific file
myfile = fnames[-1]
ext = '_alpha_'+str( myfile[-11] )
pkl_file = open(os.path.join( 'data_files' , myfile ) , 'rb')
data = cPickle.load( pkl_file )
pkl_file.close()
a = []
# Total variance norm. See Gibbs and Su (2009) for the definition.
tv_norm = np.zeros( len(data) )
N = len(data[0][-1])
dx = ( mr.x1 - mr.x0 ) / N
for cnum in range(len(data)):
a.append( data[cnum][0] )
#Compute total variation norm
tv_norm[cnum] = np.sum( np.max ( np.abs ( data[cnum][-2]-data[cnum][-1] ) , axis=1 ) ) *dx
a = np.asarray( a )
min_ind = np.argmin( tv_norm )
f_fit = data[ min_ind ][-1]
f_true = data[ min_ind ][-2]
f_init = data[ min_ind ][-3]
plt.close('all')
#==============================================================================
# plot of approximate conditional measure (cdf)
#==============================================================================
fig1 = plt.figure(1)
#Colormap for the imshow plots
my_cmap = plt.get_cmap('Set2')
plt.title('$F_{30} (x,\ y)$', fontsize=20, y=1.04)
plt.xlabel('$x$', fontsize=20)
plt.ylabel('$y$', fontsize=20)
plt.imshow(np.flipud(f_fit), interpolation='nearest', cmap=my_cmap , \
vmin=0.001, vmax = np.max( ( np.max(f_fit) , np.max(f_true) ) ) , extent=(0,1,0,1))
cbar_ax = fig1.add_axes([0.85, 0.13, 0.03, 0.75])
plt.colorbar(cax=cbar_ax)
#==============================================================================
# plot of true conditional measure (cdf)
#==============================================================================
fig2 = plt.figure(2)
plt.title('$F_{0} (x,\ y)$', fontsize=20, y=1.04)
plt.xlabel('$x$', fontsize=20)
plt.ylabel('$y$', fontsize=20)
plt.imshow( np.flipud(f_true), interpolation='nearest', cmap=my_cmap , \
vmin=0.001, vmax = np.max( ( np.max(f_fit) , np.max(f_true) ) ) , extent=(0,1,0,1))
cbar_ax = fig2.add_axes([0.85, 0.13, 0.03, 0.75])
plt.colorbar(cax=cbar_ax)
#==============================================================================
# Error between true and fit
#==============================================================================
fig3 = plt.figure(3)
plt.title('Absolute error for $F_{0}$ and $F_{30}$', fontsize=16, y=1.04)
aa = np.abs( f_fit - f_true )
imgplot = plt.imshow(np.flipud( aa ) , interpolation='nearest', cmap='Reds' , \
vmin = 0, vmax = np.max( aa ) , extent=(0,1,0,1))
cbar_ax = fig3.add_axes([0.85, 0.13, 0.03, 0.75])
plt.colorbar(cax=cbar_ax)
plt.xlabel('$x$', fontsize=20)
plt.ylabel('$y$', fontsize=20)
#==============================================================================
# Error with respect to t_f
#==============================================================================
plt.figure(4)
plt.plot(a , tv_norm , color='blue', linewidth=1 , marker='o' , markersize=5 )
x1, x2, y1, y2 = plt.axis()
plt.axis( (x1-1, x2+1 , y1, y2))
#plt.xticks( range(5, 31, 5) )
plt.ylabel( r'$\rho_{TV} \left ( F_{0} , \ F_{30} \right )$', fontsize=20 )
plt.xlabel( '$\sigma$', fontsize=20 )
fig_name = 'noise_error'+ext+'.png'
plt.savefig( os.path.join( 'images' , fig_name ) , dpi=400 , bbox_inches='tight' )
#==============================================================================
# plot of cdf for fixed y
#==============================================================================
f, ax = plt.subplots(2, sharex=True)
grid = np.linspace( mr.x0 , mr.x1 , len(f_true) )
mm = int( len(f_true)/2 )
ax[0].plot( grid, f_true[ mm ] , color='b' , linewidth=2 , label='True' )
ax[0].plot( grid, f_fit[ mm ] , color='r' , linewidth=2 , label='Fit')
ax[0].set_ylim( [ 0 , 1.1 ] )
ax[0].legend( loc = 'best' )
ax[0].set_ylabel( '$F(x,\ 0.5)$' , fontsize=20)
mm = -1
ax[1].plot( grid, f_true[ mm ] , color='b' , linewidth=2 , label='True' )
ax[1].plot( grid, f_fit[ mm ] , color='r' , linewidth=2 , label='Fit')
ax[1].set_ylim([0,1.1])
ax[1].set_xlabel('$x$', fontsize=20)
ax[1].set_ylabel('$F(x,\ 1.0)$', fontsize=20)