Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Foundation/Foundation.h>
  2. #import <math.h>
  3.  
  4. const int MAX_RAND_NUMBER = 100;
  5. const int SIZE = 50;
  6.  
  7. NSNumber * makeRandomNumber()
  8. {
  9.     return [[NSNumber alloc] initWithFloat:(random() % MAX_RAND_NUMBER) + 1];
  10. }
  11.  
  12. NSDictionary * makeDictionary(int capacity)
  13. {
  14.     NSMutableDictionary * out = [[NSMutableDictionary alloc] initWithCapacity:capacity];
  15.     srandom(time(NULL));
  16.     for (int i = 0; i < capacity; i++) {
  17.         [out setValue:makeRandomNumber() forKey:[NSString stringWithFormat:@"%i", i]];
  18.     }
  19.     return out;
  20. }
  21.  
  22.  
  23. @interface NSDictionary (Statictics)
  24.  
  25. -(float)averageArit;
  26. -(float)averageGeo;
  27. -(float)averageHarm;
  28. -(float)median;
  29. -(float)centralMoment:(unsigned int)k;
  30. -(float) variance;
  31. -(float) standardDeviation;
  32. -(float) coefficientOfVariation;
  33. -(float) skewness;
  34. -(float) kurtosis;
  35. -(float) variationsRange;
  36.  
  37. @end
  38.  
  39.  
  40. @implementation NSDictionary (Statictics)
  41.  
  42. -(float)averageArit
  43. {
  44.     float sum = 0.0;
  45.     for(NSString * item in self) {
  46.         sum += [[self valueForKey:item] floatValue];
  47.     }
  48.     if ([self count]) {
  49.         return sum/(float)[self count];
  50.     } else {
  51.         return sum;
  52.     }  
  53. }
  54.  
  55. -(float) averageGeo
  56. {
  57.     long double mul = 1;
  58.     for (NSString * item in self) {
  59.         NSNumber * val = [self valueForKey:item];
  60.         mul *= [val doubleValue];
  61.     }
  62.    
  63.     if ([self count] && mul) {
  64.         return (float)(powl(mul, 1.0/(float)[self count]));
  65.     } else {
  66.         return 0.0;
  67.     }  
  68. }
  69.  
  70. -(float) averageHarm
  71. {
  72.     long double mul = 1;
  73.     for (NSString * item in self) {
  74.         NSNumber * val = [self valueForKey:item];
  75.         mul += 1.0/[val floatValue];
  76.        
  77.     }
  78.    
  79.     if ([self count] && mul) {
  80.         return (float)[self count] / mul;
  81.     } else {
  82.         return 0;
  83.     }
  84.    
  85. }
  86.  
  87. -(float) median
  88. {
  89.     NSArray * sorted = [[self allObjects] sortedArrayUsingSelector:@selector(compare:)];
  90.     int arrSize = [sorted count];
  91.     if (arrSize%2 == 1) {
  92.         return [[sorted objectAtIndex:arrSize/2] floatValue];
  93.     } else {
  94.         return [[sorted objectAtIndex:arrSize/2] floatValue] + [[sorted objectAtIndex:arrSize/2 + 1] floatValue] / 2.0;
  95.     }
  96. }
  97.  
  98. -(float) centralMoment:(unsigned int)k
  99. {
  100.     float medianValue = [self median];
  101.     long double count = 0;
  102.     for (NSString * item in self) {
  103.         double val = [[self valueForKey:item] doubleValue];
  104.         count += powl(medianValue - val, k);
  105.     }
  106.     if ([self count]) {
  107.         return count/(float)[self count];
  108.     } else {
  109.         return 0.0;
  110.     }
  111. }
  112.  
  113. -(float) variance
  114. {
  115.     return [self centralMoment:2.0];
  116. }
  117.  
  118. -(float) standardDeviation
  119. {
  120.     return powf([self variance], 0.5);
  121. }
  122.  
  123.  
  124. -(float) coefficientOfVariation
  125. {
  126.     return [self standardDeviation] / fabs([self median]);
  127. }
  128.  
  129. -(float) skewness
  130. {
  131.     return [self centralMoment:3.0] / powf([self standardDeviation], 3.0);
  132. }
  133.  
  134. -(float) kurtosis
  135. {
  136.     return [self centralMoment:4.0] / powf([self standardDeviation], 4.0) - 3.0;
  137. }
  138.  
  139. -(float) variationsRange
  140. {
  141.     return [[self valueForKeyPath:@"@max"] floatValue] - [[self valueForKeyPath:@"@min"] floatValue];
  142. }
  143.  
  144. @end
  145.  
  146.  
  147.  
  148. int main (int argc, const char * argv[]) {
  149.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  150.  
  151.     NSDictionary * dict = [[NSDictionary alloc] initWithDictionary:makeDictionary(SIZE)];
  152.    
  153.     //NSLog(@"%@", dict);
  154.     float averageArithm = [dict averageArit];
  155.     NSLog(@"Aritmeticky prumer: %f", averageArithm);
  156.     NSLog(@"Geometricky prumer: %f", [dict averageGeo]);
  157.     NSLog(@"Harmonicky prumer: %f", [dict averageHarm]);
  158.     NSLog(@"Median: %f", [dict median]);
  159.     NSLog(@"Rozptyl: %f", [dict variance]);
  160.     NSLog(@"Smerodatna odchylka: %f", [dict standardDeviation]);
  161.     NSLog(@"Variacni koeficient: %f", [dict coefficientOfVariation]);
  162.     NSLog(@"Koeficient sikmosti: %f", [dict skewness]);
  163.     NSLog(@"Koeficient spicatosti: %f", [dict kurtosis]);
  164.     //NSLog(@"Variacni rozpeti: %f", [dict variationsRange]);
  165.    
  166.     [pool drain];
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement