Question

This is an example out of my text which I copied directly but am getting an...

This is an example out of my text which I copied directly but am getting an error every iteration and am unsure how to correct it. The error is "

the condition has length > 1 and only the first element will be used" and my code is:

f <- function(x, sigma) {
if (any(x < 0)) return(0)
stopifnot(sigma > 0)
return((x/sigma^2)*exp(-x^2 / (s*sigma^2)))
}

xt <- x[i-1]
y <- rchisq(1, df = xt)

m <- 10000
sigma <- 4
x <- numeric(m)
x[1] <- rchisq(1, df = 1)
k <- 0
u <- runif(m)

for(i in 2:m) {
xt <- x[i-1]
y <- rchisq(1, df = xt)
num <- f(y, sigma) * dchisq(xt, df = y)
den <- f(xt, sigma) * dchisq(y, df = xt)
if (u[i] <= num/den) x[i] <- y else {
x[i] <- xt
k <- k+1
}
}
print(k)

index <- 5000:5500
y1 <- x[index]
#plot(index, y1, type = "l", main = "", ylab = "x")

Homework Answers

Answer #1

The following modified code gives one error:

Error in f(y, sigma) : object 's' not found

You are not sending the value of "s" to the function "f".

No other errors. Is it "s" or '2" ?

----------------------------------------------------------------------------------------

f <- function(x, sigma) {
if (any(x < 0)) return(0)
stopifnot(sigma > 0)
return((x/sigma^2)*exp(-x^2 / (s*sigma^2)))
}
m <- 10000
sigma <- 4
x <- numeric(m)
x[1] <- rchisq(1, df = 1)
k <- 0
u <- runif(m)

xt <- x[i-1]
y <- rchisq(1, df = xt)

for(i in 2:m) {
xt <- x[i-1]
y <- rchisq(1, df = xt)
num <- f(y, sigma) * dchisq(xt, df = y)
den <- f(xt, sigma) * dchisq(y, df = xt)
if (u[i] <= num/den) x[i] <- y else {
x[i] <- xt
k <- k+1
}
}
print(k)

index <- 5000:5500
y1 <- x[index]
#plot(index, y1, type = "l", main = "", ylab = "x")

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I am not sure what I am doing wrong in this coding. I keep getting this...
I am not sure what I am doing wrong in this coding. I keep getting this error. I tried to change it to Circle2D.java but it still comes an error: Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java public class Circle2D { public class Exercise10_11 { public static void main(String[] args) {     Circle2D c1 = new Circle2D(2, 2, 5.5);     System.out.println("area: " + c1.getArea());     System.out.println("perimeter: " + c1.getPerimeter());     System.out.println("contains(3, 3): "...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
Hello! I hope you are healthy and well! I am hoping that this message finds you...
Hello! I hope you are healthy and well! I am hoping that this message finds you happy and content! I am having trouble solving this 5-part practice problem. I would greatly appreciate any and all help that you could lend! Thanks in advance! In the following proof, what is the justification for line 7? 1.     [(W ⊃ X) ⊃ Y] ∨ ( P ≡ Q) 2.     ∼X • ∼Y 3.     ∼(P ≡ Q) / ∴ ∼W 4.     ∼X                  2 Simp 5.     ∼Y                  2 Simp 6.     (W ⊃ X)...
MATLAB** I'm not sure what I'm doing wrong writing out my equation. I keep getting an...
MATLAB** I'm not sure what I'm doing wrong writing out my equation. I keep getting an error. It has something to do with the line with the function written out. Here's what I have: %3.2 clc clear close all %plot ?(?,?)=?(1+sin?), as a 3D plot on the domain ?∈[−8,8], ?∈[−8,8]. x=[-8:8]; y=[-8,8]; z=x(sin(y)+1); plot3(x,y,z) title('Plot 3.2')
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Question 4 please although I am unsure of my answers for the rest as well :(...
Question 4 please although I am unsure of my answers for the rest as well :( Here is the full list for background info. Consider a significance test for a null hypothesis versus a two-sided alternative. State all values of a standard normal test statistic z that will give a result significant at the 10% level but not at the 5% level of significance. (Sec. 6.2) You perform 1,000 significance tests using α = 0.01. Assuming that all the null...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Solve the separable equation 3xy^2 dy/dx = x^2 + 1 given y(0) =5 I am getting...
Solve the separable equation 3xy^2 dy/dx = x^2 + 1 given y(0) =5 I am getting a general solution of cubed root x2/2+ln|x|+3c1 when I enter the initial conditions y(0)=5 the equation is undefined. How do deal with this? What would the final answer be? Thanks
I am trying to print out an 8 by 8 "chess board". My first step is...
I am trying to print out an 8 by 8 "chess board". My first step is to set all values in the board to 0. I made a function to do so but it isn't working, and i can't figure out why. I'm trying to, for now, simply print out an 8 by 8 of zero values. This is what I have: (C++) // set all of board to 0 as a function // bool function to check for valid...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT