openModeller  Version 1.5.0
csmkg.cpp
Go to the documentation of this file.
1 //
2 // CsmKG
3 //
4 // Description: Csm implementation using Keiser-Gutman cutoff to discard components
5 //
6 //
7 // Author: CRIA <t.sutton@reading.ac.uk>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 
13 #include <string.h>
14 #include "csmkg.hh"
15 
16 #include <gsl/gsl_statistics_double.h>
17 #include <gsl/gsl_multifit_nlin.h> //remove this when we have proper covar matrix
18 #include <gsl/gsl_math.h>
19 #include <gsl/gsl_eigen.h>
20 #include <gsl/gsl_sf_gamma.h>
21 
22 #include <math.h>
23 
24 /****************************************************************/
25 /********************** Algorithm's Metadata ********************/
26 
27 #define NUM_PARAM 0
28 
29 
30 /************************************/
31 /*** Algorithm parameter metadata ***/
32 
34 /*
35 AlgParamMetadata parameters[NUM_PARAM] = {
36 
37  // Metadata of the first parameter.
38  {
39  "First parameter id", // Id.
40  "First parameter name", // Name.
41  "First parameter type", // Type.
42  "First parameter overview", // Overview.
43  "First parameter description", // Description.
44 
45  1, // Not zero if the parameter has lower limit.
46  0.0, // Parameter's lower limit.
47  1, // Not zero if the parameter has upper limit.
48  1.0, // Parameter's upper limit.
49  "0.1" // Parameter's typical (default) value.
50  },
51 };
52 */
53 
54 
55 /************************************/
56 /*** Algorithm's general metadata ***/
57 
59 
60  "CSMKG", // Id.
61  "Climate Space Model - Kaiser-Gutman", // Name.
62  "0.1 alpha", // Version.
63 
64  // Overview
65  "Climate Space Model [CSM] is a principle components based \
66 algorithm developed by Dr. Neil Caithness",
67 
68  // Description.
69  "Climate Space Model [CSM] is a principle components based \
70 algorithm developed by Dr. Neil Caithness. The component \
71 selection process int this algorithm implementation is \
72 based on the Keiser-Gutman cutoff where any component with \
73 an eigenvalue < 1 is discarded.\
74 \n \
75 The original CSM was written as series of Matlab functions. \
76  ",
77 
78  "Neil Caithness", // Author
79  "", // Bibliography.
80 
81  "Tim Sutton, Renato De Giovanni", // Code author.
82  "t.sutton [at] reading.ac.uk", // Code author's contact.
83 
84  0, // Does not accept categorical data.
85  0, // Does not need (pseudo)absence points.
86 
87  NUM_PARAM, // Algorithm's parameters.
88  parameters
89 };
90 
91 
92 
93 /****************************************************************/
94 /****************** Algorithm's factory function ****************/
95 
96 dllexp
99 {
100  return new CsmKG();
101 }
102 
103 dllexp
104 AlgMetadata const *
106 {
107  return &metadata;
108 }
109 
110 /****************************************************************/
111 /****************************** Csm *****************************/
112 
117 CsmKG::CsmKG(): Csm(&metadata)
118 {
119  _initialized = 0;
120 }
121 
122 
125 {
126  //This is handled by the parent class I think...
127  /*
128  if ( _initialized )
129  {
130  gsl_matrix_free (_gsl_environment_matrix);
131  gsl_matrix_free (_gsl_covariance_matrix);
132  gsl_vector_free (_gsl_avg_vector);
133  gsl_vector_free (_gsl_stddev_vector);
134  gsl_vector_free (_gsl_eigenvalue_vector);
135  gsl_matrix_free (_gsl_eigenvector_matrix);
136  }
137  */
138 }
140 {
141  printf ("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n");
142  printf (" Starting CSM - Kaiser-Gutman \n");
143  printf (" Component discarding routine \n");
144  printf ("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n");
145  // Discard any columns from the eigenvector where the eigenvalue is < 1
146  // (Keiser-Gutman method)
147  // because the eigenvalues are sorted, we can discard any columns after the
148  // first one is encountered.
149 
150  int myColumnNo = -1;
151  // for debuggin print out the averages
152  for (int j=0;j<_layer_count;j++)
153  {
154  float myValue = gsl_vector_get (_gsl_eigenvalue_vector,j);
155  if (myValue < 1)
156  {
157  myColumnNo = j;
158  }
159  }
160  if (myColumnNo > -1)
161  {
162  int j;
163  _retained_components_count = myColumnNo-1;
164  printf ("\n\nNumber of components retained: %i of %i\n",
166  _layer_count);
167  //so now create a local copy of the eigvect and eigval...
168  //first clone the vector and matrix...
169  gsl_vector * tmp_gsl_eigenvalue_vector = gsl_vector_alloc (_layer_count);
170  gsl_matrix * tmp_gsl_eigenvector_matrix = gsl_matrix_alloc (_layer_count, _layer_count);
171 
172  gsl_matrix_memcpy (tmp_gsl_eigenvector_matrix, _gsl_eigenvector_matrix);
173  gsl_vector_memcpy (tmp_gsl_eigenvalue_vector,_gsl_eigenvalue_vector);
174 
175  //now clear our current eig vec and matrix
176 
177  gsl_vector_free (_gsl_eigenvalue_vector);
178  gsl_matrix_free (_gsl_eigenvector_matrix);
179 
180  //next we reassign them to the reduced size (ie without unwanted components)
182  _gsl_eigenvector_matrix = gsl_matrix_alloc (_layer_count, _retained_components_count);
183 
184  //now copy over just the components we intend to keep
185  //...first the vector
186  for (j=0;j<_retained_components_count;j++)
187  {
188  float myFloat = gsl_vector_get (tmp_gsl_eigenvalue_vector,j);
189  gsl_vector_set (_gsl_eigenvalue_vector,j,myFloat);
190  }
191  //...now the matrix
192  gsl_vector * tmp_gsl_vector = gsl_vector_alloc (_layer_count);
193  for (j=0;j<_retained_components_count;j++)
194  {
195  gsl_matrix_get_col (tmp_gsl_vector, tmp_gsl_eigenvector_matrix, j);
196  gsl_matrix_set_col (_gsl_eigenvector_matrix,j,tmp_gsl_vector);
197  }
198  //now clear away the temporary vars
199  gsl_vector_free (tmp_gsl_vector);
200  gsl_vector_free (tmp_gsl_eigenvalue_vector);
201  gsl_matrix_free (tmp_gsl_eigenvector_matrix);
202 
203 
204  }
205  else
206  {
207  //we keep all the components!
209  printf ("\n\nNumber of components retained: %i of %i\n",
211  _layer_count);
212  }
213  return 1;
214 }
Definition: csmkg.hh:41
gsl_matrix * _gsl_eigenvector_matrix
Definition: csm.hh:442
static AlgMetadata metadata
Definition: csmkg.cpp:58
dllexp AlgMetadata const * algorithmMetadata()
Definition: csmkg.cpp:105
int _retained_components_count
Definition: csm.hh:446
gsl_vector * _gsl_eigenvalue_vector
Definition: csm.hh:440
int _layer_count
Definition: csm.hh:444
int discardComponents()
Definition: csmkg.cpp:139
Definition: csm.hh:269
~CsmKG()
Definition: csmkg.cpp:124
dllexp AlgorithmImpl * algorithmFactory()
Definition: csmkg.cpp:98
int _initialized
Definition: csm.hh:423
CsmKG()
Definition: csmkg.cpp:117
static AlgParamMetadata * parameters
Definition: csmkg.cpp:33
#define NUM_PARAM
Definition: csmkg.cpp:27