To illustrate that, I asked ChatGPT "In one sentence, why do LLM
responses vary on the same prompt?" and got three different, although clearly
related and overlapping, answers:

*LLM responses vary on the same prompt due to the model's stochastic nature,
diverse training data, and sensitivity to slight changes in input
phrasing. *

I've had low-level involvement in several research projects at work using LLMs over the last few months. When testing these kinds of systems you will typically want to evaluate the correctness, appropriateness, and relevance of the responses but, in one of the experiments I was running recently, the interesting question was whether two implementations we had built varied to a comparable extent.

If you like that kind of thing (and I do) you could frame it this way:

is the variability variable?

To investigate it, I needed three things:

- a variability metric
- a way to calculate the metric
- a comparison function for those metrics across systems under test

Although the output of the system I was looking at was textual, it was
possible to extract a list of key items from the response and ignore most of
the text. For instance, in three responses to the same prompt I might identify
five concepts:

... A ... B ... C ... D ... E ...

... B ... A ... D ... C ... E ...

... A ... B ... C ... F ... E ...

Then my simple-minded metric simply counted the number of uniquely ordered lists generated by some number of runs for that prompt. In the example above, this would be three because no two rows are the same.

It's an easy metric to calculate given that I can extract the key concepts, so
the question then turns to how to compare these metrics across two variants of
a system under test.

My instinct was that there would be statistical approaches that could be
applied but I'm as qualified in statistics as I am in AI. I thought that I
wanted something

- quick and simple for a non-expert to conceptualise and interpret
- easy to implement in Google Sheets
- with no strong assumptions about the source data

So I asked a friendly statistician who, like a good tester, knew how to ask scoping questions, gauge the level at which a client is able to hold a conversation, and provide relevant information concisely. Given what I was able to tell her about my needs, she pointed me at the paired t-test and its documentation in Google Sheets.

Bingo! I had a metric, a way to calculate the metric, and a way to compare across implementations of the system under test.

Here's an example showing how it might work:

Each row (Run) represents a different prompt submitted 100 times while columns represent the implementations. Baseline is an existing implementation and SUT (System Under Test) 1 and 2 are alternative approaches.

The values represent the variation in response for each run according to my crude metric, i.e. how many unique lists there were. So, for prompts 4 and 5, SUT 1 generates many more response variants than the other two.

The heatmap and the means give us an intuitive sense that SUT 2 is "close" to the Baseline while SUT 1 is further away.

The *pairs *of the paired t-test are the values being compared for each
row, so in the analysis of Baseline against SUT 1 the first few pairs are (21,
47), (22, 39), and (53,44).

The p-values show the result of running the paired t-test on the SUT columns against the baseline. The function is pretty straightforward:

Now, starting with the *null hypothesis *that the SUTs vary the same
amount as the Baseline, if we apply a
hypothesis test at the 5% level
we see that SUT 1 is less than 0.05, suggesting it is different to Baseline,
while SUT 2 is greater and so suggests similar variation to Baseline.

Neat, right? Our intuitive reaction to the data is apparently backed up by some basic statistics.

But.

But we're testers aren't we, so we want to test the way we analyse our data and the conclusions we're comfortable drawing from that analysis. So I mocked up some more data and ran the same analysis process.

MOCK 3 and MOCK 4 are attempts to see how the system behaves at limits. MOCK 3 is deliberately very different, while MOCK 4 is identical to Baseline. Notice that the p-value for MOCK 3 is small, as we would hope, but the p-value for MOCK 4 is undefined. Hmm.

I played around with small variations to MOCK 4 and found that it's quite easy
for data that looks very close to the Baseline in terms of values, heatmap,
and mean to have a p-value either below 0.05 (MOCK 5) or above (MOCK 6). This tests my faith in the t-test, or my understanding of it.

So why do we see this?

From my friendly statistician again: the paired t-test works on the *differences *for each pair. In the
image above, the differences are shown on the right, i.e. for MOCK 3 against Baseline the
paired differences for the first three runs are -78, -78, and -47. The t-test
assesses whether the mean of the differences is different from zero and in the case of MOCK 3 it is very different.

If we go back to SUT 1, 2 and Baseline, we can now recast the initial experiment in much more statistical language. For SUT 1, the mean of the paired differences in the number of uniquely ordered lists was statistically significantly different from zero (at the 5% significance level) suggesting a difference in variation of responses between these two implementations. Whereas the p-value for SUT 2 and Baseline was above 0.05, supporting the hypothesis that there is no variation in response between those two implementations.

What about MOCK 5 and MOCK 6, then? The differences there both appear close to zero to the untrained eye.

In the case of MOCK 5, the paired values are all either 0 or 1 and the mean of the differences is 0.4. This may seems small but it's considered to be a statistically strong "signal" relative to the low level of difference in the data. It is this that gives the statistically significant result.

If this feels counter-intuitive, gloss it this way: there is relatively little difference between MOCK 5 and Baseline, but what difference there is is all in one direction (the differences are all positive). This influences the t-test outcome.

This is clear in comparison to MOCK 6 which, on the surface, looks very similar. Notice that the differences are 0, 1, and -1 and are evenly distributed either side of 0, with a mean of 0. The "signal" is weaker and so the t-test goes the other way.

To reduce the
likelihood of what we might call *impractical *results such as these we can alter the threshold at
which we accept a statistically significant result. For example, instead of testing at the 5% level
and using 0.05, we could test at the 1% threshold and use 0.01.

Finally, MOCK 4 fails to yield a p-value because inside the t-test calculation
there is a division by zero. That topic was
discussed on Cross Validated, a statistics stack exchange.

What does this tell us?

For me, it says that the paired t-test could be a useful tool for comparison
of the variability of results in my experiments but blindly applying it is a
bad idea. The statistics here are playing the part of an oracle against which
I can test my intuition. However, as with any testing oracle you don't
understand well, it's worth testing the oracle too.

Images: Bing Image Creator

P.S. Thank you to my friendly statistician for help and review. She knows what she's talking about but I've given her an imprecisely-specified task and interpreted her words, so any errors are mine.

[[..Pingback..]]

ReplyDeleteThis article was curated as a part of #122nd Issue of Software Testing Notes Newsletter.

https://softwaretestingnotes.substack.com/p/issue-122-software-testing-notes

Web: https://softwaretestingnotes.com