Repository Public Research preview
1// Time-dimension placebo and simulation-based power for the interaction regression2// y_it = a_i + tau_t + gamma * (u_t x F_i) + sum_k theta_k (X_kt x F_i) + e_it .3// Each replication replaces the climate shock series u_t by a moving-block permutation of itself4// (block length B trading days, preserving its marginal distribution and short-run dependence but5// breaking the link with calendar time), re-estimates gamma and its currency- and date-clustered6// standard errors, and writes (b, se_unit, se_time). Because OLS is linear in y, the estimate under7// an injected effect d equals b + d with unchanged standard errors, so power curves follow from the8// same draws. Usage: placebo <input.txt> <reps> <block> <seed> <out.csv> (OpenMP parallel)9#include <algorithm>10#include <cmath>11#include <cstdio>12#include <cstdlib>13#include <fstream>14#include <iostream>15#include <numeric>16#include <random>17#include <vector>18#include <omp.h>19using namespace std;20typedef vector<double> vec;2122struct Panel { int N, T, n, k; vector<int> ui, ti; vec y, F; vector<vec> C; vec u; vector<int> cnt_i, cnt_t; };2324static void demean2(vec &x, const Panel &p) {25 vec mi(p.N), mt(p.T);26 for (int it = 0; it < 200; ++it) {27 double delta = 0;28 fill(mi.begin(), mi.end(), 0.0);29 for (int r = 0; r < p.n; ++r) mi[p.ui[r]] += x[r];30 for (int i = 0; i < p.N; ++i) { mi[i] /= p.cnt_i[i]; delta = max(delta, fabs(mi[i])); }31 for (int r = 0; r < p.n; ++r) x[r] -= mi[p.ui[r]];32 fill(mt.begin(), mt.end(), 0.0);33 for (int r = 0; r < p.n; ++r) mt[p.ti[r]] += x[r];34 for (int t = 0; t < p.T; ++t) { if (p.cnt_t[t]) mt[t] /= p.cnt_t[t]; delta = max(delta, fabs(mt[t])); }35 for (int r = 0; r < p.n; ++r) x[r] -= mt[p.ti[r]];36 if (delta < 1e-11) break;37 }38}3940// solve small SPD system A z = b (Gaussian elimination with pivoting), A is k x k row-major41static vec solve(vector<vec> A, vec b) {42 int k = (int)b.size();43 for (int c = 0; c < k; ++c) {44 int piv = c;45 for (int r = c + 1; r < k; ++r) if (fabs(A[r][c]) > fabs(A[piv][c])) piv = r;46 swap(A[c], A[piv]); swap(b[c], b[piv]);47 for (int r = c + 1; r < k; ++r) {48 double f = A[r][c] / A[c][c];49 for (int j = c; j < k; ++j) A[r][j] -= f * A[c][j];50 b[r] -= f * b[c];51 }52 }53 vec z(k);54 for (int c = k - 1; c >= 0; --c) { double s = b[c]; for (int j = c + 1; j < k; ++j) s -= A[c][j] * z[j]; z[c] = s / A[c][c]; }55 return z;56}5758// residual of x on the (already demeaned) control columns59static void partial_out(vec &x, const vector<vec> &C, const vector<vec> &CtC) {60 int k = (int)C.size(), n = (int)x.size();61 if (!k) return;62 vec b(k, 0.0);63 for (int j = 0; j < k; ++j) { double s = 0; for (int r = 0; r < n; ++r) s += C[j][r] * x[r]; b[j] = s; }64 vec z = solve(CtC, b);65 for (int r = 0; r < n; ++r) { double f = 0; for (int j = 0; j < k; ++j) f += z[j] * C[j][r]; x[r] -= f; }66}6768int main(int argc, char **argv) {69 if (argc < 6) { cerr << "usage: placebo input reps block seed out\n"; return 1; }70 int reps = atoi(argv[2]), B = atoi(argv[3]); unsigned seed = (unsigned)atol(argv[4]);71 Panel p; ifstream in(argv[1]);72 in >> p.N >> p.T >> p.n >> p.k;73 p.u.resize(p.T); for (int t = 0; t < p.T; ++t) in >> p.u[t];74 p.ui.resize(p.n); p.ti.resize(p.n); p.y.resize(p.n); p.F.resize(p.n); p.C.assign(p.k, vec(p.n));75 for (int r = 0; r < p.n; ++r) { in >> p.ui[r] >> p.ti[r] >> p.y[r] >> p.F[r]; for (int j = 0; j < p.k; ++j) in >> p.C[j][r]; }76 p.cnt_i.assign(p.N, 0); p.cnt_t.assign(p.T, 0);77 for (int r = 0; r < p.n; ++r) { p.cnt_i[p.ui[r]]++; p.cnt_t[p.ti[r]]++; }78 // fixed parts: demean y and controls, partial controls out of y79 demean2(p.y, p);80 for (auto &c : p.C) demean2(c, p);81 vector<vec> CtC(p.k, vec(p.k));82 for (int a = 0; a < p.k; ++a) for (int b = 0; b < p.k; ++b) { double s = 0; for (int r = 0; r < p.n; ++r) s += p.C[a][r] * p.C[b][r]; CtC[a][b] = s; }83 vec ey = p.y; partial_out(ey, p.C, CtC);84 vector<double> ob(reps + 1), osu(reps + 1), ost(reps + 1);85 #pragma omp parallel86 {87 vec x(p.n), u(p.T), si(p.N), st(p.T);88 #pragma omp for schedule(dynamic, 8)89 for (int rep = 0; rep <= reps; ++rep) {90 if (rep == 0) u = p.u; // rep 0 = actual shock series91 else {92 mt19937 g(seed * 1000003u + (unsigned)rep);93 int nb = (p.T + B - 1) / B; vector<int> ord(nb); iota(ord.begin(), ord.end(), 0); shuffle(ord.begin(), ord.end(), g);94 int off = (int)(g() % (unsigned)B), w = 0; // random block phase95 vec rot(p.T); for (int t = 0; t < p.T; ++t) rot[t] = p.u[(t + off) % p.T];96 for (int bb = 0; bb < nb && w < p.T; ++bb) for (int t = ord[bb] * B; t < min(p.T, (ord[bb] + 1) * B) && w < p.T; ++t) u[w++] = rot[t];97 }98 for (int r = 0; r < p.n; ++r) x[r] = u[p.ti[r]] * p.F[r];99 demean2(x, p); partial_out(x, p.C, CtC);100 double sxx = 0, sxy = 0; for (int r = 0; r < p.n; ++r) { sxx += x[r] * x[r]; sxy += x[r] * ey[r]; }101 double b = sxy / sxx;102 fill(si.begin(), si.end(), 0.0); fill(st.begin(), st.end(), 0.0);103 for (int r = 0; r < p.n; ++r) { double s = x[r] * (ey[r] - b * x[r]); si[p.ui[r]] += s; st[p.ti[r]] += s; }104 double vi = 0, vt = 0; for (double s : si) vi += s * s; for (double s : st) vt += s * s;105 ob[rep] = b; osu[rep] = sqrt(vi * p.N / (p.N - 1.0)) / sxx; ost[rep] = sqrt(vt * p.T / (p.T - 1.0)) / sxx;106 }107 }108 FILE *f = fopen(argv[5], "w"); fprintf(f, "rep,b,se_unit,se_time\n");109 for (int rep = 0; rep <= reps; ++rep) fprintf(f, "%d,%.10g,%.10g,%.10g\n", rep, ob[rep], osu[rep], ost[rep]);110 fclose(f); return 0;111}112