                                          What Makes In-context Learning Effective for Mathematical Reasoning

           Test Problem          !"#!
                                                                     $%                         LLM-oriented
                                                                                                                                                            .-shot prompting
     What is the domain of the function                                                       Semantic Similarity                                  Here are some examples you can refer to.




                                              Large Language Model
                  "#$
      ! =                 , where log                                              !"#!                                                            Q: What is the domain of the function …
             %&'("#%&'($#"))
                                                                                                     !" # =                                        A: The formula for (!) has a …




                                                                                                                                   Demonstration
     is the base 10 logarithm function?
                                                                                                                                                   …
                                                                     $&                        %&'(& ) *+, - *. %          …




                                                                                                                                     Rejection
                                                                                $%& ' $(                                                           Now it's your turn!
       Demonstration Pool                                                                                                                          - Take a deep breath
                                                                                                                                                   - Think step by step
     Problem 1: What is the domain of                                                                                      …
                                                                                                                                                   - I will tip $200
                         "#$%                                        $'       Inference Stability of Demonstration
     the function ! =        ?
                           #$&                                                                                        ()*+, -                      Q: What is the domain of the function
     Solution 1:The formula for !                                                                    $)                                                         "#$
     has a …{("#, "8) $("8, #)}.
                                                                                      !"# $ =                                                        ! =                , where log is the...
                                                                                                                                                           %&'("#%&'($#"))
                     …                                                                                *



                                                                          Figure 2. Illustration of our proposed LMS3 method.


(recall z0 = WK h and WK is indeed WK    T
                                           · WQ in the                                                                Theoretical Dependent
                                                                                                             Method                         Generality Complexity
                                                                                                                      Guarantee on LLM
aforementioned section), and 2) minimize
                                                                                                          Similar-ICL     ✗          ✗         ✓        O(M + N )
                                  def WV                                                                   Influence      ✗          ✓         ✓       O( MV
                                                                                                                                                           S + N)
                          Stab(X) = ∥ √ h∥.                                                   (22)          InfICL        ✗          ✓         ✗ O(DM + DV + N )
                                       d                                                                  TopK+MDL        ✗          ✓         ✗       O(M + KN )
.                                                                                                         TopK+ConE       ✗          ✓         ✗       O(M + KN )
                                                                                                              IDS         ✗          ✓         ✓       O(M + QN )
Specifically, Sim(X) reflects an LLM-oriented Semantic
                                                                                                               MI         ✗          ✓         ✗         O(MN )
Similarity between the demonstration X and the test data                                                    SPELL         ✗          ✓         ✓       O(MR + N )
Xtest . It illustrates the need to find a demonstration similar                                           LMS3 (ours)     ✓          ✓         ✓        O(M + N )
to the test sample. However, it goes beyond traditional meth-
ods by taking into account 1) the whole reasoning path of                                                 Table 1. Comparison of methods, including Similar-ICL (Liu et al.,
demonstrations (recall X includes both the problem context                                                2022; Zhang et al., 2023; Fu et al., 2022; Chen et al., 2024b),
and the solution) and 2) the characteristics of the inference                                             Influence (Nguyen & Wong, 2023) (S is the size of subset used to
LLM itself, which is more consistent with intuition. The                                                  estimate influences, V is the size of validation set), InfICL (Van
value of Stab(X) is an evaluation of the Inference Stability                                              et al., 2024) (D is the number of parameters of external LLMs),
of Demonstration X. Based on Eq (5), Stab(X) is indeed                                                    TopK+MDL (Wu et al., 2023), TopK+ConE (Peng et al., 2024)
the length of gradient of the loss function on X. If Stab(X)                                              (K is the number of candidate demonstrations), IDS (Qin et al.,
is low, it indicates that the LLM has reached a stable predic-                                            2023) (Q is the number of iterations), MI (Sorensen et al., 2022),
                                                                                                          SPELL (Gonen et al., 2023) (R is the number of samples for
tion with minimal loss on X, and the parameters will not be
                                                                                                          estimating perplexity). The generality setting to ✗ indicates that
excessively altered due to the introduction of this sample.                                               these works are more suitable for classification tasks and hard to
In other words, the LLM has been able to stably deduce the                                                implement for mathematical reasoning task.
correct answer for demonstration X.
Since it is hard to simultaneously achieve the minimum of
                                                                                                          highest Score(X) still do not satisfy Eq. (20). In such cases,
Sim(X) and Stab(X), two naive approximations are to
                                                                                                          unlike existing methods that always select top k examples,
minimize a demonstration scoring function that calculates
                                                                                                          we tend to refuse to provide any demonstration and instead
their sum or product:
                                                                                                          use a zero-shot approach, because our theorems suggest
            Score(X) = Sim(X) + α1 · Stab(X),                                                 (23)        that providing examples in this case will have a negative
                                                                                                          effect. We control Sim(X) to achieve this rejection mecha-
                Score(X) = Sim(X) · Stab(X),                                                  (24)        nism, because if an example’s Sim(X) is already too large,
However, Eq. (23) requires considering the scale differences                                              Sim(X) · µC1 might have exceeded the left-hand side of
between the two objectives and adjusting the hyperparame-                                                 Eq. (7). However, setting an absolute threshold for Sim(X)
ter α1 based on different LLMs and datasets, which is chal-                                               is challenging since µ, C1 is unknown, and calculating the
lenging to apply in practice. Therefore, we prefer Eq. (24)                                               gradient norm ∥∇W L(htest , Ŵ )∥ is costly. Therefore, we
as the basic form of our scoring function. To implement                                                   adopt a simplified relative threshold. We expect that the
k-shot in-context learning, we can select the top k samples                                               Sim(X) of an ideal demonstration should be as small as
with the highest Score(X) as demonstrations according to                                                  possible relative to all examples. Consequently, we rank
Theorem 2, which can ensure that the most relevant and                                                    Sim(X) of all candidate examples. If a demonstration X
stable samples are used to enhance the LLM’s performance.                                                 ranked top-k in Score(X) does not have a Sim(X) value
                                                                                                          within the top λ smallest, we reject to select it.
Furthermore, we design a demonstration rejection mecha-
nism. For instance, it is possible that the examples with the                                             Theoretically, to compute Score(X), we need to input the

                                                                                                     5
