1 | #ifndef _theplu_yat_normalizer_qquantile_normalizer_ |
---|
2 | #define _theplu_yat_normalizer_qquantile_normalizer_ |
---|
3 | |
---|
4 | /* |
---|
5 | Copyright (C) 2009 Jari Häkkinen |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "yat/utility/Vector.h" |
---|
24 | |
---|
25 | namespace theplu { |
---|
26 | namespace yat { |
---|
27 | namespace utility { |
---|
28 | class Matrix; |
---|
29 | class VectorBase; |
---|
30 | } |
---|
31 | namespace normalizer { |
---|
32 | |
---|
33 | /** |
---|
34 | \brief Perform Q-quantile normalization |
---|
35 | |
---|
36 | After a Q-quantile normalization each column has approximately |
---|
37 | the same distribution of data (the Q-quantiles are the |
---|
38 | same). Also, within each column the rank of an element is not |
---|
39 | changed. |
---|
40 | |
---|
41 | There is currently no weighted version of qQuantileNormalizer |
---|
42 | |
---|
43 | The normalization goes like this |
---|
44 | - Data is not assumed to be sorted. |
---|
45 | - Partition the target data in N parts. N must be 3 larger |
---|
46 | because of requirements from the underlying cspline fit |
---|
47 | - Calculate the arithmetic mean for each part, the mean is |
---|
48 | assigned to the mid point of each part. |
---|
49 | - Do the same for the data to be tranformed (called source |
---|
50 | here). |
---|
51 | - For each part, calculate the difference between the target and |
---|
52 | the source. Now we have N differences d_i with associated rank |
---|
53 | (midpoint of each part). |
---|
54 | - Create a cubic spline fit to this difference vector d. The |
---|
55 | resulting curve is used to recalculate all column values. |
---|
56 | - Use the cubic spline fit for values within the cubic spline |
---|
57 | fit range [midpoint 1st part, midpoint last part]. |
---|
58 | - For data outside the cubic spline fit use linear |
---|
59 | extrapolation, i.e., a constant shift. d_first for points |
---|
60 | below fit range, and d_last for points above fit range. |
---|
61 | |
---|
62 | \since New in yat 0.5 |
---|
63 | */ |
---|
64 | class qQuantileNormalizer |
---|
65 | { |
---|
66 | public: |
---|
67 | /** |
---|
68 | \brief Documentation please. |
---|
69 | |
---|
70 | \a Q is the number of parts and must be within \f$ [3,N] \f$ |
---|
71 | where \f$ N \f$ is the total number of data points in the |
---|
72 | target. However, if \f$ N \f$ is larger than the number of points |
---|
73 | in the data to be normalized the behaviour of the code is |
---|
74 | undefined. Keep \f$ N \f$ equal to or less than the smallest |
---|
75 | number of data points in the target or each data set to be |
---|
76 | normalized against a ginven target. The lower bound of three is |
---|
77 | due to restrictions in the cspline fit utilized in the |
---|
78 | normalization. |
---|
79 | */ |
---|
80 | qQuantileNormalizer(const utility::VectorBase& target, unsigned int Q); |
---|
81 | |
---|
82 | /** |
---|
83 | \brief perform the Q-quantile normalization. |
---|
84 | |
---|
85 | It is possible to normalize "in place"; it is permissible for |
---|
86 | \a matrix and \a result to reference the same Matrix. |
---|
87 | |
---|
88 | \note dimensions of \a matrix and \a result must match. |
---|
89 | */ |
---|
90 | void operator()(const utility::Matrix& matrix, |
---|
91 | utility::Matrix& result) const; |
---|
92 | |
---|
93 | private: |
---|
94 | |
---|
95 | /** |
---|
96 | \brief Partition a vector of data into equal sizes. |
---|
97 | |
---|
98 | The class also calculates the average of each part and assigns |
---|
99 | the average to the mid point of each part. The midpoint is a |
---|
100 | double, i.e., it is not forced to be an integer index. |
---|
101 | */ |
---|
102 | class Partitioner |
---|
103 | { |
---|
104 | public: |
---|
105 | /** |
---|
106 | \brief Create the partition and perform required calculations. |
---|
107 | */ |
---|
108 | Partitioner(const utility::VectorBase& vec, unsigned int N); |
---|
109 | |
---|
110 | /** |
---|
111 | \brief Return the averages for each part. |
---|
112 | |
---|
113 | \return The average vector. |
---|
114 | */ |
---|
115 | const utility::Vector& averages(void) const; |
---|
116 | |
---|
117 | /** |
---|
118 | \brief Return the mid point for each partition. |
---|
119 | |
---|
120 | \return The index vector. |
---|
121 | */ |
---|
122 | const utility::Vector& index(void) const; |
---|
123 | |
---|
124 | /** |
---|
125 | \return The number of parts. |
---|
126 | */ |
---|
127 | size_t size(void) const; |
---|
128 | |
---|
129 | private: |
---|
130 | utility::Vector average_; |
---|
131 | utility::Vector index_; |
---|
132 | }; |
---|
133 | |
---|
134 | |
---|
135 | Partitioner target_; |
---|
136 | }; |
---|
137 | |
---|
138 | }}} // end of namespace normalizer, yat and thep |
---|
139 | |
---|
140 | #endif |
---|