CS395T/CAM383M Computational Statistics > HW 1 John Woods HW1
 Register FAQ Calendar Search Today's Posts Mark Forums Read

#1
02-01-2010, 06:35 PM
 johnwoods Member Join Date: Jan 2010 Posts: 30
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
 hw01.pdf (59.3 KB, 913 views)
#2
02-01-2010, 06:36 PM
 johnwoods Member Join Date: Jan 2010 Posts: 30

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

 Tags homework, hw1, john woods

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home CS395T/CAM383M (Spring 2011) Course Administration     Announcements (click here and read!)     Basic Course Information     Supplementary Materials CS395T/CAM383M (Spring 2011) Lectures and Student Participation     Lecture Slides     Other Topics and Student Contributions     Homework Assignments and Student Postings         HW 1         HW 2         HW 3         HW 4         HW 5         HW 6     Student Term Projects Previous year: Spring, 2010     Announcements     Basic Course Information     Supplementary Materials     Lecture Slides     Other Topics and Student Contributions     Homework Assignments and Student Postings         HW 1         HW 2         HW 3         HW 4         HW 5         HW 6     Student Term Projects Previous year: Spring, 2009     Basic Course Information     Supplementary Materials     Lecture Slides     Student Term Projects

All times are GMT -6. The time now is 07:02 AM.

 www.wpressutexas.net - Archive - Top