When AI Gets It Wrong—and Then Gets It Right

Written by pairprogramming | Published 2025/05/25
Tech Story Tags: human-ai-collaboration | pair-programming | copulas | prompt-engineering | monte-carlo-simulation | stochastic-dependence-modeling | natural-language-processing | no-code-statistical-modeling

TLDRChatGPT initially misapplies copula sampling, but after being guided with the proper theory, it corrects its code and demonstrates a strong ability to learn and assist in statistical modeling.via the TL;DR App

Table of Links

Abstract and 1 Introduction

2 Methodology

2.1 The task

2.2 The communication protocol

2.3 The copula family

3 Pair programming with ChatGPT

3.1 Warm up

3.2 The density

3.3 The estimation

3.4 The sampling

3.5 The visualization

3.6 The parallelization

4 Summary and discussion

5 Conclusion and Acknowledgments

Appendix A: The solution in Python

Appendix B: The solution in R

References

3.4 The sampling

We see that u1 and u2 are drawn from the standard uniform distribution, which is typically used in such sampling algorithms. However, then these are just used as arguments of (3), resulting in two identical one-dimensional samples from the distribution of C(U1, U2), where U1 and U2 are two independent random variables with the standard uniform distribution. Note if the random vector (U1, U2) would be distributed according to C, then the distribution function of C(U1, U2) would be the distribution known as Kendall function; see Joe (2014, pp. 419–422). So we are witnessing a mixture of approaches related to copula modelling, which, however, do not lead to a correct solution. A sample generated with this code for θ = 2 is shown at the left-hand side of Figure 1, and it is clearly not a sample from the Clayton copula Cθ with parameter θ = 2.

In the second try, ChatGPT also failed to produce a correct solution; see the sample generated by this code at the center of Figure 1. Here, the reasoning behind is even less clear. These two trivial examples again illustrate that we must be extremely careful about results produced by ChatGPT. On the other hand, this gives us a chance to show that ChatGPT can interactively learn new concepts by feeding it with the necessary theory as we will demonstrate next.

A standard approach for efficient sampling from Archimedean copulas was introduced in Marshall and Olkin (1988). Let us feed ChatGPT with the related theory and ask it for a correct solution.

Apart from producing a correct solution, which generated the sample at the right-hand side of Figure 1, this is a clear demonstration of the ability of ChatGPT to learn during the interaction with the user (having in mind that this capacity is just a quite convincing illusion enabled by the prompt engineering technique applied to ChatGPT, as discussed in Section 2.2). In contrast to the previous example, where it “only” translated the fed formula for the PDF to a more-or-less similarly looking code, this example shows that ChatGPT is able to understand even relatively complex concepts.

Such an ability makes ChatGPT a feasible tool also in cases when it faces unknown concepts. This is essential, e.g., in cases when a new theory developed by a researcher is about to be coded. Also, notice that ChatGPT saves us time by mapping our concepts, e.g., the standard exponential and gamma distribution, to existing functions in the considered programming language. Particularly, without explicitly asking, ChatGPT avoided a loop iterating over 1 to n in which one sample from the copula would be generated, which is typically slow, but generates directly n samples of E1 and E2 from the standard exponential distribution (exprnd(1, 2, n), where 1 denotes the parameter of the exponential distribution). We can thus avoid probably the most boring part of coding when we are browsing the documentation for a particular function in available libraries.

Finally, let us perform a simple check of all the previously generated functions.

Being in the context of the previous conversation, ChatGPT exploited the ClaytonCopulaMLE function generated in Section 3.3. After executing the simple check code, we got the following error.

We copy-pasted the error message to ChatGPT, e.g., gave it the prompt

where was the error message, and got the following response.

ChatGPT not only detected what is wrong, but provided a corrected solution together with an explanation of the problem. Given that these trivial mistakes occur quite often in daily practice, such help that points out to the problem or even solves it can significantly save time. After executing the simple check with the corrected version of ClaytonCopulaDensity, we got theta hat = 2.12. So far so good.

Author:

(1) Jan G´orecki, Department of Informatics and Mathematics, Silesian University in Opava, Univerzitnı namestı 1934/3, 733 40 Karvina, Czech Republic (gorecki@opf.slu.cz).


This paper is available on arxiv under CC BY 4.0 DEED license.


Written by pairprogramming | Pair Programming AI Companion. You code with me, I code with you. Write better code together!
Published by HackerNoon on 2025/05/25