void DynamicInterceptTestFramework::SimulateData(int time_dimension) { state_modules().SimulateData(time_dimension); Vector state = state_modules().StateContribution(); data_.clear(); for (int t = 0; t < time_dimension; ++t) { int nobs = 1 + rpois(poisson_rate_); Matrix predictors(nobs, true_beta_.size()); predictors.randomize(); Vector response = predictors * true_beta_; response += state[t]; for (int j = 0; j < nobs; ++j) { response[j] += rnorm_mt(GlobalRng::rng, 0, observation_sd_); } NEW(StateSpace::TimeSeriesRegressionData, data_point)( response, predictors, Selector(response.size(), true)); data_.push_back(data_point); xtx_ += predictors.inner(); total_nobs_ += nobs; } }
int main(int argc, char* argv[]) { // Let us make libsvm quiet gaml::libsvm::quiet(); // random seed initialization std::srand(std::time(0)); try { // Let us collect samples. DataSet basis; basis.resize(100); for(auto& data : basis) { double theta = gaml::random::uniform(0,2*M_PI); data = Data(theta,oracle(theta)); } // Let us set configure a svm struct svm_parameter params; gaml::libsvm::init(params); params.kernel_type = RBF; // RBF kernel params.gamma = 1; // k(u,v) = exp(-gamma*(u-v)^2) params.svm_type = EPSILON_SVR; params.p = .01; // epsilon params.C = 10; params.eps = 1e-10; // numerical tolerence // This sets up a svm learning algorithm for predicting a scalar. auto scalar_learner = gaml::libsvm::supervized::learner<double,double>(params, nb_nodes_of, fill_nodes); // Let us use it for learning x, y and z. auto learner = gaml::multidim::learner<Point,3>(scalar_learner, array_of_output, output_of_array); // Let us train it and get some predictor f. f is a function, as the oracle. std::cout << "Learning..." << std::endl; auto f = learner(basis.begin(), basis.end(), input_of, output_of); // Let us retrieve the three SVMs in order to save each one. auto f_predictors = f.predictors(); std::array<std::string,3> filenames = {{std::string("x.pred"),"y.pred","z.pred"}}; auto name_iter = filenames.begin(); for(auto& pred : f_predictors) pred.save_model(*(name_iter++)); // We can compute the empirical risk with gaml tools. auto evaluator = gaml::risk::empirical(Loss()); double risk = evaluator(f, basis.begin(), basis.end(), input_of, output_of); std::cout << "Empirical risk : " << risk << std::endl << " sqrt(risk) : " << sqrt(risk) << std::endl; // Let us load our predictor. We need first to gather each loaded // scalar predictor in a collection... std::list<gaml::libsvm::Predictor<double,double>> predictors; name_iter= filenames.begin(); for(unsigned int dim = 0; dim < 3; ++dim) { gaml::libsvm::Predictor<double,double> predictor(nb_nodes_of, fill_nodes); predictor.load_model(*(name_iter++)); predictors.push_back(predictor); } // ... and create the 3D predictor (variable g) from the // collection of scalar predictors. gaml::multidim::Predictor<Point, gaml::libsvm::Predictor<double,double>, 3> g(output_of_array, predictors.begin(), predictors.end()); // let us gnuplot what we have. std::ofstream dataset_file("dataset.data"); for(auto& data : basis) dataset_file << data.first << ' ' << data.second.x << ' ' << data.second.y << ' ' << data.second.z << std::endl; dataset_file.close(); std::ofstream prediction_file("prediction.data"); for(double theta = 0; theta <= 2*M_PI; theta += .01) { Point p = g(theta); prediction_file << theta << ' ' << p.x << ' ' << p.y << ' ' << p.z << std::endl; } prediction_file.close(); std::ofstream gnuplot_file_3d("3D.plot"); gnuplot_file_3d << "set ticslevel 0" << std::endl << "set xlabel 'x(theta)'" << std::endl << "set ylabel 'y(theta)'" << std::endl << "set zlabel 'z(theta)' " << std::endl << "splot 'dataset.data' using 2:3:4 with points notitle, " << "'prediction.data' using 2:3:4 with lines notitle" << std::endl; gnuplot_file_3d.close(); std::cout << std::endl << "Try 'gnuplot -p 3D.plot'" << std::endl; std::ofstream gnuplot_file_1d("1D-x.plot"); gnuplot_file_1d << "set xlabel 'theta'" << std::endl << "set ylabel 'x(theta)'" << std::endl << "plot 'dataset.data' using 1:2 with points notitle, " << "'prediction.data' using 1:2 with lines notitle" << std::endl; gnuplot_file_1d.close(); std::cout << "Try 'gnuplot -p 1D-x.plot'" << std::endl << std::endl; } catch(gaml::exception::Any& e) { std::cout << e.what() << std::endl; } return 0; }