finding kurtosis for a dataset with boost - c++

I have a vector of data and I want to find the kurtosis of the data set. I wanted to do so with Boost and here is what I have so far (not compilable):
#include <boost/math/distributions.hpp>
using namespace std;
int main()
{
vector<double> a;
a.push_back(-1);
a.push_back(0);
a.push_back(1);
cout << "Kurtosis:"<< kurtosis(a) << endl;
return 0;
}
Why doesn't this work? My compiler gives me the error: "[...]\main.cpp|28|error: 'kurtosis' was not declared in this scope|"

For one you were not including the header for kurtosis:
#include <boost/accumulators/statistics/kurtosis.hpp>
Even if you did, as you see it does not work with a straight vector, what you probably want to do is use an accumulator_set and more headers as well.
Here is a minimal example using accumulator_set, this shows a two methods to solve the problem:
#include <boost/math/distributions.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/kurtosis.hpp>
#include <iostream>
#include <vector>
using namespace boost::accumulators;
int main()
{
accumulator_set<double, stats<tag::mean, tag::kurtosis > > acc;
accumulator_set<double, stats<tag::mean, tag::kurtosis > > acc2;
acc(2) ;
acc(3) ;
acc(4) ;
std::cout << mean(acc) << " " << kurtosis(acc) << std::endl ;
std::vector<double> v1 ;
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
acc2 = std::for_each( v1.begin(), v1.end(), acc2 ) ;
std::cout << mean(acc2) << " " << kurtosis(acc2) << std::endl ;
}
Here is a link to the Accumulators Framework User's Guide. This guide has some nice examples.
This previous thread found a way to use vector, although it is not straight forward at all and I could not get it work.

Related

the vector constructor doesn't change the end_file istream_iterator

i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}

viennacl with complex numbers

I'm using viennacl to solve a linear system of equations (AX = B) with the graphic card. Also, the code uses armadillo.
My system of equations have complex numbers. So the question is: Can I solve a system of equations (with complex numbers) using viennacl?
Above is an example of a working code with real numbers.
// System headers
#include <iostream>
// Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
#define ARMA_DONT_USE_BLAS
#define ARMA_DONT_USE_LAPACK
#include <armadillo>
#include <complex>
#define VIENNACL_WITH_ARMADILLO 1
// ViennaCL headers
#include "viennacl/linalg/cg.hpp"
#include "viennacl/linalg/bicgstab.hpp"
#include "viennacl/linalg/gmres.hpp"
#include "viennacl/io/matrix_market.hpp"
#include "vector-io.hpp"
//using namespace arma;
using namespace viennacl::linalg;
using namespace std;
typedef arma::mat armat;
typedef arma::vec arvec;
typedef complex<double> dcmplx;
int main(void)
{
int N = 500;
armat A(N,N);
A.randu();
arvec B(N);
B.randu();
arvec X(N);
arvec residual(N);
viennacl::matrix<double> vcl_A(N, N);
viennacl::vector<double> vcl_B(N);
viennacl::vector<double> vcl_X(N);
viennacl::vector<double> vcl_result(N);
viennacl::copy(A, vcl_A);
viennacl::copy(B, vcl_B);
viennacl::copy(X, vcl_X);
std::cout << "----- Running GMRES -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::gmres_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
}
Complex version of the code:
#include <iostream>
// Armadillo headers (disable BLAS and LAPACK to avoid linking issues)
#define ARMA_DONT_USE_BLAS
#define ARMA_DONT_USE_LAPACK
#include <armadillo>
#include <complex>
#define VIENNACL_WITH_ARMADILLO 1
// ViennaCL headers
#include "viennacl/linalg/cg.hpp"
#include "viennacl/linalg/bicgstab.hpp"
#include "viennacl/linalg/gmres.hpp"
#include "viennacl/io/matrix_market.hpp"
#include "vector-io.hpp"
//using namespace arma;
using namespace viennacl::linalg;
using namespace std;
typedef arma::cx_mat armat;
typedef arma::cx_vec arvec;
typedef complex<double> dcmplx;
int main(void)
{
int N = 500;
armat A(N,N);
A.randu();
arvec B(N);
B.randu();
arvec X(N);
arvec residual(N);
viennacl::matrix<dcmplx> vcl_A(N, N);
viennacl::vector<dcmplx> vcl_B(N);
viennacl::vector<dcmplx> vcl_X(N);
viennacl::vector<dcmplx> vcl_result(N);
viennacl::copy(A, vcl_A);
viennacl::copy(B, vcl_B);
viennacl::copy(X, vcl_X);
std::cout << "----- Running GMRES -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::gmres_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
std::cout << "----- Running BiCGStab -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::bicgstab_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
std::cout << "----- Running CG -----" << std::endl;
vcl_X = viennacl::linalg::solve(vcl_A, vcl_B, viennacl::linalg::cg_tag());
viennacl::copy(vcl_A, A);
viennacl::copy(vcl_B, B);
viennacl::copy(vcl_X, X);
residual = A * X - B;
cout << "Relative residual: " << norm(residual) / norm(B) << endl;
}
ViennaCL currently does not support complex numbers. The primary technical reason is that OpenCL does not natively provide support for complex numbers. While emulating complex arithmetic via real arithmetic is certainly possible, we were reluctant to go down this path and (wrongly?) hoped that a standardization for complex will come soon.

Splitting string in specific format which was created from int vector

I wrote a program which creates comma separated string out of vector of int like below:
std::vector<unsigned int> Vec;
Vec.push_back(50);
Vec.push_back(60);
Vec.push_back(10);
Vec.push_back(20);
Vec.push_back(30);
Vec.push_back(2);
Vec.push_back(1);
std::stringstream lineNumString;
lineNumString.str(std::string());
lineNumString.clear();
std::copy(Vec.begin(), Vec.end(), std::ostream_iterator<unsigned int>(lineNumString, ","));
std::string lineString(lineNumString.str());
lineString = lineString.substr(0, lineString.length()-1);
std::cout << std::endl << lineString;
If you see output of above program is:
`50,60,10,20,30,2,1`
But I want to change my output in some different format. I want to have maximum THREE numbers on one line and next numbers to next line. Like below:
50,60,10,
20,30,2,
1
I tried creating child vectors from Vec and tried using them to create different strings. Then I tried splitting lineString and then used those strings.
Please let me know if there is any better way to achieve final output?
I am using VS2010. I can use BOOST features also.
Thanks.
The classic loop way:
#include <vector>
#include <iostream>
#include <sstream>
#include <iterator>
int main()
{
std::vector<unsigned int> Vec;
Vec.push_back(50);
Vec.push_back(60);
Vec.push_back(10);
Vec.push_back(20);
Vec.push_back(30);
Vec.push_back(2);
Vec.push_back(1);
for(auto it = Vec.begin(); it != Vec.end();)
{
std::cout << *it++;
if(it != Vec.end())
std::cout << ",";
if((it - Vec.begin()) % 3 == 0)
std::cout << std::endl;
}
return 0;
}
The Boost.spirit way:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <vector>
#include <string>
namespace karma = boost::spirit::karma;
int main()
{
std::vector<unsigned int> v = {50, 60, 10, 20, 30, 2, 1};
using karma::int_;
using karma::eol;
using karma::duplicate;
using karma::generate;
std::string out;
std::back_insert_iterator<std::string> it(out);
generate(it, int_ << "," << *(int_ << "," << int_ << "," << duplicate[ &int_ << eol << int_ << ","]), v);
out[out.size() - 1] = 0;
std::cout << out << std::endl;
return 0;
}
The little trick with duplicate: it duplicates attribute int_, which is mandatory, because predicate & consume it, in order to know if we display the end of the line (eol) or not.

Using setw and left from iomanip

I want to print out the keys and values in a map in a organized table. I am trying to use setw and left but the output is
The 1
hello1
and I want it to be like
The 1
hello 1
what i have done so far
// System includes
#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
/************************************************************/
Local includes
/************************************************************/
// Using declarations
using std::cout;
using std::map;
using std::unordered_map;
using std::string;
using std::cin;
using std::endl;
using std::ifstream;
using std::left;
using std::setw;
/************************************************************/
Function prototypes/global vars/typedefs
/************************************************************/
int
main (int argc, char* argv[])
{
//call the wordCount function on the user specified input file
unordered_map <string,int> exampleMap;
exampleMap["The"]++;
exampleMap["hello"]++;
cout << left;
//iterate through the map and print out the elements
for( unordered_map<string, int>::iterator i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(5) << (*i).first << setw(15) << (*i).second << endl;
}
return EXIT_SUCCESS;
}
you need to specify a width larger than 5(If you want the field to be larger than 5 characters). You don't need the second call to setw.
try
for( auto i=exampleMap.begin(); i!=exampleMap.end(); ++i)
{
cout << setw(8) << i->first << i->second << '\n';
}

Stack STL with 2 params

Im implementing a B-tree in C++,I have a stack which saves pairs . my problem is, how i put in this stack because push only accept 1 argument. thanks
Use std::pair provided by the standard library.
You can create them with the function make_pair.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
int myInt = 1;
string myString("stringVal");
stack<pair<string, int> > myStack;
myStack.push(make_pair(myString, myInt));
return 1;
}
#include <stack>
#include <utility>
#include <iostream>
using namespace std;
int main() {
stack <pair<int,int> > s;
s.push( make_pair( 1, 2 ) );
pair <int, int> p = s.top();
cout << p.first << " " << p.second << endl;
}
#include <utility>
// ...
stack<pair<string,string> > s;
s.push(make_pair("roses", "red"));
int main()
{
stack <pair<int,int> > s;
s.push({1,2});
cout << s.top().first << " " << s.top().second;
}