Error: uninitialized variable

Forums Questions on specific programs Error: uninitialized variable

This topic contains 2 replies, has 2 voices, and was last updated by M E April 25, 2019 at 9:33 pm.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1635

    M E
    User

    Hi,

    I am replicating a code written by Jesús Fernández-Villaverde. It is a simple RBC model. I can compile and run the code using gfortran in terminal, but I cannot compile it with Geany. Geany tells me that there is a variable (capitalChoice) that is not initialized, which it is. I copied the code below. Please tell me where the problem lies.

    Error message:
    —————————————————————————————————-
    mkdir -p Build && gfortran -O3 -Werror -Wno-unused -fimplicit-none -Wall -fcheck=bound,do -ffpe-trap=invalid,zero,overflow -I/usr/local/include/ -J”./Build” -c “RBC.f90” -o “Build/RBC.o” (in directory: /home/mehrdad/projects/RBC)
    RBC.f90:117:0:
    mPolicyFunction(nCapital,nProductivity) = capitalChoice
    Error: ‘capitalchoice’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
    f951: all warnings being treated as errors
    Compilation failed.
    —————————————————————————————————–

    Source code:

    !============================================================================
    ! Name : RBC_F90.f90
    ! Description : Basic RBC model with full depreciation
    ! Date : July 21, 2013
    !============================================================================

    program RBC_F90
    !—————————————————————-
    ! 0. variables to be defined
    !—————————————————————-

    implicit none

    integer, parameter :: nGridCapital = 17820
    integer, parameter :: nGridProductivity = 5

    real(8), parameter :: tolerance = 0.0000001
    real :: time_begin, time_end

    integer :: nCapital, nCapitalNextPeriod, gridCapitalNextPeriod, nProductivity, nProductivityNextPeriod
    integer :: iteration

    real(8) :: aalpha, bbeta, capitalSteadyState, outputSteadyState, consumptionSteadyState
    real(8) :: valueHighSoFar, valueProvisional, consumption, capitalChoice
    real(8) :: maxDifference,diff,diffHighSoFar

    real(8), dimension(nGridProductivity) :: vProductivity
    real(8), dimension(nGridProductivity,nGridProductivity) :: mTransition
    real(8), dimension(nGridCapital) :: vGridCapital
    real(8), dimension(nGridCapital,nGridProductivity) :: mOutput, mValueFunction, mValueFunctionNew, mPolicyFunction
    real(8), dimension(nGridCapital,nGridProductivity) :: expectedValueFunction

    call CPU_TIME(time_begin)

    !—————————————————————-
    ! 1. Calibration
    !—————————————————————-

    aalpha = 0.33333333333; ! Elasticity of output w.r.t
    bbeta = 0.95 ! Discount factor

    ! Productivity value
    vProductivity = (/0.9792, 0.9896, 1.0000, 1.0106, 1.0212/)

    ! Transition matrix
    mTransition = reshape( (/0.9727, 0.0273, 0., 0., 0., &
    0.0041, 0.9806, 0.0153, 0.0, 0.0, &
    0.0, 0.0082, 0.9837, 0.0082, 0.0, &
    0.0, 0.0, 0.0153, 0.9806, 0.0041, &
    0.0, 0.0, 0.0, 0.0273, 0.9727 /), (/5,5/))

    mTransition = transpose(mTransition)

    !—————————————————————-
    ! 2. Steady state
    !—————————————————————-

    capitalSteadyState = (aalpha*bbeta)**(1.0/(1.0-aalpha))
    outputSteadyState = capitalSteadyState**(aalpha)
    consumptionSteadyState = outputSteadyState-capitalSteadyState

    print *, ‘Steady State values’
    print *, ‘Output: ‘, outputSteadyState, ‘Capital: ‘, capitalSteadyState, ‘Consumption: ‘, consumptionSteadyState

    ! Grid for capital
    do nCapital = 1, nGridCapital
    vGridCapital(nCapital) = 0.5*capitalSteadyState+0.00001*(nCapital-1)
    end do

    !—————————————————————-
    ! 3. Pre-build Output for each point in the grid
    !—————————————————————-

    do nProductivity = 1, nGridProductivity
    do nCapital = 1, nGridCapital
    mOutput(nCapital, nProductivity) = vProductivity(nProductivity)*(vGridCapital(nCapital)**aalpha)
    end do
    end do

    !—————————————————————-
    ! 4. Main Iteration
    !—————————————————————-

    maxDifference = 10.0
    iteration = 0

    do while (maxDifference>tolerance)

    expectedValueFunction = matmul(mValueFunction,transpose(mTransition));

    do nProductivity = 1,nGridProductivity

    ! We start from previous choice (monotonicity of policy function)

    gridCapitalNextPeriod = 1

    do nCapital = 1,nGridCapital

    valueHighSoFar = -100000.0

    do nCapitalNextPeriod = gridCapitalNextPeriod,nGridCapital

    consumption = mOutput(nCapital,nProductivity)-vGridCapital(nCapitalNextPeriod)
    valueProvisional = (1.0-bbeta)*log(consumption)+bbeta*expectedValueFunction(nCapitalNextPeriod,nProductivity)

    if (valueProvisional>valueHighSoFar) then ! we break when we have achieved the max
    valueHighSoFar = valueProvisional
    capitalChoice = vGridCapital(nCapitalNextPeriod)
    gridCapitalNextPeriod = nCapitalNextPeriod
    else
    exit
    end if

    end do

    mValueFunctionNew(nCapital,nProductivity) = valueHighSoFar
    mPolicyFunction(nCapital,nProductivity) = capitalChoice

    end do

    end do

    maxDifference = maxval((abs(mValueFunctionNew-mValueFunction)))
    mValueFunction = mValueFunctionNew

    iteration = iteration+1
    if (mod(iteration,10)==0 .OR. iteration==1) then
    print *, ‘Iteration:’, iteration, ‘Sup Diff:’, MaxDifference
    end if

    end do

    !—————————————————————-
    ! 5. PRINT RESULTS
    !—————————————————————-

    print *, ‘Iteration:’, iteration, ‘Sup Diff:’, MaxDifference
    print *, ‘ ‘
    print *, ‘My check:’, mPolicyFunction(1000,3)
    print *, ‘ ‘

    call CPU_TIME (time_end)
    print *, ‘Elapsed time is ‘, time_end – time_begin

    end program RBC_F90

    #1636

    Fabian Kindermann
    Moderator

    Hi,

    your compiler is in fact right. There might arise a situation in which capitalChoice is not initialized. In practice, this situation is highly unlikely. Yet, we set the compiler restrictions in geany to be very restrictive. The goal of this is to enforce people to maintain a good programming style from the very beginning. If you just compile a program from the console using just the gfortran command without any compiler flags, the compiler will be much less restrictive and accept the situation that a variable is not initialized. If such a situation arises, Fortran just assumes some number for an uninitialized variable (typically 0, but you can’t count on it).

    The solution to your problem is quite simple. Just initialze the variable capitalChoice. You could for example do this after the program statement gridCapitalNextPeriod = 1 where you just want to add a line stating capitalChoice= vGridCapital(1). Now, capitalChoice has an initial value and the compiler should be satisfied.

    Let me know if there is anything unclear with this.

    Best,
    Fabian

    #1637

    M E
    User

    Thanks a lot Fabian

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.