title: Building a covariance matrix in Python
body: <p><strong>Problem</strong>
  I want to implement an algorithm from an unpublished paper by my supervisor and as part of that, I need to construct a covariance matrix C using some rules given in the paper. I'm coming from Matlab and wanted to take this opportunity to finally learn Python, hence my question: How do I do this in the most efficient (fast) way in Python (including numpy,scipy)?</p>
  
  <p><em>Subproblem 1:</em></p>
  
  <ul>
  <li>Option 1: I use 2 <em>for</em> loops , looping over all rows and all columns. I would assume that's the worst thing to do.</li>
  <li>Option 2: Using list comprehension, I construct a list of euclidean pairs and then iterate over that list. That's what I'm doing now.</li>
  </ul>
  
  <p>Is there any better way?</p>
  
  <p><em>Subproblem 2</em></p>
  
  <ul>
  <li>Option 1: I iterate over all elements in the matrix.</li>
  <li>Option 2: I iterate only over the lower triangular part (without diagonal), then add the transpose (because covariance matrices are symmetric) and then add the diagonal.</li>
  </ul>
  
  <p>I'm fairly convinced subproblem 1 is a no-brainer but I don't know about subproblem 2. I should probably also say that the matrix I'm dealing with is probably 2*10^4 x 2*10^4.</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>
  I prefer not to give the actual covariance matrix but since people want to have an example, let's say we want to construct the Covariance matrix of a stochastic process called 'Brownian bridge'. It's structure is given by:</p>
  
  <blockquote>
    <p><em>cov(Xs, Xt) = min{s,t} − st</em></p>
  </blockquote>
  
  <p>for let's say <em>s,t ∈ {1,...,100}</em>. How would you build it?</p>