I asked ChatGPT-5 to generate a program to simulate an AR(1) process:
y(t) = rho*y(t-1) + eps(t), where eps(t) is iid Normal(0,sigma2)
The promt was:
Can you provide Fortran code to simulate an AR(1) process of the form y(t)=rho*y(t-1)+e(t), where e(t) is i.i.d. N(0,\sigma^2)?
and I got this code, which is OK but has a trivial mistake and will not compile:
!=============================================================
! AR(1) Simulation: y_t = rho * y_{t-1} + eps_t
! eps_t ~ N(0, sigma^2)
!=============================================================
module rng_normal_mod
use iso_fortran_env, only: real64
implicit none
private
public :: init_rng, randn_vec
contains
!-------------------------------------------------------------
subroutine init_rng(seed)
! Optional: initialize RNG with a deterministic seed
implicit none
integer, intent(in), optional :: seed
integer :: n, i
integer, allocatable :: seed_array(:)
call random_seed(size=n)
allocate(seed_array(n))
if (present(seed)) then
do i = 1, n
seed_array(i) = seed + 37*i
end do
else
call system_clock(count=i)
do i = 1, n
seed_array(i) = i*abs(mod(i*37, 7919))
end do
end if
call random_seed(put=seed_array)
deallocate(seed_array)
end subroutine init_rng
!-------------------------------------------------------------
subroutine randn_vec(x)
! Generate vector x of N(0,1) random numbers using Box–Muller
implicit none
real(real64), intent(out) :: x(:)
integer :: n, i
real(real64) :: u1, u2, r, theta
n = size(x)
do i = 1, n, 2
call random_number(u1)
call random_number(u2)
if (u1 == 0.0_real64) u1 = 1.0e-10_real64
r = sqrt(-2.0_real64 * log(u1))
theta = 2.0_real64 * acos(-1.0_real64) * u2
x(i) = r * cos(theta)
if (i+1 <= n) x(i+1) = r * sin(theta)
end do
end subroutine randn_vec
end module rng_normal_mod
!=============================================================
program simulate_ar1
use iso_fortran_env, only: real64
use rng_normal_mod
implicit none
! ---- Parameters ----
integer, parameter :: T = 500
real(real64), parameter :: rho = 0.8_real64
real(real64), parameter :: sigma = 1.0_real64
real(real64), parameter :: y0 = 0.0_real64
integer, parameter :: seed = 123456 ! optional fixed seed
! ---- Variables ----
real(real64), dimension(T) :: y, eps
integer :: t
integer :: unit
! ---- Initialize RNG and generate shocks ----
call init_rng(seed)
call randn_vec(eps)
eps = sigma * eps
! ---- Simulate AR(1) ----
y(1) = rho * y0 + eps(1)
do t = 2, T
y(t) = rho * y(t-1) + eps(t)
end do
! ---- Print and save results ----
print '(A)', 'First 10 simulated y values:'
print '(10F10.4)', y(1:10)
open(newunit=unit, file='ar1_vector.csv', status='replace')
write(unit,'(A)') 't,y,eps'
do t = 1, T
write(unit,'(I0,1x,ES20.10,1x,ES20.10)') t, y(t), eps(t)
end do
close(unit)
print *, 'Simulation complete. Results saved to ar1_vector.csv.'
end program simulate_ar1
Of course the bug is that the integer t and T are the same variable. It seems that the AI assumes that Fortran is a case sensitive language like e.g. Matlab