Advertisement
Guest User

Untitled

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