Newest questions tagged matrix probability-or-statistics - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T11:30:26Z https://mathematica.stackexchange.com/feeds/tag?tagnames=matrix+probability-or-statistics&sort=newest https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/194840 2 Compute the potential of confusion between two matrices Ben Aawf https://mathematica.stackexchange.com/users/63824 2019-04-08T22:16:07Z 2019-07-20T15:30:13Z <p>I have a problem. Any help will be appreciated.</p> <p>In order to calculate the accuracy, sensitivity, sensibility and F1 score between diseases and their symptoms, I have two matrices with a different Diseases associated with their symptoms with the same format but have different dimensions</p> <pre><code>Mtx1 = {{"di","s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11"},{"d1",1,1,0,1,1,1,0,0,1,0,0},{"d2",0,1,1,1,0,0,1,0,1,1,1},{"d3",0,1,1,0,0,0,0,1,0,0,1},{"d4",0,1,0,1,1,0,0,1,0,1,0},{"d5",0,0,1,1,0,0,0,0,1,1,1},{"d6",0,0,0,0,1,0,0,0,0,0,0}}; </code></pre> <p>second matrix:</p> <pre><code>Mtx2 = {{"di","s1","s7","s3","s6","s4","s8","s9","s10","s12"},{"d7",1,1,0,1,1,1,0,0,1}, {"d8",0,1,1,1,0,0,1,0,1},{"d9",0,1,1,0,0,0,0,1,0},{"d4",0,1,0,1,1,0,0,1,0},{"d1",0,0,1,1,0,0,0,0,1},{"d3",0,0,0,0,1,0,0,0,0},{"d10",0,1,0,0,1,0,0,0,0},{"d11",0,0,1,1,0,0,0,0,1}}; </code></pre> <p>I would like to calculate the index of confusion diseases in <code>Mtx1</code> and <code>Mtx2</code> then the confusion matrix between diseases in two lists. using the below equation to calculate the index of confusion(IC), in fact, the IC assigns the similarity score of symptoms between two diseases. <em><code>IC =(number of common symptoms between two diseases/Union of symptoms of these two diseases)</code></em> Using Mathematica, this is my script for two diseases in a separated list:</p> <pre><code>(* d = disease s = symptoms 1 = symptoms correspond to disease 0 = not a symptom for this disease *) D1 = Mtx1[]; D2 = Mtx2[]; PSMtx1 = Position[D1, 1] // Flatten; PSMtx2 = Position[D1, 1] // Flatten; AllS = (Union[PSMtx1, PSMtx2] // Dimensions)[] CommonS = (Intersection[PSMtx1, PSMtx2] // Dimensions)[] IC = CommonS/AllS // N (* Out: *) (* 6 *) (* 6 *) (* 1 *) </code></pre> <p>I did this computation, but it serves only for confusion between two diseases. what about the confusion between all diseases in two lists?</p> <p>I want to adopt this code above for all diseases in both matrices to get the confusion matrices.</p> <p>Does anybody have any insight?</p> https://mathematica.stackexchange.com/q/194617 1 Noise covariance matrix jsid https://mathematica.stackexchange.com/users/60268 2019-04-04T19:23:30Z 2019-04-05T01:07:59Z <p>I am trying to find the noise covariance matrix of a random vector <span class="math-container">$\mathbf{x}$</span> with itself. The elements of this vector represent image pixels and are treated as random variables i.e, <span class="math-container">$\mathbf{x}=\{x_1,x_2 \dots x_n \}$</span>. For a <span class="math-container">$M\times M$</span> image we have <span class="math-container">$n=M*M$</span>. The resulting covariance matrix, therefore, should be of the size <span class="math-container">$n\times n$</span>.</p> <p>The covariance matrix can be given as: </p> <p><span class="math-container">$$COV(\mathbf{x}) = \mathbb{E}((\mathbf{x}-\mathbb{E}(\mathbf{x}))(\mathbf{x}-\mathbb{E}(\mathbf{x}))^\dagger)$$</span></p> <p>In element form I can write it as:</p> <p><span class="math-container">$$COV(x_i,x_j) = \mathbb{E}((x_i-\mathbb{E}(x_i))(x_j-\mathbb{E}(x_j))^*), \quad 1\leq i,j\leq n$$</span></p> <p>With this definition I think I need to have <span class="math-container">$k$</span> images or vectors, resulting in <span class="math-container">$k$</span> samples for each pixel <span class="math-container">$x$</span>, to compute the covariance matrix. I wrote an example code that generates <span class="math-container">$50$</span> images with Gaussian noise and finds the covariance matrix as defined above. However, I am not sure if this is correct or not. </p> <pre><code>x = Flatten[Table[Exp[-u^2/5 - v^2/5], {u, -10, 10, 1}, {v, -10, 10, 1}], 1]; Noise[x_] := # + RandomVariate[NormalDistribution[0.5, 0.2]] &amp; /@ x; NoisyDat = Table[Noise[x], {k, 1, 30}]; (* Generate a list of noisy image vectors *) MeanDat = Mean[NoisyDat]; (* E(x) *) OuterProd[list_] := KroneckerProduct[list - MeanDat, Conjugate[list - MeanDat]]; (* Outer product: (x- E(x))((x- E(x))*)^T *) OuterMeanRemoved = Map[OuterProd, NoisyDat]; COV = Mean[OuterMeanRemoved]; ArrayPlot[COV] </code></pre> <p>I have <span class="math-container">$30$</span> recorded images from which I want to find the noise covariance matrix and I am first trying to make a correct code by simulating Gaussian noise. Is this the correct way to do it?. </p> <p><strong>EDIT:</strong> Using <code>Covaiance[NoisyDat]</code> gives different result than the code above.</p> https://mathematica.stackexchange.com/q/193216 2 Matrix of deviate scores BeanSith https://mathematica.stackexchange.com/users/16154 2019-03-14T01:39:55Z 2019-03-14T01:39:55Z <p>I am trying to replicate some steps in Jonathon D. Brown's text "Linear Models in Matrix Form: A Hands-On Approach for the Behavioral Sciences" <a href="https://books.google.ca/books?id=POlVBgAAQBAJ&amp;pg=PA12&amp;dq=We%20begin%20by%20calculating%20a%20matrix%20of%20deviate%20scores,%20found%20by%20subtracting%20the%20column%20mean%20from%20each%20corresponding%20column%20entry%20in%20the%20original%20matrix.&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwjyuvyMvoDhAhXF5YMKHecgBpcQ6AEIKjAA#v=onepage&amp;q=We%20begin%20by%20calculating%20a%20matrix%20of%20deviate%20scores%2C%20found%20by%20subtracting%20the%20column%20mean%20from%20each%20corresponding%20column%20entry%20in%20the%20original%20matrix.&amp;f=false" rel="nofollow noreferrer">Brown, 2014, p.12</a>. I am creating a "matrix of deviate scores" and I am bothered that I am using the Transpose command twice:</p> <pre><code>X = {{1, 2, 6}, {4, 8, -4}, {5, 6, 5}, {4, 2, 3}} N[Transpose[Transpose[X] - Mean[X]]] </code></pre> <p>I realize this is artificial; but I am trying to "color within the lines" of the theme of "Matrix Form". I muddled about with code such as:</p> <pre><code>onesMat = ConstantArray[1, {4, 3}] xmeans = N[Mean[X]] Xmeans = Transpose[Transpose[onesMat]*xmeans] X - Xmeans </code></pre> <p>However, the former code snippet is briefer. But, still, I seem to be artificially transposing the covariate matrix into a 3 row by 4 column matrix in order to subtract the means and then transposing again to regain the 4 x 3 covariate matrix structure. Is this just what it is? Or am I abusing Mathematics's syntax for matrices and vectors? </p> https://mathematica.stackexchange.com/q/192468 2 How to generate a matrix with certain conditions tiffany https://mathematica.stackexchange.com/users/63199 2019-03-02T15:21:12Z 2019-03-03T02:24:14Z <p>I want to generate a <span class="math-container">$n \times n$</span> matrix.</p> <ol> <li>I want the diagonal entries to be all 0</li> <li>I want a random choice of matrix elements with 0 or 1.</li> <li>The probability of having a 1 as a matrix element is <span class="math-container">$1/m$</span> and the probability of having a 0 as a matrix element is <span class="math-container">$1-1/m$</span>.</li> </ol> <p>I used the following command but it is wrong. </p> <pre><code>A[n_, m_] :=Table[If[i == j, 0,RandomVariate[BernoulliDistribution[m],{n,n}]]] </code></pre> <p>And I tried to test this command with n=4, m=0.4 but it didn't work.</p> <p>Could anyone kindly tell me how to do this please? Thank you!</p> https://mathematica.stackexchange.com/q/191250 0 Calculating the probability of hitting, Fisher linear discriminant Aleksei Zhidkov https://mathematica.stackexchange.com/users/62858 2019-02-10T15:52:01Z 2019-02-11T23:11:56Z <p>I have 4 data matrix, which are described two parameters. <code>(M1, M2, M3, M4)</code>. So I made transition from base parameters to principal components and displayed them in the space of new signs. I need help in calculating the probability of hitting a value in an area, example <code>M1</code> (red triangles on picture). And how to calculate the straight line separating the plane? I want to separate the M1 from the other matrices, so only two classes can be considered, example M1 and M2. (M1 - values with no defects, others - with defects). Example, I want say, that if y1 and y2 will 0 and -0.5, then the probability that the object has no defect is 90%.</p> <pre><code>M1={{0.07, 0.09}, {0.09, 0.04}, {0.06, 0.5}, {0.05, 0.1}, {0.08, 0.07}, {0.1, 0.05}, {0.12, 0.14}, {0.09, 0.12}, {0.1, 0.07}, {0.04, 0.1}, {0.06, 0.1}, {0.09, 0.3}} M2={{0.330347, 0.66736}, {0.283692, 0.59829}, {0.230995, 0.549643}, {0.353132, 0.711974}, {0.19679, 0.360885}, {0.268241, 0.505476}, {0.625252, 0.87299}, {0.732484, 0.939535}, {0.499508, 0.728917}, {0.646746, 0.870734}, {0.684303, 0.818975}, {0.448433, 0.644385}} M3={{0.569952, 0.838573}, {0.451936, 0.801501}, {0.228608, 0.547101}, {0.161797, 0.345203}, {0.116077, 0.235871}, {0.124553, 0.249579}, {0.115536, 0.233847}, {0.114552, 0.23632}, {0.126844, 0.269214}, {0.158276, 0.340853}, {0.204667, 0.474522}, {0.263137, 0.576838}} M4={{0.136822, 0.317039}, {0.198275, 0.482787}, {0.301204, 0.606273}, {0.424842, 0.540176}, {0.222032, 0.355069}, {0.12944, 0.2534}, {0.192282, 0.487613}, {0.230306, 0.517964}, {0.155553, 0.30778}, {0.205009, 0.476323}, {0.211225, 0.496847}, {0.146949, 0.255088}} data={{0.330347, 0.66736}, {0.283692, 0.59829}, {0.230995, 0.549643}, {0.353132, 0.711974}, {0.19679, 0.360885}, {0.268241, 0.505476}, {0.625252, 0.87299}, {0.732484, 0.939535}, {0.499508, 0.728917}, {0.646746, 0.870734}, {0.684303, 0.818975}, {0.448433, 0.644385}, {0.569952, 0.838573}, {0.451936, 0.801501}, {0.228608, 0.547101}, {0.161797, 0.345203}, {0.116077, 0.235871}, {0.124553, 0.249579}, {0.115536, 0.233847}, {0.114552, 0.23632}, {0.126844, 0.269214}, {0.158276, 0.340853}, {0.204667, 0.474522}, {0.263137, 0.576838}, {0.136822, 0.317039}, {0.198275, 0.482787}, {0.301204, 0.606273}, {0.424842, 0.540176}, {0.222032, 0.355069}, {0.12944, 0.2534}, {0.192282, 0.487613}, {0.230306, 0.517964}, {0.155553, 0.30778}, {0.205009, 0.476323}, {0.211225, 0.496847}, {0.146949, 0.255088}, {0.07, 0.09}, {0.09, 0.04}, {0.06, 0.5}, {0.05, 0.1}, {0.08, 0.07}, {0.1, 0.05}, {0.12, 0.14}, {0.09, 0.12}, {0.1, 0.07}, {0.04, 0.1}, {0.06, 0.1}, {0.09, 0.3}} builtinscores = PrincipalComponents[data, Method -&gt; "Correlation"]; manscores = Standardize[data].Transpose[Eigenvectors[Correlation[data]]]; ListPlot[Partition[builtinscores[[All, 1 ;; 2]], 12], Frame -&gt; True, PlotMarkers -&gt; {Automatic, 20}, LabelStyle -&gt; Directive[Black]] </code></pre> <p><a href="https://i.stack.imgur.com/Nx4aH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nx4aH.jpg" alt="enter image description here"></a></p> https://mathematica.stackexchange.com/q/189071 0 How to write a command that produces such a matrix that takes n as an argument, where every node has probability 1/m [closed] Mithali https://mathematica.stackexchange.com/users/62271 2019-01-08T19:18:44Z 2019-01-08T21:52:23Z <p><a href="https://i.stack.imgur.com/adijR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/adijR.png" alt="This is the complete question"></a></p> <p>Please help in answering the questions above on mathematica. Which commands do I use to answer the question?</p> https://mathematica.stackexchange.com/q/186553 1 Computing the PDF of a WishartMatrixDistribution. Is this a limitation in the PDF Mathematica function? An old man in the sea. https://mathematica.stackexchange.com/users/11313 2018-11-23T09:44:07Z 2019-02-24T02:36:02Z <p>I'm trying to compute the pdf of a wishart distribution.</p> <p>Why does </p> <pre><code>PDF[WishartMatrixDistribution[10, IdentityMatrix], 2*IdentityMatrix] </code></pre> <p>not return a number? The output I get is exactly the same line of code... There are no error warnings, nothing. </p> https://mathematica.stackexchange.com/q/186001 1 how to derive a conditional covariance matrix (symbolically) Tugrul Temel https://mathematica.stackexchange.com/users/60365 2018-11-14T22:56:43Z 2019-02-24T02:35:25Z <p>I have searched the database of the questions asked in this forum but to my surprise I could not find any satisfactory answer to my relatively simple question. </p> <p>Given a vector of random variables <code>(X1,X2,X3,X4)</code> all having a normal distribution with mean <code>(m1,m2,m3,m4)</code> and variance <code>(v1,v2,v3,v4)</code>. Each random variable is respectively conditioned to signals <code>(s1,s2,s3,s4)</code>. I can compute manually covariance, say, between <code>X1</code> and <code>X2</code> as:</p> <blockquote> <p>Cov(X1,X2|s1,s2)=E{(X1-m1s1)(X2-m2s2)|s1,s2}</p> <p>where conditional mean of <code>X1</code> given <code>s1</code> is denoted by </p> <p><code>m1s1</code> = E[X1|s1] and similarly, <code>m2s2</code> = E[X2|s2] and E[X1X2|s1,s2] = <code>Integrate[X1X2, p(X1X2|s1,s2), {X1, -inf, inf},{X2, -inf, inf}]</code></p> </blockquote> <p>I can of course go ahead and derive all the elements of <code>conditional covariance matrix</code>. I thought that <code>Mathematica</code> can derive this conditional covariance matrix.</p> <p>My ultimate objective is to calculate with a given set data the following ratio:</p> <blockquote> <p>Det[Covariance matrix for X]/Det[Conditional covariance matrix X|S]</p> </blockquote> <p>Any idea about how to achieve this?</p> https://mathematica.stackexchange.com/q/166686 2 Find rank of the row nearest to random variable SAAN https://mathematica.stackexchange.com/users/7872 2018-02-26T14:49:18Z 2018-02-27T14:43:30Z <p>I have following code</p> <pre><code>dist[a_, b_] = WeibullDistribution[a, b]; data = Table[ j = Table[i = RandomVariate[dist[2, 1], {3, 3}], {i, 1, 4}], {j, 1, 3}] </code></pre> <p>above code generates 3*3 matrix four times and repeat all process three times. Next I want to grab the nearest value in each row as compare to random number with its rank (Minimum to maximum) in each row </p> <pre><code>data1 = Table[ Table[Table[ Nearest[data[[k, i, j]], RandomVariate[dist[2, 1]]], {j, 1, 3}], {i, 1, 4}], {k, 1, 3}] </code></pre> <p>Above code grabs the nearest value of the random number in each row. But I have no idea how to separate first, second and third ranked values. e.g in data1 we get total 36 values, some of them first ranked, some second and remaining third ranked. </p> https://mathematica.stackexchange.com/q/162829 4 Sample matrix indices in proportion to the matrix element values Tomi https://mathematica.stackexchange.com/users/36939 2017-12-30T22:43:58Z 2017-12-31T02:40:56Z <p>I have a 4 by 4 array which has a probability associated with each point. </p> <pre><code>{{0., 0., 0., 0.9}, {0., 0.05, 0., 0.}, {0., 0., 0., 0.}, {0., 0., 0.05, 0.}} </code></pre> <p>I want to sample the indices according to the probability/value at those indices in the matrix. </p> <p>I should find point <code>{1,4}</code> many more times than I should <code>{4,3}</code> or <code>{2,2}</code>. </p> <p>How can I sample the index where the matrix values correspond to the probability of finding that index?</p> https://mathematica.stackexchange.com/q/155467 11 How can I reproduce the result of PrincipalComponents HyperGroups https://mathematica.stackexchange.com/users/6648 2017-09-11T03:37:00Z 2019-08-12T09:32:05Z <pre><code>matrix = N[{{1, 2}, {2, 3}, {4, 10}}] {{1., 2.}, {2., 3.}, {4., 10.}} res1 = PrincipalComponents[matrix, Method -&gt; "Correlation"] {{1.10388, 0.130549}, {0.478746, -0.170139}, {-1.58262, 0.0395904}} res2 = PrincipalComponents[matrix, Method -&gt; "Covariance"] {{3.27053, 0.285293}, {1.99969, -0.335165}, {-5.27023, 0.0498715}} res3 = PrincipalComponents[Standardize @ matrix, Method -&gt; "Covariance"] {{1.10388, 0.130549}, {0.478746, -0.170139}, {-1.58262, 0.0395904}} </code></pre> <p>Here, you see that <code>res1 == res3</code>. My question is, how can I get <code>res2</code> manually like the following:</p> <pre><code>eigenVectors = Eigenvectors @ Covariance[Standardize @ matrix]; Standardize[matrix].Transpose[eigenVectors] {{-1.10388, 0.130549}, {-0.478746, -0.170139}, {1.58262, 0.0395904}} </code></pre> https://mathematica.stackexchange.com/q/153734 2 Creating a contingency table Mudy Fa https://mathematica.stackexchange.com/users/51615 2017-08-14T15:58:59Z 2018-01-04T20:11:35Z <p>I need help in creating a function for a contingency table... I imagined to create it like this... </p> <pre><code>Contingency[a_] := Module[{n = Dimensions[a][], m = Dimensions[a][], ...}, ... ] </code></pre> <p><code>a</code> is a $n \times m$ matrix with frequency distribution.</p> <p>Is this kinda way possible?</p> https://mathematica.stackexchange.com/q/153560 0 Rewriting a given multivariate PDF so that it has the form of multivariate normal pms https://mathematica.stackexchange.com/users/5501 2017-08-11T11:49:51Z 2017-08-13T11:42:52Z <p>Say, we are given a probability distribution function that we suspect is a multivariate normal. In Mathematica, how can we rewrite such PDF to get its covariance matrix and vector of means?</p> <p>For instance, we have the following PDF:</p> <p>$$p(x,y,z) = \frac{\exp \left(\frac{1}{2} \left(\frac{\left(y-x r_{\text{xy}}\right){}^2}{r_{\text{xy}}^2-1}+\frac{z \left(z-x r_{\text{xz}}\right)}{r_{\text{xz}}^2-1}+\frac{x \left(x-z r_{\text{xz}}\right)}{r_{\text{xz}}^2-1}\right)\right)}{2 \sqrt{2} \pi ^{3/2} \sqrt{\left(r_{\text{xy}}^2-1\right) \left(r_{\text{xz}}^2-1\right)}}$$</p> <p>What is its covariance matrix and the vector of means? How to find them for any PDF that we suspect is a normal distribution?</p> https://mathematica.stackexchange.com/q/152987 1 Is this a Symmetric Matrix or not? An old man in the sea. https://mathematica.stackexchange.com/users/11313 2017-08-03T12:42:54Z 2017-11-13T00:32:24Z <p>I've generated the <code>cov</code> matrix in the following way:</p> <pre><code>kernel[x1_, x2_] := Exp[-1/2*Norm[x1 - x2]^2]; Xtest = Range[-5, 5, 2]; n = Length[Xtest]; Xtrain = RandomReal[{-5, 5}, 5]; kmat[x1_, x2_] := Module[{mat}, n = Length[x1]; n2 = Length[x2]; mat = ConstantArray[0, {n, n2}]; For[i = 1, i &lt;= n, i++, For[j = 1, j &lt;= n2, j++, mat[[i, j]] = kernel[x1[[i]], x2[[j]]]; ]; ]; mat ]; m = kmat[Xtest, Xtrain] // N[#, {Infinity, 1000}] &amp;; mean = (m.Inverse[kmat[Xtrain, Xtrain]].fobs) // N[#, {Infinity, 1000}] &amp;; cov = (kmat[Xtest, Xtest] - m.Inverse[kmat[Xtrain, Xtrain]].Transpose[m]) // N[#, {Infinity, 1000}] &amp;; </code></pre> <p>Theoretically, <code>cov</code> should be symmetric. However, when I do <code>SymmetricMatrixQ[cov]</code>, it returns <code>False</code>. It's the <code>m.Inverse[kmat[Xtrain, Xtrain]].Transpose[m]</code> which returns a non-symmetric matrix when it should not. When I do <code>SymmetricMatrixQ[Inverse[kmat[Xtrain, Xtrain]]]</code> I get True.</p> <p>My objective is to be able to run <code>RandomVariate[MultinormalDistribution[mean, cov], 4];</code> which I can't, since <em>Mathematica</em> thinks it's not symmetric or PD...</p> <p>Any help would be appreciated.</p> https://mathematica.stackexchange.com/q/135800 1 Construct a univariate function pertaining to the space of $2 \times 2$ real matrices [closed] Paul B. Slater https://mathematica.stackexchange.com/users/29989 2017-01-20T04:33:58Z 2017-01-20T23:34:09Z <p>Consider the space of uniformly distributed $2 \times 2$ real matrices \begin{equation} \left( \begin{array}{cc} a &amp; b \\ c &amp; d \\ \end{array} \right), \end{equation} with the restriction that the larger of the matrices' two singular values (that is, operator [Schatten-$\infty$] norm) is bounded above by 1. Now, the (apparently very challenging) problem is to construct the function $\tilde{\chi}_1(\epsilon)= \tilde{\chi}_1(1/\epsilon)$, for $\epsilon &gt;0$, giving the probability that the companion matrix \begin{equation} \left( \begin{array}{cc} a &amp; b \epsilon \\ \frac{c}{\epsilon } &amp; d \\ \end{array} \right) \end{equation} also has its larger singular value bounded above by 1. This problem has very recently been solved (I can give the reference), but I would hope to encourage a (fresh) Mathematica formulation/solution. This might be helpful in trying to extend the problem to the (presently unsolved) one of constructing the function $\tilde{\chi}_2(\epsilon)$ corresponding to the $2 \times 2$ matrices with complex entries.</p> <p>This problem can be easily formulated in a few lines of Mathematica code (using the SingularValueList and Integrate--with a Boole [4-dimensional] condition--commands, for instance). However, it appears to be--at least, with my somewhat limited resources--too computationally demanding to arrive at a solution. Perhaps there are other set-ups/transformations/workarounds to investigate. In any case, I thought it might be some "fun" to try for a few Mathematica devotees--and an interesting test for the power/limitations of Mathematica. Further, let me point out the reference in which the solution was found (eq. (9) in <a href="https://arxiv.org/pdf/1610.01410.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1610.01410.pdf</a>, also the succinct Conclusion section [p. 17]). It is not clear if these authors employed Mathematica, and, if so, in what manner.</p> <p>I added this last paragraph in response to the question having been put on hold as requiring "either advice from Wolfram support or the services of a professional consultant".</p> <p>Here (per the comment of Jim Baldwin) is some Mathematica coding for the problem:</p> <pre><code>H1 = {{a, b}, {c, d}}; H2 = {{a, e b}, {c/e, d}}; v1 = Expand[PowerExpand[FullSimplify[(SingularValueList[H1] /. Conjugate[f_] -&gt; f)[]]^2]] v2 = Expand[PowerExpand[FullSimplify[(SingularValueList[H2] /. Conjugate[f_] -&gt; f)[]]^2]] s1 = Integrate[Boole[v1 &lt; 1], {a, -1, 1}, {b, -1, 1}, {c, -1, 1}, {d, -1, 1}] s2 = Integrate[Boole[v1 &lt; 1 &amp;&amp; v2 &lt; 1 &amp;&amp; e &gt; 0], {a, -1, 1}, {b, -1, 1}, {c, -1, 1}, {d, -1, 1}] </code></pre> <p>The computation of the variable "s2" should give the desired function $\tilde{\chi}_1(\epsilon)$, but it seems too demanding a task to complete. ("H1" and "H2" are the indicated matrices, while "v1" and "v2" are the squares of the corresponding larger singular values. These serve as constraints in the four-dimensional integrations.) Perhaps the CylindricalDecomposition command might be helpful, but some preliminary efforts along such lines have not been fruitful.</p> https://mathematica.stackexchange.com/q/123702 2 Can't define average for my matrix Amelia Henkel https://mathematica.stackexchange.com/users/42201 2016-08-11T16:38:23Z 2016-08-12T03:44:59Z <p>I have a list defined as <code>MarsDisk</code> which, in <code>MatrixForm</code>, has 17 rows and 3 columns, which hold information about a photon energy level, photon flux, and uncertainty in the flux. I'm interested in taking the average of the energy levels and executed the following:</p> <pre><code>Average[MarsDisk[[i, 2]]] </code></pre> <p>and I received this error message:</p> <blockquote> <p>Part::partw: Part 18 of {{0.8725,0.5,0.5},{0.817,0.4,0.4},[continues with the rest of my data points]} does not exist. >></p> </blockquote> <p>I don't get why this doesn't work because I never asked for the 18th element. </p> <p>Then, I tried to create a For loop instead:</p> <pre><code>ψ = Array[c, {17, 2}]; For [i = 1, i &lt;= 17, i++, ψ[[i, 1]] = Avg[MarsDisk[[i, 1]]]; ψ[[i, 2]] = Avg[MarsDisk[[i, 2]]]] </code></pre> <p>which gave no error message but when I executed <code>ψ</code> it printed all elements as <code>{Avg[0.8725], Avg[0.5]}</code>, etc. So neither way worked.</p> <p>Can anyone show me a way to successfully quantify the averages for each column of information in my matrix?</p> https://mathematica.stackexchange.com/q/104868 2 Generate a simulated covariance matrix Sprog https://mathematica.stackexchange.com/users/37135 2016-01-26T01:42:14Z 2017-01-02T23:16:45Z <p>I am working on a problem where one of the input variables is the level of covariance between the entries in a particular matrix. To simulate this problem in <em>Mathematica</em>, I will need to feed my code some simulated covariance matrices. </p> <p>I will, therefore, need to generate these pretend covariance matrices for dummy data for a range of specified mean levels of covariance.</p> <p>Is there a way to do this in <em>Mathematica</em>?</p> https://mathematica.stackexchange.com/q/104535 11 How to interpret the results of PCA madeinQuant https://mathematica.stackexchange.com/users/34755 2016-01-21T13:31:04Z 2017-08-20T07:14:40Z <p>There is a larger matrix (1500 rows x 40 columns), 1500 observations x 40 variables. then I follow the procedures of PCA(Principal components analysis), 1. find correlation 2. find eigenvalues 3. find Eigenvectors I have got the result (Mathematica)</p> <pre><code>dataCorrEigenvalues/Total@dataCorrEigenvalues {0.647833, 0.128731, 0.0843738, 0.0519215, 0.0246577, 0.018331, \ 0.0100494, 0.00657219, 0.0054721, 0.00373078, 0.00310175, 0.00244999, \ 0.0022292, 0.00190861, 0.00166728, 0.00124446, 0.00113064, \ 0.00093684, 0.000673087, 0.000579798, 0.00049716, 0.000425554, \ 0.000371012, 0.000261027, 0.000225517, 0.000173631, 0.000133479, \ 0.000128954, 0.000103792, 0.0000853669} FoldList[Plus, dataCorrEigenvalues/Total@dataCorrEigenvalues] {0.647833, 0.776564, 0.860938, 0.91286, 0.937517, 0.955848, 0.965898, \ 0.97247, 0.977942, 0.981673, 0.984775, 0.987225, 0.989454, 0.991362, \ 0.99303, 0.994274, 0.995405, 0.996342, 0.997015, 0.997595, 0.998092, \ 0.998517, 0.998888, 0.999149, 0.999375, 0.999548, 0.999682, 0.999811, \ 0.999915, 1.} </code></pre> <p>As I know the first 6 components explain about 95% of the variability, However, I don't understand how to use these components for data analysis.</p> <p>The projection Matrix "w" as the following</p> <pre><code>w = dataCorrEigenvectors[[All, 1 ;; 4]]; </code></pre> <p>I try to calculate the dot product of W against data, however, it seems the result is not same as expectation.</p> <pre><code>PC5 = data.w; </code></pre> <p>Please feel free to command and advise what I should do. Thank you.</p> https://mathematica.stackexchange.com/q/103502 3 How to form binary matrix with constants and given probability? Mamoona https://mathematica.stackexchange.com/users/36691 2016-01-06T20:34:52Z 2016-01-06T22:05:46Z <p>I want to create a binary matrix of 6x3 in which six elements (m13,m23, m33, m42, m52, m61) are constant and assigned '0' value; whereas for other twelve elements (m11, m12, m21, m22, m31,m32, m41, m43, m51, m53, m62, m63) I want to assign value of "1" with the probability of 0.5. Given below my effort, I had made yet.</p> <pre><code>mat = Table[Subscript[m, i, j], {i, 6}, {j, 3}]; mat // MatrixForm n = Round[18*0.5]; k = RandomSample[{mat[[1, 1]], mat[[1, 2]], mat[[1, 3]], mat[[2, 1]], mat[[2, 2]], mat[[2, 3]], mat[[3, 1]], mat[[3, 2]], mat[[3, 3]], mat[[4, 1]], mat[[4, 2]], mat[[4, 3]], mat[[5, 1]], mat[[5, 2]], mat[[5, 3]], mat[[6, 1]], mat[[6, 2]], mat[[6, 3]]}, n] </code></pre> https://mathematica.stackexchange.com/q/102351 0 How to map the second highest value in each row of a matrix Bjoj https://mathematica.stackexchange.com/users/36437 2015-12-18T10:34:46Z 2015-12-19T04:41:57Z <p>I have a matrix with three or more columns called <code>mat</code>. I would like to map the second highest value in each row of <code>mat</code> into its own list, and then take the mean of all the 2nd highest values. With only two rows, I managed to do this by using <code>Min</code>, like this:</p> <pre><code>h = Map[Min, mat]; Mean[h] </code></pre> <p>How can I achieve this with three or more rows? I have tried to use <code>RankedMin</code> and <code>RankedMax</code>, but could not get them to work with the <code>Map</code> function.</p> https://mathematica.stackexchange.com/q/97960 4 What is the Mathematica equivalent for corr2? Mushegh https://mathematica.stackexchange.com/users/10945 2015-10-27T14:35:18Z 2015-10-28T05:43:33Z <p>What is the <em>Mathematica</em> equivalent for MATLAB's <a href="http://www.mathworks.com/help/images/ref/corr2.html" rel="nofollow"><code>corr2</code></a>, which gives the correlation coefficient of two 2D matrices?</p> https://mathematica.stackexchange.com/q/97861 1 Sampling from an one-dimensional Wishart [closed] Titus https://mathematica.stackexchange.com/users/30703 2015-10-26T08:19:22Z 2015-10-26T09:35:24Z <p>I searched for a similar question but could not find one.</p> <p>My problem is sampling from an one-dimensional Wishart distribution. The Mathematica specification is</p> <pre><code>RandomVariate[WishartDistribution[Y,n]] </code></pre> <p>where $Y$ is a $p\times p$ matrix and $n$ is degrees of freedom, $n&gt;p-1$ . So I can sample from a 2-dimensional distribution</p> <pre><code>Needs["MultivariateStatistics`"] RandomVariate[WishartDistribution[{{1,0},{0,1}},3]] </code></pre> <p>but when I set $p=1$ (so that $Y$ is just a number/ a $1\times1$ matrix) I get an error message. My problem is not $n$ but Mathematica not identifying $1 \times 1$ matrices as parameters for a Wishart.</p> <p>Other than switching to a Chi squared distribution, does anyone know of a way around this? Did I make a mistake somewhere?</p> <p>Thank you very much in advance.</p> https://mathematica.stackexchange.com/q/51965 1 Generic Matrices pentadecagon https://mathematica.stackexchange.com/users/16285 2014-07-02T13:42:00Z 2016-06-29T01:12:52Z <p>A multivariate Gaussian distribution for $k$ dimensions looks like this:</p> <p>$$\frac{1}{\sqrt{(2\pi)^k|\mathbf \Sigma|}}\mathbf e^{ -\frac 1 2 \mathbf x^T \mathbf \Sigma^{-1}\mathbf x }$$ </p> <p>$\mathbf x$ is a $k$-dimensional vector, and $\mathbf \Sigma$ a $k$ by $k$ symmetric positive definite matrix. Now I want Mathematica to integrate this expression over all $\mathbf x$, the result should be 1. How can I do this? Nothing else is known, in particular we don't know the number of dimensions.</p> https://mathematica.stackexchange.com/q/51345 2 Measures of association, Concordant and Discordant user3646666 https://mathematica.stackexchange.com/users/16096 2014-06-23T16:03:17Z 2014-06-24T16:58:14Z <p>I want to calculate concordant and discordant pairs on nx2 tables.</p> <hr> <pre><code>| Value1 | Value2 | |_____________|__________| | 0.3434 | 1 | | 0.2132 | 2 | | 0.3 | 3.3245 | | 0.6 | 0.12321 | | 0.745234 | 523 | | 4 | 0.2134 | | 3 | 111 | | . | . | | . | . | | . | . | | . | . | | . | . | |_____________|__________| </code></pre> <p>This is a n x 2 table. How can I calculate concordant and discordant pairs? I am sorry I can't find suitable question tag.</p> https://mathematica.stackexchange.com/q/43447 2 Extracting Imported Information user12779 https://mathematica.stackexchange.com/users/12779 2014-03-05T11:55:19Z 2014-03-06T12:49:39Z <p>What I want to do is to compare the relationship between the consumption of say beer and wine using scatter plots where each point designates the particular country (preferably named). I have tried to part the information - so that each particular column becomes a vector - using the function Part without any success. I find this bewildering given that <code>Dimension[data]</code> yields the result <code>{19,4}</code>.</p> <p>For those of you that are familiar with R: What I would like to do to is to part the information, like this</p> <pre><code> &gt; Country &lt;-data$Country &gt; Beer &lt;-data$Beer &gt; Wine &lt;-data$Wine &gt; HardLiquid &lt;-data$HardLiquid </code></pre> <p>and then produce scatter plots which names every country. </p> <pre><code>&gt; plot(Beer,Wine,type="n") &gt; text(Beer,Wine,Country) </code></pre> <p><strong>Extra</strong> </p> <p>This is the data that I am working on in <em>Mathematica</em>:</p> <pre><code>data = {{"Country", "Beer", "Wine", "Hard Liquid"}, {"Sweden", 56., 16., 2.9}, {"Danmark", 98., 32., 2.7}, {"Finland", 79., 10., 5.7}, {"Norway", 56., 11., 2.4}, {"Belgium", 98., 30., 2.6}, {"France", 47., 41., 7.2}, {"Irland", 155., 13., 5.3}, {"Italy", 29., 54., 2.7}, {"Netherlands", 80., 20., 4.7}, {"Switzerland", 57., 42., 2.4}, {"Kingdom United", 20., 97., 3.9}, {"Germany", 26., 119., 5.3}, {"Austria", 36., 106., 3.2}, {"USA", 85., 7., 4.8}, {"Canada", 70., 10., 4.3}, {"Australia", 21., 89., 2.6}, {"Nya Zeeland", 19., 78., 2.3}, {"Japan", 55., 10., 8.2}} </code></pre> https://mathematica.stackexchange.com/q/37043 4 Calculate the covariance of a large matrix phidelio https://mathematica.stackexchange.com/users/5589 2013-11-14T20:49:48Z 2014-09-20T21:14:20Z <p>I have to compute the covariance of 50 very large integer matrices (2500x2000 elements). However, according to my estimation this will take around 10 days. Do you have any ideas how to speed things up?</p> https://mathematica.stackexchange.com/q/21396 6 How to use a 3×3 covariance matrix to plot an error ellipsoid? K-1 https://mathematica.stackexchange.com/users/795 2013-03-15T11:00:35Z 2018-03-22T14:42:40Z <p>I have a 3×3 error covariance in <em>Mathematica</em>, but I don't know how to use it for plotting the error ellipsoid. It would be great if you can show me how I can do that for the below covariance matrix:</p> <pre><code>CovMat= {{88.5333, -33.6, -5.33333}, {-33.6, 15.4424, 2.66667}, {-5.33333, 2.66667, 0.484848}} eigenvalues= {0.0098, 0.4046, 104.7} eigenvectors= {{0.93, 0.36, -0.03}, {-0.36, 0.9, -0.23}, {-0.06, 0.23, 0.97}} </code></pre> <p>And as the last question, how can I project this ellipsoid onto 2D planes?</p> https://mathematica.stackexchange.com/q/19009 0 Discrete 3D plots of median ratios of two 2D matrices of lists of values JohnnyM https://mathematica.stackexchange.com/users/5627 2013-02-04T13:03:37Z 2013-02-04T15:29:18Z <p>Lets say I have 2 2D arrays where each cell contains a list of values:</p> <p>Example:</p> <pre><code>data_1[][] = {1,2,3} data_1[][] = {1,2,3} data_1[][] = {1,2,3} data_1[][] = {1,2,3} data_2[][] = {2,4,6} data_2[][] = {3,6,9} data_2[][] = {4,8,12} data_2[][] = {5,10,15} </code></pre> <p>What's a neat way to make a discrete 3D plot of the ratio between medians of these data sets (assuming that the indices always mach, but can represent any value, not only consecutive natural numbers).For the following data I would expect a plot of:</p> <pre><code>x,y,z 1,1,2 1,2,3 2,1,4 2,2,5 </code></pre> https://mathematica.stackexchange.com/q/13862 3 Mathematica Complains about Non Symmetric Covariance matrix, when it's not the case pablo https://mathematica.stackexchange.com/users/4923 2012-10-29T16:19:27Z 2012-10-29T16:30:59Z <p>I was doing some fitting with Mathematica7 using NonlinearModelFit. It's quite long the program to do the fit and that's why I am not displaying here ...</p> <p>It goes ok, and I can get the fit parameters as well as the covariance matrix</p> <pre><code>In:= {Subscript[a, 0], Subscript[a, 1]}/.fitCCBA34["BestFitParameters"] In:= fitCCBA34["CovarianceMatrix"] </code></pre> <p>When testing it is Symmetric, it goes ok, as should be by construction of the matrix:</p> <pre><code>In:= SymmetricMatrixQ[b] Out= True </code></pre> <p>However, it turns out, that, when trying to get some Multinormal distribution of the parameters, it complains about the matrix which is non-symmetric ...</p> <pre><code>In:= MultinormalDistribution[{Subscript[a, 0], Subscript[a, 1]} /. fitCCBA34[ "BestFitParameters"], {{fitCCBA34["CovarianceMatrix"][[1, 1]], fitCCBA34["CovarianceMatrix"][[1, 2]]}, {fitCCBA34[ "CovarianceMatrix"][[2, 1]], fitCCBA34["CovarianceMatrix"][[2, 2]]}}] During evaluation of In:= MultinormalDistribution::cmsym: The covariance matrix must be symmetric. &gt;&gt; Out= MultinormalDistribution[{0.275723, 0.246948}, {{0.00001521, -0.0000535383}, {-0.0000535383, 11.6061}}] </code></pre> <p>Of course, if now you do copy paste, it works perfectly, so I guess it's some numerical issue, any clue about how to fix this? I need to encode this inside a function rather than copy paste each time ...</p> <p>Thanks in advance!</p> https://mathematica.stackexchange.com/q/11588 4 Total Variation Distance of probability matrix whynot https://mathematica.stackexchange.com/users/2917 2012-10-05T00:01:00Z 2015-11-24T07:41:07Z <p>How can I calculate the <a href="http://en.wikipedia.org/wiki/Total_variation_distance_of_probability_measures" rel="nofollow">Total Variation Distance</a> of a transition Matrix? is there any built in function? I've searched all documentation and haven;t found anything.</p> <p>** More information:</p> <p>Let me try to explain it better. let's say we have a transition matrix ($P$), $4\times4$ that describes the probability of going from a, b, c and d to a, b, c or d in 1 step.</p> <p>We can calculate the stationary distribution of $P$, and that's called $\pi$ in the following equation, and $P_{yx}$ is the probability of going from state $y$ to state $x$ (a,b,c,d):</p> <p>$$\frac12\sum_x\left|P_{yx}^t-\pi(x)\right|$$</p> <p>What I want to do is calculate the Total Variation Distance of $P$ from $\pi$ after $n$ steps and starting on a given state.</p> <p>*** This is what I have so far:</p> <pre><code>M = {{0.3, 0, 0.5, 0.2}, {0, 0.4, 0.3, 0.3}, {0.3, 0.2, 0, 0.5}, {0.4, 0.1, 0, 0.5}} B = Transpose[M] N[B] // MatrixForm {eVals, eVecs} = Eigensystem[B] eVals // MatrixForm eVecs // MatrixForm eigenvector = eVecs[] Print["Stationary Distribution"]; eigenvector/Total[eigenvector] Print["M after 1 step"]; M2 = MatrixPower[M, 2] </code></pre>