แสดงบทความที่มีป้ายกำกับ SD แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ SD แสดงบทความทั้งหมด

วันอังคารที่ 31 มีนาคม พ.ศ. 2558

C# Calculate Standard Deviation (Using List Object)

ไม่มีความคิดเห็น:
สร้าง Class StandardDeviation ไว้คำนวนค่าหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
public class StandardDeviation {
        private double ExP2 { set; get; } //Ex^2
        private double Ex2 { set; get; }//(Ex)^2
        private List listScore { set; get; }
        private int n { set; get; }

        private double sum {set;get;}

        public StandardDeviation(List scores){
            this.listScore = scores;
            this.n = scores.Count;
        }

        public double Calculator(){
             calExP2();
             CalEx2();
             /*แทนค่าในสูตร 
                สูตร s.d = รูท nEx^2 - (Ez)^2 / n(n-1)
            */
            double g1 = this.n*this.ExP2 - this.Ex2;
            int g2 = this.n * (this.n - 1);

            sum = Math.Sqrt(g1 / g2);
             return sum;
        }
        private void calExP2(){
            for (int i = 0; i < this.listScore.Count; i++) {
                this.ExP2 = this.ExP2 + (this.listScore[i] * this.listScore[i]);
            }

            
        }

        private void CalEx2() {
            for (int i = 0; i < this.listScore.Count; i++ )
            {
                this.Ex2 = this.Ex2 + this.listScore[i];
            }
            this.Ex2 = (this.Ex2 * this.Ex2);
        }
    }
ตัวอย่างการเรียกใช้งาน
จากตัวอย่าง : http://www.stvc.ac.th/elearning/stat/csu3.html
//Ex2.จากข้อมูลต่อไปนี้จงหาส่วนเบี่ยงเบนมาตรฐาน 1, 2, 4, 6, 8, 9
            List listScore = new List();
            listScore.Add(1d);// 1, 2, 4, 6, 8, 9
            listScore.Add(2d);
            listScore.Add(4d);
            listScore.Add(6d);
            listScore.Add(8d);
            listScore.Add(9d);

            //1.การหาส่วนเบี่ยงเบนมาตรฐาน(S.D.) ในกรณีข้อมูลไม่ได้มีการแจกแจงความถี่
            StandardDeviation sd = new StandardDeviation(listScore);
            Console.WriteLine("SD : "+ sd.Calculator());
            Console.ReadLine();
ผลการรันโปรแกรม

Read More