public async Task <string> SendRequestAsync(string transactionHash)
        {
            var transaction = await _apiTransactionsService.GetTransactionByHash.SendRequestAsync(transactionHash);

            var transactionInput    = transaction.ConvertToTransactionInput();
            var functionCallDecoder = new FunctionCallDecoder();

            if (transactionInput.MaxFeePerGas != null)
            {
                transactionInput.GasPrice = null;
            }
            try
            {
                var errorHex = await _apiTransactionsService.Call.SendRequestAsync(transactionInput, new BlockParameter(transaction.BlockNumber));

                if (ErrorFunction.IsErrorData(errorHex))
                {
                    return(functionCallDecoder.DecodeFunctionErrorMessage(errorHex));
                }
                return(string.Empty);
            }
            catch (RpcResponseException rpcException)
            {
                ContractRevertExceptionHandler.HandleContractRevertException(rpcException);
                throw;
            }
        }
            public SsaAnomalyDetectionBase(IHostEnvironment env, ModelLoadContext ctx, string name)
                : base(env, ctx, name)
            {
                // *** Binary format ***
                // <base>
                // int: _seasonalWindowSize
                // float: _discountFactor
                // byte: _errorFunction
                // bool: _isAdaptive
                // AdaptiveSingularSpectrumSequenceModeler: _model

                Host.CheckDecode(InitialWindowSize == 0);

                SeasonalWindowSize = ctx.Reader.ReadInt32();
                Host.CheckDecode(2 <= SeasonalWindowSize);

                DiscountFactor = ctx.Reader.ReadSingle();
                Host.CheckDecode(0 <= DiscountFactor && DiscountFactor <= 1);

                byte temp;

                temp = ctx.Reader.ReadByte();
                Host.CheckDecode(Enum.IsDefined(typeof(ErrorFunction), temp));
                ErrorFunction = (ErrorFunction)temp;
                ErrorFunc     = ErrorFunctionUtils.GetErrorFunction(ErrorFunction);

                IsAdaptive = ctx.Reader.ReadBoolean();
                StateRef   = new State(ctx.Reader);

                ctx.LoadModel <SequenceModelerBase <Single, Single>, SignatureLoadModel>(env, out Model, "SSA");
                Host.CheckDecode(Model != null);
                StateRef.InitState(this, Host);
            }
Esempio n. 3
0
        /// <summary>
        /// Used internally to calculate the error for a training set.
        /// </summary>
        /// <param name="trainingSet"> The training set to calculate for. </param>
        /// <returns> The error value. </returns>
        private double determineError(DataSet trainingSet)
        {
            double result = 0d;

            IEnumerator <DataSetRow> iterator = trainingSet.iterator();

            while (iterator.MoveNext() && !Stopped)
            {
                DataSetRow trainingSetRow = iterator.Current;
                double[]   input          = trainingSetRow.Input;
                Network.Input = input;
                Network.calculate();
                double[] output        = Network.Output;
                double[] desiredOutput = trainingSetRow.DesiredOutput;

                double[] patternError = ErrorFunction.calculatePatternError(desiredOutput, output);
                double   sqrErrorSum  = 0;
                foreach (double error in patternError)
                {
                    sqrErrorSum += (error * error);
                }
                result += sqrErrorSum / (2 * patternError.Length);
            }

            return(result);
        }
Esempio n. 4
0
 public double Cdf(double x)
 {
     if (x < 1e-9)
     {
         return(0);
     }
     return(0.5 * (1 + ErrorFunction.Value((Log(x) - Mean) / (Constants.Sqrt2 * StandardDeviation))));
 }
Esempio n. 5
0
 /// <summary>
 /// Perform SSA spike detection over a column of time series data. See <see cref="SsaSpikeEstimator"/>.
 /// </summary>
 public static Vector <double> SsaSpikeDetect(
     this Scalar <float> input,
     int confidence,
     int changeHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     AnomalySide side            = AnomalySide.TwoSided,
     ErrorFunction errorFunction = ErrorFunction.SignedDifference
     ) => new OutColumn(input, confidence, changeHistoryLength, trainingWindowSize, seasonalityWindowSize, side, errorFunction);
Esempio n. 6
0
        // Use this for initialization
        void Start()
        {
            if (this.tag == "finger")
            {
                GetJoints();
            }

            ErrorFunction = DistanceFromTarget;
        }
Esempio n. 7
0
        /// <summary>
        ///     Calculates matched error (out-target) and propagates it through layer to inputs
        /// </summary>
        /// <param name="targets">Sequence of targets</param>
        public virtual List <Matrix <T> > ErrorPropagate(List <Matrix <T> > targets)
        {
            if (ErrorFunction == null)
            {
                throw new InvalidOperationException("Layer error function is not specified!");
            }

            return(ErrorFunction.BackpropagateError(Outputs, targets));
        }
Esempio n. 8
0
        /// <summary>
        ///     Calculates matched layer error.
        /// </summary>
        /// <param name="y">Layer output</param>
        /// <param name="target">Layer target</param>
        /// <returns></returns>
        public virtual double LayerError(Matrix <T> y, Matrix <T> target)
        {
            if (ErrorFunction == null)
            {
                throw new InvalidOperationException("Layer error function is not specified!");
            }

            return(ErrorFunction.GetError(y, target));
        }
Esempio n. 9
0
 /// <summary>
 /// Perform SSA change point detection over a column of time series data. See <see cref="SsaChangePointEstimator"/>.
 /// </summary>
 public static Vector <double> SsaChangePointDetect(
     this Scalar <float> input,
     int confidence,
     int changeHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     ErrorFunction errorFunction = ErrorFunction.SignedDifference,
     MartingaleType martingale   = MartingaleType.Power,
     double eps = 0.1) => new OutColumn(input, confidence, changeHistoryLength, trainingWindowSize, seasonalityWindowSize, errorFunction, martingale, eps);
Esempio n. 10
0
        // Use this for initialization
        void Start()
        {
            if (Joints == null)
            {
                GetJoints();
            }

            ErrorFunction = DistanceFromTarget;
        }
        public static void MNIST2()
        {
            DataSet dataSet = new DataSet("mnist2.txt", ' ', 10, false);
            var     p       = new Perceptron(600, 3, ErrorFunction.CrossEntropy()).Layer(784, null).Layer(16, ActivationFunction.Sigmoid())
                              .Layer(16, ActivationFunction.Sigmoid()).Layer(10, ActivationFunction.Sigmoid());

            p.Train2(dataSet, 200);
            double mse = p.CalculateMeanErrorOverDataSet(dataSet);
        }
        // Use this for initialization
        void Start()
        {
            target = (Vec3)Destination.position;
            if (Joints == null)
            {
                GetJoints();
            }

            ErrorFunction = DistanceFromTarget;
        }
Esempio n. 13
0
 public OutColumn(Scalar <float> input,
                  int confidence,
                  int pvalueHistoryLength,
                  int trainingWindowSize,
                  int seasonalityWindowSize,
                  AnomalySide side,
                  ErrorFunction errorFunction)
     : base(new Reconciler(confidence, pvalueHistoryLength, trainingWindowSize, seasonalityWindowSize, side, errorFunction), input)
 {
     Input = input;
 }
Esempio n. 14
0
        public async Task <string> SendRequestAsync(string transactionHash)
        {
            var transaction = await _apiTransactionsService.GetTransactionByHash.SendRequestAsync(transactionHash);

            var errorHex = await _apiTransactionsService.Call.SendRequestAsync(transaction.ConvertToTransactionInput(), new BlockParameter(transaction.BlockNumber));

            if (ErrorFunction.IsErrorData(errorHex))
            {
                return(new FunctionCallDecoder().DecodeFunctionErrorMessage(errorHex));
            }
            return(string.Empty);
        }
Esempio n. 15
0
 public OutColumn(Scalar <float> input,
                  int confidence,
                  int changeHistoryLength,
                  int trainingWindowSize,
                  int seasonalityWindowSize,
                  ErrorFunction errorFunction,
                  MartingaleType martingale,
                  double eps)
     : base(new Reconciler(confidence, changeHistoryLength, trainingWindowSize, seasonalityWindowSize, errorFunction, martingale, eps), input)
 {
     Input = input;
 }
Esempio n. 16
0
 public double Quantile(Probability p)
 {
     if (p < 1e-9)
     {
         return(0);
     }
     if (p > 1 - 1e-9)
     {
         return(double.PositiveInfinity);
     }
     return(Exp(Mean + Constants.Sqrt2 * StandardDeviation * ErrorFunction.InverseValue(2 * p - 1)));
 }
Esempio n. 17
0
        public static IEstimator <ITransformer> _DetectSpikeBySsa(this MLContext MLContext, JToken componentObject)
        {
            string        outputColumn      = componentObject.Value <string>("OutputColumnName");
            string        inputColumn       = componentObject.Value <string>("InputColumnName");
            int           confidence        = componentObject.Value <int>("Confidence");
            int           pValueHistory     = componentObject.Value <int>("PvalueHistoryLength");
            int           trainingWindow    = componentObject.Value <int>("TrainingWindowSize");
            int           seasonalityWindow = componentObject.Value <int>("SeasonalityWindowSize");
            AnomalySide   side          = Enum.Parse <AnomalySide>(componentObject.Value <string>("AnomalySide"));
            ErrorFunction errorFunction = Enum.Parse <ErrorFunction>(componentObject.Value <string>("ErrorFunction"));

            return(MLContext.Transforms.DetectSpikeBySsa(outputColumn, inputColumn, confidence, pValueHistory,
                                                         trainingWindow, seasonalityWindow, side, errorFunction));
        }
Esempio n. 18
0
        public virtual void Save(Stream s)
        {
            using (var writer = s.NonGreedyWriter())
            {
                writer.Write(BatchSize);
                writer.Write(SeqLen);
                writer.Write(ErrorFunction != null);

                if (ErrorFunction != null)
                {
                    writer.Write(ErrorFunction.GetType().FullName);
                }
            }
        }
Esempio n. 19
0
 public Reconciler(
     int confidence,
     int pvalueHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     AnomalySide side,
     ErrorFunction errorFunction)
 {
     _confidence            = confidence;
     _pvalueHistoryLength   = pvalueHistoryLength;
     _trainingWindowSize    = trainingWindowSize;
     _seasonalityWindowSize = seasonalityWindowSize;
     _side          = side;
     _errorFunction = errorFunction;
 }
Esempio n. 20
0
        public static IEstimator <ITransformer> _DetectChangePointBySsa(this MLContext MLContext, JToken componentObject)
        {
            string         outputColumn      = componentObject.Value <string>("OutputColumnName");
            string         inputColumn       = componentObject.Value <string>("InputColumnName");
            int            confidence        = componentObject.Value <int>("Confidence");
            int            changeHistory     = componentObject.Value <int>("ChangeHistoryLength");
            int            trainingWindow    = componentObject.Value <int>("TrainingWindowSize");
            int            seasonalityWindow = componentObject.Value <int>("SeasonalityWindowSize");
            ErrorFunction  errorFunction     = Enum.Parse <ErrorFunction>(componentObject.Value <string>("ErrorFunction"));
            MartingaleType martingale        = Enum.Parse <MartingaleType>(componentObject.Value <string>("MartingaleType"));
            double         eps = componentObject.Value <double>("Eps");

            return(MLContext.Transforms.DetectChangePointBySsa(outputColumn, inputColumn, confidence, changeHistory,
                                                               trainingWindow, seasonalityWindow, errorFunction,
                                                               martingale, eps));
        }
Esempio n. 21
0
 public Reconciler(
     int confidence,
     int changeHistoryLength,
     int trainingWindowSize,
     int seasonalityWindowSize,
     ErrorFunction errorFunction,
     MartingaleType martingale,
     double eps)
 {
     _confidence            = confidence;
     _changeHistoryLength   = changeHistoryLength;
     _trainingWindowSize    = trainingWindowSize;
     _seasonalityWindowSize = seasonalityWindowSize;
     _errorFunction         = errorFunction;
     _martingale            = martingale;
     _eps = eps;
 }
Esempio n. 22
0
        // Use this for initialization
        void Start()
        {
            if (Joints == null)
            {
                GetJoints();
            }

            // The error function to minimise
            if (IsTentacle)
            {
                ErrorFunction = delegate(Vector3 target, float[] solution)
                {
                    PositionRotation result = ForwardKinematics(Solution);

                    // Minimise torsion (rotation on x axis)
                    float torsion = 0;
                    for (int i = 0; i < solution.Length; i++)
                    {
                        torsion += Mathf.Abs(solution[i]) * TorsionPenality.x;
                        torsion += Mathf.Abs(solution[i]) * TorsionPenality.y;
                        torsion += Mathf.Abs(solution[i]) * TorsionPenality.z;
                    }

                    return
                        // The distance
                        (Vector3.Distance(target, result)

                         // The orientation of the effector
                         + Mathf.Abs(Quaternion.Angle(result, Destination.rotation) / 180f)
                         * OrientationWeight

                         // The torsion
                         + (torsion / solution.Length)
                         * TorsionWeight);

                    ;
                }
            }
            ;
            else
            {
                ErrorFunction = DistanceFromTarget;
            }
        }
            public SsaAnomalyDetectionBase(SsaOptions options, string name, IHostEnvironment env, SsaAnomalyDetectionBaseWrapper parent)
                : base(options.WindowSize, 0, options.Source, options.Name, name, env, options.Side, options.Martingale, options.AlertOn, options.PowerMartingaleEpsilon, options.AlertThreshold)
            {
                Host.CheckUserArg(2 <= options.SeasonalWindowSize, nameof(options.SeasonalWindowSize), "Must be at least 2.");
                Host.CheckUserArg(0 <= options.DiscountFactor && options.DiscountFactor <= 1, nameof(options.DiscountFactor), "Must be in the range [0, 1].");
                Host.CheckUserArg(Enum.IsDefined(typeof(ErrorFunction), options.ErrorFunction), nameof(options.ErrorFunction), ErrorFunctionUtils.ErrorFunctionHelpText);

                SeasonalWindowSize = options.SeasonalWindowSize;
                DiscountFactor     = options.DiscountFactor;
                ErrorFunction      = options.ErrorFunction;
                ErrorFunc          = ErrorFunctionUtils.GetErrorFunction(ErrorFunction);
                IsAdaptive         = options.IsAdaptive;
                // Creating the master SSA model
                Model = new AdaptiveSingularSpectrumSequenceModeler(Host, options.InitialWindowSize, SeasonalWindowSize + 1, SeasonalWindowSize,
                                                                    DiscountFactor, AdaptiveSingularSpectrumSequenceModeler.RankSelectionMethod.Exact, null, SeasonalWindowSize / 2, false, false);

                StateRef = new State();
                StateRef.InitState(WindowSize, InitialWindowSize, this, Host);
                Parent = parent;
            }
 /// <summary>
 /// Create a new instance of <see cref="SsaSpikeEstimator"/>
 /// </summary>
 /// <param name="env">Host Environment.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
 /// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
 /// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
 /// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
 /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.
 /// The vector contains Alert, Raw Score, P-Value as first three values.</param>
 /// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
 /// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
 internal SsaSpikeEstimator(IHostEnvironment env,
                            string outputColumnName,
                            int confidence,
                            int pvalueHistoryLength,
                            int trainingWindowSize,
                            int seasonalityWindowSize,
                            string inputColumnName      = null,
                            AnomalySide side            = AnomalySide.TwoSided,
                            ErrorFunction errorFunction = ErrorFunction.SignedDifference)
     : this(env, new SsaSpikeDetector.Options
 {
     Source = inputColumnName ?? outputColumnName,
     Name = outputColumnName,
     Confidence = confidence,
     PvalueHistoryLength = pvalueHistoryLength,
     TrainingWindowSize = trainingWindowSize,
     SeasonalWindowSize = seasonalityWindowSize,
     Side = side,
     ErrorFunction = errorFunction
 })
 {
 }
 /// <summary>
 /// Create a new instance of <see cref="SsaChangePointEstimator"/>
 /// </summary>
 /// <param name="env">Host Environment.</param>
 /// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
 /// Column is a vector of type double and size 4. The vector contains Alert, Raw Score, P-Value and Martingale score as first four values.</param>
 /// <param name="confidence">The confidence for change point detection in the range [0, 100].</param>
 /// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
 /// <param name="changeHistoryLength">The size of the sliding window for computing the p-value.</param>
 /// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
 /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
 /// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
 /// <param name="martingale">The martingale used for scoring.</param>
 /// <param name="eps">The epsilon parameter for the Power martingale.</param>
 internal SsaChangePointEstimator(IHostEnvironment env, string outputColumnName,
                                  int confidence,
                                  int changeHistoryLength,
                                  int trainingWindowSize,
                                  int seasonalityWindowSize,
                                  string inputColumnName      = null,
                                  ErrorFunction errorFunction = ErrorFunction.SignedDifference,
                                  MartingaleType martingale   = MartingaleType.Power,
                                  double eps = 0.1)
     : this(env, new SsaChangePointDetector.Options
 {
     Name = outputColumnName,
     Source = inputColumnName ?? outputColumnName,
     Confidence = confidence,
     ChangeHistoryLength = changeHistoryLength,
     TrainingWindowSize = trainingWindowSize,
     SeasonalWindowSize = seasonalityWindowSize,
     Martingale = martingale,
     PowerMartingaleEpsilon = eps,
     ErrorFunction = errorFunction
 })
 {
 }
        public static Func <Double, Double, Double> GetErrorFunction(ErrorFunction errorFunction)
        {
            switch (errorFunction)
            {
            case ErrorFunction.SignedDifference:
                return(SignedDifference);

            case ErrorFunction.AbsoluteDifference:
                return(AbsoluteDifference);

            case ErrorFunction.SignedProportion:
                return(SignedProportion);

            case ErrorFunction.AbsoluteProportion:
                return(AbsoluteProportion);

            case ErrorFunction.SquaredDifference:
                return(SquaredDifference);

            default:
                throw Contracts.Except(ErrorFunctionHelpText);
            }
        }
        public static void FillResults(ILPanel panel, Panel panelFuzzySets, NeuralNetwork ann)
        {
            SetFuzzySets(panelFuzzySets, ann);
            int xmin = (int)ann.Instance.Samples[0].Variables[0];
            int xmax = (int)ann.Instance.Samples[ann.Instance.NumSamples - 1].Variables[0];
            int ymin = (int)ann.Instance.Samples[0].Variables[1];
            int ymax = (int)ann.Instance.Samples[ann.Instance.NumSamples - 1].Variables[0];

            ErrorFunction er   = new ErrorFunction(ann.Instance.Samples, ann);
            ILPlotCube    cube = new ILPlotCube(twoDMode: false)
            {
                // rotate plot cube
                Rotation = Matrix4.Rotation(new Vector3(-1, 1, .1f), 0.4f),
                // perspective projection
                Projection = Projection.Perspective,
                Children   =
                {
                    new ILSurface((x,   y) => (float)Convert(er.ValueAt, x,               y),
                                  xmin, xmax,                            xmax - xmin + 1,
                                  ymin, ymax,                            ymax - ymin + 1,
                                  (x,   y) => x * y,
                                  colormap: Colormaps.Hsv)
                    {
                        UseLighting = true
                    }
                }
            };

            panel.Scene.Remove(panel.Scene.First <ILPlotCube>());
            panel.Scene.Add(cube);
            panel.Scene.Screen.Add(new ILLabel("Error per sample")
            {
                Position = new Vector3(1, 0, 0),
                Anchor   = new PointF(1, 0)
            });
            panel.Refresh();
        }
Esempio n. 28
0
 /// <summary>
 /// Return the percentile (i.e. cdf) at value test_statistic = q where test_statistic ~ N(0, 1)
 /// </summary>
 /// <param name="q"></param>
 /// <returns></returns>
 public static double GetPercentile2(double q)
 {
     return(0.5 + 0.5 * ErrorFunction.GetErf(q / Constants.Sqrt2));
 }
Esempio n. 29
0
 public override double GetCDF(double x)
 {
     return(0.5 + 0.5 * ErrorFunction.GetErf((x - mMean) / (Constants.Sqrt2 * mStdDev)));
 }
Esempio n. 30
0
 public static double Quantile(double p, double m, double s)
 {
     return(m + s * ErrorFunction.QuantileOfNormalDistribution01(p));
 }
Esempio n. 31
0
 public void set_train_error_function(ErrorFunction train_error_function)
 {
     fanndoublePINVOKE.neural_net_set_train_error_function(swigCPtr, (int)train_error_function);
 }
Esempio n. 32
0
 public Vector Solve(ErrorFunction errorFunction, Vector initialGuess)
 {
     if (initialGuess.Size != NumVariables)
     {
         throw new MatrixException("Size of the initial guess vector is not correct");
     }
     var vector = new Vector(initialGuess);
     NumStepsToConverge = 0;
     Vector result;
     for (var i = 0; i < 20; i++)
     {
         var matrix = CalculateJacobian(errorFunction, vector);
         var vec = errorFunction(vector);
         var matrix2 = matrix.Transpose();
         var squareMatrix = new SquareMatrix(matrix2*matrix);
         var vec2 = matrix2*vec;
         var vector2 = squareMatrix.PseudoInverse()*vec2;
         vector -= vector2;
         if (IsDone(vector2))
         {
             NumStepsToConverge = i + 1;
             result = vector;
             return result;
         }
     }
     result = vector;
     return result;
 }
Esempio n. 33
0
 private Matrix CalculateJacobian(ErrorFunction errorFunction, Vector guess)
 {
     var matrix = new Matrix(NumEquations, NumVariables);
     for (var i = 0; i < matrix.Columns; i++)
     {
         var num = (Math.Abs(guess[i]) >= 1.0) ? (Math.Abs(guess[i])*1E-07) : 1E-07;
         var vector = new Vector(guess);
         Vector vector2;
         int index;
         (vector2 = vector)[index = i] = vector2[index] + num;
         var v = errorFunction(vector);
         Vector vector3;
         int index2;
         (vector3 = vector)[index2 = i] = vector3[index2] - 2.0*num;
         var v2 = errorFunction(vector);
         var vec = v - v2;
         matrix.SetColumn(i, vec/(2.0*num));
     }
     return matrix;
 }