58 float (*samples)[D],
uint16_t count,
float *params,
float *fit_error __attribute__((unused)))
63 float errors[n_iterations];
67 float subset_params[n_iterations][D_1];
71 n_samples = (n_samples < D_1) ? D_1 : n_samples;
73 n_samples = (n_samples < count) ? n_samples : count;
76 for (
int i = 0; i < n_iterations; i++) {
83 subset_targets[j] = targets[indices_subset[j]];
84 for (
int k = 0; k <
D; k++) {
85 subset_samples[j][k] = samples[indices_subset[j]][k];
90 fit_linear_model(subset_targets, D, subset_samples, n_samples, use_bias, subset_params[i], &err);
91 printf(
"params normal: %f, %f\n", subset_params[i][0], subset_params[i][1]);
95 fit_linear_model_prior(subset_targets, D, subset_samples, n_samples, use_bias, priors, subset_params[i], &err);
96 printf(
"params prior: %f, %f\n", subset_params[i][0], subset_params[i][1]);
101 for (
int j = 0; j < count; j++) {
103 prediction =
predict_value(samples[j], subset_params[i], D, use_bias);
104 err = fabsf(prediction - targets[j]);
106 err = (err > error_threshold) ? error_threshold : err;
113 float min_err = errors[0];
115 for (
int i = 1; i < n_iterations; i++) {
116 if (errors[i] < min_err) {
123 for (
int d = 0; d < D_1; d++) {
124 params[d] = subset_params[min_ind][d];
141 for (
int w = 0; w <
D; w++) {
142 sum += weights[w] * sample[w];
166 bool picked_number =
false;
167 while (!picked_number) {
168 index = rand() % count;
170 for (
int k = 0; k < j; k++) {
171 if (indices_subset[k] == index) {
177 indices_subset[j] = index;
178 picked_number =
true;
void get_indices_without_replacement(int *indices_subset, int n_samples, int count)
Get indices without replacement.
Paparazzi floating point algebra.
Perform Random Sample Consensus (RANSAC), a robust fitting method.
void fit_linear_model_prior(float *targets, int D, float(*samples)[D], uint16_t count, bool use_bias, float *priors, float *params, float *fit_error)
Fit a linear model from samples to target values with a prior.
float predict_value(float *sample, float *weights, int D, bool use_bias)
Predict the value of a sample with linear weights.
void RANSAC_linear_model(int n_samples, int n_iterations, float error_threshold, float *targets, int D, float(*samples)[D], uint16_t count, float *params, float *fit_error)
Perform RANSAC to fit a linear model.
Matrix decompositions in floating point.
void fit_linear_model(float *targets, int D, float(*samples)[D], uint16_t count, bool use_bias, float *params, float *fit_error)
Fit a linear model from samples to target values.