• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Beakerboy / VBA-Projects / 45231230442

23 Nov 2026 05:46PM UTC coverage: 36.232% (-47.6%) from 83.824%
45231230442

push

manual

web-flow
Merge 8c2a8e298 into c8660568c

2 of 6 new or added lines in 1 file covered. (33.33%)

125 of 345 relevant lines covered (36.23%)

6.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.76
/src/Modules/EllipseMod.bas
1
Attribute VB_Name = "EllipseMod"
2 ✔
2
' Function: Ellipse
3
' Given a set of data, produce a confidence ellipse at a given standard deviation
4
'
5
' http://stackoverflow.com/questions/3417028/ellipse-around-the-data-in-matlab
6
Function Ellipse(Data, sigma, Optional NumPoints = 11)
2 ✔
7

×
8
    p = 2 * WorksheetFunction.Norm_S_Dist(sigma, True) - 1
2 ✔
9
    chi = WorksheetFunction.ChiSq_Inv(p, 2)
×
10
    Dim Averages As Variant
×
11
    ReDim Averages(1 To 2)
×
12
    Averages(1) = WorksheetFunction.Average(Application.Index(Data, , 1))
×
13
    Averages(2) = WorksheetFunction.Average(Application.Index(Data, , 2))
×
14
    'Calculate yhe covarience matrix of the data
15
    Covarience = CovarienceMatrix(Data)
×
16

×
17
    'Scale the data by the confidence interval
18
    Cov = bsxfun("multiply", Covarience, chi)
×
19

×
20
    'Calculate our Eigenvalues
21
    D = Eigenvalues(Cov)
×
22

×
23
    V = Eigenvectors(Cov)
×
24

×
25
    'extract diagonal
26
    DV = ExtractDiagonal(D)
×
27

×
28
    'Sort eigenvalues large to small
29
    'If they have to be reordered, reorder eigenvectors as well
30
    If (DV(2) > DV(1)) Then
×
31
        D1 = DV(1)
×
32
        DV(1) = DV(2)
×
33
        DV(2) = D1
×
34
        V11 = V(1, 1)
×
35
        V21 = V(2, 1)
×
36
        V(1, 1) = V(1, 2)
×
37
        V(2, 1) = V(2, 2)
×
38
        V(1, 2) = V11
×
39
        V(2, 2) = V21
×
40
    End If
×
41

×
42
    'Take the square root of Eigenvalues
43
    DV(1) = Math.Sqr(DV(1))
×
44
    DV(2) = Math.Sqr(DV(2))
×
45
    'Convert back to a diagonal matrix
46
    D = Diagonal(DV)
×
47

×
48
    TranslationMatrix = WorksheetFunction.MMult(V, D)
×
49

×
50
    Unit_Circle = WorksheetFunction.Transpose(UnitCircle(NumPoints))
×
51
    CenteredEllipse = WorksheetFunction.Transpose(WorksheetFunction.MMult(TranslationMatrix, Unit_Circle))
×
52
    Ellipse = bsxfun3("plus", CenteredEllipse, Averages)
×
53
End Function
2 ✔
54

55
' Function: Eigenvalues
56
' A diagonal Matrix of eigenvalues
57
Function Eigenvalues(Covarience)
4 ✔
58
    a = 1
4 ✔
59
    b = -1 * Covarience(1, 1) - Covarience(2, 2)
4 ✔
60
    c = -1 * Covarience(2, 1) * Covarience(1, 2) + Covarience(1, 1) * Covarience(2, 2)
4 ✔
61

×
62
    Eigenvalue1 = (-b - Math.Sqr(b * b - 4 * a * c)) / 2 / a
4 ✔
63
    Eigenvalue2 = (-1 * b + Math.Sqr(b * b - 4 * a * c)) / 2 / a
4 ✔
64

×
65
    Eigen = ScalerMatrix(0, 2, 2)
4 ✔
66
    Eigen(1, 1) = Eigenvalue1
8 ✔
67
    Eigen(2, 2) = Eigenvalue2
8 ✔
68
    Eigenvalues = Eigen
4 ✔
69
End Function
4 ✔
70

71
' Function: Eigenvectors
72
' Calculate unit eigenvectors from a covarience matrix
73
Function Eigenvectors(Covarience)
2 ✔
74
    Dim ReturnMatrix As Variant
2 ✔
75
    ReDim ReturnMatrix(1 To 2, 1 To 2)
8 ✔
76

×
77
    D = Eigenvalues(Covarience)
2 ✔
78
    Lambda = bsxfun("multiply", Identity(2), D(2, 2))
2 ✔
79
    AMinusLambda = bsxfun2("minus", Covarience, bsxfun("multiply", Identity(2), D(2, 2)))
2 ✔
80
    A21 = AMinusLambda(1, 2)
2 ✔
81
    A11 = AMinusLambda(1, 1)
2 ✔
82
    ReturnMatrix(2, 1) = Math.Sqr(A21 ^ 2 / (A11 ^ 2 + A21 ^ 2))
4 ✔
83
    ReturnMatrix(1, 2) = ReturnMatrix(2, 1)
4 ✔
84
    ReturnMatrix(1, 1) = ReturnMatrix(2, 1) * A11 / A21
4 ✔
85
    ReturnMatrix(2, 2) = -1 * ReturnMatrix(1, 1)
4 ✔
86
    Eigenvectors = ReturnMatrix
2 ✔
87
End Function
2 ✔
88

89
' Function: Identity
90
' Create an Identity Matrix of a given size
91
Function Identity(Size_num)
6 ✔
92
    ReturnMatrix = ScalerMatrix(0, Size_num, Size_num)
6 ✔
93
    For i = 1 To Size_num
26 ✔
94
        ReturnMatrix(i, i) = 1
28 ✔
95
    Next i
6 ✔
96
    Identity = ReturnMatrix
6 ✔
97
End Function
6 ✔
98

99
' Function: CovarienceMatrix
100
' Sample Covarience Matrix
101
Function CovarienceMatrix(Data)
2 ✔
102
    Y = Application.Index(Data, , 2)
2 ✔
NEW
103
    Count = WorksheetFunction.Count(Y)
×
104

×
105
    OnesM = ScalerMatrix(1, Count, Count)
×
106

×
107
    'Average the columns
NEW
108
    PartA = WorksheetFunction.MMult(OnesM, Data)
×
109
    PartAScale = bsxfun("divide", PartA, Count)
×
110

×
111
    'Center the data by subtracting the averages
112
    PartB = bsxfun2("minus", Data, PartAScale)
×
113

×
NEW
114
    PartBprime = WorksheetFunction.Transpose(PartB)
×
115

×
NEW
116
    PartC = WorksheetFunction.MMult(PartBprime, PartB)
×
117
    PartD = bsxfun("divide", PartC, Count - 1)
×
118
    CovarienceMatrix = PartD
×
119
End Function
2 ✔
120

121
'Make a matrix of dimensions m x n where every element is the value 'value'
122
Function ScalerMatrix(value, matrix_length, matrix_width)
14 ✔
123
    Dim matrix_object As Variant
14 ✔
124
    ReDim matrix_object(1 To matrix_length, 1 To matrix_width)
56 ✔
125
    For i = 1 To matrix_length
58 ✔
126
        For j = 1 To matrix_width
130 ✔
127
            matrix_object(i, j) = value
140 ✔
128
        Next j
30 ✔
129
    Next i
14 ✔
130
    ScalerMatrix = matrix_object
14 ✔
131
End Function
14 ✔
132

133
'apply an element-wise operation between a matrix and a scaler or two matricies
134
'currently works only on matricies of width=2 and unlimited length
135
Function bsxfun(operator_type, matrix_object, scaler_value)
12 ✔
136
    Dim ReturnMatrix As Variant
12 ✔
137
    ReDim ReturnMatrix(LBound(matrix_object, 1) To UBound(matrix_object, 1), 1 To 2)
48 ✔
138
  
139
    For i = LBound(matrix_object, 1) To UBound(matrix_object, 1)
48 ✔
140
        For j = 1 To 2
96 ✔
141
            If operator_type = "minus" Then
56 ✔
142
                ReturnMatrix(i, j) = matrix_object(i, j) - scaler_value
16 ✔
143
            ElseIf operator_type = "plus" Then
48 ✔
144
                ReturnMatrix(i, j) = matrix_object(i, j) + scaler_value
16 ✔
145
            ElseIf operator_type = "divide" Then
40 ✔
146
                ReturnMatrix(i, j) = matrix_object(i, j) / scaler_value
16 ✔
147
            ElseIf operator_type = "multiply" Then
48 ✔
148
                ReturnMatrix(i, j) = matrix_object(i, j) * scaler_value
48 ✔
149
            End If
48 ✔
150
        Next j
24 ✔
151
    Next i
12 ✔
152
    bsxfun = ReturnMatrix
12 ✔
153
End Function
12 ✔
154

155
'apply an element-wise operation between a matrix and a scaler or two matricies
156
'currently works only on matricies of width=2 and unlimited length
157
'We use the bounds on matrix_2 because matrix_object is sometimes a range,
158
'and lBound/UBound don't work on ranges
159
Function bsxfun2(operator_type, matrix_object, matrix_2)
10 ✔
160
    Dim ReturnMatrix As Variant
10 ✔
161
    ReDim ReturnMatrix(LBound(matrix_2, 1) To UBound(matrix_2, 1), 1 To 2)
40 ✔
162
  
163
    For i = LBound(matrix_2, 1) To UBound(matrix_2, 1)
40 ✔
164
        For j = 1 To 2
80 ✔
165
        'MsgBox matrix_object(i, j)
166
            operator_value = matrix_2(i, j)
40 ✔
167
            If operator_type = "minus" Then
56 ✔
168
                ReturnMatrix(i, j) = matrix_object(i, j) - operator_value
32 ✔
169
            ElseIf operator_type = "plus" Then
32 ✔
170
                ReturnMatrix(i, j) = matrix_object(i, j) + operator_value
16 ✔
171
            ElseIf operator_type = "divide" Then
24 ✔
172
                ReturnMatrix(i, j) = matrix_object(i, j) / operator_value
16 ✔
173
            ElseIf operator_type = "multiply" Then
16 ✔
174
                ReturnMatrix(i, j) = matrix_object(i, j) * operator_value
16 ✔
175
            End If
40 ✔
176
        Next j
20 ✔
177
    Next i
10 ✔
178
    bsxfun2 = ReturnMatrix
10 ✔
179
End Function
10 ✔
180

181
'apply an element-wise operation between a matrix and a vector
182
'currently works only on matricies of width=2 and unlimited length
183
Function bsxfun3(operator_type, matrix_object, scaler_value)
8 ✔
184
    Dim ReturnMatrix As Variant
8 ✔
185
    ReDim ReturnMatrix(LBound(matrix_object, 1) To UBound(matrix_object, 1), 1 To 2)
32 ✔
186
  
187
    For i = LBound(matrix_object, 1) To UBound(matrix_object, 1)
32 ✔
188
        For j = 1 To 2
64 ✔
189
            If operator_type = "minus" Then
40 ✔
190
                ReturnMatrix(i, j) = matrix_object(i, j) - scaler_value(j)
16 ✔
191
            ElseIf operator_type = "plus" Then
32 ✔
192
                ReturnMatrix(i, j) = matrix_object(i, j) + scaler_value(j)
16 ✔
193
            ElseIf operator_type = "divide" Then
24 ✔
194
                ReturnMatrix(i, j) = matrix_object(i, j) / scaler_value(j)
16 ✔
195
            ElseIf operator_type = "multiply" Then
16 ✔
196
                ReturnMatrix(i, j) = matrix_object(i, j) * scaler_value(j)
16 ✔
197
            End If
32 ✔
198
        Next j
16 ✔
199
    Next i
8 ✔
200
    bsxfun3 = ReturnMatrix
8 ✔
201
End Function
8 ✔
202

203
'Extract the diagonals from a matrix
204
Function ExtractDiagonal(matrix_object)
2 ✔
205
    Dim ReturnVector As Variant
2 ✔
206
    ReDim ReturnVector(LBound(matrix_object, 1) To UBound(matrix_object, 1))
4 ✔
207
    For i = LBound(matrix_object, 1) To UBound(matrix_object, 1)
8 ✔
208
        ReturnVector(i) = matrix_object(i, i)
8 ✔
209
    Next i
2 ✔
210
    ExtractDiagonal = ReturnVector
2 ✔
211
End Function
2 ✔
212

213
'Create a diagonal matrix from a vector
214
Function Diagonal(VectorObject)
2 ✔
215
    ReturnMatrix = ScalerMatrix(0, UBound(VectorObject), UBound(VectorObject))
2 ✔
216
    For i = LBound(VectorObject) To UBound(VectorObject)
8 ✔
217
        ReturnMatrix(i, i) = VectorObject(i)
8 ✔
218
    Next i
2 ✔
219
    Diagonal = ReturnMatrix
2 ✔
220
End Function
2 ✔
221

222
' Function: UnitCircle
223
'
224
' Generates Cartesian coordinates (x, y) for a unit circle divided into the
225
' specified number of points. The starting/ending point is duplicated so the
226
' circle closes cleanly when plotted.
227
'
228
' Parameters:
229
'   NumPoints - The total number of coordinate pairs to generate (must be > 1).
230
'
231
' Returns:
232
'   A 1-based, two-dimensional Double array of size (1 To NumPoints, 1 To 2),
233
'   where column 1 contains x-coordinates and column 2 contains y-coordinates.
234
Function UnitCircle(NumPoints)
2 ✔
235
    Dim ReturnVector() As Double
2 ✔
236
    ReDim ReturnVector(1 To NumPoints, 1 To 2)
8 ✔
237
    Pi = WorksheetFunction.Pi()
2 ✔
238
    For i = 1 To NumPoints
×
239
        ReturnVector(i, 1) = Math.Cos(2 * Pi * i / (NumPoints - 1))
×
240
        ReturnVector(i, 2) = Math.Sin(2 * Pi * i / (NumPoints - 1))
×
241
    Next i
×
242
    UnitCircle = ReturnVector
×
243
End Function
2 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc