Sort by

recency

|

133 Discussions

|

  • + 0 comments

    in R Could be:

    stdin <- file('stdin')
    open(stdin)
    
    stats <- as.numeric(strsplit(trimws(readLines(stdin, n = 1, warn = FALSE), which = "right"), " ")[[1]])
    less <- as.numeric(trimws(readLines(stdin, n = 1, warn = FALSE), which = "both"))
    between <- as.numeric(strsplit(trimws(readLines(stdin, n = 1, warn = FALSE), which = "right"), " ")[[1]])
    mean <- stats[1]
    sd <- stats[2]
    cat(round(1 - pnorm(less, mean, sd, FALSE),3), fill=TRUE)
    cat(round(pnorm(between[2], mean, sd, TRUE) - pnorm(between[1], mean, sd, TRUE),3), fill=TRUE)
    
  • + 0 comments
    import math as m
    def cdf(mn, std, x):
        return 0.5 * (1 + m.erf((x-mn)/(std*m.sqrt(2))))
        
    print(f'{cdf(20,2,19.5):.3f}')
    print(f'{cdf(20,2,22) - cdf(20,2,20):.3f}')
    
  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            double mean = 20;
            double stdDeviation = 2;
            double x1 = 19.5, x2 = 22;
    
            double result1 = probability(mean, stdDeviation, x1);
            double probLe20 = 0.5; // le mean == ge mean == 50%
            double result2 = probability(mean, stdDeviation, x2) - probLe20;
    
            System.out.println(round(result1));
            System.out.println(round(result2));
        }
    		
        static double probability(double mean, double stdDeviation, double x) {
            final double z = calcZ(mean, stdDeviation, x);
            return 1 - (1 + erf(z)) / 2;
        }
    		
        static double calcZ(double mean, double stdDeviation, double x) {
            return (mean - x) / (stdDeviation * Math.sqrt(2));
        }
    		
        static double erf(double z) {
            double sum = 0;
            for (int n = 0; n < 11; n++) {
                double t = z / (2 * n + 1);
                for (int i = 1; i <= n; i++)  t *= -z * z / i;
                sum += t;
            }
            return 2 / Math.sqrt(Math.PI) * sum;
        }
    		
        static double round(double v) {
            return Math.round(v * 1000) * 1d / 1000;
        }
    }
    
  • + 0 comments

    JS

      function erf(x) {
            var a1 = 0.254829592;
            var a2 = -0.284496736;
            var a3 = 1.421413741;
            var a4 = -1.453152027;
            var a5 = 1.061405429;
            var p = 0.3275911;
            var sign = 1;
            if (x < 0) {
                sign = -1;
            }
            x = Math.abs(x);
            var t = 1.0 / (1.0 + p * x);
            var y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
            return sign * y;
        }
        let value = input.split(/[ \n]/g);
        let hours = 0.5 * (1 + erf((parseFloat([value[2]]) - parseFloat(value[0])) / (parseFloat(value[1]) * Math.sqrt(2))));
        let hour_lower = 0.5 * (1 + erf((parseFloat(value[3]) - parseFloat(value[0])) / (parseFloat(value[1]) * Math.sqrt(2))));
        let hour_upper = 0.5 * (1 + erf((parseFloat(value[4]) - parseFloat(value[0])) / (parseFloat(value[1]) * Math.sqrt(2))));
        console.log(hours.toFixed(3), "\n", (hour_upper - hour_lower).toFixed(3));
    
  • + 0 comments

    Python 3

    from statistics import NormalDist

    m,s=map(int,input().split())

    x=float(input())

    a,b=map(int,input().split())

    z1=(x-m)/s print(NormalDist().cdf(z1))

    z2=(a-m)/s z3=(b-m)/s print(NormalDist().cdf(z3)-NormalDist().cdf(z2))