                                  What Makes In-context Learning Effective for Mathematical Reasoning

the empirical loss is (Ling, 1984; Koh & Liang, 2017):                          According to Eq. (7), the right-hand side of Eq. (16) is
                                                                                greater than 0, which leads to the conclusion. □
                    dL(htest , Ŵϵ,z0 )
     Iloss (z) =
                          dϵ                                          (8)       Extension to k-shot setting. In Theorem 1, we only con-
                                          ϵ=0

                = −∇W L(htest , Ŵ )      T    −1
                                            · HŴ ∇W L(z0 , Ŵ )
                                                                                sider one demonstration X (i.e., the one-shot scenario). For
                                                                                the k-shot scenario, Eq (3) can be written as
Then, the testing loss L(htest , Ŵ |D 1 | ,z0 ) can be evaluated                                  WV
                                                                                     k
                                1
                                            pre
                                                                                    FICL (htest ) ≈ √ htest · (WK htest )T · htest
by Taylor approximation since |Dpre | is sufficiently small,                                         d
                                                                                                                                              (17)
i.e., L(htest , Ŵ |D 1 | ,z0 ) ≈                                                                           WV
                     pre
                                                                                                    + Σki=1 √ hi · (WK hi )T · htest ,
                                                                                                             d
                                   1     dL(htest , Ŵϵ,z0 )                     where h1 , ..., hk are the representations of demonstrations
          L(htest , Ŵ0,z0 ) +
                                 |Dpre |       dϵ                               X1 , ..., Xk . This formalization can be interpreted as intro-
                                                               ϵ=0
                                   1                                  (9)       ducing k training samples z1 = WK h1 , ..., zk = WK hk
      =L(htest , Ŵ0,z0 ) −              ∇W L(htest , Ŵ )T                     to optimize the linear function F(z) simultaneously, where
                                 |Dpre |                                        the gradient at each training sample zi satisfies
             −1
          · HŴ ∇W L(z0 , Ŵ )
                                                                                                                     WV
                                                                                                    ∇F L(zi , W0 ) = √ hi .                   (18)
Therefore, now the question turns to evaluate                                                                         d
    def                    −1
L1 = ∇W L(htest , Ŵ )T · HŴ ∇W L(z0 , Ŵ ) =                                  Similar to the proof of Theorem 1, we derive the following
                                          −1
 (∇W L(z0 , Ŵ ) − ∇W L(htest , Ŵ ))T · HŴ ∇W L(htest , Ŵ )                  Theorem 2 to illustrate the condition of these samples to
 |                          {z                               }                  ensure a reduction in the loss of testing data Xtest , where
                                    L11
                          −1
 + ∇W L(htest , Ŵ )T · HŴ  ∇W L(htest , Ŵ )                                                   def               1
                                                                                          Ŵϵ,z̄k = argmin               Σz∈Dpre L(F(z))
   |                   {z                    }                                                               W   |Dpre |                      (19)
                           L12
                                                                     (10)                                        + ϵ · Σki=1 L(F(zi ))
Since HŴ is positive definite, we denote λ1 ≥ λ2 ≥ ... ≥
                                  −1
λdd′ > 0 are the eigenvalues of HŴ  and can prove that                         Theorem 2. Assume ∇F L is Lipschitz continuous
                                                                                w.r.t F with constant µ. If inequality (20) holds true,
                                                                                then L(htest , Ŵ |D 1 | ,z̄k ) < L(htest , Ŵ0,z̄k ), i.e., intro-
                               
L11 ≥ −λ1 ∥∇W L(htest , Ŵ )∥ · ∥∇F L(htest , Ŵ )                                                     pre
                                                                               ducing training samples {z1 , ..., zk } (i.e., demonstrations
− ∇F L(z0 , Ŵ )∥ · ∥htest ∥ + ∥∇F L(z0 , Ŵ )∥ · ∥htest − z0 ∥                 X1 , ..., Xk ) can reduce the testing loss on htest .
                                                            (11)
Since ∇F L is Lipschitz continuous, we get L11 ≥                                        kλdd′
                                                                                              ∥∇W L(htest , Ŵ )∥ >Σki=1 ∥htest − zi ∥·
                                                                                         λ1
   − λ1 ∥∇W L(htest , Ŵ )∥ · (µ∥Ŵ (htest − z0 )∥ · ∥htest ∥+                                                                                (20)
                                                                                                                      WV
                                                                                                                   (∥ √ hi ∥ + µC1 )
  ∥∇F L(z0 , Ŵ )∥ · ∥htest − z0 ∥)                                                                                     d
                                                                     (12)
Applying Eqs. (4) (5) to Eq. (12), we have:                                     Theorem 2 further indicates that, under our setup, the joint
                      WV
                                                                                effect of different demonstrations follows an additive re-
∥Ŵ (htest − z0 )∥ ≤ ∥ √ htest ∥ · ∥WK htest ∥ · ∥htest − z0 ∥                  lationship. This implies that the selection of k different
                        d
                                                          (13)                  demonstrations can be approximately considered indepen-
                                                                                dently. We leave the exploration of more complex interac-
                                     WV
 ∥∇F L(z0 , Ŵ )∥ · ∥htest − z0 ∥ = ∥ √ h∥ · ∥htest − z0 ∥ (14)                 tions among demonstrations to future work.
                                       d

For L12 , we similarly prove that:                                              4. LMS3: Method Design
                      ′
           L12 = Σdd      2
                  i=1 λi bi ≥ λdd′ ∥∇W L(htest , Ŵ )∥
                                                       2
                                                                     (15)       Based on Section 3, an ideal demonstration X needs to max-
                                                                                imize the value of L1 (i.e., minimize the empirical testing
Combining Eqs. (12)-(15), we finally get:                                       loss L(htest , Ŵ |D 1 | ,z0 ) in Eq. (9)). This is equivalent to
                                                                                                    pre

L1 ≥λdd′ ∥∇W L(htest , Ŵ )∥2 − λ1 ∥∇W L(htest , Ŵ )∥ · (µ · C1                minimize the right-hind side of Eq. (7) according to Eq. (16)
                                  WV                                            and can be further divided into: 1) minimize
    · ∥htest − z0 ∥ · ∥htest ∥ + ∥ √ h∥ · ∥htest − z0 ∥) .
                                    d                                                                    def   T
                                                           (16)                             Sim(X) = ∥htest − WK · WQ h∥,                     (21)

                                                                            4
