#sourcing a file executes the lines in the file as though they had been typed in R console.
source('GCMpred.R')

N<- 2*80
N_A <- round(N*.968)

#parameter c scales the similarity function 
c<- 4
w<-c(0.19, 0.12, 0.25, 0.45)

#csv file has 4 cols, each represents a psychological dimension
stim<- as.matrix(read.table('faceStim.csv', sep=','))

#this creates a list of 2 matrices; first holds training examplars from Category A, second holds training examplars from Category B
examplars<- list(a=stim[1:5, ], b=stim[6:10, ])

#to obtain the predicted probability of an A response for the first stimuli, given the stored examplars and GCM parameters c and w
preds<- GCMpred(stim[1, ], examplars, c, w)


#implement the binomial probability mass function; data N_A are given, so we calculate the likelihood of the parameter values given the data
likelihood<- dbinom(N_A, size = N, prob = preds[1])

GCMpred<- function(probe, exemplars, c, w){
  dist<- list()
  
  #to loop through the 2 matrices in examplar (matrix containing A stims, matrix containing B stims); loop will be run twice, once ex equals examplars in A, once examplars in B 
  for (ex in exemplars){
    dist[[length(dist)+1]] <- apply(as.array(ex), 1, #apply function to each row
                                    function(x) #calculates Euclidian distance
                                      sqrt(sum(w*(x-probe)^2))) #x refers to specific row which contains multidimentional values
  }
  
  #dist is a list of 2 vectors (category A or B), each vector contains distance between specific examplar and probe
  
  #turns distances into similarities; 
  #lapply apply function to each element in a list; 
  #to get summed similarity between prob and all examplars in the category currently being analyzed 
  sumsim<- lapply(dist, function(a) sum(exp(-c*a)))
 
  #the probability of making A or B response 
  r_prob<- unlist(sumsim)/sum(unlist(sumsim))
  
  #the vector r_prob is then passed back to the calling script GCMbinom.R
}