Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java คำนวณค่าเบี่ยงเบนมาตรฐาน (Standard Deviation)


ในบทความนี้ เราจะเรียนรู้วิธีการคำนวณค่าเบี่ยงเบนมาตรฐาน (Standard Deviation) ค่าเบี่ยงเบนมาตรฐานเป็นตัววัดว่าตัวเลขกระจายตัวมากน้อยแค่ไหน สัญลักษณ์คือ ซิกม้า (σ) มันคือรากที่สองของค่าแปรผัน (Variance)

สูตรการคำนวณค่าเบี่ยงเบนมาตรฐาน: รากที่สองของ Σ(Xi - μ)² / N โดยที่ Xi คือองค์ประกอบของอาร์เรย์ μ คือค่าเฉลี่ยขององค์ประกอบ N คือจำนวนองค์ประกอบ Σ คือผลรวมของแต่ละองค์ประกอบ

ตัวอย่างการทำงาน

สมมติข้อมูลนำเข้า (Input) คือ:

Input Array : [ 35.0, 48.0, 60.0, 71.0, 80.0, 95.0, 130.0 ]

ผลลัพธ์ที่ต้องการ (Output) คือ:

Standard Deviation: 29.313227

อัลกอริทึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศอาร์เรย์ double input_array และตัวแปร double sum และ standard_deviation
ขั้นตอนที่ 3 - อ่านค่าจากผู้ใช้ หรือกำหนดค่า
ขั้นตอนที่ 4 - คำนวณ Σ(Xi - μ)² / N เก็บค่าไว้ในตัวแปรผลลัพธ์
ขั้นตอนที่ 5 - แสดงผลลัพธ์
ขั้นตอนที่ 6 - จบการทำงาน

ตัวอย่างที่ 1: รับค่าจากผู้ใช้

public class StandardDeviation { 
    public static void main(String[] args) { 
        double[] input_array = { 35, 48, 60, 71, 80, 95, 130}; 
        System.out.println("The elements of the array is defined as"); 
        for (double i : input_array) { 
            System.out.print(i +" "); 
        } 
        double sum = 0.0, standard_deviation = 0.0; 
        int array_length = input_array.length; 
        for(double temp : input_array) { 
            sum += temp; 
        } 
        double mean = sum/array_length; 
        for(double temp: input_array) { 
            standard_deviation += Math.pow(temp - mean, 2); 
        } 
        double result = Math.sqrt(standard_deviation/array_length); 
        System.out.format("\n\nThe Standard Deviation is: %.6f", result); 
    } 
}

ผลลัพธ์

The elements of the array is defined as 35.0 48.0 60.0 71.0 80.0 95.0 130.0
The Standard Deviation is: 29.313227

ตัวอย่างที่ 2: ใช้ฟังก์ชันแยกคำนวณ

public class StandardDeviation { 
    public static void main(String[] args) { 
        double[] input_array = { 35, 48, 60, 71, 80, 95, 130}; 
        System.out.println("The elements of the array is defined as"); 
        for (double i : input_array) { 
            System.out.print(i +" "); 
        } 
        double standard_deviation = calculateSD(input_array); 
        System.out.format("\n\nThe Standard Deviation is: %.6f", standard_deviation); 
    } 
    public static double calculateSD(double input_array[]) { 
        double sum = 0.0, standard_deviation = 0.0; 
        int array_length = input_array.length; 
        for(double temp : input_array) { 
            sum += temp; 
        } 
        double mean = sum/array_length; 
        for(double temp: input_array) { 
            standard_deviation += Math.pow(temp - mean, 2); 
        } 
        return Math.sqrt(standard_deviation/array_length); 
    } 
}

ผลลัพธ์

The elements of the array is defined as 35.0 48.0 60.0 71.0 80.0 95.0 130.0
The Standard Deviation is: 29.313227