*! version 1.1 1April2020
*! version 1.2 9September2020 - Simplifies syntax and changes how the estimation is done
*! version 1.3 11September2020 - Fix some issues regarding collinearity
*! version 1.4 22September2020 - Fix issue with the syntax in the selection eq.
*! version 1.5 3March2021 - Added progress' dots, simplify the code and speed it up
* Authors: Ercio Munoz & Mariel Siravegna

/* Wrap file to implement copula-based sample selection model in quantile
regression as suggested by Arellano and Bonhomme (2017). */

* qregsel wage educ age, quantile(.1 .5 .9) select(married children educ age)

program define qregsel, eclass sortpreserve
    version 15.0

    syntax varlist(numeric) [if] [in], SELect(string) quantile(string) ///
	[ copula(string) NOCONStant finergrid coarsergrid rescale nodots ]

    gettoken depvar indepvars : varlist
    _fv_check_depvar `depvar'

    fvexpand `indepvars'
    local cnames `r(varlist)'

	tokenize `select', parse("=")
	if "`2'" != "=" {
		local x_s `select'
		tempvar y_s
		qui gen byte `y_s' = (`depvar'!=.) `if' `in'
	}
	else {
		local y_s `1'
		local x_s `3'
	}
	capture unab x_s : `x_s'

	if ("`noconstant'"!="") local _constant , noconstant

********************************************************************************
** Marking the samples to use
********************************************************************************
	tempvar touse_all touse
	mark `touse_all' `if' `in'
	markout `touse_all' `indepvars' `x_s'

	qui gen byte `touse' = `touse_all'
	markout `touse' `depvar' `indepvars'
	qui replace `touse' = 0 if `y_s'==1 & `touse'!=1

	qui count if `touse_all'
	local all = r(N)
	qui count if `touse'
	local N_selected = r(N)


********************************************************************************
** Checking errors in the selection indicator
********************************************************************************
	qui tab `y_s'
	if r(r) != 2 {
	dis as error "Dependent variable never censored because of selection or selection indicator is not binary."
	exit 198
	}
	qui sum `y_s'
	if r(max) != 1 & r(min) != 0 {
	dis as error "Dependent variable never censored because of selection or selection indicator is not binary."
	exit 198
	}


********************************************************************************
** Checking collinearity
********************************************************************************
	qui _rmdcoll `depvar' `indepvars' if `touse' `_constant'
	local result "`r(varlist)'"
	local coll_x: list indepvars - result
	if ~missing("`coll_x'") {
		noisily display as text "note: `coll_x' omitted from outcome equation because of collinearity"
		local `indepvars' `result'
	}
	cap qui _rmdcoll `y_s' `x_s'
	if (_rc!=0) display as error "Check the syntax in the selection equation"
	local result "`r(varlist)'"
	local coll_x: list x_s - result
	if ~missing("`coll_x'") {
		noisily display as text "note: `coll_x' omitted from selection equation because of colliearity"
		local x_s `result'
	}


********************************************************************************
** Checking errors with copula specification (default is gaussian)
********************************************************************************
	if "`copula'" == "" {
	local copula "gaussian"
	}
	if  "`copula'" != "gaussian" & "`copula'" != "frank"	  {
		dis as error "`copula' is not an available copula."
		exit 198
	}


********************************************************************************
** Checking errors in the quantiles requested
********************************************************************************
	tokenize `quantile'
	local orig `1'
	macro shift
if "`orig'" == "" {
	di in red "option quantile() required"
	exit 198
}
	capture confirm number `orig'
if _rc {
	di "`orig' not a number"
	exit 198
}
if `orig' >= 1 {
	local orig = `orig'/100
}
if `orig'<=0 | `orig' >=1 {
	local orig = 100*`orig'
	di "`orig' out of range"
	exit 198
}
	local quants = `orig'

while "`1'" != "" {
	local orig `1'
	macro shift
if "`orig'" == "" {
	di in red "option quantile() required"
	exit 198
}
	capture confirm number `orig'
if _rc {
	di "`orig' not a number"
	exit 198
}
if `orig' >= 1 {
	local orig = `orig'/100
}
if  `orig' >=1 {
	local orig = 100*`orig'
	di "`orig' out of range"
	exit 198
}
if `orig'<=0 {
	di "`orig' out of range"
	exit 198
}
	local quants = "`quants' `orig'"
}


********************************************************************************
** Generate the propensity score and the instrument
********************************************************************************
	tempname pZ grid object y X R M N k index copula_p b betas_taus coefs value gridsize obj
	qui: probit `y_s' `x_s' if `touse_all'
	qui: predict `pZ' if `touse_all'


********************************************************************************
** Estimate rho looping over quantiles and the grid for rho
********************************************************************************
if "`finergrid'" == "finergrid" & "`coarsergrid'" != "coarsergrid" {
	if ("`copula'" == "frank") mata: `grid' = finergrid("frank")
	if ("`copula'" == "gaussian") mata: `grid' = finergrid("gaussian")
}
else if "`coarsergrid'" == "coarsergrid" {
	if ("`copula'" == "frank") mata: `grid' = coarsergrid("frank")
	if ("`copula'" == "gaussian") mata: `grid' = coarsergrid("gaussian")
}
else {
	if ("`copula'" == "frank") mata: `grid' = regulargrid("frank")
	if ("`copula'" == "gaussian") mata: `grid' = regulargrid("gaussian")
}
	mata: st_matrix("`object'",`grid')


********************************************************************************
* Send data to mata
********************************************************************************
	mata: `y' = st_data(., "`depvar'", "`touse'")
	mata: `X' = st_data(., "`cnames'", "`touse'")

if ("`rescale'" != "") {
	mata:	`R'    = meanvariance(`X')
	mata:	`X' = (`X':-`R'[1,.]):/sqrt(diagonal(`R'[2::rows(`R'),]))'
}

	mata: `M'    = rows(`X')
	mata: `N'    = cols(`X')

if ("`noconstant'"=="") mata: `X' = `X',J(`M',1,1)
	mata: `k'    = cols(`X')
	mata: st_numscalar("`k'",`k')

	mata: `pZ' = st_data(.,"`pZ'", "`touse'")


********************************************************************************
* Grid search
********************************************************************************
	mata: st_numscalar("`gridsize'",rows(`grid'))
	mat `value' = J(`gridsize',1,.)
	local gridsize = `gridsize'
forvalues g = 1(1)`gridsize' {
	mata: `obj' = maxrho(`pZ',`y',`X',`M',`N',`grid',"`copula'",`g')
	mata: st_numscalar("`obj'",`obj')
	mat `value'[`g',1] = `obj'

if "`nodots'"=="" {
    if `g'==1 {
	display "Grid for the copula parameter (`gridsize')"
	display "----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5"
	}
	if !inlist(`g',50,100,150,200)  di "." _cont
	if inlist(`g',50,100,150,200) di "." _cont _newline
}

}
	mata: `value' = st_matrix("`value'")
	mata: `index' = select((1::rows(`value')), (`value' :== min(`value'')))
	mata: st_numscalar("`index'",`index')


********************************************************************************
** Estimate rotated quantile regression using the selected rho
********************************************************************************
	local rho      = `object'[`index',1]
	local spearman = `object'[`index',2]
	local kendall  = `object'[`index',3]
	mat `object' = `object',`value'
	mat colnames `object' = rho spearman kendall value

	local count: word count `indepvars'
if ("`noconstant'" == "") mat `coefs' = J(`count'+1,1,.)
else if ("`noconstant'" != "") mat `coefs' = J(`count',1,.)

foreach tau of local quants {

	mata: `copula_p' = copulafn(`pZ',`rho',`tau',"`copula'")
	mata: `b'        = mywork(`y',`X',`pZ',`rho',`tau',`copula_p',`M',`N')

	local qtau = round(100*`tau')
   if ("`noconstant'" == "") local cnames2 `cnames' _cons
   else if ("`noconstant'" != "") local cnames2 `cnames'
	local names = ""
	foreach lname2 of local cnames2   {
		local names = "`names' q`qtau':`lname2'"
	}
	mata st_matrix("`b'", `b')
	mat rownames `b' = `names'
	mat `betas_taus'  = ( nullmat(`betas_taus') \ `b')
	mat colnames `b' = "q`qtau'"
	mat `coefs' = `coefs',`b'
}

	mat `coefs' = `coefs'[1...,2...]
	if ("`noconstant'" == "") local cnames `cnames' _cons
	matrix rownames `coefs' = `cnames'


********************************************************************************
** Generating the output
********************************************************************************
	if ("`rescale'"!="") local rescale = "rescaled"
	else if ("`rescale'"=="") local rescale = "non-rescaled"

	dis " "
	dis in green "Quantile selection model" ///
		_column (50) "Number of obs" _column(69) "=" _column(71) %8.0f in yellow `all'
	dis in green	_column (50) "Selected"      _column(69) "=" _column(71) %8.0f in yellow `N_selected'
	dis in green	_column (50) "Nonselected"   _column(69) "=" _column(71) %8.0f in yellow `all'-`N_selected'

	dis in green "Copula parameter (`copula'): " %8.2f in yellow `rho'

	tempname b
	matrix `b' = `betas_taus''
    ereturn post `b', esample(`touse_all') buildfvinfo
	ereturn matrix coefs      = `coefs'
	ereturn matrix grid       = `object'
	ereturn scalar N          = `all'
	ereturn scalar N_selected = `N_selected'
    ereturn scalar rho        = `rho'
	ereturn scalar kendall    = `kendall'
	ereturn scalar spearman   = `spearman'
	ereturn local title   "Quantile selection model"
	ereturn local rescale "`rescale'"
	ereturn local predict "qregsel_p"
    ereturn local cmd     "qregsel"
	ereturn local select_eq "`select'"
	ereturn local outcome_eq "`depvar' `indepvars'"
	ereturn local cmdline "qregsel `depvar' `indepvars', select(`select')"
	ereturn local indepvars "`cnames'"
	ereturn local depvar  "`depvar'"
	ereturn local copula "`copula'"

    ereturn display

end

********************************************************************************
** Auxiliary functions needed for the estimation
********************************************************************************
mata:

real scalar maxrho(real matrix pZ1,
					real matrix y,
					real matrix X,
					real scalar M,
					real scalar N,
					real matrix grid,
					string scalar copula,
					real scalar j)
{

real vector copula_p, b
real scalar rhoa, obj, obj2, gridsize

gridsize = rows(grid)
rhoa = grid[j,1]

obj = 0
for (i=1; i<=9; i++) {
copula_p  = copulafn(pZ1,rhoa,i/10,copula)
b         = mywork(y,X,pZ1,rhoa,i/10,copula_p,M,N)
obj2      = objective(pZ1,y,X,b,copula_p)
obj = obj + obj2
}
obj = obj*obj
return(obj)
}

real matrix bound(numeric matrix x, numeric matrix dx)
{

	real vector b, f

	b = 1e20 :+ 0* x
	f = selectindex(dx:<0)
	b[f] = -x[f] :/ dx[f]

	return(b)
}

real vector copulafn(real vector pZ1,
						numeric vector rho,
						numeric vector tau,
						string scalar name)
{
	real vector G,vs,v1

	if (name=="gaussian") {
	vs = J(rows(pZ1),1,invnormal(tau))
	v1 = invnormal(pZ1)
	G  = binormal(vs,v1,rho) :/ pZ1
	}
	else {
	G = -ln(1:+(exp(-rho*tau):-1):*(exp(-rho:*pZ1):-1):/(exp(-rho)-1)):/(rho:*pZ1)
	}

	return(G)
}

real vector mywork(real vector y,
					real matrix X,
					real vector pZ1,
					numeric vector rho,
					real vector tau,
					real matrix p,
					real scalar M,
					real scalar N)
{
    real vector yy, bb
    real matrix u, a, b, A, x, G
    real scalar m, n, k, it, beta, small, max_it

	k    = cols(X)

	u    = J(M, 1, 1)
	a    = (1:-p):*u
	it=0

	A = X'
	c = -y'
	b = X'*a
	x = a

	beta = 0.9995
	small = 1e-5
	max_it = 50
	m = rows(A)
	n = cols(A)

// Generate initial feasible point
	s = u - x
	yy = svsolve(A',c')'
	r = c - yy * A
	r = mm_cond(r:==0,r:+0.001,r)
	z = mm_cond(r:>0,r,0)
	w = z - r
	gap = c * x - yy * b + w * u

// Start iterations
	it = 0
while (gap > small & it < max_it) {
    it++

// Compute affine step
    q = 1 :/ (z' :/ x + w' :/ s)
    r = z - w
	AQ = J(k,n,0)
	for (i=1;i<=n;i++) {
		for (j=1;j<=k;j++) {
			AQ[j,i] = q[i,1]*A[j,i]
		}
	}
	AQA = AQ * A'
	rhs = AQ * r'
	dy = (invsym(AQA) * rhs)'
    dx = q :* (dy * A - r)'
    ds = -dx
    dz = -z :* (1 :+ dx :/ x)'
    dw = -w :* (1 :+ ds :/ s)'

// Compute maximum allowable step lengths
	fx = bound(x, dx)
	fs = bound(s, ds)
	fw = bound(w, dw)
	fz = bound(z, dz)
	fp = rowmin((fx,fs))
	fd = colmin((fw \ fz))
	fp = min((min(beta * fp), 1))
	fd = min((min(beta * fd), 1))

if (mm_cond(fp:<fd,fp,fd) < 1) {

// Update mu
    mu = z * x + w * s
    g = (z + fd * dz) * (x + fp * dx) + (w + fd * dw) * (s + fp * ds)
    mu = mu * (g / mu) ^3 / ( 2 * n)

// Compute modified step
	dxdz = dx :* dz'
	dsdw = ds :* dw'
	xinv = 1 :/ x
	sinv = 1 :/ s
	xi = mu * (xinv - sinv)
	rhs = rhs + A * ( q :* (dxdz - dsdw - xi))
	dy = (invsym(AQA)* rhs)'
	dx = q :* (A' * dy' + xi - r' -dxdz + dsdw)
	ds = -dx
	dz = mu * xinv' - z - xinv' :* z :* dx' - dxdz'
	dw = mu * sinv' - w - sinv' :* w :* ds' - dsdw'

// Compute maximum allowable step lengths
	fx = bound(x, dx)
	fs = bound(s, ds)
	fw = bound(w, dw)
	fz = bound(z, dz)
	fp = rowmin((fx,fs))
	fd = colmin((fw\ fz))
	fp = min((min(beta * fp), 1))
	fd = min((min(beta * fd), 1))
}

// Take the step
    x = x + fp * dx
    s = s + fp * ds
    yy = yy + fd * dy
    w = w + fd * dw
    z = z + fd * dz
    gap = c * x - yy * b + w * u
}

// Return vector of estimates
	bb = -yy'
	return(bb)
}

real scalar objective(real vector varphi1,
						real vector y,
						real matrix X,
						real matrix b,
						real vector copula_p)
{
	real scalar obj

	obj = mean( varphi1 :* ((y:<=X*b):-copula_p) )
	return(obj)
}

real matrix finergrid(string scalar copula)
{

real matrix frank_spearman, frank_rho, frank_kendall, frank, gaussian_spearman, gaussian_rho, gaussian_kendall, gaussian

frank_spearman = -0.990000412\
-0.980000728\
-0.970002141\
-0.960003183\
-0.950003857\
-0.940003417\
-0.930007206\
-0.920008887\
-0.910005355\
-0.900007066\
-0.890006086\
-0.880013322\
-0.870000579\
-0.860010871\
-0.850011222\
-0.840005201\
-0.830012728\
-0.820024826\
-0.810033486\
-0.800003837\
-0.790027662\
-0.780038855\
-0.770026349\
-0.760011178\
-0.75000917\
-0.74003011\
-0.730022892\
-0.720032519\
-0.710047337\
-0.700047628\
-0.690005421\
-0.680018194\
-0.670055967\
-0.660007951\
-0.650042329\
-0.640040276\
-0.630022828\
-0.620011739\
-0.610029484\
-0.600012162\
-0.590065232\
-0.58002924\
-0.57000805\
-0.560019072\
-0.550079911\
-0.540006549\
-0.530009681\
-0.520002113\
-0.510102969\
-0.500001365\
-0.490031748\
-0.480098527\
-0.47009768\
-0.460035046\
-0.45003668\
-0.440114692\
-0.430033259\
-0.420044539\
-0.410032835\
-0.400004523\
-0.390097294\
-0.38005684\
-0.370019087\
-0.360126867\
-0.350115566\
-0.340126274\
-0.330024314\
-0.320096671\
-0.310066017\
-0.30007975\
-0.290144081\
-0.280116996\
-0.270000625\
-0.260098588\
-0.250116782\
-0.240057501\
-0.230077249\
-0.220026553\
-0.210064099\
-0.200038247\
-0.190109613\
-0.180124532\
-0.170085574\
-0.160155992\
-0.150018153\
-0.140158563\
-0.130096133\
-0.120158135\
-0.110021534\
-0.100016126\
-0.090146208\
-0.080085588\
-0.070001409\
-0.06006238\
-0.050106169\
-0.040135595\
-0.030153497\
-0.020162731\
-0.010166162\
-0.000166667\
0.00999952\
0.019996161\
0.029987049\
0.039969313\
0.049940105\
0.059896579\
0.069835911\
0.079920458\
0.089981487\
0.099851854\
0.109857764\
0.119994921\
0.129933512\
0.13999659\
0.149856851\
0.159995447\
0.169925824\
0.179965595\
0.189951551\
0.199881119\
0.209907923\
0.219871386\
0.229923146\
0.239904497\
0.249964952\
0.259947949\
0.269851211\
0.27996886\
0.289997291\
0.29993432\
0.309921995\
0.319954107\
0.329883234\
0.339986747\
0.349977615\
0.359990537\
0.369884398\
0.379923856\
0.389966055\
0.399875044\
0.409905176\
0.419918734\
0.429909342\
0.439992716\
0.449916651\
0.459917013\
0.469981691\
0.479984604\
0.489919909\
0.499891623\
0.509995436\
0.519896751\
0.529906542\
0.539905659\
0.549981316\
0.559922768\
0.569914074\
0.579937624\
0.589976004\
0.599925323\
0.609945031\
0.619929764\
0.629943316\
0.639963247\
0.649967793\
0.659935914\
0.669986462\
0.67995121\
0.689940972\
0.699985737\
0.709987996\
0.719975728\
0.729968627\
0.739978426\
0.749960028\
0.759964578\
0.769982284\
0.779997311\
0.789988618\
0.79996727\
0.809999402\
0.819993171\
0.82998347\
0.839978306\
0.849986652\
0.859988579\
0.869980512\
0.879995427\
0.889990296\
0.899993296\
0.909993553\
0.919998954\
0.929999034\
0.939996913\
0.949998892\
0.959999619\
0.96999982\
0.979999461\
0.989999963

frank_rho = -42.88899994\
-29.83799934\
-24.04199982\
-20.57699966\
-18.20499992\
-16.44799995\
-15.07800007\
-13.96899986\
-13.04599953\
-12.26200008\
-11.58399963\
-10.98999977\
-10.46199989\
-9.989999771\
-9.562999725\
-9.173999786\
-8.81799984\
-8.489999771\
-8.18599987\
-7.90199995\
-7.638000011\
-7.389999866\
-7.156000137\
-6.934999943\
-6.725999832\
-6.527999878\
-6.339000225\
-6.15899992\
-5.986999989\
-5.822000027\
-5.663000107\
-5.511000156\
-5.364999771\
-5.22300005\
-5.086999893\
-4.954999924\
-4.827000141\
-4.703000069\
-4.583000183\
-4.46600008\
-4.353000164\
-4.242000103\
-4.133999825\
-4.028999805\
-3.927000046\
-3.825999975\
-3.727999926\
-3.631999969\
-3.539000034\
-3.446000099\
-3.355999947\
-3.267999887\
-3.180999994\
-3.095000029\
-3.010999918\
-2.928999901\
-2.846999884\
-2.76699996\
-2.687999964\
-2.609999895\
-2.53399992\
-2.457999945\
-2.382999897\
-2.309999943\
-2.236999989\
-2.164999962\
-2.092999935\
-2.023000002\
-1.952999949\
-1.883999944\
-1.815999985\
-1.748000026\
-1.679999948\
-1.613999963\
-1.547999978\
-1.481999993\
-1.417000055\
-1.351999998\
-1.287999988\
-1.223999977\
-1.161000013\
-1.09800005\
-1.034999967\
-0.97299999\
-0.910000026\
-0.848999977\
-0.787\
-0.726000011\
-0.663999975\
-0.602999985\
-0.542999983\
-0.481999993\
-0.421000004\
-0.361000001\
-0.300999999\
-0.240999997\
-0.180999994\
-0.120999999\
-0.061000001\
-0.001\
0.059999999\
0.119999997\
0.180000007\
0.239999995\
0.300000012\
0.360000014\
0.419999987\
0.481000006\
0.541999996\
0.601999998\
0.662999988\
0.725000024\
0.786000013\
0.84799999\
0.908999979\
0.972000003\
1.034000039\
1.097000003\
1.159999967\
1.22300005\
1.286999941\
1.350999951\
1.416000009\
1.480999947\
1.547000051\
1.613000035\
1.67900002\
1.746999979\
1.815000057\
1.883000016\
1.952000022\
2.022000074\
2.092000008\
2.164000034\
2.236000061\
2.309000015\
2.381999969\
2.457000017\
2.532999992\
2.608999968\
2.687000036\
2.766000032\
2.845999956\
2.927999973\
3.00999999\
3.094000101\
3.180000067\
3.26699996\
3.355000019\
3.444999933\
3.538000107\
3.631000042\
3.726999998\
3.825000048\
3.926000118\
4.027999878\
4.132999897\
4.241000175\
4.352000237\
4.465000153\
4.581999779\
4.702000141\
4.826000214\
4.953999996\
5.085999966\
5.222000122\
5.363999844\
5.510000229\
5.662000179\
5.821000099\
5.986000061\
6.157999992\
6.337999821\
6.52699995\
6.724999905\
6.934000015\
7.15500021\
7.388999939\
7.637000084\
7.901000023\
8.18500042\
8.48900032\
8.817000389\
9.173000336\
9.562000275\
9.98900032\
10.46100044\
10.98900032\
11.58300018\
12.26099968\
13.04500008\
13.96800041\
15.07699966\
16.4470005\
18.20400047\
20.57600021\
24.04100037\
29.83699989\
42.88800049

frank_kendall=-0.910312974\
-0.873333174\
-0.845007766\
-0.821147963\
-0.800133184\
-0.781130368\
-0.763654246\
-0.747370638\
-0.732051168\
-0.717548136\
-0.703725961\
-0.690503043\
-0.677766417\
-0.665508549\
-0.653636977\
-0.642114185\
-0.630926698\
-0.620032903\
-0.609397601\
-0.598962237\
-0.588801724\
-0.578829786\
-0.569020853\
-0.55938234\
-0.549916787\
-0.540621499\
-0.531438974\
-0.5224017\
-0.513490511\
-0.504680507\
-0.495941137\
-0.487350767\
-0.478876683\
-0.470420196\
-0.462118028\
-0.453866084\
-0.445678145\
-0.437568191\
-0.429550425\
-0.421570198\
-0.413708046\
-0.405835143\
-0.398030843\
-0.390305695\
-0.382670228\
-0.374981402\
-0.367398279\
-0.359852186\
-0.352430374\
-0.344898506\
-0.337504636\
-0.330175228\
-0.322832133\
-0.315478823\
-0.308205959\
-0.301020293\
-0.293750117\
-0.286576327\
-0.27941432\
-0.272267667\
-0.265232984\
-0.258128686\
-0.251050411\
-0.244097384\
-0.237082582\
-0.230104278\
-0.223067781\
-0.216171958\
-0.209223141\
-0.20232281\
-0.195474242\
-0.188578902\
-0.181637953\
-0.17485872\
-0.168038868\
-0.161179623\
-0.154387045\
-0.147558742\
-0.14080187\
-0.134012926\
-0.127300027\
-0.120558636\
-0.113790073\
-0.107103773\
-0.100285418\
-0.093661595\
-0.086908478\
-0.080245266\
-0.07345491\
-0.066757876\
-0.060156326\
-0.053431622\
-0.046695118\
-0.040058954\
-0.03341419\
-0.02676224\
-0.020104526\
-0.013442476\
-0.006777526\
-0.000111111\
0.006666427\
0.013331414\
0.019993524\
0.026651321\
0.03330338\
0.039948276\
0.046584591\
0.05332128\
0.060046189\
0.066647964\
0.07334525\
0.080135885\
0.086799395\
0.093552837\
0.100176996\
0.106995733\
0.113682432\
0.120451402\
0.127193235\
0.133906609\
0.140696031\
0.147453414\
0.154282257\
0.161075393\
0.167935238\
0.174755696\
0.181535554\
0.188477152\
0.195373183\
0.202222448\
0.209123501\
0.216073067\
0.222969654\
0.230006952\
0.236986071\
0.244001713\
0.250955592\
0.258034755\
0.265139964\
0.272175567\
0.279323175\
0.286486158\
0.293660943\
0.300932146\
0.308118846\
0.315392773\
0.322747175\
0.330091378\
0.337421908\
0.344816906\
0.352349977\
0.359772971\
0.367320281\
0.374904641\
0.382594734\
0.390231472\
0.397957918\
0.405763541\
0.41363779\
0.421501297\
0.429482875\
0.437502073\
0.445613452\
0.453802835\
0.462056241\
0.470359886\
0.478817881\
0.487293478\
0.495885386\
0.50462632\
0.5134379\
0.522350682\
0.531389544\
0.540573724\
0.549870657\
0.559337877\
0.568978075\
0.578788711\
0.588762364\
0.598924606\
0.609361742\
0.619998807\
0.630894379\
0.64208366\
0.653608262\
0.665481659\
0.677741362\
0.690479842\
0.703704624\
0.71752866\
0.7320336\
0.747354974\
0.763640484\
0.781118546\
0.800123301\
0.82114003\
0.845001796\
0.873329179\
0.910310967

frank = frank_rho,frank_spearman,frank_kendall

gaussian_spearman=-0.99\
-0.98\
-0.97\
-0.96\
-0.95\
-0.94\
-0.93\
-0.92\
-0.91\
-0.90\
-0.89\
-0.88\
-0.87\
-0.86\
-0.85\
-0.84\
-0.83\
-0.82\
-0.81\
-0.80\
-0.79\
-0.78\
-0.77\
-0.76\
-0.75\
-0.74\
-0.73\
-0.72\
-0.71\
-0.70\
-0.69\
-0.68\
-0.67\
-0.66\
-0.65\
-0.64\
-0.63\
-0.62\
-0.61\
-0.60\
-0.59\
-0.58\
-0.57\
-0.56\
-0.55\
-0.54\
-0.53\
-0.52\
-0.51\
-0.50\
-0.49\
-0.48\
-0.47\
-0.46\
-0.45\
-0.44\
-0.43\
-0.42\
-0.41\
-0.40\
-0.39\
-0.38\
-0.37\
-0.36\
-0.35\
-0.34\
-0.33\
-0.32\
-0.31\
-0.30\
-0.29\
-0.28\
-0.27\
-0.26\
-0.25\
-0.24\
-0.23\
-0.22\
-0.21\
-0.20\
-0.19\
-0.18\
-0.17\
-0.16\
-0.15\
-0.14\
-0.13\
-0.12\
-0.11\
-0.10\
-0.09\
-0.08\
-0.07\
-0.06\
-0.05\
-0.04\
-0.03\
-0.02\
-0.01\
0.00\
0.01\
0.02\
0.03\
0.04\
0.05\
0.06\
0.07\
0.08\
0.09\
0.10\
0.11\
0.12\
0.13\
0.14\
0.15\
0.16\
0.17\
0.18\
0.19\
0.20\
0.21\
0.22\
0.23\
0.24\
0.25\
0.26\
0.27\
0.28\
0.29\
0.30\
0.31\
0.32\
0.33\
0.34\
0.35\
0.36\
0.37\
0.38\
0.39\
0.40\
0.41\
0.42\
0.43\
0.44\
0.45\
0.46\
0.47\
0.48\
0.49\
0.50\
0.51\
0.52\
0.53\
0.54\
0.55\
0.56\
0.57\
0.58\
0.59\
0.60\
0.61\
0.62\
0.63\
0.64\
0.65\
0.66\
0.67\
0.68\
0.69\
0.70\
0.71\
0.72\
0.73\
0.74\
0.75\
0.76\
0.77\
0.78\
0.79\
0.80\
0.81\
0.82\
0.83\
0.84\
0.85\
0.86\
0.87\
0.88\
0.89\
0.90\
0.91\
0.92\
0.93\
0.94\
0.95\
0.96\
0.97\
0.98\
0.99

gaussian_rho=-0.990917337\
-0.981807507\
-0.972670761\
-0.963507348\
-0.954317521\
-0.94510153\
-0.935859629\
-0.92659207\
-0.917299109\
-0.907980999\
-0.898637997\
-0.889270358\
-0.87987834\
-0.870462199\
-0.861022194\
-0.851558583\
-0.842071627\
-0.832561585\
-0.823028717\
-0.813473286\
-0.803895553\
-0.794295781\
-0.784674233\
-0.775031173\
-0.765366865\
-0.755681574\
-0.745975565\
-0.736249105\
-0.726502461\
-0.716735899\
-0.706949688\
-0.697144095\
-0.687319389\
-0.67747584\
-0.667613718\
-0.657733293\
-0.647834836\
-0.637918619\
-0.627984912\
-0.618033989\
-0.608066122\
-0.598081585\
-0.58808065\
-0.578063594\
-0.568030689\
-0.557982212\
-0.547918437\
-0.537839641\
-0.5277461\
-0.51763809\
-0.507515889\
-0.497379774\
-0.487230024\
-0.477066915\
-0.466890728\
-0.45670174\
-0.446500232\
-0.436286483\
-0.426060773\
-0.415823382\
-0.405574591\
-0.395314681\
-0.385043933\
-0.374762629\
-0.364471051\
-0.354169481\
-0.343858201\
-0.333537493\
-0.323207642\
-0.31286893\
-0.30252164\
-0.292166057\
-0.281802464\
-0.271431145\
-0.261052384\
-0.250666467\
-0.240273678\
-0.229874301\
-0.219468622\
-0.209056927\
-0.198639499\
-0.188216627\
-0.177788594\
-0.167355687\
-0.156918191\
-0.146476394\
-0.136030581\
-0.125581039\
-0.115128054\
-0.104671912\
-0.094212901\
-0.083751307\
-0.073287417\
-0.062821518\
-0.052353897\
-0.04188484\
-0.031414635\
-0.020943568\
-0.010471928\
0\
0.010471928\
0.020943568\
0.031414635\
0.04188484\
0.052353897\
0.062821518\
0.073287417\
0.083751307\
0.094212901\
0.104671912\
0.115128054\
0.125581039\
0.136030581\
0.146476394\
0.156918191\
0.167355687\
0.177788594\
0.188216627\
0.198639499\
0.209056927\
0.219468622\
0.229874301\
0.240273678\
0.250666467\
0.261052384\
0.271431145\
0.281802464\
0.292166057\
0.30252164\
0.31286893\
0.323207642\
0.333537493\
0.343858201\
0.354169481\
0.364471051\
0.374762629\
0.385043933\
0.395314681\
0.405574591\
0.415823382\
0.426060773\
0.436286483\
0.446500232\
0.45670174\
0.466890728\
0.477066915\
0.487230024\
0.497379774\
0.507515889\
0.51763809\
0.5277461\
0.537839641\
0.547918437\
0.557982212\
0.568030689\
0.578063594\
0.58808065\
0.598081585\
0.608066122\
0.618033989\
0.627984912\
0.637918619\
0.647834836\
0.657733293\
0.667613718\
0.67747584\
0.687319389\
0.697144095\
0.706949688\
0.716735899\
0.726502461\
0.736249105\
0.745975565\
0.755681574\
0.765366865\
0.775031173\
0.784674233\
0.794295781\
0.803895553\
0.813473286\
0.823028717\
0.832561585\
0.842071627\
0.851558583\
0.861022194\
0.870462199\
0.87987834\
0.889270358\
0.898637997\
0.907980999\
0.917299109\
0.92659207\
0.935859629\
0.94510153\
0.954317521\
0.963507348\
0.972670761\
0.981807507\
0.990917337

gaussian_kendall=-0.909893173\
-0.872462878\
-0.843668136\
-0.819331059\
-0.797834752\
-0.778350627\
-0.760386833\
-0.743623133\
-0.727837239\
-0.712867414\
-0.698591632\
-0.684915149\
-0.671762661\
-0.659073143\
-0.646796326\
-0.634890218\
-0.623319311\
-0.612053264\
-0.601065905\
-0.590334471\
-0.579839017\
-0.569561949\
-0.559487654\
-0.549602199\
-0.539893088\
-0.530349062\
-0.520959934\
-0.511716449\
-0.50261017\
-0.493633378\
-0.484778987\
-0.476040478\
-0.467411831\
-0.458887475\
-0.450462243\
-0.442131328\
-0.43389025\
-0.425734827\
-0.417661144\
-0.409665529\
-0.401744536\
-0.393894918\
-0.386113619\
-0.378397753\
-0.370744589\
-0.363151543\
-0.355616165\
-0.348136128\
-0.340709219\
-0.333333333\
-0.326006462\
-0.318726689\
-0.311492184\
-0.304301194\
-0.297152044\
-0.290043124\
-0.28297289\
-0.275939861\
-0.268942609\
-0.261979761\
-0.255049993\
-0.24815203\
-0.241284636\
-0.234446622\
-0.227636835\
-0.220854156\
-0.214097505\
-0.207365832\
-0.200658117\
-0.193973368\
-0.187310622\
-0.180668941\
-0.174047409\
-0.167445135\
-0.160861247\
-0.154294893\
-0.147745242\
-0.141211478\
-0.134692803\
-0.128188434\
-0.121697602\
-0.115219553\
-0.108753545\
-0.102298847\
-0.09585474\
-0.089420514\
-0.08299547\
-0.076578918\
-0.070170173\
-0.063768561\
-0.057373412\
-0.050984064\
-0.044599858\
-0.038220142\
-0.031844266\
-0.025471586\
-0.019101459\
-0.012733244\
-0.006366304\
0\
0.006366304\
0.012733244\
0.019101459\
0.025471586\
0.031844266\
0.038220142\
0.044599858\
0.050984064\
0.057373412\
0.063768561\
0.070170173\
0.076578918\
0.08299547\
0.089420514\
0.09585474\
0.102298847\
0.108753545\
0.115219553\
0.121697602\
0.128188434\
0.134692803\
0.141211478\
0.147745242\
0.154294893\
0.160861247\
0.167445135\
0.174047409\
0.180668941\
0.187310622\
0.193973368\
0.200658117\
0.207365832\
0.214097505\
0.220854156\
0.227636835\
0.234446622\
0.241284636\
0.24815203\
0.255049993\
0.261979761\
0.268942609\
0.275939861\
0.28297289\
0.290043124\
0.297152044\
0.304301194\
0.311492184\
0.318726689\
0.326006462\
0.333333333\
0.340709219\
0.348136128\
0.355616165\
0.363151543\
0.370744589\
0.378397753\
0.386113619\
0.393894918\
0.401744536\
0.409665529\
0.417661144\
0.425734827\
0.43389025\
0.442131328\
0.450462243\
0.458887475\
0.467411831\
0.476040478\
0.484778987\
0.493633378\
0.50261017\
0.511716449\
0.520959934\
0.530349062\
0.539893088\
0.549602199\
0.559487654\
0.569561949\
0.579839017\
0.590334471\
0.601065905\
0.612053264\
0.623319311\
0.634890218\
0.646796326\
0.659073143\
0.671762661\
0.684915149\
0.698591632\
0.712867414\
0.727837239\
0.743623133\
0.760386833\
0.778350627\
0.797834752\
0.819331059\
0.843668136\
0.872462878\
0.909893173

gaussian = gaussian_rho,gaussian_spearman,gaussian_kendall

if (copula=="gaussian") {
	return(gaussian)
}
else {
    return(frank)
}

}

real matrix regulargrid(string scalar copula)
{

real matrix frank_spearman, frank_rho, frank_kendall, frank, gaussian_spearman, gaussian_rho, gaussian_kendall, gaussian

frank_spearman = -0.990000412\
-0.970002141\
-0.950003857\
-0.930007206\
-0.910005355\
-0.890006086\
-0.870000579\
-0.850011222\
-0.830012728\
-0.810033486\
-0.790027662\
-0.770026349\
-0.75000917\
-0.730022892\
-0.710047337\
-0.690005421\
-0.670055967\
-0.650042329\
-0.630022828\
-0.610029484\
-0.590065232\
-0.57000805\
-0.550079911\
-0.530009681\
-0.510102969\
-0.490031748\
-0.47009768\
-0.45003668\
-0.430033259\
-0.410032835\
-0.390097294\
-0.370019087\
-0.350115566\
-0.330024314\
-0.310066017\
-0.290144081\
-0.270000625\
-0.250116782\
-0.230077249\
-0.210064099\
-0.190109613\
-0.170085574\
-0.150018153\
-0.130096133\
-0.110021534\
-0.090146208\
-0.070001409\
-0.050106169\
-0.030153497\
-0.010166162\
0.00999952\
0.029987049\
0.049940105\
0.069835911\
0.089981487\
0.109857764\
0.129933512\
0.149856851\
0.169925824\
0.189951551\
0.209907923\
0.229923146\
0.249964952\
0.269851211\
0.289997291\
0.309921995\
0.329883234\
0.349977615\
0.369884398\
0.389966055\
0.409905176\
0.429909342\
0.449916651\
0.469981691\
0.489919909\
0.509995436\
0.529906542\
0.549981316\
0.569914074\
0.589976004\
0.609945031\
0.629943316\
0.649967793\
0.669986462\
0.689940972\
0.709987996\
0.729968627\
0.749960028\
0.769982284\
0.789988618\
0.809999402\
0.82998347\
0.849986652\
0.869980512\
0.889990296\
0.909993553\
0.929999034\
0.949998892\
0.96999982\
0.989999963

frank_rho = -42.88899994\
-24.04199982\
-18.20499992\
-15.07800007\
-13.04599953\
-11.58399963\
-10.46199989\
-9.562999725\
-8.81799984\
-8.18599987\
-7.638000011\
-7.156000137\
-6.725999832\
-6.339000225\
-5.986999989\
-5.663000107\
-5.364999771\
-5.086999893\
-4.827000141\
-4.583000183\
-4.353000164\
-4.133999825\
-3.927000046\
-3.727999926\
-3.539000034\
-3.355999947\
-3.180999994\
-3.010999918\
-2.846999884\
-2.687999964\
-2.53399992\
-2.382999897\
-2.236999989\
-2.092999935\
-1.952999949\
-1.815999985\
-1.679999948\
-1.547999978\
-1.417000055\
-1.287999988\
-1.161000013\
-1.034999967\
-0.910000026\
-0.787\
-0.663999975\
-0.542999983\
-0.421000004\
-0.300999999\
-0.180999994\
-0.061000001\
0.059999999\
0.180000007\
0.300000012\
0.419999987\
0.541999996\
0.662999988\
0.786000013\
0.908999979\
1.034000039\
1.159999967\
1.286999941\
1.416000009\
1.547000051\
1.67900002\
1.815000057\
1.952000022\
2.092000008\
2.236000061\
2.381999969\
2.532999992\
2.687000036\
2.845999956\
3.00999999\
3.180000067\
3.355000019\
3.538000107\
3.726999998\
3.926000118\
4.132999897\
4.352000237\
4.581999779\
4.826000214\
5.085999966\
5.363999844\
5.662000179\
5.986000061\
6.337999821\
6.724999905\
7.15500021\
7.637000084\
8.18500042\
8.817000389\
9.562000275\
10.46100044\
11.58300018\
13.04500008\
15.07699966\
18.20400047\
24.04100037\
42.88800049

frank_kendall= -0.910312974\
-0.845007766\
-0.800133184\
-0.763654246\
-0.732051168\
-0.703725961\
-0.677766417\
-0.653636977\
-0.630926698\
-0.609397601\
-0.588801724\
-0.569020853\
-0.549916787\
-0.531438974\
-0.513490511\
-0.495941137\
-0.478876683\
-0.462118028\
-0.445678145\
-0.429550425\
-0.413708046\
-0.398030843\
-0.382670228\
-0.367398279\
-0.352430374\
-0.337504636\
-0.322832133\
-0.308205959\
-0.293750117\
-0.27941432\
-0.265232984\
-0.251050411\
-0.237082582\
-0.223067781\
-0.209223141\
-0.195474242\
-0.181637953\
-0.168038868\
-0.154387045\
-0.14080187\
-0.127300027\
-0.113790073\
-0.100285418\
-0.086908478\
-0.07345491\
-0.060156326\
-0.046695118\
-0.03341419\
-0.020104526\
-0.006777526\
0.006666427\
0.019993524\
0.03330338\
0.046584591\
0.060046189\
0.07334525\
0.086799395\
0.100176996\
0.113682432\
0.127193235\
0.140696031\
0.154282257\
0.167935238\
0.181535554\
0.195373183\
0.209123501\
0.222969654\
0.236986071\
0.250955592\
0.265139964\
0.279323175\
0.293660943\
0.308118846\
0.322747175\
0.337421908\
0.352349977\
0.367320281\
0.382594734\
0.397957918\
0.41363779\
0.429482875\
0.445613452\
0.462056241\
0.478817881\
0.495885386\
0.5134379\
0.531389544\
0.549870657\
0.568978075\
0.588762364\
0.609361742\
0.630894379\
0.653608262\
0.677741362\
0.703704624\
0.7320336\
0.763640484\
0.800123301\
0.845001796\
0.910310967

frank = frank_rho,frank_spearman,frank_kendall

gaussian_spearman= -0.99\
-0.97\
-0.95\
-0.93\
-0.91\
-0.89\
-0.87\
-0.85\
-0.83\
-0.81\
-0.79\
-0.77\
-0.75\
-0.73\
-0.71\
-0.69\
-0.67\
-0.65\
-0.63\
-0.61\
-0.59\
-0.57\
-0.55\
-0.53\
-0.51\
-0.49\
-0.47\
-0.45\
-0.43\
-0.41\
-0.39\
-0.37\
-0.35\
-0.33\
-0.31\
-0.29\
-0.27\
-0.25\
-0.23\
-0.21\
-0.19\
-0.17\
-0.15\
-0.13\
-0.11\
-0.09\
-0.07\
-0.05\
-0.03\
-0.01\
0.01\
0.03\
0.05\
0.07\
0.09\
0.11\
0.13\
0.15\
0.17\
0.19\
0.21\
0.23\
0.25\
0.27\
0.29\
0.31\
0.33\
0.35\
0.37\
0.39\
0.41\
0.43\
0.45\
0.47\
0.49\
0.51\
0.53\
0.55\
0.57\
0.59\
0.61\
0.63\
0.65\
0.67\
0.69\
0.71\
0.73\
0.75\
0.77\
0.79\
0.81\
0.83\
0.85\
0.87\
0.89\
0.91\
0.93\
0.95\
0.97\
0.99


gaussian_rho= -0.990917337\
-0.972670761\
-0.954317521\
-0.935859629\
-0.917299109\
-0.898637997\
-0.87987834\
-0.861022194\
-0.842071627\
-0.823028717\
-0.803895553\
-0.784674233\
-0.765366865\
-0.745975565\
-0.726502461\
-0.706949688\
-0.687319389\
-0.667613718\
-0.647834836\
-0.627984912\
-0.608066122\
-0.58808065\
-0.568030689\
-0.547918437\
-0.5277461\
-0.507515889\
-0.487230024\
-0.466890728\
-0.446500232\
-0.426060773\
-0.405574591\
-0.385043933\
-0.364471051\
-0.343858201\
-0.323207642\
-0.30252164\
-0.281802464\
-0.261052384\
-0.240273678\
-0.219468622\
-0.198639499\
-0.177788594\
-0.156918191\
-0.136030581\
-0.115128054\
-0.094212901\
-0.073287417\
-0.052353897\
-0.031414635\
-0.010471928\
0.010471928\
0.031414635\
0.052353897\
0.073287417\
0.094212901\
0.115128054\
0.136030581\
0.156918191\
0.177788594\
0.198639499\
0.219468622\
0.240273678\
0.261052384\
0.281802464\
0.30252164\
0.323207642\
0.343858201\
0.364471051\
0.385043933\
0.405574591\
0.426060773\
0.446500232\
0.466890728\
0.487230024\
0.507515889\
0.5277461\
0.547918437\
0.568030689\
0.58808065\
0.608066122\
0.627984912\
0.647834836\
0.667613718\
0.687319389\
0.706949688\
0.726502461\
0.745975565\
0.765366865\
0.784674233\
0.803895553\
0.823028717\
0.842071627\
0.861022194\
0.87987834\
0.898637997\
0.917299109\
0.935859629\
0.954317521\
0.972670761\
0.990917337

gaussian_kendall= -0.909893173\
-0.843668136\
-0.797834752\
-0.760386833\
-0.727837239\
-0.698591632\
-0.671762661\
-0.646796326\
-0.623319311\
-0.601065905\
-0.579839017\
-0.559487654\
-0.539893088\
-0.520959934\
-0.50261017\
-0.484778987\
-0.467411831\
-0.450462243\
-0.43389025\
-0.417661144\
-0.401744536\
-0.386113619\
-0.370744589\
-0.355616165\
-0.340709219\
-0.326006462\
-0.311492184\
-0.297152044\
-0.28297289\
-0.268942609\
-0.255049993\
-0.241284636\
-0.227636835\
-0.214097505\
-0.200658117\
-0.187310622\
-0.174047409\
-0.160861247\
-0.147745242\
-0.134692803\
-0.121697602\
-0.108753545\
-0.09585474\
-0.08299547\
-0.070170173\
-0.057373412\
-0.044599858\
-0.031844266\
-0.019101459\
-0.006366304\
0.006366304\
0.019101459\
0.031844266\
0.044599858\
0.057373412\
0.070170173\
0.08299547\
0.09585474\
0.108753545\
0.121697602\
0.134692803\
0.147745242\
0.160861247\
0.174047409\
0.187310622\
0.200658117\
0.214097505\
0.227636835\
0.241284636\
0.255049993\
0.268942609\
0.28297289\
0.297152044\
0.311492184\
0.326006462\
0.340709219\
0.355616165\
0.370744589\
0.386113619\
0.401744536\
0.417661144\
0.43389025\
0.450462243\
0.467411831\
0.484778987\
0.50261017\
0.520959934\
0.539893088\
0.559487654\
0.579839017\
0.601065905\
0.623319311\
0.646796326\
0.671762661\
0.698591632\
0.727837239\
0.760386833\
0.797834752\
0.843668136\
0.909893173

gaussian = gaussian_rho,gaussian_spearman,gaussian_kendall

if (copula=="gaussian") {
	return(gaussian)
}
else {
    return(frank)
}

}

real matrix coarsergrid(string scalar copula)
{

real matrix frank_spearman, frank_rho, frank_kendall, frank, gaussian_spearman, gaussian_rho, gaussian_kendall, gaussian

frank_spearman = -0.990000412\
-0.950003857\
-0.910005355\
-0.870000579\
-0.830012728\
-0.790027662\
-0.75000917\
-0.710047337\
-0.670055967\
-0.630022828\
-0.590065232\
-0.550079911\
-0.510102969\
-0.47009768\
-0.430033259\
-0.390097294\
-0.350115566\
-0.310066017\
-0.270000625\
-0.230077249\
-0.190109613\
-0.150018153\
-0.110021534\
-0.070001409\
-0.030153497\
0.00999952\
0.049940105\
0.089981487\
0.129933512\
0.169925824\
0.209907923\
0.249964952\
0.289997291\
0.329883234\
0.369884398\
0.409905176\
0.449916651\
0.489919909\
0.529906542\
0.569914074\
0.609945031\
0.649967793\
0.689940972\
0.729968627\
0.769982284\
0.809999402\
0.849986652\
0.889990296\
0.929999034\
0.96999982

frank_rho = -42.88899994\
-18.20499992\
-13.04599953\
-10.46199989\
-8.81799984\
-7.638000011\
-6.725999832\
-5.986999989\
-5.364999771\
-4.827000141\
-4.353000164\
-3.927000046\
-3.539000034\
-3.180999994\
-2.846999884\
-2.53399992\
-2.236999989\
-1.952999949\
-1.679999948\
-1.417000055\
-1.161000013\
-0.910000026\
-0.663999975\
-0.421000004\
-0.180999994\
0.059999999\
0.300000012\
0.541999996\
0.786000013\
1.034000039\
1.286999941\
1.547000051\
1.815000057\
2.092000008\
2.381999969\
2.687000036\
3.00999999\
3.355000019\
3.726999998\
4.132999897\
4.581999779\
5.085999966\
5.662000179\
6.337999821\
7.15500021\
8.18500042\
9.562000275\
11.58300018\
15.07699966\
24.04100037


frank_kendall= -0.910312974\
-0.800133184\
-0.732051168\
-0.677766417\
-0.630926698\
-0.588801724\
-0.549916787\
-0.513490511\
-0.478876683\
-0.445678145\
-0.413708046\
-0.382670228\
-0.352430374\
-0.322832133\
-0.293750117\
-0.265232984\
-0.237082582\
-0.209223141\
-0.181637953\
-0.154387045\
-0.127300027\
-0.100285418\
-0.07345491\
-0.046695118\
-0.020104526\
0.006666427\
0.03330338\
0.060046189\
0.086799395\
0.113682432\
0.140696031\
0.167935238\
0.195373183\
0.222969654\
0.250955592\
0.279323175\
0.308118846\
0.337421908\
0.367320281\
0.397957918\
0.429482875\
0.462056241\
0.495885386\
0.531389544\
0.568978075\
0.609361742\
0.653608262\
0.703704624\
0.763640484\
0.845001796


frank = frank_rho,frank_spearman,frank_kendall

gaussian_spearman= -0.99\
-0.95\
-0.91\
-0.87\
-0.83\
-0.79\
-0.75\
-0.71\
-0.67\
-0.63\
-0.59\
-0.55\
-0.51\
-0.47\
-0.43\
-0.39\
-0.35\
-0.31\
-0.27\
-0.23\
-0.19\
-0.15\
-0.11\
-0.07\
-0.03\
0.01\
0.05\
0.09\
0.13\
0.17\
0.21\
0.25\
0.29\
0.33\
0.37\
0.41\
0.45\
0.49\
0.53\
0.57\
0.61\
0.65\
0.69\
0.73\
0.77\
0.81\
0.85\
0.89\
0.93\
0.97

gaussian_rho= -0.990917337\
-0.954317521\
-0.917299109\
-0.87987834\
-0.842071627\
-0.803895553\
-0.765366865\
-0.726502461\
-0.687319389\
-0.647834836\
-0.608066122\
-0.568030689\
-0.5277461\
-0.487230024\
-0.446500232\
-0.405574591\
-0.364471051\
-0.323207642\
-0.281802464\
-0.240273678\
-0.198639499\
-0.156918191\
-0.115128054\
-0.073287417\
-0.031414635\
0.010471928\
0.052353897\
0.094212901\
0.136030581\
0.177788594\
0.219468622\
0.261052384\
0.30252164\
0.343858201\
0.385043933\
0.426060773\
0.466890728\
0.507515889\
0.547918437\
0.58808065\
0.627984912\
0.667613718\
0.706949688\
0.745975565\
0.784674233\
0.823028717\
0.861022194\
0.898637997\
0.935859629\
0.972670761

gaussian_kendall= -0.909893173\
-0.797834752\
-0.727837239\
-0.671762661\
-0.623319311\
-0.579839017\
-0.539893088\
-0.50261017\
-0.467411831\
-0.43389025\
-0.401744536\
-0.370744589\
-0.340709219\
-0.311492184\
-0.28297289\
-0.255049993\
-0.227636835\
-0.200658117\
-0.174047409\
-0.147745242\
-0.121697602\
-0.09585474\
-0.070170173\
-0.044599858\
-0.019101459\
0.006366304\
0.031844266\
0.057373412\
0.08299547\
0.108753545\
0.134692803\
0.160861247\
0.187310622\
0.214097505\
0.241284636\
0.268942609\
0.297152044\
0.326006462\
0.355616165\
0.386113619\
0.417661144\
0.450462243\
0.484778987\
0.520959934\
0.559487654\
0.601065905\
0.646796326\
0.698591632\
0.760386833\
0.843668136

gaussian = gaussian_rho,gaussian_spearman,gaussian_kendall

if (copula=="gaussian") {
	return(gaussian)
}
else {
    return(frank)
}

}

end