- 假如你有8筆實際的時間序列價格資料,依大小排序後為X(1)、X(2)、...、X(8),然後價格資料給予標準化
- 假如你有8筆數據,是透過標準常態分配亂數而得,由小而大依序為 Y(1)、Y(2)、...、Y(8)
- 對於步驟2,重複抓取數十萬次,然後取平均,分別得到常態分配狀態下,8筆資料的大小順序期望值,並製作為查表值的依據,所以Z(1)、Z(2)、...、Z(8)依序為 : -1.4317、-0.8600、-0.4831、-0.1624、0.1452、0.4667、0.8570、1.4328
- 有了順序期望值之後,直覺的,之後每次產生的8筆隨機亂數,也依序按大小排序,然後與對應的順序期望值做相減,並對該差值取平方,然後予以加總,也就是計算D(i)=X(i)-Z(i),對D(i)取平方,然後把8筆取平方後的D(i)值在加總
- 承續前文的常態亂數分配製作方法,對於不同的資料筆數所對應的常態分配大小順序分配期望值,範例以8筆資料為依據,單尾5%的臨界水準值(約為5.25),利下述程式碼改寫與再利用,製作成所需查表值
- 以MC程式碼及交易策略而言,可以構想如下
x=0;
y=Average(High,8);
z=StdDev(High,8);
For ii = 0 to 7 begin
x=x+Power(((NthHighest(ii+1,High,8)-y)/z) - Rank(ii+1) , 2);
End;
If x>5.25 then Buy next bar at Highest(High,8) stop;
- Excel VBA 亂數模擬參考程式碼(8筆資料、5.25的查表值)如下
Public RankSort(1 To 8) As Variant
Const FixConst As Variant = 0.398942284 '1/sqr(2*3.1415926)
Public Sub avgRank()
'模擬常態分配隨機變數的順序統計量期望值
Dim seed, seedCalc, nextSeed, NorValue As Variant
Dim ArrNorRandom(1 To 8) As Variant
Dim iith, testNum As Long
For testNum = 1 To 10000
iith = 1
Do
seed = Int(Rnd() * 1000000)
seedCalc = ((seed - 500000) * 0.000001) * 8 '模擬4個標準差範圍的 N(0,1) 常態分配變數
NorValue = Format(FixConst * Exp(-0.5 * seedCalc * seedCalc), "0.000000") '常態分配機率函數的數值
nextSeed = 0
nextSeed = Int(0.001 * (seed * seed))
nextSeed = Format(0.000001 * nextSeed - Int(0.000001 * nextSeed), "0.000000")
nextSeed = Int(1000000 * nextSeed)
nextSeed = Int(((23 + iith * 0.01) * nextSeed + 1011 * iith)) Mod 999999
nextSeed = Format(nextSeed * 0.00001 * FixConst, "0.000000")
'Debug.Print seedCalc, NorValue, nextSeed
If (nextSeed) < NorValue Then
ArrNorRandom(iith) = seedCalc
'Debug.Print iith, ArrNorRandom(iith)
'Sheets("工作表3").Cells(iith, 1) = ArrNorRandom(iith)
iith = iith + 1
End If
Loop While iith <= 8
'泡沫排序法
Dim swapTemp, ii, jj As Variant
For jj = 0 To 6
For ii = 1 To 7 - jj
If ArrNorRandom(ii) > ArrNorRandom(ii + 1) Then
swapTemp = ""
swapTemp = ArrNorRandom(ii + 1)
ArrNorRandom(ii + 1) = ArrNorRandom(ii)
ArrNorRandom(ii) = swapTemp
End If
Next ii
Next jj
'Debug.Print ArrNorRandom(1), ",", ArrNorRandom(2), ",", ArrNorRandom(3), ",", ArrNorRandom(4)
'Debug.Print ArrNorRandom(5), ",", ArrNorRandom(6), ",", ArrNorRandom(7), ",", ArrNorRandom(8)
For ii = 1 To 8
RankSort(ii) = RankSort(ii) + ArrNorRandom(ii)
Next ii
Next testNum
For ii = 1 To 8
RankSort(ii) = RankSort(ii) / testNum
Next ii
'Debug.Print RankSort(1), ",", RankSort(2), ",", RankSort(3), ",", RankSort(4)
'Debug.Print RankSort(5), ",", RankSort(6), ",", RankSort(7), ",", RankSort(8)
End Sub
Public Sub RankLevel()
Dim seed, seedCalc, nextSeed, NorValue As Variant
Dim ArrNorRandom(1 To 8), sqrDiff(1 To 100000) As Variant
Dim iith, testNum As Long
avgRank '事前取得順序統計量的期望值,並宣告為全域變數
For testNum = 1 To 100000 '模擬次數
iith = 1
Do
seed = Int(Rnd() * 1000000)
seedCalc = ((seed - 500000) * 0.000001) * 8
'模擬4個標準差範圍的 N(0,1) 常態分配變數
NorValue = Format(FixConst * Exp(-0.5 * seedCalc * seedCalc), "0.000000") '常態分配機率函數的數值
nextSeed = 0
nextSeed = Int(0.001 * (seed * seed))
nextSeed = Format(0.000001 * nextSeed - Int(0.000001 * nextSeed), "0.000000")
nextSeed = Int(1000000 * nextSeed)
nextSeed = Int(((23 + iith * 0.01) * nextSeed + 1011 * iith)) Mod 999999
nextSeed = Format(nextSeed * 0.00001 * FixConst, "0.000000")
If (nextSeed) < NorValue Then
ArrNorRandom(iith) = seedCalc
iith = iith + 1
End If
Loop While iith <= 8
'泡沫排序法
Dim swapTemp, ii, jj As Variant
For jj = 0 To 6
For ii = 1 To 7 - jj
If ArrNorRandom(ii) > ArrNorRandom(ii + 1) Then
swapTemp = ""
swapTemp = ArrNorRandom(ii + 1)
ArrNorRandom(ii + 1) = ArrNorRandom(ii)
ArrNorRandom(ii) = swapTemp
End If
Next ii
Next jj
'本次產生的隨機亂數排序後與對應的序列期望值做運算
For ii = 1 To 8
xTemp = ArrNorRandom(ii) - RankSort(ii)
sqrDiff(testNum) = sqrDiff(testNum) + xTemp * xTemp
Next ii
Next testNum '本次模擬次數,testNum=100000
'sqrDiff陣列共計10萬筆數據,泡沫排序,求解第95001序位
For jj = 0 To 4999 '資料數據共計10萬筆,求解第95001序位
For ii = 1 To 100000 - 1 - jj
If sqrDiff(ii) > sqrDiff(ii + 1) Then
swapTemp = ""
swapTemp = sqrDiff(ii + 1)
sqrDiff(ii + 1) = sqrDiff(ii)
sqrDiff(ii) = swapTemp
End If
Next ii
Next jj
Debug.Print sqrDiff(95001)
End Sub