{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/2506-05188","title":"Counterfactual reasoning: an analysis of in-context emergence","arxiv_id":"2506.05188","date":"2025-06-05","proceeding":null,"authors":["Moritz Miller","Bernhard Schölkopf","Siyuan Guo"],"abstract":"Large-scale neural language models (LMs) exhibit remarkable performance in in-context learning: the ability to learn and reason the input context on the fly without parameter update. This work studies in-context counterfactual reasoning in language models, that is, to predict the consequences of changes under hypothetical scenarios. We focus on studying a well-defined synthetic setup: a linear regression task that requires noise abduction, where accurate prediction is based on inferring and copying the contextual noise from factual observations. We show that language models are capable of counterfactual reasoning in this controlled setup and provide insights that counterfactual reasoning for a broad class of functions can be reduced to a transformation on in-context observations; we find self-attention, model depth, and data diversity in pre-training drive performance in Transformers. More interestingly, our findings extend beyond regression tasks and show that Transformers can perform noise abduction on sequential data, providing preliminary evidence on the potential for counterfactual story generation. Our code is available under https://github.com/moXmiller/counterfactual-reasoning.git .","url_abs":"https://arxiv.org/abs/2506.05188v1","url_pdf":"https://arxiv.org/pdf/2506.05188v1.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"2506-05188","repo_url":"https://github.com/moxmiller/counterfactual-reasoning","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"counterfactual-reasoning","task_name":"Counterfactual Reasoning"},{"task_slug":"diversity","task_name":"Diversity"},{"task_slug":"in-context-learning","task_name":"In-Context Learning"},{"task_slug":"story-generation","task_name":"Story Generation"},{"task_slug":null,"task_name":"counterfactual"},{"task_slug":"regression-1","task_name":"regression"}],"methods":[{"method_slug":"focus","method_name":"Focus"},{"method_slug":"linear-regression","method_name":"Linear Regression"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2506.05188","atlas_url":"https://app.syntology.ai/?focus=2506.05188","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2506.05188"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-25T09:33:49+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"deterministic:regex_extraction","url":"https://github.com/mrtzmllr/iccr","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/moxmiller/counterfactual-reasoning","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran_draft_wrong":4,"ran_honours":1,"unverified":11},"by_repo_kind":{"official":{"samples":13,"ran":2,"repositories":1},"found_in_text":{"samples":3,"ran":3,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"0e520b701a9608ee","entry":"colormap","repo":"mrtzmllr/iccr","repo_kind":"found_in_text","path":"src/attentions.py","file_url":"https://github.com/mrtzmllr/iccr/blob/HEAD/src/attentions.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"0e520b701a9608ee"}},{"code_sha256_prefix":"fc94f27a0973e093","entry":"mean_squared_error","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/tasks.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/tasks.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"fc94f27a0973e093"}},{"code_sha256_prefix":"3fcbb3a3a960cbdb","entry":"model_to_device","repo":"mrtzmllr/iccr","repo_kind":"found_in_text","path":"src/write_eval.py","file_url":"https://github.com/mrtzmllr/iccr/blob/HEAD/src/write_eval.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"3fcbb3a3a960cbdb"}},{"code_sha256_prefix":"081d0d39b2556eae","entry":"retrieve_attentions","repo":"mrtzmllr/iccr","repo_kind":"found_in_text","path":"src/attentions.py","file_url":"https://github.com/mrtzmllr/iccr/blob/HEAD/src/attentions.py","link_basis":"first_harvest_node","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"081d0d39b2556eae"}},{"code_sha256_prefix":"348d63a78eb4cb94","entry":"squared_error","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/tasks.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/tasks.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"348d63a78eb4cb94"}},{"code_sha256_prefix":"6dab71e46d7c02d7","entry":"build_model","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/models.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/models.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"6dab71e46d7c02d7"}},{"code_sha256_prefix":"7c1cc7fd9fc6e491","entry":"derivative","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/lotka_volterra.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/lotka_volterra.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"7c1cc7fd9fc6e491"}},{"code_sha256_prefix":"c5e5709a14db68f8","entry":"direct_thetas","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/dataset.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c5e5709a14db68f8"}},{"code_sha256_prefix":"8b0dd188295931de","entry":"effective_support_size","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/dataset.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"8b0dd188295931de"}},{"code_sha256_prefix":"de267184c2825d5a","entry":"get_data_sampler","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/dataset.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/dataset.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"de267184c2825d5a"}},{"code_sha256_prefix":"3c24ac252fef0868","entry":"get_final_var","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/curriculum.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/curriculum.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"3c24ac252fef0868"}},{"code_sha256_prefix":"bfd1a4e013b52634","entry":"get_task_sampler","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/tasks.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/tasks.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"bfd1a4e013b52634"}},{"code_sha256_prefix":"8a4ce5192b8c6e35","entry":"legend_model_names","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/eval/plotting.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/eval/plotting.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"8a4ce5192b8c6e35"}},{"code_sha256_prefix":"5b5ddbde62248e97","entry":"plot_sde_bar_chart","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/eval/plotting.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/eval/plotting.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"5b5ddbde62248e97"}},{"code_sha256_prefix":"566d397aa980c8cf","entry":"retrieve_attentions","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/attentions.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/attentions.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"566d397aa980c8cf"}},{"code_sha256_prefix":"0942014e4b037c99","entry":"variance_extensions","repo":"moxmiller/counterfactual-reasoning","repo_kind":"official","path":"src/write_eval.py","file_url":"https://github.com/moxmiller/counterfactual-reasoning/blob/HEAD/src/write_eval.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"0942014e4b037c99"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}