summaryrefslogtreecommitdiffstats
path: root/src/armadillo/include/armadillo_bits/glue_quantile_meat.hpp
blob: 370e432a6a0c2dc88427e9c504086fbd4a192d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-License-Identifier: Apache-2.0
// 
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------


//! \addtogroup glue_quantile
//! @{


template<typename eTa, typename eTb>
inline
void
glue_quantile::worker(eTb* out_mem, Col<eTa>& Y, const Mat<eTb>& P)
  {
  arma_extra_debug_sigprint();
  
  // NOTE: assuming out_mem is an array with P.n_elem elements
  
  // TODO: ignore non-finite values ?
  
  // algorithm based on "Definition 5" in:
  // Rob J. Hyndman and Yanan Fan.
  // Sample Quantiles in Statistical Packages.
  // The American Statistician, Vol. 50, No. 4, pp. 361-365, 1996.
  // http://doi.org/10.2307/2684934
  
  const eTb*  P_mem    = P.memptr();
  const uword P_n_elem = P.n_elem;
  
  const eTb alpha = 0.5;
  const eTb N     = eTb(Y.n_elem);
  const eTb P_min = (eTb(1) - alpha) / N;
  const eTb P_max = (N      - alpha) / N;
  
  for(uword i=0; i < P_n_elem; ++i)
    {
    const eTb P_i = P_mem[i];
    
    eTb out_val = eTb(0);
    
    if(P_i < P_min)
      {
      out_val = (P_i < eTb(0)) ? eTb(-std::numeric_limits<eTb>::infinity()) : eTb(Y.min());
      }
    else
    if(P_i > P_max)
      {
      out_val = (P_i > eTb(1)) ? eTb( std::numeric_limits<eTb>::infinity()) : eTb(Y.max());
      }
    else
      {
      const uword   k = uword(std::floor(N * P_i + alpha));
      const eTb   P_k = (eTb(k) - alpha) / N;
      
      const eTb w = (P_i - P_k) * N;
      
      eTa* Y_k_ptr = Y.begin() + uword(k);
      std::nth_element( Y.begin(), Y_k_ptr, Y.end() );
      const eTa Y_k_val = (*Y_k_ptr);
      
      eTa* Y_km1_ptr = Y.begin() + uword(k-1);
      // std::nth_element( Y.begin(), Y_km1_ptr, Y.end() );
      std::nth_element( Y.begin(), Y_km1_ptr, Y_k_ptr );
      const eTa Y_km1_val = (*Y_km1_ptr);
      
      out_val = ((eTb(1) - w) * Y_km1_val) + (w * Y_k_val);
      }
    
    out_mem[i] = out_val;
    }
  }



template<typename eTa, typename eTb>
inline
void
glue_quantile::apply_noalias(Mat<eTb>& out, const Mat<eTa>& X, const Mat<eTb>& P, const uword dim)
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( ((P.is_vec() == false) && (P.is_empty() == false)), "quantile(): parameter 'P' must be a vector" );
  
  if(X.is_empty())  { out.reset(); return; }
  
  const uword X_n_rows = X.n_rows;
  const uword X_n_cols = X.n_cols;
  
  const uword P_n_elem = P.n_elem;
  
  if(dim == 0)
    {
    out.set_size(P_n_elem, X_n_cols);
    
    if(out.is_empty())  { return; }
    
    Col<eTa> Y(X_n_rows, arma_nozeros_indicator());
    
    if(X_n_cols == 1)
      {
      arrayops::copy(Y.memptr(), X.memptr(), X_n_rows);
      
      glue_quantile::worker(out.memptr(), Y, P);
      }
    else
      {
      for(uword col=0; col < X_n_cols; ++col)
        {
        arrayops::copy(Y.memptr(), X.colptr(col), X_n_rows);
        
        glue_quantile::worker(out.colptr(col), Y, P);
        }
      }
    }
  else
  if(dim == 1)
    {
    out.set_size(X_n_rows, P_n_elem);
    
    if(out.is_empty())  { return; }
    
    Col<eTa> Y(X_n_cols, arma_nozeros_indicator());
    
    if(X_n_rows == 1)
      {
      arrayops::copy(Y.memptr(), X.memptr(), X_n_cols);
      
      glue_quantile::worker(out.memptr(), Y, P);
      }
    else
      {
      Col<eTb> tmp(P_n_elem, arma_nozeros_indicator());
      
      eTb* tmp_mem = tmp.memptr();
      
      for(uword row=0; row < X_n_rows; ++row)
        {
        eTa* Y_mem = Y.memptr();
        
        for(uword col=0; col < X_n_cols; ++col)  { Y_mem[col] = X.at(row,col); }
        
        glue_quantile::worker(tmp_mem, Y, P);
        
        for(uword i=0; i < P_n_elem; ++i)  { out.at(row,i) = tmp_mem[i]; }
        }
      }
    }
  }



template<typename T1, typename T2>
inline
void
glue_quantile::apply(Mat<typename T2::elem_type>& out, const mtGlue<typename T2::elem_type,T1,T2,glue_quantile>& expr)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T2::elem_type eTb;
  
  const uword dim = expr.aux_uword;
  
  arma_debug_check( (dim > 1), "quantile(): parameter 'dim' must be 0 or 1" );
  
  const quasi_unwrap<T1> UA(expr.A);
  const quasi_unwrap<T2> UB(expr.B);
  
  arma_debug_check((UA.M.internal_has_nan() || UB.M.internal_has_nan()), "quantile(): detected NaN");
  
  if(UA.is_alias(out) || UB.is_alias(out))
    {
    Mat<eTb> tmp;
    
    glue_quantile::apply_noalias(tmp, UA.M, UB.M, dim);
    
    out.steal_mem(tmp);
    }
  else
    {
    glue_quantile::apply_noalias(out, UA.M, UB.M, dim);
    }
  }



template<typename T1, typename T2>
inline
void
glue_quantile_default::apply(Mat<typename T2::elem_type>& out, const mtGlue<typename T2::elem_type,T1,T2,glue_quantile_default>& expr)
  {
  arma_extra_debug_sigprint();
  
  typedef typename T2::elem_type eTb;
  
  const quasi_unwrap<T1> UA(expr.A);
  const quasi_unwrap<T2> UB(expr.B);
  
  const uword dim = (T1::is_xvec) ? uword(UA.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0);
  
  arma_debug_check((UA.M.internal_has_nan() || UB.M.internal_has_nan()), "quantile(): detected NaN");
  
  if(UA.is_alias(out) || UB.is_alias(out))
    {
    Mat<eTb> tmp;
    
    glue_quantile::apply_noalias(tmp, UA.M, UB.M, dim);
    
    out.steal_mem(tmp);
    }
  else
    {
    glue_quantile::apply_noalias(out, UA.M, UB.M, dim);
    }
  }


//! @}