-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiseq_maxim.R
1331 lines (1124 loc) · 47 KB
/
diseq_maxim.R
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(data.table)
library(GenSA)
library(DEoptim)
library(gsubfn)
library(numDeriv)
library(mvtnorm)
library(foreach)
library(parallel)
# makes it possible to unpack lists returned by functions
list <- structure(NA, class = "result")
"[<-.result" <- function(x, ..., value) {
args <- as.list(match.call())
args <- args[-c(1:2, length(args))]
length(value) <- length(args)
for (i in seq(along = args)) {
a <- args[[i]]
if (!missing(a))
eval.parent(substitute(a <- v, list(a = a, v = value[[i]])))
}
x
}
# Function: loglike.diseq.tobit
# Description: Calculates the log-likelihood for a disequilibrium model ala Maddala & Nelson (1974).
# Parameters:
# - beta: Vector of coefficients.
# - y: Vector of observed dependent variable.
# - X: Matrix of independent variables.
# - idx_bd: Indices of coefficients for the dependent variable mean equation.
# - idx_bs: Indices of coefficients for the dependent variable selection equation.
# - idx_sd: Index of the coefficient for the standard deviation of the dependent variable mean equation.
# - idx_ss: Index of the coefficient for the standard deviation of the dependent variable selection equation.
# - likelihood_bias: Bias term added to the likelihood to avoid taking the logarithm of zero.
# Returns:
# - The negative log-likelihood value.
loglike.diseq.tobit <- function (beta, y, X, idx_bd, idx_bs, idx_sd, idx_ss, likelihood_bias = 0) {
theta1 <- X[, idx_bd, drop = FALSE] %*% beta[idx_bd]
theta2 <- X[, idx_bs, drop = FALSE] %*% beta[idx_bs]
sigma1 <- exp(beta[idx_sd])
if (is.null(idx_ss)) {
# when the sigmas are fixed to be equal
sigma2 <- sigma1
} else {
sigma2 <- exp(beta[idx_ss])
}
f1 <- dnorm(y, mean=theta1, sd=sigma1)
f2 <- dnorm(y, mean=theta2, sd=sigma2)
F1 <- 1 - pnorm((y-theta1) / sigma1)
F2 <- 1 - pnorm((y-theta2) / sigma2)
G <- ifelse(y > 0, f1*F2 + f2*F1, 1 - F1*F2)
-sum(log(likelihood_bias + G))
}
# Function: mvnorm_exact
# Description: Calculates the joint probabilities of two normal random variables being below zero.
# Parameters:
# - theta1: Mean of the first normal random variable.
# - sigma1: Standard deviation of the first normal random variable.
# - theta2: Mean of the second normal random variable.
# - sigma2: Standard deviation of the second normal random variable.
# - rho: Correlation coefficient of the two normal random variables.
# Returns:
# - The joint probabilities of the two normal random variables being below zero.
mvnorm_exact <- function(theta1, sigma1, theta2, sigma2, rho) {
mvnorm_scalar_fun <- function(theta1, sigma1, theta2, sigma2, rho) {
pmvnorm(lower = c(-Inf, -Inf),
upper = c(0, 0),
mean = c(theta1, theta2),
sigma = matrix(c(sigma1^2, sigma1*sigma2*rho,
sigma1*sigma2*rho, sigma2^2),
nrow = 2)
)[1]
}
mapply(mvnorm_scalar_fun, theta1, sigma1, theta2, sigma2, rho)
}
# Function: mvnorm_exact
# Description: Calculates the approximate joint probabilities of two normal random variables being below zero. Faster than mvnorm_exact.
# Parameters:
# - theta1: Mean of the first normal random variable.
# - sigma1: Standard deviation of the first normal random variable.
# - theta2: Mean of the second normal random variable.
# - sigma2: Standard deviation of the second normal random variable.
# - rho: Correlation coefficient of the two normal random variables.
# Returns:
# - The joint probabilities of the two normal random variables being below zero.
mvnorm_approx <- function(theta1, sigma1, theta2, sigma2, rho) {
a <- -theta1 / sigma1
b <- -theta2 / sigma2
c <- ifelse(abs(a) >= abs(b), a, b)
d <- ifelse(abs(a) >= abs(b), b, a)
h <- ifelse(c <= 0, c, -c)
k <- d
r <- ifelse(c <= 0, rho, -rho)
mu_approx <- -r * ifelse(h > -30, dnorm(h) / pnorm(h), -h)
sigma_approx <- 1 + r * h * mu_approx - mu_approx ^ 2
B <- pnorm(h) * pnorm(k, mean = mu_approx, sd = sqrt(sigma_approx))
F_00 <- ifelse(c <= 0, B, pnorm(k) - B)
return(F_00)
}
# Function: loglike.diseq.tobit.corr
# Description: Calculates the log-likelihood for a disequilibrium model with correlated errors.
# Parameters:
# - beta: Vector of coefficients.
# - y: Vector of observed dependent variable.
# - X: Matrix of independent variables.
# - idx_bd: Indices of coefficients for the dependent variable mean equation.
# - idx_bs: Indices of coefficients for the dependent variable selection equation.
# - idx_sd: Index of the coefficient for the standard deviation of the dependent variable mean equation.
# - idx_ss: Index of the coefficient for the standard deviation of the dependent variable selection equation.
# - likelihood_bias: Bias term added to the likelihood to avoid taking the logarithm of zero.
# Returns:
# - The negative log-likelihood value.
loglike.diseq.tobit.corr <- function (beta, y, X, idx_bd, idx_bs, idx_sd, idx_ss, idx_corr, likelihood_bias = 0) {
theta1 <- X[, idx_bd, drop = FALSE] %*% beta[idx_bd]
theta2 <- X[, idx_bs, drop = FALSE] %*% beta[idx_bs]
sigma1 <- exp(beta[idx_sd])
if (is.null(idx_ss)) {
# when the sigmas are fixed to be equal
sigma2 <- sigma1
} else {
sigma2 <- exp(beta[idx_ss])
}
rho <- tanh(beta[idx_corr])
# y > 0 eset (F1, F2 feltételes eloszlások) - Maddala & Nelson
f1 <- dnorm(y, mean=theta1, sd=sigma1)
f2 <- dnorm(y, mean=theta2, sd=sigma2)
F1 <- 1 - pnorm(y,
mean = theta1 + sigma1 / sigma2 * rho * (y - theta2),
sd = sqrt(1 - rho^2) * sigma1)
F2 <- 1 - pnorm(y,
mean = theta2 + sigma2 / sigma1 * rho * (y - theta1),
sd = sqrt(1 - rho^2) * sigma2)
# y < 0 esetre együttes eloszlás közelítése - Mee & Owen 1982
F_00 <- mvnorm_approx(theta1, sigma1, theta2, sigma2, rho)
G <- ifelse(
y > 0,
f1*F2 + f2*F1,
pnorm(0, mean=theta1, sd=sigma1) + pnorm(0, mean=theta2, sd=sigma2) - F_00
)
-sum(log(likelihood_bias + G))
# if (is.na(ret)) {
# return(Inf)
# } else {
# return(ret)
# }
# From DEOptim docs: Note that DEoptim stops if any NA or NaN value is obtained.
# You have to redefine your function to handle these values (for instance, set
# NA to Inf in your objective function).
}
# Function: model.matrix.diseq
# Description: Creates the model matrix and the corresponding coefficient indices for a disequilibrium model.
# Parameters:
# - demand_formula: Formula for the demand equation.
# - supply_formula: Formula for the supply equation.
# - data: Data frame containing the variables in the formulas.
# - corr: Whether the model has correlated errors.
# - equal_sigmas: Whether the errors on the demand and supply sides are assumed to have equal standard deviations.
# Returns:
# - A list containing the model matrix and the coefficient indices.
model.matrix.diseq <- function (demand_formula, supply_formula, data, corr=FALSE, equal_sigmas=FALSE) {
X_d <- model.matrix(formula(demand_formula), data = data)
X_s <- model.matrix(formula(supply_formula), data = data)
rows <- intersect(rownames(X_d), rownames(X_s))
n_d <- ncol(X_d)
n_s <- ncol(X_s)
coef_indices <- list(
'beta_demand' = 1 : n_d,
'beta_supply' = (n_d+1) : (n_d+n_s),
'sigma_demand' = n_d+n_s+1,
'sigma_supply' = if (equal_sigmas) {NULL} else {n_d+n_s+2},
'sigma_corr' = if (!corr) {NULL} else if (equal_sigmas) {n_d+n_s+2} else {n_d+n_s+3}
)
# Unavailable indices are NULL
list(cbind(X_d[rows, ], X_s[rows, ]), coef_indices)
}
# Function: fitdiseq
# Description: Fits a disequilibrium model to the data.
# Parameters:
# - demand_formula: Formula for the demand equation.
# - supply_formula: Formula for the supply equation.
# - data: Data frame containing the variables in the formulas.
# - lb: Lower bounds for the coefficients.
# - ub: Upper bounds for the coefficients.
# - init: Initial values for the coefficients.
# - initpop: Initial population for the DE algorithm.
# - equal_sigmas: Whether the errors on the demand and supply sides are assumed to have equal standard deviations.
# - corr: Whether the model has correlated errors.
# - optimizer: The optimization algorithm to use. Can be 'SA', 'DE', or 'optim'.
# - control: Control parameters for the optimization algorithm.
# - method: The optimization method to use when optimizer is 'optim'.
# - na.action: The NA handling function to use.
# - random_seed: The random seed to use.
# - elapsed_times: A list of elapsed times from previous optimization runs.
# - prev_history: A list of optimization traces from previous optimization runs.
# - fixed_params: A vector of fixed parameters.
# - likelihood_bias: A bias term added to the likelihood to avoid taking the logarithm of zero.
# Returns:
# - The fitted disequilibrium model (diseq object).
fitdiseq <- function(demand_formula,
supply_formula,
data,
lb = NULL,
ub = NULL,
init = NULL,
initpop = NULL,
equal_sigmas = FALSE,
corr = FALSE,
optimizer = 'SA',
control = (if (optimizer == 'SA') {list('verbose' = TRUE, 'max.time' = 1200)}
else if (optimizer == 'DE') {DEoptim.control(trace = TRUE, itermax = 1000)}
else if (optimizer == 'optim') {list('trace' = TRUE)}),
method = 'Nelder-Mead',
na.action = na.exclude,
random_seed = 1991,
elapsed_times = list(),
prev_history = list(),
fixed_params = NULL,
likelihood_bias = 0
) {
cl <- match.call()
mf <- na.action(data[, union(all.vars(demand_formula), all.vars(supply_formula)), with=FALSE])
attr(mf, 'demand_terms') <- terms(demand_formula)
attr(mf, 'supply_terms') <- terms(supply_formula)
y <- mf[[all.vars(demand_formula[[2]])]]
list[X, coef_indices] <- model.matrix.diseq(demand_formula, supply_formula, data=mf,
corr=corr, equal_sigmas=equal_sigmas)
idx_bd <- coef_indices[['beta_demand']]
idx_bs <- coef_indices[['beta_supply']]
idx_sd <- coef_indices[['sigma_demand']]
idx_ss <- coef_indices[['sigma_supply']] # A NULL here automatically signals loglike to use sigma1 for sigma2
idx_corr <- coef_indices[['sigma_corr']]
num_of_params <- length(idx_bd) + length(idx_bs)
if (equal_sigmas) {
num_of_params <- num_of_params + 1
} else {
num_of_params <- num_of_params + 2
}
if (corr) {
num_of_params <- num_of_params + 1
}
# Note: no automatic bounds will be created if not provided explicitely in the function call.
# If the optimizer needs them, let it throw an exception.
if (length(initpop) == 1 && initpop == FALSE) {
# FALSE/NULL should only make a difference when calling refitdiseq
initpop <- NULL
}
if (!is.null(lb) && length(lb) != num_of_params) {
stop("Incorrect lower bound size")
}
if (!is.null(ub) && length(ub) != num_of_params) {
stop("Incorrect upper bound size")
}
if (!is.null(init) && length(init) != num_of_params) {
stop("Incorrect initial vector size")
}
if (!is.null(initpop) && ncol(initpop) != num_of_params) {
stop('Incorrect size of vectors in the initial population.')
}
if (!is.null(fixed_params) && length(fixed_params) != num_of_params) {
stop('Incorrect parameter fixing vector size.')
}
if (!corr) {
loglike <- function(beta) loglike.diseq.tobit(beta, y, X, idx_bd, idx_bs, idx_sd, idx_ss, likelihood_bias)
} else {
loglike <- function(beta) loglike.diseq.tobit.corr(beta, y, X, idx_bd, idx_bs, idx_sd, idx_ss, idx_corr, likelihood_bias)
}
orig_init <- init
orig_lb <- lb
orig_ub <- ub
orig_initpop <- initpop
if (!is.null(fixed_params)) {
if (is.null(init)) {
stop("An initial vector is required when fixing parameters")
}
objective_fun <- function(beta_restr) {
beta_full <- orig_init
beta_full[!fixed_params] <- beta_restr
loglike(beta_full)
}
# NULL indexed by anything is still NULL, so no change needed here
init <- orig_init[!fixed_params]
lb <- orig_lb[!fixed_params]
ub <- orig_ub[!fixed_params]
if (!is.null(initpop)) {
initpop <- orig_initpop[, !fixed_params]
}
} else {
objective_fun <- loglike
}
set.seed(random_seed)
t0 <- Sys.time()
cat(paste('Starting estimation at', t0, '\n'))
if (optimizer == 'SA') {
list[neg_log_likelihood_opt, beta_opt, history] <- GenSA(init, objective_fun, lb, ub, control = control)
} else if (optimizer == 'DE') {
if(!is.null(initpop)) {
control$initialpop <- initpop
} else if(!is.null(init)) {
NP <- ifelse(!is.na(control$NP), control$NP, 10 * length(init))
initrand <- runif((NP - 1) * length(init),
min = rep(lb, times = NP - 1),
max = rep(ub, times = NP - 1))
initmat <- matrix(c(init, initrand),
ncol = length(init),
nrow = NP,
byrow = TRUE)
control$initialpop <- initmat
control$NP <- NP
}
list[opt, member] <- DEoptim(objective_fun, lb, ub, control = control)
neg_log_likelihood_opt <- opt$bestval
beta_opt <- opt$bestmem
history <- list(member$bestvalit, member$bestmemit, member$pop, member$storepop)
} else if (optimizer == 'optim') {
if (is.null(init)) {
cat("No initial vector provided. Using all zeros as the starting point.")
init <- rep(0, num_of_params)
# Therefore the starting point for sigmas is exp(0) = 1
}
list[beta_opt, neg_log_likelihood_opt, counts, convergence] <-
optim(init, objective_fun, method = method, control = control)
history <- c(counts, convergence)
names(history) = c(names(counts), 'convergence')
} else {
stop('Unknown optimizer. Use one of: SA, DE, optim.')
}
t1 <- Sys.time()
cat(paste('Estimation finished at', t1, '\n'))
t_delta = difftime(t1, t0)
if (!is.null(fixed_params)) {
beta_opt_restr <- beta_opt
beta_opt <- orig_init
beta_opt[!fixed_params] <- beta_opt_restr
}
beta_names <- c(
paste0(colnames(X)[idx_bd], ' - d'),
paste0(colnames(X)[idx_bs], ' - s')
)
if (equal_sigmas) {
beta_names <- c(beta_names, 'sigma')
} else {
beta_names <- c(beta_names, 'sigma_demand', 'sigma_supply')
}
if (corr) {
beta_names <- c(beta_names, 'rho')
}
names(beta_opt) <- beta_names
coefficients <- beta_opt
coefficients[idx_sd] <- exp(coefficients[idx_sd])
if (!is.null(idx_ss)) {
coefficients[idx_ss] <- exp(coefficients[idx_ss])
}
if (corr) {
coefficients[idx_corr] <- tanh(coefficients[idx_corr])
}
diseq_obj <- list(
'coefficients' = coefficients,
'raw_coefficients' = beta_opt,
'coef_indices' = coef_indices,
'call' = cl,
'demand_terms' = terms(demand_formula),
'supply_terms' = terms(supply_formula),
'model' = mf,
'log_likelihood' = -neg_log_likelihood_opt,
'orig_rownames' = rownames(data),
'N' = nrow(mf),
'optim_trace' = c(prev_history, list(history)),
'settings' = list(
'lb' = orig_lb,
'ub' = orig_ub,
'init' = orig_init,
'initpop' = orig_initpop,
'optimizer' = optimizer,
'control' = control,
'method' = method,
'corr' = corr,
'equal_sigmas' = equal_sigmas,
'likelihood_bias' = likelihood_bias
),
'na.action' = na.action,
'random_seed' = random_seed,
'elapsed_times' = c(elapsed_times, list(t_delta))
)
attr(diseq_obj, 'class') <- 'diseq'
return(diseq_obj)
}
# Function: refitdiseq
# Description: Refits a disequilibrium model to the data. Non-specified parameters are taken from the original model.
# Parameters:
# - diseq_obj: The disequilibrium model to refit.
# - demand_formula: Formula for the demand equation.
# - supply_formula: Formula for the supply equation.
# - data: Data frame containing the variables in the formulas.
# - lb: Lower bounds for the coefficients.
# - ub: Upper bounds for the coefficients.
# - init: Initial values for the coefficients.
# - initpop: Initial population for the DE algorithm.
# - corr: Whether the model has correlated errors.
# - equal_sigmas: Whether the errors on the demand and supply sides are assumed to have equal standard deviations.
# - optimizer: The optimization algorithm to use. Can be 'SA', 'DE', or 'optim'.
# - control: Control parameters for the optimization algorithm.
# - method: The optimization method to use when optimizer is 'optim'.
# - na.action: The NA handling function to use.
# - random_seed: The random seed to use.
# - elapsed_times: A list of elapsed times from previous optimization runs.
# - prev_history: A list of optimization traces from previous optimization runs.
# - fixed_params: A vector of fixed parameters.
# - likelihood_bias: A bias term added to the likelihood to avoid taking the logarithm of zero.
# - continue: Whether to continue from the parameters of the previous model.
refitdiseq <- function(diseq_obj,
demand_formula = formula(diseq_obj$demand_terms),
supply_formula = formula(diseq_obj$supply_terms),
data = diseq_obj$model,
lb = diseq_obj$settings$lb,
ub = diseq_obj$settings$ub,
init = NULL,
initpop = NULL,
corr = diseq_obj$settings$corr,
equal_sigmas = diseq_obj$settings$equal_sigmas,
optimizer = diseq_obj$settings$optimizer,
control = diseq_obj$settings$control,
method = diseq_obj$settings$method,
na.action = diseq_obj$na.action,
random_seed = diseq_obj$random_seed,
elapsed_times = NULL,
prev_history = NULL,
fixed_params = NULL,
likelihood_bias = diseq_obj$settings$likelihood_bias,
continue = TRUE
) {
if (is.null(init)) {
if (continue) {
init <- diseq_obj$raw_coefficients
} else {
init <- diseq_obj$settings$init
}
}
if (optimizer == "DE" && is.null(initpop)) {
if (continue) {
n <- length(diseq_obj$optim_trace)
if (length(initpop) == 1 && initpop == FALSE) {
initpop <- NULL
} else if (!is.null(diseq_obj$optim_trace[[n]][[3]])) {
initpop <- initpop <- diseq_obj$optim_trace[[n]][[3]]
}
} else {
initpop <- diseq_obj$settings$initpop
}
}
if (is.null(elapsed_times)) {
if (continue) {
# Append the elapsed times with a new value
elapsed_times <- diseq_obj$elapsed_times
} else {
# Replace the last elapsed time with a new value
elapsed_times <- diseq_obj$elapsed_times[-length(diseq_obj$elapsed_times)]
}
}
if (is.null(prev_history)) {
if (continue) {
# Append the history with a new value
prev_history <- diseq_obj$optim_trace
} else {
# Replace the last step of the history with a new value
prev_history <- diseq_obj$optim_trace[-length(diseq_obj$elapsed_times)]
}
}
# The next three ifs are necessary for backward compatibility reasons
if(is.null(corr)) {
corr <- FALSE
}
if (is.null(equal_sigmas)) {
equal_sigmas <- FALSE
}
if(is.null(likelihood_bias)) {
likelihood_bias <- 0
}
fitdiseq(
demand_formula = demand_formula,
supply_formula = supply_formula,
data = data,
lb = lb,
ub = ub,
init = init,
initpop = initpop,
corr = corr,
equal_sigmas = equal_sigmas,
optimizer = optimizer,
control = control,
method = method,
na.action = na.action,
random_seed = random_seed,
elapsed_times = elapsed_times,
prev_history = prev_history,
fixed_params = fixed_params,
likelihood_bias = likelihood_bias
)
}
# Function: ll_contributions
# Description: Calculates the contribution of each observation to the log-likelihood.
# Parameters:
# - demand_formula: Formula for the demand equation.
# - supply_formula: Formula for the supply equation.
# - data: Data frame containing the variables in the formulas.
# - beta: Vector of coefficients.
# - equal_sigmas: Whether the errors on the demand and supply sides are assumed to have equal standard deviations.
# - na.action: The NA handling function to use.
# - corr: Whether the model has correlated errors.
# - likelihood_bias: A bias term added to the likelihood to avoid taking the logarithm of zero.
# Returns:
# - A data table containing the contributions to the log-likelihood.
ll_contributions <- function(demand_formula = NULL,
supply_formula = NULL,
data = NULL,
beta = NULL,
equal_sigmas = FALSE,
na.action = na.exclude,
corr = FALSE,
likelihood_bias = 0
) {
mf <- na.action(data[, union(all.vars(demand_formula), all.vars(supply_formula)), with=FALSE])
attr(mf, 'demand_terms') <- terms(demand_formula)
attr(mf, 'supply_terms') <- terms(supply_formula)
y <- mf[[all.vars(demand_formula[[2]])]]
list[X, coef_indices] <- model.matrix.diseq(demand_formula, supply_formula, data=mf,
corr=corr, equal_sigmas=equal_sigmas)
idx_bd <- coef_indices[['beta_demand']]
idx_bs <- coef_indices[['beta_supply']]
idx_sd <- coef_indices[['sigma_demand']]
idx_ss <- coef_indices[['sigma_supply']]
idx_corr <- coef_indices[['sigma_corr']]
y_name <- as.character(demand_formula[[2]])
contributions <- as.data.table(X %*% diag(beta[c(idx_bd, idx_bs)]))
setnames(contributions, c(paste0('beta * ', colnames(X)[idx_bd], ' - d'),
paste0('beta * ', colnames(X)[idx_bs], ' - s')))
contributions[, (y_name) := y]
theta1 <- X[, idx_bd] %*% beta[idx_bd]
theta2 <- X[, idx_bs] %*% beta[idx_bs]
sigma1 <- exp(beta[idx_sd])
if (is.null(idx_ss)) {
sigma2 <- sigma1
} else {
sigma2 <- exp(beta[idx_ss])
}
if (corr) {
rho <- tanh(beta[idx_corr])
}
if (!corr) {
f1 <- dnorm(y, mean=theta1, sd=sigma1)
f2 <- dnorm(y, mean=theta2, sd=sigma2)
F1 <- 1 - pnorm((y-theta1) / sigma1)
F2 <- 1 - pnorm((y-theta2) / sigma2)
prob_zero <- 1 - F1*F2
G <- ifelse(y > 0, f1*F2 + f2*F1, prob_zero)
} else {
f1 <- dnorm(y, mean=theta1, sd=sigma1)
f2 <- dnorm(y, mean=theta2, sd=sigma2)
F1 <- 1 - pnorm(y,
mean = theta1 + sigma1 / sigma2 * rho * (y - theta2),
sd = sqrt(1 - rho^2) * sigma1)
F2 <- 1 - pnorm(y,
mean = theta2 + sigma2 / sigma1 * rho * (y - theta1),
sd = sqrt(1 - rho^2) * sigma2)
# y < 0 esetre együttes eloszlás közelítése - Mee & Owen 1982
F_00 <- mvnorm_approx(theta1, sigma1, theta2, sigma2, rho)
prob_zero <- pnorm(0, mean=theta1, sd=sigma1) + pnorm(0, mean=theta2, sd=sigma2) - F_00
G <- ifelse(
y > 0,
f1*F2 + f2*F1,
prob_zero
)
}
contributions[, pred_demand := theta1]
contributions[, pred_supply := theta2]
contributions[, pred_outcome := pmin(pred_demand, pred_supply)]
contributions[, `demand_density (f1)` := f1]
contributions[, `supply_density (f2)` := f2]
contributions[, `prob_demand_above_observed (F1)` := F1]
contributions[, `prob_supply_above_observed (F2)` := F2]
contributions[, prob_zero := prob_zero]
contributions[, likelihood_contribution := G]
contributions[, loglike_contribution := log(G)]
contributions[, biased_loglike_contribution := log(G + likelihood_bias)]
return(contributions)
}
# Function: model_ll_contributions
# Description: Convenience function to calculate the log-likelihood contributions for a fitted disequilibrium model.
# Parameters:
# - diseq_obj: The fitted disequilibrium model.
# Returns:
# - A data table containing the contributions to the log-likelihood.
model_ll_contributions <- function(diseq_obj) {
corr <- !is.null(diseq_obj$settings$corr) && diseq_obj$settings$corr == TRUE
equal_sigmas <- !is.null(diseq_obj$settings$equal_sigmas) && diseq_obj$settings$equal_sigmas == TRUE
ll_contributions(demand_formula = formula(diseq_obj$demand_terms),
supply_formula = formula(diseq_obj$supply_terms),
data = diseq_obj$model,
beta = diseq_obj$raw_coefficients,
corr = corr,
equal_sigmas = equal_sigmas,
na.action = diseq_obj$na.action,
likelihood_bias = diseq_obj$settings$likelihood_bias
)
}
# Function: print.diseq
# Description: Prints the fitted disequilibrium model, including the demand and supply equations and the coefficients.
print.diseq <- function(diseq_obj) {
cat('\nCall:\n')
print(diseq_obj$call)
cat('\n')
cat('Demand equation:\n')
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['beta_demand']] ])
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['sigma_demand']] ])
cat('\n')
cat('Supply equation:\n')
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['beta_supply']] ])
if (diseq_obj$settings$equal_sigmas) {
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['sigma_demand']] ])
} else {
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['sigma_supply']] ])
}
cat('\n')
if (!is.null(diseq_obj$settings$corr) && diseq_obj$settings$corr) {
cat('Correlation of error terms:\n')
print(diseq_obj$coefficients[ diseq_obj$coef_indices[['sigma_corr']] ])
cat('\n')
}
}
# Function: predict.diseq
# Description: Predicts from a fitted disequilibrium model.
# Parameters:
# - diseq_obj: The fitted disequilibrium model.
# - newdata: Data frame containing the variables in the formulas. If NULL, the original data is used.
# - type: The type of prediction to make. Can be 'prob_supply_constrained', 'demand', 'supply', 'response', or 'expected_deficit'.
# - conditional: Whether to make conditional predictions.
# - na.action: The NA handling function to use.
# - prob_without_positive_demand: Whether negative demand values should be included in the probability of supply constraint. Deprecated.
# - exact: Whether to use the exact or approximate method for the joint probabilities.
# Returns:
# - The predicted values.
predict.diseq <- function(diseq_obj,
newdata = NULL,
type='prob_supply_constrained',
conditional=FALSE,
na.action = diseq_obj$na.action,
prob_without_positive_demand = FALSE,
exact = TRUE) {
corr <- !is.null(diseq_obj$settings$corr) && diseq_obj$settings$corr == TRUE
equal_sigmas <- !is.null(diseq_obj$settings$equal_sigmas) && diseq_obj$settings$equal_sigmas == TRUE
if (is.null(newdata)) {
list[X, coef_indices] = model.matrix.diseq(
diseq_obj$demand_terms,
diseq_obj$supply_terms,
data = diseq_obj$model,
corr = corr,
equal_sigmas = equal_sigmas
)
if (conditional) {
y <- diseq_obj$model[[all.vars(formula(diseq_obj$demand_terms)[[2]])]]
}
orig_rownames <- diseq_obj$orig_rownames
} else {
if (!conditional) {
vars_to_select <- union(all.vars(update(diseq_obj$demand_terms, NULL ~ .)),
all.vars(update(diseq_obj$supply_terms, NULL ~ .)))
} else {
vars_to_select <- union(all.vars(diseq_obj$demand_terms),
all.vars(diseq_obj$supply_terms))
}
newdata_na_cleaned <- na.action(newdata[, vars_to_select, with=FALSE])
list[X, coef_indices] = model.matrix.diseq(
update(diseq_obj$demand_terms, NULL ~ .),
update(diseq_obj$supply_terms, NULL ~ .),
data = newdata_na_cleaned,
corr = corr,
equal_sigmas = equal_sigmas
)
if (conditional) {
y <- newdata_na_cleaned[[all.vars(formula(diseq_obj$demand_terms)[[2]])]]
}
orig_rownames <- rownames(newdata)
}
new_rownames <- rownames(X)
idx_bd <- coef_indices[['beta_demand']]
idx_bs <- coef_indices[['beta_supply']]
idx_sd <- coef_indices[['sigma_demand']]
idx_ss <- coef_indices[['sigma_supply']]
idx_corr <- coef_indices[['sigma_corr']]
beta_opt <- diseq_obj$raw_coefficients
theta_demand <- X[, idx_bd] %*% beta_opt[idx_bd]
theta_supply <- X[, idx_bs] %*% beta_opt[idx_bs]
sigma1 <- exp(beta_opt[idx_sd])
if (is.null(idx_ss)) {
sigma2 <- sigma1
} else {
sigma2 <- exp(beta_opt[idx_ss])
}
if (is.null(idx_corr)) {
rho <- 0
} else {
rho <- tanh(beta_opt[idx_corr])
}
if (type == 'prob_supply_constrained' && conditional == FALSE) {
out <- predict_prob_supply_constrained_uncond(
theta_demand, theta_supply, sigma1, sigma2, rho,
prob_without_positive_demand, exact
)
} else if (type == 'demand' && conditional == FALSE) {
out <- theta_demand
} else if (type == 'supply' && conditional == FALSE) {
out <- theta_supply
} else if (type == 'response' && conditional == FALSE) {
out <- pmax(0, pmin(theta_supply, theta_demand))
} else if (type == 'expected_deficit' && conditional == FALSE) {
out <- predict_expected_deficit_uncond(
theta_demand, theta_supply, sigma1, sigma2, rho, exact
)
} else if (type == 'prob_supply_constrained' & conditional == TRUE) {
if (prob_without_positive_demand) {
stop("prob_without_positive_demand is a legacy argument and is not implemented for conditional probabilities")
}
out <- predict_prob_supply_constrained_cond(
y, theta_demand, theta_supply, sigma1, sigma2, rho, exact
)
} else if (type == 'expected_deficit' && conditional == TRUE) {
out <- predict_expected_deficit_cond(
y, theta_demand, theta_supply, sigma1, sigma2, rho, exact
)
} else if (type == 'response' && conditional == TRUE) {
warning("Chosing type=response and conditional=TRUE returns the observed outcome")
out <- y
} else {
stop('Supplied prediction type and conditional/unconditional combination is not implemented.')
}
if (identical(na.action, na.exclude)) {
out_final <- rep(NA, times = length(orig_rownames))
names(out_final) <- orig_rownames
out_final[new_rownames] <- out
}
else {
out_final <- out
}
return(out_final)
}
# Function: predict_prob_supply_constrained_uncond
# Description: Predicts the unconditional probability of being supply constrained.
predict_prob_supply_constrained_uncond <- function(theta_demand, theta_supply, sigma1, sigma2, rho,
prob_without_positive_demand = FALSE, exact = TRUE) {
if (prob_without_positive_demand == TRUE) {
# Legacy code, should not be used
sigma <- sqrt(sigma1^2 + sigma2^2 - 2 * rho * sigma1 * sigma2)
prob_supply_constrained <- pnorm((theta_demand-theta_supply) / sigma)
} else {
mvnorm_fun <- if (exact == TRUE) {mvnorm_exact} else {mvnorm_approx}
# A P(D > S metszet D > 0) = P(S - D < 0 metszet -D < 0) együttes eloszlása
# levezetés papíron Peti mappájában (térkép!)
mu_1 <- theta_supply - theta_demand
mu_2 <- -theta_demand
sigma_1_bar <- sqrt(sigma1^2 + sigma2^2 - 2 * rho * sigma1 * sigma2)
sigma_2_bar <- sigma2
corr_coef <- sigma1 / sigma2 - rho
prob_supply_constrained <- mvnorm_fun(mu_1, sigma_1_bar, mu_2, sigma_2_bar, corr_coef)
}
return(prob_supply_constrained)
}
# Function: predict_prob_supply_constrained_cond
# Description: Predicts the conditional probability of being supply constrained.
predict_prob_supply_constrained_cond <- function(y, theta_demand, theta_supply, sigma_d, sigma_s, rho, exact = TRUE) {
mvnorm_fun <- if (exact == TRUE) {mvnorm_exact} else {mvnorm_approx}
# y > 0 part
mu_D_cond_S_y <- theta_demand + rho * sigma_d / sigma_s * (y - theta_supply)
sigma_D_cond_S_y <- sigma_d * sqrt(1 - rho^2)
mu_S_cond_D_y <- theta_supply + rho * sigma_s / sigma_d * (y - theta_demand)
sigma_S_cond_D_y <- sigma_s * sqrt(1 - rho^2)
P_y_smaller_D_cond_S_eq_y <- 1 - pnorm(y, mean = mu_D_cond_S_y, sd = sigma_D_cond_S_y)
P_y_smaller_S_cond_D_eq_y <- 1 - pnorm(y, mean = mu_S_cond_D_y, sd = sigma_S_cond_D_y)
f_D <- dnorm(y, mean = theta_demand, sd = sigma_d)
f_S <- dnorm(y, mean = theta_supply, sd = sigma_s)
prob_cond_pos_y <- (P_y_smaller_D_cond_S_eq_y * f_S) / (P_y_smaller_D_cond_S_eq_y * f_S + P_y_smaller_S_cond_D_eq_y * f_D)
# y == 0 part
P_D_smaller_0 <- pnorm(0, theta_demand, sigma_d)
P_S_smaller_0 <- pnorm(0, theta_supply, sigma_s)
P_both_smaller_0 <- mvnorm_fun(theta_demand, sigma_d, theta_supply, sigma_s, rho)
prob_cond_zero_y <- (P_S_smaller_0 - P_both_smaller_0) / (P_S_smaller_0 + P_D_smaller_0 - P_both_smaller_0)
prob_supply_constrained <- ifelse(y > 0, prob_cond_pos_y, prob_cond_zero_y)
return(prob_supply_constrained)
}
# Function: predict_prob_supply_constrained_uncond
# Description: Predicts the unconditional expected deficit (demand - supply).
predict_expected_deficit_uncond <- function(theta_demand, theta_supply, sigma_d, sigma_s, rho, exact = TRUE) {
# Preallocate result vectors
N <- length(theta_demand)
expected_deficit_part_below_0 <- rep(NA, times = N)
expected_deficit_part_above_0 <- rep(NA, times = N)
# Note: D_star = D | S = eta ~ N(m, s)
s <- sigma_d * sqrt(1 - rho^2)
for (i in 1 : N) {
fun_to_integrate_below_0 <- function(eta) {
m <- theta_demand[i] + rho * sigma_d / sigma_s * (eta - theta_supply[i])
E_D_star_cond_D_star_greater_0 <- m + s * dnorm(m / s) / pnorm(m / s)
P_D_star_greater_0 <- 1 - pnorm(0, mean = m, sd = s)
f_S_eta <- dnorm(eta, mean = theta_supply[i], sd = sigma_s)
# The ifelse below is needed to get rid of 0 * Inf issues
# The limit is of the product is 0 at -Inf so it should be fine
return(ifelse(
P_D_star_greater_0 * f_S_eta == 0,
0,
E_D_star_cond_D_star_greater_0 * P_D_star_greater_0 * f_S_eta
))
}
fun_to_integrate_above_0 <- function(eta) {
m <- theta_demand[i] + rho * sigma_d / sigma_s * (eta - theta_supply[i])
E_D_star_cond_D_star_greater_eta <- m + s * dnorm((eta - m) / s) / (1 - pnorm((eta - m) / s))
P_D_star_greater_eta <- 1 - pnorm(eta, mean = m, sd = s)
f_S_eta <- dnorm(eta, mean = theta_supply[i], sd = sigma_s)
# The ifelse below is needed to get rid of 0 * Inf issues
# The limit is of the product is 0 at Inf so it should be fine
return(ifelse(
P_D_star_greater_eta * f_S_eta == 0,
0,
(E_D_star_cond_D_star_greater_eta - eta) * P_D_star_greater_eta * f_S_eta
))
}
expected_deficit_part_below_0[i] <- integrate(fun_to_integrate_below_0, -Inf, 0)$value
expected_deficit_part_above_0[i] <- integrate(fun_to_integrate_above_0, 0, Inf)$value
}
return(expected_deficit_part_below_0 + expected_deficit_part_above_0)
}
# Function: predict_prob_supply_constrained_uncond
# Description: Predicts the conditional expected deficit (demand - supply).
predict_expected_deficit_cond <- function(y, theta_demand, theta_supply, sigma_d, sigma_s, rho, exact = TRUE) {
prob_supply_constrained <- predict_prob_supply_constrained_cond(
y, theta_demand, theta_supply, sigma_d, sigma_s, rho, exact = exact
)
# y > 0 part
mu_D_cond_S_y <- theta_demand + rho * sigma_d / sigma_s * (y - theta_supply)
sigma_D_cond_S_y <- sigma_d * sqrt(1 - rho^2)
y_bar <- (y - mu_D_cond_S_y) / sigma_D_cond_S_y
expected_deficit_cond_constr <- mu_D_cond_S_y + sigma_D_cond_S_y * dnorm(y_bar) / (1 - pnorm(y_bar))
# y == 0 part
s <- sigma_d * sqrt(1 - rho^2)
for (i in seq_along(y)) {
if (y[i] == 0) {
fun_to_integrate <- function(z) {
m <- theta_demand[i] + rho * sigma_d / sigma_s * (z - theta_supply[i])
E_D_star_cond_D_star_greater_0 <- m + s * dnorm(m / s) / pnorm(m / s)
f_S_z <- dnorm(z, mean = theta_supply[i], sd = sigma_s)