Running a script once, reserves input parameters and outputs in Python's namespace, allowing you to check the state of these in the Interactive Window, IW, at any time. Often I save the results of the Interactive Window to document a particular case or change in state of one of the inputs. I was getting bored writing multiple print statements to try to remember what the inputs and outputs were. Moreover, I had documented all this in the script in the header.
I decided to combine the best of both worlds: 1) reduce the number of print statements; 2) retrieve the header information so I could check namespace and outputs in the IW which I could then save and/or print.
The following are the results of a demo scripts output which includes the input namespace and their meaning and the results for a run. The actual script is not relevant but I have included it anyways as an attachment. The example here is the result from PythonWin's IW. I did take a huge chunk out of the outputs to facilitate reading.
<SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">-</SPAN>
<SPAN class="punctuation token">:</SPAN>Script<SPAN class="punctuation token">:</SPAN> matrix_covariance_demo<SPAN class="punctuation token">.</SPAN>py
<SPAN class="punctuation token">:</SPAN>Author<SPAN class="punctuation token">:</SPAN> Dan<SPAN class="punctuation token">.</SPAN>Patterson@carleton<SPAN class="punctuation token">.</SPAN>ca
<SPAN class="punctuation token">:</SPAN>Modified<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2016</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">10</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">25</SPAN>
<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">:</SPAN>Notes<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">:</SPAN> Namespace<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">:</SPAN> x<SPAN class="punctuation token">,</SPAN> y x<SPAN class="punctuation token">,</SPAN>y values
<SPAN class="punctuation token">:</SPAN> xy_s X<SPAN class="punctuation token">,</SPAN>Y values zipped together forming a column list
<SPAN class="punctuation token">:</SPAN> x_m<SPAN class="punctuation token">,</SPAN> y_m means of X <SPAN class="operator token">and</SPAN> Y
<SPAN class="punctuation token">:</SPAN> x_t<SPAN class="punctuation token">,</SPAN> x_t X <SPAN class="operator token">and</SPAN> Y converted to arrays <SPAN class="operator token">and</SPAN> translated to form row arrays
<SPAN class="punctuation token">:</SPAN> s_x<SPAN class="punctuation token">,</SPAN> s_y sample std<SPAN class="punctuation token">.</SPAN> deviations
<SPAN class="punctuation token">:</SPAN> v_x<SPAN class="punctuation token">,</SPAN> v_y sample variances
<SPAN class="punctuation token">:</SPAN> cov_m numpy covariance matrix<SPAN class="punctuation token">,</SPAN> sample treatement<SPAN class="punctuation token">,</SPAN> see docs re<SPAN class="punctuation token">:</SPAN> ddof
<SPAN class="punctuation token">:</SPAN> Exy sum of the X_t<SPAN class="punctuation token">,</SPAN>Y_t products
<SPAN class="punctuation token">:</SPAN> cv_alt alternate method of calculating <SPAN class="string token">"cov_m"</SPAN> <SPAN class="keyword token">in</SPAN> terms of var<SPAN class="punctuation token">.</SPAN> etc
<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">:</SPAN> Useage<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">:</SPAN> Create a list of key values <SPAN class="keyword token">in</SPAN> execuation order<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">print</SPAN> using locals<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">:</SPAN> Syntax follows<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">:</SPAN> names <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"x"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"x"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"xy_s"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"x_m"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"y_m"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"xy_t"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"x_t"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"y_t"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"s_x"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"s_y"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"v_x"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"v_y"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"cov_m"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Exy"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"n"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"cv_alt"</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> names<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!s:<8}:\n {!s: <60}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>name<SPAN class="punctuation token">,</SPAN> locals<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">:</SPAN>References
<SPAN class="punctuation token">:</SPAN> http<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">//</SPAN>en<SPAN class="punctuation token">.</SPAN>wikipedia<SPAN class="punctuation token">.</SPAN>org<SPAN class="operator token">/</SPAN>wiki<SPAN class="operator token">/</SPAN>Pearson_product<SPAN class="operator token">-</SPAN>moment_
<SPAN class="punctuation token">:</SPAN> correlation_coefficient
<SPAN class="punctuation token">:</SPAN> http<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">//</SPAN>stackoverflow<SPAN class="punctuation token">.</SPAN>com<SPAN class="operator token">/</SPAN>questions<SPAN class="operator token">/</SPAN><SPAN class="number token">932818</SPAN><SPAN class="operator token">/</SPAN>retrieving<SPAN class="operator token">-</SPAN>a<SPAN class="operator token">-</SPAN>variables<SPAN class="operator token">-</SPAN>
<SPAN class="punctuation token">:</SPAN> name<SPAN class="operator token">-</SPAN><SPAN class="keyword token">in</SPAN><SPAN class="operator token">-</SPAN>python<SPAN class="operator token">-</SPAN>at<SPAN class="operator token">-</SPAN>runtime
<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">)</SPAN>
Results listed <SPAN class="keyword token">in</SPAN> order of execution<SPAN class="punctuation token">:</SPAN>
x <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">]</SPAN>
y <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">0.11</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.12</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.13</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.15</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.18</SPAN><SPAN class="punctuation token">]</SPAN>
xy_s <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.11</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.12</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.13</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.15</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.18</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
x_m <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">3.8</SPAN>
y_m <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0.138</SPAN>
x_t <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">2.800</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1.800</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.800</SPAN> <SPAN class="number token">1.200</SPAN> <SPAN class="number token">4.200</SPAN><SPAN class="punctuation token">]</SPAN>
y_t <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">0.028</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.018</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">0.008</SPAN> <SPAN class="number token">0.012</SPAN> <SPAN class="number token">0.042</SPAN><SPAN class="punctuation token">]</SPAN>
s_x <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">2.7748873851</SPAN>
s_y <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0.027748873851</SPAN>
v_x <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">7.7</SPAN>
v_y <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0.00077</SPAN>
cov_m <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">7.700</SPAN> <SPAN class="number token">0.077</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0.077</SPAN> <SPAN class="number token">0.001</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
Exy <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0.308</SPAN>
n <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">4</SPAN>
cv_alt<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">7.700</SPAN> <SPAN class="number token">0.077</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0.077</SPAN> <SPAN class="number token">0.001</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Now the code.... I have left out the scripts doc since I always like to keep a copy in the output so I don't forget what I used to produce it. The only real important parts are the list of names in the main part of the script and the lines in the __main__ section to process the locals() variable yes.
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> SNIP <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">from</SPAN> numpy<SPAN class="punctuation token">.</SPAN>linalg <SPAN class="keyword token">import</SPAN> linalg <SPAN class="keyword token">as</SPAN> nla
<SPAN class="keyword token">from</SPAN> textwrap <SPAN class="keyword token">import</SPAN> dedent
ft <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"bool"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">lambda</SPAN> x<SPAN class="punctuation token">:</SPAN> repr<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">.</SPAN>astype<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"int32"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"float"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"{: 0.3f}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">}</SPAN>
np<SPAN class="punctuation token">.</SPAN>set_printoptions<SPAN class="punctuation token">(</SPAN>edgeitems<SPAN class="operator token">=</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> linewidth<SPAN class="operator token">=</SPAN><SPAN class="number token">80</SPAN><SPAN class="punctuation token">,</SPAN> precision<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
suppress<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> threshold<SPAN class="operator token">=</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">,</SPAN>
formatter<SPAN class="operator token">=</SPAN>ft<SPAN class="punctuation token">)</SPAN>
script <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token"># .... Variables and calculations ....</SPAN>
x <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># x values</SPAN>
y <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">0.11</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.12</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.13</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.15</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.18</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># y values</SPAN>
xy_s <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>zip<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># stack together</SPAN>
x_m<SPAN class="punctuation token">,</SPAN> y_m <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>mean<SPAN class="punctuation token">(</SPAN>xy_s<SPAN class="punctuation token">,</SPAN> axis<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># get the means</SPAN>
xy_t <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>xy_s<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="punctuation token">[</SPAN>x_m<SPAN class="punctuation token">,</SPAN> y_m<SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># convert to an array and translate</SPAN>
x_t<SPAN class="punctuation token">,</SPAN> y_t <SPAN class="operator token">=</SPAN> xy_t<SPAN class="punctuation token">.</SPAN>T <SPAN class="comment token"># x,y coordinates, transposed array</SPAN>
s_x<SPAN class="punctuation token">,</SPAN> s_y <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>std<SPAN class="punctuation token">(</SPAN>xy_t<SPAN class="punctuation token">,</SPAN> axis<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> ddof<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># sample std. deviations</SPAN>
v_x<SPAN class="punctuation token">,</SPAN> v_y <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>var<SPAN class="punctuation token">(</SPAN>xy_t<SPAN class="punctuation token">,</SPAN> axis<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> ddof<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># sample variances</SPAN>
cov_m <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>cov<SPAN class="punctuation token">(</SPAN>x_t<SPAN class="punctuation token">,</SPAN> y_t<SPAN class="punctuation token">,</SPAN> ddof<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># covariance matrix</SPAN>
<SPAN class="comment token">#</SPAN>
<SPAN class="comment token"># .... alternate expressions of the covariance matrix ....</SPAN>
Exy <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sum<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>product<SPAN class="punctuation token">(</SPAN>xy_t<SPAN class="punctuation token">,</SPAN> axis<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># sum of the X_t,Y_t products</SPAN>
n <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>x_t<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN>
cv_alt <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN>v_x<SPAN class="punctuation token">,</SPAN> Exy<SPAN class="operator token">/</SPAN>n<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>Exy<SPAN class="operator token">/</SPAN>n<SPAN class="punctuation token">,</SPAN> v_y<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># create a list of key values in execution order format from locals()[name]</SPAN>
names <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"x"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"y"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"xy_s"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"x_m"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"y_m"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"x_t"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"y_t"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"s_x"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"s_y"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"v_x"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"v_y"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"cov_m"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Exy"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"n"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"cv_alt"</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#-------------------------</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n{}\n{})"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"-"</SPAN><SPAN class="operator token">*</SPAN><SPAN class="number token">70</SPAN><SPAN class="punctuation token">,</SPAN> __doc__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\nResults listed in order of execution:"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> names<SPAN class="punctuation token">:</SPAN>
args <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>name<SPAN class="punctuation token">,</SPAN> str<SPAN class="punctuation token">(</SPAN>locals<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN>name<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\n"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">","</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{!s:<6}.... {!s:}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Hope you find something useful in this. Send comments via email.