Helper functions and classes

In [45]:
from etrics.Utilities import Timing
with Timing("time to print", True):
    print("hello world")
time to print: hello world
0:00:00.000853

Quantile Regression

In [1]:
from etrics import quantreg
from etrics.quantreg import main, main_2
main()
In [4]:
main_2()
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                    PID   R-squared:                       0.437
Model:                            OLS   Adj. R-squared:                  0.434
Method:                 Least Squares   F-statistic:                     145.5
Date:                Wed, 10 Jun 2015   Prob (F-statistic):          2.70e-114
Time:                        17:39:22   Log-Likelihood:                -1843.3
No. Observations:                 944   AIC:                             3699.
Df Residuals:                     938   BIC:                             3728.
Df Model:                           5                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
logpopul      -0.0600      0.018     -3.395      0.001        -0.095    -0.025
selfLR         0.9772      0.039     24.773      0.000         0.900     1.055
age           -0.0033      0.003     -0.954      0.340        -0.010     0.003
educ           0.1608      0.038      4.212      0.000         0.086     0.236
income         0.0520      0.010      5.137      0.000         0.032     0.072
==============================================================================
Omnibus:                        1.440   Durbin-Watson:                   2.081
Prob(Omnibus):                  0.487   Jarque-Bera (JB):                1.460
Skew:                          -0.095   Prob(JB):                        0.482
Kurtosis:                       2.966   Cond. No.                         310.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
                         Quantile Regression Results                          
==============================================================================
Dep. Variable:                    PID   No. Observations:                  944
Model:                       quantreg   Df Residuals:                      938
Method:                Interior Point   Df Model:                            5
Date:                Wed, 10 Jun 2015   Tau:                             0.500
Time:                        17:39:22   Iterations:                         13
==============================================================================
                 coef    std err          z      P>|z|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
logpopul      -3.2918      2.120     -1.553      0.121        -7.447     0.864
selfLR        -0.0859      0.026     -3.360      0.001        -0.136    -0.036
age            1.1857      0.098     12.099      0.000         0.994     1.378
educ          -0.0034      0.014     -0.247      0.805        -0.030     0.023
income         0.1551      0.121      1.279      0.201        -0.083     0.393
==============================================================================

Simulation Class

In [1]:
import numpy as np
from scipy.stats.distributions import norm

def createData(theta, form, N):
    beta = theta["beta"]
    X = np.matrix([np.repeat(1, N), norm.rvs(loc=4, scale=2, size=N), norm.rvs(loc=5, scale=1, size=N)])
    eps = np.matrix(norm.rvs(loc=0, scale=1, size=N))
    return [X.T * np.matrix(beta).T + eps.T, X.T]

def estimateModel(data):
    import statsmodels.regression.linear_model as model
    res = model.OLS(data[0], data[1]).fit()
    return res.params
In [18]:
from etrics.Simulation import Simulation, Progress, onWarning, Results

x = Simulation("/tmp/statefile.tmp")
x.AddStatistics({"95th Quantile": lambda x: [np.percentile(x, 95, axis=0)]}, type=Results.Original)
x.SetIdentifiedParameters([1,2,3], ("beta{} "*3).format(1,2,3).split())
x.SetStructuralParameters({"beta":[1,2,3]}, None)
x.SetSamplingParameters(N=1000)
x.SetEstimationParameters()
x.Generating += createData
x.Estimating += estimateModel
x.PreEstimation += Progress
x.Warning += onWarning
x.SetWritingOptions("/tmp/table.tmp")
x.Simulate(100)

res1 = x.GetResults(Results.Original)
res2 = x.GetResults(Results.Bias)

print(res2)
Statefile found. Resume? [y]es, [n]o?n
0...
1/10...
1/5...
3/10...
2/5...
1/2...
3/5...
7/10...
4/5...
9/10...
[('Mean Bias', [0.040915290151131024, -0.0008824609885129497, -0.0072975617964554824]), ('Median Bias', [0.04415061248194252, -0.0011061016745995689, -0.0031729741088586394]), ('RMSE', [0.16686336858173237, 0.0172183611228727, 0.03189389954142763])]
In [17]:
from IPython.display import Latex
with open("/tmp/table.tmp", 'r') as t:
    print(t.read())
\begin{table}[h!]
	\centering
	\begin{tabular}{l|ccccc}
	\toprule
		
                      &  beta1 &  beta2 &  beta3 \\
	\midrule
		True Values           &  1.0000 & 2.0000 & 3.0000  \\
		Mean                  &  1.0164 & 1.9976 & 2.9984  \\
		Median                &  1.0123 & 1.9978 & 2.9981  \\
		Std.Dev.              &  0.1864 & 0.0167 & 0.0337  \\
		95th Quantile         &  1.3114 & 2.0260 & 3.0480  \\
		Mean Bias             &  0.0164 & -0.0024 & -0.0016  \\
		Median Bias           &  0.0123 & -0.0022 & -0.0019  \\
		RMSE                  &  0.1871 & 0.0168 & 0.0337  \\
	\bottomrule
	\end{tabular}
\end{table}
\iffalse
% Fct.Form             = None                                              
% B                    = 100                                               
% N                    = 1000                                              
% beta                 = [1, 2, 3]                                         
\fi