Repository: QuantEcon/QuantEcon.py (python)

Issue:
BUG: lcp_lemke
Bug found by an instance in some benchmark script generated by ChatGPT:

```python
import numpy as np
from quantecon.optimize import lcp_lemke

n = 10
seed = 0
rng = np.random.RandomState(seed)
A = rng.standard_normal((n, n))
M = A.T @ A + np.eye(n)
q = rng.standard_normal(n)
res = lcp_lemke(M, q)
res.z
```

```
array([ 0.        ,  0.        ,  0.3476126 ,  0.        ,  0.        ,
        0.        ,  0.        ,  0.27700921,  0.        , -0.40131452])
```

The condition `z >= 0` is violated.

Pull request:
FIX: lcp_lemke: Fix bug in first iteration
Fix #768

Bug spotted by Deep Research by ChatGPT o4-mini-high.
(Given the failure reported in #768, I prompted ChatGPT to read the relevant files and identify the bug; it successfully did.)

Patch:
diff --git a/quantecon/optimize/lcp_lemke.py b/quantecon/optimize/lcp_lemke.py
index 2ee54f9..b0464ec 100644
--- a/quantecon/optimize/lcp_lemke.py
+++ b/quantecon/optimize/lcp_lemke.py
@@ -150,7 +150,7 @@ def lcp_lemke(M, q, d=None, max_iter=10**6, piv_options=PivOptions(),
         ratio = q[i] / d[i]
         if ratio <= ratio_min + piv_options.tol_ratio_diff:
             pivrow = i
-            ratio = ratio_min
+            ratio_min = ratio
 
     _pivoting(tableau, pivcol, pivrow)
     basis[pivrow], pivcol = pivcol, pivrow + n


Test patch:
diff --git a/quantecon/optimize/tests/test_lcp_lemke.py b/quantecon/optimize/tests/test_lcp_lemke.py
index 15d2707..457b252 100644
--- a/quantecon/optimize/tests/test_lcp_lemke.py
+++ b/quantecon/optimize/tests/test_lcp_lemke.py
@@ -107,3 +107,13 @@ class TestLCPLemke:
 
         res = lcp_lemke(M, q)
         _assert_success(res, M, q)
+
+    def test_bug_768(self):
+        n = 10
+        seed = 0
+        rng = np.random.RandomState(seed)
+        A = rng.standard_normal((n, n))
+        M = A.T @ A + np.eye(n)
+        q = rng.standard_normal(n)
+        res = lcp_lemke(M, q)
+        _assert_success(res, M, q)