This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathYeoJohnsonTransformer.cs
71 lines (64 loc) · 2.31 KB
/
YeoJohnsonTransformer.cs
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
using RCNet.Extensions;
using System;
using System.Collections.Generic;
namespace RCNet.Neural.Data.Transformers
{
/// <summary>
/// Implements the transformer of values from one input field. Calculates the Yeo-Johnson transformation.
/// </summary>
/// <remarks>
/// For more detailed information see the https://en.wikipedia.org/wiki/Power_transform#Yeo%E2%80%93Johnson_transformation wiki pages or read the https://www.stat.umn.edu/arc/yjpower.pdf paper.
/// </remarks>
[Serializable]
public class YeoJohnsonTransformer : ITransformer
{
//Attributes
private readonly int _fieldIdx;
private readonly YeoJohnsonTransformerSettings _cfg;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="availableFieldNames">The collection of names of all available input fields.</param>
/// <param name="cfg">The configuration.</param>
public YeoJohnsonTransformer(List<string> availableFieldNames, YeoJohnsonTransformerSettings cfg)
{
_cfg = (YeoJohnsonTransformerSettings)cfg.DeepClone();
_fieldIdx = availableFieldNames.IndexOf(_cfg.InputFieldName);
return;
}
//Methods
/// <inheritdoc />
public void Reset()
{
return;
}
/// <inheritdoc />
public double Transform(double[] data)
{
if (double.IsNaN(data[_fieldIdx]))
{
throw new InvalidOperationException($"Invalid data value at input field index {_fieldIdx} (NaN).");
}
double arg = data[_fieldIdx].Bound();
double result;
if (_cfg.Lambda != 0 && arg >= 0)
{
result = (Math.Pow(arg + 1d, _cfg.Lambda) - 1d) / _cfg.Lambda;
}
else if (_cfg.Lambda == 0 && arg >= 0)
{
result = Math.Log(arg + 1d);
}
else if (_cfg.Lambda != 2 && arg < 0)
{
result = -(Math.Pow(-arg + 1d, 2d - _cfg.Lambda) - 1d) / (2d - _cfg.Lambda);
}
else
{
result = -Math.Log(-arg + 1d);
}
return result.Bound();
}
}//YeoJohnsonTransformer
}//Namespace