Generating From Copulas
Steve and Thealexa
July 11 (2011)

Clayton: alpha is in (0, oo)
Using paper by Kpanzou (Copulas in Statistics, 2007)

Hyperlink["http://resources.aims.ac.za/archive/2006/tchilabalo.pdf"]
Hyperlink["http://www.ermii.org/Research/downloads/SimulatingArchimedeanCopulas.pdf"]

(* useful functions *)
firstdigit[x_] := Floor[10^Mod[Log[10., x],1]];
phi[t_, alpha_] := ((t^(-alpha))-1)/alpha;
phiinverse[q_, alpha_] :=1/(alpha*q+1.)^(1/alpha);
phiprime[t_, alpha_] := -t^(-alpha-1);
Cphi[u_, v_, alpha_] := Exp[-( phi[u,alpha] + phi[v,alpha])^(1/alpha)] (*page 23 *)
phiinvder[u_,alpha_,k_] := (-1)^k (1 + alpha u)^(-(1+ k alpha)/alpha) Product[1 + j alpha, {j,0,k-1}];
(*KC[t_, alpha_] := t - (phi[t, alpha] / phiprime[t,alpha])*)
KC[n_,t_,alpha_] := t + Sum[(1/k!) (-1)^k phiinvder[phi[t,alpha],alpha,k] phi[t,alpha]^k, {k,1,n-1}];
Plot[KC[4,t,1],{t,0,1}]

(* this creates KCinverselist, returns that value as a list *)
(* when using this program, set your list equal to this *)
(* for example: KCinverselist = makeKCinverselist[2,10000]; *)
makeKCinverselist[nn_,alpha_, numdiv_] := Module[{},
tempKCinverselist = {};
For[i = 0.0001, i <=1.0001, i = i + (1/numdiv), tempKCinverselist = AppendTo[tempKCinverselist, {i,KC[nn,i,alpha]}]];
Return[tempKCinverselist];
];

(* need to input KCinverselist *)
KCinversefn[q_, templist_] := Module[{},
location = 1;
While[templist[[location,2]] < q, location = location + 1];
xx = templist[[location,1]];
Return[xx];
];

benfordcopulatestor[n_,alphastart_, alphaend_,numalpha_,KCinversenumdiv_,numiter_] := Module[{},
benfordplot = Plot[1/(x Log[10.]), {x,1,10}];
chibounds = Plot[{15.5073, 20.0902},{x,alphastart,alphaend}];
For[d = 1, d ≤ 9, d++,benfordexpected[d] = Log[10., (d+1)/d] numiter];
Print["This program checks alpha in range ", alphastart, " to ", alphaend, "."];
Print["We are doing ", numiter, " iterations for each choice of alpha."];
Print["We are doing ", n, " variables."];
Print["We compare first digit of products to Benford probabilities and do a chisquare test."];
Print["Critical values: 5% has critical value 15.5073, 1% has 20.0902."];

productchisquarealphalist = {};
amalgamatedchisquarealphalist = {};
amalgamateddata = {};
productdata = {};

For[alphause = alphastart, alphause ≤ alphaend, alphause = alphause + (alphaend - alphastart)/(numalpha-1.),
{
(* initialize KCinverselist *)
KCinverselist = makeKCinverselist[n,alphause,KCinversenumdiv];
(*output = {};
uoutput = {};
voutput={};
uvoutput={};*)
For[d = 1, d ≤ 9, d++, 
{
productdigitcount[d] = 0;
amalgamateddigitcount[d] = 0;
}];
For[m = 1, m ≤ numiter, m++,
{
q = Random[];
For[k = 1, k ≤ n, k++, s[k] = Random[]^(1/k)];
t = KCinversefn[s[n]^n, KCinverselist];
sproduct[n] = 1;
For[k=n-1, k ≥ 1, k--,sproduct[k] = sproduct[k+1] s[k]];
Clear[u];
Clear[x];
u[1] = phiinverse[sproduct[1] phi[t,alphause], alphause];
u[n] = phiinverse[(1-s[n-1]) phi[t,alphause],alphause];
If[n > 2,
For[k = 2, k ≤ n-1, k++, u[k] = phiinverse[(1-s[k-1]) sproduct[k] phi[t,alphause],alphause]
];
];
For[k = 1, k ≤ n, k++, x[k] = KCinversefn[u[k], KCinverselist]];
(*output = AppendTo[output, {u,v}];*)
(*uoutput = AppendTo[uoutput, u];*)
(*voutput=AppendTo[voutput, v];*)
(*uvoutput=AppendTo[uvoutput,firstdigit[u*v]];*)
d = firstdigit[Product[x[k],{k,1,n}]];
productdigitcount[d] = productdigitcount[d]+1;
For[k = 1, k ≤ n, k++,
{
d = firstdigit[x[k]];
amalgamateddigitcount[d] = amalgamateddigitcount[d] + 1;
}]; (* end of k loop for amalgamated digit count *)
}]; (* end of m loop *)
productchisquare =Sum[(productdigitcount[d] - benfordexpected[d])^2 / benfordexpected[d], {d,1,9}];
(*Print["Alpha = ", alphause, " and chisquare = ", productchisquare];*)
productchisquarealphalist = AppendTo[productchisquarealphalist, {alphause, productchisquare}];
amalgamatedchisquare =Sum[(amalgamateddigitcount[d] - n benfordexpected[d])^2 / (n benfordexpected[d]), {d,1,9}];
(*Print["Alpha = ", alphause, " and chisquare = ", amalgamatedchisquare];*)
amalgamatedchisquarealphalist = AppendTo[amalgamatedchisquarealphalist, {alphause, amalgamatedchisquare}];
Print["Amalgamated: Alpha = ", alphause, " and chisquare = ", amalgamatedchisquare, "; Product: Alpha = ", alphause, " and chisquare = ", productchisquare];
amalgamateddata = AppendTo[amalgamateddata,{n,alphause,numiter,amalgamatedchisquarealphalist}];
productdata = AppendTo[productdata, {n,alphause,numiter,productchisquarealphalist}];
}]; (* end of alphause loop *)
(*Print[ListPlot[output]];
Print[Histogram[uoutput, Automatic, "ProbabilityDensity"]];
Print[Histogram[voutput, Automatic, "ProbabilityDensity"]];*)
(*Print[Show[Histogram[uvoutput, Automatic, "ProbabilityDensity"],benfordplot]];*)
(*Print["Here are chisquare plots for products."];
Print[ListPlot[productchisquarealphalist]];
Print[ListPlot[productchisquarealphalist, Joined -> True]];
Print[Show[ListPlot[productchisquarealphalist],chibounds]];
Print["Here are chisquare plots for amalgamation."];
Print[ListPlot[amalgamatedchisquarealphalist]];
Print[ListPlot[amalgamatedchisquarealphalist, Joined -> True]];
Print[Show[ListPlot[amalgamatedchisquarealphalist],chibounds]];*)
Print["First is amalgamated chisquare, second is product chisquare"];
Print["Amalgamated data:"];
Print[amalgamateddata];
Print["Product data:"];
Print[productdata];
]; (* end of module *)

benfordcopulatestor[2,1,10,10, 10000,10000]

benfordcopulatestor[3,1,10,10, 10000,10000]

benfordcopulatestor[4,1,10,10, 10000,10000]

benfordcopulatestor[5,1,10,10, 10000,10000]

ALL ITEMS BELOW HERE ARE FOR DEBUGGING AND CAN BE IGNORED FOR NOW

KCinverselist[[8]]

KCinversefn[.4]

KC[t,alpha]

Plot[KC[t,2], {t,0,1}]

temp = {};
For[i = 1, i ≤ 400, i++, 
{
y = Random[];
temp = AppendTo[temp, {KCinversefn[y],y}]
}];

ListPlot[temp]