typename std::iterator_traits<iterator1>::value_type calculate_correlation_coefficient( iterator1 first_begin, iterator1 first_end, iterator2 second_begin, iterator2 second_end ) { typedef typename std::iterator_traits<iterator1>::value_type value1_t; typedef typename std::iterator_traits<iterator2>::value_type value2_t; typedef typename std::iterator_traits<iterator1>::difference_type diff1_t; typedef typename std::iterator_traits<iterator1>::difference_type diff2_t; static_assert( ( std::is_same<value1_t, value2_t>::value ), "Value type of iterators not equal" ); value1_t result = value1_t(); diff1_t length1 = first_end - first_begin; diff2_t length2 = second_end - second_begin; if ( length1 != length2 ) return result; value1_t mean1 = calculate_mean( first_begin, first_end ); value1_t mean2 = calculate_mean( second_begin, second_end ); value1_t std_dev1 = calculate_stddev( first_begin, first_end ); value2_t std_dev2 = calculate_stddev( second_begin, second_end ); result = calculate_correlation_coefficient( first_begin, first_end, mean1, std_dev1, second_begin, second_end, mean2, std_dev2 ); return result; }
void FeatureStatistics::set_d2Features(){ int nCols = this->get_featureObject()->get_nCols(); int nRows = this->get_featureObject()->get_nRows(); // memory allocation m_d2Feature = (SAMPLE**)malloc(nCols*sizeof(SAMPLE*)); for (int i = 0; i < nCols; ++i){ m_d2Feature[i] = (SAMPLE*)malloc(nRows*sizeof(SAMPLE)); } m_d2Mean = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_d2Mean[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } m_d2Var = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_d2Var[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } m_d2Std = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_d2Std[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } // calculate SAMPLE** feature = this->get_d1Feature(); m_d2Feature = calculate_delta(feature); m_d2Mean = calculate_mean(m_d2Feature); m_d2Var = calculate_var(m_d2Feature, m_d2Mean); m_d2Std = calculate_var2std(m_d2Var); }
double calculate_stddev(const std::vector<double>& values, double mean) { std::vector<double> deviations; deviations.reserve(values.size()); std::transform(values.begin(), values.end(), std::back_inserter(deviations), [=](auto v) { return std::pow(v - mean, 2.0); }); return std::sqrt(calculate_mean(deviations)); }
cv::Mat_<double> calculate_similarity_transform(const Points3d &points, int height, int width) { cv::Point3_<double> mean = calculate_mean(points); double maximum = std::numeric_limits<double>::min(); double minimum = std::numeric_limits<double>::max(); for (size_t i = 0; i < points.size(); i++) { maximum = std::max(maximum, points[i].x - mean.x); maximum = std::max(maximum, points[i].y - mean.y); maximum = std::max(maximum, points[i].z - mean.z); minimum = std::min(minimum, points[i].x - mean.x); minimum = std::min(minimum, points[i].y - mean.y); minimum = std::min(minimum, points[i].z - mean.z); } // calculate the hypotenuse of the cube defined by minimum and // maximum as this represents the widest two points. double side_length = maximum - minimum; double distance = std::sqrt( 3*side_length*side_length ); // widen the debug to this distance. minimum -= (distance - side_length)/2.0; maximum += (distance - side_length)/2.0; // calculate the similarity transform double target_max = 0.98*std::min(height, width); double target_min = 0.02*std::min(height, width); double a = ( target_max - target_min ) / (maximum - minimum); double b = target_max - a*maximum; cv::Mat_<double> translation = cv::Mat_<double>::eye(4,4); translation(0,3) = -mean.x; translation(1,3) = -mean.y; translation(2,3) = -mean.z; cv::Mat_<double> scaling = cv::Mat_<double>::eye(4,4); scaling(0,0) = a; scaling(1,1) = a; scaling(2,2) = a; scaling(0,3) = b; scaling(1,3) = b; scaling(2,3) = b; return scaling * translation; }
/** * Returns the variance of the given array. * * NOTE: This expects a sorted array! */ float calculate_variance(float data_array[], size_t array_size, int data_position, bool print_value) { float mean = calculate_mean(data_array, array_size, data_position, false), sum = 0.0, variance = 0.0; int i = 0; for (i = 0; i < data_position; i++) sum += pow(data_array[i]-mean, 2); variance = sum / (data_position-1); if (print_value) printf("%5.2lf", variance); return variance; }
void FeatureStatistics::set_d0features(){ // memory allocation this->m_mean = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_mean[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } this->m_var = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_var[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } this->m_std = (SAMPLE**)malloc(m_nStatCols*sizeof(SAMPLE*)); for (int i = 0; i < m_nStatCols; ++i){ m_std[i] = (SAMPLE*)malloc(m_nStatRows*sizeof(SAMPLE)); } // calculate feature variables SAMPLE** feature = this->get_featureObject()->get_feature(); m_mean = calculate_mean(feature); m_var = calculate_var(feature, m_mean); m_std =calculate_var2std(m_var); }
int main (void) { FILE *infile = NULL, *outfile = NULL; double gpa1 = 0.0, gpa2 = 0.0, gpa3 = 0.0, gpa4 = 0.0, gpa5 = 0.0, age1 = 0.0, age2 = 0.0, age3 = 0.0, age4 = 0.0, age5 = 0.0, sum_gpa = 0.0, sum_class_standing = 0.0, sum_age = 0.0, mean_gpa = 0.0, mean_class_standing = 0.0, mean_age = 0.0, gpa1_dev = 0.0, gpa2_dev = 0.0, gpa3_dev = 0.0, gpa4_dev = 0.0, gpa5_dev = 0.0, gpa_variance = 0.0, gpa_std_dev = 0.0, min_gpa = 0.0, max_gpa = 0.0; int id1 = 0, id2 = 0, id3 = 0, id4 = 0, id5 =0, class_standing1 = 0, class_standing2 = 0, class_standing3 = 0, class_standing4 = 0, class_standing5 = 0; //Opens an input file "input.dat" for reading; infile = fopen ("input.dat", "r"); //Opens an output file "output.dat" for writing; outfile = fopen ("output.dat", "w"); //Reads five records from the input file (input.dat); You will need to use a combination of read_double ( ) and read_integer ( ) function calls here! id1 = read_integer (infile); gpa1 = read_double (infile); class_standing1 = read_integer (infile); age1 = read_double (infile); id2 = read_integer (infile); gpa2 = read_double (infile); class_standing2 = read_integer (infile); age2 = read_double (infile); id3 = read_integer (infile); gpa3 = read_double (infile); class_standing3 = read_integer (infile); age3 = read_double (infile); id4 = read_integer (infile); gpa4 = read_double (infile); class_standing4 = read_integer (infile); age4 = read_double (infile); id5 = read_integer (infile); gpa5 = read_double (infile); class_standing5 = read_integer (infile); age5 = read_double (infile); //Calculates the sum of the GPAs; sum_gpa = calculate_sum (gpa1, gpa2, gpa3, gpa4, gpa5); //Calculates the sum of the class standings; sum_class_standing = calculate_sum (class_standing1, class_standing2, class_standing3, class_standing4, class_standing5); //Calculates the sum of the ages; sum_age = calculate_sum (age1, age2, age3, age4, age5); //Calculates the mean of the GPAs, writing the result to the output file (output.dat); mean_gpa = calculate_mean (sum_gpa, 5); fprintf (outfile, "The mean GPA is %lf\n", mean_gpa); //Calculates the mean of the class standings, writing the result to the output file (output.dat); mean_class_standing = calculate_mean (sum_class_standing, 5); fprintf (outfile, "The mean class standing is %lf\n", mean_class_standing); //Calculates the mean of the ages, writing the result to the output file (output.dat); mean_age = calculate_mean (sum_age, 5); fprintf (outfile, "The mean age is %lf\n", mean_age); //Calculates the deviation of each GPA from the mean (Hint: need to call calculate_deviation ( ) 5 times) //Calculates the variance of the GPAs //Calculates the standard deviation of the GPAs, writing the result to the output file (output.dat); //Determines the min of the GPAs, writing the result to the output file (output.dat); //Determines the max of the GPAs, writing the result to the output file (output.dat); //Closes the input and output files (i.e. input.dat and output.dat) fclose (infile); fclose (outfile); return 0; }
int run_program(int argc, char **argv) { CommandLineArgument<std::string> points_pathname; Configuration cfg; cfg.width = 640; cfg.height = 480; cfg.window_title = ""; cfg.circle_radius = 2; cfg.circle_thickness = 1; cfg.circle_linetype = 8; cfg.circle_shift = 0; cfg.circle_colour = cv::Scalar(0, 0, 255); for (int i = 1; i < argc; i++) { std::string argument(argv[i]); if (argument == "--help") { print_usage(); return 0; } else if (argument == "--width") { cfg.width = get_argument<int>(&i, argc, argv); } else if (argument == "--height") { cfg.height = get_argument<int>(&i, argc, argv); } else if (argument == "--window-title") { cfg.window_title = get_argument(&i, argc, argv); } else if (argument == "--circle-radius") { cfg.circle_radius = get_argument<int>(&i, argc, argv); } else if (!assign_argument(argument, points_pathname)) { throw make_runtime_error("Unable to process argument: %s\n", argument.c_str()); } } if (!have_argument_p(points_pathname)) { print_usage(); return -1; } if (cfg.window_title == "") cfg.window_title = points_pathname->c_str(); std::vector<cv::Point3_<double> > shape3D; try { shape3D = load_points3(points_pathname->c_str()); } catch (std::exception &e) { throw make_runtime_error("Encountered error when reading file '%s': %s", points_pathname->c_str(), e.what()); } cv::Point3_<double> mean = calculate_mean(shape3D); for (size_t i = 0; i < shape3D.size(); i++) { shape3D[i] -= mean; } cv::Mat_<double> projection = cv::Mat_<double>::eye(3,4); if (cfg.height < cfg.width) { projection(0,3) = double(cfg.width - cfg.height) / 2.0; } else { projection(1,3) = double(cfg.height - cfg.width) / 2.0; } cv::Mat_<double> starting_A = calculate_similarity_transform(shape3D, cfg.height, cfg.width); cv::Mat_<double> A(starting_A.clone()); bool quit = false; while (!quit) { int key = cv::waitKey(1); switch (key) { case 27: quit = true; break; case 'z': A = A * scaling_matrix(1.1); break; case 'x': A = A * scaling_matrix(0.9); break; case 'a': A = A * rotation_about_y_axis(-0.1); break; case 'd': A = A * rotation_about_y_axis(0.1); break; case 'w': A = A * rotation_about_x_axis(-0.1); break; case 's': A = A * rotation_about_x_axis(0.1); break; case 'q': A = A * rotation_about_z_axis(0.1); break; case 'e': A = A * rotation_about_z_axis(-0.1); break; case 'r': A = starting_A.clone(); break; } display_points(cfg, projection * A, shape3D); } return 0; }
typename std::iterator_traits<iterator>::value_type calculate_stddev( iterator begin, iterator end ) { typedef typename std::iterator_traits<iterator>::value_type value_t; value_t mean = calculate_mean( begin, end ); return std::sqrt( calculate_variance( begin, end, mean ) ); }
typename std::iterator_traits<iterator>::value_type calculate_mean_stddev( iterator begin, iterator end ) { typedef typename std::iterator_traits<iterator>::value_type value_t; value_t mean = calculate_mean( begin, end ); return calculate_mean_stddev( begin, end, mean ); }