From 0efdc25ec589833b3729b6e0a313061ffb61d860 Mon Sep 17 00:00:00 2001
From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com>
Date: Sat, 30 Nov 2024 03:41:58 +0100
Subject: [PATCH] Some refactoring of market functions (#36)

---
 src/markets/search_and_matching_credit.jl      | 3 ++-
 src/markets/search_and_matching_labour.jl      | 7 ++-----
 test/deterministic/make_model_deterministic.jl | 2 +-
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/src/markets/search_and_matching_credit.jl b/src/markets/search_and_matching_credit.jl
index 33b89c2..69dcf32 100644
--- a/src/markets/search_and_matching_credit.jl
+++ b/src/markets/search_and_matching_credit.jl
@@ -20,8 +20,9 @@ function search_and_matching_credit(firms::AbstractFirms, model)
     DL_i = zeros(size(DL_d_i))
     I_FG = findall(DL_d_i .> 0)
     shuffle!(I_FG)
+    s_L_e_i = sum(L_e_i)
     for i in I_FG
-        DL_i[i] = max(0, min(min(DL_d_i[i], zeta_LTV * K_e_i[i] - L_e_i[i]), E_k / zeta - (sum(L_e_i) + sum(DL_i))))
+        DL_i[i] = max(0.0, min(DL_d_i[i], zeta_LTV * K_e_i[i] - L_e_i[i], E_k / zeta - s_L_e_i - sum(DL_i)))
     end
     return DL_i
 end
diff --git a/src/markets/search_and_matching_labour.jl b/src/markets/search_and_matching_labour.jl
index 2693660..3dbd6a0 100644
--- a/src/markets/search_and_matching_labour.jl
+++ b/src/markets/search_and_matching_labour.jl
@@ -31,10 +31,9 @@ function search_and_matching_labour(firms::AbstractFirms, model)
     shuffle!(H_E)
 
     # fire workers if vacancies are negative
-    for e in eachindex(H_E)
+    for h in H_E
 
         # find employer of worker
-        h = H_E[e]
         i = O_h[h]
 
         # if employer has negative vacancies, fire the worker
@@ -53,9 +52,7 @@ function search_and_matching_labour(firms::AbstractFirms, model)
     # while there are no more vacancies or unemployed workers
     while !isempty(H_U) && !isempty(I_V)
         shuffle!(I_V)
-        for f in eachindex(I_V)
-            # select random vacancy
-            i = I_V[f]
+        for i in I_V
             # select random unemployed worker
             h = H_U[1]
             # employ worker
diff --git a/test/deterministic/make_model_deterministic.jl b/test/deterministic/make_model_deterministic.jl
index c285759..5b03dd7 100644
--- a/test/deterministic/make_model_deterministic.jl
+++ b/test/deterministic/make_model_deterministic.jl
@@ -18,7 +18,7 @@ end
 function rand(s::DynamicSampler)
     idx = minimum(minimum.(s.level_buckets; init=typemax(Int)))
     weight = s.weights[idx]
-    level = ceil(Int, log2(weight)) - first(s.level_inds) + 1
+    level = ceil(Int, log2(weight)) - s.info.level_min + 1
     idx_in_level = findfirst(x -> x == idx, s.level_buckets[level])
     return idx
 end