-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSGS_Bayesian.m
156 lines (122 loc) · 3.61 KB
/
SGS_Bayesian.m
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
%% SGS with Bayesian updating
NST;%%normal score transform the data
nrlzn=5;%%% number of realizations
for t=1:nrlzn
Data=Data_nst;
Nx=40;
Ny=40;
dx=1;
dy=1;
L=[1:Nx*Ny];
Data_SGS=Data;%%initialize the data structure to which
%new conditioning data will be added
Data_SGS.varperm=zeros(length(Data.x),1);
sill=1;
rho=0.8; %coefficient of correlation
iter=length(Data.x);
%% start SGS loop
while L %%while there are locations left in grid
%%choose 1st location
pos=randi(length(L));
index=L(pos);
L(pos)=[];
%% calculate x and y index and co-ordinates of randomly selected grid centre
if mod(index,Nx)==0
xindex=Nx;
else
xindex=mod(index,Nx);
end
yindex=((index-xindex)/Ny) + 1;
x_coord=(xindex-1)*dx+dx/2;
y_coord=(yindex-1)*dy+dy/2;
%% extract the k nearest neighbours
N=40;
List=[Data_SGS.x Data_SGS.y];
Query=[x_coord y_coord];
idx=knnsearch(List,Query,'k',N);
idx=idx(5:end);
N=N-4;
%% extract a matrix of the known nearest points which will be used for kriging
Dummy.x=Data_SGS.x(idx);
Dummy.y=Data_SGS.y(idx);
Dummy.lnperm=Data_SGS.lnperm(idx);
Dummy.varperm=Data_SGS.varperm(idx);
%% formulate the left hand side matrix for kriging
for i=1:N
for j=1:N
Coord1=[Dummy.x(i) Dummy.y(i)];
Coord2=[Dummy.x(j) Dummy.y(j)];
% get covariance from function
cov=vargm(Coord1,Coord2);
A(i,j)=cov;
end
end
%% cholesky decomposition of A
LA=chol(A,'lower');
%% calculate global mean
u= mean(Data_SGS.lnperm);
%% calculate grid centers
grid_dimensions
%% formulate the right hand matrix for all the grid centers
for j=1:N
Coord1=[x_coord y_coord];
Coord2=[Dummy.x(j) Dummy.y(j)];
%get covariance
cov=vargm(Coord1,Coord2);
B(j,1)=cov;
end
%solve Lz=B;
z=LA\B;
%solve L'x=z;
lambda=LA'\z;
lambda0=u*(1-sum(lambda));
SK_est=lambda0+ lambda'*Dummy.lnperm;%%kriging mean
SK_var=sill- lambda'*B;%% kriging variance
%% bayesian update
zi=Data_sec_nst(yindex,xindex);
SK_est=(rho*zi*SK_var+ SK_est*(1-rho^2))/(rho^2*(SK_var-1)+1);%%updated mean
SK_var=SK_var*(1-rho^2)/(rho^2*(SK_var-1)+1);%%updated variance
%%Random selction of location data value
p=rand(1); %% choose a random value b/w 0&1;
Gauss_est=norminv(p,SK_est,SK_var);
%% update dataset to include newly calculated data value
iter=iter+1;
Data_SGS.x(iter,1)=x_coord;
Data_SGS.y(iter,1)=y_coord;
Data_SGS.xindex(iter,1)=xindex;
Data_SGS.yindex(iter,1)=yindex;
Data_SGS.lnperm(iter,1)=Gauss_est;
Data_SGS.varperm(iter,1)=SK_var;
iter-64
end
RLZN=zeros(Ny,Nx);
for i=65:iter
%assign the dat values to the location on the grid for a particular
%realization
xindex=Data_SGS.xindex(i);
yindex=Data_SGS.yindex(i);
lnperm=Data_SGS.lnperm(i);
%back transforming to original data space
pt=normcdf(lnperm);
lnperm=interp1(P,rearrdata,pt);
RLZN(yindex,xindex)=lnperm;
end
Realization(t).RLZN=exp(RLZN);%% convert to permeability
disp('realization complete');
end
%% plotting
for i=1:nrlzn
subplot(3,2,i);
imagesc((Realization(i).RLZN));
if i==1
cl = caxis; %# get color limits from the 1st image
else
caxis(cl) %# apply the same color limits to other images
end
set(gca,'YDir','Normal');
xlabel('East');
ylabel('North');
s=strcat('Realization',num2str(i));
title(s);
colorbar;
end