72 std::vector<double>
const& y_input,
73 std::vector<double>
const& weights,
74 std::vector<size_t>
const& strata,
77 std::vector<double>& y_output
83 if( &y_output != &y_input ) {
84 y_output.resize( y_input.size() );
86 assert( y_output.size() == y_input.size() );
89 if( ! weights.empty() && weights.size() != y_input.size() ) {
90 throw std::invalid_argument(
91 "weighted_residuals: y and weights need to have same length."
95 if( strata.empty() ) {
96 if( ! with_intercept ) {
98 if( &y_output != &y_input ) {
102 y_output = std::vector<double>( y_input.size(), 0.0 );
111 if( weights.empty() ) {
113 for(
size_t i = 0; i < y_input.size(); ++i ) {
114 if( std::isfinite( y_input[i] )) {
119 swt =
static_cast<double>( swt_cnt );
121 for(
size_t i = 0; i < y_input.size(); ++i ) {
122 if( weights[i] < 0.0 ) {
123 throw std::runtime_error(
124 "weighted_mean_centering: weights have to be non-negative."
127 if( std::isfinite( weights[i] ) && std::isfinite( y_input[i] )) {
128 swy += weights[i] * y_input[i];
133 assert( std::isfinite( swy ) );
134 assert( std::isfinite( swt ) );
135 assert( swt >= 0.0 );
141 assert( std::isfinite( swy ) );
143 for(
size_t i = 0; i < y_input.size(); ++i ) {
144 y_output[i] = ( centering ? y_input[i] - swy : swy );
151 if( strata.size() != y_input.size() ) {
152 throw std::invalid_argument(
153 "weighted_centering: y and strata need to have same length."
157 auto swy = std::vector<double>( strata.size(), 0.0 );
158 auto swt = std::vector<double>( strata.size(), 0.0 );
161 for(
size_t s = 0; s < strata.size(); ++s ) {
162 if( strata[s] < 1 || strata[s] > strata.size() ) {
163 throw std::invalid_argument(
164 "weighted_residuals: invalid stratum value outside of [1,N] found."
173 if( weights.empty() ) {
174 for(
size_t i = 0; i < y_input.size(); ++i ) {
175 if( std::isfinite( y_input[i] )) {
176 size_t const s = strata[i] - 1;
177 swy[s] += y_input[i];
182 for(
size_t i = 0; i < y_input.size(); ++i ) {
183 if( weights[i] < 0.0 ) {
184 throw std::runtime_error(
185 "weighted_mean_centering: weights have to be non-negative."
188 if( std::isfinite( weights[i] ) && std::isfinite( y_input[i] )) {
189 size_t const s = strata[i] - 1;
190 swy[s] += weights[i] * y_input[i];
191 swt[s] += weights[i];
195 for(
size_t s = 0; s < strata.size(); ++s ) {
196 assert( std::isfinite( swy[s] ) );
197 assert( std::isfinite( swt[s] ) );
198 assert( swt[s] >= 0.0 );
205 assert( std::isfinite( swy[s] ) );
210 for(
size_t i = 0; i < y_input.size(); ++i ) {
211 size_t const s = strata[i] - 1;
213 y_output[i] = ( centering ? y_input[i] - swy[s] : swy[s] );
222 std::vector<double>
const& x_input,
223 std::vector<double>
const& y_input,
224 std::vector<double>
const& weights,
225 std::vector<double>& y_output
227 if( x_input.size() != y_input.size() ) {
228 throw std::invalid_argument(
229 "weighted_residuals: x and y need to have same length."
236 if( weights.empty() ) {
237 for(
size_t i = 0; i < x_input.size(); ++i ) {
238 if( std::isfinite( x_input[i] ) && std::isfinite( y_input[i] )) {
239 swxy += x_input[i] * y_input[i];
240 swxx += x_input[i] * x_input[i];
244 if( weights.size() != x_input.size() ) {
245 throw std::invalid_argument(
246 "weighted_residuals: x and weights need to have same length."
250 for(
size_t i = 0; i < x_input.size(); ++i ) {
251 if( weights[i] < 0.0 ) {
252 throw std::runtime_error(
253 "weighted_residuals: weights have to be non-negative."
256 if( std::isfinite( x_input[i] )
257 && std::isfinite( y_input[i] )
258 && std::isfinite( weights[i] )
260 double const wx = weights[i] * x_input[i];
261 swxy += wx * y_input[i];
262 swxx += wx * x_input[i];
266 assert( std::isfinite( swxx ));
267 assert( std::isfinite( swxy ));
269 if( &y_output != &y_input ) {
270 y_output.resize( y_input.size() );
272 assert( y_output.size() == y_input.size() );
275 for(
size_t i = 0; i < x_input.size(); ++i ) {
276 y_output[i] = y_input[i] - swxy * x_input[i];
281 if( &y_output != &y_input ) {
282 for(
size_t i = 0; i < x_input.size(); ++i ) {
283 y_output[i] = y_input[i];
286 return std::numeric_limits<double>::quiet_NaN();
291 std::vector<double>
const& x_input,
292 std::vector<double>
const& weights
295 if( weights.empty() ) {
296 for(
size_t i = 0; i < x_input.size(); ++i ) {
297 if( std::isfinite( x_input[i] )) {
298 res += x_input[i] * x_input[i];
302 if( weights.size() != x_input.size() ) {
303 throw std::invalid_argument(
304 "weighted_sum: x and weights need to have same length."
307 for(
size_t i = 0; i < x_input.size(); ++i ) {
308 if( weights[i] < 0.0 ) {
309 throw std::runtime_error(
310 "weighted_sum_of_squares: weights have to be non-negative."
313 if( std::isfinite( x_input[i] ) && std::isfinite( weights[i] )) {
314 res += weights[i] * x_input[i] * x_input[i];
318 assert( std::isfinite( res ));
323 std::vector<double>
const& x_input,
324 std::vector<double>
const& y_input,
325 std::vector<double>
const& weights
327 if( x_input.size() != y_input.size() ) {
328 throw std::invalid_argument(
329 "weighted_inner_product: x and y need to have same length."
334 if( weights.empty() ) {
335 for(
size_t i = 0; i < y_input.size(); ++i ) {
336 if( std::isfinite( y_input[i] ) && std::isfinite( x_input[i] )) {
337 res += y_input[i] * x_input[i];
341 if( weights.size() != y_input.size() ) {
342 throw std::invalid_argument(
343 "weighted_inner_product: x and weights need to have same length."
346 for(
size_t i = 0; i < y_input.size(); ++i ) {
347 if( weights[i] < 0.0 ) {
348 throw std::runtime_error(
349 "weighted_inner_product: weights have to be non-negative."
352 if( std::isfinite( weights[i] )
353 && std::isfinite( y_input[i] )
354 && std::isfinite( x_input[i] )
356 res += weights[i] * y_input[i] * x_input[i];
360 assert( std::isfinite( res ));
365 std::vector<double>
const& x_input,
366 std::vector<double>
const& weights
369 if( weights.empty() ) {
370 for(
size_t i = 0; i < x_input.size(); ++i ) {
371 if( std::isfinite( x_input[i] )) {
376 if( weights.size() != x_input.size() ) {
377 throw std::invalid_argument(
378 "weighted_sum: x and weights need to have same length."
381 for(
size_t i = 0; i < x_input.size(); ++i ) {
382 if( weights[i] < 0.0 ) {
383 throw std::runtime_error(
384 "weighted_sum: weights have to be non-negative."
387 if( std::isfinite( weights[i] ) && std::isfinite( x_input[i] )) {
388 res += weights[i] * x_input[i];
392 assert( std::isfinite( res ));