Compustat Data: Capital Gains Calculations

In [1]:
#display options
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

Import stuff

In [2]:
import numpy as np
import pandas as pd
import time

%matplotlib inline
import matplotlib as mpl
mpl.rc("savefig",dpi=100)
from matplotlib import pyplot as plt

#pandas option
pd.options.display.max_columns = 50
idx = pd.IndexSlice
# #define winsorizing filter
# #winsorize the top and bottom 1% values of the target variable
# def wins_one(df, var):
#     return df.groupby(level = 1).apply(lambda x: pd.Series(winsorize(x[var], limits = [0.01, 0.01]), index = x['gvkey'])).reorder_levels(order = ['gvkey', 'fyear'])
    
def wins_one(df_var, p = 0.01):
    return df_var.groupby(level = 1).apply(lambda x: x.clip(lower = x.quantile(p), upper = x.quantile(1. - p)))

percentile = [0.1, 0.25, 0.50, 0.75, 0.9][::-1]
scf_years=[1989,1992,1995,1998,2001,2004,2007,2010,2013,2016]
all_years=range(1980,2017)

Load compustat data

Load compustat data from WRDS. We use annual data from 1980-2016. The following variables (named as per WRDS conventions) are used in our analysis.

  • GVKEY -- Global Company Key
  • FYEAR -- Data Year - Fiscal
  • FIC -- Foreign Incorporation Code
  • CSHO -- Common Shares Outstanding
  • PRCC_F -- Price Close - Annual - Fiscal
  • PI -- Pretax Income
  • DVT -- Dividends - Total
In [3]:
df = pd.read_stata('./compustat_mergenfo_plus_sale_at.dta')
df['fyear'] = df['fyear'].astype(int)
In [4]:
#set index after sorting
df = df.drop_duplicates(subset = ['gvkey', 'fyear']) 
df = df.sort_values(['gvkey', 'fyear'])
df = df.set_index(['gvkey', 'fyear'], verify_integrity = True, drop = False)
df = df[df['fic'] == 'USA']

Measurement Concepts

We now construct some user-defined variables:

  • mkvalt: market value at the close of fiscal year, MARKET VALUE = CSHO x PRCC_F
  • wgt_mkvalt: weights proportional to mkvalt
  • pi_mval_ratio: Net income yield, PI/mkvalt
  • dvt_mval_ration: dividend yield, DVT/mkvalt
  • capital_gain: capital gain, mkvakt/mkvalt(-1)-1
  • capital_gain_3: 3 year averaged capital gain,(mkvakt/mkvalt(-3))^(1/3) -1
  • X_wins: winsorised version of X
In [5]:
df['mkvalt'] = df['csho'] * df['prcc_f']
df = df[pd.notnull(df['pi']) & pd.notnull(df['dvt']) & (pd.notnull(df['mkvalt']))]
df['wgt_mval'] = df['mkvalt'] / df['mkvalt'].groupby(level = 1).sum()
df['pi_mval_ratio'] = (df['pi']/df['mkvalt']).replace([np.inf, -np.inf], np.nan)
df['dvt_mval_ratio'] = (df['dvt']/df['mkvalt']).replace([np.inf, -np.inf], np.nan)
df['capital_gain']= (df['mkvalt']/df['mkvalt'].shift()-1).replace([np.inf, -np.inf], np.nan)
df['capital_gain_3']= ((df['mkvalt']/df['mkvalt'].shift(3))**(1.0/3)-1).replace([np.inf, -np.inf], np.nan)

Means of capital gains using various concepts

These moments are computed after winsorising and for all years. To change to scf_years replace the variable "all_years" by "scf_years"

  • VW_CG_surv_compustat: value weighted capital gains from t-->t+1 with wgts based on firms that survived in t+1
  • VW_CG_surv_compustat_3yr: : value weighted capital gains from t-->t+3 with wgts based on firms that survived in t+3
  • VW_CG_index_compustat: sum of mkvalt/sum of mkvalt(-1)
  • VW_CG_index_compustat_3yrs: (sum of mkvalt/sum of mkvalt (-3))^(1/3)-1 For details refer to the Appendix

select the years to report

In [6]:
sel=df['fyear'].isin(all_years)
In [7]:
df['capital_gain_wins'] = wins_one(df['capital_gain'])
df['capital_gain_3_wins'] = wins_one(df['capital_gain_3'])
VW_CG_surv_compustat=(df[sel]['capital_gain_wins']*df[sel]['wgt_mval'].shift(1)).groupby(level = 1).sum()
VW_CG_surv_compustat_3yr=(df[sel]['capital_gain_3_wins']*df[sel]['wgt_mval'].shift(3)).groupby(level = 1).sum()
mkvalt_sum = df[sel]['mkvalt'].groupby(level = 1).sum()
VW_CG_index_compustat=mkvalt_sum/mkvalt_sum.shift()-1
L=3
VW_CG_index_compustat_3yrs=(mkvalt_sum/mkvalt_sum.shift(L))**(1.0/L)-1
compstat=pd.DataFrame([VW_CG_surv_compustat,VW_CG_surv_compustat_3yr,VW_CG_index_compustat,VW_CG_index_compustat_3yrs]).T
compstat.columns=['surving','surving3','index','index3']
compstat.to_csv('compustat.csv')
compstat
Out[7]:
surving surving3 index index3
fyear
1980 0.201026 0.016687 NaN NaN
1981 -0.117559 -0.037731 -0.055696 NaN
1982 0.119180 -0.067040 0.145004 NaN
1983 1.258997 0.012873 0.256829 0.107639
1984 -0.052823 0.019403 -0.018657 0.121936
1985 0.197097 0.070618 0.217408 0.145103
1986 0.126056 -0.037941 0.161022 0.115234
1987 -0.019971 -0.007180 0.004324 0.123872
1988 0.063156 -0.015163 0.071368 0.077005
1989 0.209101 0.023836 0.196045 0.087727
1990 -0.092515 -0.004396 -0.075854 0.057976
1991 0.269045 0.052629 0.312036 0.131909
1992 0.058367 -0.001017 0.108700 0.103655
1993 0.067950 0.022676 0.159978 0.190519
1994 -0.032355 -0.059787 -0.006786 0.085015
1995 0.325581 0.030084 0.355954 0.160324
1996 0.184467 0.074354 0.224005 0.181291
1997 0.302002 0.178812 0.313736 0.296718
1998 0.213988 0.124109 0.190839 0.241790
1999 0.214030 0.096091 0.246803 0.249452
2000 0.036908 0.030956 -0.006449 0.138362
2001 -0.148298 -0.033292 -0.148636 0.017891
2002 -0.208969 -0.163901 -0.207271 -0.124727
2003 0.286450 -0.091032 0.279848 -0.047644
2004 0.106685 -0.004724 0.120330 0.043622
2005 0.046076 0.089825 0.033856 0.140219
2006 0.102749 0.029528 0.113531 0.088519
2007 0.013328 -0.012469 0.002430 0.048911
2008 -0.369324 -0.168603 -0.367815 -0.109706
2009 0.215850 -0.108366 0.203085 -0.086452
2010 0.167532 -0.064807 0.166320 -0.039156
2011 -0.014458 0.075961 -0.010481 0.115611
2012 0.120368 0.045718 0.141806 0.096339
2013 0.267262 0.075199 0.280981 0.131149
2014 0.095206 0.108785 0.101213 0.172201
2015 -0.029826 0.056298 -0.037428 0.107344
2016 0.091775 0.019871 0.080155 0.046157

Other moments for capital gains for all firms

In [8]:
num=(df[sel]['capital_gain_wins']).groupby(level = 1).count()
CG_mom=pd.DataFrame([num])
CG_mom=CG_mom.T
CG_mom.columns=['num']
CG_mval=[df[sel]['capital_gain'].groupby(level = 1).quantile(p) for p in percentile]
per_head=['p90','p75','p50','p25','p10']
for p in range(len(percentile)):
    CG_mom[per_head[p]]=CG_mval[p]

    
CG_mom.to_csv('compustat_CG_all_firms.csv')
CG_mom
Out[8]:
num p90 p75 p50 p25 p10
fyear
1980 4159 1.399006 0.638565 0.184745 -0.066847 -0.290707
1981 4480 0.859664 0.290975 -0.014216 -0.307043 -0.622612
1982 4505 1.218450 0.509434 0.111111 -0.257640 -0.574660
1983 4927 2.080056 0.846755 0.305023 -0.018690 -0.425613
1984 4994 0.556796 0.178408 -0.095226 -0.386737 -0.645885
1985 4982 1.175848 0.518369 0.161033 -0.169251 -0.500000
1986 5225 1.375789 0.464905 0.084362 -0.249739 -0.566415
1987 5452 0.901433 0.224064 -0.108768 -0.399392 -0.662398
1988 5269 0.870816 0.343674 0.041667 -0.249152 -0.570555
1989 5113 0.991922 0.394940 0.051293 -0.254292 -0.548860
1990 5057 0.546960 0.065479 -0.206343 -0.488027 -0.715821
1991 5162 2.029327 0.740114 0.208112 -0.152681 -0.504000
1992 5320 1.500217 0.505496 0.097508 -0.205806 -0.509354
1993 6416 1.978793 0.615255 0.107911 -0.246167 -0.691693
1994 6789 0.834013 0.250000 -0.035614 -0.287981 -0.592660
1995 6930 1.731871 0.639227 0.212708 -0.116471 -0.459163
1996 7344 1.656342 0.553708 0.138444 -0.171566 -0.532171
1997 7291 1.459457 0.658830 0.216586 -0.166477 -0.518750
1998 6894 1.007099 0.268587 -0.101238 -0.407841 -0.659040
1999 6670 2.934931 0.618599 -0.032909 -0.302082 -0.539096
2000 6396 1.377031 0.354948 -0.098517 -0.489378 -0.798228
2001 5741 1.135434 0.448889 0.056689 -0.297196 -0.623256
2002 5349 0.552845 0.181011 -0.096827 -0.436384 -0.702515
2003 5057 2.382510 1.021467 0.430037 0.148803 -0.096383
2004 5006 1.111214 0.491817 0.167656 -0.082620 -0.351665
2005 4906 0.942879 0.347149 0.041827 -0.164961 -0.407133
2006 4813 0.940368 0.392200 0.111012 -0.096141 -0.330552
2007 4692 0.810751 0.227802 -0.097246 -0.336365 -0.548069
2008 4437 0.087926 -0.186773 -0.439286 -0.667055 -0.824427
2009 4210 1.838172 0.808637 0.243734 -0.091124 -0.376584
2010 4082 1.179805 0.531448 0.193879 -0.037278 -0.287921
2011 3966 0.515793 0.178693 -0.051606 -0.278454 -0.535408
2012 3897 0.849942 0.372251 0.120608 -0.091790 -0.351158
2013 3963 1.461225 0.680000 0.316262 0.067608 -0.180885
2014 4106 0.770818 0.291596 0.056233 -0.161000 -0.454371
2015 4062 0.559773 0.168641 -0.058226 -0.304144 -0.562129
2016 3934 0.805856 0.412618 0.149227 -0.098176 -0.405841
In [ ]: