Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- const int MAX_RAND_NUMBER = 100;
- const int SIZE = 50;
- NSNumber * makeRandomNumber()
- {
- return [[NSNumber alloc] initWithFloat:(random() % MAX_RAND_NUMBER) + 1];
- }
- NSDictionary * makeDictionary(int capacity)
- {
- NSMutableDictionary * out = [[NSMutableDictionary alloc] initWithCapacity:capacity];
- srandom(time(NULL));
- for (int i = 0; i < capacity; i++) {
- [out setValue:makeRandomNumber() forKey:[NSString stringWithFormat:@"%i", i]];
- }
- return out;
- }
- float absf(float val) {
- if (val < 0) {
- return -val;
- } else {
- return val;
- }
- }
- @implementation NSDictionary (Statictics)
- -(float)averageArit
- {
- float sum = 0;
- for(NSString * item in self) {
- sum += [[self valueForKey:item] floatValue];
- }
- if ([self count]) {
- NSLog(@"%f %f %f", sum, (float)[self count], sum/(float)[self count]);
- return sum/(float)[self count];
- } else {
- return sum;
- }
- }
- -(float) averageGeo
- {
- long double mul = 1;
- for (NSString * item in self) {
- NSNumber * val = [self valueForKey:item];
- mul *= [val doubleValue];
- }
- if ([self count] && mul) {
- return (float)(powl(mul, 1.0/(float)[self count]));
- } else {
- return 0.0;
- }
- }
- -(float) averageHarm
- {
- long double mul = 1;
- for (NSString * item in self) {
- NSNumber * val = [self valueForKey:item];
- mul += 1.0/[val floatValue];
- }
- if ([self count] && mul) {
- return (float)[self count] / mul;
- } else {
- return 0;
- }
- }
- -(float) median
- {
- NSArray * sorted = [[self allObjects] sortedArrayUsingSelector:@selector(compare:)];
- int arrSize = [sorted count];
- if (arrSize%2 == 1) {
- return [[sorted objectAtIndex:arrSize/2] floatValue];
- } else {
- return [[sorted objectAtIndex:arrSize/2] floatValue] + [[sorted objectAtIndex:arrSize/2 + 1] floatValue] / 2.0;
- }
- }
- -(float) centralMoment:(unsigned int)k
- {
- float medianValue = [self median];
- long double count = 0;
- for (NSString * item in self) {
- double val = [[self valueForKey:item] doubleValue];
- count += powl(medianValue - val, k);
- }
- if ([self count]) {
- return count/(float)[self count];
- } else {
- return 0.0;
- }
- }
- -(float) variance
- {
- return [self centralMoment:2.0];
- }
- -(float) standardDeviation
- {
- return powf([self variance], 0.5);
- }
- -(float) coefficientOfVariation
- {
- return [self standardDeviation] / absf([self median]);
- }
- -(float) skewness
- {
- return [self centralMoment:3.0] / powf([self standardDeviation], 3.0);
- }
- -(float) kurtosis
- {
- return [self centralMoment:4.0] / powf([self standardDeviation], 4.0) - 3.0;
- }
- -(float) variationsRange
- {
- return [[self valueForKeyPath:@"@max"] floatValue] - [[self valueForKeyPath:@"@min"] floatValue];
- }
- @end
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSDictionary * dict = [[NSDictionary alloc] initWithDictionary:makeDictionary(SIZE)];
- //NSLog(@"%@", dict);
- float averageArit = [dict averageArit];
- NSLog(@"Aritmeticky prumer: %f", averageArit);
- NSLog(@"Geometricky prumer: %f", [dict averageGeo]);
- NSLog(@"Harmonicky prumer: %f", [dict averageHarm]);
- NSLog(@"Median: %f", [dict median]);
- NSLog(@"Rozptyl: %f", [dict variance]);
- NSLog(@"Smerodatna odchylka: %f", [dict standardDeviation]);
- NSLog(@"Variacni koeficient: %f", [dict coefficientOfVariation]);
- NSLog(@"Koeficient sikmosti: %f", [dict skewness]);
- NSLog(@"Koeficient spicatosti: %f", [dict kurtosis]);
- //NSLog(@"Variacni rozpeti: %f", [dict variationsRange]);
- [pool drain];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement