Capsulates an ensemble of internally (frequently) used math functions.
Esempio n. 1
0
        public BarPlotValue Function(MatrixValue Y, ScalarValue nbins)
        {
            var nn = nbins.GetIntegerOrThrowException("nbins", Name);
            var bp = new BarPlotValue();

            if (Y.IsVector)
            {
                bp.AddPoints(YMath.Histogram(Y, nn));
            }
            else
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), nn);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                bp.AddPoints(M);
            }

            return(bp);
        }
        public MatrixValue Function(MatrixValue Y, MatrixValue x)
        {
            var X = new Double[x.Length];

            for (var i = 0; i < x.Length; i++)
            {
                X[i] = x[i + 1].Re;
            }

            if (!Y.IsVector)
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), X);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                return(M);
            }

            return(YMath.Histogram(Y, X));
        }
Esempio n. 3
0
        public BarPlotValue Function(MatrixValue Y, MatrixValue x)
        {
            var bp = new BarPlotValue();
            var X  = new double[x.Length];

            for (var i = 0; i < x.Length; i++)
            {
                X[i] = x[i + 1].Re;
            }

            if (Y.IsVector)
            {
                bp.AddPoints(YMath.Histogram(Y, X));
            }
            else
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), X);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                bp.AddPoints(M);
            }

            return(bp);
        }
Esempio n. 4
0
        public static Value Variance(MatrixValue M)
        {
            if (M.Length == 0)
            {
                return(ScalarValue.Zero);
            }

            if (M.IsVector)
            {
                var variance = ScalarValue.Zero;
                var mean     = M.Sum() / M.Length;

                for (int i = 1; i <= M.Length; i++)
                {
                    variance += (M[i] - mean).Square();
                }

                return(variance / M.Length);
            }

            var avg   = (MatrixValue)YMath.Average(M);
            var scale = 1.0;
            var s     = new MatrixValue(1, M.DimensionX);

            for (var i = 1; i <= M.DimensionY; i++)
            {
                for (int j = 1; j <= M.DimensionX; j++)
                {
                    s[1, j] += (M[i, j] - avg[j]).Square();
                }
            }

            scale /= M.DimensionY;

            for (var i = 1; i <= s.DimensionY; i++)
            {
                for (int j = 1; j <= s.DimensionX; j++)
                {
                    s[i, j] *= scale;
                }
            }

            return(s);
        }
Esempio n. 5
0
        public MatrixValue Function(MatrixValue M, ScalarValue nLag)
        {
            if (M.Length <= 1)
            {
                return(new MatrixValue());
            }

            var nOffset = nLag.GetIntegerOrThrowException("nLag", Name);

            if (nOffset < 0)
            {
                nOffset = 0;
            }
            else if (nOffset >= M.Length)
            {
                nOffset = M.Length - 1;
            }

            return(YMath.CrossCorrelation(M, M, nOffset));
        }
Esempio n. 6
0
 public MatrixValue Function(MatrixValue M)
 {
     if (M.Length <= 1)
     {
         return(new MatrixValue());
     }
     else
     {
         int nOffset = (int)(10 * Math.Log10(M.Length));
         if (nOffset < 0)
         {
             nOffset = 0;
         }
         else if (nOffset >= M.Length)
         {
             nOffset = M.Length - 1;
         }
         return(YMath.CrossCorrelation(M, M, nOffset));
     }
 }
Esempio n. 7
0
        public MatrixValue Function(MatrixValue M, MatrixValue N, ScalarValue lag)
        {
            if (M.Length != N.Length || M.Length <= 1)
            {
                return(new MatrixValue());
            }

            int nOffset = lag.GetIntegerOrThrowException("lag", Name);

            if (nOffset < 0)
            {
                nOffset = 0;
            }
            else if (nOffset >= M.Length)
            {
                nOffset = M.Length - 1;
            }

            return(YMath.CrossCorrelation(M, N, nOffset));
        }
        public MatrixValue Function(MatrixValue M, MatrixValue N)
        {
            if (M.Length == N.Length && M.Length > 1)
            {
                var nOffset = (Int32)(10 * Math.Log10(M.Length));

                if (nOffset < 0)
                {
                    nOffset = 0;
                }
                else if (nOffset >= M.Length)
                {
                    nOffset = M.Length - 1;
                }

                return(YMath.CrossCorrelation(M, N, nOffset));
            }

            return(new MatrixValue());
        }
Esempio n. 9
0
 public ScalarValue Function(MatrixValue M)
 {
     return(YMath.Median(M));
 }
        public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P)
        {
            var numberOfBootstrapSamples = n.GetIntegerOrThrowException("n", Name);
            var nConfigs     = cfgs.DimensionY;
            var nData        = cfgs.DimensionX;
            var distribution = new DiscreteUniformDistribution(Rng)
            {
                Beta  = nConfigs,
                Alpha = 1
            };

            if (numberOfBootstrapSamples <= 1)
            {
                throw new YAMPException("Bootstrap: The number of bootstrap samples n is smaller or equal to 1!");
            }

            var parameters = new ArgumentsValue(cfgs);

            foreach (var m in P.Values)
            {
                parameters.Insert(m);
            }

            var temp    = f.Perform(Context, parameters);
            var nResult = 0;//dimension of the result

            if (temp is ScalarValue)
            {
                nResult = 1;
            }
            else if (temp is MatrixValue)
            {
                nResult = ((MatrixValue)temp).Length;
            }
            else
            {
                throw new YAMPException("Bootstrap: The observable f has to return either a scalar or a matrix!");
            }

            var BootstrapObservable = new MatrixValue(numberOfBootstrapSamples, nResult);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                var BootstrapConfigs = new MatrixValue(nConfigs, nData);

                for (var j = 1; j <= nConfigs; j++)
                {
                    var idx = distribution.Next();

                    for (var k = 1; k <= nData; k++)
                    {
                        BootstrapConfigs[j, k] = cfgs[idx, k];
                    }
                }

                parameters = new ArgumentsValue(BootstrapConfigs);

                foreach (var m in P.Values)
                {
                    parameters.Insert(m);
                }

                temp = f.Perform(Context, parameters);

                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] = (ScalarValue)temp;
                }
                else
                {
                    var m = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] = m[k];
                    }
                }
            }

            temp = YMath.Average(BootstrapObservable);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] -= temp as ScalarValue;
                    BootstrapObservable[i] *= BootstrapObservable[i];
                }
                else
                {
                    var T = temp as MatrixValue;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] -= T[k];
                        BootstrapObservable[i, k] *= BootstrapObservable[i, k];
                    }
                }
            }

            var error = YMath.Average(BootstrapObservable);
            var sqrt  = new SqrtFunction();

            error = sqrt.Perform(error);
            var result = new MatrixValue(2, nResult);

            if (temp is ScalarValue)
            {
                result[1] = (ScalarValue)temp;
                result[2] = (ScalarValue)error;
            }
            else
            {
                var T = (MatrixValue)temp;
                var E = (MatrixValue)error;

                for (var k = 1; k <= nResult; k++)
                {
                    result[1, k] = T[k];
                    result[2, k] = E[k];
                }
            }

            return(result);
        }
Esempio n. 11
0
 public Value Function(MatrixValue M)
 {
     return(YMath.Average(M));
 }
Esempio n. 12
0
        public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P)
        {
            var numberOfBlocks = n.GetIntegerOrThrowException("n", Name);
            var nConfigs       = cfgs.DimensionY;
            var nData          = cfgs.DimensionX;

            if (numberOfBlocks > nConfigs)
            {
                throw new YAMPException("Jackknife: The number of measurements n is greater than the number of configurations cfgs!");
            }

            if (numberOfBlocks <= 1)
            {
                throw new YAMPException("Jackknife: The number of measurements n <= 1!");
            }

            var parameters = new ArgumentsValue(cfgs);

            foreach (var m in P.Values)
            {
                parameters.Insert(m);
            }

            var temp = f.Perform(Context, parameters);
            int nResult;//dimension of the result

            if (temp is ScalarValue)
            {
                nResult = 1;
            }
            else if (temp is MatrixValue)
            {
                nResult = ((MatrixValue)temp).Length;
            }
            else
            {
                throw new YAMPException("Jackknife: Observable f has to return either a scalar or a matrix!");
            }

            var JackknifeObservable = new MatrixValue(numberOfBlocks, nResult);
            var BlockSize           = nConfigs / numberOfBlocks;
            var nConfigsBlocked     = BlockSize * numberOfBlocks;
            var residualConfigs     = nConfigs - nConfigsBlocked;

            for (var i = 1; i <= numberOfBlocks; i++)
            {
                if (i <= numberOfBlocks - residualConfigs)
                {
                    //the first (NumberOfBlocks - residualConfigs) blocks discard (BlockSize) elements ...
                    var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize, nData);
                    var j = 1;

                    for (; j <= (i - 1) * BlockSize; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j, k] = cfgs[j, k];
                        }
                    }

                    j += BlockSize;

                    for (; j <= nConfigs; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j - BlockSize, k] = cfgs[j, k];
                        }
                    }

                    parameters = new ArgumentsValue(JackknifeConfigs);
                }
                else
                {
                    //... whereas the residual (residualConfigs) blocks discard (BlockSize + 1) elements
                    var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize - 1, nData);
                    var j = 1;

                    for (; j <= nConfigs - (numberOfBlocks - (i - 1)) * (BlockSize + 1); j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j, k] = cfgs[j, k];
                        }
                    }

                    j += BlockSize + 1;

                    for (; j <= nConfigs; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j - BlockSize - 1, k] = cfgs[j, k];
                        }
                    }

                    parameters = new ArgumentsValue(JackknifeConfigs);
                }

                foreach (var m in P.Values)
                {
                    parameters.Insert(m);
                }

                temp = f.Perform(Context, parameters);

                if (temp is ScalarValue)
                {
                    JackknifeObservable[i] = (ScalarValue)temp;
                }
                else
                {
                    var T = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        JackknifeObservable[i, k] = T[k];
                    }
                }
            }

            temp = YMath.Average(JackknifeObservable);

            for (var i = 1; i <= numberOfBlocks; i++)
            {
                if (temp is ScalarValue)
                {
                    JackknifeObservable[i] -= temp as ScalarValue;
                    JackknifeObservable[i] *= JackknifeObservable[i];
                }
                else
                {
                    var m = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        JackknifeObservable[i, k] -= m[k];
                        JackknifeObservable[i, k] *= JackknifeObservable[i, k];
                    }
                }
            }

            var error = YMath.Average(JackknifeObservable);
            var scale = numberOfBlocks - 1.0;

            if (error is ScalarValue)
            {
                error = ((ScalarValue)error) * scale;
            }
            else
            {
                var e = (MatrixValue)error;

                for (var i = 1; i <= e.DimensionY; i++)
                {
                    for (var j = 1; j <= e.DimensionX; j++)
                    {
                        e[i, j] *= scale;
                    }
                }
            }

            var sqrt = new SqrtFunction();

            error = sqrt.Perform(error);
            var result = new MatrixValue(2, nResult);

            if (temp is ScalarValue)
            {
                result[1] = (ScalarValue)temp;
                result[2] = (ScalarValue)error;
            }
            else
            {
                var T = (MatrixValue)temp;
                var E = (MatrixValue)error;

                for (var k = 1; k <= nResult; k++)
                {
                    result[1, k] = T[k];
                    result[2, k] = E[k];
                }
            }

            return(result);
        }
Esempio n. 13
0
 public Value Function(MatrixValue M)
 {
     return(YMath.Mean(M));
 }
Esempio n. 14
0
        public FunctionValue Function(MatrixValue Y, ScalarValue nbins, ScalarValue nParameters)
        {
            var nn      = nbins.GetIntegerOrThrowException("nbins", Name);
            var nP      = nParameters.GetIntegerOrThrowException("nParameters", Name);
            var N       = Y.Length;
            var min_idx = Y.Min();
            var min     = Y[min_idx.Row, min_idx.Column];
            var max_idx = Y.Max();
            var max     = Y[max_idx.Row, max_idx.Column];
            var median  = YMath.Median(Y);

            var variance = ScalarValue.Zero;
            var mean     = Y.Sum() / Y.Length;

            for (int i = 1; i <= Y.Length; i++)
            {
                variance += (Y[i] - mean).Square();
            }

            variance /= Y.Length;

            var delta = (max - min) / nn;

            var x = new MatrixValue(nn, 1);

            for (int i = 0; i < nn; i++)
            {
                x[i + 1] = min + delta * i;
            }

            var histogram = new HistogramFunction();
            var fx        = histogram.Function(Y, x);
            var linearfit = new LinfitFunction();

            var dist = linearfit.Function(x, fx, new FunctionValue((context, argument) =>
            {
                var _x       = (argument as ScalarValue - median / 2) / (variance / 4);
                var _exp_x_2 = (-_x * _x).Exp();
                var result   = new MatrixValue(1, nP - 1);

                for (int i = 0; i < nP - 1; i++)
                {
                    result[i + 1] = _exp_x_2 * _x.Pow(new ScalarValue(i));
                }

                return(result);
            }, true));

            var norm        = Y.Length * (max - min) / nbins;
            var normed_dist = new FunctionValue((context, argument) =>
            {
                var temp = dist.Perform(context, argument);

                if (temp is ScalarValue)
                {
                    return((temp as ScalarValue) / norm);
                }
                else if (temp is MatrixValue)
                {
                    return((temp as MatrixValue) / norm);
                }
                else
                {
                    throw new YAMPOperationInvalidException();
                }
            }, true);

            return(normed_dist);
        }
Esempio n. 15
0
 public MatrixValue Function(MatrixValue M)
 {
     return(YMath.Correlation(M));
 }
Esempio n. 16
0
 public Value Function(MatrixValue M)
 {
     return(YMath.Variance(M));
 }