Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. double RandNorm(){
  2. random_device rnd;
  3. normal_distribution<double> norm(0.0, 1.0);
  4. return norm(rnd);
  5. }
  6.  
  7. double MakeTree2(S s, Node *node, queue<Node *> q1, double theta_true){
  8. if(node->depth == 0){
  9. node->theta = RandNorm();
  10. node->item_no = (int)(rand() * (I + 1.0) / (1.0 + RAND_MAX));
  11. }else{
  12. node->theta = Theta_CAT(s, node);
  13. node->item_no = SelectItem(s, node, node->theta);
  14. cout << node->hist_item_no[node->depth-1] << "->";
  15. }
  16.  
  17. presented[node->item_no] = 1;
  18. cout << node->item_no << "(" << node->theta << ")" << " ";
  19.  
  20. Node *p_left;
  21. p_left = (Node *)malloc(sizeof(*node->incorrect));
  22. p_left = node->incorrect;
  23. if(node->depth > 0){
  24. for(int c = 0; c < node->depth; c++){
  25. p_left->hist_item_no.push_back(node->hist_item_no[c]);
  26. p_left->hist_response.push_back(node->hist_response[c]);
  27. }
  28. }
  29. p_left->hist_item_no.push_back(node->item_no);
  30. p_left->hist_response.push_back(0);
  31. p_left->depth = node->depth + 1;
  32. q1.push(p_left);
  33.  
  34. Node *p_right;
  35. p_right = (Node *)malloc(sizeof(*node->correct));
  36. p_right = node->correct;
  37. if(node->depth > 0){
  38. for(int c = 0; c < node->depth; c++){
  39. p_right->hist_item_no.push_back(node->hist_item_no[c]);
  40. p_right->hist_response.push_back(node->hist_response[c]);
  41. }
  42. }
  43. p_right->hist_item_no.push_back(node->item_no);
  44. p_right->hist_response.push_back(1);
  45. p_right->depth = node->depth + 1;
  46. q1.push(p_right);
  47.  
  48. int nodes = 0, rest = 0;
  49. for(int i = 0; i < I; i++){
  50. if(presented[i] == 1) nodes++;
  51. }
  52. if(nodes == pow(2, node->depth + 1) - 1){
  53. cout << endl;
  54. for(int i = 0; i < I; i++){
  55. if(presented[i] == 0) rest++;
  56. }
  57. if(pow(2, node->depth + 1) > rest){
  58. free(p_left);
  59. free(p_right);
  60. return node->depth;
  61. }
  62. }
  63.  
  64. Node *next;
  65. next = (Node *)malloc(sizeof(*q1.front()));
  66. next = q1.front();
  67. q1.pop();
  68. double depth_of_tree = MakeTree2(s, next, q1, theta_true);
  69. free(next);
  70.  
  71. if(node->depth == 0){
  72. double theta_est;
  73. Node *current;
  74. current = (Node *)malloc(sizeof(*node));
  75. current = node;
  76. for(int d = 0; d <= (int)depth_of_tree; d++){
  77. if((double) rand() / RAND_MAX < P_X_h(s.a[current->item_no], s.b[current->item_no], theta_true)){
  78. current = node->correct;
  79. }else{
  80. current = node->incorrect;
  81. }
  82. theta_est = node->theta;
  83. }
  84. free(current);
  85. return theta_est;
  86. }
  87.  
  88. return depth_of_tree;
  89. }
  90.  
  91. void BreakTree(Node *node){
  92. if(node->incorrect != NULL){
  93. BreakTree(node->incorrect);
  94. }
  95. if(node->correct != NULL){
  96. BreakTree(node->correct);
  97. }
  98. free(node);
  99. }
  100.  
  101. double CAT_sim2(S s){
  102. double theta_true, theta_est, error;
  103. theta_true = RandNorm();
  104.  
  105. queue<Node *> q1;
  106. Node root, *p_root = &root;
  107.  
  108. p_root->depth = 0;
  109. p_root->theta = 0.0;
  110. theta_est = MakeTree2(s, p_root, q1, theta_true);
  111. BreakTree(p_root);
  112.  
  113. error = theta_true - theta_est;
  114. cout << "theta_true = " << theta_true << ", theta_estimated = " << theta_est << ", error = " << error << endl;
  115. return error;
  116. }
  117.  
  118. double CAT_sim_exam(S s){
  119. int count = 10;
  120. double error[count] = {};
  121. double error_sum = 0;
  122.  
  123. for(int c = 0; c < count; c++){
  124. error[c] = CAT_sim2(s);
  125. error_sum += error[c];
  126. }
  127. cout << "MSE: " << error_sum / (double)count << endl;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement