"Fossies" - the Fresh Open Source Software Archive

Member "gretl-2020b/addons/SVAR/SVAR_compatibility_funcs.inp" (11 Apr 2020, 4748 Bytes) of package /linux/misc/gretl-2020b.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Charmm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "SVAR_compatibility_funcs.inp": 2020a_vs_2020b.

    1 # Backward compatibility functions which eventually should become
    2 # obsolete
    3 # (either because they are in extra.zip, or other reasons)
    4 
    5 
    6 ##################################
    7 # getstrings_byindex: gretl 2020b will have the built-in feature
    8 # to slice / fancy-index into an array with a vector
    9 
   10 function strings getstrings_byindex(const strings S, const matrix indices)
   11     # collects the (in general non-contiguous) sub-array
   12     # indexed by indices (imitate fancy indexing)
   13     strings out = null
   14     matrix m = vec(indices) 
   15     loop i = 1..nelem(m) -q
   16         out += S[m[i]]
   17     endloop
   18     return out
   19 end function
   20 
   21 
   22 ### drawnormwis() was called BayesianDraw
   23 ### It will go into extra 0.7 in some incarnation.
   24 
   25 function matrices drawnormwis(const matrix iXX, const matrix B,
   26                                const matrix Sigma, int T[1::])
   27 
   28     # Draw from a standard Normal-(inverse)-Wishart prior 
   29     # for a multi-equation regression model.
   30     #
   31     # iXX:  K x K matrix (X'X)^{-1} (same for all equations!)
   32     #       (in a VAR context gretl provides this as $xtxinv)
   33     # B:    matrix of purely data-based estimates (max-lik)
   34     # Sigma: cross-equation covariance matrix of innovations
   35     # T:    number of observations
   36 
   37     K = rows(B) 
   38     N = cols(B) # how many equations 
   39 
   40     # some checks 
   41     if K != rows(iXX)
   42         funcerr "Coeff and data matrix dims don't match"
   43     elif N != rows(Sigma)
   44         funcerr "Coeff and Cov matrix dims don't match"
   45     endif
   46 
   47     matrix Sigma_draw = iwishart(Sigma*T, T)
   48     matrix V = Sigma_draw ** iXX
   49     matrix C = cholesky(V)
   50     matrix B_draw = vec(B)
   51 
   52     B_draw += C * mnormal(K * N, 1)
   53     B_draw = mshape(B_draw, K, N)
   54 
   55     return defarray(B_draw, Sigma_draw)
   56 end function
   57 
   58 ###
   59 # In gretl 2020a there will be 'instrings' with the same 
   60 # functionality as this strpos_allin
   61 # (I hope it also returns a column vector...!)
   62 
   63 function matrix strpos_allin(strings S, string search_pattern)
   64     # Function to determine the numerical position(s) of a 
   65     # string in an array
   66 
   67     matrix ret = {}
   68     loop i=1..nelem(S) -q
   69         if S[i] == search_pattern
   70             ret |= i
   71         endif
   72     endloop
   73     return ret 
   74 end function
   75 
   76 
   77 ###
   78 # A very similar functionality is in splitfname in extra 0.6
   79 
   80 function strings basename(string fn)
   81     string base = regsub(fn, "(.*)\.([^\.]*)", "\1")
   82     string ext = fn + (strlen(base) + 1)
   83     return defarray(base, ext)
   84 end function
   85 
   86 
   87 
   88 ###
   89 ### the drill() function is in extra >= 0.6
   90 ###
   91 
   92 function matrix drill(const matrices x,
   93                       matrix rowspec[null],
   94                       matrix colspec[null])
   95 
   96     # This function "drills through" a matrix array and returns a matrix;
   97     # for example, drill(x, 2, 3) returns a vector with the [2,3] elements
   98     # of all matrices in the x array. "0" means "all".
   99     #
  100     # NOTA BENE: all matrices must be the same size
  101 
  102     matrix ret = {}
  103     n = nelem(x)
  104 
  105     if n == 0
  106         return ret
  107     endif
  108 
  109     ### check sizes
  110 
  111     nr = rows(x[1])
  112     nc = cols(x[1])
  113     
  114     same_dim = 1
  115     loop i = 2 .. n --quiet
  116         same_dim = same_dim && (rows(x[i]) == nr) && (cols(x[i]) == nc) 
  117         if !same_dim
  118             printf "Error: Inconsistent dimensions (not all matrices are the same size)\n"
  119             return ret
  120         endif
  121     endloop
  122 
  123     ### process specs
  124 
  125     if !exists(rowspec) 
  126         matrix rs = seq(1, nr)'
  127     else
  128         if rowspec[1] == 0
  129             matrix rs = seq(1, nr)'
  130         else
  131             matrix rs = vec(rowspec) # force to column
  132         endif
  133     endif
  134 
  135     if !exists(colspec)
  136         matrix cs = seq(1, nc)'
  137     else
  138         if colspec[1] == 0
  139             matrix cs = seq(1, nc)'
  140         else
  141             matrix cs = vec(colspec) # force to column
  142         endif
  143     endif
  144         
  145     ### check for multiple or illegal specs
  146     
  147     scalar nrspec = rows(rs)
  148     scalar ncspec = rows(cs)
  149 
  150     if xmin(nrspec, ncspec) > 1
  151         printf "Error: you can’t have multiple row and column specs\n"
  152         return ret
  153     endif
  154     
  155     if minc(rs|cs) < 0
  156         printf "Error: negative spec not allowed\n"
  157         return ret
  158     endif
  159     
  160     if maxc(rs) > nr
  161         printf "Error: incorrect row spec (matrices have %d rows, but %d wanted)\n", nr, maxc(rs)
  162         return ret
  163     endif
  164     
  165     if maxc(cs) > nc
  166         printf "Error: incorrect col spec (matrices have %d columns, but %d wanted)\n", nc, maxc(cs)
  167         return ret
  168     endif
  169     
  170     ### do the actual drilling
  171 
  172     if nrspec == 1 
  173         ret = flatten(x)[rs,]
  174         ret = transp(mshape(ret, nc, n))
  175         ret = ret[,cs]
  176     elif ncspec == 1 
  177         ret = flatten(x,1)[,cs]
  178         ret = mshape(ret, nr, n)
  179         ret = ret[rs,]
  180     endif
  181     
  182     return ret
  183 end function
  184