Sort by

recency

|

135 Discussions

|

  • + 0 comments
    import math as m
    
    def ND(x,mean,sd):
    z = (x-mean)/(2*m.sqrt(2))
    cdf = 0.5*(1 + m.erf(z))
    return cdf 
    
    print(f"{ND(19.5,20,2):.4f}")
    p1 = ND(20,20,2)
    p2 = ND(22,20,2)
    print(f"{p2-p1:.4f}")
    
  • + 0 comments
    import math
    
    mu = 20
    sigma = 2
    
    x_1 = 19.5 
    
    x_2_a = 20
    x_2_b = 22
    
    # P(X <= x) = 0.5 * (1+math.erf((x - mu)/(sigma * 2**0.5)))
    p_less_than_19_5 = 0.5 * (1+math.erf((x_1 - mu)/(sigma * 2**0.5)))
    
    print(round(p_less_than_19_5,3))
    
    # (a <= X <= b) = F(b) - F(a)
    p_between_20_and_22 = 0.5 * (1+math.erf((x_2_b - mu)/(sigma * 2**0.5))) - 0.5 * (1+math.erf((x_2_a - mu)/(sigma * 2**0.5)))
    
    print(round(p_between_20_and_22, 3))
    
  • + 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;
        }
    }