-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVBAF.ML.Regression.ps1
More file actions
762 lines (674 loc) · 30 KB
/
VBAF.ML.Regression.ps1
File metadata and controls
762 lines (674 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#Requires -Version 5.1
<#
.SYNOPSIS
Classic Regression Algorithms for Machine Learning
.DESCRIPTION
Implements classic regression algorithms from scratch.
Designed as a TEACHING resource - every step explained.
Algorithms included:
- Linear Regression (OLS - Ordinary Least Squares)
- Ridge Regression (L2 regularization)
- Lasso Regression (L1 regularization)
- Logistic Regression (binary & multiclass)
Utilities included:
- Feature scaling (StandardScaler, MinMaxScaler)
- Cross-validation (k-fold)
- Metrics (MSE, RMSE, MAE, R2, Accuracy)
Standalone - no external VBAF dependencies required.
.NOTES
Part of VBAF - Phase 4 Machine Learning Module
PS 5.1 compatible
Teaching project - math shown explicitly, no black boxes!
#>
$basePath = $PSScriptRoot
# ============================================================
# TEACHING NOTE: What is Regression?
# Regression finds the relationship between:
# - Input features X (what we know)
# - Output target y (what we want to predict)
# We find weights W such that: y_hat = X * W
# Then minimize the error between y_hat and y.
# ============================================================
# ============================================================
# FEATURE SCALING UTILITIES
# ============================================================
# TEACHING NOTE: Why scale features?
# If one feature is 0-1 and another is 0-1000000,
# the large feature dominates learning.
# Scaling puts all features on equal footing.
# ============================================================
class StandardScaler {
# Transforms features to mean=0, std=1
# Formula: z = (x - mean) / std
[double[]] $Mean
[double[]] $Std
[bool] $IsFitted = $false
[void] Fit([double[][]]$X) {
$nFeatures = $X[0].Length
$this.Mean = @(0.0) * $nFeatures
$this.Std = @(1.0) * $nFeatures
$nSamples = $X.Length
# Calculate mean for each feature
for ($f = 0; $f -lt $nFeatures; $f++) {
$sum = 0.0
foreach ($row in $X) { $sum += $row[$f] }
$this.Mean[$f] = $sum / $nSamples
}
# Calculate std for each feature
for ($f = 0; $f -lt $nFeatures; $f++) {
$sumSq = 0.0
foreach ($row in $X) {
$diff = $row[$f] - $this.Mean[$f]
$sumSq += $diff * $diff
}
$variance = $sumSq / $nSamples
$this.Std[$f] = [Math]::Max([Math]::Sqrt($variance), 1e-8)
}
$this.IsFitted = $true
}
[double[][]] Transform([double[][]]$X) {
$result = @()
foreach ($row in $X) {
$scaled = @(0.0) * $row.Length
for ($f = 0; $f -lt $row.Length; $f++) {
$scaled[$f] = ($row[$f] - $this.Mean[$f]) / $this.Std[$f]
}
$result += ,$scaled
}
return $result
}
[double[][]] FitTransform([double[][]]$X) {
$this.Fit($X)
return $this.Transform($X)
}
[double[]] InverseTransform([double[]]$y) {
$result = @(0.0) * $y.Length
for ($i = 0; $i -lt $y.Length; $i++) {
$result[$i] = $y[$i] * $this.Std[0] + $this.Mean[0]
}
return $result
}
}
class MinMaxScaler {
# Transforms features to range [0, 1]
# Formula: z = (x - min) / (max - min)
[double[]] $Min
[double[]] $Max
[bool] $IsFitted = $false
[void] Fit([double[][]]$X) {
$nFeatures = $X[0].Length
$this.Min = @(0.0) * $nFeatures
$this.Max = @(1.0) * $nFeatures
for ($f = 0; $f -lt $nFeatures; $f++) {
$minVal = [double]::MaxValue
$maxVal = [double]::MinValue
foreach ($row in $X) {
if ($row[$f] -lt $minVal) { $minVal = $row[$f] }
if ($row[$f] -gt $maxVal) { $maxVal = $row[$f] }
}
$this.Min[$f] = $minVal
$this.Max[$f] = $maxVal
}
$this.IsFitted = $true
}
[double[][]] Transform([double[][]]$X) {
$result = @()
foreach ($row in $X) {
$scaled = @(0.0) * $row.Length
for ($f = 0; $f -lt $row.Length; $f++) {
$range = [Math]::Max($this.Max[$f] - $this.Min[$f], 1e-8)
$scaled[$f] = ($row[$f] - $this.Min[$f]) / $range
}
$result += ,$scaled
}
return $result
}
[double[][]] FitTransform([double[][]]$X) {
$this.Fit($X)
return $this.Transform($X)
}
}
# ============================================================
# METRICS
# ============================================================
# TEACHING NOTE: How do we measure how good our model is?
# MSE = Mean Squared Error - penalizes large errors heavily
# RMSE = Root Mean Squared Error - same units as target
# MAE = Mean Absolute Error - robust to outliers
# R2 = R-squared - 1.0 = perfect, 0.0 = baseline
# ============================================================
function Get-RegressionMetrics {
param([double[]]$yTrue, [double[]]$yPred)
$n = $yTrue.Length
$mse = 0.0
$mae = 0.0
$yMean = ($yTrue | Measure-Object -Average).Average
$ssTot = 0.0
$ssRes = 0.0
for ($i = 0; $i -lt $n; $i++) {
$err = $yTrue[$i] - $yPred[$i]
$mse += $err * $err
$mae += [Math]::Abs($err)
$ssRes += $err * $err
$ssTot += ($yTrue[$i] - $yMean) * ($yTrue[$i] - $yMean)
}
$mse = $mse / $n
$rmse = [Math]::Sqrt($mse)
$mae = $mae / $n
$r2 = if ($ssTot -gt 1e-10) { 1.0 - ($ssRes / $ssTot) } else { 0.0 }
return @{
MSE = [Math]::Round($mse, 6)
RMSE = [Math]::Round($rmse, 6)
MAE = [Math]::Round($mae, 6)
R2 = [Math]::Round($r2, 6)
}
}
function Get-ClassificationMetrics {
param([int[]]$yTrue, [int[]]$yPred)
$n = $yTrue.Length
$correct = 0
for ($i = 0; $i -lt $n; $i++) {
if ($yTrue[$i] -eq $yPred[$i]) { $correct++ }
}
$accuracy = $correct / $n
return @{
Accuracy = [Math]::Round($accuracy, 6)
Correct = $correct
Total = $n
}
}
# ============================================================
# LINEAR REGRESSION (OLS)
# ============================================================
# TEACHING NOTE: Linear Regression finds weights W such that:
# y_hat = X*W + bias
# We minimize: Loss = (1/n) * sum((y - y_hat)^2) <- MSE
# Using gradient descent:
# W = W - learningRate * dLoss/dW
# dLoss/dW = (2/n) * X^T * (y_hat - y)
# ============================================================
class LinearRegression {
[double[]] $Weights
[double] $Bias
[double] $LearningRate
[int] $MaxIter
[bool] $IsFitted = $false
[System.Collections.Generic.List[double]] $LossHistory
LinearRegression() {
$this.LearningRate = 0.0001
$this.MaxIter = 1000
$this.LossHistory = [System.Collections.Generic.List[double]]::new()
}
LinearRegression([double]$learningRate, [int]$maxIter) {
$this.LearningRate = $learningRate
$this.MaxIter = $maxIter
$this.LossHistory = [System.Collections.Generic.List[double]]::new()
}
# Predict: y_hat = X*W + bias
[double[]] Predict([double[][]]$X) {
$yHat = @(0.0) * $X.Length
for ($i = 0; $i -lt $X.Length; $i++) {
$dot = $this.Bias
for ($j = 0; $j -lt $X[$i].Length; $j++) {
$dot += $X[$i][$j] * $this.Weights[$j]
}
$yHat[$i] = $dot
}
return $yHat
}
# Train using gradient descent
[void] Fit([double[][]]$X, [double[]]$y) {
$n = $X.Length
$nFeatures = $X[0].Length
$this.Weights = @(0.0) * $nFeatures
$this.Bias = 0.0
for ($iter = 0; $iter -lt $this.MaxIter; $iter++) {
$yHat = $this.Predict($X)
# Compute gradients
$dW = @(0.0) * $nFeatures
$dB = 0.0
$loss = 0.0
for ($i = 0; $i -lt $n; $i++) {
$err = $yHat[$i] - $y[$i]
$loss += $err * $err
$dB += $err
for ($j = 0; $j -lt $nFeatures; $j++) {
$dW[$j] += $err * $X[$i][$j]
}
}
$loss = $loss / $n
$this.LossHistory.Add($loss)
# Update weights
$this.Bias -= $this.LearningRate * (2.0 / $n) * $dB
for ($j = 0; $j -lt $nFeatures; $j++) {
$this.Weights[$j] -= $this.LearningRate * (2.0 / $n) * $dW[$j]
}
}
$this.IsFitted = $true
}
[void] PrintSummary() {
Write-Host ""
Write-Host "╔══════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Linear Regression Summary ║" -ForegroundColor Cyan
Write-Host "╠══════════════════════════════════════╣" -ForegroundColor Cyan
Write-Host ("║ Bias : {0,-24}║" -f [Math]::Round($this.Bias, 6)) -ForegroundColor White
for ($j = 0; $j -lt $this.Weights.Length; $j++) {
Write-Host ("║ Weight[{0}] : {1,-24}║" -f $j, [Math]::Round($this.Weights[$j], 6)) -ForegroundColor White
}
$finalLoss = if ($this.LossHistory.Count -gt 0) { $this.LossHistory[-1] } else { 0.0 }
Write-Host ("║ Final Loss: {0,-24}║" -f [Math]::Round($finalLoss, 6)) -ForegroundColor Magenta
Write-Host "╚══════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
}
# ============================================================
# RIDGE REGRESSION (L2 Regularization)
# ============================================================
# TEACHING NOTE: Ridge adds a penalty for large weights:
# Loss = MSE + lambda * sum(W^2) <- L2 penalty
# This SHRINKS weights toward zero but never to exactly zero.
# Use Ridge when: many features, all somewhat useful.
# Lambda controls strength: larger = more shrinkage.
# ============================================================
class RidgeRegression : LinearRegression {
[double] $Lambda # Regularization strength
RidgeRegression([double]$lambda) : base(0.01, 1000) {
$this.Lambda = $lambda
}
RidgeRegression([double]$lambda, [double]$lr, [int]$maxIter) : base($lr, $maxIter) {
$this.Lambda = $lambda
}
[void] Fit([double[][]]$X, [double[]]$y) {
$n = $X.Length
$nFeatures = $X[0].Length
$this.Weights = @(0.0) * $nFeatures
$this.Bias = 0.0
for ($iter = 0; $iter -lt $this.MaxIter; $iter++) {
$yHat = $this.Predict($X)
$dW = @(0.0) * $nFeatures
$dB = 0.0
$loss = 0.0
for ($i = 0; $i -lt $n; $i++) {
$err = $yHat[$i] - $y[$i]
$loss += $err * $err
$dB += $err
for ($j = 0; $j -lt $nFeatures; $j++) {
$dW[$j] += $err * $X[$i][$j]
}
}
# L2 regularization term added to loss and gradient
$l2Loss = 0.0
for ($j = 0; $j -lt $nFeatures; $j++) {
$l2Loss += $this.Weights[$j] * $this.Weights[$j]
}
$loss = ($loss / $n) + $this.Lambda * $l2Loss
$this.LossHistory.Add($loss)
$this.Bias -= $this.LearningRate * (2.0 / $n) * $dB
for ($j = 0; $j -lt $nFeatures; $j++) {
# Ridge gradient: add 2*lambda*W to weight gradient
$ridgeGrad = (2.0 / $n) * $dW[$j] + 2.0 * $this.Lambda * $this.Weights[$j]
$this.Weights[$j] -= $this.LearningRate * $ridgeGrad
}
}
$this.IsFitted = $true
}
[void] PrintSummary() {
Write-Host ""
Write-Host "╔══════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Ridge Regression Summary ║" -ForegroundColor Cyan
Write-Host "╠══════════════════════════════════════╣" -ForegroundColor Cyan
Write-Host ("║ Lambda : {0,-24}║" -f $this.Lambda) -ForegroundColor Yellow
Write-Host ("║ Bias : {0,-24}║" -f [Math]::Round($this.Bias, 6)) -ForegroundColor White
for ($j = 0; $j -lt $this.Weights.Length; $j++) {
Write-Host ("║ Weight[{0}] : {1,-24}║" -f $j, [Math]::Round($this.Weights[$j], 6)) -ForegroundColor White
}
$finalLoss = if ($this.LossHistory.Count -gt 0) { $this.LossHistory[-1] } else { 0.0 }
Write-Host ("║ Final Loss: {0,-24}║" -f [Math]::Round($finalLoss, 6)) -ForegroundColor Magenta
Write-Host "╚══════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
}
# ============================================================
# LASSO REGRESSION (L1 Regularization)
# ============================================================
# TEACHING NOTE: Lasso adds a different penalty:
# Loss = MSE + lambda * sum(|W|) <- L1 penalty
# This can shrink weights to EXACTLY zero = feature selection!
# Use Lasso when: many features, only some are useful.
# Lasso automatically picks the most important features.
# ============================================================
class LassoRegression : LinearRegression {
[double] $Lambda
LassoRegression([double]$lambda) : base(0.01, 1000) {
$this.Lambda = $lambda
}
LassoRegression([double]$lambda, [double]$lr, [int]$maxIter) : base($lr, $maxIter) {
$this.Lambda = $lambda
}
[void] Fit([double[][]]$X, [double[]]$y) {
$n = $X.Length
$nFeatures = $X[0].Length
$this.Weights = @(0.0) * $nFeatures
$this.Bias = 0.0
for ($iter = 0; $iter -lt $this.MaxIter; $iter++) {
$yHat = $this.Predict($X)
$dW = @(0.0) * $nFeatures
$dB = 0.0
$loss = 0.0
for ($i = 0; $i -lt $n; $i++) {
$err = $yHat[$i] - $y[$i]
$loss += $err * $err
$dB += $err
for ($j = 0; $j -lt $nFeatures; $j++) {
$dW[$j] += $err * $X[$i][$j]
}
}
# L1 regularization
$l1Loss = 0.0
for ($j = 0; $j -lt $nFeatures; $j++) {
$l1Loss += [Math]::Abs($this.Weights[$j])
}
$loss = ($loss / $n) + $this.Lambda * $l1Loss
$this.LossHistory.Add($loss)
$this.Bias -= $this.LearningRate * (2.0 / $n) * $dB
for ($j = 0; $j -lt $nFeatures; $j++) {
# Lasso gradient: sign(W)*lambda (subgradient)
$sign = if ($this.Weights[$j] -gt 0) { 1.0 } elseif ($this.Weights[$j] -lt 0) { -1.0 } else { 0.0 }
$lassoGrad = (2.0 / $n) * $dW[$j] + $this.Lambda * $sign
$this.Weights[$j] -= $this.LearningRate * $lassoGrad
}
}
$this.IsFitted = $true
}
[void] PrintSummary() {
Write-Host ""
Write-Host "╔══════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Lasso Regression Summary ║" -ForegroundColor Cyan
Write-Host "╠══════════════════════════════════════╣" -ForegroundColor Cyan
Write-Host ("║ Lambda : {0,-24}║" -f $this.Lambda) -ForegroundColor Yellow
Write-Host ("║ Bias : {0,-24}║" -f [Math]::Round($this.Bias, 6)) -ForegroundColor White
for ($j = 0; $j -lt $this.Weights.Length; $j++) {
$w = [Math]::Round($this.Weights[$j], 6)
$color = if ([Math]::Abs($w) -lt 0.001) { "DarkGray" } else { "White" }
Write-Host ("║ Weight[{0}] : {1,-24}║" -f $j, $w) -ForegroundColor $color
}
$finalLoss = if ($this.LossHistory.Count -gt 0) { $this.LossHistory[-1] } else { 0.0 }
Write-Host ("║ Final Loss: {0,-24}║" -f [Math]::Round($finalLoss, 6)) -ForegroundColor Magenta
Write-Host " (DarkGray weights ≈ 0 = Lasso eliminated them!)" -ForegroundColor DarkGray
Write-Host "╚══════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
}
# ============================================================
# LOGISTIC REGRESSION
# ============================================================
# TEACHING NOTE: Logistic Regression is for CLASSIFICATION.
# Instead of predicting a number, it predicts a probability.
# Uses the sigmoid function: sigma(z) = 1 / (1 + e^-z)
# Output is always between 0 and 1.
# If probability > 0.5 -> class 1, else class 0
# Loss function: Binary Cross-Entropy
# Loss = -(y*log(p) + (1-y)*log(1-p))
# ============================================================
class LogisticRegression {
[double[]] $Weights
[double] $Bias
[double] $LearningRate
[int] $MaxIter
[double] $Lambda # Regularization (0 = none)
[bool] $IsFitted = $false
[System.Collections.Generic.List[double]] $LossHistory
LogisticRegression() {
$this.LearningRate = 0.1
$this.MaxIter = 1000
$this.Lambda = 0.0
$this.LossHistory = [System.Collections.Generic.List[double]]::new()
}
LogisticRegression([double]$learningRate, [int]$maxIter, [double]$lambda) {
$this.LearningRate = $learningRate
$this.MaxIter = $maxIter
$this.Lambda = $lambda
$this.LossHistory = [System.Collections.Generic.List[double]]::new()
}
# Sigmoid: maps any number to [0,1]
hidden [double] Sigmoid([double]$z) {
return 1.0 / (1.0 + [Math]::Exp(-[Math]::Max(-500, [Math]::Min(500, $z))))
}
# Predict probabilities
[double[]] PredictProba([double[][]]$X) {
$proba = @(0.0) * $X.Length
for ($i = 0; $i -lt $X.Length; $i++) {
$z = $this.Bias
for ($j = 0; $j -lt $X[$i].Length; $j++) {
$z += $X[$i][$j] * $this.Weights[$j]
}
$proba[$i] = $this.Sigmoid($z)
}
return $proba
}
# Predict class labels (0 or 1)
[int[]] Predict([double[][]]$X) {
$proba = $this.PredictProba($X)
$labels = @(0) * $X.Length
for ($i = 0; $i -lt $proba.Length; $i++) {
$labels[$i] = if ($proba[$i] -ge 0.5) { 1 } else { 0 }
}
return $labels
}
[void] Fit([double[][]]$X, [int[]]$y) {
$n = $X.Length
$nFeatures = $X[0].Length
$this.Weights = @(0.0) * $nFeatures
$this.Bias = 0.0
for ($iter = 0; $iter -lt $this.MaxIter; $iter++) {
$proba = $this.PredictProba($X)
$dW = @(0.0) * $nFeatures
$dB = 0.0
$loss = 0.0
for ($i = 0; $i -lt $n; $i++) {
$err = $proba[$i] - $y[$i]
$dB += $err
# Binary cross-entropy loss
$p = [Math]::Max(1e-10, [Math]::Min(1 - 1e-10, $proba[$i]))
$loss -= $y[$i] * [Math]::Log($p) + (1 - $y[$i]) * [Math]::Log(1 - $p)
for ($j = 0; $j -lt $nFeatures; $j++) {
$dW[$j] += $err * $X[$i][$j]
}
}
$loss = $loss / $n
$this.LossHistory.Add($loss)
$this.Bias -= $this.LearningRate * $dB / $n
for ($j = 0; $j -lt $nFeatures; $j++) {
$regTerm = $this.Lambda * $this.Weights[$j]
$this.Weights[$j] -= $this.LearningRate * ($dW[$j] / $n + $regTerm)
}
}
$this.IsFitted = $true
}
[void] PrintSummary() {
Write-Host ""
Write-Host "╔══════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Logistic Regression Summary ║" -ForegroundColor Cyan
Write-Host "╠══════════════════════════════════════╣" -ForegroundColor Cyan
Write-Host ("║ Lambda : {0,-24}║" -f $this.Lambda) -ForegroundColor Yellow
Write-Host ("║ Bias : {0,-24}║" -f [Math]::Round($this.Bias, 6)) -ForegroundColor White
for ($j = 0; $j -lt $this.Weights.Length; $j++) {
Write-Host ("║ Weight[{0}] : {1,-24}║" -f $j, [Math]::Round($this.Weights[$j], 6)) -ForegroundColor White
}
$finalLoss = if ($this.LossHistory.Count -gt 0) { $this.LossHistory[-1] } else { 0.0 }
Write-Host ("║ Final Loss: {0,-24}║" -f [Math]::Round($finalLoss, 6)) -ForegroundColor Magenta
Write-Host "╚══════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
}
# ============================================================
# CROSS VALIDATION
# ============================================================
# TEACHING NOTE: Why cross-validate?
# If we test on the same data we trained on, we cheat!
# k-fold splits data into k parts:
# - Train on k-1 parts, test on 1 part
# - Repeat k times, each part gets a turn as test set
# - Average the scores = honest estimate of performance
# ============================================================
function Invoke-KFoldCV {
param(
[object] $Model,
[double[][]] $X,
[object[]] $y,
[int] $K = 5,
[string] $Task = "regression", # or "classification"
[switch] $Verbose
)
$n = $X.Length
$foldSize = [Math]::Floor($n / $K)
$scores = [System.Collections.Generic.List[double]]::new()
Write-Host ""
Write-Host "🔄 $K-Fold Cross Validation" -ForegroundColor Yellow
Write-Host " Samples : $n" -ForegroundColor Cyan
Write-Host " Fold size: $foldSize" -ForegroundColor Cyan
Write-Host ""
for ($fold = 0; $fold -lt $K; $fold++) {
$testStart = $fold * $foldSize
$testEnd = [Math]::Min($testStart + $foldSize, $n) - 1
# Split into train/test
$XTrain = @(); $yTrain = @()
$XTest = @(); $yTest = @()
for ($i = 0; $i -lt $n; $i++) {
if ($i -ge $testStart -and $i -le $testEnd) {
$XTest += ,$X[$i]
$yTest += $y[$i]
} else {
$XTrain += ,$X[$i]
$yTrain += $y[$i]
}
}
# Create fresh model instance same type
$foldModel = $Model.GetType()::new()
# Train
if ($Task -eq "classification") {
$foldModel.Fit($XTrain, [int[]]$yTrain)
$yPred = $foldModel.Predict($XTest)
$metrics = Get-ClassificationMetrics -yTrue ([int[]]$yTest) -yPred ([int[]]$yPred)
$score = $metrics.Accuracy
$label = "Accuracy"
} else {
$foldModel.Fit($XTrain, [double[]]$yTrain)
$yPred = $foldModel.Predict($XTest)
$metrics = Get-RegressionMetrics -yTrue ([double[]]$yTest) -yPred ([double[]]$yPred)
$score = $metrics.R2
$label = "R2"
}
$scores.Add($score)
if ($Verbose) {
Write-Host (" Fold {0}: {1} = {2:F4}" -f ($fold+1), $label, $score) -ForegroundColor White
}
}
$avgScore = ($scores | Measure-Object -Average).Average
$minScore = ($scores | Measure-Object -Minimum).Minimum
$maxScore = ($scores | Measure-Object -Maximum).Maximum
Write-Host ""
Write-Host "╔══════════════════════════════════════╗" -ForegroundColor Yellow
Write-Host "║ Cross Validation Results ║" -ForegroundColor Yellow
Write-Host "╠══════════════════════════════════════╣" -ForegroundColor Yellow
Write-Host ("║ Folds : {0,-24}║" -f $K) -ForegroundColor White
Write-Host ("║ Avg Score : {0,-24}║" -f [Math]::Round($avgScore, 4)) -ForegroundColor Green
Write-Host ("║ Min Score : {0,-24}║" -f [Math]::Round($minScore, 4)) -ForegroundColor White
Write-Host ("║ Max Score : {0,-24}║" -f [Math]::Round($maxScore, 4)) -ForegroundColor White
Write-Host "╚══════════════════════════════════════╝" -ForegroundColor Yellow
Write-Host ""
return @{ AvgScore = $avgScore; MinScore = $minScore; MaxScore = $maxScore; Scores = $scores }
}
# ============================================================
# BUILT-IN DEMO DATASETS
# ============================================================
function Get-VBAFDataset {
param([string]$Name = "HousePrice")
switch ($Name) {
"HousePrice" {
# Simple house price dataset
# Features: [size_sqm, bedrooms, age_years]
# Target: price in 1000s
Write-Host "📊 Dataset: HousePrice (20 samples)" -ForegroundColor Cyan
Write-Host " Features: [size_sqm, bedrooms, age_years]" -ForegroundColor Cyan
Write-Host " Target : price (1000s)" -ForegroundColor Cyan
$X = @(
@(50.0, 1.0, 20.0), @(75.0, 2.0, 15.0), @(100.0, 3.0, 10.0),
@(120.0, 3.0, 5.0), @(150.0, 4.0, 2.0), @(60.0, 2.0, 25.0),
@(80.0, 2.0, 18.0), @(90.0, 3.0, 12.0), @(110.0, 3.0, 8.0),
@(130.0, 4.0, 3.0), @(55.0, 1.0, 22.0), @(70.0, 2.0, 16.0),
@(95.0, 3.0, 11.0), @(115.0, 3.0, 6.0), @(140.0, 4.0, 1.0),
@(65.0, 2.0, 19.0), @(85.0, 2.0, 14.0), @(105.0, 3.0, 9.0),
@(125.0, 4.0, 4.0), @(160.0, 5.0, 1.0)
)
$y = @(
150.0, 220.0, 310.0, 370.0, 450.0, 175.0, 240.0, 270.0, 340.0, 400.0,
160.0, 210.0, 290.0, 355.0, 430.0, 195.0, 255.0, 320.0, 385.0, 500.0
)
return @{ X = $X; y = $y; Task = "regression" }
}
"Iris2Class" {
# Simplified 2-class Iris (Setosa vs Versicolor)
# Features: [sepal_length, petal_length]
# Target: 0=Setosa, 1=Versicolor
Write-Host "📊 Dataset: Iris2Class (20 samples)" -ForegroundColor Cyan
Write-Host " Features: [sepal_length, petal_length]" -ForegroundColor Cyan
Write-Host " Target : 0=Setosa, 1=Versicolor" -ForegroundColor Cyan
$X = @(
@(5.1, 1.4), @(4.9, 1.4), @(4.7, 1.3), @(4.6, 1.5), @(5.0, 1.4),
@(5.4, 1.7), @(4.6, 1.4), @(5.0, 1.5), @(4.4, 1.4), @(4.9, 1.5),
@(7.0, 4.7), @(6.4, 4.5), @(6.9, 4.9), @(5.5, 4.0), @(6.5, 4.6),
@(5.7, 4.5), @(6.3, 4.7), @(4.9, 3.3), @(6.6, 4.6), @(5.2, 3.9)
)
$y = @(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1)
return @{ X = $X; y = $y; Task = "classification" }
}
default {
Write-Host "❌ Unknown dataset: $Name" -ForegroundColor Red
Write-Host " Available: HousePrice, Iris2Class" -ForegroundColor Yellow
return $null
}
}
}
# ============================================================
# TEST
# 1. Run VBAF.LoadAll.ps1
# 2. $data = Get-VBAFDataset -Name "HousePrice"
# 3. $scaler = [StandardScaler]::new()
# $Xs = $scaler.FitTransform($data.X)
# 4. $model = [LinearRegression]::new()
# $model.Fit($Xs, $data.y)
# $model.PrintSummary()
# 5. $yPred = $model.Predict($Xs)
# Get-RegressionMetrics -yTrue $data.y -yPred $yPred
# 6. Compare Ridge vs Lasso:
# $ridge = [RidgeRegression]::new(0.1)
# $ridge.Fit($Xs, $data.y)
# $ridge.PrintSummary()
# $lasso = [LassoRegression]::new(0.1)
# $lasso.Fit($Xs, $data.y)
# $lasso.PrintSummary()
# 7. Classification:
# $data2 = Get-VBAFDataset -Name "Iris2Class"
# $logit = [LogisticRegression]::new()
# $logit.Fit($data2.X, [int[]]$data2.y)
# $logit.PrintSummary()
# Get-ClassificationMetrics -yTrue $data2.y -yPred $logit.Predict($data2.X)
# ============================================================
Write-Host "📦 VBAF.ML.Regression.ps1 loaded" -ForegroundColor Green
Write-Host " Classes : StandardScaler, MinMaxScaler" -ForegroundColor Cyan
Write-Host " LinearRegression, RidgeRegression" -ForegroundColor Cyan
Write-Host " LassoRegression, LogisticRegression" -ForegroundColor Cyan
Write-Host " Functions : Get-RegressionMetrics" -ForegroundColor Cyan
Write-Host " Get-ClassificationMetrics" -ForegroundColor Cyan
Write-Host " Invoke-KFoldCV" -ForegroundColor Cyan
Write-Host " Get-VBAFDataset" -ForegroundColor Cyan
Write-Host ""
Write-Host " Quick start:" -ForegroundColor Yellow
Write-Host ' $data = Get-VBAFDataset -Name "HousePrice"' -ForegroundColor White
Write-Host ' $scaler = [StandardScaler]::new()' -ForegroundColor White
Write-Host ' $Xs = $scaler.FitTransform($data.X)' -ForegroundColor White
Write-Host ' $model = [LinearRegression]::new()' -ForegroundColor White
Write-Host ' $model.Fit($Xs, $data.y)' -ForegroundColor White
Write-Host ' $model.PrintSummary()' -ForegroundColor White
Write-Host ""