Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • A loop for calculating gradient vectors in MATA

    Hello everyone

    I am wondering whether you could help me out with the following issue. I am trying to compute the vector of first partial derivative for a given utility function with respect to a set of parameters. In my case, since the utility function depends on 4 parameters, then the vector of first partial derivative should have 4 columns. I am trying to do this using Mata. I know that there are options in Mata to do so. Despite this fact, I have been facing some problems when using Mata. The issue is that I would like to evaluate the first partial derivatives at 10 different points. These points represents different combinations of payoffs an individual can get for a given game. I have data for 10 games, so I would like to get the vector of numerical first partial derivatives (w.r.t. to 4 parameters) evaluated at each of the 10 different combinations of payoffs described in the games.

    Here I show you the code I am using for this.

    First, I am determining the data containing the payoffs for the 10 games:

    use "payoffs"

    mata

    x=st_data(.,("f1","f2","f3","s1","s2","s3", ))

    end

    This gives me the following matrix

    x
    f1 f2 f3 s1 s2 s3
    -3 -7 -7 5 -3 6
    7 -6 5 3 -4 0
    -7 -4 -5 8 -5 8
    -3 -4 -1 4 3 7
    -2 -7 6 5 -2 8
    -1 4 -7 3 7 0
    -3 -1 1 -7 -4 6
    0 2 -5 1 2 1
    1 -7 -4 4 3 4
    -2 5 -4 7 -2 -2
    Then, I am using the following code to compute the derivatives and to evaluate them at each of the combinations described above. For instance, for the payoffs at the first row this code allows me to get a vector with the first partial derivatives, evaluated at (-3, -7, -7, 5, -3, 6)

    mata

    void utility_t(p, x, Prob) {


    rhoS = p[1]
    sigmaS = p[2]
    theta = p[3]
    lambdaS = p[4]

    UrS = x[1,6] :- rhoS*(x[1,6] :- x[1,3]) :- sigmaS*(x[1,3] :- x[1,6]) :- theta*(x[1,3] :- x[1,6])
    UlS = x[1,5] :- rhoS*(x[1,5] :- x[1,2]) :- sigmaS*(x[1,2] :- x[1,5]) :- theta*(x[1,2] :- x[1,5])

    Prob = 1/(exp(lambdaS*(UlS:-UrS)) + 1)

    }

    D1 = deriv_init()

    deriv_init_evaluator(D1, &utility_t())
    deriv_init_evaluatortype(D1, "t")
    deriv_init_params(D1, (0.2493496, 0.0327267, 0.0454357, 0.3370936))
    deriv_init_argument(D1, 1, x)

    y1=deriv(D1,1)

    y1

    end

    This code gives me the vector (-.2100827824, .2100827825, .2100827825, .5165310145), which represents the first partial derivatives w.r.t to the parameters under question and evaluates at the payoffs corresponding to the first row of the payoff matrix. Notice that (0.2493496, 0.0327267, 0.0454357, 0.3370936) stands for the values for the parameters that have been calculated previously by regression analysis in Stata.

    Now, I would like to repeat this procedure for the rest of rows. That is, I would like to get 9 more vectors for the rest of rows. In my real data, I have 240 games, so I would like to use a loop for doing this. However, I have failed to create such a loop. For that reason, I am wondering if you are able to help me with this. So far, I have done the following:

    mata

    void utility_t(p, x, i, Prob) {

    rhoS = p[1]
    sigmaS = p[2]
    theta = p[3]
    lambdaS = p[4]

    UrSi = x[i,6] :- rhoS*(x[i,6] :- x[i,3]) :- sigmaS*(x[i,3] :- x[i,6]) :- theta*(x[i,3] :- x[i,6])
    UlSi = x[i,5] :- rhoS*(x[i,5] :- x[i,2]) :- sigmaS*(x[i,2] :- x[i,5]) :- theta*(x[i,2] :- x[i,5])

    Probi = 1/(exp(lambdaS*(UlSi:-UrSi)) + 1)

    }

    Di = deriv_init()

    deriv_init_evaluator(Di, &utility_t())
    deriv_init_evaluatortype(Di, "t")
    deriv_init_params(Di, (0.2493496, 0.0327267, 0.0454357, 0.3370936))
    deriv_init_argument(Di, 1, x)

    yi=deriv(Di,1)

    yi

    end

    forvalues i=1/10 {

    mata:utility_t(p,`i', x, ProbF)

    }

    This does not work. Any suggestion that helps me with this will be very welcome,

    Thanks in advance for your cooperation,


    Abel Lucena

    Last edited by Abel Lucena; 30 May 2017, 07:12.
Working...
X