-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchoring_fmm
371 lines (245 loc) · 9.64 KB
/
anchoring_fmm
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#Please ignore any references to sales price/house attributes/demographics in the comments
#I wrote the codes initially for a hedonics class and simply replaced the variables with variables from the CV dataset without modifying the comments
library(readxl)
library(tidyr)
rm(list=ls())
#data <- read.table("HHdata.csv", header=TRUE, sep=",")
data <- read_excel("CV data set - Copy.xlsx")
data <- subset(data, BID > -999)
Y <- I(data$BID)
#house attributes
X <- cbind(1,I(data$PRICE), I(data$PRICE*data$PRICE), I(data$PRICE*data$PRICE*data$PRICE))
#demographic variables/mixing variables
Z <- cbind(1,data$MAINTAIN, I(data$TIME), data$BURDEN, data$PRICE)
ols_agg <- lm(Y~X-1);
summary(ols_agg)
#starting values for the hedonic estimates/betas for each type i.e. for mixing algorithm
types <- 3;
beta_start <- matrix(ols_agg$coef,(types*ncol(X)),1);
#starting values for the gamma estimates for the demographic variables
#gamma_start <- matrix(0.01,(1*ncol(Z)),1);
gamma_start <- matrix(abs(rnorm(types*ncol(Z),mean=0.01,sd=0.01)),(types*ncol(Z)),1);
#starting values for sigma
sigma_start <- matrix(sqrt(mean(ols_agg$residuals^2)),types,1)
#collecting initializing values
val_start <- c(beta_start,gamma_start,sigma_start);
vals <- val_start;
#convergence criteria comparing new and old estimates:
Iter_conv <- 0.0001;
j <- types;
#number of independent variables or beta estimates we need to keep track of - so to use when indexing
niv <- ncol(X);
#number of demographic variables to use when indexing
gvs <- ncol(Z);
#row dim of aggregate
n <- nrow(X);
conv_cg = 5000;
conv_cb = 5000;
par <- val_start
#Probability Density Function
#FnOne prob density of observing prices given mean of cross product of house attributes and current iteration of hedonic estimates and sigma (sd)
#Mean is calculated as the product of house attributes (x) and the hedonic price estimates (par[-1], excluding the first element which is the sd).
FnOne <- function(par,x,y)
{
dnorm(y, mean=x%*%par[-1], sd = par[1], log=FALSE)
}
#FnTwo max prob densities over type probabilities
#Maximum Likelihood for Hedonic Price Estimates
#Function loops over different types (j) and computes the probability density for each type using FnOne
#Then sums the logarithms of these densities, weighted by the type probabilities (d), which is a standard approach in maximum likelihood estimation
#Function is used to update the hedonic price estimates (beta) during the optimization process in FMM
FnTwo <- function(par,d,x,y)
{
pdy <- matrix(0,n,j)
b <- par[1:(niv*j)]
s <- par[(niv*j+1):((niv+1)*j)]
for (i in 1:j)
{
pdy[,i] <- FnOne(c(s[i],b[((i-1)*niv+1):(i*niv)]),X,Y)
}
sum(d*log(pdy))
}
#FnThree logit for gamma estimates
#Computes the logistic function of the demographic variable estimates
#(The exponential (exp) of the product of demographic variables (z) and their coefficients (g))
FnThree <- function(g,z)
{
L <- exp(z%*%g)
}
#FnFour max gamma estimates, type probabilities
#Maximizes the logistic function over different types, essentially refining the estimates of demographic variable effects (gamma)
#Constructs a matrix L to store the logistic function values for each type
#Then calculates the probabilities (Pi) of each house belonging to each type based on these values
#Returns the sum of the logarithms of these probabilities, which is used in the optimization process to update the demographic variable estimates
FnFour <- function(par, d, z, y, n, j, gvs) {
n <- nrow(X)
#j <- types
gvs <- ncol(Z)
L <- matrix(0, n, j)
L[,1] <- 1
# Set columns to 1 based on the value of types
if (j >= 3) {
L[,3] <- 1
}
if (j >= 4) {
L[,4] <- 1
}
# Continue with the original functionality
minus <- j - 1
for (m in 1:(j - minus)) {
L[, (m + 1)] <- FnThree(par[((m - 1) * gvs + 1):(m * gvs)], z)
}
Pi <- L / apply(L, 1, sum)
sum(apply(d * log(Pi), 1, sum))
}
#mixing algorithm
start.time <- Sys.time()
set.seed(12345)
FMM <- function(par,X,Z,y) {
#separating betas, gamma and sigma from par
b <- par[1:(j*niv)];
if (types == 2) {
#g <- par[(j * niv + 1):(j * (niv + gvs) - gvs)]
g <- par[(j*niv+1):((j*(niv+gvs)))]
s <- par[-(1:(length(par) - types))]
} else {
g <- par[(j*niv+1):((j*(niv+gvs)))]
s <- par[-(1:(length(par) - types))]
}
#empty matrix to store probs
L <- matrix(0,n,j);
f <- L;
d <- L;
b <- matrix(b,niv,j);
iter <- 0
while (abs(conv_cg) + abs(conv_cb) > Iter_conv) {
#store parameter estimates of preceding iteration of mix through loop
beta_old <- b;
gamma_old <- g;
#counter for while loop
iter <- iter+1
#Computes the probability density of observing house prices given the mean of the cross-product of
#house attributes and current hedonic estimates, along with a standard deviation parameter
for (i in 1:j)
{
f[,i] <- FnOne(c(s[i],b[,i]),X,Y)
}
#updating a column of L based on the demographic variables (Z) and their respective coefficients (g)
minus <- types - 1
for (i in 1:(j - minus)) {
L[,1] <- 0
if (types > 2) {
L[, (i + 1):(types - 1)] <- 0 # This sets the range of columns to 0 based on types
}
L[,(i + 1)] <- Z %*% g[((i - 1) * gvs + 1):(i * gvs)]
}
#estimate Pi (P) and individual probabilities of belonging to a certain type (d):
P <- exp(L)/(1+apply(exp(L[,(1:j)]),1,sum))
for (i in 1:n)
{
d[i,] <- P[i,]*f[i,]/sum(P[i,]*f[i,])
}
#use individual probs (d) to estimate beta (b), gamma (g)
#maximizes probability densities
#function loops over different types (j) and computes the probability density for each type using FnOne
#then sums the logarithms of these densities, weighted by the type probabilities (d), which is a standard approach in maximum likelihood estimation
#function is used to update the hedonic price estimates (beta) during the optimization process in FMM
b1 <- matrix(b,(niv*j),1); par1 <- c(b1,s);
beta_m <- optim(par1,FnTwo,d=d,x=X,y=Y,control=list(fnscale=-1,maxit=100000))
b <- matrix(beta_m$par[1:(j*niv)],niv,j)
s <- beta_m$par[(j*niv+1):(j*(niv+1))]
#maximizes the logistic function (FnThree) over different types essentially refining the estimates of demographic variable effects (gamma)
types <- j
gam_m <- optim(g,FnFour,j=types,d,z=Z,Y,control=list(fnscale=-1,maxit=100000))
g <- gam_m$par
#setting up convergence check
conv_cg <- sum(abs(g-gamma_old))
conv_cb <- sum(abs(b-beta_old))
#collecting parameter estimates to use to impute LL
par2 <- matrix(b,(niv*j),1)
par2 <- c(par2,s)
#types <- j
LL <- FnTwo(par2,d=d,x=X,y=Y) + FnFour(g,d=d,z=Z,y=Y, j=types);
#storing
bvector <- matrix(b,j*niv,1)
vals_fin <- c(bvector,g,s)
dvector <- d
}
#collecting parameters for output
out_pars <- list("vals_fin" = vals_fin, "i_type" = d)
print(b)
print(g)
print(iter)
#return list of estimates - index for subsetting in final updating
return(out_pars)
}
#calling:
mix <- FMM(val_start,X=X,Z=Z,y=Y)
end.time <- Sys.time()
start.time-end.time
#send_message(message_body=paste("Operation complete", Sys.time(), start.time-end.time))
#final updating:
d <- mix$i_type
b <- mix$vals_fin[1:(j*niv)];
g <- mix$vals_fin[(j*niv+1):(length(par)-types)];
s <- mix$vals_fin[-(1:(length(par)-types))];
b <- matrix(b,niv,j); #betas
b1 <- matrix(b,(niv*j),1);
par3 <- c(b1,s);
LL <- FnTwo(par3,d=d,x=X,y=Y) + FnFour(g,d=d,z=Z,y=Y, j=types);
AIC <- -2*LL+2*niv
#standard errors
#this section is problematic because:
#(i) the optim function encounters non-finite values when it was calculating the gradient of the function FnTwo (betas) and FnFour (gammas)
#OR
#(ii) the Hessian matrix (gamma_opt$hessian or beta_opt$hessian) is singular, so it cannot be inverted
#OR
#(iii) the objective function cant be evaluated at the starting values
#for (i), you can work around by playing with the step size for gradient approximation (the first argument in "rep" function, which I keep constant at 1e-8 or 1e-7)
#for (iii), you can try changing the starting values (the first argument in the optim function)
#for (ii), I dont have a solution (yet)
beta_opt <- optim(par3, FnTwo, d=d, x=X, y=Y,
control=list(fnscale=-1, ndeps=rep(1e-8, length(par3)), maxit=100000),
hessian=TRUE)
b <- matrix(beta_opt$par[1:(j * niv)], niv, j)
bse <- matrix(nrow = niv, ncol = types)
for (t in 1:types) {
start_index <- (t - 1) * niv + 1
end_index <- t * niv
bse[, t] <- sqrt(-diag(solve(beta_opt$hessian[start_index:end_index, start_index:end_index])))
}
b
bse
g <- matrix(NA, nrow = gvs, ncol = types)
gse <- matrix(NA, nrow = gvs, ncol = types)
g_val <- mix$vals_fin[(j*niv+1):(length(par)-types)]
max_types <- types
rep_values <- c(1e-7, 1e-7, 1e-7, 1e-7, 1e-7)
for (t in 1:max_types) {
start_index <- (t - 1) * gvs + 1
end_index <- t * gvs
gamma_opt <- optim(
par = g_val[start_index:end_index],
fn = FnFour,
j = types,
d = d,
z = Z,
y = Y,
control = list(
fnscale = -1,
ndeps = rep(rep_values[t], length(g_val[start_index:end_index])),
maxit = 100000
),
hessian = TRUE
)
g[, t] <- gamma_opt$par
gse[, t] <- sqrt(-diag(solve(gamma_opt$hessian[1:gvs, 1:gvs])))
}
g
gse
d #this is the latent probabilities
s #sigma
b #beta values
bse #beta standard errors
g #gamma values
gse #gamma standard errors