SlideShare a Scribd company logo
statistics.cpp
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include "statistics.hpp"
#include "tools.hpp"
using std::endl;
Histogram::Histogram(std::vector<double> &v, double t_bin_size)
{
bin_size = t_bin_size;
//Groesstes und kleinstes Element finden
min = *min_element(v.begin(), v.end());
double max = *max_element(v.begin(), v.end());
int num_bins = int((max - min) / bin_size + 1.0);
data.resize(num_bins, 0);
int position;
for (unsigned int i = 0; i < v.size(); i++)
{
position = int((v[i] - min) / bin_size);
data[position] += 1;
}
}
Histogram::~Histogram() {}
/*-----------------------------------------------*/
void Statistics::Init(int* t_grid_size, int* t_bin_size,
int* t_num_iterations, int* t_num_customers, int* t_num_salesman) {
grid_size = t_grid_size;
log_field.resize((*grid_size) * (*grid_size), 0);
bin_size = t_bin_size;
num_iterations = t_num_iterations;
num_customers = t_num_customers;
num_salesman = t_num_salesman;
log_p.clear(); log_p.resize(*num_salesman);
log_earn.clear(); log_earn.resize(*num_salesman); last_earn.clear();
last_earn.resize(*num_salesman, 0);
log_sales.clear(); log_sales.resize(*num_salesman); last_sales.clear();
last_sales.resize(*num_salesman, 0);
log_visits.clear(); log_visits.resize(*num_salesman);
Seite 1
statistics.cpp
last_visits.clear(); last_visits.resize(*num_salesman, 0);
path = "./output/";
prefix = "out";
}
void Statistics::LogField(int x, int y) {
log_field[x + y * (*grid_size)]++;
}
void Statistics::LogSalesman(int salesm_id, double earnings, double p, int
sales, int visits) {
log_p[salesm_id].push_back(p);
log_earn[salesm_id].push_back(earnings - last_earn[salesm_id]);
log_sales[salesm_id].push_back(sales - last_sales[salesm_id]);
log_visits[salesm_id].push_back(visits - last_visits[salesm_id]);
last_earn[salesm_id] = earnings;
last_sales[salesm_id] = sales;
last_visits[salesm_id] = visits;
}
void Statistics::WriteLogSalesman(std::string name) {
if (name == "") name = prefix;
Write2dVector(log_earn, path + name + "_earn.dat");
Write2dVector(log_p, path + name + "_p.dat");
Write2dVector(log_sales, path + name + "_sales.dat");
Write2dVector(log_visits, path + name + "_visits.dat");
}
void Statistics::Write2dVector(std::vector<std::vector<double> > &v, std::string
filename) {
std::ofstream write(filename.c_str());
for (unsigned int i = 0; i < v[0].size(); i++)
{
write << (i + 1) * (*bin_size) - 1;
for (unsigned int j = 0; j < v.size(); j++) {
write << "t" << v[j][i];
}
write << endl;
}
}
void Statistics::DivideData(std::vector<double> &v, double divisor) {
for (unsigned int i = 0; i < v.size(); i++) {
v[i] = v[i] / divisor;
}
}
Seite 2
statistics.cpp
void Statistics::Smoothing(std::vector<double> &v) {
int N = v.size();
std::vector<double> tmp(N, 0);
for (int i = 0; i < N; i++) {
tmp[i] += 0.4 * v[i];
if (i > 0) tmp[i] += 0.3 * v[i-1];
else tmp[i] += 0.3 * v[i];
if (i < N-1) tmp[i] += 0.3 * v[i+1];
else tmp[i] += 0.3 * v[i];
}
v = tmp;
}
void Statistics::MSmoothing(std::vector<double> &v, int n) {
for (int i = 0; i < n; i++) {
Smoothing(v);
}
}
double Statistics::Midpoint(std::vector<double> &v) {
double sum = 0; unsigned int size = v.size();
for (unsigned int i = 0; i < size; i++) {
sum = sum + v[i];
}
return sum/size;
}
double Statistics::Variance(std::vector<double> &v) {
double midpoint = Midpoint(v);
double sum = 0; unsigned int size = v.size();
for (unsigned int i = 0; i < size; i++) {
sum = sum + (v[i] - midpoint) * (v[i] - midpoint);
}
return sum/(size - 1);
}
void Statistics::WriteHistogram(std::vector<double> &v, double bin_size,
std::string filename) {
filename = path + prefix + "_" + filename;
std::ofstream write(filename.c_str());
Histogram histo(v, bin_size);
Seite 3
statistics.cpp
for (unsigned int i = 0; i < histo.data.size(); i++)
{
write << (i*bin_size + histo.min) << "t" << histo.data[i] <<
endl;
}
}
void Statistics::WriteFields(std::string filename) {
filename = path + prefix + "_" + filename;
std::ofstream write(filename.c_str());
int fx, fy;
for (unsigned int i = 0; i < log_field.size(); i++)
{
fx = i % (*grid_size);
fy = (i - fx) / (*grid_size);
write << fx << "t" << fy << "t" << log_field[i] << endl;
}
}
void Statistics::WriteFieldsDistance(std::string filename, int x, int y) {
filename = path + prefix + "_" + filename;
std::ofstream write(filename.c_str());
int fx, fy;
for (unsigned int i = 0; i < log_field.size(); i++)
{
fx = i % (*grid_size);
fy = (i - fx) / (*grid_size);
write << Distance(x, y, fx, fy, *grid_size) << "t" <<
log_field[i] << endl;
}
}
Seite 4

More Related Content

What's hot (20)

PPT
COW
永泉 韩
 
DOCX
ggtimeseries-->ggplot2 extensions
Dr. Volkan OBAN
 
DOCX
Wap in c to draw a line using DDA algorithm
Kapil Pandit
 
PPTX
Gauss in java
baxter89
 
TXT
Dvst
hahaa225
 
DOCX
Computer graphics programs in c++
Ankit Kumar
 
PPT
computer graphics practicals
Manoj Chauhan
 
PDF
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
The Statistical and Applied Mathematical Sciences Institute
 
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
PPT
C questions
mohamed sikander
 
DOCX
Doubly linklist
ilsamaryum
 
PDF
Coding
Prasanta Paul
 
PDF
Currying in Swift
Maxim Zaks
 
DOC
C Programming :- An Example
Atit Gaonkar
 
DOCX
Conversion of data types in java
One97 Communications Limited
 
DOCX
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
DOCX
Class array
nky92
 
ggtimeseries-->ggplot2 extensions
Dr. Volkan OBAN
 
Wap in c to draw a line using DDA algorithm
Kapil Pandit
 
Gauss in java
baxter89
 
Dvst
hahaa225
 
Computer graphics programs in c++
Ankit Kumar
 
computer graphics practicals
Manoj Chauhan
 
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
The Statistical and Applied Mathematical Sciences Institute
 
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
C questions
mohamed sikander
 
Doubly linklist
ilsamaryum
 
Currying in Swift
Maxim Zaks
 
C Programming :- An Example
Atit Gaonkar
 
Conversion of data types in java
One97 Communications Limited
 
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
Class array
nky92
 

Viewers also liked (13)

PDF
Structure-odor relations: a modern perspective
Vorname Nachname
 
PDF
Statstockprog
Vorname Nachname
 
PDF
Dpsm simu.hpp
Vorname Nachname
 
PDF
Electron transport in one dimensional nanosystems
Vorname Nachname
 
PDF
Smell in real noses: how the environment changes vibrations
Vorname Nachname
 
PDF
Dynamical symmetry breaking in vibration-assisted transport through nanostruc...
Vorname Nachname
 
PDF
Tools.cpp
Vorname Nachname
 
PDF
Main.cpp
Vorname Nachname
 
PDF
Statistics.hpp
Vorname Nachname
 
PDF
Could humans recognize odor by phonon assisted tunneling
Vorname Nachname
 
PDF
Tools.hpp
Vorname Nachname
 
PDF
Dpsm simu.cpp
Vorname Nachname
 
PDF
Econophysics
Vorname Nachname
 
Structure-odor relations: a modern perspective
Vorname Nachname
 
Statstockprog
Vorname Nachname
 
Dpsm simu.hpp
Vorname Nachname
 
Electron transport in one dimensional nanosystems
Vorname Nachname
 
Smell in real noses: how the environment changes vibrations
Vorname Nachname
 
Dynamical symmetry breaking in vibration-assisted transport through nanostruc...
Vorname Nachname
 
Tools.cpp
Vorname Nachname
 
Statistics.hpp
Vorname Nachname
 
Could humans recognize odor by phonon assisted tunneling
Vorname Nachname
 
Tools.hpp
Vorname Nachname
 
Dpsm simu.cpp
Vorname Nachname
 
Econophysics
Vorname Nachname
 
Ad

Similar to Statistics.cpp (6)

PDF
Histogram
'Iry Lee
 
PDF
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
ebrahimbadushata00
 
DOCX
#include PA3Header.h#include stdafx.h Function r.docx
mayank272369
 
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
PDF
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
PDF
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
fashiongallery1
 
Histogram
'Iry Lee
 
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
ebrahimbadushata00
 
#include PA3Header.h#include stdafx.h Function r.docx
mayank272369
 
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
fashiongallery1
 
Ad

More from Vorname Nachname (12)

PDF
Leni souza
Vorname Nachname
 
PDF
Alien life forms
Vorname Nachname
 
PDF
Spaceengine2
Vorname Nachname
 
PDF
Structural Language
Vorname Nachname
 
PDF
Language
Vorname Nachname
 
PDF
Spaceengine2
Vorname Nachname
 
PDF
Spaceengine
Vorname Nachname
 
PDF
Topology and Electrostatics
Vorname Nachname
 
PDF
calculation of currents in nanowires
Vorname Nachname
 
PDF
Summerpoject 2005
Vorname Nachname
 
PDF
Aspelmeyer
Vorname Nachname
 
PDF
Arndt matter wave interferometry
Vorname Nachname
 
Leni souza
Vorname Nachname
 
Alien life forms
Vorname Nachname
 
Spaceengine2
Vorname Nachname
 
Structural Language
Vorname Nachname
 
Spaceengine2
Vorname Nachname
 
Spaceengine
Vorname Nachname
 
Topology and Electrostatics
Vorname Nachname
 
calculation of currents in nanowires
Vorname Nachname
 
Summerpoject 2005
Vorname Nachname
 
Aspelmeyer
Vorname Nachname
 
Arndt matter wave interferometry
Vorname Nachname
 

Recently uploaded (20)

PDF
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
DOCX
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
PPTX
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
PDF
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
PPT
Restriction digestion of DNA for students of undergraduate and post graduate ...
DrMukeshRameshPimpli
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PPTX
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
PDF
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
DOCX
Analytical methods in CleaningValidation.docx
Markus Janssen
 
PPTX
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
PDF
GUGC Research Overview (December 2024)
Ghent University Global Campus
 
PDF
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
PDF
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
PDF
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
PPTX
Renewable Energy Resources - Introduction
BhajneetSingh1
 
PDF
EXploring Nanobiotechnology: Bridging Nanoscience and Biology for real world ...
Aamena3
 
PPTX
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PPTX
Congestive Heart failure for pharmacology topic hehe
oceangunnu
 
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
Critical Book Review (CBR) - "Hate Speech: Linguistic Perspectives"
Sahmiral Amri Rajagukguk
 
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
Preserving brand authenticity amid AI-driven misinformation: Sustaining consu...
Selcen Ozturkcan
 
Restriction digestion of DNA for students of undergraduate and post graduate ...
DrMukeshRameshPimpli
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
Analytical methods in CleaningValidation.docx
Markus Janssen
 
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
GUGC Research Overview (December 2024)
Ghent University Global Campus
 
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
Renewable Energy Resources - Introduction
BhajneetSingh1
 
EXploring Nanobiotechnology: Bridging Nanoscience and Biology for real world ...
Aamena3
 
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Congestive Heart failure for pharmacology topic hehe
oceangunnu
 

Statistics.cpp

  • 1. statistics.cpp #include <iostream> #include <fstream> #include <cmath> #include <vector> #include <algorithm> #include <string> #include "statistics.hpp" #include "tools.hpp" using std::endl; Histogram::Histogram(std::vector<double> &v, double t_bin_size) { bin_size = t_bin_size; //Groesstes und kleinstes Element finden min = *min_element(v.begin(), v.end()); double max = *max_element(v.begin(), v.end()); int num_bins = int((max - min) / bin_size + 1.0); data.resize(num_bins, 0); int position; for (unsigned int i = 0; i < v.size(); i++) { position = int((v[i] - min) / bin_size); data[position] += 1; } } Histogram::~Histogram() {} /*-----------------------------------------------*/ void Statistics::Init(int* t_grid_size, int* t_bin_size, int* t_num_iterations, int* t_num_customers, int* t_num_salesman) { grid_size = t_grid_size; log_field.resize((*grid_size) * (*grid_size), 0); bin_size = t_bin_size; num_iterations = t_num_iterations; num_customers = t_num_customers; num_salesman = t_num_salesman; log_p.clear(); log_p.resize(*num_salesman); log_earn.clear(); log_earn.resize(*num_salesman); last_earn.clear(); last_earn.resize(*num_salesman, 0); log_sales.clear(); log_sales.resize(*num_salesman); last_sales.clear(); last_sales.resize(*num_salesman, 0); log_visits.clear(); log_visits.resize(*num_salesman); Seite 1
  • 2. statistics.cpp last_visits.clear(); last_visits.resize(*num_salesman, 0); path = "./output/"; prefix = "out"; } void Statistics::LogField(int x, int y) { log_field[x + y * (*grid_size)]++; } void Statistics::LogSalesman(int salesm_id, double earnings, double p, int sales, int visits) { log_p[salesm_id].push_back(p); log_earn[salesm_id].push_back(earnings - last_earn[salesm_id]); log_sales[salesm_id].push_back(sales - last_sales[salesm_id]); log_visits[salesm_id].push_back(visits - last_visits[salesm_id]); last_earn[salesm_id] = earnings; last_sales[salesm_id] = sales; last_visits[salesm_id] = visits; } void Statistics::WriteLogSalesman(std::string name) { if (name == "") name = prefix; Write2dVector(log_earn, path + name + "_earn.dat"); Write2dVector(log_p, path + name + "_p.dat"); Write2dVector(log_sales, path + name + "_sales.dat"); Write2dVector(log_visits, path + name + "_visits.dat"); } void Statistics::Write2dVector(std::vector<std::vector<double> > &v, std::string filename) { std::ofstream write(filename.c_str()); for (unsigned int i = 0; i < v[0].size(); i++) { write << (i + 1) * (*bin_size) - 1; for (unsigned int j = 0; j < v.size(); j++) { write << "t" << v[j][i]; } write << endl; } } void Statistics::DivideData(std::vector<double> &v, double divisor) { for (unsigned int i = 0; i < v.size(); i++) { v[i] = v[i] / divisor; } } Seite 2
  • 3. statistics.cpp void Statistics::Smoothing(std::vector<double> &v) { int N = v.size(); std::vector<double> tmp(N, 0); for (int i = 0; i < N; i++) { tmp[i] += 0.4 * v[i]; if (i > 0) tmp[i] += 0.3 * v[i-1]; else tmp[i] += 0.3 * v[i]; if (i < N-1) tmp[i] += 0.3 * v[i+1]; else tmp[i] += 0.3 * v[i]; } v = tmp; } void Statistics::MSmoothing(std::vector<double> &v, int n) { for (int i = 0; i < n; i++) { Smoothing(v); } } double Statistics::Midpoint(std::vector<double> &v) { double sum = 0; unsigned int size = v.size(); for (unsigned int i = 0; i < size; i++) { sum = sum + v[i]; } return sum/size; } double Statistics::Variance(std::vector<double> &v) { double midpoint = Midpoint(v); double sum = 0; unsigned int size = v.size(); for (unsigned int i = 0; i < size; i++) { sum = sum + (v[i] - midpoint) * (v[i] - midpoint); } return sum/(size - 1); } void Statistics::WriteHistogram(std::vector<double> &v, double bin_size, std::string filename) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); Histogram histo(v, bin_size); Seite 3
  • 4. statistics.cpp for (unsigned int i = 0; i < histo.data.size(); i++) { write << (i*bin_size + histo.min) << "t" << histo.data[i] << endl; } } void Statistics::WriteFields(std::string filename) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); int fx, fy; for (unsigned int i = 0; i < log_field.size(); i++) { fx = i % (*grid_size); fy = (i - fx) / (*grid_size); write << fx << "t" << fy << "t" << log_field[i] << endl; } } void Statistics::WriteFieldsDistance(std::string filename, int x, int y) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); int fx, fy; for (unsigned int i = 0; i < log_field.size(); i++) { fx = i % (*grid_size); fy = (i - fx) / (*grid_size); write << Distance(x, y, fx, fy, *grid_size) << "t" << log_field[i] << endl; } } Seite 4