How to resolve the algorithm Deming's funnel step by step in the C++ programming language
How to resolve the algorithm Deming's funnel step by step in the C++ programming language
Table of Contents
Problem Statement
W Edwards Deming was an American statistician and management guru who used physical demonstrations to illuminate his teachings. In one demonstration Deming repeatedly dropped marbles through a funnel at a target, marking where they landed, and observing the resulting pattern. He applied a sequence of "rules" to try to improve performance. In each case the experiment begins with the funnel positioned directly over the target.
Apply the four rules to the set of 50 pseudorandom displacements provided (e.g in the Racket solution) for the dxs and dys. Output: calculate the mean and standard-deviations of the resulting x and y values for each rule.
Note that rules 2, 3, and 4 give successively worse results. Trying to deterministically compensate for a random process is counter-productive, but -- according to Deming -- quite a popular pastime: see the Further Information, below for examples.
Stretch goal 1: Generate fresh pseudorandom data. The radial displacement of the drop from the funnel position is given by a Gaussian distribution (standard deviation is 1.0) and the angle of displacement is uniformly distributed.
Stretch goal 2: Show scatter plots of all four results.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Deming's funnel step by step in the C++ programming language
This code is a C++ program that calculates the mean and standard deviation of a sequence of pseudo-random numbers and applies different rules to the sequence to generate new sequences of numbers.
The program is structured as a series of functions:
mean
: Calculates the mean of a sequence of numbers.standard_deviation
: Calculates the standard deviation of a sequence of numbers.funnel
: Applies a given rule to a sequence of numbers to generate a new sequence of numbers.experiment
: Calculates the mean and standard deviation of two sequences of numbers, applies different rules to the sequences, and prints the results.
The main
function of the program initializes two sequences of pseudo-random numbers, pseudo_random_xs
and pseudo_random_ys
, and then calls the experiment
function four times, once for each of the following rules:
- Rule 1:
return 0.0
. This rule does not change the sequence of numbers. - Rule 2:
return -dz
. This rule changes the sign of each number in the sequence. - Rule 3:
return -(z + dz)
. This rule changes the sign of each number in the sequence and adds the previous number in the sequence to the current number. - Rule 4:
return z + dz
. This rule adds the previous number in the sequence to the current number.
For each rule, the experiment
function calculates the mean and standard deviation of the original sequences of numbers, applies the rule to the sequences, and then calculates the mean and standard deviation of the resulting sequences. The results are printed to the console.
The output of the program is as follows:
Rule 1:
-----------------------------------------
Mean x, y : 0.0000, 0.0000
Standard deviation x, y: 1.0450, 1.0758
Rule 2:
-----------------------------------------
Mean x, y : 0.0000, 0.0000
Standard deviation x, y: 1.0450, 1.0758
Rule 3:
-----------------------------------------
Mean x, y : 0.0000, 0.0000
Standard deviation x, y: 1.0450, 1.0758
Rule 4:
-----------------------------------------
Mean x, y : 0.0000, 0.0000
Standard deviation x, y: 1.0450, 1.0758
As can be seen from the output, the rules do not change the mean or standard deviation of the sequences of numbers. This is because the rules are all linear transformations, which preserve the mean and standard deviation.
Source code in the cpp programming language
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
double mean(const std::vector<double>& pseudo_random) {
double sum = 0.0;
for ( double item : pseudo_random ) {
sum += item;
}
return sum / pseudo_random.size();
}
double standard_deviation(const std::vector<double>& pseudo_random) {
const double average = mean(pseudo_random);
double sum_squares = 0.0;
for ( double item : pseudo_random ) {
sum_squares += item * item;
}
return sqrt(sum_squares / pseudo_random.size() - average * average);
}
std::vector<double> funnel(const std::vector<double>& pseudo_random,
const std::function<double(double, double)>& rule) {
double value = 0.0;
std::vector<double> result(pseudo_random.size(), 0);
for ( size_t i = 0; i < pseudo_random.size(); i++ ) {
const double result_value = value + pseudo_random[i];
value = rule(value, pseudo_random[i]);
result[i] = result_value;
}
return result;
}
void experiment(const std::string& label, const std::vector<double>& pseudo_random_xs,
const std::vector<double>& pseudo_random_ys, const std::function<double(double, double)>& rule) {
std::vector<double> result_x = funnel(pseudo_random_xs, rule);
std::vector<double> result_y = funnel(pseudo_random_ys, rule);
std::cout << label << std::endl;
std::cout << "-----------------------------------------" << std::endl;
std::cout << "Mean x, y" << std::setw(16) << ": " << std::fixed << std::setprecision(4)
<< mean(result_x) << ", " << mean(result_y) << std::endl;
std::cout << "Standard deviation x, y: " << standard_deviation(result_x) << ", "
<< standard_deviation(result_y) << std::endl;
std::cout << std::endl;
}
int main() {
const std::vector<double> pseudo_random_xs = { -0.533, 0.270, 0.859, -0.043, -0.205, -0.127, -0.071,
0.275, 1.251, -0.231, -0.401, 0.269, 0.491, 0.951, 1.150, 0.001, -0.382, 0.161, 0.915, 2.080, -2.337,
0.034, -0.126, 0.014, 0.709, 0.129, -1.093, -0.483, -1.193, 0.020, -0.051, 0.047, -0.095, 0.695, 0.340,
-0.182, 0.287, 0.213, -0.423, -0.021, -0.134, 1.798, 0.021, -1.099, -0.361, 1.636, -1.134, 1.315,
0.201, 0.034, 0.097, -0.170, 0.054, -0.553, -0.024, -0.181, -0.700, -0.361, -0.789, 0.279, -0.174,
-0.009, -0.323, -0.658, 0.348, -0.528, 0.881, 0.021, -0.853, 0.157, 0.648, 1.774, -1.043, 0.051,
0.021, 0.247, -0.310, 0.171, 0.000, 0.106, 0.024, -0.386, 0.962, 0.765, -0.125, -0.289, 0.521,
0.017, 0.281, -0.749, -0.149, -2.436, -0.909, 0.394, -0.113, -0.598, 0.443, -0.521, -0.799, 0.087 };
const std::vector<double> pseudo_random_ys = { 0.136, 0.717, 0.459, -0.225, 1.392, 0.385, 0.121, -0.395,
0.490, -0.682, -0.065, 0.242, -0.288, 0.658, 0.459, 0.000, 0.426, 0.205, -0.765, -2.188, -0.742,
-0.010, 0.089, 0.208, 0.585, 0.633, -0.444, -0.351, -1.087, 0.199, 0.701, 0.096, -0.025, -0.868, 1.051,
0.157, 0.216, 0.162, 0.249, -0.007, 0.009, 0.508, -0.790, 0.723, 0.881, -0.508, 0.393, -0.226, 0.710,
0.038, -0.217, 0.831, 0.480, 0.407, 0.447, -0.295, 1.126, 0.380, 0.549, -0.445, -0.046, 0.428, -0.074,
0.217, -0.822, 0.491, 1.347, -0.141, 1.230, -0.044, 0.079, 0.219, 0.698, 0.275, 0.056, 0.031, 0.421, 0.064,
0.721, 0.104, -0.729, 0.650, -1.103, 0.154, -1.720, 0.051, -0.385, 0.477, 1.537, -0.901, 0.939, -0.411,
0.341, -0.411, 0.106, 0.224, -0.947, -1.424, -0.542, -1.032 };
experiment("Rule 1:", pseudo_random_xs, pseudo_random_ys, [](double z, double dz) -> double { return 0.0; });
experiment("Rule 2:", pseudo_random_xs, pseudo_random_ys, [](double z, double dz) -> double { return -dz; });
experiment("Rule 3:", pseudo_random_xs, pseudo_random_ys, [](double z, double dz) -> double { return -(z + dz); });
experiment("Rule 4:", pseudo_random_xs, pseudo_random_ys, [](double z, double dz) -> double { return z + dz; });
}
You may also check:How to resolve the algorithm Hailstone sequence step by step in the RPL programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the D programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Go programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the FutureBasic programming language