CS395T/CAM383M Computational Statistics  

Go Back   CS395T/CAM383M Computational Statistics > Previous year: Spring, 2010 > Homework Assignments and Student Postings > HW 1

Reply
 
Thread Tools Display Modes
  #1  
Old 02-01-2010, 06:35 PM
johnwoods johnwoods is offline
Member
 
Join Date: Jan 2010
Posts: 30
Post John Woods HW1

Hey, so sorry this took so long. Here's the Octave code, and the rest of the answers are in the file.
Code:
% John O. Woods
% HW #1, P03
% Bill Press' CS 395T

function roll = die_roll(sides)
  roll = floor(rand(1) * sides);
end

function [monty,other] = monty_picks_a_goat(car, goat1, goat2, first_guess)
  d = die_roll(2)
  if first_guess == car
    if d == 0
      monty = goat1
      other = goat2
    else
      monty = goat2
      other = goat1
    end
  elseif first_guess == goat1
    monty = goat2
    other = goat1
  elseif first_guess == goat2
    monty = goat1
    other = goat2
  else
    % Only works in Matlab, not in Octave:
    % errRecord = MException('VerifyInput:OutOfBounds', 'Inputs are outside the allowable limits');
    % throw(errRecord);
    printf("Error: Goats are problematic\n");
    first_guess
    goat1
    goat2
    quit
  end
end


function win_with_change = game_show()
  car = 1;
  goat1 = 0;
  goat2 = 2;
  monty_goat = -1;
  other_goat = -1;

  first_choice = die_roll(3)
  [monty_goat,other_goat] = monty_picks_a_goat(car, goat1, goat2, first_choice);

  if first_choice == car
    win_with_change = 0;
  else
    win_with_change = 1;
  end
end


function [rate_win_change, rate_win_same, record_wins] = monty_carlo(trials)

  record_wins = zeros(trials,1)

  win_change = 0;
  win_same = 0;
  for n = 1:trials
    % Give the trial:
    printf("------------- Trial: ");
    n

    result = game_show()
    if result == 1
      win_change++;
    else
      win_same++;
    end

    record_wins(n,1) = win_change / n;
  end

  printf("------------- End\n");
  plot(record_wins)
  printf("Rate when we change choices: ");
  rate_win_change = win_change / trials
  printf("Rate when we keep the same choice: ");
  rate_win_same = win_same / trials
end
Attached Images
File Type: pdf hw01.pdf (59.3 KB, 897 views)
Reply With Quote
  #2  
Old 02-01-2010, 06:36 PM
johnwoods johnwoods is offline
Member
 
Join Date: Jan 2010
Posts: 30
Default

Oh, almost forgot. To execute the code, just do:
Code:
monty_carlo(10000)
Reply With Quote
Reply

Tags
homework, hw1, john woods

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:47 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.