                            What Makes In-context Learning Effective for Mathematical Reasoning

Algorithm 1 Our LMS3                                                   demonstrations on the validation set alone, is insufficient. It
  Input: k-shot, Xtest , D, λ                                          is essential to balance similarity and the inference stability
  Output: Selected demonstration set Dk ⊆ D                            of demonstrations, as LMS3 does, to consistently achieve
 1: Calculate Sim(X), Stab(X), Score(X) for X ∈ D                      better results compared to zero-shot setting. In case 3, we
    based on Eqs.(21)(22)(24).                                         again observe that the two baselines incorrectly answer
 2: Define Scorek ⊆ D as the set of k samples with the                 a problem that zero-shot got right. In contrast, LMS3’s
    smallest Score(X) values.                                          rejection mechanism determines that the best demonstration
 3: Define Simλ ⊆ D as the set of λ samples with the                   still has a relatively large similarity distance Sim(X) from
    smallest Sim(X) values.                                            the test sample (ranked in the top 1.19% exceeding λ =
 4: Dk = {}.                                                           1%). By automatically rejecting this demonstration and
 5: for X ∈ Scorek do                                                  adopting the zero-shot setting, LMS3 maintains the original
 6:     If X ∈ Simλ do                                                 performance, which verifies the necessity and effectiveness
 7:         Dk = Dk ∪ {X};                                             of our proposed demonstration rejection mechanism.

                       MAWPS      GSM8K       MATH                     E. Discussion and Future Work
   Num. Problems         2,373      8,792     12,500
          |D|            1,898      7,473      7,500                   Here, we discuss several future directions of LMS3.
        |Dtest |          475       1,319      5,000
 Avg. Problem Length     30.08      45.88      30.79                   First, in this paper, motivated by LLMs’ unreliable perfor-
 Avg. Solution Length     5.90      48.31      84.50                   mance on mathematical reasoning benchmarks, we derive
   Difficulty Level   Elementary Elementary High School                our Theorems 1 and 2 to illustrate when in-context learning
                                                                       is effective for mathematical reasoning and design a corre-
                Table 6. Statistics of datasets.                       sponding demonstration selection method LMS3. However,
Combining Eqs. (34)-(37), we finally get:                              our theories and method can be easily extended to other nat-
                                                                       ural language processing tasks, such as commonsense rea-
 L1 ≥λdd′ ∥∇W L(htest , Ŵ )∥2 − λ1 ∥∇W L(htest , Ŵ )∥ · (µ           soning (Talmor et al., 2019) and sentiment analysis (Socher
      WV                                                               et al., 2013; He et al., 2017). Besides, it is also worthwhile
     ∥ √ htest ∥ · ∥WK htest ∥ · ∥htest − z0 ∥ · ∥htest ∥+
         d                                                             to explore how our analysis can be further adapted to multi-
      WV                                                               modal tasks, such as geometric reasoning (Xiao et al., 2024)
     ∥ √ h∥ · ∥htest − z0 ∥) .
         d                                                             and graph learning (Pei et al., 2020). Therefore, in the fu-
                                                           (38)        ture, we are willing to refine our theories by incorporating
According to Eq. (27), the right-hand side of Eq. (38) is              the characteristics of different tasks and to test our LMS3
greater than 0, which leads to the conclusion. □                       more broadly.
                                                                       Second, from our theories, we further find that the cor-
B. Pseudo-Code of LMS3                                                 rectness of the reasoning paths in the demonstrations may
                                                                       have less impact on the test performance compared with
The pseudo-code of our LMS3 is presented in Algorithm 1.               its semantics, which aligns with observations reported in
                                                                       previous studies (Wei et al., 2023; Min et al., 2022). This
C. Dataset Partition and Statistics                                    phenomenon does not meet the goal of enabling LLMs to
                                                                       learn and imitate the reasoning paths of demonstrations.
For GSM8K and MATH, we follow their publicly available                 Therefore, we will explore ways to amplify the influence of
train/test splits as D and Dtest . For MAWPS, we randomly              the demonstration reasoning paths based on our theory for
split the dataset into an 8:2 ratio for D/Dtest . We summarize         improving the interpretability and controllability of ICL.
the dataset statistics in Table 6. For each dataset, we also
randomly select 200 problems from D as the validation set              Moreover, similar to ICL, retrieval-augmented generation
to support the implementation of some baselines.                       (RAG) (Ding et al., 2024; Chen et al., 2024a) also enables
                                                                       LLMs to perform reasoning based on given information
                                                                       (e.g., retrieved knowledge). However, this information is
D. Case Study                                                          often semantically unrelated to the test problem (e.g., pro-
Figure 6 shows three cases to validate LMS3’s interpretabil-           viding external knowledge) or may not involve reasoning
ity. For brevity, we omit the solutions for demonstrations.            problems in itself (e.g., the knowledge is a declarative state-
                                                                       ment that does not require LLMs to reason). Therefore, we
For cases 1 and 2, the baselines OpenAI and Best-validate              believe that how to extend our theory to analyze when RAG
both made errors. This indicates that considering only the             is effective presents a promising research direction.
semantic similarity of demonstrations or the effectiveness of

                                                                  13
