Created
February 9, 2016 15:36
-
-
Save lyle-nel/c427c1c95fd5aba7c8e2 to your computer and use it in GitHub Desktop.
Script that generates the c++ simulation file and R script files, which is then compiled and run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cat << EOF > sim.cpp | |
#include <random> | |
#include <vector> | |
#include <fstream> | |
#include <algorithm> | |
int main() | |
{ | |
std::default_random_engine random_engine(std::random_device{}()); | |
double median = 50; | |
double std_dev = 35; | |
std::normal_distribution<double> distribution(median, std_dev); | |
//lambda returns a normally distributed random variable | |
auto normal_dist = [&random_engine,&distribution](){return distribution(random_engine);}; | |
const size_t num_tuts = 10; | |
const size_t simulation_runs = 1000000; | |
//simulation for healthy students | |
std::ofstream healthy_students("healthy_students.txt"); | |
for(size_t i = 0; i!=simulation_runs; ++i) | |
{ | |
std::vector<double> student(num_tuts);//all 10 marks for a student | |
generate(student.begin(), student.end(), normal_dist);//gnerate 10 normally distributed scores with median 50% and std_dev 30 | |
student.erase(std::min_element(student.begin(),student.end()));//discard the lowest score, so that 9/10 marks count | |
double sum = std::accumulate(student.begin(), student.end(), 0.0); | |
double average = sum/student.size(); | |
healthy_students << average << std::endl; | |
} | |
//simulation for sick students that missed 1 tut | |
std::ofstream sick_students("sick_students.txt"); | |
for(size_t i = 0; i!=simulation_runs; ++i) | |
{ | |
std::vector<double> student(num_tuts-1);//student was sick, only does 9 | |
generate(student.begin(), student.end(), normal_dist);//generate only 9 | |
//student.erase(std::min_element(student.begin(),student.end()));//dont discard the lowest score since only 9 tuts have been written | |
double sum = std::accumulate(student.begin(), student.end(), 0.0); | |
double average = sum/student.size(); | |
sick_students << average << std::endl; | |
} | |
} | |
EOF | |
cat << EOF > generate_stats.R | |
healthy_student = scan("healthy_students.txt") | |
sick_student = scan("sick_students.txt") | |
png(filename="healthy_student.png") | |
plot(main="Best 9/10 scoring policy for healthy vs sick students",density(healthy_student),col="green",xlab="Final Score%",ylab="Density") | |
lines(density(sick_student),col="red") | |
legend("topright",c("Healthy student","Sick student"), col=c('green','red'),lty=1) | |
dev.off() | |
print("Summary statistics for healthy students") | |
summary(healthy_student) | |
print("Summary statistics for sick students") | |
summary(sick_student) | |
EOF | |
g++-5 -std=c++14 sim.cpp -o sim | |
./sim | |
Rscript generate_stats.R |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment