2017 年 2 月

# 測試回合 - 使用 C# 的符號檢驗

[圖 1 號測試使用 C#

## 示範程式結構

[圖 2 號測試示範程式結構

``````using System;
using System.Numerics;
namespace SignTestUsingCSharp
{
class SignTestProgram
{
static void Main(string[] args)
{
Console.WriteLine("\nBegin Sign Test demo \n");
// All calling statements go here
Console.WriteLine("\n\nEnd Sign Test demo \n");
}
static int[] DoCounts(double[] before,
double[] after) { . . }
static void ShowVector(string pre, double[] v,
int dec, string post) { . . }
static double BinomProb(int k, int n,
double p) { . . }
static double BinomRightTail(int k, int n,
double p) { . . }
static BigInteger Choose(int n, int k) { . . }
}
}
``````

``````double[] before = new double[] { 70, 80, 75, 85, 70, 75, 50, 60 };
double[] after  = new double[] { 65, 78, 72, 87, 68, 74, 48, 63 };
Console.WriteLine("The weight data is: \n");
ShowVector("Before:  ", before, 0, "");
ShowVector("After :  ", after, 0, "\n");
``````

``````int[] counts = DoCounts(before, after);
Console.WriteLine("Num success = " + counts[2]);
Console.WriteLine("Num failure = " + counts[0]);
``````

``````Before:  70 80 75 85 70 75 50 60
After :  65 78 72 87 68 74 48 63
+  +  +  -  +  +  +  -
``````

``````int k = counts[2];
int n = counts[0] + counts[2];
Console.WriteLine("k = " + k + " n = " + n + " p = 0.5");
``````

Main 方法已結束︰

``````double p_value = BinomRightTail(k, n, 0.5);
Console.WriteLine("\nProbability of 'no effect' is " + p_value.ToString("F4"));
Console.WriteLine("Probability of 'an effect' is " + (1 - p_value).ToString("F4"));
``````

## 了解選擇函式

``````(A, B, C), (A, B, D), (A, B, E), (A, C, D), (A, C, E),
(A, D, E), (B, C, D), (B, C, E), (B, D, E), (C, D, E)
``````

[選擇函式會定義選擇 (n k) = n ！/ [k! * (n-k)!]其中"！"字元表示階乘。那麼

``````Choose(5, 3) = 5! / (3! * 2!) = (5 * 4 * 3 * 2 * 1) / (3 * 2 * 1) *
(2 * 1) = 120 / 12 = 10
``````

``````Choose(100, 25) = 242,519,269,720,337,121,015,504
``````

``````Choose(10, 7) = Choose(10, 3)
``````

``````Choose(10, 3) = (10 * 9 * 8) / (3 * 2 * 1)
``````

[圖 3 選擇函式

``````static BigInteger Choose(int n, int k)
{
if (n == k) return 1; // Required special case
int delta, iMax;
if (k < n - k) { // Ex: Choose(100,3)
delta = n - k;
iMax = k;
}
else { // Ex: Choose(100,97)
delta = k;
iMax = n - k;
}
BigInteger ans = delta + 1;
for (int i = 2; i <= iMax; ++i)
ans = (ans * (delta + i)) / i;
return ans;
}
``````

## 了解分配

``````Pr(X = 8) = 0.6 * 0.6 * 0.6 * 0.6 * 0.6 * 0.6 * 0.6 * 0.6 = (0.6)^8 * (0.4)^0 = 0.0168
``````

``````Pr(X = 7) = Choose(8, 1) * [ (0.6)^7 * (0.4)^1 ] = 8 * 0.0280 * 0.4 = 0.0896
``````

``````P(X = k) = Choose(n, k) * p^k * (1-p)^n-k
``````

``````P(X = k) = Choose(n, k) * (0.5)^n
``````

``````P(X = 6) = Choose(8, 6) * (0.5)^8 = 28 * 0.0039 = 0.1094
``````

``````static double BinomProb(int k, int n, double p)
{
// Probability of k "successes" in n trials
// if p is prob of success on a single trial
BigInteger c = Choose(n, k);
double left = Math.Pow(p, k);
double right = Math.Pow(1.0 - p, n - k);
return (double)c * left * right;
}
``````

[圖 4] 分配 n = 8 和 p = 0.5

## 實作號測試

``````static double BinomRightTail(int k, int n, double p)
{
// Probability of k or more successes in n trials
double sum = 0.0;
for (int i = k; i <= n; ++i)
sum += BinomProb(i, n, p);
return sum;
}
``````

``````static int[] DoCounts(double[] before, double[] after)
{
int[] result = new int[3];
for (int i = 0; i < before.Length; ++i) {
if (after[i] > before[i])
++result[0];  // Fail
else if (after[i] < before[i])
++result[2]; // Success
else
++result[0]; // Neither
}
return result;
}
``````

``````static void ShowVector(string pre, double[] v, int dec, string post)
{
Console.Write(pre);
for (int i = 0; i < v.Length; ++i)
Console.Write(v[i].ToString("F" + dec) + " ");
Console.WriteLine(post);
}
``````

## 總結

Dr。James McCaffrey適用於在美國華盛頓州 Redmond 的 Microsoft Research 他曾在數個 Microsoft 產品，包括 Internet Explorer 和 Bing。Dr。可以連線到 McCaffrey jammc@microsoft.com