<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://rahmani-hossein.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://rahmani-hossein.github.io/" rel="alternate" type="text/html" /><updated>2026-08-23T09:01:06-07:00</updated><id>https://rahmani-hossein.github.io/feed.xml</id><title type="html">Hossein Rahmani</title><subtitle>personal description</subtitle><author><name>Hossein Rahmani</name><email>hrahmani@math.ubc.ca</email></author><entry><title type="html">Causal LLM Attributions</title><link href="https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md" rel="alternate" type="text/html" title="Causal LLM Attributions" /><published>2025-01-26T00:00:00-08:00</published><updated>2025-01-26T00:00:00-08:00</updated><id>https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions</id><content type="html" xml:base="https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md"><![CDATA[<p>You shouldn’t miss this if you want to know how the context of your prompt affects in your Preferred LLM response.</p>

<h1 id="causal-prompt-attribution-in-large-language-models-llms">Causal Prompt Attribution in Large Language Models (LLMs)</h1>

<h2 id="introduction">Introduction</h2>

<p>Large Language Models (LLMs) have revolutionized natural language processing, achieving impressive performance across various tasks. However, understanding how individual words in a prompt influence the model’s output remains a challenge. This blog post introduces a novel framework for quantifying the influence of individual words in a prompt on the responses generated by LLMs. Our approach leverages the <strong>Average Marginal Effect (AME)</strong>, a metric that measures the expected marginal contribution of adding a particular word to a randomly sampled subset of other words.</p>

<h2 id="the-average-marginal-effect-ame">The Average Marginal Effect (AME)</h2>

<p>The AME is a scalable and principled measure of how individual elements, such as training data points or prompt words, contribute to a model’s behavior. In the context of LLMs, the AME quantifies the influence of individual words on the model’s output. When the sampling distribution is uniform, the AME aligns with the well-known <strong>Shapley value</strong>.</p>

<h3 id="definition-of-ame">Definition of AME</h3>

<p>For a given element $n$, the AME is defined as:
$ \text{AME}<em>{n} = E</em>{S^{n}\sim L^{n}}[U(S^{n}+{n})-U(S^{n})$ 
Here, $L^{n}$ is a distribution over subsets $S^{n}$ that exclude $n$, and $U(S)$ is a utility function that evaluates an outcome of interest (e.g., token-level probabilities) for a subset $S$ of the original set.</p>

<h2 id="algorithms-for-sparse-ame-estimation">Algorithms for Sparse AME Estimation</h2>

<p>We propose two algorithms to estimate sparse AMEs at the word level:</p>

<h3 id="1-uncompleted-prompt-design">1. Uncompleted-Prompt Design</h3>

<p>This design aims to reveal word-level contributions by partially masking the original prompt $D_{\text{org}}$. Words are retained with probability $p$, and omitted words are replaced with a placeholder symbol. The LLM is then queried with this incomplete prompt, and the utility $U(D_{p})$ is measured. This approach provides insights into which words have a substantial influence on the model’s output.</p>

<h3 id="2-completed-prompt-design">2. Completed-Prompt Design</h3>

<p>This design includes an additional step: filling in the missing words with candidates drawn from a baseline distribution before querying the LLM. This approach simulates a more “complete” context, making the causal effect of including or excluding a particular word clearer.</p>

<h4 id="baseline-construction">Baseline Construction</h4>

<p>To build the baseline distribution, we generate multiple incomplete prompts by sampling $p$ and masking words accordingly. For each incomplete prompt, we query the LLM to produce completions for the missing words. Over multiple trials, these completions form an empirical distribution of likely substitutes for each blank position.</p>

<h3 id="computing-ames-via-regression">Computing AMEs via Regression</h3>

<p>After collecting $M$ sampled prompts and their corresponding utility values, we construct data matrices for AME estimation. Let $\mathbf{X}$ be an $M\times N$ matrix, where $N$ is the number of words in $D_{\text{org}}$, and $\mathbf{Y}$ be an $M$-dimensional vector of utility values. AME estimation proceeds through sparse linear regression, as described by Lin et al. (2022), achieving query complexity on the order of $(O(k\log N)$.</p>

<h2 id="experimental-details">Experimental Details</h2>

<h3 id="headline-classification-example-with-token-level-attribution">Headline Classification Example with Token-Level Attribution</h3>

<p>In this example, the LLM output is a single word, and we aim to find the attribution of the context in headline classification. We used the <strong>Uncompleted-Prompt Design</strong> with $M=200$ samples for estimating AME.</p>

<h4 id="context">Context</h4>

<p><strong>Local Mayor launches Initiative to Enhance Urban Public Transport.</strong></p>

<h4 id="query">Query</h4>

<p>Classify the headline into one of the following categories: <strong>Technology, Politics, Sports, Art, or Others</strong>.</p>

<h4 id="generated-response">Generated Response</h4>

<p><strong>Politics</strong></p>

<h4 id="context-attribution">Context Attribution</h4>

<p>Below, we show how each token contributes to the classification decision. Higher positive scores indicate stronger influence toward “Politics”.</p>

<table>
  <thead>
    <tr>
      <th>Token</th>
      <th>Index</th>
      <th>Coefficient</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Local</td>
      <td>1</td>
      <td>0.00</td>
    </tr>
    <tr>
      <td>mayor</td>
      <td>2</td>
      <td>5.83</td>
    </tr>
    <tr>
      <td>launches</td>
      <td>3</td>
      <td>0.00</td>
    </tr>
    <tr>
      <td>Initiative</td>
      <td>4</td>
      <td>-1.93</td>
    </tr>
    <tr>
      <td>to</td>
      <td>5</td>
      <td>0.00</td>
    </tr>
    <tr>
      <td>enhance</td>
      <td>6</td>
      <td>0.00</td>
    </tr>
    <tr>
      <td>urban</td>
      <td>7</td>
      <td>2.76</td>
    </tr>
    <tr>
      <td>public</td>
      <td>8</td>
      <td>4.06</td>
    </tr>
    <tr>
      <td>transport</td>
      <td>9</td>
      <td>6.49</td>
    </tr>
  </tbody>
</table>

<p><strong>Interpretation</strong>: The words “<strong>mayor</strong>” and “<strong>transport</strong>” are key drivers toward classifying this headline under Politics, while “<strong>urban</strong>” and “<strong>public</strong>” also contribute positively.</p>

<h2 id="conclusion">Conclusion</h2>

<p>Our framework provides a scalable, interpretable, and causally grounded means of detecting which parts of the prompt drive token-level generation in LLM outputs. By illuminating the underlying mechanisms that connect input words to generated text, our work aids in diagnosing biases, assessing data fidelity, and enhancing overall trust in large-scale language models.</p>

<h2 id="references">References</h2>

<ul>
  <li>Cohen-Wang, B., Shah, H., Georgiev, K., &amp; Madry, A. (2024). Contextcite: Attributing model generation to context. <em>arXiv preprint arXiv:2409.00729</em>.</li>
  <li>Ghorbani, A., &amp; Zou, J. (2019). Data shapley: Equitable valuation of data for machine learning. In <em>International conference on machine learning</em> (pp. 2242-2251). PMLR.</li>
  <li>Hammoudeh, Z., &amp; Lowd, D. (2024). Training data influence analysis and estimation: a survey. <em>Machine Learning</em>, 113(5), 2351-2403.</li>
  <li>Jia, R., Dao, D., Wang, B., Hubis, F. A., Hynes, N., Gurel, N. M., Li, B., Zhang, C., Song, D., &amp; Spanos, C. J. (2019). Towards efficient data valuation based on the shapley value. In <em>The 22nd International Conference on Artificial Intelligence and Statistics</em> (pp. 1167-1176). PMLR.</li>
  <li>Lin, J., Zhang, A., Lecuyer, M., Li, J., Panda, A., &amp; Sen, S. (2022). Measuring the effect of training data on deep learning predictions via randomized experiments. <em>ArXiv</em>, abs/2206.10013.</li>
</ul>]]></content><author><name>Hossein Rahmani</name><email>hrahmani@math.ubc.ca</email></author><category term="LLM" /><category term="Average Marginal Effect" /><category term="Causal Effect Estimation by Orthogonal Estimator" /><summary type="html"><![CDATA[You shouldn’t miss this if you want to know how the context of your prompt affects in your Preferred LLM response.]]></summary></entry></feed>