public static double NormalCdf(double x, double mean, double variance)
        {
            double centered = x - mean;
            double ztrans   = centered / (Math.Sqrt(variance) * Math.Sqrt(2));

            return(0.5 * (1 + ProbabilityFunctions.Erf(ztrans)));
        }
        public static bool TryGetBiasStatistics(LinearModelStatistics stats, Single bias, out Single stdError, out Single zScore, out Single pValue)
        {
            if (!stats._coeffStdError.HasValue)
            {
                stdError = 0;
                zScore   = 0;
                pValue   = 0;
                return(false);
            }

            const Double sqrt2 = 1.41421356237; // Math.Sqrt(2);

            stdError = stats._coeffStdError.Value.Values[0];
            Contracts.Assert(stdError == stats._coeffStdError.Value.GetItemOrDefault(0));
            zScore = bias / stdError;
            pValue = 1 - (Single)ProbabilityFunctions.Erf(Math.Abs(zScore / sqrt2));
            return(true);
        }
        private IEnumerable <CoefficientStatistics> GetUnorderedCoefficientStatistics(LinearBinaryPredictor parent, RoleMappedSchema schema)
        {
            Contracts.AssertValue(_env);
            _env.CheckValue(parent, nameof(parent));

            if (!_coeffStdError.HasValue)
            {
                yield break;
            }

            var weights = parent.Weights2 as IReadOnlyList <Single>;

            _env.Assert(_paramCount == 1 || weights != null);
            _env.Assert(_coeffStdError.Value.Length == weights.Count + 1);

            var names = default(VBuffer <ReadOnlyMemory <char> >);

            MetadataUtils.GetSlotNames(schema, RoleMappedSchema.ColumnRole.Feature, weights.Count, ref names);

            Single[]     stdErrorValues = _coeffStdError.Value.Values;
            const Double sqrt2          = 1.41421356237; // Math.Sqrt(2);

            bool denseStdError = _coeffStdError.Value.IsDense;

            int[]    stdErrorIndices = _coeffStdError.Value.Indices;
            Single[] zScores         = new Single[_paramCount - 1];
            for (int i = 1; i < _paramCount; i++)
            {
                int wi = denseStdError ? i - 1 : stdErrorIndices[i] - 1;
                _env.Assert(0 <= wi && wi < weights.Count);
                var name = names.GetItemOrDefault(wi).ToString();
                if (string.IsNullOrEmpty(name))
                {
                    name = $"f{wi}";
                }
                var weight   = weights[wi];
                var stdError = stdErrorValues[i];
                var zScore   = zScores[i - 1] = weight / stdError;
                var pValue   = 1 - (Single)ProbabilityFunctions.Erf(Math.Abs(zScore / sqrt2));
                yield return(new CoefficientStatistics(name, weight, stdError, zScore, pValue));
            }
        }
            private Double ComputeKernelPValue(Double rawScore)
            {
                int i;
                int n = RawScoreBuffer.Count;

                if (n == 0)
                {
                    return(0.5);
                }

                Double pValue    = 0;
                Double bandWidth = Math.Sqrt(2) * ((n == 1) ? 1 : Math.Sqrt(_sumSquaredDist) / n);

                bandWidth = Math.Max(bandWidth, 1e-6);

                Double diff;

                for (i = 0; i < n; ++i)
                {
                    diff             = rawScore - RawScoreBuffer[i];
                    pValue          -= ProbabilityFunctions.Erf(diff / bandWidth);
                    _sumSquaredDist += diff * diff;
                }

                pValue = 0.5 + pValue / (2 * n);
                if (RawScoreBuffer.IsFull)
                {
                    for (i = 1; i < n; ++i)
                    {
                        diff             = RawScoreBuffer[0] - RawScoreBuffer[i];
                        _sumSquaredDist -= diff * diff;
                    }

                    diff             = RawScoreBuffer[0] - rawScore;
                    _sumSquaredDist -= diff * diff;
                }

                return(pValue);
            }
 public static double StdNormalCdf(double x)
 {
     return(0.5 * (1 + ProbabilityFunctions.Erf(x * 1 / Math.Sqrt(2))));
 }
        private static void GetUnorderedCoefficientStatistics(LinearModelStatistics stats, ref VBuffer <Single> weights, ref VBuffer <ReadOnlyMemory <char> > names,
                                                              ref VBuffer <Single> estimate, ref VBuffer <Single> stdErr, ref VBuffer <Single> zScore, ref VBuffer <Single> pValue, out ValueGetter <VBuffer <ReadOnlyMemory <char> > > getSlotNames)
        {
            if (!stats._coeffStdError.HasValue)
            {
                getSlotNames = null;
                return;
            }

            Contracts.Assert(stats._coeffStdError.Value.Length == weights.Length + 1);

            var estimateValues = estimate.Values;

            if (Utils.Size(estimateValues) < stats.ParametersCount - 1)
            {
                estimateValues = new Single[stats.ParametersCount - 1];
            }
            var stdErrorValues = stdErr.Values;

            if (Utils.Size(stdErrorValues) < stats.ParametersCount - 1)
            {
                stdErrorValues = new Single[stats.ParametersCount - 1];
            }
            var zScoreValues = zScore.Values;

            if (Utils.Size(zScoreValues) < stats.ParametersCount - 1)
            {
                zScoreValues = new Single[stats.ParametersCount - 1];
            }
            var pValueValues = pValue.Values;

            if (Utils.Size(pValueValues) < stats.ParametersCount - 1)
            {
                pValueValues = new Single[stats.ParametersCount - 1];
            }

            const Double sqrt2 = 1.41421356237; // Math.Sqrt(2);

            bool denseStdError = stats._coeffStdError.Value.IsDense;

            int[] stdErrorIndices = stats._coeffStdError.Value.Indices;
            for (int i = 1; i < stats.ParametersCount; i++)
            {
                int wi = denseStdError ? i - 1 : stdErrorIndices[i] - 1;
                Contracts.Assert(0 <= wi && wi < weights.Length);
                var weight   = estimateValues[i - 1] = weights.GetItemOrDefault(wi);
                var stdError = stdErrorValues[wi] = stats._coeffStdError.Value.Values[i];
                zScoreValues[i - 1] = weight / stdError;
                pValueValues[i - 1] = 1 - (Single)ProbabilityFunctions.Erf(Math.Abs(zScoreValues[i - 1] / sqrt2));
            }

            estimate = new VBuffer <Single>(stats.ParametersCount - 1, estimateValues, estimate.Indices);
            stdErr   = new VBuffer <Single>(stats.ParametersCount - 1, stdErrorValues, stdErr.Indices);
            zScore   = new VBuffer <Single>(stats.ParametersCount - 1, zScoreValues, zScore.Indices);
            pValue   = new VBuffer <Single>(stats.ParametersCount - 1, pValueValues, pValue.Indices);

            var slotNames = names;

            getSlotNames =
                (ref VBuffer <ReadOnlyMemory <char> > dst) =>
            {
                var values = dst.Values;
                if (Utils.Size(values) < stats.ParametersCount - 1)
                {
                    values = new ReadOnlyMemory <char> [stats.ParametersCount - 1];
                }
                for (int i = 1; i < stats.ParametersCount; i++)
                {
                    int wi = denseStdError ? i - 1 : stdErrorIndices[i] - 1;
                    values[i - 1] = slotNames.GetItemOrDefault(wi);
                }
                dst = new VBuffer <ReadOnlyMemory <char> >(stats.ParametersCount - 1, values, dst.Indices);
            };
        }
Exemple #7
0
 public static double erf(double x)
 => ProbabilityFunctions.Erf(x);