Deming's funnel: Difference between revisions
Added FreeBASIC
SqrtNegInf (talk | contribs) m (→{{header|Sidef}}: Fix link: Perl 6 --> Raku) |
(Added FreeBASIC) |
||
(26 intermediate revisions by 15 users not shown) | |||
Line 19:
* Further [http://blog.newsystemsthinking.com/w-edwards-deming-and-the-funnel-experiment/ explanation and interpretation]
* [https://www.youtube.com/watch?v=2VogtYRc9dA Video demonstration] of the funnel experiment at the Mayo Clinic.
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V 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]
V 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]
F funnel(dxs, rule)
V x = 0.0
[Float] rxs
L(dx) dxs
rxs.append(x + dx)
x = rule(x, dx)
R rxs
F mean(xs)
R sum(xs) / xs.len
F stddev(xs)
V m = mean(xs)
R sqrt(sum(xs.map(x -> (x - @m) ^ 2)) / xs.len)
F experiment(label, rule)
V (rxs, rys) = (funnel(:dxs, rule), funnel(:dys, rule))
print(label)
print(‘Mean x, y : #.4, #.4’.format(mean(rxs), mean(rys)))
print(‘Std dev x, y : #.4, #.4’.format(stddev(rxs), stddev(rys)))
print()
experiment(‘Rule 1:’, (z, dz) -> 0)
experiment(‘Rule 2:’, (z, dz) -> -dz)
experiment(‘Rule 3:’, (z, dz) -> -(z + dz))
experiment(‘Rule 4:’, (z, dz) -> z + dz)</syntaxhighlight>
{{out}}
<pre>
Rule 1:
Mean x, y : 0.0004, 0.0702
Std dev x, y : 0.7153, 0.6462
Rule 2:
Mean x, y : 0.0009, -0.0103
Std dev x, y : 1.0371, 0.8999
Rule 3:
Mean x, y : 0.0439, -0.0063
Std dev x, y : 7.9871, 4.7784
Rule 4:
Mean x, y : 3.1341, 5.4210
Std dev x, y : 1.5874, 3.9304
</pre>
=={{header|Ada}}==
{{trans|Go}}
<
with Ada.Text_IO;
Line 126 ⟶ 199:
Experiment ("Rule 3", Rule_3'Access);
Experiment ("Rule 4", Rule_4'Access);
end Demings_Funnel;</
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">Dxs: @[
neg 0.533, 0.270, 0.859, neg 0.043, neg 0.205, neg 0.127, neg 0.071, 0.275,
1.251, neg 0.231, neg 0.401, 0.269, 0.491, 0.951, 1.150, 0.001,
neg 0.382, 0.161, 0.915, 2.080, neg 2.337, 0.034, neg 0.126, 0.014,
0.709, 0.129, neg 1.093, neg 0.483, neg 1.193, 0.020, neg 0.051, 0.047,
neg 0.095, 0.695, 0.340, neg 0.182, 0.287, 0.213, neg 0.423, neg 0.021,
neg 0.134, 1.798, 0.021, neg 1.099, neg 0.361, 1.636, neg 1.134, 1.315,
0.201, 0.034, 0.097, neg 0.170, 0.054, neg 0.553, neg 0.024, neg 0.181,
neg 0.700, neg 0.361, neg 0.789, 0.279, neg 0.174, neg 0.009, neg 0.323, neg 0.658,
0.348, neg 0.528, 0.881, 0.021, neg 0.853, 0.157, 0.648, 1.774,
neg 1.043, 0.051, 0.021, 0.247, neg 0.310, 0.171, 0.000, 0.106,
0.024, neg 0.386, 0.962, 0.765, neg 0.125, neg 0.289, 0.521, 0.017,
0.281, neg 0.749, neg 0.149, neg 2.436, neg 0.909, 0.394, neg 0.113, neg 0.598,
0.443, neg 0.521, neg 0.799, 0.087
]
Dys: @[
0.136, 0.717, 0.459, neg 0.225, 1.392, 0.385, 0.121, neg 0.395,
0.490, neg 0.682, neg 0.065, 0.242, neg 0.288, 0.658, 0.459, 0.000,
0.426, 0.205, neg 0.765, neg 2.188, neg 0.742, neg 0.010, 0.089, 0.208,
0.585, 0.633, neg 0.444, neg 0.351, neg 1.087, 0.199, 0.701, 0.096,
neg 0.025, neg 0.868, 1.051, 0.157, 0.216, 0.162, 0.249, neg 0.007,
0.009, 0.508, neg 0.790, 0.723, 0.881, neg 0.508, 0.393, neg 0.226,
0.710, 0.038, neg 0.217, 0.831, 0.480, 0.407, 0.447, neg 0.295,
1.126, 0.380, 0.549, neg 0.445, neg 0.046, 0.428, neg 0.074, 0.217,
neg 0.822, 0.491, 1.347, neg 0.141, 1.230, neg 0.044, 0.079, 0.219,
0.698, 0.275, 0.056, 0.031, 0.421, 0.064, 0.721, 0.104,
neg 0.729, 0.650, neg 1.103, 0.154, neg 1.720, 0.051, neg 0.385, 0.477,
1.537, neg 0.901, 0.939, neg 0.411, 0.341, neg 0.411, 0.106, 0.224,
neg 0.947, neg 1.424, neg 0.542, neg 1.032
]
funnel: function [a, rule][
x: 0.0
result: []
loop a 'val [
'result ++ x + val
x: do rule
]
return result
]
formatFloat: function [f]->
to :string .format:"7.4f" f
experiment: function [label, rule][
rxs: funnel Dxs rule
rys: funnel Dys rule
print label
print repeat "=" 30
print ["Mean x,y :" formatFloat average rxs, formatFloat average rys]
print ["Std.dev x,y :" formatFloat deviation rxs, formatFloat deviation rys]
print ""
]
experiment "Rule 1" [0.0]
experiment "Rule 2" [neg val]
experiment "Rule 3" [neg x + val]
experiment "Rule 4" [x + val]</syntaxhighlight>
{{out}}
<pre>Rule 1
==============================
Mean x,y : 0.0004 0.0702
Std.dev x,y : 0.7153 0.6462
Rule 2
==============================
Mean x,y : 0.0009 -0.0103
Std.dev x,y : 1.0371 0.8999
Rule 3
==============================
Mean x,y : 0.0439 -0.0063
Std.dev x,y : 7.9871 4.7784
Rule 4
==============================
Mean x,y : 3.1341 5.4210
Std.dev x,y : 1.5874 3.9304</pre>
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Linq;
public class DemingsFunnel
{
public static void Main(string[] args)
{
double[] dxs = {
-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};
double[] dys = {
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:", dxs, dys, (z, dz) => 0.0);
Experiment("Rule 2:", dxs, dys, (z, dz) => -dz);
Experiment("Rule 3:", dxs, dys, (z, dz) => -(z + dz));
Experiment("Rule 4:", dxs, dys, (z, dz) => z + dz);
}
static void Experiment(string label, double[] dxs, double[] dys, Func<double, double, double> rule)
{
double[] resx = Funnel(dxs, rule);
double[] resy = Funnel(dys, rule);
Console.WriteLine(label);
Console.WriteLine($"Mean x, y: {Mean(resx):F4}, {Mean(resy):F4}");
Console.WriteLine($"Std dev x, y: {StdDev(resx):F4}, {StdDev(resy):F4}");
Console.WriteLine();
}
static double[] Funnel(double[] input, Func<double, double, double> rule)
{
double x = 0;
double[] result = new double[input.Length];
for (int i = 0; i < input.Length; i++)
{
double rx = x + input[i];
x = rule(x, input[i]);
result[i] = rx;
}
return result;
}
static double Mean(double[] xs)
{
return xs.Average();
}
static double StdDev(double[] xs)
{
double m = Mean(xs);
return Math.Sqrt(xs.Select(x => Math.Pow((x - m), 2)).Sum() / xs.Length);
}
}
</syntaxhighlight>
{{out}}
<pre>
Rule 1:
Mean x, y: 0.0004, 0.0702
Std dev x, y: 0.7153, 0.6462
Rule 2:
Mean x, y: 0.0009, -0.0103
Std dev x, y: 1.0371, 0.8999
Rule 3:
Mean x, y: 0.0439, -0.0063
Std dev x, y: 7.9871, 4.7784
Rule 4:
Mean x, y: 3.1341, 5.4210
Std dev x, y: 1.5874, 3.9304
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#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; });
}
</syntaxhighlight>
{{ out }}
<pre>
Rule 1:
-----------------------------------------
Mean x, y : 0.0004, 0.0702
Standard deviation x, y: 0.7153, 0.6462
Rule 2:
-----------------------------------------
Mean x, y : 0.0009, -0.0103
Standard deviation x, y: 1.0371, 0.8999
Rule 3:
-----------------------------------------
Mean x, y : 0.0439, -0.0063
Standard deviation x, y: 7.9871, 4.7784
Rule 4:
-----------------------------------------
Mean x, y : 3.1341, 5.4210
Standard deviation x, y: 1.5874, 3.9304
</pre>
=={{header|D}}==
{{trans|Python}}
<
auto mean(T)(in T[] xs) pure nothrow @nogc {
Line 206 ⟶ 568:
experiment("Rule 3:", dxs, dys, (z, dz) => -(z + dz));
experiment("Rule 4:", dxs, dys, (z, dz) => z + dz);
}</
{{out}}
<pre>Rule 1:
Line 223 ⟶ 585:
Mean x, y: 3.1341, 5.4210
Std dev x, y: 1.5874, 3.9304</pre>
=={{header|EasyLang}}==
{{trans|Python}}
<syntaxhighlight>
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 ]
#
proc funnel rule . dxs[] rxs[] .
rxs[] = [ ]
for dx in dxs[]
rxs[] &= x + dx
if rule = 1
x = 0
elif rule = 2
x = -dx
elif rule = 3
x = -(x + dx)
else
x = x + dx
.
.
.
proc mean . xs[] r .
r = 0
for x in xs[]
r += x
.
r /= len xs[]
.
proc stddev . xs[] r .
mean xs[] m
for x in xs[]
s += (x - m) * (x - m)
.
r = sqrt (s / len xs[])
.
proc experiment rule . .
funnel rule dxs[] rxs[]
funnel rule dys[] rys[]
print "Rule " & rule
mean rxs[] mx
mean rys[] my
print "Mean x, y : " & mx & " " & my
stddev rxs[] dx
stddev rys[] dy
print "Std dev x, y : " & dx & " " & dy
print ""
.
numfmt 4 0
experiment 1
experiment 2
experiment 3
experiment 4
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def funnel(dxs, rule) do
{_, rxs} = Enum.reduce(dxs, {0, []}, fn dx,{x,rxs} ->
Line 281 ⟶ 699:
Deming.experiment("Rule 2:", dxs, dys, fn _z, dz -> -dz end)
Deming.experiment("Rule 3:", dxs, dys, fn z, dz -> -(z+dz) end)
Deming.experiment("Rule 4:", dxs, dys, fn z, dz -> z+dz end)</
{{out}}
Line 304 ⟶ 722:
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
<
kernel math math.statistics sequences ;
Line 347 ⟶ 765:
[ "Rule 3:" print [ 0 [ - neg ] accumulate* ] bi@ ]
[ "Rule 4:" print [ cum-sum ] bi@ ]
} [ show ] map-compose 2cleave</
{{out}}
<pre>
Line 363 ⟶ 781:
Std dev x, y : 1.5874, 3.9304
</pre>
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Dim Shared As Double DXs(100) => {_
-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}
Dim Shared As Double DYs(100) => { _
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}
Function SumArray(arr() As Double) As Double
Dim As Double sum = 0.0
For i As Integer = Lbound(arr) To Ubound(arr)
sum += arr(i)
Next i
Return sum
End Function
Sub Funnel(DXs() As Double, rule As Integer, rxs() As Double)
Dim As Double x = 0.0
Dim As Integer i
For i = 1 To Ubound(dxs)
Dim As Double dx = DXs(i)
rxs(i) = x + dx
Select Case rule
Case 2: x = -dx
Case 3: x = -(x+dx)
Case 4: x = x+dx
End Select
Next i
End Sub
Function Mean(xs() As Double) As Double
Return SumArray(xs())/Ubound(xs)
End Function
Function StdDev(xs() As Double) As Double
Dim As Double m = Mean(xs())
Dim As Double sum = 0.0
For i As Integer = Lbound(xs) To Ubound(xs)
sum += (xs(i) - m) ^ 2
Next i
Return Sqr(sum / Ubound(xs))
End Function
Sub experiment(n As Integer, DXs() As Double, DYs() As Double)
Dim As Double rxs(Ubound(dxs)), rys(Ubound(dys))
Funnel(DXs(), n, rxs())
Funnel(DYs(), n, rys())
Print Using "Mean x, y : ###.####, ###.####"; mean(rxs()); mean(rys())
Print Using "Std dev x, y : ###.####, ###.####"; stddev(rxs()); stddev(rys())
End Sub
For i As Integer = 1 To 4
experiment(i, DXs(), DYs())
Next i
Sleep</syntaxhighlight>
{{out}}
<pre>Mean x, y : 0.0057, 0.0689
Std dev x, y : 0.7133, 0.6462
Mean x, y : -0.0000, 0.0000
Std dev x, y : 1.0330, 0.9068
Mean x, y : -0.0381, 0.0752
Std dev x, y : 7.5940, 4.7279
Mean x, y : 3.6729, 5.3539
Std dev x, y : 1.6174, 3.9340</pre>
=={{header|Go}}==
{{trans|Python}}
<
import (
Line 456 ⟶ 965:
return z + dz
})
}</
{{out}}
Line 479 ⟶ 988:
=={{header|Haskell}}==
{{trans|Python}}
<
import Text.Printf
Line 535 ⟶ 1,044:
experiment "Rule 2:" dxs dys (\_ dz -> -dz)
experiment "Rule 3:" dxs dys (\z dz -> -(z+dz))
experiment "Rule 4:" dxs dys (\z dz -> z+dz)</
{{out}}
Line 558 ⟶ 1,067:
=={{header|J}}==
<syntaxhighlight lang="j">
dx=:".0 :0-.LF
_0.533 0.270 0.859 _0.043 _0.205 _0.127 _0.071 0.275
Line 610 ⟶ 1,119:
smoutput ' Rule 4 (x,y):'
smoutput ' Mean: ',":dx ,&mean&Rule4 dy
smoutput ' Std dev: ',":dx ,&stddev&Rule4 dy</
Displayed result:
Line 635 ⟶ 1,144:
Translation of [[Deming's_Funnel#Python|Python]] via [[Deming's_Funnel#D|D]]
{{works with|Java|8}}
<
import java.util.Arrays;
import java.util.function.BiFunction;
Line 709 ⟶ 1,218:
return sqrt(Arrays.stream(xs).map(x -> pow((x - m), 2)).sum() / xs.length);
}
}</
<pre>Rule 1:
Line 726 ⟶ 1,235:
Mean x, y: 3,1341, 5,4210
Std dev x, y: 1,5874, 3,9304</pre>
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq, and with fq'''
'''Preliminaries'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
# Simplistic approach:
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p;
# Emit {mean, ssdev, std} where std is (ssdev/length|sqrt)
def basic_statistics:
. as $in
| length as $length
| (add / $length) as $mean
| { $mean,
ssdev: (reduce $in[] as $x (0; . + (($x - $mean) | .*.))) }
| .std = ((.ssdev / $length ) | sqrt);
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
def dxs: [
-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
];
def dys: [
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
];
# fa is an array
# r is an expression that expects [x,f] as input
def funnel(fa; r):
{ x: 0, res: []}
| reduce range(0;fa|length) as $i (.;
fa[$i] as $f
| .res[$i] = .x + $f
| .x |= ([., $f] | r ) )
| .res;
# r is an expression as per `funnel`
def experiment(alabel; r):
def pp: round(4) | lpad(8);
(funnel(dxs; r) | basic_statistics) as $x
| (funnel(dys; r) | basic_statistics) as $y
| "\(alabel) : x y",
"Mean : \($x.mean|pp) \($y.mean|pp)",
"Std Dev : \($x.std|pp) \($y.std|pp)" ;
def task:
experiment("\nRule 1"; 0 ),
experiment("\nRule 2"; -.[1] ),
experiment("\nRule 3"; - add),
experiment("\nRule 4"; add ) ;
task
</syntaxhighlight>
{{output}}
<pre>
Rule 1 : x y
Mean : 0.0004 0.0702
Std Dev : 0.7153 0.6462
Rule 2 : x y
Mean : 0.0009 -0.0103
Std Dev : 1.0371 0.8999
Rule 3 : x y
Mean : 0.0439 -0.0063
Std Dev : 7.9871 4.7784
Rule 4 : x y
Mean : 3.1341 5.421
Std Dev : 1.5874 3.9304
</pre>
=={{header|Julia}}==
<
using Statistics, Distributions, Plots
Line 783 ⟶ 1,394:
testfunnel(false)
display(plots)
</
<pre>
Using Racket data.
Line 808 ⟶ 1,419:
=={{header|Kotlin}}==
{{trans|Python}}
<
typealias Rule = (Double, Double) -> Double
Line 875 ⟶ 1,486:
experiment("Rule 3") { z, dz -> -(z + dz) }
experiment("Rule 4") { z, dz -> z + dz }
}</
{{out}}
Line 895 ⟶ 1,506:
Std Dev : 1.5874, 3.9304
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">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.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.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};
(*Mathematica's StandardDeviation function computes the unbiased standard deviation. The solutions seem to be using the biased standard deviation, so I'll create a custom function for that.*)
BiasedStandardDeviation[data_] :=
With[
{mean = Mean@data},
Sqrt[Total[(# - mean)^2 & /@ data]/Length[data]]
]
(*Mathematica's FoldPair functionality will work well with this if we provide a properly defined function to fold with.*)
DemingRule[1][funnelPosition_, diff_] := {funnelPosition + diff, 0};
DemingRule[2][funnelPosition_, diff_] := {funnelPosition + diff, -diff};
DemingRule[3][funnelPosition_, diff_] := {funnelPosition + diff, -funnelPosition - diff};
DemingRule[4][funnelPosition_, diff_] := {funnelPosition + diff, funnelPosition + diff};
(*The core implementation.*)
MarblePositions[rule_][diffs_] := FoldPairList[DemingRule[rule], 0, diffs];
(*This is to help format the output.*)
Results[rule_, diffData_] :=
With[
{positions = MarblePositions[rule][diffData]},
StringForm["Rule `1`\nmean: `2`\nstd dev: `3`", rule, Mean[positions], BiasedStandardDeviation[positions]]
];
TableForm[Results[#, Transpose[{dxs, dys}]] & /@ Range[4], TableSpacing -> 5]</syntaxhighlight>
{{out}}
<pre>Rule 1
mean: {0.0004,0.07023}
std dev: {0.715271,0.646206}
Rule 2
mean: {0.00087,-0.01032}
std dev: {1.03714,0.899948}
Rule 3
mean: {0.04386,-0.0063}
std dev: {7.98712,4.77842}
Rule 4
mean: {3.13412,5.42102}
std dev: {1.58739,3.93036}</pre>
=== Stretch 1 ===
<syntaxhighlight lang="mathematica">
RadiusDistribution = NormalDistribution[0, 1];
AngleDistribution = UniformDistribution[{0, Pi}];
(*Mathematica has built in transformation functions, but this seems clearer given the way the instructions were written.*)
ToCartesian[{r_, a_}] := ToCartesian[{Abs@r, a - Pi}] /; Negative[r];
ToCartesian[{r_, a_}] := FromPolarCoordinates[{r, a}];
newData =
ToCartesian /@
Transpose[{RandomVariate[RadiusDistribution, 100],
RandomVariate[AngleDistribution, 100]}];
TableForm[Results[#, newData] & /@ Range[4], TableSpacing -> 5]
</syntaxhighlight>
{{out}}
<pre>
Rule 1
mean: {0.0236483,-0.0480581}
std dev: {0.75398,0.678437}
Rule 2
mean: {-0.00586115,0.00205628}
std dev: {1.07625,0.922341}
Rule 3
mean: {0.0180857,-0.0707311}
std dev: {2.53086,4.29764}
Rule 4
mean: {1.78937,-0.132491}
std dev: {2.36082,3.15051}
</pre>
=== Stretch 2 ===
<syntaxhighlight lang="mathematica">
ListPlot[MarblePositions[#][Transpose[{dxs,dys}]]&/@Range[4],PlotLegends->PointLegend[{1,2,3,4}],AspectRatio->Automatic,ImageSize->600]
</syntaxhighlight>
{{out}}
~images disabled~
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import stats, strformat
type Rule = proc(x, y: float): float
const Dxs = [-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 Dys = [ 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]
func funnel(a: openArray[float]; rule: Rule): seq[float] =
var x = 0.0
result.setlen(a.len)
for i, val in a:
result[i] = x + val
x = rule(x, val)
proc experiment(label: string; r: Rule) =
let rxs = funnel(Dxs, r)
let rys = funnel(Dys, r)
echo label
echo fmt"Mean x, y : {rxs.mean:7.4f} {rys.mean:7.4f}"
echo fmt"Std dev x, y : {rxs.standardDeviation:7.4f} {rys.standardDeviation:7.4f}"
echo ""
experiment("Rule 1", proc(z, dz: float): float = 0.0)
experiment("Rule 2", proc(z, dz: float): float = -dz)
experiment("Rule 3", proc(z, dz: float): float = -(z + dz))
experiment("Rule 4", proc(z, dz: float): float = z + dz)</syntaxhighlight>
{{out}}
<pre>Rule 1
Mean x, y : 0.0004 0.0702
Std dev x, y : 0.7153 0.6462
Rule 2
Mean x, y : 0.0009 -0.0103
Std dev x, y : 1.0371 0.8999
Rule 3
Mean x, y : 0.0439 -0.0063
Std dev x, y : 7.9871 4.7784
Rule 4
Mean x, y : 3.1341 5.4210
Std dev x, y : 1.5874 3.9304</pre>
=={{header|PARI/GP}}==
:''This is a work-in-progress.''
<
my(v=vector(drops),target=0);
v[1]=rule(target, 0);
Line 923 ⟶ 1,721:
print()
)
}</
=={{header|Perl}}==
<
-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
Line 976 ⟶ 1,774:
printf "Mean x, y : %7.4f %7.4f\n", mean(@ddx), mean(@ddy);
printf "Std dev x, y : %7.4f %7.4f\n", stddev(@ddx), stddev(@ddy);
}</
{{out}}<pre>
Rule 1
Line 992 ⟶ 1,790:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">funnel</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dxs</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rule</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rxs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dxs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dxs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">rxs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rxs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">switch</span> <span style="color: #000000;">rule</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">dx</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">case</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dx</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">rxs</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">xs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">stddev</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">xs</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">experiment</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">dxs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dys</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rxs</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">funnel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dxs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">rys</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">funnel</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dys</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mean x, y : %7.4f, %7.4f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rxs</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rys</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Std dev x, y : %7.4f, %7.4f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">stddev</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rxs</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">stddev</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rys</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">dxs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">0.533</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.270</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.859</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.043</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.205</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.127</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.071</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.275</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">1.251</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.231</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.401</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.269</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.491</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.951</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.150</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.001</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.382</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.161</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.915</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2.080</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2.337</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.034</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.126</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.014</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.709</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.129</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.093</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.483</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.193</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.020</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.051</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.047</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.095</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.695</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.340</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.182</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.287</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.213</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.423</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.021</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.134</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.798</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.021</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.099</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.361</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.636</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.134</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.315</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.201</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.034</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.097</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.170</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.054</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.553</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.024</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.181</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.700</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.361</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.789</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.279</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.174</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.009</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.323</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.658</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.348</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.528</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.881</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.021</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.853</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.157</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.648</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.774</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">1.043</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.051</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.021</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.247</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.310</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.171</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.106</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.024</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.386</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.962</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.765</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.125</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.289</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.521</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.017</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.281</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.749</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.149</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2.436</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.909</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.394</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.113</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.598</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.443</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.521</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.799</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.087</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">dys</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">0.136</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.717</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.459</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.225</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.392</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.385</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.121</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.395</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.490</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.682</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.065</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.242</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.288</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.658</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.459</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.000</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.426</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.205</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.765</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2.188</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.742</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.010</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.089</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.208</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.585</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.633</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.444</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.351</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.087</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.199</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.701</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.096</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.025</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.868</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.051</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.157</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.216</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.162</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.249</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.007</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.009</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.508</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.790</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.723</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.881</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.508</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.393</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.226</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.710</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.038</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.217</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.831</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.480</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.407</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.447</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.295</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">1.126</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.380</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.549</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.445</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.046</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.428</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.074</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.217</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.822</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.491</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.347</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.141</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1.230</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.044</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.079</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.219</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">0.698</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.275</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.056</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.031</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.421</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.064</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.721</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.104</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.729</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.650</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.103</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.154</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.720</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.051</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.385</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.477</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">1.537</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.901</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.939</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.411</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.341</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.411</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.106</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.224</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.947</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.424</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">0.542</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1.032</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">experiment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dxs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dys</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,068 ⟶ 1,869:
=={{header|Python}}==
{{trans|Racket}}
<
dxs = [-0.533, 0.27, 0.859, -0.043, -0.205, -0.127, -0.071, 0.275, 1.251,
Line 1,117 ⟶ 1,918:
experiment('Rule 2:', lambda z, dz: -dz)
experiment('Rule 3:', lambda z, dz: -(z+dz))
experiment('Rule 4:', lambda z, dz: z+dz)</
{{output}}
Line 1,138 ⟶ 1,939:
'''Alternative''': [Generates pseudo-random data and gives some interpretation.] The funnel experiment is performed in one dimension. The other dimension would act similarly.
<
from math import sqrt
from pprint import pprint as pp
Line 1,212 ⟶ 2,013:
for rule, comment in rcomments:
printit(rule)
print(' %s\n' % comment)</
{{out}}
Line 1,260 ⟶ 2,061:
=={{header|Racket}}==
The stretch solutions can be obtained by uncommenting radii etc. (delete the 4 semi-colons) to generate fresh data, and scatter-plots can be obtained by deleting the #; .
<
(require math/distributions math/statistics plot)
Line 1,310 ⟶ 2,111:
(experiment "Rule 2:" (λ (z dz) (- dz)))
(experiment "Rule 3:" (λ (z dz) (- (+ z dz))))
(experiment "Rule 4:" (λ (z dz) (+ z dz))) </
{{output}}
Line 1,334 ⟶ 2,135:
(formerly Perl 6)
{{Works with|Rakudo|2018.10}}
<syntaxhighlight lang="raku"
sub stddev {
# <(x - <x>)²> = <x²> - <x>²
Line 1,386 ⟶ 2,187:
printf "Mean x, y : %7.4f %7.4f\n", mean(@z».re), mean(@z».im);
printf "Std dev x, y : %7.4f %7.4f\n", stddev(@z».re), stddev(@z».im);
}</
{{out}}
<pre>Rule 1:
Line 1,403 ⟶ 2,204:
=={{header|Ruby}}==
{{trans|Python}}
<
x, rxs = 0, []
for dx in dxs
Line 1,458 ⟶ 2,259:
experiment('Rule 2:', dxs, dys) {|z, dz| -dz}
experiment('Rule 3:', dxs, dys) {|z, dz| -(z+dz)}
experiment('Rule 4:', dxs, dys) {|z, dz| z+dz}</
{{out}}
Line 1,478 ⟶ 2,279:
Std dev x, y : 1.5874, 3.9304
</pre>
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object DemingsFunnel {
def main(args: Array[String]): Unit = {
val dxs = Array(
-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
)
val dys = Array(
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:", dxs, dys, (z, dz) => 0.0)
experiment("Rule 2:", dxs, dys, (z, dz) => -dz)
experiment("Rule 3:", dxs, dys, (z, dz) => -(z + dz))
experiment("Rule 4:", dxs, dys, (z, dz) => z + dz)
}
def experiment(label: String, dxs: Array[Double], dys: Array[Double], rule: (Double, Double) => Double): Unit = {
val resx = funnel(dxs, rule)
val resy = funnel(dys, rule)
println(label)
printf("Mean x, y: %.4f, %.4f%n", mean(resx), mean(resy))
printf("Std dev x, y: %.4f, %.4f%n", stdDev(resx), stdDev(resy))
println()
}
def funnel(input: Array[Double], rule: (Double, Double) => Double): Array[Double] = {
var x = 0.0
val result = new Array[Double](input.length)
for (i <- input.indices) {
val rx = x + input(i)
x = rule(x, input(i))
result(i) = rx
}
result
}
def mean(xs: Array[Double]): Double = xs.sum / xs.length
def stdDev(xs: Array[Double]): Double = {
val m = mean(xs)
math.sqrt(xs.map(x => math.pow((x - m), 2)).sum / xs.length)
}
}
</syntaxhighlight>
{{out}}
<pre>
Rule 1:
Mean x, y: 0.0004, 0.0702
Std dev x, y: 0.7153, 0.6462
Rule 2:
Mean x, y: 0.0009, -0.0103
Std dev x, y: 1.0371, 0.8999
Rule 3:
Mean x, y: 0.0439, -0.0063
Std dev x, y: 7.9871, 4.7784
Rule 4:
Mean x, y: 3.1341, 5.4210
Std dev x, y: 1.5874, 3.9304
</pre>
=={{header|Sidef}}==
{{trans|Raku}}
<
a.sum / a.len
}
Line 1,537 ⟶ 2,434:
printf("Mean x, y : %.4f %.4f\n", x̄(z.map{.re}), x̄(z.map{.im}))
printf("Std dev x, y : %.4f %.4f\n", σ(z.map{.re}), σ(z.map{.im}))
}</
{{out}}
<pre>
Line 1,558 ⟶ 2,455:
{{trans|Kotlin}}
<
let dxs = [
Line 1,633 ⟶ 2,530:
experiment(label: "Rule 2", rule: {_, dz in -dz })
experiment(label: "Rule 3", rule: {z, dz in -(z + dz) })
experiment(label: "Rule 4", rule: {z, dz in z + dz })</
{{out}}
Line 1,656 ⟶ 2,553:
{{works with|Tcl|8.6}}
{{trans|Ruby}}
<
namespace path {tcl::mathop tcl::mathfunc}
Line 1,714 ⟶ 2,611:
experiment "Rule 2:" $dxs $dys {{z dz} {expr {-$dz}}}
experiment "Rule 3:" $dxs $dys {{z dz} {expr {-($z+$dz)}}}
experiment "Rule 4:" $dxs $dys {{z dz} {expr {$z+$dz}}}</
The first stretch goal:
{{tcllib|math::constants}}
{{tcllib|simulation::random}}
<
package require simulation::random
Line 1,737 ⟶ 2,634:
experiment "Rule 2:" $dxs $dys {{z dz} {expr {-$dz}}}
experiment "Rule 3:" $dxs $dys {{z dz} {expr {-($z+$dz)}}}
experiment "Rule 4:" $dxs $dys {{z dz} {expr {$z+$dz}}}</
{{out}}
<pre>
Line 1,773 ⟶ 2,670:
Mean x, y : -6.3314, -4.0168
Std dev x, y : 3.2387, 4.4825
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math
type Rule = fn(f64, f64) f64
const (
dxs = [
-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,
]
dys = [
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,
]
)
fn funnel(fa []f64, r Rule) []f64 {
mut x := 0.0
mut result := []f64{len: fa.len}
for i, f in fa {
result[i] = x + f
x = r(x, f)
}
return result
}
fn mean(fa []f64) f64 {
mut sum := 0.0
for f in fa {
sum += f
}
return sum / f64(fa.len)
}
fn std_dev(fa []f64) f64 {
m := mean(fa)
mut sum := 0.0
for f in fa {
sum += (f - m) * (f - m)
}
return math.sqrt(sum / f64(fa.len))
}
fn experiment(label string, r Rule) {
rxs := funnel(dxs, r)
rys := funnel(dys, r)
println("$label : x y")
println("Mean : ${mean(rxs):7.4f}, ${mean(rys):7.4f}")
println("Std Dev : ${std_dev(rxs):7.4f}, ${std_dev(rys):7.4f}")
println('')
}
fn main() {
experiment("Rule 1", fn(_ f64, _ f64) f64 {
return 0.0
})
experiment("Rule 2", fn(_ f64, dz f64) f64 {
return -dz
})
experiment("Rule 3", fn(z f64, dz f64) f64 {
return -(z + dz)
})
experiment("Rule 4", fn(z f64, dz f64) f64 {
return z + dz
})
}</syntaxhighlight>
{{out}}
<pre>
Rule 1 : x y
Mean : 0.0004, 0.0702
Std Dev : 0.7153, 0.6462
Rule 2 : x y
Mean : 0.0009, -0.0103
Std Dev : 1.0371, 0.8999
Rule 3 : x y
Mean : 0.0439, -0.0063
Std Dev : 7.9871, 4.7784
Rule 4 : x y
Mean : 3.1341, 5.4210
Std Dev : 1.5874, 3.9304
</pre>
=={{header|Wren}}==
{{trans|Go}}{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Nums
import "./fmt" for Fmt
var dxs = [
-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
]
var dys = [
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
]
var funnel = Fn.new { |fa, r|
var x = 0
var res = List.filled(fa.count, 0)
for (i in 0...fa.count) {
var f = fa[i]
res[i] = x + f
x = r.call(x, f)
}
return res
}
var experiment = Fn.new { |label, r|
var rxs = funnel.call(dxs, r)
var rys = funnel.call(dys, r)
System.print("%(label) : x y")
Fmt.print("Mean : $7.4f, $7.4f", Nums.mean(rxs), Nums.mean(rys))
Fmt.print("Std Dev : $7.4f, $7.4f", Nums.popStdDev(rxs), Nums.popStdDev(rys))
Fmt.print()
}
experiment.call("Rule 1") { |z, dz| 0 }
experiment.call("Rule 2") { |z, dz| -dz }
experiment.call("Rule 3") { |z, dz| -(z + dz) }
experiment.call("Rule 4") { |z, dz| z + dz }</syntaxhighlight>
{{out}}
<pre>
Rule 1 : x y
Mean : 0.0004, 0.0702
Std Dev : 0.7153, 0.6462
Rule 2 : x y
Mean : 0.0009, -0.0103
Std Dev : 1.0371, 0.8999
Rule 3 : x y
Mean : 0.0439, -0.0063
Std Dev : 7.9871, 4.7784
Rule 4 : x y
Mean : 3.1341, 5.4210
Std Dev : 1.5874, 3.9304
</pre>
=={{header|XPL0}}==
Works on RPi. MAlloc works differently in DOS versions and in EXPL.
<syntaxhighlight lang "XPL0">include xpllib; \for Print
func real Mean(Array, Size);
real Array; int Size;
real Sum;
int I;
[Sum:= 0.0;
for I:= 0 to Size-1 do
Sum:= Sum + Array(I);
return Sum / float(Size);
];
func real StdDev(Array, Size);
real Array; int Size;
real M, Sum;
int I;
[M:= Mean(Array, Size);
Sum:= 0.0;
for I:= 0 to Size-1 do
Sum:= Sum + (Array(I)-M) * (Array(I)-M);
return sqrt(Sum / float(Size));
];
func real Funnel(Array, Size, Rule);
real Array; int Size, Rule;
real Posn, Result, Fall;
int AddrResult, I;
def SizeOfReal = 8; \bytes
[AddrResult:= addr Result;
AddrResult(0):= MAlloc(Size*SizeOfReal);
AddrResult(1):= 0; \for safety
Posn:= 0.0;
for I:= 0 to Size-1 do
[Fall:= Array(I);
Result(I):= Posn + Fall;
case Rule of
1: [];
2: Posn:= -Fall;
3: Posn:= -(Posn+Fall);
4: Posn:= Posn+Fall
other [];
];
return Result;
];
func Experiment(Rule);
int Rule;
real DXs, DYs, RXs, RYs;
def Size = 100;
[
DXs:= [ -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 ];
DYs:= [ 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 ];
RXs:= Funnel(DXs, Size, Rule);
RYs:= Funnel(DYs, Size, Rule);
Print("Rule %d : X Y\n", Rule);
Print("Mean : %3.4f, %3.4f\n", Mean(RXs, Size), Mean(RYs, Size));
Print("Std Dev : %3.4f, %3.4f\n", StdDev(RXs, Size), StdDev(RYs, Size));
CrLf(0);
];
int R;
for R:= 1 to 4 do Experiment(R)</syntaxhighlight>
{{out}}
<pre>
Rule 1 : X Y
Mean : 0.0004, 0.0702
Std Dev : 0.7153, 0.6462
Rule 2 : X Y
Mean : 0.0009, -0.0103
Std Dev : 1.0371, 0.8999
Rule 3 : X Y
Mean : 0.0439, -0.0063
Std Dev : 7.9871, 4.7784
Rule 4 : X Y
Mean : 3.1341, 5.4210
Std Dev : 1.5874, 3.9304
</pre>
Line 1,778 ⟶ 2,977:
=={{header|zkl}}==
{{trans|Ruby}}
<
x:=0.0; rxs:=L();
foreach dx in (dxs){
Line 1,800 ⟶ 2,999:
"Std dev x, y : %7.4f, %7.4f".fmt(stddev(rxs),stddev(rys)).println();
println();
}</
<
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,
Line 1,832 ⟶ 3,031:
experiment("Rule 2:", dxs, dys, fcn(z,dz){ -dz });
experiment("Rule 3:", dxs, dys, fcn(z,dz){ -(z+dz) });
experiment("Rule 4:", dxs, dys, fcn(z,dz){ z+dz });</
{{out}}
<pre>
|