Having some troubles understanding this code - generative-adversarial-network

The image of the code
What does the 5 highlighted lines of the code mean?
Basically to get the "get_top_covariances", I need to follow these 4 steps.
Start by finding the covariance matrix
The target feature should not be included in the outputs.
It may be easiest to solve this if you find the relevant_indices first, and then use relevant_indices to calculate highest_covariances.
You want to sort by absolute value but return the actual values.
Hence, I would highlight appreciate it if anyone could help to explain those 5 lines,as in what's it is trying to do.
By the way, what's that top_n for? I'm quite lost at this part.

Related

Write a loop to divide last value in y and first value in x and so on for #whole vector

I'm unsure how to do this. If possible a walkthrough on this would be greatly appreciated! Just started a class that uses R and find somethings to be very complicated.
Thanks!
I have tried to just write simple types of loops but not sure how to tie in all of the functions I need to do.

For loop setup with multiple parameters in R

I'm trying to figure out how to get a for loop setup in R when I want it to run two or more parameters at once. Below I have posted a sample code where I am able to get the code to run and fill a matrix table with two values. In the 2nd line of the for loop I have
R<-ARMA.var(length(x_global_sample),ar=c(tt[i],-.7))
And what I would like to do is replace the -.7 with another tt[i], example below, so that my for loop would run through the values starting at (-1,-1), then it would be as follows (-1,-.99),
(-1,-.98),...,(1,.98),(1,.99),(1,1) where the result matrix would then be populated by the output of Q and sigma.
R<-ARMA.var(length(x_global_sample),ar=c(tt[i],tt[i]))
or something similar to
R<-ARMA.var(length(x_global_sample),ar=c(tt[i],ss[i]))
It may be very possible that this would be better handled by two for loops however I'm not 100% sure on how I would set that up so the first parameter would be fixed and the code would run through the sequence of the second parameter, once that would get finished the first parameter would now increase by one and fix itself at that increase until the second parameter does another run through.
I've posted some sample code down below where the ARMA.var function just comes from the ts.extend package. However, any insight into this would be great.
Thank you
tt<-seq(-1,1,0.01)
Result<-matrix(NA, nrow=length(tt)*length(tt), ncol=2)
for (i in seq_along(tt)){
R<-ARMA.var(length(x_global_sample),ar=c(tt[i],-.7))
Q<-t((y-X%*%beta_est_d))%*%solve(R)%*%(y-X%*%beta_est_d)+
lam*t(beta_est_d)%*%D%*%beta_est_d
RSS<-sum((y-X%*%solve(t(X)%*%solve(R)%*%X+lam*D)%*%t(X)%*%solve(R)%*%y)^2)
Denom<-n-sum(diag(X%*%solve(t(X)%*%solve(R)%*%X+lam*D)%*%t(X)%*%solve(R)))
sigma<-RSS/Denom
Result[i,1]<-Q
Result[i,2]<-sigma
rm(Q)
rm(R)
rm(sigma)
}
Edit: I realize that what I have posted above is quite unclear so to simplify things consider the following code,
x<-seq(1,20,1)
y<-seq(1,20,2)
Result<-matrix(NA, nrow=length(x)*length(y), ncol=2)
for(i in seq_along(x)){
z1<-x[i]+y[i]
z2<-z1+y[i]
Result[i,1]<-z1
Result[i,2]<-z2
}
So the results table would appear as follow as the following rows,
Row1: 1+1=2, 2+1=3
Row2: 1+3=4, 4+3=7
Row3: 1+5=6, 6+5=11
Row4: 1+7=8, 8+7=15
And this pattern would continue with x staying fixed until the last value of y is reached, then x would start at 2 and cycle through the calculations of y to the point where my last row is as,
RowN: 20+19=39, 39+19=58.
So I just want to know if is there a way to do it in one loop or if is it easier to run it as 2 loops.
I hope this is clearer as to what my question was asking, and I realize this is not the optimal way to do this, however for now it is just for testing purposes to see how long my initial process takes so that it can be streamlined down the road.
Thank you

Expressing set of sequences as one sequence

This is a discrete math problem, and i was hoping that someone will guide me in the right direction on how to go about solving it...
I have the following set of sequences:
a_(2n) = 8^n
This will give me the values of all the even terms
a_(2n+1) = (-3)8^n
This will give me the values of all the odd terms
I would like to know if there's a way for me to express the values of all terms (both even and odd) using only one formula! Would you please help me!
Thank you,
You can define it as follows:
a_k=[-1+2*(-1)^k]*8^(floor(k/2))

Product of two permutations (cycles) in Prolog

I would like to find the product of two permutations in Prolog (in cycle form) and I'm having problems with it (mostly because I can't even imagine, what it will look like).
I thought about changing these permutations into another representation, but I'm not sure which way is right.
So help me please, any hint is greatly appreciated.
%permutationproduct(+P1,+P2,-Result)
EDIT: Under the product of permutation I mean this: CLICK (but our inputs are in cycle notation, which makes the project more difficult). The inputs are 2 permutations (P1,P2) and the expected result is the 3rd parameter - the product of the permutations.
And I'm actually working on a bigger project, this is just one part of it, but as mentioned, I can't even start it, because I can't imagine it.

Writing to a file in R prints on 2 lines

I am writing a vector to a file in R. However the output comes on 2 lines. I wanted all the values to come on a single line. Can you let me know how to fix this
write(value,file=fileconn,append=TRUE,sep="\t")
The o/p comes as follows
1777.167 1825.167 1873.167 1921.167 1969.167
2017.167
Regards
Ganesh
I'm not sure write() is probably not the best choice, but if you want to use it, it might be a good idea to check the ?write help file. It does have an ncolumns= parameter which defaults to 5 for simple numeric vectors.
I would think cat() would be a better solution for just dumping numeric vectors.

Resources