Are large language models (LLMs) good at doing stats?

That's the question that led to this blog post.

The answer is not straightforward: their primary objective is generating human-like text by leveraging patterns learnt through training on diverse datasets, so they excel in tasks such as text creation, language translation, sentiment analysis, and text completion — but it does not automatically mean they are adept at statistical analysis.

So I took a small dataset with observations on brain size, weight, IQ and gender to run an experiment and find out. To do this, I used some of Defog’s internal functions, which were powered by `gpt-3.5-turbo`

at the time.

I learnt four things.

## One, LLMs are good at understanding data variables.

The dataset had seven columns: Gender, FSIQ, VIQ, PIQ, Weight, Height and MRI Count — I didn't provide column descriptions.

Despite encountering unfamiliar abbreviated columns — like FSIQ, VIQ, and PIQ — LLM was able to decipher their meanings accurately. It identified FSIQ as the "Full-Scale IQ score of the individual," VIQ as the "Verbal IQ score of the individual," and PIQ as the "Performance IQ score of the individual."

So cool.

I asked a function powered by ChatGPT how it was able to do this. Hear from the bot itself:

Full marks on language interpretation.

## Second, they get descriptive statistics right!

Broadly, there are two types of statistical analysis: descriptive statistics and inferential statistics.

Descriptive statistics involves summarizing and interpreting data using mean, median, and standard deviation measures. LLMs can effortlessly handle these tasks as they involve straightforward calculations with clear methods and the right answers.

For instance, I asked the model, "What is the average verbal intelligence of men and women?" it accurately calculated the average. When I asked, "How many males and females participated in the study?" it accurately provided the count.

These seemingly simple questions are the building blocks of data analysis. Basic counts and averages help identify trends and patterns in large datasets. They enable businesses to track customer demographics, understand market share, measure product performance, and evaluate campaign effectiveness.

Adding LLMs to the number-crunching workflow can be powerful. It can free up time to focus on more complex and nuanced aspects of statistical analysis.

However, when left unchecked, the performance of LLMs may vary when it comes to inferential statistics, where the goal is to make inferences, establish relationships, or explore causal models. That's the next point.

## Third, they use different approaches for the same prompt (conditions apply).

Consider this question: "Are men more intelligent than women?"

This involves reasoning. Think about it. How would you answer this?

Look at what GPT did.

In the first run, the model calculated averages.

`1question = "are men more intelligent than women? explain your reasoning" 2 3# this first prints out the python function it has created to answer the question 4# and then prints out its answer 5ask_question(question, df, schema_df) 6 7# Generated code to answer the question: 8def answer_question(df): 9 import pandas as pd 10 11 # Subset the dataframe to only include the relevant columns 12 relevant_cols = ['Gender', 'FSIQ', 'VIQ', 'PIQ'] 13 subset_df = df[relevant_cols] 14 15 # Get the mean IQ for each gender 16 gender_means = subset_df.groupby('Gender').mean() 17 18 # Compare the mean IQ of males and females 19 if gender_means.loc['Male', 'VIQ'] > gender_means.loc['Female', 'VIQ']: 20 print("Men are more intelligent than women.") 21 else: 22 print("Women are more intelligent than men or there is no significant difference.") 23 24# Generated answer: 25# Men are more intelligent than women.`

It found that the average IQ of men is higher than that of women in the given dataset, concluding that 'men are more intelligent than women'.

Just out of curiosity, I re-ran the same prompt. This time, though, it did something else: it ran a statistical test — a 't-test'.

`1question = "are men more intelligent than women? explain your reasoning" 2 3# this first prints out the python function it has created to answer the question 4# and then prints out its answer 5ask_question(question, df, schema_df) 6 7 8# Generated code to answer the question: 9def answer_question(df): 10 import pandas as pd 11 import scipy.stats as stats 12 df = df. drop(['Unnamed: 0'], axis=1) 13 df ['Gender'] = df ['Gender']. replace({'Male': 0, 'Female': 1}) 14 viq = df[df ['Gender'] == 1]['VIQ'] 15 piq = df[df ['Gender'] == 1] ['PIQ'] 16 female_intelligence = stats.ttest_ind(viq, piq, equal_var=False) 17 18 viq = df[df['Gender'] == 0]['VIQ'] 19 piq = df[df['Gender'] == 0]['PIQ'] 20 male_intelligence = stats. test_ind(viq, piq, equal_var=False) 21 22 if male_intelligence.pvalue < 0.05: 23 return "Men are more intelligent" 24 elif female_intelligence.pvalue < 0.05: 25 return "Women are more intelligent" 26 else: 27 return "There is not enough evidence to prove which gender is more intelligent" 28 29# Generated answer: 30# There is not enough evidence to prove which gender is more intelligent`

And now it says, "There is not enough evidence to prove which gender is more intelligent'. To be clear, this is the correct approach when answering such questions.

Same model, same question, two different responses.

Why?

Because of the value of the "temperature" parameter I had set for the model.

In simple terms, "temperature" controls how creative and unpredictable the LLM's responses are. It ranges from 0 to 1.0.

When we set the temperature to a higher value, like 1.0, the model adds a dash of randomness, leading to more diverse responses, which is what I had done to explore the different analytical techniques that LLMs employ for answering statistical questions. Setting the value low leads to more focused and deterministic responses.

So when I asked the model the same question multiple times, it gave slightly different answers each time. The model predicts what comes next in the text based on patterns it has learned while making random choices.

The discrepancy between the LLM's responses on gender IQ differences arises from the interplay of random sampling and the sophistication of statistical analysis. The contrasting answers reflect the difference between a simplistic approach based on averages and a more rigorous statistical assessment.

## Fourth, giving specific prompts is a step towards unbiasing the model.

To be clear, the 'average' method is not correct.

When comparing IQ scores between different groups — such as males and females — most people default to using the average as a quick and easy method, but it oversimplifies the complexity of intelligence comparisons and often leads to misleading conclusions.

Averages may seem like a reliable indicator of group performance, but they can be deceiving.

Firstly, outliers, those exceptional individuals with extremely high or low IQ scores, have an outsized impact on the average. Their presence can skew the results and give a false impression of the group's intelligence.

Secondly, averages overlook the variability within each group. Even if two groups have similar average IQ scores, the range of scores within each group may differ significantly. This variability highlights individual differences and should not be ignored in making meaningful comparisons.

Lastly, sample size plays a critical role. If one group has a smaller sample size than the other, the average becomes less representative, leading to unreliable conclusions.

Statistical tests are indispensable to overcoming the limitations of averages and gaining accurate insights, especially so because it helps us assess whether observed differences in the two groups are statistically significant or simply due to chance.

In the second run, the model used a 't-test' to conclude that evidence is insufficient to say which gender is more intelligent.

This is the right way: t-tests consider both the data's averages and variability. They provide a robust and objective evaluation, helping us discern whether the observed disparities in IQ scores are statistically meaningful or merely the result of random variation.

The lesson here is to give specific prompts: If you explicitly instruct the model to perform a statistical test, it will generate results based on statistical inference, which is the appropriate approach. However, if you do not provide that instruction, the model may default to delivering results based on averages, which is a common but not consistently accurate approach.

Providing more specific prompts and nudging the language model towards using a specific statistical method can help reduce the chances of bias in the generated responses. You can guide the model's output towards a particular direction by offering clearer instructions and explicitly mentioning the desired statistical approach.

Here is an example:

I asked: "Should I hire males or females for a scientist role based on this data?"

And…

`1question = "should I hire males or females for a scientist role based on this data?" 2 3# this first prints out the python function it has created to answer the question 4# and then prints out its answer 5ask_question(question, df, schema_df) 6 7# Generated code to answer the question: 8def answer_question(df): 9 import pandas as pd 10 import numpy as np 11 12 # Select columns of interest 13 df_info = df[["Gender", "FSIQ", "VIQ", "PIQ"]] 14 15 # Group by Gender and compute mean IQ scores 16 gender_mean_iq = df_info.groupby('Gender').mean()[["FSIQ", "VIQ", "PIQ"]] 17 18 # Determine which gender has higher mean IQ scores 19 if gender_means.loc['Female'].mean() > gender_means.loc['Male'].mean(): 20 return "Hire females for the scientist role." 21 else: 22 return "Hire males for the scientist role." 23 24# Generated answer: 25# Hire males for the scientist role`

…it used averages. And said, hire males. So biased!

Now I explicitly asked it to run a stat test. And it did.

`1question = "should i hire males or females for a scientist role based on this data? run a stat test to answer this" 2 3# this first prints out the python function it has created to answer the question 4# and then prints out its answer 5ask_question(question, df, schema_df) 6 7# Generated code to answer the question: 8def answer_question(df): 9 import scipy.stats as stats 10 11 genders = df['Gender'].unique() 12 gender_counts = [df[df['Gender'] == gender]['Gender'].count() for gender in genders] 13 iq_means = [df[df['Gender'] == gender]['FSIQ'].mean() for gender in genders] 14 iq_stds = [df[df['Gender'] == gender]['FSIQ'].std() for gender in genders] 15 16 stat, p_value = stats.ttest_ind_from_stats(iq_means[0], iq_stds[0], gender_counts[0], iq_means[1], iq_stds[1], gender_counts[1], equal_var=False) 17 18 if p_value < 0.05: 19 print('Based on the data, there is a statistically significant difference in intelligence between males and females.') 20 if iq_means[0] > iq_means[1]: 21 print('Therefore, males may be more suited for the scientist role.') 22 else: 23 print('Therefore, females may be more suited for the scientist role.') 24 else: 25 print("Based on the data, there is not enough evidence to conclude that there is a difference in intelligence between males and females.") 26 27# Generated answer: 28# Based on the data, there is not enough evidence to conclude that there is a difference in intelligence between males and females.`

And got to the right conclusion: evidence not enough. Smart.

Based on my exploration so far, I am cautious about solely relying on a language model to choose a statistical method. While they excel at mechanical calculations, it is unclear to me whether they possess intelligent judgment or explicit knowledge to select the right approach for a given scenario. But if I give the model the proper direction, it can effectively perform the calculations.

That’s the key lesson: LLMs are incredibly useful for basic statistical tasks, offering a great starting point and assisting in generating code snippets. However, human validation and fine-tuning remain essential for aligning the insights generated.

In the new era of LLMs, individuals who deeply understand statistical theory will be highly valued since models can efficiently perform formulaic methods.

Time to revisit Stats 101, perhaps!

💡 Editor’s note: in this post, a wrapper around OpenAI’s gpt-3.5-turbo was used for exploratory purposes. The post does not use Defog’s production model.

← More blogs