[Vinecopula-commits] r90 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
So Mär 29 19:29:19 CEST 2015


Author: ulf
Date: 2015-03-29 19:29:18 +0200 (Sun, 29 Mar 2015)
New Revision: 90

Modified:
   pkg/src/deriv.c
   pkg/src/deriv2.c
   pkg/src/evCopula.c
Log:
some more comments of C-functions

Modified: pkg/src/deriv.c
===================================================================
--- pkg/src/deriv.c	2015-03-29 16:07:38 UTC (rev 89)
+++ pkg/src/deriv.c	2015-03-29 17:29:18 UTC (rev 90)
@@ -129,6 +129,10 @@
 // Reference: Schepsmeier and Stoeber (2012, 2013)
 /////////////////////////////////////////////////////////////
 
+// the stepwise calculation is due to performance and numerical stability reasons (t1,t2,...)
+// for Gauss some of the step can be found in the reference
+// for the archimedean copulas one gets this optimization with Maple
+
 void diffPDF(double* u, double* v, int* n, double* param, int* copula, double* out)
 {
 	int j;
@@ -145,7 +149,7 @@
 		{
 			out[j]=0;
 		}
-		else if(*copula==1)		// gauss
+		else if(*copula==1)		// gauss, formula see reference
 		{
 			t1 = qnorm(u[j],0.0,1.0,1,0); 
 			t2 = qnorm(v[j],0.0,1.0,1,0);
@@ -161,7 +165,7 @@
 			out[j] = (-2.0*(theta*t3-t1*t2)*t9-t15/(t8*t8)*theta)*t22/t24+t22/t24/t8*theta;
 		}
 		// t-copula is separate; very complicated
-		else if(*copula==3)
+		else if(*copula==3)	// the archimedean copula derivatives are derived by Maple 
 		{
 			t1 = u[j]*v[j];
 			t2 = -theta-1.0;

Modified: pkg/src/deriv2.c
===================================================================
--- pkg/src/deriv2.c	2015-03-29 16:07:38 UTC (rev 89)
+++ pkg/src/deriv2.c	2015-03-29 17:29:18 UTC (rev 90)
@@ -24,7 +24,16 @@
 ////////////////////////////////////////////////////////////////////
 //
 // 2. Ableitung von c nach dem Parameter
+// Second derivative of the bivariate copula density with respect to the parameter
 //
+// u,v			copula arguments (data vectors)
+// n			length of u,v 
+// param		parameter vector (par,par2)
+// copula		copula family (1,2,3,4,5,6)
+//
+// Output:
+// out			derivative
+//
 ////////////////////////////////////////////////////////////////////
 
 void diff2PDF_mod(double* u, double* v, int* n, double* param, int* copula, double* out)
@@ -108,6 +117,25 @@
 }
 
 
+////////////////////////////////////////////////////////////////////
+//
+// 2. Ableitung von c nach dem Parameter
+// Second derivative of the bivariate copula density with respect to the parameter (main function)
+//
+// u,v			copula arguments (data vectors)
+// n			length of u,v 
+// param		parameter vector (par,par2)
+// copula		copula family (1,2,3,4,5,6)
+//
+// Output:
+// out			derivative
+//
+// Reference: Schepsmeier and Stoeber (2012, 2013)
+////////////////////////////////////////////////////////////////////
+
+// the structure is the same as for the first derivative
+// see also the comments for the first derivatives
+
 void diff2PDF(double* u, double* v, int* n, double* param, int* copula, double* out)
 {
 	int j;
@@ -325,6 +353,7 @@
 ///////////////////////////////////////////////////////////////////
 //
 // 2. Ableitung von c nach u (2mal)
+// second derivative with respect to u (two times)
 //
 ////////////////////////////////////////////////////////////////////
 
@@ -400,8 +429,10 @@
   free(nparam);
 }
 
-
+//////////////////////////////////////////
 // Ableitung von c nach v (2 mal)
+// Second derivative with respect to v (two times)
+//////////////////////////////////////////
 
 void diff2PDF_v_mod(double* u, double* v, int* n, double* param, int* copula, double* out)
 {
@@ -477,6 +508,11 @@
 
 
 
+////////////////////////////////
+// Main function to calculate the derivative with respect to u
+////////////////////////////////
+
+
 void diff2PDF_u(double* u, double* v, int* n, double* param, int* copula, double* out)
 {
 	int j;
@@ -498,9 +534,9 @@
 			double nu=0, c=0, diffc=0;
 			int k=1;
 
-			LL(copula, &k, &u[j], &v[j], &theta, &nu, &c);
+			LL(copula, &k, &u[j], &v[j], &theta, &nu, &c);	// one needs the density
 			c=exp(c);
-			diffPDF_u_mod(&u[j],&v[j],&k,param,copula,&diffc);
+			diffPDF_u_mod(&u[j],&v[j],&k,param,copula,&diffc); // and also the derivative with respect to u (first derivative)
 
 			t1=qnorm(u[j],0.0,1.0,1,0);
 			t2=qnorm(v[j],0.0,1.0,1,0);
@@ -515,7 +551,7 @@
 		else if(*copula==2)
 		{
 			int k=1;
-			diff2PDF_u_tCopula_new(&u[j], &v[j], &k, param, copula, &out[j]);
+			diff2PDF_u_tCopula_new(&u[j], &v[j], &k, param, copula, &out[j]);  // special function for t-copula
 		}
 		else if(*copula==3)
 		{
@@ -682,6 +718,7 @@
 ///////////////////////////////////////////////////////////////////
 //
 // 2. Ableitung von c nach u und v 
+// Second derivative with respect to u and v (first and second argument)
 //
 ////////////////////////////////////////////////////////////////////
 
@@ -957,7 +994,8 @@
 
 ///////////////////////////////////////////////////////////////////
 //
-// 2. Ableitung von c nach par und u 
+// 2. Ableitung von c nach par und u
+// Second derivative with respect to the parameter and the first argument 
 //
 ////////////////////////////////////////////////////////////////////
 
@@ -1266,6 +1304,9 @@
 }
 
 
+
+// The same with respect to the parameter and v (second argument)
+
 void diff2PDF_par_v_mod(double* u, double* v, int* n, double* param, int* copula, double* out)
 {
   double* negv;

Modified: pkg/src/evCopula.c
===================================================================
--- pkg/src/evCopula.c	2015-03-29 16:07:38 UTC (rev 89)
+++ pkg/src/evCopula.c	2015-03-29 17:29:18 UTC (rev 90)
@@ -9,9 +9,28 @@
 #define XEPS 1e-4
 
 // Some function for the Tawn copula
+// (theory based on the extreme value copulas)
+// Reference: See help (some master thesis)
 
+// for the calculation of the density as well as for the h-function we need some help functions
+// the naming of the functions is due to the notation of the master thesis (and also references therein)
+
 // CDF
-void ta(double* t, int* n, double* par, double* par2, double* par3, double* out)	//für CDF
+
+///////////////////////////////////
+//
+// Input:
+// t		t-vector
+// n		number of observations
+// par		first parameter
+// par2		second parameter
+// par3		third parameter
+//
+// Output:
+// out		ta
+//////////////////////////////
+
+void ta(double* t, int* n, double* par, double* par2, double* par3, double* out)	//for CDF
 {
 	int i=0;
 	double t1,t2;
@@ -25,7 +44,19 @@
 
 //ta<-function(t,par,par2,par3) {(par2*t)^par+(par3*(1-t))^par}
 
-// Pickands A
+////////////////////////////////////////////////
+// Pickands A for the Tawn copula
+// Input:
+// t		t-vector
+// n		number of observations
+// par		first parameter
+// par2		second parameter
+// par3		third parameter
+//
+// Output:
+// out		Pickands A for the Tawn copula
+//////////////////////////////
+
 void Tawn(double* t, int* n, double* par, double* par2, double* par3, double* out)		//für CDF
 {
 	int i=0, T=1;
@@ -42,15 +73,29 @@
 
 //Tawn<-function(t,par,par2,par3) {(1-par3)*(1-t)+(1-par2)*t+ta(t,par,par2,par3)^(1/par)}
 
+////////////////////////////////////////////////////
+// CDF of Tawn
+// Input:
+// t		t-vector
+// n		number of observations
+// par		first parameter
+// par2		second parameter
+// par3		third parameter
+//
+// Output:
+// out		CDF
+/////////////////////////////////////////////////////
+
+
 void TawnCDF(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)	// CDF-function
 {
 	int i=0, T=1;
 	double w, A;
 	for(i=0; i<*n;i++)
 	{
-		w=log(v[i])/log(u[i]*v[i]);
-		Tawn(&w, &T, par, par2, par3, &A);		//!!!
-		out[i]=pow(u[i]*v[i],A);
+		w=log(v[i])/log(u[i]*v[i]);				// w vector
+		Tawn(&w, &T, par, par2, par3, &A);		//Pickands A
+		out[i]=pow(u[i]*v[i],A);				// CDF
 	}
 }
 
@@ -58,6 +103,9 @@
 //////////////////////////////////////////////////////////////////
 // PDF
 
+// some more help function for the PDF
+// see reference for details
+
 void ta2(double* t, int* n, double* par, double* par2, double* par3, double* out)	//für PDF
 {
 	int i=0;
@@ -70,6 +118,8 @@
 	}
 }
 
+// something like the first derivative of the ta function
+
 void d1ta(double* t, int* n, double* par, double* par2, double* par3, double* out)	//für PDF
 {
 	int i=0;
@@ -98,7 +148,7 @@
 
 //d2ta<-function(t,par,par2,par3) {par*(par-1)*(par3^2*(par3*t)^(par-2)+par2^2*(par2*(1-t))^(par-2))}
 
-
+// I guess this was some kind of derivative of A (I don't remember, see master thesis)
 void Tawn2(double* t, int* n, double* par, double* par2, double* par3, double* out)		//für PDF
 {
 	int i=0, T=1;
@@ -145,6 +195,8 @@
 //d2Tawn<-function(t,par,par2,par3) {1/par*((1/par-1)*ta(t,par,par2,par3)^(1/par-2)*d1ta(t,par,par2,par3)^2+ta(t,par,par2,par3)^(1/par-1)*d2ta(t,par,par2,par3))}
 
 // Ableitung von A nach u
+// derivative of A with respect to u (first argument)
+// needed for the derivative of c with respect to u
 void dA_du(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)
 {
 	int i=0, T=1;
@@ -160,6 +212,8 @@
 
 //dA_du<-function(u,v,par,par2,par3) {evcBiCopAfuncDeriv(w(u,v),fam,par,par2,par3)*dw_du(u,v)} 
 
+// derivative of A with respect to v
+
 void dA_dv(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)
 {
 	int i=0, T=1;
@@ -173,6 +227,8 @@
 	}
 }
 
+// second derivative with respect to u and v
+
 void dA_dudv(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)
 {
 	int i=0, T=1;
@@ -205,6 +261,7 @@
 }
 
 // Ableitung von C nach u
+// derivative of PDF with respect to u
 void dC_du(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)
 {
 	int i=0, T=1;
@@ -250,6 +307,7 @@
 
 
 // Ableitung von C nach v (fuer h-function)
+// derivative of PDF with respect to v (for h-func)
 void dC_dv(double* u, double* v, int* n, double* par, double* par2, double* par3, double* out)
 {
 	int i=0, T=1;



Mehr Informationen über die Mailingliste Vinecopula-commits