double Scale_factor(std::vector<double> X, std::vector<double> Y) { double sigmax = standard_deviation(X); //% 0.0834 ; %mean(std(X)); double sigmay = standard_deviation(Y);// %0.0648 ; %mean(std(Y)); return ( (sigmay*sigmay) /(sigmax*sigmax) ); }
// Calculates the correlation coefficient for two lists of numbers in an array // in O(5n) double correlation_coefficient(double a[], double b[], int n) { double arithmetic_meanA = arithmetic_mean(a, n); // O(n) double arithmetic_meanB = arithmetic_mean(b, n); // O(n) double stddevA = standard_deviation(a, n); // O(2n) double stddevB = standard_deviation(b, n); // O(2n) double total = 0; for (int i = 0; i < n; ++i) { // O(n) total += ((a[i] - arithmetic_meanA) / stddevA) * ((b[i] - arithmetic_meanB) / stddevB); } return total / (double) (n - 1); }
// 標準化偏回帰係数を求める void standaraized_partial_regression(double *inputX1, double *inputX2, double *inputY, int input_size, double *output[2]){ double *pr[2], s1, s2, sY; s1 = standard_deviation(inputX1, input_size); s2 = standard_deviation(inputX2, input_size); sY = standard_deviation(inputY, input_size); partial_regression(inputX1, inputX2, inputY, input_size, pr); *pr[0] = *pr[0] * s1 / sY; *pr[1] = *pr[1] * s2 / sY; output[0] = pr[0]; output[1] = pr[1]; }
void evolutionary_algorithm::write_data() { individual_vector::iterator it = std::max_element(population.begin(), population.end(), &compare_individuals); double max_fitness = (*it)->fitness(); double avg = average_fitness(population); double std = standard_deviation(population); csv.write(3, avg, std, max_fitness); if(debug_flag) { //print_individuals(population); for(std::size_t i = 0; i < population.size(); ++i) { sstream ss; ss << "id: " << i << "\tphenotype:\t"; for(std::size_t j = 0; j < population[i]->phenotype()->size(); ++j) { ss << " " << (*population[i]->phenotype())[j]; } ss << "\tgenotype:"; for(std::size_t j = 0; j < population[i]->genotype()->size(); ++j) { if(j % development->bit_chunk_size()==0) ss << " "; ss << (*population[i]->genotype())[j]; } debug.write(1, ss.str().c_str()); } } }
Value_type<I> correlation_coefficient(I firstA, I lastA, I firstB, I lastB, T id) { typedef Value_type<I> R; R arithmetic_meanA = arithmetic_mean(firstA, lastA, id); R arithmetic_meanB = arithmetic_mean(firstB, lastB, id); R stddevA = standard_deviation(firstA, lastA, id); R stddevB = standard_deviation(firstB, lastB, id); R count = R(id); R total = R(id); while (firstA != lastA) { total += ((*firstA - arithmetic_meanA) / stddevA) * ((*firstB - arithmetic_meanB) / stddevB); ++firstA; ++firstB; ++count; } return total / (count - R(1)); }
int main(void) { int c, d, swap; int i = 0; int counter = 0; int numbers[100]; char line[100]; FILE *file; file = fopen("data.txt", "r"); while(fgets(line, sizeof line, file)!=NULL) { numbers[i]=atoi(line); /* convert string to int*/ i++; counter ++; } fclose(file); // using bubble sort algorithm for(c = 0; c < (counter - 1); c++) { for(d = 0; d < (counter - c - 1); d++) { if( numbers[d] > numbers[d+1]) { swap = numbers[d]; numbers[d] = numbers[d+1]; numbers[d+1] = swap; } } } // print sorted array printf("Sorted list is ascending order: \n"); for(c = 0; c < counter; c++) { printf("%d\n", numbers[c]); } printf("Maximum number in list: %d\n", max(numbers, counter)); printf("Minimum number in list: %d\n", min(numbers, counter)); printf("Range between numbers in list: %d\n", range(numbers, counter)); printf("Average value in list: %.2f\n", mean(numbers, counter)); printf("Median is : %.2f\n", nquartile(numbers, counter , 2)); printf("First quartile is : %.2f\n", nquartile(numbers, counter , 1)); printf("Third quartile is : %.2f\n", nquartile(numbers, counter , 3)); printf("Standard Deviation is : %.2f\n", standard_deviation(numbers, counter)); z_test(numbers, counter); return 0; }
// 重回帰方程式を求める void multiple_regression(double *inputX1, double *inputX2, double *inputY, int input_size, double *output[3]){ double a, b1, b2, co1Y, co2Y, co12, av1, av2, avY, s1, s2, sY; co1Y = coefficient(inputX1, inputY, input_size); co2Y = coefficient(inputX2, inputY, input_size); co12 = coefficient(inputX1, inputX2, input_size); av1 = average(inputX1, input_size); av2 = average(inputX2, input_size); avY = average(inputY, input_size); s1 = standard_deviation(inputX1, input_size); s2 = standard_deviation(inputX2, input_size); sY = standard_deviation(inputY, input_size); b1 = ((co1Y - co2Y * co12) * sY) / ((1 - pow(co12, 2)) * s1); b2 = ((co2Y - co1Y * co12) * sY) / ((1 - pow(co12, 2)) * s2); a = avY - b1 * av1 - b2 * av2; output[0] = &a; output[1] = &b1; output[2] = &b2; }
int main() { int n, i; float data[100]; printf("Enter number of datas( should be less than 100): "); scanf("%d",&n); printf("Enter elements: "); for(i=0; i<n; ++i) scanf("%f",&data[i]); printf("\n"); printf("Standard Deviation = %.2f", standard_deviation(data,n)); return 0; }
::std::pair<ValueT,ValueT> confidence_interval_mean(SampleT const& sample, ValueT level) { ValueT n = count(sample); students_t<ValueT> dist(n-1); ValueT t = dist.quantile((1+level)/ValueT(2)); ValueT s = standard_deviation(sample); ValueT m = mean(sample); ValueT h = t*s/::std::sqrt(n); return ::std::make_pair(m-h, m+h); }
float find_location(Map map, Particle particles[]) { float readings[72] = { 32, 32, 32, 32, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 15, 15, 15, 15, 15, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 15, 15, 15, 15, 15, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 15, 15, 15, 15, 15, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 }; int i = 54, ii; int *histogram = calloc(360, sizeof(int)); robot_position = 270; while (variance(particles, NUM_PARTICLES) > POSITION_PROBABILITY_THRESHOLD * POSITION_PROBABILITY_THRESHOLD) { move_particles(particles); robot_position += TRAVEL_DISTANCE; if (robot_position > 360) robot_position -= 360; monte_carlo(map, particles, NUM_PARTICLES, readings[i % 72]); qsort(particles, NUM_PARTICLES, sizeof(Particle), compare_particles); if (i == 1 || i == 10) { printf("at time %d:\n", i); for (ii = 0; ii < NUM_PARTICLES; ii++) { histogram[(int)particles[ii].position]++; } for (ii = 0; ii < 360; ii++) { printf("%d, %d\n", ii, histogram[ii]); } } printf("at time %d std deviation: %.2f\n", i, standard_deviation(particles, NUM_PARTICLES)); i++; if (i > 71) i = 0; } printf("Location %.2f in %d turns\n", mean_position(particles, NUM_PARTICLES), i); printf("Actual location %d\n", (int)robot_position); free(histogram); return mean_position(particles, NUM_PARTICLES); }
int main(int argc, char *argv[]) { int N = 100; double *timings = malloc(sizeof(double) * N); for (int t = 0; t < N; t++) { int begin = clock(); int result = 0; for (int i = 0; i < 1000000; i++) result += sqrt(i); int end = clock(); double time_elapsed = (double)(end - begin) / CLOCKS_PER_SEC * 1000.0 * 1000.0; timings[t] = time_elapsed; printf("%f ns\n", time_elapsed); } struct Stats stats = standard_deviation(N, timings); int one_std = 0; int two_std = 0; for (int i = 0; i < N; i++) { if (stats.mean - stats.std < timings[i] && timings[i] < stats.mean + stats.std) { one_std++; } if (stats.mean - 2.0 * stats.std < timings[i] && timings[i] < stats.mean + 2.0 * stats.std) { two_std++; } } double one = (double)one_std / N * 100.0; double two = (double)two_std / N * 100.0; printf( "\n\nMax:%.2f\n" "Min:%.2f\n" "Mean:%.2f\n" "Standard deviation:%.2f\n" "Within one std:%.2f%%\n" "Within two std:%.2f%%\n", stats.max, stats.min, stats.mean, stats.std, one, two); }
double Scale(std::vector<double> X, std::vector<double> Y) { double sigmay = standard_deviation(Y); //% 0.0834 ; %mean(std(X)); double sigmax = 0.01; int N = X.size(); double sumxx = 0; double sumyy = 0; double sumxy = 0; for(int i = 0; i< N; i++ ) sumxx = sumxx + (X[i]*X[i]); double sxx = std::pow(sigmay,2)*sumxx; for(int i=0; i<N; i++) sumyy = sumyy + (Y[i]*Y[i]); double syy = std::pow(sigmax,2)*sumyy; for (int i = 0; i< N; i++) sumxy = sumxy +(X[i]*Y[i]); double sxy = sigmay*sigmax*sumxy; double temp_sign; if(sxy == 0) temp_sign = 0; else temp_sign = (sxy>0 ? 1 : -1); double scale = (sxx-syy+temp_sign*std::sqrt(std::pow((sxx-syy),2)+4*sxy*sxy))/(2*(1/sigmax)*sigmay*sxy); return scale; }
void z_test(int array[], int arraySize) { int temp = 0; /* counter*/ int z_testArray[arraySize]; float mymean = mean(array, arraySize); float mystdev = standard_deviation(array, arraySize); int i, j ; for(i = 0; i < arraySize; i++) { if((fabs(array[i] - mymean) / mystdev) > 3) { temp += 1; } } if( temp == 0) { printf("There are no outliers"); } else{ for(i = 0; i < arraySize; i++) { if((fabs(array[i] - mymean) / mystdev) > 3) { for( j = 0; j < temp; j++) { z_testArray[j] = array[i]; } } } printf("Outliers: "); for( j = 0; j < temp; j++) { printf("%2d ,", z_testArray[j]); } } }
int main() { std::cout.precision(6); // Create test array with values for "Simple Test Case" case double array1[] = {10, 20, 30, 40, 50}; int size1 = 5; // Display test values to screen std::cout << "For this array of numbers {"; for (int i = 0; i < (size1-1); i++) { std::cout << array1[i] << ", "; } std::cout << array1[(size1-1)]; std::cout << "}" << std::endl; // Calculate standard deviation using "Simple Test Case" array std::cout << std::fixed << "The Standard Deviation: " << standard_deviation(array1, size1) << std::endl; // Create test array with values for "Small Number Edge Case" double array2[] = {0.00004, 1.000003, 4.0000001, 0.19936, 0.00000000005, 0.0, 0.20, 0.00000000000000000000000001}; int size2 = 8; // Display test values to screen std::cout << "For this array of numbers {"; for (int i = 0; i < (size2-1); i++) { std::cout << array2[i] << ", "; } std::cout << array2[(size2-1)]; std::cout << "}" << std::endl; // Calculate standard deviation using "Small Number Edge Case" array std::cout << std::fixed << "The Standard Deviation: " << standard_deviation(array2, size2) << std::endl; // Create test array with values for "Large Number Edge Case" double array3[] = {20000000, 50000000, 101345000000000, 202567543, 9007199254740992}; int size3 = 5; // Display test values to screen std::cout << "For this array of numbers {"; for (int i = 0; i < (size3-1); i++) { std::cout << std::fixed << array3[i] << ", "; } std::cout << array3[(size3-1)]; std::cout << "}" << std::endl; // Calculate standard deviation using "Large Number Edge Case" array std::cout << std::fixed << "The Standard Deviation: " << standard_deviation(array3, size3) << std::endl; // Create test array with values for "Over Large Number Edge Case" // Commented out due to compliler error //double array4[] = {20000000, 50000000, 101345000000000, 202567543, 9007199254740992, 9007199254740993}; //int size4 = 6; // Display test values to screen //std::cout << "For this array of numbers {"; //for (int i = 0; i < (size4-1); i++) //{ // std::cout << array3[i] << ", "; //} //std::cout << array3[(size4-1)]; //std::cout << "}" << std::endl; // Calculate standard deviation using "Over Large Number Edge Case" array //std::cout << std::fixed << "The Standard Deviation: " << standard_deviation(array4, size4) << std::endl; }
// 決定係数を求める double determination(double *input1, double *input2, int input_size){ return pow(covariance(input1, input2, input_size) / (standard_deviation(input1, input_size) * standard_deviation(input2, input_size)), 2); }
// 相関係数を求める double coefficient(double *input1, double *input2, int input_size){ return (covariance(input1, input2, input_size)) / (standard_deviation(input1, input_size) * standard_deviation(input2, input_size)); }
static void constraints() { typedef typename Distribution::value_type value_type; const Distribution& dist = DistributionConcept<Distribution>::get_object(); value_type x = 0; // The result values are ignored in all these checks. check_result<value_type>(cdf(dist, x)); check_result<value_type>(cdf(complement(dist, x))); check_result<value_type>(pdf(dist, x)); check_result<value_type>(quantile(dist, x)); check_result<value_type>(quantile(complement(dist, x))); check_result<value_type>(mean(dist)); check_result<value_type>(mode(dist)); check_result<value_type>(standard_deviation(dist)); check_result<value_type>(variance(dist)); check_result<value_type>(hazard(dist, x)); check_result<value_type>(chf(dist, x)); check_result<value_type>(coefficient_of_variation(dist)); check_result<value_type>(skewness(dist)); check_result<value_type>(kurtosis(dist)); check_result<value_type>(kurtosis_excess(dist)); check_result<value_type>(median(dist)); // // we can't actually test that at std::pair is returned from these // because that would mean including some std lib headers.... // range(dist); support(dist); check_result<value_type>(cdf(dist, f)); check_result<value_type>(cdf(complement(dist, f))); check_result<value_type>(pdf(dist, f)); check_result<value_type>(quantile(dist, f)); check_result<value_type>(quantile(complement(dist, f))); check_result<value_type>(hazard(dist, f)); check_result<value_type>(chf(dist, f)); check_result<value_type>(cdf(dist, d)); check_result<value_type>(cdf(complement(dist, d))); check_result<value_type>(pdf(dist, d)); check_result<value_type>(quantile(dist, d)); check_result<value_type>(quantile(complement(dist, d))); check_result<value_type>(hazard(dist, d)); check_result<value_type>(chf(dist, d)); check_result<value_type>(cdf(dist, l)); check_result<value_type>(cdf(complement(dist, l))); check_result<value_type>(pdf(dist, l)); check_result<value_type>(quantile(dist, l)); check_result<value_type>(quantile(complement(dist, l))); check_result<value_type>(hazard(dist, l)); check_result<value_type>(chf(dist, l)); check_result<value_type>(cdf(dist, i)); check_result<value_type>(cdf(complement(dist, i))); check_result<value_type>(pdf(dist, i)); check_result<value_type>(quantile(dist, i)); check_result<value_type>(quantile(complement(dist, i))); check_result<value_type>(hazard(dist, i)); check_result<value_type>(chf(dist, i)); unsigned long li = 1; check_result<value_type>(cdf(dist, li)); check_result<value_type>(cdf(complement(dist, li))); check_result<value_type>(pdf(dist, li)); check_result<value_type>(quantile(dist, li)); check_result<value_type>(quantile(complement(dist, li))); check_result<value_type>(hazard(dist, li)); check_result<value_type>(chf(dist, li)); }
int main() { int i,c,v,flag,v1,v2,v3,v4,v5,v6,graphcount=0,r,flag2,arr,d,s; char** tokens; char* token; char** edge; char buffer[2500]; double at1[10],at2[10],at3[10],at4[10],at5[10]; double st1[10],st2[10],st3[10],st4[10],st5[10]; arr=0; while(fgets(buffer,sizeof buffer,stdin)!=NULL) { flag=0; flag2=0; if(feof(stdin)) break; switch(buffer[0]) { case 'V': if(graphcount>1) { for(i=0;i<=graphcount;i++) { free(gr[i]); free(grk[i]); } free(gr); free(grk); } tokens=split(buffer,' '); v3=v4=v5=v6=v=atoi(*(tokens+1)); if(v<0){fprintf(stderr,"Error: Number of total Vertices can not be negative\n");fflush(stderr);graphcount=0;} else if(v==0){fprintf(stderr,"Error: There will be no graph with 0 vertices\n");fflush(stderr);graphcount=0;} else { graphcount=init(v);} free(tokens); continue; case 'E': v1=v2=0; if(buffer[3]=='}'){flag=1;} else{tokens = split(buffer, ' '); if(tokens){ token=*(tokens+1); for(i=0;i<strlen(token);i++) { if(token[i]=='-') { fprintf(stderr,"Error: Negetive vertex is not possible for edge\n"); fflush(stderr); flag=1;free(tokens);free(token); break; } if(token[i]=='{'||token[i]=='<') { token[i]='0'; } } if(flag!=1) { token[i-2]='\0'; tokens=split(token,'>'); if(tokens) { for (i = 0; *(tokens + i); i++) { token=*(tokens + i); if(token[0]==',')token[0]='0'; edge=split(token,','); if(edge) { v1=atoi(*(edge+0)); v2=atoi(*(edge+1)); if(v1>(v-1)||v2>(v-1)) { fprintf(stderr,"Error: The vertex you have given for edge does not exist\n"); fflush(stderr); flag2=1; break; } else graph(v1,v2); } free(token); free(edge); } } free(tokens); } }} if(flag2!=1 && flag!=1) { int counter; double time1[10],time2[10],time3[10],time4[10],time5[10]; for(counter=0;counter<10;counter++) { /***************************************************Initializing Global array******************************/ node=(int*)malloc(v*sizeof(int));//cnf-sat for(i=0;i<v;i++)node[i]=INF; res1=(int*)malloc(v*sizeof(int));//approx-vc1 for(i=0;i<v;i++)res1[i]=INF; res2=(int*)malloc(v*sizeof(int));//refine 1 for(i=0;i<v;i++)res2[i]=INF; res3=(int*)malloc(v*sizeof(int));//approx vc 2 for(i=0;i<v;i++)res3[i]=INF; res4=(int*)malloc(v*sizeof(int));//refine 2 for(i=0;i<v;i++)res4[i]=INF; /************************************************create and join threads************************************/ pthread_create(&tid1, NULL,cnf, &v); s=pthread_getcpuclockid(tid1, &cid1); if (s != 0){printf("Error creating cid1");fflush(stdout);} pthread_create(&tid2, NULL,apv1, &v3); s=pthread_getcpuclockid(tid2, &cid2); if (s != 0){printf("Error creating cid2");fflush(stdout);} pthread_create(&tid3, NULL,re1, &v4); s=pthread_getcpuclockid(tid3, &cid3); if (s != 0){printf("Error creating cid3");fflush(stdout);} pthread_create(&tid4, NULL,apv2, &v5); s=pthread_getcpuclockid(tid4, &cid4); if (s != 0){printf("Error creating cid4");fflush(stdout);} pthread_create(&tid5, NULL,re2, &v6); s=pthread_getcpuclockid(tid5, &cid5); if (s != 0){printf("Error creating cid5");fflush(stdout);} /*********************************getting time *****************************************/ sleep(2); pthread_join(tid1,NULL); pthread_join(tid2,NULL); pthread_join(tid3,NULL); pthread_join(tid4,NULL); pthread_join(tid5,NULL); /********************************************Printing result**************************************************/ free(node); free(res1); free(res2); free(res3); free(res4); time1[counter]=t1; time2[counter]=t2; time3[counter]=t3; time4[counter]=t4; time5[counter]=t5; } /**********finding average for 10 runs****/ at1[arr]=average(time1,10); at2[arr]=average(time2,10); at3[arr]=average(time3,10); at4[arr]=average(time4,10); at5[arr]=average(time5,10); arr++; fprintf(stdout,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",v,average(at1,arr),average(at2,arr),average(at3,arr),average(at4,arr),average(at5,arr),standard_deviation(at1,arr),standard_deviation(at2,arr),standard_deviation(at3,arr),standard_deviation(at4,arr),standard_deviation(at5,arr)); fflush(stdout); } continue; } } return 0; }
void print_results(struct root_struct *ptr) { int x, hashes, gt100, bucket_reuse, bmax, bmin, average_count; float std_dev; unsigned int lasthash; DEBUG("Print results"); hashes = gt100 = bucket_reuse = bmax = 0; bmin = 999999; if(ptr == NULL) { printf("No packets!\n"); return; } average_count = average(ptr); median = get_median(ptr); /* if(!(flags & (SEQ_NUMS | IPIDS | SRC_PORTS | DST_PORTS | SRC_HOSTS))) { average_count = (average_count + 2) * 100; } if(flags & (SRC_HOSTS | SRC_PORTS | DST_PORTS)) { average_count = (average_count + 2) * 50; } if(flags & SEQ_NUMS) { average_count = (average_count * 1000); } if(flags & IPIDS) { average_count = average_count * 2; } */ std_dev=standard_deviation(root); lasthash = ptr -> hash_value; hashes ++; while(ptr) { bucket_reuse++; if(lasthash != ptr -> hash_value) { hashes ++; lasthash = ptr -> hash_value; if(bmax < bucket_reuse) bmax = bucket_reuse; if(bmin > bucket_reuse) bmin = bucket_reuse; bucket_reuse = 0; } // if(abs(ptr -> count - median) > ((std_dev * anomalosity) )) { gt100 ++; if(!(flags & (SEQ_NUMS | IPIDS | SRC_PORTS | DST_PORTS | SRC_HOSTS | CHECKSUM))) { if(ptr->count > anomalosity){ printf("+-------------------------------------------"\ "--------------------------------------------+\n"); for(x=0;x!=significant_bytes;x++) { printf("%s%x ", (ptr->key[x] < 16 ? "0" : ""), (unsigned char)(ptr->key[x])); } printf("\n"); for(x=0;x!=significant_bytes;x++) { printf("%c ",(isprint(ptr->key[x])? ptr->key[x] : '.')); } printf(" -#: %d\n", ptr->count); } } else { printf("%d ", ptr->count); } if((flags & DST_PORTS) | (flags & SRC_PORTS) | (flags &IPIDS) | (flags & CHECKSUM)) { unsigned short int port; port = ntohs(((unsigned short int *)ptr->key)[0]); printf("%u\n",port); } if(flags & SEQ_NUMS) { unsigned long int seq; seq = ntohl(((unsigned long int *)ptr->key)[0]); printf("%u\n",seq); } if(flags & SRC_HOSTS) { unsigned char octet; int x; for (x=0; x!= 4; x++) { octet = *((unsigned char *)(ptr->key)+x); printf("%u%c", octet, (x == 3 ? ' ' : '.')); } printf("\n"); } } ptr = ptr->next_root; } if(!(flags & QUIET)) { printf("Hashes: %d\nHits: %d\nMisses: %d\nSignatures: "\ "%d\nHits > 100: %d\nPackets: %u\n"\ "Efficiency: Max Reuse: %d Min Reuse: %d\n", hashes, hash_hits, hash_misses, sigs, gt100, packets, bmax, bmin); printf("Standard Deviation: %f Average: %f Median: %f\n",standard_deviation(root),average(root),get_median(root)); } }