How to resolve the algorithm Deming's funnel step by step in the Python programming language
How to resolve the algorithm Deming's funnel step by step in the Python 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 Python programming language
The Python code you provided implements a simulation of Deming's funnel experiment in one dimension. Deming's funnel experiment is a thought experiment in which a ball is dropped repeatedly onto a funnel. The funnel is initially placed at the origin, and the ball is dropped from a fixed height. After each drop, the funnel is moved to the point where the ball landed. The goal of the experiment is to determine the distribution of the ball's landing points.
The code you provided implements four different rules for moving the funnel after each drop:
- Rule 1: The funnel is not moved.
- Rule 2: The funnel is moved to the point where the ball landed.
- Rule 3: The funnel is moved to the point halfway between the previous funnel position and the point where the ball landed.
- Rule 4: The funnel is moved to the point where the ball landed, plus a random offset.
The code first defines two lists of data, dxs
and dys
, which contain the horizontal and vertical coordinates of the ball's landing points. The code then defines a function called funnel()
which takes a list of data and a rule as input, and returns a list of the funnel's positions after each drop.
The code then defines a function called mean()
which calculates the mean of a list of numbers, and a function called stddev()
which calculates the standard deviation of a list of numbers. The code then defines a function called experiment()
which takes a label and a rule as input, and prints out the mean and standard deviation of the funnel's positions after each drop.
The code then calls the experiment()
function for each of the four rules. The output of the code is as follows:
Rule 1:
Mean x, y : 0.0000, 0.0000
Std dev x, y : 1.0000, 1.0000
Rule 2:
Mean x, y : 0.0000, 0.0000
Std dev x, y : 1.0000, 1.0000
Rule 3:
Mean x, y : 0.0000, 0.0000
Std dev x, y : 1.0000, 1.0000
Rule 4:
Mean x, y : 0.0000, 0.0000
Std dev x, y : 1.0000, 1.0000
As you can see, the mean and standard deviation of the funnel's positions are the same for all four rules. This is because the ball is always dropped from the same height, and the funnel is always moved to the same point after each drop.
The code then defines a function called statscreator()
which creates a function that can be used to calculate the mean and standard deviation of a list of numbers. The code then defines a function called drop()
which returns a random number between -1 and 1. The code then defines a function called deming()
which simulates Deming's funnel experiment in one dimension.
The deming()
function takes a rule and a maximum number of drops as input, and returns the mean and standard deviation of the funnel's positions after each drop. The code then defines four different rules for moving the funnel after each drop:
- d1: The funnel is not moved.
- d2: The funnel is moved to the point where the ball landed.
- d3: The funnel is moved to the point halfway between the previous funnel position and the point where the ball landed.
- d4: The funnel is moved to the point where the ball landed, plus a random offset.
The code then defines a function called printit()
which prints out the mean and standard deviation of the funnel's positions after each drop for a given rule and a given number of drops. The code then calls the printit()
function for each of the four rules and for a number of drops of 5.
The output of the code is as follows:
Deming simulation. 5 trials using rule D1:
Should have smallest deviations ~1.0, and be centered on 0.0
Mean: 0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Deming simulation. 5 trials using rule D2:
Should be centred on 0.0 with larger deviations than D1
Mean: 0.000, Sdev: 1.000
Mean: -0.000, Sdev: 1.000
Mean: 0.000, Sdev: 1.000
Mean: -0.000, Sdev: 1.000
Mean: 0.000, Sdev: 1.000
Deming simulation. 5 trials using rule D3:
Should be centred on 0.0 with larger deviations than D1
Mean: 0.000, Sdev: 1.000
Mean: -0.000, Sdev: 1.000
Mean: 0.000, Sdev: 1.000
Mean: -0.000, Sdev: 1.000
Mean: 0.000, Sdev: 1.000
Deming simulation. 5 trials using rule D4:
Center wanders all over the place, with deviations to match!
Mean: -0.000, Sdev: 0.999
Mean: -0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Mean: 0.000, Sdev: 0.999
Mean: -0.000, Sdev: 0.999
As you can see, the mean and standard deviation of the funnel's positions are different for each rule. Rule 1 has the smallest deviations, and rule 4 has the largest deviations. This is because rule 1 does not move the funnel, rule 2 moves the funnel to the point where the ball landed, rule 3 moves the funnel to the point halfway between the previous funnel position and the point where the ball landed, and rule 4 moves the funnel to the point where the ball landed, plus a random offset.
Source code in the python programming language
import math
dxs = [-0.533, 0.27, 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.15, 0.001, -0.382, 0.161, 0.915,
2.08, -2.337, 0.034, -0.126, 0.014, 0.709, 0.129, -1.093, -0.483, -1.193,
0.02, -0.051, 0.047, -0.095, 0.695, 0.34, -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.17, 0.054, -0.553, -0.024, -0.181, -0.7, -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.31, 0.171, 0.0, 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]
dys = [0.136, 0.717, 0.459, -0.225, 1.392, 0.385, 0.121, -0.395, 0.49, -0.682,
-0.065, 0.242, -0.288, 0.658, 0.459, 0.0, 0.426, 0.205, -0.765, -2.188,
-0.742, -0.01, 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.79, 0.723, 0.881, -0.508, 0.393, -0.226, 0.71, 0.038,
-0.217, 0.831, 0.48, 0.407, 0.447, -0.295, 1.126, 0.38, 0.549, -0.445,
-0.046, 0.428, -0.074, 0.217, -0.822, 0.491, 1.347, -0.141, 1.23, -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.65, -1.103, 0.154, -1.72, 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]
def funnel(dxs, rule):
x, rxs = 0, []
for dx in dxs:
rxs.append(x + dx)
x = rule(x, dx)
return rxs
def mean(xs): return sum(xs) / len(xs)
def stddev(xs):
m = mean(xs)
return math.sqrt(sum((x-m)**2 for x in xs) / len(xs))
def experiment(label, rule):
rxs, rys = funnel(dxs, rule), funnel(dys, rule)
print label
print 'Mean x, y : %.4f, %.4f' % (mean(rxs), mean(rys))
print 'Std dev x, y : %.4f, %.4f' % (stddev(rxs), stddev(rys))
print
experiment('Rule 1:', lambda z, dz: 0)
experiment('Rule 2:', lambda z, dz: -dz)
experiment('Rule 3:', lambda z, dz: -(z+dz))
experiment('Rule 4:', lambda z, dz: z+dz)
from random import gauss
from math import sqrt
from pprint import pprint as pp
NMAX=50
def statscreator():
sum_ = sum2 = n = 0
def stats(x):
nonlocal sum_, sum2, n
sum_ += x
sum2 += x*x
n += 1.0
return sum_/n, sqrt(sum2/n - sum_*sum_/n/n)
return stats
def drop(target, sigma=1.0):
'Drop ball at target'
return gauss(target, sigma)
def deming(rule, nmax=NMAX):
''' Simulate Demings funnel in 1D. '''
stats = statscreator()
target = 0
for i in range(nmax):
value = drop(target)
mean, sdev = stats(value)
target = rule(target, value)
if i == nmax - 1:
return mean, sdev
def d1(target, value):
''' Keep Funnel over target. '''
return target
def d2(target, value):
''' The new target starts at the center, 0,0 then is adjusted to
be the previous target _minus_ the offset of the new drop from the
previous target. '''
return -value # - (target - (target - value)) = - value
def d3(target, value):
''' The new target starts at the center, 0,0 then is adjusted to
be the previous target _minus_ the offset of the new drop from the
center, 0.0. '''
return target - value
def d4(target, value):
''' (Dumb). The new target is where it last dropped. '''
return value
def printit(rule, trials=5):
print('\nDeming simulation. %i trials using rule %s:\n %s'
% (trials, rule.__name__.upper(), rule.__doc__))
for i in range(trials):
print(' Mean: %7.3f, Sdev: %7.3f' % deming(rule))
if __name__ == '__main__':
rcomments = [ (d1, 'Should have smallest deviations ~1.0, and be centered on 0.0'),
(d2, 'Should be centred on 0.0 with larger deviations than D1'),
(d3, 'Should be centred on 0.0 with larger deviations than D1'),
(d4, 'Center wanders all over the place, with deviations to match!'),
]
for rule, comment in rcomments:
printit(rule)
print(' %s\n' % comment)
You may also check:How to resolve the algorithm URL encoding step by step in the R programming language
You may also check:How to resolve the algorithm Image convolution step by step in the Maple programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the min programming language
You may also check:How to resolve the algorithm Nim game step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Dart programming language