Variance matrix for a vector of i.i.d Normal Random Variables

Question: I'm working on a statistics problem:
Given $X = (X_1, X_2, X_3 ... X_n)$ where $X_i \sim N(\mu, \sigma^2)$for i = {1, ... , n} and each of the $X_i$ are i.i.d. Find $E[AX]$ and $Var(AX)$. We're also given: $$A = \begin{pmatrix}1 -\frac{1}{n} & -\frac{1}{n} & -\frac{1}{n} & ...\\-\frac{1}{n} & 1 -\frac{1}{n} & -\frac{1}{n}
\\-\frac{1}{n} & -\frac{1}{n} & 1 -\frac{1}{n}\\ ...& & & ...
\end{pmatrix},
AX = \begin{pmatrix}X_1 - \bar X \\ X_2 - \bar X
\\ X_3 - \bar X\\.\\.\\.
\end{pmatrix}
$$
We can rewrite A as $A = I - \frac{1}{n}$1 1'
Where 1 is a column vector of all 1's.
First $E[AX]$
$$E[AX] = E[\begin{pmatrix}X_1 - \bar X \\ X_2 - \bar X
\\ X_3 - \bar X\\.\\.\\.
\end{pmatrix}]$$
$$\bar X = \frac{1}{n} \sum_i X_i$$
$$E[\bar X] = \mu$$
Therefore,
$$E[AX] = E[\begin{pmatrix}X_1 - \bar X \\ X_2 - \bar X
\\ X_3 - \bar X\\.\\.\\.
\end{pmatrix}] = 0$$
Then $V(AX)$
Okay here's where I get a little lost:
My original approach is:
$$V(AX) = E[(AX - E[AX]) (AX - E[AX])^T] \\= E[(AX - 0) (AX - 0)^T] = E[(A X X^T A^T] = AE[XX^T]A^T$$
And I understand $$E[XX^T] = \begin{pmatrix}E X_1^2 & E X_1X_2 & E X_1X_3 &... \\E X_1X_2 &E X_2^2 & E X_2X_3
\\E X_1X_3 &E X_2X_3 & E X_3^2 \\ ... & & &...
\end{pmatrix}, $$
However, approaching it a little differently,
$$Var(AX) = AVar(X)A^T = A \begin{pmatrix}\sigma^2 & 0 & 0 &... \\0 &\sigma^2 & 0
\\0 & 0 & \sigma^2 \\ ... & & &...
\end{pmatrix}A^T$$
Where we used the fact that $X_i's$ are all i.i.d, so $Cov(X_i, X_j) = 0$ for $i \neq$ j
Which is what I'm so confused about, both of my approaches seem correct, but they can't be because $$E[XX^T] \neq Var(X)$$
However, from my approaches $$Var(AX) = AE[XX^T]A^T$$
and $$Var(AX) = AVar(X)A^T$$
So are they actually the same? I'm so confused. Any help untangling this confusion is greatly appreciated :-)
Additionally, why doesn't
$$Var(AX) = E[XX^T] = \begin{pmatrix}Cov(X_1 - \bar X, X_1 - \bar X) & Cov(X_1 - \bar X, X_2 - \bar X)  & Cov(X_1 - \bar X, X_3 - \bar X)  &... \\Cov(X_2 - \bar X, X_1 - \bar X) & Cov(X_2 - \bar X, X_2 - \bar X)  & Cov(X_2 - \bar X, X_3 - \bar X)
\\Cov(X_3 - \bar X, X_1 - \bar X) & Cov(X_3 - \bar X, X_2 - \bar X)  & Cov(X_3 - \bar X, X_3 - \bar X) \\ ... & & &...
\end{pmatrix}  = \begin{pmatrix}\sigma^2 & 0 & 0 &... \\0 &\sigma^2 & 0
\\0 &0 & \sigma^2 \\ ... & & &...
\end{pmatrix} = \sigma^2 I$$ the answer?
Answer: There are a couple of issues. First, in general
$$
V(Y) = E[(Y-EY)(Y-EY)^T]
$$
where $Y$ is a random vector. In particular
$$
V(AY) = AV(Y)A^T
$$
Putting $Y=X$ as in your example we get that
$$
V(AX) = AV(X)A^T=\sigma^2\left(I-\frac{1}{n}B\right)\tag{0}
$$
where B is a matrix of ones.
Note that we should not expect that $V(AX) = \sigma^2I$. Indeed,
$$
\begin{align}
Var(X_{1}-\bar{X})&=Var\left((1-\frac{1}{n})X_{1}-\frac{1}{n}X_2+\dotsb+-\frac{1}{n}X_{n}\right)\\
&=\left(1-\frac{1}{n}\right)^2\sigma^2+\frac{n-1}{n^2}\sigma^2=\sigma^2\left(1-\frac{1}{n}\right)
\end{align}
$$
for example by independence. Next in your computation the $X_i$ are not zero mean so the formulas ought to be $EX_i^2 = \sigma^2+\mu^2$ and $EX_{i}X_{j} = \mu^2$. So
$$
E[XX^T]=\mu^2B+\sigma^2I
$$
But Indeed
$$
AE[XX^T]A^T=\mu^2ABA^T+\sigma^2AA^T=\sigma^2AA^T\tag{1}
$$
since $AB=0$ the zero matrix. Now we note that $(0)$ and $(1)$ agree.
Answer: First note the mean can be computed quite simply as
$$E[AX]=AE[X]=A(\mu {\bf 1})=\mu (A{\bf 1} )=0,\quad (1)$$
where the final equality follows since the rows of $A$ sum to zero.  Or as you did, you can note that $E[X_i-\bar{X}]=\mu-\mu=0\quad \forall i$.

For the variance, both your computations are correct: even though we can't say $$E[XX']=\text{Var}(X),$$ we can still say
$$AE[XX']A'=A\text{Var}(X)A'.$$
To see this, note that in general we have $\text{Var}(X)=E[XX']-E[X]E[X'],$ and
$$A\text{Var}(X)A'=A(E[XX']-E[X]E[X'])A'=AE[XX']A-AE[X](AE[X])'=AE[XX']A,$$
where the last equality is by $(1)$.
Finally, the variance computation can be simplified as follows: Note $A$ is symmetric and idempotent. Think about the intuition: demeaning the data twice is the same as doing it once. Hence $$\text{Var}AX=A\text{Var}(X)A'=A(\sigma^2 I)A'=\sigma^2A^2=\sigma^2A.$$


Is it possible to enable sudo -u only for specific users?

Question: I am trying to figure this out, but have not found a proper answer to this yet:
Is it possible to add a user to /etc/sudoers only with the rights to use sudo -u for specific users?
Comment: it's not clear to me: do you mean to limit the *target* users? E.g. permit say jsmith to run `sudo -u jbloggs foo` and `sudo -u jdoe foo` but not `sudo -u jchin foo`
Comment: It's clear that this question is about being about to `sudo -u someuser` and not `sudo -u someotheruser` or just `sudo` as root.
Answer: Yes you can specify the users in the sudoers file
To let youruser run any command as the user specificuser do this:
youruser      ALL = (specificuser)    ALL
Answer: From the man sudoers page:

A Runas_Spec sets the default for the commands that follow it.  What
  this means is that for the entry:
    dgb    boulder = (operator) /bin/ls, /bin/kill, /usr/bin/lprm

The user dgb may run /bin/ls, /bin/kill, and /usr/bin/lprm -- but only
  as operator.  E.g.,
    $ sudo -u operator /bin/ls.

I'm running sudo-1.7.4p5-4.fc15.x86_64


How can I programmatically get a checkbox value?

Question:
I need to check if "Close this ticket after replying" checkbox is checked or not. The value $ab is not getting displayed. This is a comment type created from Structure > Comment Type.
function helper_ticket_comment_submit(&$form, FormStateInterface $form_state)
{
          $ab = $form_state ->getValue('field_close_this_ticket');
           \Drupal::logger('CheckBoxValue')->notice($ab);

 }

Error:
Answer: Install the Devel module and then use its dpm() function to print a variable to the message area of your page. So inside your submit handler put dpm($form_state->getValue('field_close_this_ticket')); and you'll see what the error message already is telling you: It's an array. While the logger expects a string. What you are looking for is inside $form_state->getValue('field_close_this_ticket')['value'], note the ['value'] at the end.
function helper_ticket_comment_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  if ($form_state->hasValue('field_close_this_ticket') && $form_state->getValue('field_close_this_ticket')['value'] == TRUE) {

    \Drupal::service('messenger')->addMessage(t('Ticket closed'));
  }
}


How does Vue check instantiation?

Question: All:
If I made Vue instance on same element twice, I wonder how does Vue check if that element has been applied before?

new Vue({el:"#app"})
new Vue({el:"#app"})

Comment: Who says it does? It doesn't appear to in my testing: https://jsfiddle.net/6rk0hmyn/1/
Comment: @BoussadjraBrahim That is also what I thought, but I just not do not have a simple way to understand the implement detail
Comment: @ceejayoz  what Daniel Beck said is what I mean
Comment: it seems like the first one creates an exclusive lock on that element
Answer: Vue doesn't check to see whether you've created multiple instances on the same DOM element; as you can see from the console messages below, both Vue instances are created, each with its own distinct data, and their mounted() events both fire:

var instance1 = new Vue({
  el: '#app',
  data: {
    foo: 'This is instance 1'
  },
  mounted() {
    console.log("Instance 1 mounted")
  }
});

var instance2 = new Vue({
  el: '#app',
  data: {
    foo: 'This is instance 2'
  },
  mounted() {
    console.log("Instance 2 mounted")
  }
});

// Both Vue instances exist, with their data intact:
console.log(instance1._data.foo)
console.log(instance2._data.foo)

{{foo}}

But both instances wind up competing with each other over, causing errors.  Above, only the first instance's data is visible in the DOM. As another example, in the below snippet the update method for both instances fails to run at all:

var instance1 = new Vue({
  el: '#app',
  data: {
    foo: 'This is instance 1'
  },
  methods: {
    update() {
      this.foo = "Update from instance 1"
    }
  }
});

var instance2 = new Vue({
  el: '#app',
  data: {
    foo: 'This is instance 2'
  },
  methods: {
    update() {
      this.foo = "Update from instance 2"
    }
  }
});

{{foo}}

...even though the same method works fine when there aren't duplicate instances of Vue on the element:

var instance1 = new Vue({
  el: '#app',
  data: {
    foo: 'This is instance 1'
  },
  methods: {
    update() {
      this.foo = "Update from instance 1"
    }
  }
});

{{foo}}

These problems aren't limited to DOM interactions; similar errors occur if the data changes on a timeout instead of on a click event.
TL;DR
The upshot here is, the precise details of what goes wrong and why aren't simple -- it's not as easy as "the second instance clobbers the first one" or "the first instance locks out the second one" -- but it does go wrong, so don't try to do it.   Both instances will wind up fighting over the same DOM elements and bindings, so it's not at all surprising that they end up interfering with one another.
(In general Vue isn't happy with DOM modifications it doesn't know about; the same types of issue are happening here as when people naively try to use jQuery to overwrite the DOM and then wonder why Vue can't update things anymore, except that in this case it's another instance of Vue instead of jQuery doing the interfering.)
If you need to check to see whether a DOM element already has a Vue instance attached to it, you can do so by checking for the presence of a "__vue__" property on the element:

var hasVue = function(id) {
  if (document.getElementById(id).__vue__) {
    console.log("Has a vue instance attached")
  } else {
    console.log("No vue instance attached")
  }
}

hasVue('app');
new Vue({ el: '#app' });
hasVue('app');

Comment: Thanks. Could you  simply explain why "the update method for both instances fails to run at all"?
Comment: Well, the simple possibilities (B clobbers A, or A locks out B) are ruled out by the above experiments, so there probably *isn't* a simple explanation -- both instances are fighting over the same DOM elements and bindings, so it's not at all surprising that they end up interfering with one another.  In general Vue isn't happy with DOM modifications it doesn't know about; the same problems are happening here as when people try to use jQuery to overwrite the DOM and then wonder why Vue can't update things anymore, except that it's another instance of Vue instead of jQuery doing the interfering.
Comment: Thanks, and sorry for my slowness, do you mean when there are multiple bindings on same element, Vue will do extra job to reject them all rather than choose one to bind?
Comment: No, I mean that neither instance knows the other one exists, so each may wind up inadvertently removing bindings set by the other (by, for example, replacing the DOM element to which those bindings are attached, or by overwriting one binding with another of the same name.)  Vue doesn't appear to have any code that checks to see whether you've doubly-instantiated it, so it's not deliberately doing any extra work to remove the other's bindings; but there are lots of ways for that to happen accidentally.
Comment: Thanks, but why there is no final winner if they just try to remove each other's binding
Comment: I don't know the precise cause, and to be honest don't see much point in digging through the Vue source code to figure out exactly why something that shouldn't work doesn't work.  Is there some reason you need to know the answer to that, or is it just academic curiosity at this point?
Comment: It is just because I need to know if one DOM element has already been taken
Comment: OK, I already explained how to check that in the answer above; look for `__vue__` on the element.
Comment: Yeah, get it. Thanks

How does Vue check instantiation?


Search button text not showing in a Bootstrap sub-theme

Question: I'm using a sub-theme of the Bootstrap v8.x-3.1 theme.
By default the search button shows the search icon.
In the theme settings you can un-check 'Iconize Buttons' which I expect would show the 'Search' text instead of the icon, however it shows an empty button... no text or icon.

How do I get the button to show 'Search' text instead of an icon?
Answer: The solution I found for this was to copy this file from the Bootstrap theme directory
src/Plugin/Form/SearchBlockForm.php
to the same location in your sub-theme.
Then you can set the icon_only property to FALSE.
e.g.
<?php
/**
 * @file
 * Contains \Drupal\subtheme\Plugin\Form\SearchBlockForm.
 */

namespace Drupal\subtheme\Plugin\Form;

use Drupal\bootstrap\Annotation\BootstrapForm;
use Drupal\bootstrap\Plugin\Form\FormBase;
use Drupal\bootstrap\Utility\Element;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @ingroup plugins_form
 *
 * @BootstrapForm("search_block_form")
 */
class SearchBlockForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
    $form->actions->submit->setProperty('icon_only', FALSE);
    $form->keys->setProperty('input_group_button', FALSE);
  }

}

I still think there must be an easier way... but until then.


S.M.A.R.T Power_On_Hours wrap around with Seagate Baracuda?

Question: I have a well used Seagate Barracuda 7200.9 with 80 GB. The smart report shows
<code> Power_On_Hours          0x0032   094   094   000    Old_age   Always       -       5646
</code>
But that can't be right. 5646h is only 235 days or less than eight month. The sister disk, a Seagate Barracuda 7200.10, shows:
<code>9 Power_On_Hours          0x0032   001   001   000    Old_age   Always       -       90427
</code>
This is plausible: The 90427 hours is 10.3 years. I remember the server has been setup in 2009 and at that time the hardware was not new.
So I assume that the 7200.9 has experienced an overflow of the hour counter and has restarted at some point at 0. Does anybody know at what hour that would be? What is the internal time unit used to count uptime? And what variable type is used to count those?
Comment: Does it really matter?
Answer: The same happened to one of my server disks recently:
ST2000NM0033 (Seagate Constellation ES.3 2TB)
SMART record (previous day):
9 Power_On_Hours          0x0032   011   011   000    Old_age   Always       -       78177
SMART record (next day):
9 Power_On_Hours          0x0032   100   011   000    Old_age   Always       -       13
So it jumped from 78177 hours to 13 hours the next day ...


Is it possible to learn abstract algebra no precalculus or calculus?

Question: See above. I am trying to re-teach myself mathematics in a different manner than is formally taught (i.e., set theory, number theory, mathematical logic, abstract algebra, discrete math and then precalculus (college algebra and analytical geometry). These are the pillars to upper level mathematics correct?
Comment: It's certainly possible, but there's generally a level of mathematical maturity required to approach abstract algebra or the other (usual undergraduate) upper-division courses. Lacking the experience that precalc and a few semesters of calc gives might hinder you quite a bit.
Comment: Honestly I think it would be quite possible because the material from pre-calc isn't fundamental for learning abstract algebra but agree with the above poster that it's about the experience of the math classes.
Comment: My middle school started abstract algebra in the second half of eighth grade. It was very elementary abstract algebra,  but it was the real thing. The answer to your question is certainly yes.
Comment: Pre-calculus is a must for abstract algebra. In my first two-semester introductory course (covering groups, rings, fields, Galois theory), algebraic manipulations abound and at least some exposure to linear algebra, complex analysis and real analysis presumed. For example, the cyclotomic roots. How else can you understand the field extensions and Galois groups? Even still, this progression doesn't make any sense: number theory uses complex analysis and abstract algebra.
Comment: The missing parentheses makes it confusing.
Comment: In principle you could start with abstract algebra having only a very elementary background in math, except that course materials for learning it that way don't really exist.
Answer: You could learn mathematics any way you want, but the way of mathematics, as formally taught, is elegant and (most of the time) displays the beauty of the subject. As T. Bongers has said, one requires some mathematical maturity in order to fully appreciate the beauty of mathematics.
Abstract algebra is the study of groups, rings, fields, modules, vector spaces, and algebras. I agree with EgoKilla that precalculus is not fundamental to learning abstract algebra - however, it gives one the foundation for learning the subject in detail, and appreciating its mathematical beauty.

You need to know the following to learn abstract algebra:

Set Theory
Logic
Proof techniques
Functions and Relations
Induction
Cardinal numbers
Number theory

Most universities also need Calculus along with this. You could learn these in whichever order you want, but you must make sure to cover all these topics in order to fully understand abstract algebra.

I recommend reading the responses here - A Book for abstract Algebra.
Just as a note, this is the usual way of going about learning mathematics:
Start with precalculus, then go to single variable calculus. (I would like to note here that I am a self-learner, and I skipped almost the whole of precalculus.) After single-variable, go to multivariable calculus. Then learn differential equations, and then proceed on to linear algebra. This should set a sound basis for real and then complex analysis. Go on to functional analysis, topology, differential geometry, and then whatever else interests you.
I hope this helps.
Comment: I kinda disagree. I think you learn about the numbered items in an abstract algebra class rather than before one.
Comment: Thank you for the help. I tried the traditional way in college and it didn't work as well for me. I am trying to learn math in the way that I described with the hope that I will be able to tackle Calculus and other higher level topics with ease.
Comment: I am also adding a philosophy/history component to help with my thinking.
Comment: @JpMcCarthy In abstract algebra, you learn groups, rings, fields, modules, vector spaces, and algebras. It depends on how you study the course - this would determine if one learned the numbered items in abstract algebra or not. Also, if one does learn the numbered items in abstract algebra, then this basically means that an understanding of high-school math is sufficient to learn abstract algebra!
Answer: You can certainly learn abstract algebra before you learn calculus (I did this myself), but you (almost certainly) won't be able to learn it if you aren't comfortable with high school algebra (which, I guess, is a sizable component of what is called pre-calculus).   A basic facility with factorization, algebraic manipulations (like modifying two sides of an equation by applying the same process to both sides),
exponential notation, and complex numbers (among various algebra topics of this
sort) is more-or-less essential to successfully learning abstract algebra.
Comment: Couldn't have said it better myself.
Answer: If anyone is looking for a great mathematics series, get used copies of University of Chicago Mathematics School Program or get the series by Dolciani for high school mathematics. The UCMSP teaches precalculus and discrete math simultaneously. I am not sure where the abstract algebra (AA) and number theory (NT) segments go but AA precedes NT. I will implement before pre-calculus and after "Algebra 2/Geometry". Another way is to place NT before AA but after the precalculus/discrete mathematics sequence.


How to solve partial integro-differential equation?

Question: Suppose the following partial integro-differential equation for a function $u(x,t)$ with $t\geq0$, $x \in [0,L]$:
$\partial_t u = \partial_{xx} u + f(u,\lambda)$
$\lambda = B\left(u_0 - \int_{x=0}^L {u(x,t)dx} \right) = B \left(u_0 - <u> \right)$ (the latter for brevity).
$B,u_0$ are constants. $B$ is the global interaction sensitivity, $u_0$ can be thought of as a measure of how much mass can go into $u$.
$f$ has nice properties and can be assumed to be polynomial in $u$ and $\lambda$. E.g. take $f$ as
$f(u, \lambda) = -u^3 + u + \lambda$
Assume nice initial and boundary conditions, e.g.
$u(x,0) = u_i = \text{const}$
$\partial_t u(0,t) = \partial_t u(L,t) = 0$
(this problem is from: http://link.aip.org/link/doi/10.1063/1.466399)
How can I go about solving this? Is there a way to turn this equation into something more easily solvable?
One approach I've tried was using the Laplace Transform with respect to $x$.
Here's my attempt doing this but I am far from avid with this technique so please correct me:
$\mathcal{L}[\partial_t u](s) = \int_{0}^\infty{e^{-xs}\partial_t u(x,t) dx} = \partial_t \overline{u}(t | s) = \frac{d \overline{u}(t | s)}{dt}$,
where I read $\overline{u}(t | s)$ as a function of $t$, given $s$ ($s$ is arbitrary but fixed as I understand).
For the spatial derivative I get (using a table)
$\mathcal{L}[\partial_{xx} u](s) = s^2 \overline{u}(t|s) - s u(0,t) - \partial_x u (0,t)$,
and for $\lambda$ I am not sure what to do.
Comment: In the expression $\lambda=B(u_0-\left)$, is $B$ a constant, or some unknown function?
Comment: Also, what is $u_0$ in that expression?
Comment: @rajb245 I hope I clarified your questions with my edits. Also see the reference for the paper where this is from -- I might still miss something in my recount of the problem.
Comment: Thanks for the clarifications.  Given that the authors used numerical methods to solve the above equation, my guess is that it does not have a closed form solution, so Laplace transform methods will probably not be useful, but I'll take a look into it.
Comment: Cheers @rajb245 ... if you happen to know how to recast this problem into something that standard numerical methods can handle I would also be very grateful! Also talked to colleagues of mine about the problem and they suggested (i) imposing the integral condition with a Lagrange multiplier (ii) solving the PDE with the integral as new variable $(u_0 - ) = u_i(t)$ so that you get a solution in terms of an integral equation $u(x,t) = h(u_i(t), x, t)$, then solve this integral equation
Comment: I have one more question.  The no-flux condition in this case is $\partial_x u(0,t)=\partial_x u(L,t)=0$, usual Neumann boundary conditions.  Is this what you meant?
Comment: Also, as defined in the paper, the average $\left$ should be $\frac{1}{L}\int_{0}^L u(x,t)\;dx$
Comment: @rajb245 You're correct in both instances.
Answer: The equation for $u(x,t)$ is
$$
\partial_t u = \partial_{xx}u - u^3 + u + B\left(u_0-\frac{1}{L}\int_{0}^{L} u\;dx\right)
$$
This is a non-linear integro-differential equation (if not for the $u^3$, it would be linear).  Laplace transform techniques don't get you very far with this equation, because the Laplace transform of $u^3$ is an integral in the Laplace domain.  So even after transforming, you have an integro-differential equation; it doesn't simplify to an algebraic equation or ODE, as would happen with linear equations.
It won't be simple to develop your own, but numerical solutions are the way to go here.  The authors of the paper from which this comes hastily mention that they use spatial discretization to reduce the problem to a system of ODEs in time, which they then solve with Runge-Kutta methods.  The equation above is directly amenable to such an approach, but you have to formulate it.  There are also a myriad of other techniques for numerical solutions to such equations.  A good package I've been using recently is Chebfun for MATLAB, which can solve this using pseudospectral methods.
MATLAB and Chebfun
This is the easy way.  The pde15s routine from the Chebfun package seems to solve this problem correctly.  For a bump shaped initial condition with $L=4$, $B=10$, $u_0=1/2$, I get plots like

, which has the diffusion kind of properties I would expect from this equation.  My numerical experiments tell me that in order to see interesting behavior, you have to have the equation on a ring with periodic boundary conditions (like the authors of that paper), or you need non constant initial conditions.  For the constant initial conditions and Neumann boundary conditions like you propose, the solution doesn't even depend on $x$, i.e. the lack of initial $x$ variations can't be "smoothed out" any further by this kind of diffusion equation, so the solution is just a constant in $x$ along the rod that changes in time.
Finite Differences and Runge-Kutta
Another option is to do what the authors of that paper did.  The idea is to sample $u(x,t)$ on a set of $N$ equidistant-spaced points (distance $\Delta x$), say $x_n\in[0,L]$, $n\in[1,2\ldots N]$.  These are called nodes in a mesh.  Let $u_n(t)=u(x_n,t)$ be the value of $u$ at the point $x_n$.  You approximate your spatial derivatives and integrals with finite approximations, which in this case boils down to
$$
\partial_{xx} u(x_n,t)\approx \frac{u_{n+1}(t)-2u_{n}(t)+u_{n-1}(t)}{\left(\Delta x\right)^2}
$$
$$
\int_{0}^{L} u\;dx\approx \sum_{n=1}^N u_n(t) \Delta x
$$
The integral could be approximated with a higher order rule like the trapezoid or Simpson's rule.  There is probably some optimum order for this problem (something like matching the approximation order used for the derivative), you'll have to find the details on how that works.  Plugging these into your equation and enforcing the equation on the nodes gives a system of $N$ equations
$$
u_n' = \frac{u_{n+1}-2u_{n}+u_{n-1}}{\left(\Delta x\right)^2} - u_n^3 + u_n + B\left(u_0-\sum_{n=1}^N u_n \Delta x\right),\;\;\forall n\in[1,2\ldots N]
$$
I've dropped the explicit time dependence and let the prime denote a time derivative.  These are $N$ ODEs in the $N$ unknowns, the $u_n(t)$ functions.  You'll also need some more boundary conditions to specify what $u_{0}(t)$ and $u_{N+1}(t)$ are.  Now you can use the Runge-Kutta method to solve this system of ODEs at discrete time steps.  This is what the authors of the paper did.
You also have the option to directly discretize in space and time, which I think is the more standard approach.  This is essentially a heat/diffusion type equation, so the discretization methods used there all apply.  Check out the heat-equation example of finite differences on Wikipedia.
Comment: Absolutely fantastic answer. I agree that Runge-Kutta and one of the numerical integration methods you mentioned are most likely the way to go here. Thank you so much for typing this up! From work I've done on a slightly different system, I would expect that there is a range for $u_0$ that gives interesting behavior (due to Turing instability) both with von Neumann b/c and constant initial conditions. At least, this is what I observe for a very similar system (I think only the kinetics differ) described here: http://www.math.umn.edu/~ymori/docs/publications/wavepin.pdf
Comment: But generally, you're right that initial conditions with dependence on $x$ give interesting behavior more easily and over a wider range of parameter values.

However, let me take a look at the particular example I asked about and see if I can find that regime with Turing instabilities.
Comment: could you help me please https://math.stackexchange.com/questions/2340696/the-equivalence-of-two-nonlinear-partial-differential-equations


Why are there so many different ways to translate menus?

Question: I'm very confused about the way Drupal 7 translates menu items. I have many modules installed, so I'm not sure which would be relevant to state; in fact, I don't have my head fully wrapped around which modules do what and what they are dependent on.
I'm working on a bi-lingual website, in English and French. I noticed two different ways to create menues with translation. Let me outline the steps and results for both, and you can explain to me why these differences exist and how to effectively apply these differences.
Method 1

Go to Structure>Menu>(select the menu you want)
Click Add Link
Provide a "Menu link title" and "Path"
Under language, select English
Save
Translate this menu item into french

Result of Method 1

There are TWO menu items under Structure>Menu>(select the menu you want), one for each language item.  Additionally, I can no longer convert a menu's languge from English or French back to Language Neutral...what's up with that?

Method 2

Go to Structure>Menu>(select the menu you want)
Click Add Link
Provide a "Menu link title" and "Path"
Under language, select Language Neutral
Save
Go back to the edit form of the menu link you just saved, change language to English and save
Translate this menu item into french

Result of Method 2

There is ONE menu item for the link you added under Structure>Menu>(select the menu you want).  But when you edit that menu item, you will see the french translation under the translate tab.

Why is the Result of Method 1 different from the Result of Method 2?  Why is it that in Method 1, each translation is a new menu item, but in Method 2, translations are grouped together?
It is so easy for a user to forget to select a language when creating a menu item, which unintentionally creates a parallel track of information in the site (it's almost like a duplicate site where we lose the correlation between english and french pages ).  Fixing this after the fact would be so troublesome!
Answer: Method 1 points to the node translation. Method 2 is a translation of the link but goes to the same path. For most people method 1 is the way to go. But method 2 could be handy as well.
Maybe life would be easier if you just build it in one language and use Google Translate Elements JS addon. It really depends on the needs of your users. For my client I had to use method 1.


efficient way to find several rows above and below a subset of data

Question: I'm wondering if there's an efficient way to get X number of rows below and above a subset of rows. I've created a basic implementation below, but I'm sure there's a better way. The subset that I care about is buyindex, which is the indices of rows that have the buy signal. I want to get several rows above and below the sellindex to verify that my algorithm is working correctly. How do I do it in an efficient way? My way seems roundabout.
buyindex = list(data2[data2['buy'] == True].index)

print buyindex [71, 102, 103, 179, 505, 506, 607]

buyindex1 = map(lambda x: x + 1, buyindex)
buyindex2 = map(lambda x: x - 1, buyindex)
buyindex3 = map(lambda x: x - 2, buyindex)
buyindex4 = map(lambda x: x + 2, buyindex)
buyindex.extend(buyindex1)
buyindex.extend(buyindex2)
buyindex.extend(buyindex3)
buyindex.extend(buyindex4)

buyindex.sort()
data2.iloc[buyindex]

UPDATE - this is the structure of the data.
I have the indices of the "buys." but I basically want to get several indices above and below the buys.
VTI upper   lower   sell    buy AboveUpper  BelowLower  date    tokens_left
38   61.25   64.104107   61.341893   False   True    False   True   2007-02-28 00:00:00  5
39   61.08   64.218341   61.109659   False   True    False   True   2007-03-01 00:00:00  5
40   60.21   64.446719   60.640281   False   True    False   True   2007-03-02 00:00:00  5
41   59.51   64.717936   60.050064   False   True    False   True   2007-03-05 00:00:00  5
142  63.27   68.909776   64.310224   False   True    False   True   2007-07-27 00:00:00  5
217  62.98   68.858308   63.587692   False   True    False   True   2007-11-12 00:00:00  5
254  61.90   66.941126   61.944874   False   True    False   True   2008-01-07 00:00:00  5
255  60.79   67.049925   61.312075   False   True    False   True   2008-01-08 00:00:00  5
296  57.02   61.382677   57.371323   False   True    False   True   2008-03-07 00:00:00  5
297  56.15   61.709166   56.788834   False   True    False   True   2008-03-10 00:00:00  5

UPDATE: I created a general function based off the chosen answer. Let me know if you think this could be made even more efficient.
def get_test_index(df, column, numbers):
    """
    builds an test index based on a range of numbers above and below the a specific index you want.
    df = dataframe to build off of
    column = the column that is important to you. for instance, 'buy', or 'sell'
    numbers = how many above and below you want of the important index

    """

    idx_l = list(df[df[column] == True].index)
    for i in range(numbers)[1:]:
        idxpos = data2[column].shift(i).fillna(False)
        idxpos = list(df[idxpos].index)
        idx_l.extend(idxpos)

        idxneg = data2[column].shift(-i).fillna(False)
        idxneg = list(df[idxneg].index)
        idx_l.extend(idxneg)
    #print idx_l
    return sorted(idx_l)

Comment: Could you show us some data and an example of what you want? So we can manipulate it.
Comment: possible duplicate of [index out of range error while working with lists in python](http://stackoverflow.com/questions/22038929/index-out-of-range-error-while-working-with-lists-in-python)
Answer: This will be a very efficient method
In [39]: df = DataFrame(np.random.randn(10,2))

In [41]: start=3

In [42]: stop=4

In [43]: df.iloc[(max(df.index.get_loc(start)-2,0)):min(df.index.get_loc(stop)+2,len(df))]
Out[43]:
          0         1
1  0.348326  1.413770
2  1.898784  0.053780
3  0.825941 -1.986920
4  0.075956 -0.324657
5 -2.736800 -0.075813

[5 rows x 2 columns]

If you want essentially a function of arbitrary indexers, just create a list
of the ones you want and pass to .iloc
In [18]: index_wanted = [71, 102, 103, 179, 505, 506, 607]

In [19]: from itertools import chain

In [20]: df = DataFrame(np.random.randn(1000,2))

You prob want unique ones
f = lambda i: [ i-2, i-1, i, i+1, i+2 ]

In [21]: indexers = Index(list(chain(*[ f(i) for i in [71, 102, 103, 179, 505, 506, 607] ]))).unique()

In [22]: df.iloc[indexers]
Out[22]:
            0         1
69   0.792996  0.264597
70   1.084315 -0.620006
71  -0.030432  1.219576
72  -0.767855  0.765041
73  -0.637771 -0.103378
100 -1.087505  1.698133
101  1.007143  2.594046
102 -0.307440  0.308360
103  0.944429 -0.411742
104  1.332445 -0.149350
105  0.165213  1.125668
177  0.409580 -0.375709
178 -1.757021 -0.266762
179  0.736809 -1.286848
180  1.856241  0.176931
181 -0.492590  0.083519
503 -0.651788  0.717922
504 -1.612517 -1.729867
505 -1.786807 -0.066421
506  1.423571  0.768161
507  0.186871  1.162447
508  1.233441 -0.028261
605 -0.060117 -1.459827
606 -0.541765 -0.350981
607 -1.166172 -0.026404
608 -0.045338  1.641864
609 -0.337748  0.955940

[27 rows x 2 columns]

Comment: is it possible to easily generalize this to an index that's 20 units long? I think this code is really clean. Yet if I have an index that has 20 different numbers out of a 1000 row long dataframe, it's not clear how you'd just get specific slices of the dataframe based on the sellindex ([71, 102, 103, 179, 505, 506, 607])
Comment: updated...sure I misread your question, only thought you wanted one. It generalizes pretty easily.
Comment: Thanks Jeff. This looks really good. I'm reading about chain here: http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/itertools/index.html Can you help me understand why chain is used here instead of zip?
Comment: sorry one more: if i wanted to create a function that specified the range I wanted, how could I adapt the line below so I wouldn't ahve to change it everytime? So for instance, if I wanted 3 or 4 above and below. indexers = Index(list(chain(*[ [i-2,i-1,i,i,i+1,i+2] for i in [71, 102, 103, 179, 505, 506, 607] ]))).unique()
Comment: I updated to show you how you can use a function. All chain does is put together lists (essentially flattening a list-of-lists to a single list)
Answer: you can use shift and | operator; for example for +/- 2 days you can do
idx = (data2['buy'] == True).fillna(False)
idx |= idx.shift(-1) | idx.shift(-2)   # one & two days after
idx |= idx.shift(1) | idx.shift(2)     # one & two days before
data2[ idx ] # this is what you need

Comment: Thanks Behzad. could you flesh this out a bit more? I keep getting this error: TypeError: unsupported operand type(s) for |: 'bool' and 'float'for ----> 3 idx |= data2.buy.shift(1) | data2.buy.shift(2)
Comment: can you help me understand what the | (is this called a pipe?) is doing here? I'm reading this link but it's not entirely clear to me: http://stackoverflow.com/questions/5988665/pipe-character-in-python
Comment: **sorry for all the comments. I think I understand now. The Pipe acts like an "OR" and it allows you to assemble multiple indices that fit the condition. If you use "&" you'll get an empty because it will be difficult to find parts of the df that fit all the conditions?
Comment: However, your second line "idx |= data2.buy.shift(1) | data2.buy.shift(2)" still doesn't seem to work for me...
Comment: @user3314418  `|` here is basically pair-wise or; plz try my edited answer
Comment: Hm, the |= is giving me problems still. However, I mae a general function of your answer (see my update above). Do you think this basically achieves the same thing?
Comment: I changed the answer to the chain function because it only requires one line.
Comment: Its very helpful!
Comment: what if I want to take all the days before? do we take idx.shift(len(df))? wouldnt it take just all the days and not all the days before?


South scheme migration apparently didn't work

Question: Okay, so I had an app called locationmanager with a model that looks like this:
class Location(models.Model):
    region = models.ForeignKey(Region)

and I want to change it to this
class Location(models.Model):
    region = models.ForeignKey(Region, blank=True, null=True)

After changing it, I ran a schemamigration that looked like the following:
    $ python manage.py schemamigration locationmanager --auto
     ? The field 'Location.region' does not have a default specified, yet is NOT NULL.
     ? Since you are making this field nullable, you MUST specify a default
     ? value to use for existing rows. Would you like to:
     ?  1. Quit now.
     ?  2. Specify a one-off value to use for existing columns now
     ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
     ? Please select a choice: 2
     ? Please enter Python code for your one-off default value.
     ? The datetime module is available, so you can do e.g. datetime.date.today()
     >>> 0
     ~ Changed field region on locationmanager.Location
     ? The field 'Location.manager' does not have a default specified, yet is NOT NULL.
     ? Since you are making this field nullable, you MUST specify a default
     ? value to use for existing rows. Would you like to:
     ?  1. Quit now.
     ?  2. Specify a one-off value to use for existing columns now
     ?  3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
     ? Please select a choice: 0
     ! Invalid choice.
     ? Please select a choice: 2
     ? Please enter Python code for your one-off default value.
     ? The datetime module is available, so you can do e.g. datetime.date.today()
     >>> 0
     ~ Changed field manager on locationmanager.Location
    Created 0003_auto__chg_field_location_region__chg_field_location_manager.py. You can now apply this migration with: ./manage.py migrate locationmanager

$ sudo python manage.py migrate locationmanager --fake
Running migrations for locationmanager:
 - Migrating forwards to 0003_auto__chg_field_location_region__chg_field_location_manager.
 > locationmanager:0002_initial
   (faked)
 > locationmanager:0003_auto__chg_field_location_region__chg_field_location_manager
   (faked)

After running the test, the form spit the error out at me:
   IntegrityError at /dash/location/add
    locationmanager_location.region_id may not be NULL

I had thought that I changed it? What could I have done wrong? What can I do currently to fix this?
Answer: You specify a --fake migration. Fake your initial migration to get to the current state (before the change) and them migrate regularly.
Edit (based on the comments):
In order to fix the migration you will have to roll back to migration 0002 using
./manage.py migrate locationmanager 0002

Migration 0002 is faked, and it's fine, because you probably don't want south to change the db scheme for the state before the change.
Now you can migrate regularly to migration 0003 using
./manage.py migrate locationmanager

Note that you can always look at the list of applied migrations using
./manage.py migrate locationmanager --list

Comment: What? I used --fake on the migration as shown in the terminal. How/why would you --fake an initial migration?
Comment: @ApathyBear why are you faking the migration? Faking it will only mark the migration as done but will do nothing on a database level
Comment: Well, thats good to know. What should I have done instead @CésarBustíos
Comment: @ApathyBear I guess you can go back to previous migrations and migrate again without the --fake flag


The area region bounded by $y=3x$, $x=5$ and $x$-axis

Question: Find the area region bounded by $y=3x$, $x=5$ and $x$-axis, hence find the volume revolved at $y$-axis $(360^\circ)$ and $y$-axis ($180^\circ$). How to find the volume and what it is mean by 'hence'. For the area i get the answer $75/2$ units. Please help since i cannot configure it other way around
Comment: What do you mean by $360$ and $180$?
Comment: 360 and 180 Degree.sorry i dont know how to make that sign
Comment: I'm still confused. There is only one y-axis. Are you asking the volume of the region you specified about the y-axis?
Comment: Yeah i copy the entire question.
Comment: Merely copying the "entire question" may not result in Readers giving your post thorough consideration.  It would greatly improve your Question if you added context, such as what you tried or what you failed to understand about tackling the problem.
Answer: Notice, plot the lines, $y=3x$, $x=5$ then one should find that  the region bounded by the lines $y=3x$, $x=5$ & x-axis is a right triangle having vertices at $(0, 0)$, $(5, 0)$ & $(5, 15)$ hence, its area is given as $$A=\int_{x=0}^{x=5} y\ dx$$  $$=\int_{x=0}^{x=5} (3x)\ dx=3\left[\frac{x^2}{2}\right]_0^5=\color{red}{\frac{75}{2}}$$  Now, the volume of the solid obtained by rotating the curve (line) $y=3x$ about $y$-axis $$=\text{volume of complementary cylinder}-\text{volume of cone of rotation}$$
$$=\pi r^2l-\int_{y=0}^{y=15}\pi x^2\ dy $$
$$=\pi(5)^2(15)-\int_{y=0}^{y=15}\pi \left(\frac y3\right)^2\ dy $$
$$=375\pi-\frac{\pi}{9}\left[\frac{y^3}{3}\right]_{y=0}^{y=15} $$
$$=375\pi-125\pi=\color{red}{250\pi}$$
Comment: @Ameet: Try to visualize or draw a rough diagram. It will be a cone if the line $y=3x$ is rotated about the x-axis but the line $y=3x$ is rotated about the y-axis hence the solid will be cylinder-cone composite
Comment: Why it is a cylinder?i think i visualize a cone


How to write a random variable is not distributed as N(0,1)

Question: I was wondering if the symbol $\not\sim$ (i.e., not tilde) could be used to express that a random variable $X$ is not distributed as a normal distribution with a mean of $0$ and variance of $1$. For instance, is this appropriate:   $$X\not\sim N(0,1)$$
Or is it more common to say: $$X \text{ is not } X\sim N(0,1)$$
I need a way to express it in a symbolic form to be as succinct as possible. Any help would be greatly appreciated.
Comment: I think the most common way would be to just use words
Answer: I have never seen anyone say this, but that first notation seems to have an obvious meaning to me, so I would think that is a perfectly reasonable way to write this.  (The second notation seems flawed to me, since it says "$X \text{ is not }X$" which is a contradiction.)


The Composition of Functions

Question: Say $f(x)= \sqrt{2x + 2}$
Domain here would be limited to values of $x$ greater than or equal to $-1$.
$g(x)=(2x^2)+3$ (arbitrary, really)
$g(f(x))$ would have no restrictions on domain despite $f(x)$ having restrictions, right?
Thanks
Answer: That's not correct.
First of all, Domain of $f$ consists of $x \geq -1$. $g\circ f$ will also have this domain restriction, as plugging any number less than $-1$ in $f$ doesn't make sense.
Comment: To elaborate on the second part: Just because we have a nice expression $2(2x+2)+3$ that happens to conicide with $g(f(x))$ whenever $f(x)$ is defined, does not mean that the function $g\circ f$ is defined at points where not even $f$ is defined.
Comment: One might say: "the composition can be extended to all of $\mathbb{R}$", but the composition itself is not defined everywhere.

The Composition of Functions


Converting Decimal to Hex is truncating the first digit

Question: I have a function to convert decimal to HEX but it's truncating the first character when returning the hex string. Why and how is it truncating the returned string?
this is the char string I'm dissecting:
str = "095154.000,4506.1389N,07389.6017W,2.00,290.0,3,299.12,0.08,0.04,120417,05";

Code:
char str[90];
char** tokens;
tokens = str_split(str, ',');

strncpy(tempTime, *(tokens)+9, 6);tempTime[7]="\0";

num = atoi(tempTime);
char hex_response[(sizeof(long int) * CHAR_BIT / 4) + 1];
printf("%s", int_hex(num, hex_response));

char* int_hex(long int num, char* hextresponse)
{
    sprintf(hex_response, "%lX", (unsigned long)num);

    return hextresponse;
}

hex_response returns 73B2 and it should return 173B2
Comment: `char tempTime = "082431";` won't compile. Please create [mcve].
Comment: `hex_response` in the calling code has wrong type; you should really post accurate code that compiles - i.e. the question should include the code you are asking about, rather than code that bares a passing similarity - otherwise we will be addressing the wrong problem.
Comment: The function `dec_hex` is misleadingly named since it is not converting "decimal to hex", but rather an integer to a hex string representation; you already performed the decimal string reprsentation to integer conversion with `atoi()`
Comment: It is also not a good idea to pass a variable string as the format string argument to `printf()`; use instead the format string "%s", and pass the string to be output as a second argument.
Comment: Making the buffer length for the hex the same as the length of the decimal is safe (because it will always be as long to longer) but makes little sense.  Hex representation requires exactly one character for each four bits, so the buffer can more appropriately be sized for the length of a `long int`.
Comment: It is not clear what `tempTime` is or contains, but presumably points to `"095154.000..."` given the expected value?  You should ideally post complete code that reproduces the problem - these are still broken fragments.
Comment: This is still not "real" code; `int_hex()` takes an argument named `hextresponse`, but writes to `hex_response`.  Write the code, compile it, and copy & paste code that at least compiles!
Comment: Array elements are indexed from from zero, and charavter constants use single quotes, so should be `tempTime[6 ]= '\0' ;`.  Your inability to post plausible/compilable code is severely hampering giving an accurate answer.  There is no point in addressing this question when this is certainly not the actual code producing the error - we'll just end up addressing the flaws in your post rather than the actual problem.
Answer: The solution was to change the following code
num = atoi(tempTime);  // String to int conversion

to
num = atol(tempTime);  // String to long conversion

Comment: While it is certainly correct in this case to use `atol()`; what platform are you using where `int` and `long int` are not the same size?  Or are you using some antique 16 bit compiler? That would have been useful information.
Comment: I'm using the latest version of Atmel Studio 6.
Comment: I meant the _target_ platform, not the _development_ platform.  Moreover Atmel Studio can be used for developing for Atmel's ARM and AVR, so the answer is not enlightening, but given that this is the solution, you are presumably targeting AVR?  My point is that had you stated that in the question or tagged it, the error may have been spotted more quickly.  Credit for posting an answer however rather then just "disappearing".
Comment: It is an AVR platform. My apologies for the confusion.
Comment: Clifford, Just have one question for you so I understand better. Can you explain the "(sizeof(long int) * CHAR_BIT / 4) + 1" part of the code? Thank you
Comment: `CHAR_BIT` is defined in limits.h and is the number of bits in a `char` (typically but not universally 8); `sizeof(long int)` is the size in `char`s of a `long int`, `4` is the number of bits which a hex digit _exactly_ represents (0x0 to 0xF).  So the expression calculates the number of characters needed to represent then maximum length hex string possible from a `long int`.  Say a `long int` is 4 bytes (32 bit), then the expression yields (4 * 8) / 4 = 8 - the number of characters needed to represent a 32 bit value in hex.


A question about unreachable stopping time

Question: I have two questions related to the stopping time.
Suppose we have a iid sequence $X_n$, where $P(X<0)=1$. And we have a stopping time N, N=$\inf${n:$\sum_{i=1}^nX_i>0$}.
======================================================
My First question is what the value of E($\sum_{i=1}^NX_i$) is.
For one side, because we set the stopping rule such that $\sum_{i=1}^NX_i$ has to be greater than $0$, so E($\sum_{i=1}^NX_i$) should be greater than 0.
On the other hand, because $X_i$ is always less than $0$, and the stopping rule will never be satisfied, so N=$\infty$, and therefore E($\sum_{i=1}^NX_i$)=-$\infty$.
======================================================
Me second question is actually related to the first question: What is E($\sum_{i=1}^NX_i$|N)? Is that $0$ or a positive number or negative infinity?
Because if it is $0$, We can use the expectation of the conditional expectation to solve E($\sum_{i=1}^NX_i$), so E($\sum_{i=1}^NX_i$) =$E_N$($E_X$($\sum_{i=1}^NX_i$|N))=$E_N(0)$=$0$.
If it is a positive number, we can use the same technique to solve E($\sum_{i=1}^NX_i$) and find E($\sum_{i=1}^NX_i$)$>0$.
From different aspect one can end up with different result, but there must be only one result. Could any one see the problem for the above solutions? I would really appreciate any help. Thanks.
Comment: Here you should write $$N=\inf\left\{n>0: \sum_{i=1}^n X_i >0\right\} $$ as that implicitly means we are taking the convention $$\left\{N=\infty\right\} = \bigcap_{n=1}^\infty \left\{ \sum_{i=1}^n X_i\leqslant 0\right\}. $$
Comment: You are right, I've changed that. But I still cannot understand my question. Could you make it more clear? Thank you!
Answer: Since $\mathbb P(X_1<0)=1$, it is clear that $$\mathbb P\left(\bigcap_{n=1}^\infty \left\{\sum_{i=1}^n X_i < 0\right\} \right) = 1. $$ This implies that $\mathbb P(N=\infty)=1$, and so by monotone convergence (applied to $-X_n$) $$\mathbb E\left[\sum_{i=1}^N X_i \right] = \mathbb E\left[ \sum_{i=1}^\infty X_i \right] = \sum_{i=1}^\infty \mathbb E[X_i] = -\infty. $$
(This is assuming $\mathbb E[X_1]>-\infty$ - else the sum is clearly $-\infty$.)
Since $\mathbb P(N=\infty)=1$, we have $\sigma(N) = \{\varnothing, \Omega\}$ - so conditioning on $N$ does not change anything, and $$\mathbb E\left[\sum_{i=1}^N X_i\mid N\right] = \mathbb E\left[\sum_{i=1}^N X_i\right] =-\infty.$$


Evaluation of $ \int \sqrt{1+\cos^2 x}\,dx$

Question:
Evaluation of $\displaystyle \int \sqrt{1+\cos^2 x}\,dx$

$\bf{My\; Try::}$ Let $$I = \int \sqrt{1+\cos^2 x},dx$$
Put $\cos x= t\;,$ Then $\displaystyle \,dx = -\frac{1}{\sin x}dx = -\frac{1}{\sqrt{1-t^2}}\,dt$
So $$I = -\int\sqrt{\frac{1+t^2}{1-t^2}}\,dt$$
Now How can i solve it after that, Help required, Thanks
Comment: https://en.wikipedia.org/wiki/Elliptic_integral#Incomplete_elliptic_integral_of_the_second_kind Doesn't look promising
Comment: This is an elliptic integral, you will not find an elementary antiderivative.
Comment: https://www.integral-calculator.com/
Answer: That is not an evaluable integral. In terms of elementary functions I mean, unless you consider Elliptic integrals as elementary.
First of all, we cannot even try any numerical or Series evaluation since the integral is unbounded.
Your way of substitution is good, because as other users have already pointed out, the result is in terms of Special functions called the Elliptic Functions. That is:
$$\int\sqrt{1 + \cos^2(x)}\ \text{d}x = \sqrt{2}\ \text{E}\left(x, \frac{1}{2}\right)$$
Where $E$ stands for the elliptic integral of Second Kind.
More here: https://en.wikipedia.org/wiki/Elliptic_integral


FCM returns error : NotRegistered after the first message

Question: I followed the instructions on firebase.google.com to add FCM in my project. Everything was fine, I've got the message "FirebaseApp initialization successful" and fresh token in my logs. But It's not working... Only the first push notification that I send to the token I get success. The second push and rest get error "NotRegistered".
Documentation says

If it is NotRegistered, you should remove the registration ID from your server database because the application was uninstalled from the device, or the client app isn't configured to receive messages.

but application wasn't removed and client app is configured to receive messages because the first message comes to the client
Comment: Check if the `onTokenRefresh()` method is being called. This means that the token is updated and you have to use the new token.
Are you using the firebase console to send the notification? or you are using your own server implementation.
Comment: GeorgeLBA , onTokenRefresh in my case is called only once at the first start of programm or after cleaning all apps data. I'm using my own server implementation
Comment: Nikita, Try printing the token after you send the first notification. Just to make sure that the token is not changing. Use `FirebaseInstanceID.getToken()` and verify if the token is the same after you send the notification. If it is null is because the token was removed for some reason.
Comment: I have same problem... FirebaseInstanceId.getInstance().getToken() return same token which is on the server. When I tested on my machine with curl so result is "NotRegistered"
Comment: Any insights on this? I have the same issue.
Comment: We have been getting this in Firebase for web esp in case of chrome
Comment: facing same issue .. anyone solved this?
Comment: it's happening to me too
Answer: I struggled for hours but I found the problem. It was really hard to find and explain as well. I'll try:
My project has a dependency with another dependency inside that:
My project > Dependency > Sub-dependency
The sub-dependency contains an AndroidManifest.xml file with <application> tag. Something like that:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my_sub_library">

    <application android:allowBackup="true">

    </application>

</manifest>

Because of this application tag, my dependency needed to do it:
<manifest package="br.com.ilhasoft.support.databinding"
    xmlns:tools="http://schemas.android.com/tools">
    <application tools:node="replace"/>
</manifest>

My solution was to update sub-dependency by removing the application tag and update dependency removing the replace action.
Somehow, firebase was ignoring my FirebaseMessagingService and revoking my FCM registration token.
I suggest you check your dependencies to see which one has application tag and update/replace it. It worked for me!
Comment: Isn't that 'sub-dependency' in an npm package? If it is, how did you updated it?
Comment: I got a crash when I used `replace` in the application tag.


Reference Request: Neural Networks as Mathematical Models

Question: Could you point out some reference books [accessible to an undergrad math student] that deal with the mathematical modelling aspect of neural networks?
Answer: This is a nice article concerning the mathematical modeling of the neural system of the nematode C. elegans (the only species for which the neural system is completely known up to the cellular level). Perhaps you find the article or its references to be interesting.
Hopefully someone else can suggest a reference for a more mathematical approach to modeling neuronal networks - the above article is written from a biological viewpoint.
Answer: This is perhaps a more computational/physical treatment, but there is still some good mathematics, at least in my opinion, as well.
https://www.scribd.com/doc/311373901/Neural-Networks-and-the-Brain


There is no REQUIRED field on the attribute of a view model but still form submission fails .Net 6

Question: This is view model through which I am passing data :
 public class DriverRegisterViewModel

    {
            [Required]
            public string Name { get; set; }
            [Required]
            [EmailAddress]
            public string Email { get; set; }
            [Required]
            public string Password { get; set; }
            public string MechanicTypesNames { get; set; }
            public string Location { get; set; }
            public string Latitude { get; set; }
            public string Longitude { get; set; }
    }

and facing the following error in response :
errors: {Latitude: ["The Latitude field is required."], Location: ["The Location field is required."],…}
status: 400
title: "One or more validation errors occurred."
traceId: "00-8c51a8400365bcce1564833f6a2659bd-492eeda6ab8c61f1-00"
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1"

Answer: use [ValidateNever] attribute on fields that are not Required.


How to display data from multiple json files at once?

Question: I'm dynamically creating the json urls based on what is currently saved inside a browser cookie.
var cookie = $.cookie('faved_posts');

The contents of that cookie are 123456, 111111, 000001, and so on.
Now I'll need to request the json file for each ID that was saved in that cookie.
I have the following:
$.each(cookie, function(i, val){
    $.getJSON('/ajax/posts/'+val+'.json', function(data){
        var html = "
" + data.post.headline + "
";
        $("#container").html(html);
    });
});

I'm able to retrieve all the files
/ajax/posts/123456.json
/ajax/posts/111111.json
/ajax/posts/000001.json
The problem I'm having is I can only render the data.post.headline value from only one of the ajax files.
This is what I need outputted in html:

headline for 123456

headline for 111111

headline for 000001

What is the best way to do something like this? Thanks
Comment: Use `$("#container").append(html)`
Comment: I should probably also use jQuery's $.when they're all .done().
Comment: Wow so simple smacked my head. Thanks!
Answer: $("#container").html(html);

.html() will replace everything that is currently in #container which would mean you would only see the html from the last execution of the loop. I believe you are looking for append() instead.


How to recieve message in perticular activity when sending two different broadcast messages for different activity?

Question: I am trying to understand a Broadcast reciever, Here I have tried registering Reciever programatically so that i can get message from MyReciever class to perticular activity through interface call back, It worked fine. But now  i have commented my programatic registering of reciever and I have registered reciever in menifest file.I am recieving the message in MyReciever's onRecieve() method as expected, the problem is how to render that message to perticula activity? In this case my listener will be null i cant use call backs here as per my knowledge (Correct me if i am wrong). How can I achieve this?
Note: Here i am sending two BroadCasts(when on Back pressed), I need to render that or get that message in perticular activity say when he clicks on the back button in SecondActivity then in MainActivity i should get that message, if he clicks back button in fouth activity then i should get that message in ThirdActivity(Reciever registered in Menifest file not programatically).
Complete Code Link: http://pastebin.com/eTwxMcHh ,
Your help Highly appreciated.
Answer: Instead of using startActivity use startActivityForResult for starting Second activity from MainActivity.
MainActivity mButton.setOnClickListener code change:
<code>Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 9999); //changed startActivity to startActivityForResult.
</code>
In Second activity onBackPressed()  do:
<code>@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent();
    intent.setAction("com.navi.test");
    intent.putExtra("INFORMATION","SENT FROM SECOND ACTIVITY");
    //sendBroadcast(intent);
    setResult(Activity.RESULT_OK, intent);
    finish();
}
</code>
So instead of sendBroadcast(), you can setResult and intent, and in your MainActivity implement onActivityResult()
<code>@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && resultCode == 9999) {
        //Read your values from data and implement your action.
    }
}
</code>
So this way you will not require any Broadcast receiver, instead you are sending result back to previous activity by setting RESULT_OK and intent with required result values.
Comment: hey thank you for ur reply, This i can do but i am trying to understand Broadcast Reciever here, is that possible to get the same result using broadcast reciever? i can do it registering reciever programmatically bec i can get the call back to perticular activity but globally registering reciever i.e in menifest file i am facing problem bec i am not able to get the call back bec the listener will be null at that time..:(
Answer: You can achieve using broadcast receiver but still you have to pass intent to your activity for example just override in your activities.
but don't forgot to add intent.addFlags (FLAG_ACTIVITY_SINGLE_TOP);
<code>@Override
   protected void onNewIntent(Intent intent) {
      ... use intent info to do your stuff
   }
</code>
Comment: after search bit found this answer its same http://stackoverflow.com/questions/18672482/how-to-pass-intent-with-extras-to-an-already-running-activity


determinging dhcp lease period

Question: As I understand, it is responsibility of the client to send renew requests periodically. How long is this period? Can I receive this information once IP address is dynamically assigned do my interface? Does DHCP protocol provides me this information or I should ask it from the server administrator?
Comment: Welcome to Server Fault, a site for IT professionals. We've tried to give you answers appropriate to system administrators, but your question now indicates that you are an end user. It will likely be closed for this reason. If you are interested in learning what your DHCP lease time is, you may wish to visit our sister site [Super User](http://superuser.com/). Before posting there (or here!) you should check the FAQ to learn what is on topic, and search to see if your question has been asked before. You should also clarify the question to specify your operating system, etc.
Comment: I do not understand why you deleted my comment where I explain that my question is not appropriate in Superuser. Superuser explicitly says that it is not intended for corporate networks. Secondly, we have nobody discovered duplicate of this question, so your blaming me that I did not do the search is inappropirate also. So, we have inappropriate and inadequate administration in the Serverfault.
Comment: @Val The comment was deleted due to combative tone, much like you're showing in your new comment. These are community run sites with not as much inter-site communication as any one would like. Inadequate administration is part and parcel to StackExchange, we just try to minimize it as much as possible.
Comment: There is nothing combative when person is wondering why he is sent to inappropriate forum. I used analogy with fruits (if I consume apples at home then you should discuss it at householding forum, you say, not our professional fruit forum). There is nothing combative in this analogy. Sending me into inappropriate discussion board, that is combative.
Comment: @Val: You don't appear to be a corporate network admin which puts you outside our scope too. This doesn't seem like a corporate network question either - it's a user level question about a corporate network as your edit reveals. http://meta.serverfault.com/a/3397/9517 is interesting in this regard.
Answer: The client will initially try and renew the lease from the DHCP server that supplied it when 50% of the lease time has expired. If it gets no response by the time 87.5% of the lease time has expired the client will try to obtain a new lease from any DHCP server that will respond.
If it gets a response then the timers are reset to 0 and restarted.
Comment: To compute 0.5 and 0.87, I need to know what is 1!
Comment: @Val: the client knows this
Comment: @Val: you need to tell us what your client is so we can tell you the rest.
Comment: I use two clients: Microsoft Windows 7 (DHCP works fine) and lwIP (I need DHCP there)
Comment: @Val: windows 7 ipconfig/all will tell you under Lease Obtained and Lease Expires
Answer: The validity period of the lease is however long you set it up to be on the DHCP server. Clients typically renew the lease once 50% of that time has elapsed, though actual DHCP client implementations may vary.
Comment: can server tell me the period so that I can determine what is 50%?
Comment: Just look at the server's configuration. You didn't say what DHCP server you were running.
Comment: I am asking whether I can receive that information over DHCP.
Comment: Yes, it's sent to your client, obviously.
Answer: The lease time is sent in a DHCP option.
Here, excerpt of tshark -V output:
Option: (t=51,l=4) IP Address Lease Time = 3 days
    Option: (51) IP Address Lease Time
    Length: 4
    Value: 0003F480

From dhcpoptions(5):

option dhcp-lease-time uint32;
This option is used in a client request (DHCPDISCOVER or DHCPREQUEST)
     to allow the client to request a lease time for the IP address.  In a
     server  reply  (DHCPOFFER),  a  DHCP server uses this option to
     specify the lease time it is willing to offer.
Comment: Thanks, I see, it matches the difference between Lease Obtained and Lease Expires in ipconfig /all.

determinging dhcp lease period


Using the determinant to solve a system of equations

Question: Here is the question: Factorise the determinant
$$\text{det}\begin{pmatrix}
z & 1 & 2 \\
1 & z & 3 \\
1 & 1 & z+1
\end{pmatrix}$$
and hence solve the system of equations
$$\left\{\begin{matrix}
zx+y=2\\
x+zy=3\\
x+y=z+1
\end{matrix}\right.$$
I've factored the determinant to $(z-1)(z^2+2z-4)$, but I fail to see how to use the determinant to solve this system.
How can the determinant actually be used for this? After doing some research into potential methods, I came across something called Cramer's Rule (although I do not believe that this would be the intended method). Not to mention, it seems to only be useable for linear/planar equations.
Answer: If you don't mind, I would like to offer a solution without using determinants. If we add the first two equations,
$$x+y+zx+xy=5 \implies (x+y)(z+1)=5$$
From the third equation, $x+y=z+1$
$$\therefore (z+1)^2=5 \implies z=-1\pm\sqrt5 \implies x+y=-1\pm\sqrt5$$
You can solve for $x$ and $y$ using the above.
Hope this helps.
Answer: The system of linear equations can be written as
$$\begin{bmatrix}
z&1&2\\
1&z&3\\
1&1&z+1
\end{bmatrix}
\begin{bmatrix}
x\\
y\\
-1
\end{bmatrix}=
\begin{bmatrix}
0\\
0\\
0
\end{bmatrix}\tag1
$$
If the matrix on the left is invertible there is no solution, so the only possibility is that $z$ is such that the determinant vanishes.  Once you know the possible values of $z$, you can substitute them in $(1)$ to get ordinary systems of linear equations with constant coefficients.


Exibir valor de array no php

Question: Estou colocando valores em um array e preciso exibir esses valores do lado dos titulos nos th de uma table.
Não quero usar um loop para percorrer o array, quero acessar o valor pelo indice.
Segue minha função que alimenta o array e envia para a view
public function index() {

    $novas = $this->model->countSituacaoNova();
    $analise = $this->model->countSituacaoAnalise();
    $aprovada = $this->model->countSituacaoAprovada();
    $reprovada = $this->model->countSituacaoReprovada();
    $pendente = $this->model->countSituacaoPendente();
    $paga = $this->model->countSituacaoPaga();

    $data = array(
        'novas' => $novas,
        'analise' => $analise,
        'aprovada' => $aprovada,
        'reprovada' => $reprovada,
        'pendente' => $pendente,
        'paga' => $paga
        );

    $situacoes['situacao'] = $data;

    //var_dump($data);
    $this->load->view('propostas_view', $situacoes);
}

Segue um trecho da view com a table na qual tento usar o valor do array

        Em análise (<?php echo $situacao['novas']; ?>)
        Aprovadas
        Reprovadas
        Pendentes
        Pagas

meu array $novas tem isso:
  array (size=1)
 0 =>
   object(stdClass)[22]
     public 'Novas' => string '1' (length=1)

Erro: Message: Array to string conversion
Comment: Da um dd() no seu array e ve o conteudo dele
Comment: Qual erro que da ?
Comment: `Message: Array to string conversion`
Comment: A variável `$novas` está recebendo um valor inteiro mesmo? Cheque se não está vindo um array.
Comment: verdade.. está vindo um array em `$novas`
Answer: Com a sua descrição para o array $novas, o valor pode ser obtido da seguinte forma, pois há o Objeto "Novas" dentro da posição 0 desse vetor:
$novas[0]->Novas
Comment: `Undefined index: Novas`
Comment: qual das 3 tentativas você utilizou?
Comment: a primeira tentativa.. na segunda o mesmo problema
Comment: experimente trocar por $query->result()[0]['Novas']; e $query->result()[0]->Novas;
Comment: não deu certo... editei a pergunta
Comment: editei minha resposta aqui também
Answer: Você tá recebendo um array no índice "novas". Por isso que tá sendo disparado o erro.
Se sua model retorna um array:
Troque esse trecho:
<?php echo $situacao['novas']; ?>
Por esse:
<?php echo $situacao['novas']['Novas']; ?>
Se sua model retorna um objeto:
Troque esse trecho:
<?php echo $situacao['novas']; ?>
Por esse:
<?php echo $situacao['novas']->Novas; ?>
Comment: editei a pergunta e coloquei o array `$novas`... tentei mas deu o seguinte erro: `Undefined index: Novas`
Answer: Se o seu model retorna apenas um registro deveria usar result_row() no lugar de result() que devolve mais de um e necessita de for/foreach.
Para acessar o valor corretamente na sua view faça:
foreach($situacao['novas'] as $item){
   echo $item->Nova .'
';
}

Ou para um registro com result_row()
echo $stituacao['novas']->Novas;

Exibir valor de array no php


How to implement NullText in a TextBlock with Binding?

Question: I would like to implement a "NullText" behavior for a TextBlock that is bound to a property in a ViewModel. When that property in the ViewModel is null or empty, I would like to display gray italic text something like "No Data". I'd like this to follow MVVM pattern but I am lost...
Update
So after playing around with the solution James Webster suggested, I got it to work like this...
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <c:NullOrEmptyValueConverter x:Key="NullOrEmptyValueConverter" Text="(No Data)"/>
</UserControl.Resources>

 ...

<TextBlock Name="SerialNumberTextBlock" Text="{Binding Path=SerialNumber, Converter={StaticResource NullOrEmptyValueConverter}}">
    <TextBlock.Resources>

        </Style>
    </TextBlock.Resources>
</TextBlock>

Comment: What's your existing view and view model code?
Comment: Actually, the ViewModel is an ObservableCollection of an object that is itself observable. This collection is then bound to an ItemsControl. Then in the ItemsControl.Template, I have a TextBlock bound to a property of the item in the collection. If that property is empty or null, I want "No Data" to show in the ItemsControl in the View.
Answer: I'd recommend implementing an IValueConverter; if the source value is not null or empty, then pass it through to the TextBlock. If the source value is null or empty, then render your chosen text.
public class NullValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string str = (string)value;
        if (str.IsNullOrWhitespace())
        {
            return "No Data";
        }
        return str;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ... //An empty implementation I expect...
    }
}

However I have just realised that you want to set the style as well... hmmm, probably a DataTrigger that sets the style if the value is 'No Data' required I expect;
<TextBlock Text="{Binding Path=SomeProperty, Converter={StaticResource keyToNullValueConverter}">
    <TextBlock.Triggers>
        <DataTrigger Binding="{Binding Path=Text}" Value="No Data">
            <Setter Property="FontStyle" Value="Italic"/>
        </DataTrigger>
    </TextBlock.Triggers>
</TextBlock>

Something along those lines might work.
Comment: +1, but you should fix this: `string str = (DateTime)value;` ;)
Comment: I like this idea. I will have to give it a try!
Comment: I keep getting this exception:

A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

Additional information: Triggers collection members must be of type EventTrigger.
Comment: Ok, after playing around with it for a while I got it to work...see my edited original post
Answer: I think you don't need to create Converter Class, you can simply write your style code like this.

</Style>

Note :- You need to include the system namespace as
xmlns:System="clr-namespace:System;assembly=mscorlib"

Comment: What would you do to set an alternate text, e.g. "No Data" ?
Comment: Add another setter on each DatatTrigger to set the required text.
Answer: You could try to bind to a property that looks thus
    private string _textBlockText;
    public string TextBlockText
    {
        get
        {
            if(string.IsNullOrEmpty(_textBlockText))
            {
                return "No Data";
            }
            return _textBlockText;
        }
        set
        {
            _textBlockText = value;
        }
    }

and then use the XAML that James has mentioned for styling. Saves the need for a converter.
Comment: I think this would work in a single instance but when there are multiple TextBlocks and values to apply the same NullText to, I think the converter approach is better. Thanks anyway though!
Answer: Very late to the party, but here is my answer.
<TextBox >
    <TextBox.Style>

        </Style>
    </TextBox.Style>
</TextBox>

My answer assumes that you want the default text to disappear on focus, which is more in line how inputs with default text usually behave. Also this works only for null values. If you need a check for string empty or additional logic, you could instead use a converter like in other provided answers. The key idea is, that you remove null value text or converter in the binding when control gains focus and thus only display the default text when control has no focus. This also prevents the default value flowing back to your view model.
DataTriggers part for font style was kindly borrowed from pchajer's answer.


Data APIs/feeds available as packages in R

Question: EDIT: The Web Technologies and Services CRAN task view contains a much more comprehensive list of data sources and APIs available in R.  You can submit a pull request on github if you wish to add a package to the task view.

I'm making a list of the various data feeds that are already hooked into R or that are easy to setup. Here's my initial list of packages, and I was wondering what else I'm missing.
I'm trying to limit this list to "real time" or "close to real time" data feeds/APIs, where the underlying data might change between downloads.  There's plenty of lists out there for static datasets, which only require one download.
This list is currently biased towards financial/time series data, and I could use some help expanding it into other domains.
Free Data:
Data Source -  Package
Google Finance historical data - quantmod
Google Finance balance sheets - quantmod
Yahoo Finance historical data - quantmod
Yahoo Finance historical data - tseries
Yahoo Finance current options chain - quantmod
Yahoo Finance historical analyst estimates - fImport
Yahoo Finance current key stats - fImport - seems to be broken
OANDA historic exchange rates/metal prices - quantmod
FRED historic macroeconomic indicators - quantmod
World Bank historic  macroeconomic indicators - WDI
Google Trends historic search volume data - RGoogleTrends
Google Docs - RGoogleDocs
Google Storage - RGoogleStorage
Twitter - twitteR
Zillow - Zillow
New York Times - RNYTimes
US Census 2000 - UScensus2000
infochimps - infochimps
datamarket - rdatamarket - requires free account
Factual.com - factualR
Geocode addresses - RDSTK
Map coordinates to political boundaries - RDSTK
Weather Underground - Roll your own
Google News - Roll your own
Earth Sciences netCDF Data - Roll your own
Climate Data - Roll your own
Public health data - Roll your own
OAI Harvester - Open Archives Initiative harvester
RAmazonS3 -  S3 Amazon storage server
Rflikr - Flikr api
Requires a subscription:
Bloomberg - RBloomberg
LIM - LIM
Trades and Quotes from NYSE - RTAQ
Interactive Brokers - IBrokers
Comment: Right now I'm defining "easy to setup" as "the package exists" or "someone's posted/published a function to download the data"
Comment: depends on what you mean by 'easy to set up'... I have a number of custom scripts that are 'easy to set up'
Comment: I would further define "the packages exists" as "the package is on CRAN" or "the package is on R Forge," "the package is on some CRAN-like, public repository."
Comment: Addition: [Federal Register API](http://christophergandrud.blogspot.fr/2012/09/federal-register-apir-package-ideas.html) (in the works).
Comment: This thread may also be of interest: [Import stock price from Yahoo Finance into R](http://stats.stackexchange.com/questions/8407/).
Comment: I'm using a lot MERRA data from the NASA http://gmao.gsfc.nasa.gov/research/merra/intro.php I don't know if there is an API but I wrote some code to do the scrapping of any variable in the past and still using it... should clean it some day to make a package
Comment: @robingirard If you do, add it to the [Web Technologies and services](https://github.com/ropensci/webservices) CRAN task view!
Answer:
Instructions for using R to download netCDF data can be found here, a common format used for storing Earth science data, e.g. as in marine geospatial data from OpenEarth or climate model driver and forecasts from UCAR
rnpn (under development) enables you to get data from the National Phenology Network - a citizen science project to track the timing of plant green-up, flowering, and senescence. See the developer's blog post.
-obsolete- RClimate provides tools to download and manipulate flat-file climate data (with tutorials, including here-
Download historical finance data with tseries::get.hist.quote
Michael Samuel's documents downloading public health data
raster::getData provides access to climate variables via worldclim
Comment: What is "netCDF data?"
Comment: @Zach [NetCDF](http://www.unidata.ucar.edu/software/netcdf/) is a kind of universal structured way of storing large datasets. An alternative approach relies on the [HDF5](http://www.hdfgroup.org/HDF5/) format.
Comment: @David (+1) Many thanks for the fourth point!
Comment: What happend to RClimate? I cannot find the package anywhere
Comment: now there is a ncdf4 library on R to handle netcdf4. It is more complicated to install than ncdf but the later is deprecated I think. A few years ago I even managed to install netcdf4 with Min GW64 to make this package work on windows :) if I did it, it means it's feasible ... and it seems that D. Pierce (author of the package) is providing sources on his web site http://cirrus.ucsd.edu/~pierce/ncdf/
Answer: There's a project aimed at creating R packages with this objective (R interface to real-time APIs) called rOpenSci, which has 18 packages currently available or in development. Some (rnpn, rfishbase) are on your list already.
Great list!  and full disclosure - I'm part of the rOpenSci project.
Answer:
ONETr - efficient interaction with the O*NET™ API, offering occupational descriptor data from the U.S. Department of Labor.


How can I test two samples for different variance?

Question: I am using an accelerometer to test for vibration. To clarify the physics, imagine an accelerometer (which measures acceleration) is sitting on top of a washing machine.  On the spin cycle the accelerometer will move up and down, giving a continuous(ly changing sample) reading with the mean being earth's gravity.  Put one wet towel in the machine, and the accelerometer will move up and down more, with (aprox) the same frequency. How can I tell if there is a wet towel?
As long as my samples are larger than the period (and the period of any harmonics I guess) then the mean is going to be constant. I want to test for change (quickly, and preferably formally) . The undergraduate text book I have doesn't seem to help; I was hoping to use the apache commons stats package:
https://commons.apache.org/proper/commons-math/userguide/stat.html
I guess, given the above, I can take the mean of just the positive values from both samples and perform a one tailed test, but the distribution of the positive values will be awfully skewed...
The discussion here: Compare the variances of several groups
Does seem to be the answer (note the comments below on ANOVA).
I am surprised my problem is not more mainstream - variance of variance, I would have thought, would have many uses. Mean number of people in hospital dying of the flu/covid-19 for instance is quite constant across the country I believe, but the rate in individual hospitals presumably varies enormously as the disease spreads.
P
Comment: This question could profit from a better description of the data and the question to answer , the kind of change that is to be detected..How is the title connected to the question?
Comment: Despite the first comment to the linked question, analysis of variance (ANOVA), either parametric or nonparametric, is the wrong technique for this. ANOVA is a test of mean equality that uses variances in a way clever enough to get that word in the name, but it does not compare the variances of multiple groups.
Comment: [Compare the variances of several groups](https://stats.stackexchange.com/questions/83712/compare-the-variances-of-several-groups)
Comment: Dave thanks for clearing that up. I had to go back and see if I had foolishly mis-remembered what an anova does and was about to say that anova compares variances in means; I want to compare variances in variances (presumably std deviations).
Comment: Re "not more mainstream:" based on your description of *first* observing baseline acceleration and *then* "putting in a wet towel,* this can be framed as a *very specialized* form of *changepoint* problem (*q.v.*). It is specialized because (a) you can collect streams of (b) large amounts of (c) highly autocorrelated (d) multivariate data and (e) you are interested in a change in variance rather than a change in level. There are still many standard solutions, depending on how you run your experiment and whether you know when the towel was introduced.
Comment: I'd like to use a standard solution if I can,  Whuber's description is great - a change in variance rather than a change in level is exactly what I want. I am expecting I can detect when the towel is added (or the bearing fails) from my stream of large amounts of autocorrelated multivariate (?) data. I will look up "changepoint problem" and see how I go.
Answer: Although it still seems odd that variance of variance is not something people think about, it turns out that as Whuber says, what I really wanted was on-line change point detection.  With those key words in hand, I am deciding that there are lots of interesting ways to make change point detection better but for me, a really naive approach based on how the signal is generated is probably as good as it gets.


Ideals of $\mathbb{Z}[X]$

Question: Is it possible to classify all ideals of $\mathbb{Z}[X]$? By this I mean a preferably short enumerable list which contains every ideal exactly once, preferably specified by generators. The prime ideals are well-known, but I'm interested in all the ideals. I couldn't find any literature about this. I don't even know the number of generators we need (Edit: Hurkyl has pointed out that any number appears!).
An ideal of $\mathbb{Z}[X]$ restricts to some ideal of $\mathbb{Z}$, say $n \mathbb{Z}$. For $n>0$ the ideal corresponds to some ideal in $\mathbb{Z}/n [X]$, and by CRT we may assume that $n$ is a prime power, say $n=p^k$. For $k=1$ we have the PID $\mathbb{F}_p[X]$, whose ideal structure is well-understood. What happens for $k=2$? The case $n=0$ seems to be even more complicated.
Since $\mathbb{Z}[X]$ is noetherian, every ideal is an intersection of primary ideals, and we should classify them first? What about other special classes of ideals, for example radical ones?
If the question is too naive in this generality, what about special cases, or slight weakenings? For example, is it possible to classify all (reduced, finite, coprimary, ...) commutative rings which are generated by a single element? This comes down to the classification of the ideals of $\mathbb{Z}[X]$ modulo isomorphic quotient rings.
Comment: The ideal $(2,x)^n$ requires $n+1$ generators.
Comment: Also, an ideal requiring $2$ generators may still satisfy $I \cap \mathbb{Z} = 0$, such as $I = (x^2, 2x)$
Comment: To address the radical ideal blurb: If you know the prime ideals you know the radical ideals: they are just all possible intersections of prime ideals.
Comment: @rschwieb: Oh, yes! Is it also possible to give generators? I will think about this ...
Comment: @MartinBrandenburg Beyond my ken :) Glad you asked this question so that I can slowly learn commutative algebra too...
Comment: In http://math.stackexchange.com/questions/187530/ I described the classification of ideals of length $4$ in $k[x,y]$ for $k$ a field. There is a heuristic that $\mathbb{Z}[x]$ should be similar to $k[x,y]$ but harder, so this should give you some idea of what an answer to this question might look like.
Comment: http://math.stackexchange.com/questions/110458/construct-ideals-in-mathbb-zx-with-a-given-least-number-of-generators
Answer: First some simple facts:
The ring $\mathbb{Z}[X]$ has dimension $2$ and it is a unique factorization domain with unit group $\{\pm 1\}$. We immediately conclude:

Every ideal has height $0$, $1$, or $2$.
The only ideal of height $0$ is $(0)$.
If the primary decomposition of an ideal contains only height $1$ ideals, then:

The ideal is principal
These ideals are in one-to-one correspondence with nonzero polynomials with positive leading coefficient

Now let $I$ be a nonprincipal ideal. Suppose $I \cap \mathbb{Z} = (0)$. Then $I \otimes \mathbb{Q}$ is a proper, nonzero ideal of $\mathbb{Q}[X]$, generated by a primitive polynomial $g(x)$ (i.e. integer coefficients of gcd 1). It follows that $I = g(x) J$ where $J \cap \mathbb{Z} \neq (0)$.
Alternatively, $g(x)$ can be obtained a the gcd of all elements of $I$. (the $g(x)$ so constructed would have content $n$ rather than $1$ if $I$ is divisible by $(n)$). But the above is the method of proof I thought of to show the cofactor contains an integer.

Taking a different tactic, essentially by the Euclidean algorithm any ideal has a basis of the form
$$ I = \langle g_i(x) \mid 1 \leq i \leq k \rangle $$
where

$g_i(x) = c_i x^{n_i} + f_i(x)$

where $c_i \in \mathbb{Z}$, $c_i > 0$, $\deg f_i(x) < n_i$

$n_i < n_{i+1}$
$c_{i+1} | c_i$ and $c_i \neq c_{i+1}$

For example, the ideal
$$ \langle 8, 4x + 4, 2x^2 + 2 \rangle$$
In fact, I'm pretty sure this works out to a normal form by the algorithm:

For each $d$, pick (if any) the smallest polynomial of degree $d$ in $I$
Throw out any polynomial whose leading term is divisible by a picked polynomial of lesser degree

"Smallest", here, is determined by first comparing the coefficients on $x^d$, and if those are equal comparing the coefficients on $x^{d-1}$, and so forth. Integers are compared by absolute value first, and if equal, $n$ is considered smaller than $-n$.
e.g. the polynomial $4x + 4$ is considered smaller than $8x$ and than $4x-4$.
Unfortunately, not every choice of $g_i$'s is admissible. For example, the ideal
$$ \langle 4, 2x+1 \rangle $$
actually has normal form
$$ \langle 1 \rangle $$
I conjecture a sequence of reduced $g_i$'s (meaning we reject $\langle 8, 4x-4 \rangle$ because we can reduce $4x-4$ to $4x+4$ by a monomial multiple of $8$) satisfying the above properties is the normal form of an ideal if and only if $c_i \mid f_i(x)$.
Alas, I don't know enough about the theory of Groebner bases over Euclidean rings to say for sure.
Comment: Thank you. Could you explain a little bit more the paragraph beginning with "Now let $I$ be a height $2$ ..."? I don't really understand your arguments.
Comment: @Martin: If $I$ contains no integers, then we can invert every integer without killing $I$, making it an ideal of $\mathbb{Q}[X]$.
Comment: How do you define $J$, and how do you prove $I=g(x) J$?
Comment: @Martin: If $f \in I$, then there exists $q \in \mathbb{Q}$ and $h \in \mathbb{Q}[X]$ such that $fq = gh$. By adjusting $q$, we may assume $h \in \mathbb{Z}[X]$. If $g$ has content $1$, then $f = gh/q$ implies $g | f$ (and that $h/q \in \mathbb{Z}[X]$).
Comment: Dear Hurkyl, I think a height $1$ ideal is not necessarily principal, e.g. $(6, 2X)$; and a height $2$ ideal contains necessarily a non-zero integer (see my answer). Your example $I=g(X)J$ is height $1$.
Comment: @QiL'8: Oh bother, you're right: height is an *inf* and I wanted a *sup*. :(
Comment: Unfortunately the conjecture at the end is incorrect: see remark 4 in my answer for a counterexample
Answer: There are generalizations of Grobner/standard ideal basis algorithms that work over coefficient rings more general than fields, e.g. certain PIDs (which, in particular, apply to $\rm\,\Bbb Z[x]).$ There is, by now, much literature on such topics. Perhaps the easiest way to chase it down is to start with the references listed in Buchberger's survey [1] on the history of the Grobner basis algorithm.
See in particular the work mentioned in section "Generalizations of the CPC approach for polynomial rings", p. 19. If you chase citations of the work mentioned there, it will quickly lead you to the most recent work on said generalizations.
[1] B. Buchberger. History and basic features of the critical pair / completion procedure.
Journal of Symbolic Computation, 3(1/2) (1987) 3-38
Answer: The earliest such classification (of canonical generating sets of ideals in $\mathbb{Z}[X]$) I could find is from 1952, due to Szekeres. I summarize the main points:
Theorem: Let $I \subseteq \mathbb{Z}[X]$ be an ideal. Let $f \in \mathbb{Z}[X]$ be the polynomial with positive leading coefficient defining the smallest principal ideal containing $I$. Then there exists $m \in \mathbb{N}$, and $q_k, b_{k,i} \in \mathbb{Z}$ satisfying $$ 0 \le b_{k,i} < q_k, \qquad \forall 1 \le k \le m, \; 0 \le i < k \tag{*}\label{*}$$ such that recursively defining $g_k \in \mathbb{Z}[X]$ via
$$
\begin{align*}
g_0 &= \prod_{i=1}^m q_i \\
q_kg_k &= Xg_{k-1} + \sum_{i=0}^{k-1} b_{k,i} g_i, \qquad k > 0
\end{align*}
% g_k = \Big( \prod_{j > k}^m q_j \prod_{j=0}^{k-1} (x + b_{j+1,j}) \Big) + \sum_{i=1}^{k-1} b_{ki} \Bigg[ \prod_{j > i, j \ne k}^m q_j \prod_{j=0}^{i-1} (x + b_{ij}) \Bigg]
$$
one has $I = (fg_0, fg_1, \ldots, fg_m)$. Conversely, for any choice of $f, q_k, b_{k,i}$ satisfying $\eqref{*}$, there is an ideal generated by $fg_0, \ldots, fg_m$.
Remarks:

$f$ can always be obtained as the gcd of any generating set of $I$ (up to sign), so it suffices to consider the case $f = 1$, i.e. $I$ has height $2$, in which case $I \cap \mathbb{Z} \ne 0$ (by expressing $1$ as a $\mathbb{Q}[X]$-linear combination of generators and clearing denominators). This is the setting that Szekeres works in.
It follows from the recursive definition that for all $k$, $\deg g_k = k$ and the leading coefficient $a_k$ of $g_k$ satisfies $a_{k-1} = q_ka_k$. In particular $a_k \mid a_{k-1}$, and $a_m = 1$, i.e. $g_m$ is monic.
(Assuming $f = 1$) The $g_k$ are canonical in the following sense: for each $k$, $a_k$ is the least positive integer occurring as the leading coefficient of a degree $k$ polynomial in $I$ (this property is called minimal by Szekeres, but I avoid this to prevent confusion with the minimality in (5)).
The conditions in (2) alone are not sufficient for a canonical enumeration in the sense of (3). E.g. $(4, 2X+2, X^2+X+1)$ does not occur as a valid $(g_0, g_1, g_2)$, as the ideal would contain $2(X^2+X+1) - X(2X+2) = 2$ (so $4$ is not the canonical generator of degree $0$).
The generating set $fg_0, \ldots, fg_m$ need not be a minimal generating set, but by (3), any non-minimality is "obvious", i.e. only occurs when $q_k = 1$. Thus the generating set can be made minimal by removing the $fg_k$ with $q_k = 1$. E.g. the ideal in (4) is $(2, X^2+X+1)$, which has $f = 1$, $m = 2$, $q_1 = 1$, $q_2 = 2$, $b_{1,0} = 0$, $b_{2,0} = b_{2,1} = 1$, and $g_0 = 2$, $g_1 = 2X$ (which is redundant), and $g_2 = X^2 + X + 1$.
Comment: For a non-paywall link to Szekeres' paper, see [here](https://archive.org/details/sim_american-mathematical-monthly_june-july-1952_59_6/page/378/mode/2up)
Answer:   Say $I$ is an ideal. If $I=0$, we stop.
  If $I\neq 0$, we look at the set of the elements of $I\setminus \{0\}$ with minimal degrees, in this set we pick an element $f_1$ with minimal positive leading coefficient. If $I=(f_1)$, we stop.
  If not, consider the set $I\setminus (f_1)$. Similar as above,  pick an element $f_2$ with minimal positive leading coefficient in the subset of the elements with minimal degree. If $I=(f_1,f_2)$, we stop. Repeat this process. With finite steps, we can stop at a step $I=(f_1,f_2,\ldots,f_n)$.
   We can see that the degree of $f_{i+1}$ is strictly greater than the degree of $f_i$ for $i=1,2,\ldots, n-1$.
  Say the leading coefficient of $f_i$ is $a_i$. Then $a_{i+1}| a_i$ and  $a_{i}$ is strictly greater than $a_{i+1}$. So we can see the length of  the chain  can be bounded by $a_1$. For example, if $a_1=1$, then $I=(f_1)$, if $a_1$ is prime, then $I$ is generated by one or two elements. If $a_1=p^2$, $p$ is prime, then $I$ can be generated by 3 elements.

Ideals of $\mathbb{Z}[X]$