Friday, October 6, 2017

Estimating aggregate random coefficients logit models with Bayesian techniques

Today I gave a talk to the CS/Econ seminar at Harvard on my work with Shosh Vasserman on estimating aggregate RCL using Bayesian techniques. Was a really fun talk---computer science seminars have a very different feel to econ seminars!

It was my first experience using R's ioslides framework. That makes the slides very easy to share, but they also look quite good. I've intended for them to be readable.

Slides here: rpubs.com/jimsavage/ag_rcl_talk

Tuesday, September 26, 2017

Places I'm welcome


I use Google Analytics to monitor this blog, but don't check in much. Was playing yesterday and discovered you can split reader geography down to the city level. It's mapped above.

- I chose the right place (NYC) to live.
- If you can't do NYC, then the Acela corridor is the right place to be as a Bayesian economist.
- Twin cities, Chicago, Columbus OH and the research triangle round out the big cities in the East non-West. I have to say, not a small part of me thinks that throwing it all in and moving to one of those (cheap) places is a good thing to do.
- In the West, it's all SF and Seattle.
- No Nevada! No Wyoming!
- Los Angeles disappoints. 

Sunday, September 24, 2017

How does gender affect X?

An interesting question from Allen Riddell popped into my inbox yesterday. I thought the discussion would be interesting to some readers of the blog. 
I've got a case where I think an IV might be useful. Do you have a tutorial on this ready yet? (I recall you mentioning you were writing one.) Or perhaps there's a book/journal article you'd recommend?
I'll sketch out the case I'm working with in the event that it's not a case where IVs would be useful:
I'm trying to see if there's gender bias in the books (fiction) selected for review in a magazine. I've got a population of books and only some of them are selected for review.
The problem is that the genre of the book (e.g., thriller, romance) is correlated with gender AND the magazine is open about only reviewing certain kinds of books. So it's hard to say whether or not a bias against women-authored books isn't just a bias against genres in which they tend to predominate. 
I have a continuous variable which measures genre -- so right now it's a logistic regression with reviewed ~ gender + genre. Perhaps an IV would help?

My response:  
Hey Allen, Good to hear from you. I've also included Adam and Cory. Allen does analysis on text at Indiana U, and wrote pystan. Adam does causal inference thinking at Buzzfeed, and recently wrote a package on graphical causal inference. Cory is an economics PhD student at MIT and has very strong causality chops.
Just to be clear, you're looking for the causal path from an author's gender to their being reviewed, but you're saying that gender has an influence on genre (which will also influence review probability).
If there is a causal path from gender to genre here, then genre is a post-treatment control that will bias your effect estimate. That is, some of the causal effect of being female on review probability is being soaked up by their choice of genre. If you randomly vary their gender at birth, they _don't_ write the same book; they write a different genre which has a different probability of being reviewed (but this choice is caused by their gender). So if you're genuinely willing to estimate the causal effect of gender on being reviewed, you would drop the genre covariate.
But that's not the interesting question. If I read between the lines, your research question is more "if I randomly change the gender of a given book's author, how does it's probability of being reviewed vary". If so, you need to do more work than the current set-up.
I'm also not sure that an IV approach would work here, simply because a good instrument should result in exogenous variation in the treatment (gender). Unless your population of authors come from gender-selectively aborted populations in Chinese villages, or publishers with gender quotas, it'll be hard to argue for a good IV.
One approach would be to go from actual gender to perceptions of gender (which might be able to be exogenously varied by, say, names). I can't recall where, but I recall hearing about someone using initialization of first-middle names to help inform a perception of gender. Ambiguous names would also be. The idea being that initialized first-middle names (J.K. Rowling etc) are ambiguously gendered. The assumption here is that the reviewer "perceives" gender after the book has been written. 
[I go on to recommend a model, but that's not so interesting]

Next, Cory: 
Jim basically said what I was going to! Gender is a very weird case because it IS randomly assigned at birth (perhaps along with a few other X/Y chromosomal genes) so in some sense *all* regressions of something on gender are valid in reduced form!
We now run into two problems. First, since this was an RCT from conception, literally anything else that happens to the person is a pathway through which gender can matter. If you could be SURE that genre was the ONLY other factor that mattered you'd be fine with controlling for it, but that's a tough claim to make. The risk, as Jim says, is that people select into genres based on characteristics we don't observe (not gender). And then controlling for genre is just picking up the effects of those characteristics.
If you want to just account for genre to see genre-adjusted gaps, you should find an instrument for *genre* rather than gender.
Jim's idea of manipulating *perceived gender* is a good one & many studies have used this approach to good effect! The best approach is randomly changing the name on the manuscript to suggest a man vs a woman. Or making the process name blind. 
Then Adam: 

Hi All!
Hard problem! I think there's a problem with conditioning on genre to remove the effect of genre on acceptance, and studying the remainder of the total effect. It's even harder if you want to work with gender. I analyze sex first, then say how they'll be different.
First, the question, to make sure I have this right: It sounds like you're looking for "After accounting for acceptance differences due to genre, is there a sex bias in acceptance rates?", and a secondary question "Is there a sex difference in acceptance rates".
(1) IV isn't great, since, as the others mentioned, sex is randomly assigned already. It'd be hard to find an instrument for it! You'd need to identify an effectively randomly controlled cause of sex. Fortunately, you don't need to -- it's already randomly controlled! To be complete, the basic structure that lets you do IV looks something like (it turns out this picture is a little too strict -- the Z --> X arrow could also be from confounding between Z and X, i.e. Z <--> X, and that's perfectly fine!)

where Z is the instrument, X is the cause, Y is the effect, and U is a set of (possibly unobserved) confounding variables. We assume Z doesn't cause Y except possibly through X. More precisely, we assume (1) Z is not independent of Y, and (2) If we remove the causes of X, Z and Y become independent. Having made these assumptions, we can identify a conditional effect: the effect of X on Y for the subset of the population who is induced into the treatment by Z.
(2) If you want to control for genre to remove the intermediate effect S --> G --> A to focus on the total effect (minus that path), you have to make some additional assumptions. I believe the most general graph we can write, assuming RCT for sex, S, writing genre as G, confounders as U, and acceptance as A, looks like this


If we control for G, we break the causal path S --> G --> A, but introduce spurious correlation by conditioning on the collider at G along the path S --> G <--U --> A. To avoid this, you'd have to be willing to assume no confounding between genre and acceptance! That doesn't seem like something you could assume without a _really_ good reason. Note the edge from S --> U isn't required for this problem to happen.
Bottom line:It looks like the only identifiable effect from this graph is the effect of S on any measured variable.
Introducing gender, which is adopted by the individual at some point, removes randomized control for S (unless we assume gender is 100% biological). Then, you need additional confounding pointing to all variables, and no effect is identifiable.

Allen responds: 

This is all really helpful. Thanks!
Let me add a small wrinkle:
Could matching or some similar approach help somewhat? For example,books are also associated with publishers, and publishers tend to preferto offer book contracts to writers likely to be commercially successful(i.e., they care about different things than the reviewing magazine).Moreover, we have, typically, the full text of the books so we canestimate the "quality" of the book to an appreciable extent.
Re: sex vs. gender I think it's a safe simplifying assumption thatgender identity is randomly assigned. And we can restrict the analysisto authors who are writing under a name which is aligned with theirvisible gender. (Most books feature author photos.)
Also, I think I might be working with subtly different assumptions. Thisis the scenario I'm working with:
1. Magazine reviews books which are classified as genre 1 ("thriller")and not genre 2 ("romance"). (I know I said genre was a continuousvariable, but this is barely true; signs of genre are as conspicuous asthey get)2. Women and men write books in these genres at very different rates.The worry is that men and women are writing in these genres differsystematically.
So, in general, you can predict with very high probability if a book isthe kind of book which the magazine will *not* review under anycircumstances. Adam, does this address what you were saying aboutassuming "no confounding between genre and acceptance".
Then Adam: 
If you're satisfied that you've controlled enough covariates to control for all of the confounders of genre and acceptance, then they do help! If you're able to block the path which is opened by conditioning on G, S --> G <--U --> A, by conditioning on U as well, that's a perfectly legitmate thing to do. Then you've removed the spurious dependence (collider bias) for the remainder of the total effect of gender on acceptance (minus the path through genre).
Generally, a claim that you've accounted for all confounders is pretty dubious. After controlling, how can you be sure the remaining statistical dependence between genre and acceptance isn't due to yet more unobserved variables?  

Tuesday, September 12, 2017

Upcoming talks

For whatever reason, October is shaping up to be a big month for talks. If you'd to hear about things I've been thinking about lately (choice modeling, Bayesian structuralism etc.) then come along to one of these.

October 6, EconCS@Harvard (page not updated)

Shosh Vasserman and I will be talking through our Bayesian method of estimating aggregate random coefficients logit models. It's a bit like BLP, but with measurement error and Bayesian inference.

October 11, 8-9.30 pm, Columbia University. Details to follow.

I'll be speaking about how we're using structural models estimated using Bayesian techniques to price portfolios of consumer debt from Sub-Saharan Africa.

October 25, PAPIs, Microsoft Cambridge MA.

I'll be speaking about productizing structural models for industry.

October 30-31. DataEngConf, NYC

Same talk as above.




Sunday, August 6, 2017

Modeling as measurement

A fixture in online discussion of data science is the causal inference vs predictive analytics debate. Where should a bright young person be focusing their efforts? Here’s Francis Diebold. Here’s Yanir Seroussi. There are dozens more.
What is often missing from these debates is another important sub-field: modeling as measurement. I’ll get to that in a sec, but let me first describe how I think about how data analysis (as catholic a grouping as I can imagine) can add value.
A hierarchy of value add
Organizations distinguish themselves by strategy. We see firms of similar ages with similar-quality operations, with some firms being far more successful than others. Smart-phones, fast-food outlets etc. They can’t be totally inept at operations, but high-quality operations don’t seem to be necessary to make a mountain of money. If you’ve called a telecom recently, you know this to be true.
So how do various fields of data science map to the value-driving functions of an organization? A crass simplification, sure, but to my mind it looks like this:
Sub-fieldOperational/tactical/strategicDifficulty
Business intelligenceTactical/strategicEasy
Predictive modelingOperational/tacticalEasy-difficult
Experimental causal inferenceTacticalModerately difficult
Structural causal inferenceStrategicExtremely difficult
Modeling-as-measurmentStrategicModerately difficult
By “business intelligence” I mean “writing smart database queries/manual data entry and making pretty plots”. The barriers to entry in this field are pretty low—especially since the advent of R’s tidyverse and the Tableau BI platform—and the potential upside, letting senior decision-makers understand what’s going on, is enormous. Many intelligent folks snobbishly look down on this field, and they’re stupid to do so. People running companies and countries do not look down on BI.
I’ll skip over predictive modeling, which you know all about. By experimental causal inference I mean “A/B” testing, as it is known in industry, or randomized control trials in science. At first glance it mightn’t seem to be that difficult. Yet there are subtleties. Sometimes your treatment group doesn’t take the treatment, and control-group folks seek out the treatment. You get big selection-into-study effects, etc. So doing it properly is pretty tough. Causal inference does give us promise of being able to make decisions based on some notion of science, improving management decisions. Yet most applications are very much tactical (“which demographic groups should we target for this campaign?”, “what aid interventions actually affect schooling levels”). Of course, getting tactics right are important, but they’re a small part of a big picture. Singapore and South Korea didn’t get rich by running RCTs on every policy.
Structural modeling (I’ve called it “structural causal inference” above) is when you try to identify deep parameters of decision-makers’ optimization problems (like risk aversion or discount rate or preference structure). The notion is that if you’ve got a good model of how people and companies make decisions, then you can draw inference about how they might behave in situations that are quite dissimilar to history, and respond accordingly. That’s almost by definition what strategy is. Structural modeling field can plausibly add a lot of value. Yet you have to be a certified genius to understand it, and none of the many excellent structuralists I know would be able to sell any of their (excellent) ideas to a board of non-specialists. Time spent learning how to impress other structuralists seems to come at a 1:1 cost of time spent learning how to influence anyone else.
If you are smart, you prioritize. Spending cognitive resources on operational-level stuff just seems a waste. All the easy questions are (or will be) automated, and the hard questions—natural language and image processing—are akin to the difference between different models of Samsung cellphones, not the difference between Apple and Samsung. You want to get people who make important decisions to make good decisions.
One way to do this is by beefing up on good BI skills, but low barriers to entry decrease the wages and prestige in doing this. If you’re very bright, you might consider becoming a structuralist. The danger in doing that is in falling in love with the models rather than the world. Or you could learn modeling as measurement.
Modeling as measurement
Almost none of the data I work with maps cleanly to the things I care about. For context, the data I work with is mostly mobile money payments made to asset-backed lenders in East Africa. What I care about is whether their portfolio of loans is getting better or worse (we lend them money as a wholesale lender). This is actually a difficult problem.
  • The probability of a loan turning sour changes during the duration of a loan. It’s quite typical that borrowers make higher payments at the beginning of the loan, but the relationship is non-linear, and varies across individual borrowers (whose probabilities are almost certainly correlated).
  • The lenders who we lend to are growing very quickly. Each month they have more new, eager borrowers who make good payments (for the first few months at least).
  • Lenders often introduce new products, different loan terms, occasionally have product defects/recalls, expand into new geographies with less-experienced management etc.
The combination of all these factors makes it surprisingly difficult to work out whether a lender’s portfolio is improving or not. The typical BI answer would be to break the problem down into sub-problems, say, by analyzing different cohorts or different products or different geographies. Banks typically do this by coming up with Key Performance Indicators (KPIs) that they can quickly calculate for sub-groups. This is a helpful approach. Yet it leaves a lot on the table. To get the most from your data, you have to build a model.
In the “modeling as measurement” world, you’re not really doing prediction or causal inference—though you should be building models that can do both. You are building models of your observed variables as a function of the unobserved variables that you actually care about. If you build a good model of your observed data as a function of the unknowns, then it’s fairly straightforward to use the model to make predictions of the way things actually are, abstracting from the measurement problems in your data. That sort of information is profoundly useful to decision-makers.
A good example of this is a state-space model, often described using the application of the Kalman filter to space flight. Picture a spacecraft zooming to the moon. You want to work out its location in some space and its velocity, and tell it where to turn (if necessary). How do you work out where the spacecraft is? One way would be to simultaneously point two or more telescopes at the spacecraft at the same time, and use trigonometry. The problem with this is that it returns only an imprecise estimate of the location of the spacecraft. Can we do better?
The genius of the state-space approach is that we can combine sources of information. First, we have a sequence of noisy estimates of the location of the spacecraft. Second, we have the knowledge that, undisturbed, a space-craft zooming through space won’t be making any turns. We can combine these two sources of information (using the Kalman filter or more modern approaches) in a way that results in far less uncertainty about the (true, unobserved) location and velocity of the spacecraft. That’s profoundly useful—and this is before we even consider the potential use of state-space models for prediction or (typically structural) causal inference.
The thing that I find strange about this is that when I talk to investors and other decision-makers, they want to know how things are. They don’t trust forecasts—probably a good rule of thumb. And for executive decisions they’re definitely not interested in treatment effects. They want an unbiased description of reality. My bet is that building skills in descriptive modeling—modeling as measurement—is profoundly useful, and will be seen to be in the coming years.

Sunday, July 16, 2017

A few simple reparameterizations

It’s often convenient to make use of reparameterizations in our models. Often we want to be able to express parameters in a way that makes coming up with a prior more intuitive. Other times we want to use reparameterizations to improve sampling performance during estimation. In any case, the below are some very simple reparameterizations that we often use in applied modeling.
Reparametrizaing a univariate normal random variable
Sometimes we have a normally distributed unknown  with mean (location)  and standard deviation (scale) . We write this as

If we have information about the expected mean and standard deviation for data at the level of the observation, , we could happily incorporate this information like so:

For instance, a normal linear model has , but we could use a variety of functional forms for both.
In such a case, we can always reparameterize  as
In Stan, we’d implement this by declaring  in the parameter block, and  in the transformed parameter block. We then provide distributional information about  in the modeling block but typically use  in the likelihood. For example:


// ... Your data declaration here
parameters {
  vector[N] z;
  // parameters of f() and g()
}
transformed parameters {
  vector[N] theta;
  theta = f(X) + g(X)*z; // f() and g() are vector valued functions that probably have parameters
}
model {
  // priors
  z ~ normal(0, 1);
  
  
  // likelihood
  // ..
}
Reparameterizing a covariance matrix
I always found covariance matrices tricky to think about until I realized how easily they can be reparameterized. We all know how to think about the standard deviation of a random variable. If the growth rate of GDP has a standard deviation of 1%, then that’s very intuitive. How about a vector of random variables? If the vector is (GDP, Unemployment, Rainfall) then each of those random variables has its own standard deviation. Let’s call it . Easy!
Now those random variables might move together. We typically measure (linear) mutual information of a vector of random variables using a correlation matrix. The diagonal of a correlation matrix is 1 (all variables are perfectly correlated with themselves). and the off-diagonals are between -1 and 1, reflecting the correlation coefficient between each random variable. It it symmetric. For instance, the element  is the correlation between GDP and rainfall.
We have everything we need now. The covariance matrix  is simply:
The cool thing about this parameterization is that often our likelihood calls for a covariance matrix (for instance, if we were jointly modeling the three random variables), but we find it easier to provide prior information about the (marginal) scale and correlation between the variables.

We’d implement this in Stan by declaring  and  as parameters, then  as a transformed parameter. We then provide priors for  and , but use  in the likelihood. We often use the LKJ distribution as a prior for correlation matrices.
parameters {
  vector<lower = 0>[3] tau;
  corr_matrix[3] Omega;
}
transformed parameters {
  matrix[3, 3] Sigma;
  Sigma = diag_matrix(tau)*Omega*diag_matrix(tau);
}
model {
  // priors
  tau ~ student_t(3, 0, 2);
  Omega ~ lkj_corr(4);
  
  // likelihood
  // expression involving Sigma
}
Reparameterizing multivariate normals
You’ll notice in the reparameterization  for some  that  is the square root of the variance of . Multivariate normal distributions are typically parameterized in terms of their variance covariance matrix, which is the analog to the variance of a univariate normal. But if we want to apply our intuition from the above reparameterization, we need the “square root” of this covariance matrix.
There are many such “square roots” of positive definite matrices; one is the Cholesky factorization
where L is a lower triangular matrix with the same dimensions as . If we have such an , we can very easily apply the reparameterization at the top. For some vector


then we can also say that

Another convenient take on this is to use the fact that if

where  is the Cholesky factor of the correlation matrix, then we can use the parameterization

This parameterization requires less fiddling than the one above. Stan also gives us an LKJ prior distribution for the Cholesky factors of correlation matrix
In Stan, we implement this by declaring   and  as parameters, and  as a transformed parameter.
parameters {
  vector[K] mu;
  vector<lower = 0>[K] tau;
  vector[N] z[K];
  cholesky_factor_corr[K] L_Omega;
}
transformed parameters {
  matrix[K, K] L;
  vector[N] Theta[K];
  L = diag_pre_multiply(tau, L_Omega);

  for(n in 1:N) {
    Theta[n] = mu + L * z[n];
  }
}
model {
  mu ~ normal(0, 1);
  tau ~ student_t(3, 0, 2);
  for(n in 1:N) {
    z[n] ~ normal(0, 1);
  }
  L_Omega ~ lkj_corr_cholesky(4);
  
  // likelihood below, depending on Theta
}
There are of course many other reparameterizations that we use in buiding models, but I tend to use these three daily.