Deming's funnel: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(2 intermediate revisions by one other user not shown)
Line 284:
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++}}==
Line 678 ⟶ 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}}==
Line 1,381 ⟶ 1,575:
std dev: {1.58739,3.93036}</pre>
 
=== Stretch 1 ===
<syntaxhighlight lang="mathematica">
RadiusDistribution = NormalDistribution[0, 1];
Line 1,418 ⟶ 1,612:
 
 
=== Stretch 2 ===
<syntaxhighlight lang="mathematica">
ListPlot[MarblePositions[#][Transpose[{dxs,dys}]]&/@Range[4],PlotLegends->PointLegend[{1,2,3,4}],AspectRatio->Automatic,ImageSize->600]
2,122

edits