swati-jagtap / javascript-practice

0 stars 0 forks source link

9.3.4--Try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) #130

Closed swati-jagtap closed 3 years ago

swati-jagtap commented 3 years ago
//9.3.4-- Try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) 
//and measure of variability(range, variance, standard deviation).
// In addition to those measures find the min, max, count, percentile, and frequency distribution of the sample.
// You can create an object called statistics and create all the functions which do statistical calculations as method for the statistics object. 
const ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27,
   27, 24, 32, 33, 27, 25,26, 38, 37, 31, 34, 24, 33, 29, 26]

   const statistics={
     count:function(){
       var c=0;
       ages.forEach((a)=>c++)
        return c;
     },
     min:function(){
       var min=100000000;
       ages.forEach((a)=>{
         if(a<min)min=a;         
       })
        return min;
     },
     max:function(){
      return (Math.max(...ages))       
    },
     sum:function(){
       const Sum=ages.reduce((s,c)=>s+c)
       return Sum;
     },
     range:function(){
       //The Range is the difference between the lowest and highest values

       return statistics.max()-statistics.min();
     },
     mean:function(){
       return Math.round(statistics.sum()/statistics.count());
     },
     median:function(){
       //median is {(n + 1) ÷ 2}th value, where n is the number of values in a set of data.
        //data must be in ascending order
       age=ages.sort()
       //console.log(age)
       // console.log(Math.round((age.length+1)/2))     --->13
       if(age.length%2!=0) {
          return age[(Math.round((age.length+1)/2)-1)]    //-1 because array index starts from 0
       }else{
       var m=Math.floor((age.length+1)/2)
       return ((age[m]+age[m-1])/2)
       }
     },

     mode:function (arg){
       const uniV=[]
       const uniC=[]
       ages.forEach((a)=>c(a))
       function c(a){
         let c=0;
         for(let i=0;i<ages.length;i++)
         {
           if(ages[i]==a)
           {
               c++;
           }
         }
         if(uniV.indexOf(a)==-1){
           uniV.push(a)
           uniC.push(c)
         }

       }
       if(arg==1)
       {
              return `value :${uniV} \n frequency : ${uniC}`;                                                                      
       }

       var ans=(uniV[uniC.indexOf(Math.max(...uniC))])
       return `mode :${ans} , count : ${Math.max(...uniC)}`
       //console.log(uniV)
       //console.log(uniC)

      },
      var:function()
      {
        //variance=The average of the squared differences from the Mean.
        var sum=0;
         ages.forEach((a)=>{

          sum=sum+(Math.pow(a-statistics.mean(),2));
         })
         return (sum/statistics.count());
      },
      std:function()
      {
        return (Math.sqrt(statistics.var()))
      },
      freqDist:function()
      { 
        return (statistics.mode(1));

      }

   }

console.log('Count:', statistics.count()) // 25
console.log('Sum: ', statistics.sum()) // 744
console.log('Min: ', statistics.min()) // 24
console.log('Max: ', statistics.max()) // 38
console.log('Range: ', statistics.range()) // 14
console.log('Mean: ', statistics.mean()) // 30
console.log('Median: ',statistics.median()) // 29
console.log('Mode: ', statistics.mode()) // {'mode': 26, 'count': 5}
console.log('Variance: ',statistics.var()) // 17.5
console.log('Standard Deviation: ', statistics.std()) // 4.2
console.log('Frequency Distribution: ',statistics.freqDist())

image

swati-jagtap commented 3 years ago
//9.3.4-- Try to develop a program which calculate measure of central tendency of a sample(mean, median, mode) 
//and measure of variability(range, variance, standard deviation).
// In addition to those measures find the min, max, count, percentile, and frequency distribution of the sample.
// You can create an object called statistics and create all the functions which do statistical calculations as method for the statistics object. 
const ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27,
   27, 24, 32, 33, 27, 25,26, 38, 37, 31, 34, 24, 33, 29, 26]

   const statistics={
     count:function(){
       var c=0;
       ages.forEach((a)=>c++)
        return c;
     },
     min:function(){
       var min=100000000;
       ages.forEach((a)=>{
         if(a<min)min=a;         
       })
        return min;
     },
     max:function(){
      return (Math.max(...ages))       
    },
     sum:function(){
       const Sum=ages.reduce((s,c)=>s+c)
       return Sum;
     },
     range:function(){
       //The Range is the difference between the lowest and highest values

       return statistics.max()-statistics.min();
     },
     mean:function(){
       return Math.round(statistics.sum()/statistics.count());
     },
     median:function(){
       //median is {(n + 1) ÷ 2}th value, where n is the number of values in a set of data.
        //data must be in ascending order
       age=ages.sort()
       //console.log(age)
       // console.log(Math.round((age.length+1)/2))     --->13
       if(age.length%2!=0) {
          return age[(Math.round((age.length+1)/2)-1)]    //-1 because array index starts from 0
       }else{
       var m=Math.floor((age.length+1)/2)
       return ((age[m]+age[m-1])/2)
       }
     },

     mode:function (arg){
       const uniV=[]
       const uniC=[]
       ages.forEach((a)=>c(a))
       function c(a){
         let c=0;
         for(let i=0;i<ages.length;i++)
         {
           if(ages[i]==a)
           {
               c++;
           }
         }
         if(uniV.indexOf(a)==-1){
           uniV.push(a)
           uniC.push(c)
         }

       }
       if(arg==1)
       {
              return `value :${uniV} \n frequency : ${uniC}`;                                                                      
       }

       var ans=(uniV[uniC.indexOf(Math.max(...uniC))])
       return `mode :${ans} , count : ${Math.max(...uniC)}`
       //console.log(uniV)
       //console.log(uniC)

      },
      var:function()
      {
        //variance=The average of the squared differences from the Mean.
        var sum=0;
         ages.forEach((a)=>{

          sum=sum+(Math.pow(a-statistics.mean(),2));
         })
         return (sum/statistics.count());
      },
      std:function()
      {
        return (Math.sqrt(statistics.var()))
      },
      freqDist:function()
      { 
        return (statistics.mode(1));

      },
      describe:function()
      {
         return `Count:${statistics.count()}\nSum:${ statistics.sum()}\nMin:${statistics.min()}
max:${statistics.max()}\nrange:${statistics.range()}
Mean:${statistics.mean()}\nMedian:${statistics.median()}\nMode:${statistics.mode()}
Variance:${statistics.var()}\nStandard Deviation${statistics.std()}\nFrequency Distribution${statistics.freqDist()}`
      }

    }
  console.log(statistics.describe())

image