2D list comprehension for the sum of indices of a particular value bigger than a constant?












1












$begingroup$


I have a matrix $b$ with elements:
$$b =
begin{pmatrix}
0.01 & 0.02 & cdots & 1 \
0.01 & 0.02 & cdots & 1 \
vdots& vdots & ddots & vdots \
0.01 & 0.02 & cdots & 1 \
end{pmatrix}
$$
For which through a series of calculation which is vectorised, $b$ is used to calculate $a$ which is another matrix that has the same dimension/shape as $b$.
$$a =
begin{pmatrix}
3 & 5 & cdots & 17 \
2 & 6 & cdots & 23 \
vdots& vdots & ddots & vdots \
4 & 3 & cdots & 19 \
end{pmatrix}
$$

At this point it is important to note that the elements of $a$ and $b$ have a one to one correspondence. The different row values(let's call it $sigma$) $0.01, 0.02...$ are different parameters for a series of simulations that I'm running. Hence for a fixed value of say $sigma = 0.01$, the length of its column values correspond to the total number of "simulations" I'm running for that particular parameter. If you know python vectorisation then you'll start to understand what I'm doing.



It is known that higher the $sigma$, the more the number of simulations for that particular sigma will have a value higher than 5 i.e. more of the matrix element along a column will have value bigger than 5. Essentially what I'm doing is vectorising $N$(columns) different simulations for $M$(rows) different parameters. Now I wish to find out the value of $sigma$ for which the total number simulation that's bigger than 5, is bigger than 95% of the total simulation.



To put it more concisely, for a $sigma$ of 0.02, each simulation would have results of $$5, 6, ..., 3$$ with say a total simulation of $N$. So let $$kappa = sum{ (text{all the simulations that have values bigger than 5})},$$I wish to find out the FIRST $sigma$ for which
$$frac{kappa}{N} > 0.95*N$$
i.e. the FIRST $sigma$ for which the proportion of total experiment for which its value $>5$ is bigger than 95% of the total number of experiment.



The code that I have written is:



# say 10000 simulations for a particular sigma 
SIMULATION = 10000

# say 100 different values of sigma ranging from 0.01 to 1
# this is equivalent to matrix b in mathjax above
SIGMA = np.ones((EXPERIMENTS,100))*np.linspace(0.01, 1, 100)

def return_sigma(matrix, simulation, sigma):
"""
My idea here is I put in sigma and matrix and total number of simulation.
Each time using np.ndenumerate looping over i and j to compare if the
element values are greater than 5. If yes then I add 1 to counter, if no
then continue. If the number of experiments with result bigger than 5 is
bigger than 95% of total number of experiment then I return that particular
sigma.
"""
counter = 0
for (i, j), value in np.ndenumerate(matrix):
if value[i, j] > 5:
counter+=1
if counter/experiments > 0.95*simulation:
break
return sigma[0, j] # sigma[:, j] should all be the same anyway
"""Now this can be ran by:"""
print(return_sigma(a, SIMULATION, SIGMA))


which doesn't seem to quite work as I'm not well-versed with 2D slicing comprehension so this is quite a challenging problem for me. Thanks in advance.










share|improve this question







New contributor




user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    I have a matrix $b$ with elements:
    $$b =
    begin{pmatrix}
    0.01 & 0.02 & cdots & 1 \
    0.01 & 0.02 & cdots & 1 \
    vdots& vdots & ddots & vdots \
    0.01 & 0.02 & cdots & 1 \
    end{pmatrix}
    $$
    For which through a series of calculation which is vectorised, $b$ is used to calculate $a$ which is another matrix that has the same dimension/shape as $b$.
    $$a =
    begin{pmatrix}
    3 & 5 & cdots & 17 \
    2 & 6 & cdots & 23 \
    vdots& vdots & ddots & vdots \
    4 & 3 & cdots & 19 \
    end{pmatrix}
    $$

    At this point it is important to note that the elements of $a$ and $b$ have a one to one correspondence. The different row values(let's call it $sigma$) $0.01, 0.02...$ are different parameters for a series of simulations that I'm running. Hence for a fixed value of say $sigma = 0.01$, the length of its column values correspond to the total number of "simulations" I'm running for that particular parameter. If you know python vectorisation then you'll start to understand what I'm doing.



    It is known that higher the $sigma$, the more the number of simulations for that particular sigma will have a value higher than 5 i.e. more of the matrix element along a column will have value bigger than 5. Essentially what I'm doing is vectorising $N$(columns) different simulations for $M$(rows) different parameters. Now I wish to find out the value of $sigma$ for which the total number simulation that's bigger than 5, is bigger than 95% of the total simulation.



    To put it more concisely, for a $sigma$ of 0.02, each simulation would have results of $$5, 6, ..., 3$$ with say a total simulation of $N$. So let $$kappa = sum{ (text{all the simulations that have values bigger than 5})},$$I wish to find out the FIRST $sigma$ for which
    $$frac{kappa}{N} > 0.95*N$$
    i.e. the FIRST $sigma$ for which the proportion of total experiment for which its value $>5$ is bigger than 95% of the total number of experiment.



    The code that I have written is:



    # say 10000 simulations for a particular sigma 
    SIMULATION = 10000

    # say 100 different values of sigma ranging from 0.01 to 1
    # this is equivalent to matrix b in mathjax above
    SIGMA = np.ones((EXPERIMENTS,100))*np.linspace(0.01, 1, 100)

    def return_sigma(matrix, simulation, sigma):
    """
    My idea here is I put in sigma and matrix and total number of simulation.
    Each time using np.ndenumerate looping over i and j to compare if the
    element values are greater than 5. If yes then I add 1 to counter, if no
    then continue. If the number of experiments with result bigger than 5 is
    bigger than 95% of total number of experiment then I return that particular
    sigma.
    """
    counter = 0
    for (i, j), value in np.ndenumerate(matrix):
    if value[i, j] > 5:
    counter+=1
    if counter/experiments > 0.95*simulation:
    break
    return sigma[0, j] # sigma[:, j] should all be the same anyway
    """Now this can be ran by:"""
    print(return_sigma(a, SIMULATION, SIGMA))


    which doesn't seem to quite work as I'm not well-versed with 2D slicing comprehension so this is quite a challenging problem for me. Thanks in advance.










    share|improve this question







    New contributor




    user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      I have a matrix $b$ with elements:
      $$b =
      begin{pmatrix}
      0.01 & 0.02 & cdots & 1 \
      0.01 & 0.02 & cdots & 1 \
      vdots& vdots & ddots & vdots \
      0.01 & 0.02 & cdots & 1 \
      end{pmatrix}
      $$
      For which through a series of calculation which is vectorised, $b$ is used to calculate $a$ which is another matrix that has the same dimension/shape as $b$.
      $$a =
      begin{pmatrix}
      3 & 5 & cdots & 17 \
      2 & 6 & cdots & 23 \
      vdots& vdots & ddots & vdots \
      4 & 3 & cdots & 19 \
      end{pmatrix}
      $$

      At this point it is important to note that the elements of $a$ and $b$ have a one to one correspondence. The different row values(let's call it $sigma$) $0.01, 0.02...$ are different parameters for a series of simulations that I'm running. Hence for a fixed value of say $sigma = 0.01$, the length of its column values correspond to the total number of "simulations" I'm running for that particular parameter. If you know python vectorisation then you'll start to understand what I'm doing.



      It is known that higher the $sigma$, the more the number of simulations for that particular sigma will have a value higher than 5 i.e. more of the matrix element along a column will have value bigger than 5. Essentially what I'm doing is vectorising $N$(columns) different simulations for $M$(rows) different parameters. Now I wish to find out the value of $sigma$ for which the total number simulation that's bigger than 5, is bigger than 95% of the total simulation.



      To put it more concisely, for a $sigma$ of 0.02, each simulation would have results of $$5, 6, ..., 3$$ with say a total simulation of $N$. So let $$kappa = sum{ (text{all the simulations that have values bigger than 5})},$$I wish to find out the FIRST $sigma$ for which
      $$frac{kappa}{N} > 0.95*N$$
      i.e. the FIRST $sigma$ for which the proportion of total experiment for which its value $>5$ is bigger than 95% of the total number of experiment.



      The code that I have written is:



      # say 10000 simulations for a particular sigma 
      SIMULATION = 10000

      # say 100 different values of sigma ranging from 0.01 to 1
      # this is equivalent to matrix b in mathjax above
      SIGMA = np.ones((EXPERIMENTS,100))*np.linspace(0.01, 1, 100)

      def return_sigma(matrix, simulation, sigma):
      """
      My idea here is I put in sigma and matrix and total number of simulation.
      Each time using np.ndenumerate looping over i and j to compare if the
      element values are greater than 5. If yes then I add 1 to counter, if no
      then continue. If the number of experiments with result bigger than 5 is
      bigger than 95% of total number of experiment then I return that particular
      sigma.
      """
      counter = 0
      for (i, j), value in np.ndenumerate(matrix):
      if value[i, j] > 5:
      counter+=1
      if counter/experiments > 0.95*simulation:
      break
      return sigma[0, j] # sigma[:, j] should all be the same anyway
      """Now this can be ran by:"""
      print(return_sigma(a, SIMULATION, SIGMA))


      which doesn't seem to quite work as I'm not well-versed with 2D slicing comprehension so this is quite a challenging problem for me. Thanks in advance.










      share|improve this question







      New contributor




      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have a matrix $b$ with elements:
      $$b =
      begin{pmatrix}
      0.01 & 0.02 & cdots & 1 \
      0.01 & 0.02 & cdots & 1 \
      vdots& vdots & ddots & vdots \
      0.01 & 0.02 & cdots & 1 \
      end{pmatrix}
      $$
      For which through a series of calculation which is vectorised, $b$ is used to calculate $a$ which is another matrix that has the same dimension/shape as $b$.
      $$a =
      begin{pmatrix}
      3 & 5 & cdots & 17 \
      2 & 6 & cdots & 23 \
      vdots& vdots & ddots & vdots \
      4 & 3 & cdots & 19 \
      end{pmatrix}
      $$

      At this point it is important to note that the elements of $a$ and $b$ have a one to one correspondence. The different row values(let's call it $sigma$) $0.01, 0.02...$ are different parameters for a series of simulations that I'm running. Hence for a fixed value of say $sigma = 0.01$, the length of its column values correspond to the total number of "simulations" I'm running for that particular parameter. If you know python vectorisation then you'll start to understand what I'm doing.



      It is known that higher the $sigma$, the more the number of simulations for that particular sigma will have a value higher than 5 i.e. more of the matrix element along a column will have value bigger than 5. Essentially what I'm doing is vectorising $N$(columns) different simulations for $M$(rows) different parameters. Now I wish to find out the value of $sigma$ for which the total number simulation that's bigger than 5, is bigger than 95% of the total simulation.



      To put it more concisely, for a $sigma$ of 0.02, each simulation would have results of $$5, 6, ..., 3$$ with say a total simulation of $N$. So let $$kappa = sum{ (text{all the simulations that have values bigger than 5})},$$I wish to find out the FIRST $sigma$ for which
      $$frac{kappa}{N} > 0.95*N$$
      i.e. the FIRST $sigma$ for which the proportion of total experiment for which its value $>5$ is bigger than 95% of the total number of experiment.



      The code that I have written is:



      # say 10000 simulations for a particular sigma 
      SIMULATION = 10000

      # say 100 different values of sigma ranging from 0.01 to 1
      # this is equivalent to matrix b in mathjax above
      SIGMA = np.ones((EXPERIMENTS,100))*np.linspace(0.01, 1, 100)

      def return_sigma(matrix, simulation, sigma):
      """
      My idea here is I put in sigma and matrix and total number of simulation.
      Each time using np.ndenumerate looping over i and j to compare if the
      element values are greater than 5. If yes then I add 1 to counter, if no
      then continue. If the number of experiments with result bigger than 5 is
      bigger than 95% of total number of experiment then I return that particular
      sigma.
      """
      counter = 0
      for (i, j), value in np.ndenumerate(matrix):
      if value[i, j] > 5:
      counter+=1
      if counter/experiments > 0.95*simulation:
      break
      return sigma[0, j] # sigma[:, j] should all be the same anyway
      """Now this can be ran by:"""
      print(return_sigma(a, SIMULATION, SIGMA))


      which doesn't seem to quite work as I'm not well-versed with 2D slicing comprehension so this is quite a challenging problem for me. Thanks in advance.







      python dataset data






      share|improve this question







      New contributor




      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 34 mins ago









      user3613025user3613025

      62




      62




      New contributor




      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user3613025 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          user3613025 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48316%2f2d-list-comprehension-for-the-sum-of-indices-of-a-particular-value-bigger-than-a%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user3613025 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user3613025 is a new contributor. Be nice, and check out our Code of Conduct.













          user3613025 is a new contributor. Be nice, and check out our Code of Conduct.












          user3613025 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48316%2f2d-list-comprehension-for-the-sum-of-indices-of-a-particular-value-bigger-than-a%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus I

          Tabula Rosettana

          How to label and detect the document text images