library(scatterplot3d)


n <- 1000
l1 <- 1
l2 <- 2
X1 <- rexp(n,l1)
X2 <- rexp(n,l2)
U1 <- pexp(X1,l1)
U2 <- pexp(X2,l2)
Indep <- matrix(c(X1,X2,U1*U2),ncol=3)
if(require(scatterplot3d))
par(mfrow=c(2,2))
fig1 <- scatterplot3d(Indep)

#comonotonic case (perfect positive dependence)
l1 <- 1
l2 <- 2
X1 <- rexp(n,l1)
X2 <- rexp(n,l2)
U1 <- pexp(X1,l1)
U2 <- pexp(X2,l2)
copula <- min(U1,U2)
Comono <- matrix(c(X1,X2,copula),ncol=3)
if(require(scatterplot3d))
scatterplot3d(Comono)


#Clayton example
l1 <- 1
l2 <- 2
tt <- -0.5
X1 <- rexp(n,l1)
X2 <- rexp(n,l2)
U1 <- pexp(X1,l1)
U2 <- pexp(X2,l2)
copula <- (U1^(-tt)+U2^(-tt)-1)^(-1/tt)
Clayton <- matrix(c(X1,X2,copula),ncol=3)
if(require(scatterplot3d))
scatterplot3d(Clayton)



library(copula)

norm.cop <- normalCopula(0.5)
norm.cop
Normal copula, dim. d = 2 
Dimension:  2 
Parameters:
  rho.1   = 0.5
## one d-vector =^= 1-row matrix, works too :
dCopula(c(0.5, 0.5), norm.cop)
[1] 1.154701
pCopula(c(0.5, 0.5), norm.cop)
[1] 0.3333333
u <- rCopula(n, norm.cop)
plot(u)

dCopula(u, norm.cop)
   [1]  2.77969757  1.20871444  0.20932848  1.29037083  1.56510294  1.07933251
   [7]  2.01281368  1.22193954  1.16659427  0.90265543  1.19016671  0.99659721
  [13]  0.97622012  2.73641208  1.22639224  0.99412900  0.67140321  1.42349100
  [19]  0.93746346  1.23571576  1.20295335  2.59149222  1.43061658  5.44325043
  [25]  1.22278117  1.22305469  1.07595197  1.14169071  1.28970487  1.24389353
  [31]  1.90095646  1.20278164  1.80034650  1.15652130  1.75258093  1.91676225
  [37]  1.43705223  0.95265574  0.85474863  0.89484636  1.36191922  1.10171733
  [43]  1.10929924  1.16572189  2.25552563  1.02299745  1.25003119  3.68299640
  [49]  1.18348099  1.38168953  1.22982680  2.28755868  1.19400369  1.13784403
  [55]  1.13863308  1.12088730  1.54289964  1.48849345  1.46917301  1.24315973
  [61]  1.33601080  1.32696709  0.74031502  0.92561433  3.59133622  1.16606359
  [67]  1.24412610  1.01552959  0.70953129  1.64469101  1.35796587  1.05858619
  [73]  0.88729423  1.39973000  0.96275942  1.28355039  1.18967068  1.20073049
  [79]  0.80328842  1.33250820  1.66768576  1.19790120  2.19757861  1.19350095
  [85]  1.74531801  1.15136540  1.16138241  1.47597167  1.12340433  0.86171156
  [91]  1.03800765  1.16024476  2.05768899  1.80377624  0.89803545  2.22014654
  [97]  0.14478608  0.94204502  2.00432739  0.83917187  0.66720632  2.13455761
 [103]  0.59172486  0.70688658  1.15872220  1.09582817  0.82239755  0.91878547
 [109]  2.01986054  1.17753966  0.22251638  0.88439148  2.28364799  0.62844439
 [115]  1.32650841  1.65407032  1.20628986  1.88681847  1.94995557  0.91149899
 [121]  0.58479678  1.63333289  1.03407626  0.77242331  1.07484549  1.16553848
 [127]  1.55247482  1.21497260  0.36500688  0.83473712  1.19543439  0.56096764
 [133]  1.23764004  3.20316879  0.30991853  1.63009176  0.95076855  0.70304148
 [139]  0.86194554  1.07401694  3.06574870  0.67397608  0.67042598  0.37925764
 [145]  1.70427258  1.15196786  1.05405530  1.52926528  0.96236022  0.43650111
 [151]  0.95455427  1.20777523  1.50403815  0.84903569  0.96939661  1.17063441
 [157]  1.03370930  0.76128946  1.12019147  4.39161497  1.22270285  1.16204602
 [163]  1.04495228  1.18075555  1.56804861  1.43408348  1.01321865  0.92852664
 [169]  1.36765911  2.41611968  0.67612013  1.13421259  1.09254274  1.20728878
 [175]  0.66705303  1.13798012  0.54994257  1.14481655  1.62648352  1.21439823
 [181]  1.15706973  0.99993102  1.18831791  1.24801826  0.15675655  0.48057101
 [187]  0.69959809  0.64900173  1.23268666  1.21738730  1.10920573  0.75926444
 [193]  2.29268496  1.70376671  1.69009471  1.32917028  1.30902065  1.64861437
 [199]  1.01383459  1.50541292  0.67321648  1.28395912  2.26221105  1.24528675
 [205]  1.22327188  1.23798099  1.26867053  2.86118257  0.34261695  1.36525136
 [211]  1.33631016  1.13292711  1.62019129  2.33302236  0.30204836  2.99968498
 [217]  1.33245742  0.38879664  0.79224865  1.19664401  0.85976997  1.02329175
 [223]  0.95455793  0.79907780  0.48094907  0.72150051  1.02051534  0.63821355
 [229]  0.98122402  1.36643479  1.27142103  0.93857066  2.66922502  0.85909059
 [235]  0.25537029  1.05257924  1.84425933  1.42773218  1.21371947  0.99505296
 [241]  1.38049101  1.11551126  1.66238035  2.45187080  0.58843709  1.12288446
 [247]  0.97801547  0.82386368  0.33245463  1.72085788  1.26763544  1.06284572
 [253]  1.13054819  0.85329526  2.04474154  0.65681587  1.04134876  1.15591392
 [259]  1.92197568  1.88392636  0.90951072  1.09058957  2.13407611  2.04183698
 [265]  1.35830248  1.12899579  1.10165048  0.90883498  1.15387985  1.36136026
 [271]  1.12181099  3.35736133  1.12776395  1.16892340  1.11845027  0.94533042
 [277]  1.31313782  1.15594797  0.91355060  1.54232593  1.18070868  1.23022214
 [283]  0.98969090  1.96298101  1.50595508  0.65362650  0.72435856  1.10207826
 [289]  1.29061946  1.06621181  1.17236302  1.07544541  1.07009494  1.90511868
 [295]  2.60394277  1.15921782  1.28303858  1.42367107  0.93619780  1.26638602
 [301]  1.34571543  1.24341813  0.94896304  1.03832101  1.04409714  1.21670710
 [307]  1.16127384  0.93816611  1.30941072  1.20347155  1.13232586  1.08703046
 [313]  1.00583631  1.25863680  0.95293937  1.30120852  1.31323829  0.86285549
 [319]  0.53796240  1.04044351  5.39053074  1.33516742  1.29760499  1.05931239
 [325]  1.33020724  1.21468366  1.13601199  0.41413547  0.94797690  1.14423227
 [331]  1.97911137  0.26534787  1.27979754  3.43228262  1.02075163  2.53237443
 [337]  0.50718840  1.15458328  0.53656119  1.66271709  1.10762582  1.17745269
 [343]  1.55708323  0.55375877  0.94902672  1.19323347  2.17716630  0.51924966
 [349]  2.31367856  1.02296674  1.24042654  1.17435522  1.12446732  0.83098403
 [355]  1.16989991  1.19729446  0.81583526  0.90966908  1.55693962  0.99139946
 [361]  5.75654385  1.34967108  1.20195288  1.11912215  0.77895366  1.41880252
 [367]  1.70608774  1.62697724  0.98749704  1.33075722  0.83323807  1.14615751
 [373]  1.46133133  1.21343305  1.17363303  0.06897765  0.69427011  1.14589311
 [379]  1.14998027  1.05096266  1.03834954  1.07417502  1.48194396  1.08109384
 [385]  2.09192854  3.25149220  1.16556812  2.69230930  1.32710400  1.44458675
 [391]  1.02673071  0.63199770  3.10134508  0.92782653  1.16022081  0.79371100
 [397]  1.53962863  0.51847944  1.21618277  1.76135546  1.15342672  1.57098669
 [403]  1.92223290  1.15295968  1.55175334  1.21398645  1.22443273  4.61478870
 [409]  1.57449510  1.80238758  1.15765457  1.13341943  0.88714394  1.31317044
 [415]  0.67147273  1.25376115  1.57346483  1.21287561  1.02476553  1.15378310
 [421]  0.95034698  1.07047249  0.95539797  1.29324410  0.54420970  2.65393942
 [427]  1.11016409  1.14372386  1.23808657  1.42246401  1.17514117  1.20384316
 [433]  1.14454022  1.40920150  1.05089355  0.38769241  1.19602189  1.18097047
 [439]  1.25552358  1.26469103  1.14969339  1.11469454  2.23163139  1.16332389
 [445]  1.22527054  1.42812384  1.54654058  1.75239728  1.13727532  0.52847277
 [451]  0.90096727  1.69298730  0.60155092  1.14881816  1.10238906  1.22987407
 [457]  1.22388708  0.52854817  0.96507655  0.79663544  1.51716610  6.11570195
 [463]  1.15313970  1.35837125  1.99059773  1.30607950  2.47187358  1.05361074
 [469]  0.98852150  0.44501172  1.14186615  7.42814503  2.16932630  0.76265133
 [475]  1.39838234  1.70015134  0.70060665  1.43482038  1.07756174  1.94124010
 [481]  1.12743406  0.89943128  1.15262024  1.25186217  1.14948907  1.38242908
 [487]  1.17931302  1.41276702  0.89336781  1.17642271  1.45255893  2.01585006
 [493]  2.50753901  1.33099707  1.25454972  0.84793948  1.23917277  0.96793071
 [499]  1.74415716  0.68823929  0.96369832  0.88948767  0.74567202  0.86995926
 [505]  1.32553493  1.41069478  0.25856673  0.70980408  1.54808792  0.34757684
 [511]  1.13835092  1.34250618  0.30166874  0.69504518  0.96829857  0.83459885
 [517]  0.84068831  0.73022544  0.89274322  2.81791317  1.13088721  1.21323042
 [523]  1.16584784  1.18581012  1.08787984  0.58633840  3.48777103  1.00114655
 [529]  1.79770008  0.84404564  1.07102541  2.04347071  2.73325926  1.08708227
 [535]  0.43234029  1.49399869  1.10216148  1.69330692  1.65305318  0.46718153
 [541]  1.36653964  2.53017610  1.35336012  1.25787684  1.44548260  1.01748054
 [547]  8.25025191  1.25033789  0.88587275  0.73115582  1.12790270  4.30516898
 [553]  1.17481860  1.13730792  1.15337417  0.72399646  0.84963903  1.40240698
 [559]  1.69333562  0.24274432  0.81549584  0.95706150  0.98548146  0.56396362
 [565]  1.06360544  0.95356000  1.41854526  1.18658830  1.37291817  1.11710809
 [571]  0.46782866  0.99323667  1.69627707  1.20617416  1.18155398  0.94179940
 [577]  1.02811930  1.46912989  0.65436537  2.05852823  1.17751319  0.75444040
 [583]  1.08652635  2.96154346  1.21388861  1.51037325  0.36768739  1.17142929
 [589]  0.91555094  1.23071195  0.70345551  0.62702504  0.90586597  0.53933924
 [595]  1.10380737  2.02795726  1.13795782  1.93574386  1.12347853  1.03594172
 [601]  1.10445666  3.75036715  3.34883335  0.94468324  1.02143827  1.23254982
 [607]  0.52126343  0.88750140  1.09086375  0.80061498  1.32445713  0.95407430
 [613]  0.84760660  1.14056102  1.43189346  1.14161991  0.91311068  1.00021690
 [619]  1.09775855  1.86297196  1.48963116  1.15780814  2.51809100  1.16907979
 [625]  0.75626501  0.71522034  3.05222939  0.82489071  1.22065042  0.85482614
 [631]  1.45330325  1.53986839  1.00442773  0.77600966  0.63287494  1.12565486
 [637]  1.25423772  1.38425076  1.54332197  0.62306299  0.41063991  0.91015715
 [643]  1.10221901  0.94546715  0.25901170  1.12573417  1.05968656  1.14809920
 [649]  1.74605755  0.47012424  2.57215187  1.26370347  0.80459553  0.62917479
 [655]  2.24190669  0.74867732  1.37837425  1.14869480  1.33271332  1.57255338
 [661]  1.87959744  0.93638668  1.17277734  1.47057980  1.94480934  1.00784428
 [667]  1.23049664  1.11669961  1.89018287  0.95315424  6.37323862  1.09609611
 [673]  1.16051604  0.68607620  1.15736421  2.41957917  1.13878111  1.67627089
 [679]  0.84351531  1.75301971  1.32125904  0.64651241  0.85848997  0.18924307
 [685]  0.78298833  0.61764163  0.96655072  0.46332040  1.63361309  1.12487865
 [691]  1.28623539  1.04173026  1.54752410  1.42878163  1.19074307  1.13998971
 [697]  1.20542625  0.92651049  1.56273126  0.45139407  0.35346607  1.29340922
 [703]  0.77029043  1.02333548  0.96338920  0.91655353  1.26569577  1.21566898
 [709]  1.70879089  0.73225550  0.88547259  1.35254126  0.74484369  1.09564286
 [715]  0.72813061  1.40949662  1.05144570  1.33332385  2.31868970  1.45552753
 [721]  0.28206203  1.27179695  1.36881910  1.36908619  1.07899178  1.75388958
 [727]  0.74669250  1.44818283  1.10164739  1.27613275  1.16244104  1.59372438
 [733]  0.89526635  1.11964108  0.94894856  1.12189219  1.32354587  5.30566347
 [739]  1.26711389  0.86154100  1.22982532  2.03326465  0.38679909  1.58926921
 [745]  0.50978853  0.98468401  1.16042887  1.35781709  0.35910367  1.17912163
 [751]  1.78258125  0.60164446  1.10991036  1.98202536  1.04045524  1.11448157
 [757]  1.14940157  1.51702639  0.54623888  1.01768853  1.31402546  1.07821097
 [763] 40.02691121  0.78595032  0.68504066  1.02896319  1.15330430  1.17580231
 [769]  1.12274518  1.22571587  1.69551731  1.53374105  0.94559218  1.22050095
 [775]  0.22452591  1.08540077  0.98559061  1.43481005  1.07490849  1.43708849
 [781]  1.49576885  2.22703015  0.98742654  0.55473988  1.09276241  1.11903718
 [787]  1.14628435  0.34091809  1.18152095  1.37293740  1.45463947  1.19612266
 [793]  1.12209784  0.35910734  1.46933191  0.95600456  1.24444336  0.89836106
 [799]  1.44766540  0.87240328  1.08640789  1.22342806  1.76983486  0.84120501
 [805]  0.88378475  1.50248176  1.21978664  1.01132628  1.07137802  1.02399236
 [811]  1.10633382  1.14785883  2.24722590  1.13441216  1.13750886  0.71934651
 [817]  0.96460060  1.15383359  1.39185778  1.14748764  1.16926565  0.58309954
 [823]  2.34263487  0.59189470  0.81425248  0.59626116  2.87651602  1.39195972
 [829]  1.14137356  1.20793511  1.21399473  1.02031037  1.43288425  0.96296420
 [835]  1.41834136  1.12543593  0.99283432  1.15172711  1.11098981  1.58146949
 [841]  0.75764233  0.85230806  1.28811162  0.84879595  1.16460250  1.10624694
 [847]  2.79057090  0.67694484  1.08975014  2.78574999  0.58239530  0.55692645
 [853]  1.06661852  1.15022260  2.79103018  0.96672182  0.53037565  1.19260523
 [859]  1.15643833  0.94956020  0.24982044  0.73241246  0.94414474  0.65456630
 [865]  5.48789134  0.57018887  0.56577228  2.09124823  0.75130671  1.14029797
 [871]  3.78254886  1.03005890  5.11991341  0.85558797  1.61587502  0.89429592
 [877]  1.06520106  0.55602512  1.11889703  0.96072558  1.61458143  0.94784168
 [883]  1.19075540  0.92810840  0.90113948  1.33170260  1.13292956  1.11069386
 [889]  0.63786347  0.95461738  1.61887774  0.74989472  1.17160062  1.10846868
 [895]  1.30908603  2.29212823  1.13135557  1.27758979  0.32436977  1.14529097
 [901]  1.02868882  1.41071280  1.02231822  1.10147726  1.66662788  1.08503243
 [907]  0.95587731  0.36184906  1.11541868  1.30172373  0.76699755  1.33170476
 [913]  1.18305948  1.01001822  0.53402130  1.17005252  1.62484911  0.94509220
 [919]  1.11056033  0.53075402  2.03214229  1.13107488  0.58328052  1.35696463
 [925]  1.28157062  1.30303696  0.98868129  1.47564303  1.22571535  1.85392303
 [931]  1.18046378  0.96586157  1.13705851  1.16626104  1.49545089  1.22889375
 [937]  1.56844771  1.42556013  0.44913510  0.53126749  1.23762484  1.23063798
 [943]  1.14293027  1.18896690  1.07161412  1.02383070  1.13184112  1.68560486
 [949]  1.57803515  3.31562651  1.99001943  3.78215614  1.46253902  6.66363176
 [955]  1.22727976  1.30984842  1.15302096  1.11442681  0.44164364  1.43550294
 [961]  1.59960708  0.73505490  0.31564236  1.15017797  0.92992038  1.21951342
 [967]  1.02965959  1.32363031  0.97020922  1.36824838  0.68304922  0.88261554
 [973]  1.33590775  1.04487560  1.24495728  1.38559289  1.38887148  0.54411820
 [979]  0.64330269  1.15259685  1.07462465  0.75123138  0.95011504  0.90340750
 [985]  1.44389796  1.46118911  1.68321438  1.00767666  1.01629862  0.82479083
 [991]  1.46469671  0.43750979  1.08662326  0.99372715  1.28795762  1.31395328
 [997]  1.17416305  0.98995827  1.11176173  1.09006134
pCopula(u, norm.cop)
   [1] 9.055457e-01 1.493363e-01 2.355613e-01 7.228710e-01 7.352776e-01 2.332423e-01
   [7] 8.377604e-01 5.749740e-01 2.339516e-01 4.837571e-01 5.616725e-01 2.921658e-01
  [13] 1.377766e-01 9.048727e-01 5.137597e-01 4.589918e-01 1.995227e-01 7.103918e-01
  [19] 5.516266e-01 3.522164e-02 1.777019e-01 1.500785e-03 6.734840e-01 9.691848e-01
  [25] 6.263944e-01 7.149573e-02 2.720538e-01 3.242186e-01 6.227376e-01 5.410591e-01
  [31] 6.466029e-03 5.700820e-01 3.402444e-02 2.534737e-01 1.464094e-04 3.543043e-02
  [37] 9.227036e-02 6.069309e-01 5.460751e-02 5.171632e-01 8.434108e-02 1.969748e-01
  [43] 3.832355e-01 4.064610e-01 7.549764e-03 3.190567e-01 1.616472e-01 9.373271e-01
  [49] 2.255126e-01 3.989479e-02 1.783831e-01 2.188172e-02 2.010047e-01 5.484927e-01
  [55] 5.803872e-01 1.861342e-01 6.999784e-02 8.073033e-02 6.950501e-01 6.103390e-01
  [61] 6.818546e-01 1.019740e-01 2.494342e-01 2.303892e-01 3.962215e-03 2.630668e-01
  [67] 5.359523e-01 5.194565e-01 2.249219e-01 5.849041e-02 7.428611e-01 2.477612e-01
  [73] 2.409297e-02 6.843860e-01 5.201255e-01 6.284164e-02 2.049779e-01 6.194372e-01
  [79] 4.056294e-01 6.124044e-01 7.883649e-01 5.183306e-01 2.029667e-02 1.324520e-01
  [85] 4.769130e-02 3.329489e-01 4.796234e-01 6.942223e-01 2.180035e-01 3.717169e-01
  [91] 4.250314e-01 8.918427e-02 2.079043e-02 2.083841e-02 4.953066e-01 8.626168e-01
  [97] 3.299125e-02 1.422449e-01 8.379182e-01 5.196431e-01 1.048168e-01 1.902249e-02
 [103] 2.325424e-01 2.622439e-02 4.313860e-01 4.163925e-01 3.689639e-01 1.484290e-01
 [109] 8.368196e-01 1.701413e-01 1.774088e-01 4.070815e-01 1.967001e-02 4.122166e-01
 [115] 6.825298e-01 7.663779e-01 4.941852e-01 8.301594e-01 8.298732e-01 4.881628e-01
 [121] 4.987142e-01 7.568871e-01 2.922170e-01 2.571692e-01 3.143447e-01 6.762228e-01
 [127] 1.930725e-02 7.077749e-01 2.613593e-01 2.015622e-01 5.410653e-01 1.108609e-01
 [133] 7.070732e-01 7.907563e-03 2.829044e-01 7.801689e-01 6.059738e-02 2.691752e-01
 [139] 1.827085e-02 3.816795e-01 6.918478e-03 5.481135e-02 3.086990e-01 4.050850e-01
 [145] 5.222952e-02 2.494163e-01 3.435692e-01 4.845439e-02 6.390131e-02 1.870173e-01
 [151] 4.520129e-01 6.423510e-01 1.631948e-02 4.216767e-01 4.416639e-02 5.587092e-01
 [157] 3.549312e-01 2.196324e-01 1.441332e-01 9.583146e-01 1.857017e-01 4.046343e-01
 [163] 5.176573e-01 5.183896e-02 5.733827e-02 6.817495e-01 2.919662e-01 2.947894e-01
 [169] 9.382784e-02 8.819572e-01 1.440025e-01 4.441585e-01 5.315707e-01 5.108237e-01
 [175] 2.707750e-01 2.016128e-01 1.845474e-02 6.502980e-01 3.714441e-02 8.003532e-01
 [181] 3.022725e-01 2.012672e-01 2.255031e-01 1.633565e-01 2.741236e-01 3.578211e-03
 [187] 2.220626e-01 3.334660e-03 9.824954e-02 5.693390e-01 2.185250e-01 5.491511e-02
 [193] 8.704681e-01 7.724294e-01 4.353263e-02 6.442215e-01 1.278367e-01 5.262796e-02
 [199] 2.049885e-01 6.553428e-02 6.278311e-01 1.417555e-01 1.484408e-02 5.604793e-01
 [205] 8.125074e-02 1.714481e-01 1.283348e-01 1.128403e-02 9.951612e-02 6.992086e-01
 [211] 1.031776e-01 1.058609e-01 7.644866e-01 1.209296e-02 1.197053e-01 1.060128e-02
 [217] 6.138435e-01 8.964219e-02 4.112881e-02 2.118272e-01 3.546806e-01 1.181586e-01
 [223] 6.043986e-01 1.959923e-01 3.695215e-01 3.153445e-01 3.687185e-01 2.630755e-01
 [229] 2.206039e-01 1.046270e-01 6.179394e-01 2.326297e-01 8.995848e-01 2.282749e-01
 [235] 2.959208e-01 4.536267e-01 8.211156e-01 7.534729e-01 5.116651e-01 3.395069e-01
 [241] 5.267565e-02 3.684796e-01 4.165193e-02 1.187200e-02 4.557409e-01 1.319839e-01
 [247] 3.745357e-01 3.877317e-01 3.016496e-01 7.812023e-01 9.705809e-02 9.122965e-02
 [253] 3.101101e-01 4.848799e-02 1.262608e-02 1.143774e-01 5.060864e-02 2.617269e-01
 [259] 3.352816e-02 3.830280e-02 2.736699e-01 4.128628e-01 1.430632e-02 8.452722e-01
 [265] 6.396307e-01 3.783605e-01 4.954854e-01 3.317129e-01 3.324630e-01 1.036068e-01
 [271] 3.391750e-01 7.923962e-03 8.458790e-02 5.213559e-01 1.169186e-01 3.023620e-01
 [277] 1.167747e-01 5.081516e-01 6.139553e-02 7.218570e-01 4.682827e-01 6.218797e-02
 [283] 5.493051e-01 2.369594e-02 7.751532e-02 2.463304e-01 2.274959e-02 1.127134e-01
 [289] 6.480739e-01 6.479930e-02 3.793333e-02 4.745590e-01 2.669105e-01 3.631241e-02
 [295] 1.419523e-02 2.912515e-01 5.871810e-01 3.436990e-02 1.686786e-01 1.468065e-01
 [301] 1.159824e-01 5.428409e-01 2.779658e-02 3.091206e-01 1.947443e-01 1.687357e-01
 [307] 2.041200e-01 4.495089e-01 1.335180e-01 1.975268e-01 4.420369e-01 6.242902e-01
 [313] 4.599115e-01 1.442122e-01 4.754182e-01 1.287609e-01 7.901364e-02 3.165881e-01
 [319] 2.548971e-01 6.071536e-01 9.644722e-01 7.552146e-01 1.030736e-01 2.371525e-01
 [325] 6.633268e-01 1.445769e-01 5.446561e-01 1.263357e-01 5.765116e-01 1.799115e-01
 [331] 1.291017e-02 2.276199e-01 6.327153e-01 9.293639e-01 1.685091e-01 8.915386e-01
 [337] 1.294113e-01 3.164167e-01 2.927780e-01 5.632391e-02 2.838103e-01 1.364905e-01
 [343] 2.979560e-02 6.035494e-01 4.813395e-01 2.187901e-01 1.767098e-02 1.102514e-01
 [349] 2.059030e-02 3.301372e-01 1.728390e-01 1.755447e-01 3.924656e-02 3.448616e-01
 [355] 8.011003e-02 1.197086e-01 4.449732e-01 3.715305e-01 5.501474e-02 3.393185e-02
 [361] 9.637053e-01 7.201114e-01 1.990462e-01 3.099584e-01 3.934987e-01 6.655071e-01
 [367] 8.354900e-01 7.504714e-01 2.757545e-02 6.363397e-01 2.770169e-01 5.835349e-01
 [373] 7.300744e-01 6.127089e-01 4.251668e-01 1.691991e-02 1.392705e-01 4.731640e-01
 [379] 3.446192e-01 2.035471e-01 3.555109e-01 2.988464e-01 7.125722e-01 4.215151e-01
 [385] 8.478535e-01 9.301548e-01 4.025257e-01 9.024425e-01 1.117447e-01 8.107250e-02
 [391] 4.928706e-01 2.680799e-01 9.593563e-03 6.206732e-01 1.677678e-01 1.362459e-01
 [397] 1.275220e-02 1.554743e-02 1.416273e-01 1.969873e-02 4.599176e-01 4.930914e-02
 [403] 3.023137e-02 2.916425e-01 7.276863e-01 1.707137e-01 1.468563e-01 9.620301e-01
 [409] 6.680428e-02 4.350646e-02 2.966528e-01 2.912693e-01 1.280365e-01 1.202328e-01
 [415] 4.191492e-01 6.930493e-01 3.992693e-02 5.094501e-01 3.435941e-01 6.389841e-01
 [421] 1.977921e-01 2.343297e-01 5.522267e-01 6.807014e-01 1.099659e-01 6.310798e-03
 [427] 3.297476e-01 3.328104e-01 6.589120e-01 7.674737e-02 4.343931e-01 9.378852e-02
 [433] 1.739703e-02 3.306407e-02 2.464828e-01 1.934448e-03 4.778930e-01 4.687767e-01
 [439] 1.224113e-01 6.548988e-01 4.141875e-01 3.878122e-01 2.102211e-02 4.211005e-01
 [445] 9.129762e-02 2.343484e-02 7.235581e-01 7.853807e-01 3.447447e-01 4.214318e-01
 [451] 2.703595e-01 5.311303e-02 4.543310e-03 4.790646e-01 3.997125e-01 6.863295e-02
 [457] 5.488174e-02 3.037116e-01 2.369356e-02 1.363082e-01 7.395935e-01 9.685871e-01
 [463] 3.871605e-01 6.812199e-01 2.922733e-02 1.288161e-01 8.868138e-01 5.270316e-01
 [469] 5.313999e-01 4.455367e-01 5.014624e-01 9.828841e-01 2.021192e-02 1.741246e-01
 [475] 1.003603e-01 5.203528e-02 5.743589e-02 7.003494e-01 5.357029e-01 8.349316e-01
 [481] 5.658955e-01 4.059563e-01 1.708686e-01 5.630973e-01 6.088057e-01 6.323915e-02
 [487] 2.322438e-01 7.307802e-01 2.112289e-01 4.627428e-01 8.334613e-02 2.144778e-02
 [493] 8.896429e-01 1.055349e-01 5.478401e-01 3.011096e-02 5.310703e-01 9.144564e-02
 [499] 8.824727e-03 1.623863e-01 3.128845e-01 2.491212e-01 8.568781e-02 2.558525e-02
 [505] 6.477378e-01 6.079709e-02 6.323955e-02 1.764710e-01 4.424064e-02 1.341160e-01
 [511] 9.838236e-02 7.201619e-01 4.207961e-02 3.107713e-01 7.741335e-02 3.249970e-01
 [517] 4.642474e-01 2.360051e-01 3.818523e-01 9.096940e-01 5.839039e-01 1.379807e-01
 [523] 4.100254e-01 4.519102e-01 3.167486e-01 5.620942e-02 9.380157e-01 6.872716e-02
 [529] 3.734912e-03 5.574724e-01 3.773192e-01 8.407085e-01 6.998583e-03 2.872673e-01
 [535] 1.519669e-01 7.017931e-01 4.837395e-01 2.600763e-02 7.822203e-01 2.504974e-01
 [541] 6.370802e-01 1.311123e-02 1.166500e-01 5.494985e-01 7.527242e-01 5.906405e-01
 [547] 6.532581e-04 5.417189e-01 3.352854e-01 3.060244e-01 1.310286e-01 9.571986e-01
 [553] 1.003619e-01 3.124364e-01 3.070791e-01 1.195358e-01 4.192472e-01 6.692653e-01
 [559] 2.195682e-02 3.439369e-02 3.533449e-01 2.362257e-01 2.839779e-01 1.182349e-01
 [565] 1.552976e-01 1.201724e-01 6.655536e-01 4.732873e-01 6.769330e-01 3.709768e-01
 [571] 3.097858e-01 4.656974e-01 7.775886e-01 5.211705e-01 1.754998e-01 2.729278e-02
 [577] 1.677669e-01 7.785553e-02 3.176802e-01 1.789924e-02 4.992599e-01 1.810612e-01
 [583] 3.743114e-01 4.299654e-03 5.760945e-01 7.215573e-01 2.482256e-01 4.213773e-01
 [589] 3.256556e-01 1.681888e-01 5.481356e-01 2.779828e-01 2.254037e-01 2.839569e-01
 [595] 4.656757e-01 8.485011e-01 3.715099e-01 8.228101e-01 5.065756e-01 5.589764e-01
 [601] 3.389544e-01 4.022050e-03 9.311052e-01 4.066529e-01 1.482923e-01 5.778827e-02
 [607] 2.967075e-01 4.179490e-01 3.235503e-01 3.742678e-01 8.880590e-02 3.272667e-01
 [613] 2.272470e-01 3.037672e-01 8.547365e-02 3.768896e-01 2.062949e-01 3.725417e-01
 [619] 2.006588e-01 8.116245e-01 7.010492e-01 2.824625e-01 2.617002e-03 4.932602e-01
 [625] 3.990552e-01 5.776035e-02 9.114638e-03 2.428396e-01 1.844131e-01 9.911340e-02
 [631] 6.665240e-02 4.809352e-02 3.467324e-01 4.581250e-01 9.312133e-02 3.020336e-01
 [637] 1.353608e-01 5.097727e-02 7.229774e-01 6.023802e-01 2.111887e-01 3.692955e-01
 [643] 2.277264e-01 6.372935e-01 4.139836e-02 1.062717e-01 2.478863e-01 4.002319e-01
 [649] 7.874901e-01 3.570306e-01 1.512700e-02 5.615482e-01 9.925400e-02 6.615239e-02
 [655] 2.298588e-02 2.525389e-01 6.530936e-01 5.993899e-01 1.227453e-01 7.348700e-01
 [661] 2.967527e-02 1.700588e-02 4.247140e-01 1.838559e-02 2.885298e-02 3.303703e-01
 [667] 1.810991e-01 5.112987e-01 8.142059e-01 2.357738e-01 9.720410e-01 3.472161e-01
 [673] 1.279913e-01 2.659982e-01 3.686023e-01 1.533501e-02 4.604261e-01 7.701586e-01
 [679] 2.114372e-01 8.104690e-01 6.345434e-01 4.847291e-02 3.765175e-01 7.124445e-02
 [685] 3.010837e-01 6.257096e-02 2.496068e-01 2.219050e-01 7.555209e-01 5.073248e-01
 [691] 6.193475e-01 1.422605e-01 5.056857e-02 8.037646e-02 4.766097e-01 3.046036e-01
 [697] 5.392758e-01 1.897024e-01 6.264857e-03 3.015740e-01 1.592712e-01 1.413460e-01
 [703] 1.058969e-01 7.485109e-02 8.906232e-02 3.811374e-01 7.367366e-02 5.098653e-02
 [709] 7.863639e-01 3.726586e-01 1.076684e-01 1.078451e-01 1.623909e-01 3.822444e-01
 [715] 6.479468e-02 6.677946e-01 2.045723e-01 6.660068e-01 8.752853e-01 6.841454e-01
 [721] 5.648878e-02 6.842253e-01 9.441947e-02 7.647907e-02 2.606034e-01 7.883777e-01
 [727] 4.964722e-01 6.804647e-01 1.077720e-01 5.692224e-01 2.347397e-01 6.481835e-02
 [733] 6.605966e-01 2.695486e-01 5.809842e-01 4.738044e-01 6.625741e-01 9.698292e-01
 [739] 5.595185e-01 2.037110e-02 5.977044e-01 8.475226e-04 4.680995e-02 5.173977e-02
 [745] 7.822199e-03 4.540161e-01 4.060282e-01 6.225905e-02 1.583022e-01 4.700098e-01
 [751] 4.013632e-02 2.790382e-01 5.716533e-01 8.310345e-01 6.078468e-01 2.569309e-01
 [757] 3.711374e-01 7.138537e-01 1.626437e-01 2.641310e-01 6.233014e-01 1.537837e-01
 [763] 5.882776e-06 1.686297e-01 2.524873e-01 1.255524e-01 3.482305e-01 4.994785e-01
 [769] 4.510950e-01 5.440924e-01 3.147012e-02 7.454850e-01 1.647777e-01 1.730952e-01
 [775] 3.406074e-02 5.985457e-01 5.951989e-01 9.288403e-02 5.217923e-01 9.094643e-02
 [781] 8.038541e-02 2.370248e-02 1.514892e-01 4.768854e-02 3.307831e-01 1.844955e-01
 [787] 3.038722e-01 1.943694e-01 5.252698e-01 1.044882e-01 7.424818e-02 4.711027e-01
 [793] 4.150524e-01 1.533301e-02 8.149952e-02 3.444645e-01 5.374595e-01 3.056263e-01
 [799] 6.917869e-01 1.761013e-01 3.744695e-01 5.115004e-01 8.220874e-01 8.938884e-02
 [805] 5.794138e-01 7.431004e-02 1.145950e-01 8.513084e-02 6.943690e-01 2.434026e-01
 [811] 3.205387e-01 5.422082e-01 2.311071e-02 3.278570e-01 6.734465e-02 1.726096e-01
 [817] 6.703786e-02 2.825556e-01 6.597684e-01 2.414635e-01 6.232228e-01 9.625988e-02
 [823] 8.754269e-01 3.263360e-01 2.760126e-01 2.980436e-01 9.102001e-01 6.572958e-01
 [829] 1.683669e-01 1.248572e-01 7.588888e-02 1.302142e-01 6.937156e-01 6.748957e-02
 [835] 7.222778e-01 6.019653e-02 2.752867e-01 3.014193e-01 2.740876e-01 2.450780e-02
 [841] 2.110247e-02 3.666045e-02 6.264171e-01 2.244692e-01 1.504642e-01 3.661342e-01
 [847] 9.078452e-01 4.253740e-02 3.307359e-01 8.458022e-03 2.227062e-02 6.337658e-02
 [853] 1.723266e-01 1.390392e-01 9.058631e-01 1.942725e-01 1.397242e-01 1.507633e-01
 [859] 3.833109e-01 4.207211e-01 1.118220e-01 3.139132e-01 1.461812e-01 2.188298e-02
 [865] 9.669004e-01 4.052197e-01 4.953540e-01 2.833462e-02 5.047957e-01 4.982985e-01
 [871] 1.374568e-03 5.888138e-01 1.992903e-03 3.332768e-01 7.813947e-01 5.179981e-01
 [877] 3.354220e-01 4.670286e-01 2.275425e-01 1.271431e-01 5.919505e-02 1.264364e-01
 [883] 2.199576e-01 4.044867e-01 2.988418e-01 9.567206e-03 3.054507e-01 5.088395e-01
 [889] 7.874430e-02 2.624189e-01 5.773221e-02 2.100609e-01 4.333832e-01 2.977724e-01
 [895] 6.132885e-01 8.715129e-01 4.288529e-01 8.353010e-02 3.153712e-02 3.337630e-01
 [901] 4.876363e-01 7.556262e-01 3.506534e-01 4.335203e-01 5.440659e-02 3.789065e-01
 [907] 4.821965e-01 2.036158e-01 2.849034e-01 8.846015e-02 1.919689e-01 9.326952e-02
 [913] 4.504232e-01 2.121128e-01 3.632859e-01 2.360357e-01 7.511624e-01 1.741467e-01
 [919] 1.878145e-01 8.438195e-02 2.513571e-02 3.399685e-01 7.221796e-02 1.613823e-02
 [925] 1.446398e-01 6.031716e-01 3.655398e-01 7.289484e-01 1.846199e-01 8.177293e-01
 [931] 4.410115e-01 4.706873e-01 2.023209e-01 1.534971e-01 7.103954e-01 4.232693e-02
 [937] 4.047788e-02 7.055048e-01 2.100562e-01 1.743288e-01 5.445693e-01 1.424265e-01
 [943] 3.834866e-01 5.148969e-01 5.337005e-01 4.238626e-01 4.861040e-02 5.341801e-02
 [949] 6.071442e-02 6.045838e-03 8.352417e-01 5.460328e-03 6.307538e-02 8.855054e-04
 [955] 5.336287e-01 5.954733e-01 3.176198e-01 5.233299e-01 9.859179e-02 6.823492e-01
 [961] 7.465246e-01 3.316057e-01 1.173287e-01 3.708756e-01 4.281456e-01 1.601084e-01
 [967] 2.960062e-01 1.082114e-01 5.813804e-01 7.440451e-02 1.294801e-01 5.046213e-01
 [973] 6.228118e-01 1.332760e-01 6.514171e-01 9.808488e-02 8.809372e-02 6.352028e-02
 [979] 1.192136e-01 1.904173e-01 5.477199e-01 1.360254e-01 4.028805e-01 4.994055e-01
 [985] 7.276962e-01 3.305985e-02 7.703350e-01 2.151704e-01 3.482507e-01 8.506260e-02
 [991] 3.179623e-02 2.114379e-01 4.578711e-01 6.491603e-01 9.286277e-02 1.003440e-01
 [997] 4.616247e-01 3.388365e-01 5.428122e-01 5.968400e-01
persp (norm.cop, dCopula)
contour(norm.cop, pCopula)

## a 3-dimensional normal copula
u <- rCopula(1000, normalCopula(0.5, dim = 3))
if(require(scatterplot3d))
scatterplot3d(u)

 ### data location
mydata <-read.csv(file="/Users/mmailhot/Dropbox/Mac (2)/Desktop/DataCopula.csv",header = TRUE)
# Estimate x  gamma distribution parameters and visually compare simulated vs observed data
x_mean <- mean(mydata$x)
x_var <- var(mydata$x)
x_rate <- x_mean / x_var
x_shape <- ( (x_mean)^2 ) / x_var

hist(mydata$x, breaks = 20, col = "green", density = 20)
hist(rgamma( nrow(mydata), rate = x_rate, shape = x_shape), breaks = 20,col = "blue", add = T, density = 20, angle = -45)


# Estimate y gamma distribution parameters and visually compare simulated vs observed data
y_mean <- mean(mydata$y)
y_var <- var(mydata$y)
y_rate <- y_mean / y_var
y_shape <- ( (y_mean)^2 ) / y_var
hist(mydata$y, breaks = 20, col = "green", density = 20)
hist(rgamma(nrow(mydata), rate = y_rate, shape = y_shape), breaks = 20, col = "blue", add = T, density = 20, angle = -45)



# Measure association using Kendall's Tau
cor(mydata, method = "kendall")
           X          x          y
X 1.00000000 0.01748549 0.02908509
x 0.01748549 1.00000000 0.44954955
y 0.02908509 0.44954955 1.00000000
library(copula)
mydata <- matrix(c(mydata$x,mydata$y),ncol = 2)
# Estimate copula parameters
cop_model <- claytonCopula(dim = 2)
m <- pobs(as.matrix(mydata))
fit <- fitCopula(cop_model, m, method = 'ml')
coef(fit)
   alpha 
1.646685 
##    param 
## 1.482168 

# Check Kendall's tau value for the Clayton copula with theta = 1.48
tau(claytonCopula(param = 1.646685))
[1] 0.4515567
## [1] 0.4515567


# Build the bivariate distribution
my_dist <- mvdc(claytonCopula(param = 1.48, dim = 2), margins = c("gamma","gamma"), paramMargins = list(list(shape = x_shape, rate = x_rate), list(shape = y_shape, rate = y_rate)))

# Generate random sample observations from the multivariate distribution
v <- rMvdc(5000, my_dist)
# Compute the density
pdf_mvd <- dMvdc(v, my_dist)
# Compute the CDF
cdf_mvd <- pMvdc(v, my_dist)

install.packages("scatterplot3d") # Install
trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.4/scatterplot3d_0.3-44.tgz'
Content type 'application/x-gzip' length 348854 bytes (340 KB)
==================================================
downloaded 340 KB

The downloaded binary packages are in
    /var/folders/xq/4d6jckb55r1fl1nqmql87cznpwjmt5/T//RtmprohrGQ/downloaded_packages
library("scatterplot3d") # load
# 3D plain scatterplot of the generated bivariate distribution
par(mfrow = c(1, 2))

scatterplot3d(v[,1],v[,2], pdf_mvd, color="red", main="Density", xlab = "u1", ylab="u2", zlab="pMvdc",pch=".")
scatterplot3d(v[,1],v[,2], cdf_mvd, color="red", main="CDF", xlab = "u1", ylab="u2", zlab="pMvdc",pch=".")

persp(my_dist, dMvdc, xlim = c(-4, 4), ylim=c(0, 2), main = "Density")
contour(my_dist, dMvdc, xlim = c(-4, 4), ylim=c(0, 2), main = "Contour plot")

persp(my_dist, pMvdc, xlim = c(-4, 4), ylim=c(0, 2), main = "CDF")
contour(my_dist, pMvdc, xlim = c(-4, 4), ylim=c(0, 2), main = "Contour plot")

LS0tCnRpdGxlOiAiQUNUVS00NTcgKE1hc3QgNzI0KSBSaXNrIFRoZW9yeSIKYXV0aG9yOiAiUHJvZi4gTWVsaW5hIE1haWxob3QiCmRhdGU6ICIyMDI1LTAzLTEyIgpvdXRwdXQ6CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAotLS0KCgoKYGBge3J9CmxpYnJhcnkoc2NhdHRlcnBsb3QzZCkKCgpuIDwtIDEwMDAKbDEgPC0gMQpsMiA8LSAyClgxIDwtIHJleHAobixsMSkKWDIgPC0gcmV4cChuLGwyKQpVMSA8LSBwZXhwKFgxLGwxKQpVMiA8LSBwZXhwKFgyLGwyKQpJbmRlcCA8LSBtYXRyaXgoYyhYMSxYMixVMSpVMiksbmNvbD0zKQppZihyZXF1aXJlKHNjYXR0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