Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Beta.cs
1using System;
2using System.Collections.Generic;
3using System.Security.Cryptography;
9
11{
16 {
17 private static readonly ArgumentType[] argumentTypes3Parameters = new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar };
18 private static readonly ArgumentType[] argumentTypes2Parameters = new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, };
19
30 : base(new ScriptNode[] { Alpha, Beta, N }, argumentTypes3Parameters, Start, Length, Expression)
31 {
32 }
33
43 : base(new ScriptNode[] { Alpha, Beta }, argumentTypes2Parameters, Start, Length, Expression)
44 {
45 }
46
50 public override string FunctionName => nameof(Beta);
51
55 public override string[] DefaultArgumentNames
56 {
57 get { return new string[] { "alpha", "beta", "N" }; }
58 }
59
67 {
68 switch (Arguments.Length)
69 {
70 case 2:
71 double Alpha = Expression.ToDouble(Arguments[0].AssociatedObjectValue);
72 double Beta = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
73
74 return new DoubleNumber(NextDouble(Alpha, Beta));
75
76 case 3:
77 default:
78 Alpha = Expression.ToDouble(Arguments[0].AssociatedObjectValue);
79 Beta = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
80 double d = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
81 int N = (int)Math.Round(d);
82 if (N < 0 || N != d)
83 throw new ScriptRuntimeException("N must be a non-negative integer.", this);
84
85 double[] v = new double[N];
86 int i;
87
88 for (i = 0; i < N; i++)
89 v[i] = NextDouble(Alpha, Beta);
90
91 return new DoubleVector(v);
92 }
93 }
94
101 public static double NextDouble(double Alpha, double Beta)
102 {
103 if (Alpha <= 0)
104 throw new ArgumentOutOfRangeException("Must be positive.", nameof(Alpha));
105
106 if (Beta <= 0)
107 throw new ArgumentOutOfRangeException("Must be positive.", nameof(Beta));
108
109 double A = Gamma.NextDouble(Alpha, 1);
110 double B = Gamma.NextDouble(Beta, 1);
111
112 return A / (A + B);
113 }
114
115 }
116
117}
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Generates a random number using the beta distribution.
Definition: Beta.cs:16
override string[] DefaultArgumentNames
Default Argument names
Definition: Beta.cs:56
static double NextDouble(double Alpha, double Beta)
Gets a beta distributed random value.
Definition: Beta.cs:101
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Beta.cs:66
override string FunctionName
Name of the function
Definition: Beta.cs:50
Beta(ScriptNode Alpha, ScriptNode Beta, ScriptNode N, int Start, int Length, Expression Expression)
Generates a random number using the beta distribution.
Definition: Beta.cs:29
Beta(ScriptNode Alpha, ScriptNode Beta, int Start, int Length, Expression Expression)
Generates a random number using the beta distribution.
Definition: Beta.cs:42
Generates a random number using the gamma distribution.
Definition: Gamma.cs:16
static double NextDouble(double Shape, double Scale)
Gets a gamma distributed random value.
Definition: Gamma.cs:101
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9