Question

In: Statistics and Probability

I want to know why I can not get N(should contain 1000 number) N<-vector() for (k...

I want to know why I can not get N(should contain 1000 number)

N<-vector()
for (k in 0:999){
y=vector()
for(i in 0:999){
r1=runif(1)
r2=runif(1)
x1=-log(r1)
x2=-log(r2)
k=(x1-1)^2/2
if (x2 >= k){
r<-runif(1)
if (r>0.5){
y[i]= x1
}else{
y[i]= -x1
}
}
y.1<- y[-which(is.na(y))]
number<-length(y.1)
}
N[k]<-number
}
N

Solutions

Expert Solution

There is certain problem in your code.

1. You have defined k in the for loop than again you defined the k=(x1-1)^2/2 in the program line. so this chage the value of k. First k should take the value 0 to 999 but here after running the k=(x1-1)^2/2 value changes. So you should assign different vactor. I have choosen K instead of k.

2. You have defined the array from 0. this cause problem since y[0] not defined. So either you start your loop by 1 to 1000 or you just change y[i] to y[ i+1] and N[k] to N[k+1].

3. You should close the second loop before line y.1<- y[-which(is.na(y))] . because till here we have the vector y and our work has done .

The correct code should be

N<-vector()
for (k in 0:999){
y=vector()
for(i in 0:999){
    r1=runif(1)
    r2=runif(1)
    x1=-log(r1)
    x2=-log(r2)
    K=(x1-1)^2/2 ### Just change the k to K
    if (x2 >= K){
      r<-runif(1)
      if (r>0.5){
        y[i+1]= x1
      }else{
        y[i+1]= -x1
      }
    }
}
    y.1<- y[-which(is.na(y))]
    number<-length(y.1)
number
N[k+1]<-number
}
N


Related Solutions

I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
Can I get a breakdown line by line of the time complexity, I know it is...
Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole. Input :- N , Array[N]                                function findSmallestPair()                                sort the Array                                int minimumDiff := INFINITY , A := -1 , B := -1;                                  for(int i : 1 to N-1)                                begin for                currentDiff = Array[i]-Array[i-1]                if(currentDiff < minimumDiff)                begin if   ...
I just want to know if this is decent or should I make some changes. 1....
I just want to know if this is decent or should I make some changes. 1. What are some examples of Ford's variable costs (please discuss and explain 3)? Ford is focused on units produced so their variable costs will include variable factory overhead, direct materials and direct labor. One direct labor cost is that of manufacturing and assembly which deals with the actual assembled components. These components are another form of direct costs; those of direct materials such as...
I want to know how to get the "temp" information from a API response like this...
I want to know how to get the "temp" information from a API response like this in javascript: { "coord": { "lon": -122.08, "lat": 37.39 }, "weather": [ { "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" } ], "base": "stations", "main": { "temp": 282.55, "feels_like": 281.86, "temp_min": 280.37, "temp_max": 284.26, "pressure": 1023, "humidity": 100 }, "visibility": 16093, "wind": { "speed": 1.5, "deg": 350 }, "clouds": { "all": 1 }, "dt": 1560350645, "sys": { "type": 1, "id": 5122, "message":...
I have searched the percentage of each question, but I want to know why there are...
I have searched the percentage of each question, but I want to know why there are differences? a. What's the percentage of loans out of total assets for Bank of America and Central Bank? What explains such a significant difference? BOA:40.2%, Central Bank:67%. b. What's the percentage of total deposits out of total liabilities (including capital) for Bank of America and Central Bank? Again, why do you think there is a significant difference? BOA:66.1%, Central Bank:89%.
Can I get all of the question answered in a way that I know under which...
Can I get all of the question answered in a way that I know under which sub question the answer goes please. Can I get all of the questions answered in a way that I know how it fits in the question. Some parts are explanations. Thank you. Q. When a pink aqueous solution of potassium permanganate, faintly acidified with dilute sulfuric acid was treated with 10% aq. hydrogen peroxide, the reaction took place with the evolution of gas bubbles,...
Which statistical test should I use if I want to know if two values are close...
Which statistical test should I use if I want to know if two values are close to one another? For example, the average time for birds at a birdfeeder are 53.52 sec compared to those who stay in the surrounding branches (52.81 sec) I don't know if there is an applicable test I can apply for two simple variables. Thanks !
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and if so what would it dimension size be?
For this post, I want to know which T-ratio is the appropriate result and why?
For this post, I want to know which T-ratio is the appropriate result and why?
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT