GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — statistics · cppd/math@a98993e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/statistics
    • linear_fit.h
    • test
      • test_linear_fit.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/statistics/linear_fit.h‎

Copy file name to clipboardExpand all lines: src/statistics/linear_fit.h
+23-41Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,26 @@ template <typename T>
316316
}
317317

318318
template <typename T>
319-
[[nodiscard]] Line<T> linear_fit_medians_gate_slopes(const std::vector<numerical::Vector<2, T>>& points, const T gate)
319+
[[nodiscard]] std::vector<T> linear_fit_piecewise(
320+
const std::vector<numerical::Vector<2, T>>& points,
321+
const std::vector<T>& breakpoints)
320322
{
321323
static_assert(std::is_floating_point_v<T>);
322324
namespace impl = implementation;
323325

324-
impl::check_point_count(points);
326+
impl::check_breakpoints(breakpoints);
325327

326-
const std::vector<T> slopes = impl::compute_slope_medians(points);
328+
std::vector<std::vector<T>> a = impl::least_squares_a(points, breakpoints);
329+
std::vector<T> b = impl::least_squares_b(points);
327330

328-
const std::vector<numerical::Vector<2, T>> filtered_points = impl::filter_points(points, slopes, gate);
331+
std::vector<T> s = numerical::qr_solve(a, b);
332+
ASSERT(s.size() == breakpoints.size() + 1);
329333

330-
return linear_fit_medians(filtered_points);
334+
return s;
331335
}
332336

333337
template <typename T>
334-
[[nodiscard]] Line<T> linear_fit_medians_gate_residuals(
338+
[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_slopes(
335339
const std::vector<numerical::Vector<2, T>>& points,
336340
const T gate)
337341
{
@@ -340,36 +344,30 @@ template <typename T>
340344

341345
impl::check_point_count(points);
342346

343-
const Line l = linear_fit_medians(points);
344-
345-
const std::vector<T> residuals = impl::residuals(points, l.intercept, l.slope);
346-
347-
const std::vector<numerical::Vector<2, T>> filtered_points = impl::filter_points(points, residuals, gate);
347+
const std::vector<T> slopes = impl::compute_slope_medians(points);
348348

349-
return linear_fit_medians(filtered_points);
349+
return impl::filter_points(points, slopes, gate);
350350
}
351351

352352
template <typename T>
353-
[[nodiscard]] std::vector<T> linear_fit_piecewise(
353+
[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_residuals(
354354
const std::vector<numerical::Vector<2, T>>& points,
355-
const std::vector<T>& breakpoints)
355+
const T gate)
356356
{
357357
static_assert(std::is_floating_point_v<T>);
358358
namespace impl = implementation;
359359

360-
impl::check_breakpoints(breakpoints);
360+
impl::check_point_count(points);
361361

362-
std::vector<std::vector<T>> a = impl::least_squares_a(points, breakpoints);
363-
std::vector<T> b = impl::least_squares_b(points);
362+
const Line l = linear_fit_medians(points);
364363

365-
std::vector<T> s = numerical::qr_solve(a, b);
366-
ASSERT(s.size() == breakpoints.size() + 1);
364+
const std::vector<T> residuals = impl::residuals(points, l.intercept, l.slope);
367365

368-
return s;
366+
return impl::filter_points(points, residuals, gate);
369367
}
370368

371369
template <typename T>
372-
[[nodiscard]] std::vector<T> linear_fit_piecewise_gate(
370+
[[nodiscard]] std::vector<numerical::Vector<2, T>> filter_by_residuals(
373371
const std::vector<numerical::Vector<2, T>>& points,
374372
const std::vector<T>& breakpoints,
375373
const T gate)
@@ -379,27 +377,11 @@ template <typename T>
379377

380378
impl::check_breakpoints(breakpoints);
381379

382-
const std::vector<std::vector<numerical::Vector<2, T>>> segments =
383-
impl::breakpoint_segments(points, breakpoints);
384-
385-
std::vector<numerical::Vector<2, T>> filtered_points;
386-
for (const std::vector<numerical::Vector<2, T>>& segment : segments)
380+
std::vector<numerical::Vector<2, T>> res;
381+
for (const std::vector<numerical::Vector<2, T>>& segment : impl::breakpoint_segments(points, breakpoints))
387382
{
388-
impl::check_point_count(segment);
389-
390-
const Line l = linear_fit_medians(segment);
391-
392-
const std::vector<T> residuals = impl::residuals(segment, l.intercept, l.slope);
393-
394-
filtered_points.insert_range(filtered_points.end(), impl::filter_points(segment, residuals, gate));
383+
res.insert_range(res.end(), filter_by_residuals(segment, gate));
395384
}
396-
397-
std::vector<std::vector<T>> a = impl::least_squares_a(filtered_points, breakpoints);
398-
std::vector<T> b = impl::least_squares_b(filtered_points);
399-
400-
std::vector<T> s = numerical::qr_solve(a, b);
401-
ASSERT(s.size() == breakpoints.size() + 1);
402-
403-
return s;
385+
return res;
404386
}
405387
}
Collapse file

‎src/statistics/test/test_linear_fit.cpp‎

Copy file name to clipboardExpand all lines: src/statistics/test/test_linear_fit.cpp
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void test_medians_gate_slopes(PCG& engine)
222222
points.push_back({x, y});
223223
}
224224

225-
const linear_fit::Line l = linear_fit::linear_fit_medians_gate_slopes(points, gate);
225+
const linear_fit::Line l = linear_fit::linear_fit_medians(linear_fit::filter_by_slopes(points, gate));
226226

227227
const T rmse = compute_rmse(points, intercept, slope, l.intercept, l.slope);
228228

@@ -257,7 +257,7 @@ void test_medians_gate_residuals(PCG& engine)
257257
points.push_back({x, y});
258258
}
259259

260-
const linear_fit::Line l = linear_fit::linear_fit_medians_gate_residuals(points, gate);
260+
const linear_fit::Line l = linear_fit::linear_fit_medians(linear_fit::filter_by_residuals(points, gate));
261261

262262
const T rmse = compute_rmse(points, intercept, slope, l.intercept, l.slope);
263263

@@ -301,7 +301,7 @@ void test_linear_piecewise(PCG& engine)
301301
}
302302

303303
template <typename T>
304-
void test_linear_piecewise_gate(PCG& engine)
304+
void test_linear_piecewise_gate_residuals(PCG& engine)
305305
{
306306
const T gate = 4;
307307
const int count = 2'000;
@@ -334,7 +334,8 @@ void test_linear_piecewise_gate(PCG& engine)
334334
points.push_back({x, y});
335335
}
336336

337-
const std::vector<T> betas_s = linear_fit::linear_fit_piecewise_gate(points, breakpoints, gate);
337+
const std::vector<T> betas_s = linear_fit::linear_fit_piecewise(
338+
linear_fit::filter_by_residuals(points, breakpoints, gate), breakpoints);
338339

339340
const T rmse = compute_rmse(points, breakpoints, betas, betas_s);
340341

@@ -346,11 +347,13 @@ void test(PCG& engine)
346347
{
347348
test_qr<T>(engine);
348349
test_theil_sen<T>(engine);
350+
349351
test_medians<T>(engine);
350352
test_medians_gate_slopes<T>(engine);
351353
test_medians_gate_residuals<T>(engine);
354+
352355
test_linear_piecewise<T>(engine);
353-
test_linear_piecewise_gate<T>(engine);
356+
test_linear_piecewise_gate_residuals<T>(engine);
354357
}
355358

356359
void test_median()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.