Announcement

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

  • mata ''=' found where '(' expected'

    hi, i am trying to run sign restriction using mata:

    mata:
    void print_matrix(string name, real matrix M) {
    printf("%s: %f x %f\n", name, rows(M), cols(M))
    for (i = 1; i <= rows(M); i++) {
    for (j = 1; j <= cols(M); j++) {
    printf("%8.4f ", M[i, j])
    }
    printf("\n")
    }
    printf("\n")
    }

    // Function to perform Gram-Schmidt orthogonalization
    real matrix orthogonalize(real matrix X) {
    real scalar n
    real matrix O
    n = rows(X)
    O = X

    for (i = 1; i <= n; i++) {
    // Normalize column i
    norm_i = sqrt(sum(O[,i] :* O[,i]))
    if (norm_i == 0) {
    printf("Error: Column %f has zero norm. Cannot normalize.\n", i)
    exit()
    }
    O[,i] = O[,i] / norm_i

    // Remove projection of column i from subsequent columns
    for (j = i + 1; j <= n; j++) {
    projection = O[,j]' * O[,i]
    O[,j] = O[,j] - projection * O[,i]
    }
    }

    return(O)
    }

    // Function to apply random rotation
    real matrix random_rotation(real matrix A, real matrix Q) {
    printf("Entering random_rotation function\n")

    if (rows(A) == 0 | cols(A) == 0) {
    printf("Error: Matrix A is empty or undefined.\n")
    exit()
    }

    print_matrix("Matrix A", A)

    // Generate random matrix R
    R = runiform(rows(A), rows(A)) + I(rows(A))
    R = (R + R') / 2 // Make R symmetric
    R = R + 0.1 * I(rows(R)) // Stabilize further
    print_matrix("Matrix R (before orthogonalization)", R)

    // Rescale R
    R = R / max(R)
    print_matrix("Rescaled Matrix R", R)

    // Ensure R is valid
    if (rows(R) != cols(R)) {
    printf("Error: Matrix R is not square. Cannot perform orthogonalization.\n")
    exit()
    }

    cond_R = cond(R)
    printf("Condition number of R: %f\n", cond_R)

    // Perform Gram-Schmidt orthogonalization
    Q = orthogonalize(R)
    print_matrix("Orthogonal Matrix Q", Q)

    // Validate Q for invalid values
    if (any(Q :!= Q) | any(abs(Q) :> 1e10)) {
    printf("Error: Matrix Q contains invalid values (NaN, Inf, or excessively large values).\n")
    exit()
    }
    P = Q * Q'
    print_matrix("Orthogonality Check (P = Q * Q')", P)

    // Calculate rotated matrix
    R_rot = A * Q'
    print_matrix("Rotated Matrix R_rot", R_rot)

    return(R_rot)
    }

    // Import covariance matrix
    S = st_matrix("V") // Import covariance matrix
    if (rows(S) != cols(S)) { // Ensure S is square
    printf("Error: Covariance matrix S is not square!\n")
    exit() // Exit Mata session
    }
    printf("Dimensions of S: %f x %f\n", rows(S), cols(S))

    A = cholesky(S) // Perform Cholesky decomposition
    printf("Dimensions of A: %f x %f\n", rows(A), cols(A))

    // Test random rotation
    /*R_rot = random_rotation(A) // Apply random rotation
    printf("Dimensions of R_rot: %f x %f\n", rows(R_rot), cols(R_rot))
    */


    // Apply sign restrictions
    restrictions = (1, -1, 1, 1) // Example restrictions
    real matrix Q = .
    for (i = 1; i <= 1000; i++) {

    R_rot = random_rotation(A,Q) // Generate rotated matrix

    // Debugging: Verify A and Q remain unchanged
    printf("Iteration %d:\n", i)
    print_matrix("Matrix A (Inside Loop)", A)
    print_matrix("Matrix Q (Inside Loop)", Q)
    print_matrix("Matrix Q' (Transpose of Q)", Q')

    // Check dimensions
    printf("Dimensions of A: %f x %f\n", rows(A), cols(A))
    printf("Dimensions of Q': %f x %f\n", rows(Q'), cols(Q'))
    printf("Dimensions of R_rot: %f x %f\n", rows(R_rot), cols(R_rot))

    // Debug sign restrictions
    print_matrix("R_rot[:,1]", R_rot[,1])
    print_matrix("Restrictions", restrictions)

    // Check sign restrictions
    if (all((R_rot[,1]:*restrictions) >= 0)) { // Check restrictions
    st_matrix("Identified", R_rot) // Save identified matrix
    break
    }
    }
    end

    and the error says that
    '=' found where '(' expected
    (25 lines skipped)
    ---------------------------
    after real matrix Q = .
    and if I clear out mata and do the basic calculation, it doesn't seem to work.

    for example,
    mata:
    real matrix Q = J(4, 4, 0)
    end

    gives me an error message as ''=' found where '(' expected'.
    but the fact that "=" doesn't work is akward since I used = correctly before.
    example: A = cholesky(S) <- nothing wrong happened.

    if I replace real matrix Q = . with real matrix Q,
    it also does not work. but it worked well with making Gram-Schmidt orthogonalization function.

    what is the thing that I am misunderstanding? I am confused.
    Thank you for your help in advance.
    Last edited by sophie jane; 05 Jan 2025, 00:16.

  • #2
    When you specify "real matrix" what follows is expecting a function definition or declaration, not an assignment. See
    Code:
    help [M-2] declarations
    For example:

    Code:
    mata:
    
    Q = J(4, 4, 8)
    
    Q
    
    real matrix Q(n) return(J(n,n,2*n))
    
    Q(4)
    
    
    end
    which gives

    Code:
    :
    : Q = J(4, 4, 8)
    
    :
    : Q
    [symmetric]
           1   2   3   4
        +-----------------+
      1 |  8              |
      2 |  8   8          |
      3 |  8   8   8      |
      4 |  8   8   8   8  |
        +-----------------+
    
    :
    : real matrix Q(n) return(J(n,n,2*n))
    
    :
    : Q(4)
    [symmetric]
           1   2   3   4
        +-----------------+
      1 |  8              |
      2 |  8   8          |
      3 |  8   8   8      |
      4 |  8   8   8   8  |
        +-----------------+

    Comment

    Working...
    X