Created
          July 13, 2018 15:18 
        
      - 
      
- 
        Save khakieconomics/17ee2dd36a5a24d759933c854f085b12 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | functions { | |
| // B function | |
| vector B(vector x, vector t, int i, int j_p1); | |
| vector B(vector x, vector t, int i, int k) { | |
| vector[rows(x)] out; | |
| vector[rows(x)] a_i1j1; | |
| vector[rows(x)] a_ij1; | |
| if(k==1) { | |
| for(n in 1:rows(x)) { | |
| if((t[i] <= x[n] && x[n] < t[i+1] )){ | |
| out[n] = 1.0; | |
| } else { | |
| out[n] = 0.0; | |
| } | |
| } | |
| } else { | |
| if(t[i+k-1] == t[i]) { | |
| a_ij1 = rep_vector(0.0, rows(x)); | |
| } else { | |
| a_ij1 = (x - t[i]) ./ (t[i+k-1] - t[i]); | |
| } | |
| if(t[i+k] == t[i+1]) { | |
| a_i1j1 = rep_vector(0.0, rows(x)); | |
| } else { | |
| a_i1j1 = (t[i+k] - x) ./ (t[i+k] - t[i+1]); | |
| } | |
| out = a_ij1 .* B(x, t, i, k-1) + a_i1j1 .* B(x, t, i+1, k-1); | |
| } | |
| return(out); | |
| } | |
| // Create b-spline matrix | |
| // this is modified from the original to provide fixed minimum/maximum points, | |
| // as you will need if the vector x is a predicted value within the model. | |
| // The application I'm thinking of is when you have some probability of exit from | |
| // a panel and you want to control for this probabilty in the primary regression | |
| // model. Normally we'd use high-order polynomials, yet B-splines are normally | |
| // more stable. | |
| // if you don't fix the minimum and maximum points, it'll make the geometry weird for | |
| // HMC. So here we fix them to 0.0 and 1.0. | |
| matrix b_spline(vector x, vector interior_knots, int order) { | |
| real xmin = 0.0; | |
| real xmax = 1.0; | |
| vector[rows(interior_knots) + 2*order] t; | |
| matrix[rows(x), rows(interior_knots) + order] out; | |
| t= append_row(append_row(rep_vector(xmin, order), | |
| interior_knots), | |
| rep_vector(xmax, order)); | |
| for(p in 1:(rows(interior_knots) + order)) { | |
| out[1:rows(x),p] = B(x, t, p, order-1); | |
| } | |
| for(i in 1:rows(out)) { | |
| if(x[i]==xmax) { | |
| out[i,cols(out)] = 1; | |
| } | |
| } | |
| return(out[1:rows(out),2:cols(out)]); | |
| } | |
| } | |
| data { | |
| } | |
| transformed data { | |
| } | |
| parameters { | |
| } | |
| transformed parameters { | |
| } | |
| model { | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment