Step with scipy integrator without going past final point

Question: The scipy integrators take two parameters when integrating: (1) step which tells the integrator to take only a single step, and (2) relax which tells the integrator that it is fine to step past the final time point.
I want to take steps (so that I can collect the entire solution), but also not step past the final time (because I have a discontinuity in my real problem). However, step being True seems to ignore relax being False. It will take one step over the final time with abandon.
Here is a simple example illustrating the problem:
from scipy.integrate import ode

obj = ode(lambda t, y: -y)  # Simple exponential decay
obj.set_initial_value(4.0)

final_time = 2.0

ts_new = []
ys_new = []

# Take one step at a time until the final time is reached
while obj.t < final_time:
    y_new = obj.integrate(final_time, step=True, relax=False)

    ts_new.append(obj.t)
    ys_new.append(y_new)

print(ts_new[-1])  # 2.073628416585726

I would have expected the last step to be a partial step (is that what it's called?) and stop exactly on final_time, such as the value given by obj.integrate(final_time, step=False, relax=False). Other than rerunning the entire ODE, is there a way to get the last point? I would be fine with setting step to False just before the last step, but I see no way to know if it will step over the final time until it does.
Comment: What do you expect it to do when you want to take a single step but you can't, because that's beyond the final time? It makes sense that these two options are incompatible. But looking at your code: why not just let it take that final step, but check in the loop after calling `integrate` and throw away the final point? What am I missing?
Comment: When `relax` is `False`, it doesn't stop before `final_time`, it stops exactly on it. When `step` is `True`, I would have expected it to still take a partial step and stop exactly on `final_time`. (Question edited to reflect this.)
Comment: Does your discontinuity at `final_time` not affect the solution at exactly that point? And how big is the difference there: can't you use a final Euler-step or something to solve for the final time value? You could also try setting the `min_step`, `max_step` parameters of `vode` (the [default integrator](http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.ode.html))
Comment: My final time is actually a small delta before the discontinuity, so it doesn't affect it exactly at that point, but it is important to not step over the time. I have thought about throwing away the last point and restarting, but that still steps over and might trigger an error. The max step is a good idea and might work if I can avoid floating point subtraction issues.
Answer: A passable solution is to throw away the point that steps over the final time and create an fresh integrator that starts at the penultimate time and integrates step=False to the final time. I have not timed it, but I suspect that this is also somewhat expensive.
from scipy.integrate import ode

obj = ode(lambda t, y: -y)  # Simple exponential decay
obj.set_initial_value(4.0)

final_time = 2.0

ts_new = []
ys_new = []

# Take one step at a time until the final time is reached
while obj.t < final_time:
    y_new = obj.integrate(final_time, step=True, relax=False)

    if obj.t <= final_time:
        ts_new.append(obj.t)
        ys_new.append(y_new)
    else:
        t_last = ts_new[-1]
        obj = ode(lambda t, y: -y)
        obj.set_initial_value(ys_new[-1], t_last)
        y_new = obj.integrate(final_time, step=False, relax=False)
        ts_new.append(obj.t)
        ys_new.append(y_new)

print(ts_new[-1])  # 2.0

Note that it gives the correct final time. This does not protect against errors during integration caused by stepping over the discontinuity, so those will have to be caught in production software to trigger the bonus step.
There are undoubtedly some extra steps as the bonus integrator revs up, but those are quietly tossed away by step=False. I tried to set min_step to something approximately equal to the full distance to final time to give it a hint, but that just blew up the integrator.
Answer: A bad solution is to set the max_step to final_time - obj.t at each step. It is tempting because it does actually work, but the performance is horrendous.
from scipy.integrate import ode

obj = ode(lambda t, y: -y)  # Simple exponential decay
obj.set_initial_value(4.0)

final_time = 2.0

ts_new = []
ys_new = []

# Take one step at a time until the final time is reached
while obj.t < final_time:
    obj.set_integrator('vode', max_step=final_time-obj.t)
    y_new = obj.integrate(final_time, step=True, relax=False)

    ts_new.append(obj.t)
    ys_new.append(y_new)

print(ts_new[-1])  # 2.0

Note that it stops at the correct time. However, it appears that the call to set_integrator rebuilds the integrator, which causes a colossal slowdown. It takes 27 steps to integrate the system in the question, but 36215 steps to integrate the system in this answer. It is not possible to avoid rebuilding the integrator by directly mutating obj._integrator.max_step, as this appears to be a copy of the Fortran parameter and has no effect on the integration.


ANT: Use "," in  without expansion to multiple arguments

Question: The following ANT exec tag does not behave as expected.
<exec executable="c:\scratch\test.cmd">
    <arg value="A,B,C"/>
</exec>

When executed, I would expect this to call text.cmd with 1 argument.  However, the arg is being expanded to three separate arguments.
As per the Manual, value is supposed to pass the contents as a single argument, however, it is passed as three (one for each component of the string separated by a comma).
I tried replacing the command "," with a semicolon (;) but this does not work either.  It appears as if arg's value attribute parses the supplied string as if it were a path, which it is not.
Anyone know how to the "A,B,C" to pass as one argument?
For the sake of completeness, my test.cmd file is this:
@echo off
echo Arg1: %1
echo Arg2: %2
echo Arg3: %3
echo Arg4: %4
echo Arg5: %5
echo Arg6: %6
echo Arg7: %7
echo Arg8: %8
echo Arg9: %9

and the output of the ant build is:
 [exec] Arg1: A
 [exec] Arg2: B
 [exec] Arg3: C
 [exec] Arg4:
 [exec] Arg5:
 [exec] Arg6:
 [exec] Arg7:
 [exec] Arg8:
 [exec] Arg9:

Answer: Issue has been resolved.  I was so focused on the issue being in ant, that I didn't take the time to test how DOS like command lines interpret the command-line arguments.
from Window command line, I ran test.cmd a,c,b and see that the command argument was split, therefore, the issue is not related to ant.  so now I just need to figure out how to force ANT to quote the arguments.
Comment: For anyone reading this, to wrap arguments in quotes just using the single quote form, so that double-quotes are ignored by xml parser, and passed to the argument string:   value='"some,text here"'
Answer: Try:
<exec executable="c:\scratch\test.cmd">
    <arg line="A,B,C"/>
</exec>

See ant manual for description of how arguments work.
Comment: the line= property exhibits the same behavior as the value= property.  I still receive the distinctly separate arguments in my test script.
Comment: I am very familiar with the contents of the apache ant manuals, and have review the arguments information, as well as the exec task's pages many times trying to locate anything that suggests that commas will be interpreted as list delimiters and therefore will split the string into multiple arguments.  But nothing in there about this.


Select elements without values in ancestors using XPath

Question: I'm trying to select certain elements in XML for which their ancestors don't have a specific value. For example purposes let's have this XML:
<doc>
   <block platform="mobile">
      <xref>1</xref>
   </block>
   <block platform="desktop">
      <xref>2</xref>
   </block>
   <block>
      <xref>3</xref>
   </block>
</doc>

I need something that selects all xrefs that don't have "mobile" as their platform value in themselves or any of their ancestors. In the example above the query should return xrefs 2 and 3, as neither them or any of their ancestors have platform set to "mobile" (or don't have platform defined at all).
I tried several combinations of xpath queries using ancestors, trying to concat all platform values for ancestors of xref and using not(contains()) to filter out the mobile part, but all with no luck.
Thanks in advance
Answer: You can use this XPath:
//xref[not(ancestor-or-self::*/@platform = 'mobile')]

It's generally good to avoid // if you can. If your XML is always going to have the structure above, you can instead use:
/doc/block/xref[not(ancestor-or-self::*/@platform = 'mobile')]

The part between the []s is the same in both.
Comment: From what I can see right now this solves my needs perfectly. I only started learning XPath 2 days ago so I'm trying to wrap my head around what this is doing, but thanks a lot!


Does the Psycopg2 "copy_from" Function (or the PostgreSQL COPY command) Support Sequence "nextval()" Function Values

Question: I have a database that I am inserting large amounts of rows at a time (~100,000).  From the PostgreSQL documentation, it seems like the use case for the "COPY" command are mostly for moving databases, backing up databases to a CSV, etc.  My use case doesn't seem to be under the same realm as the use cases listed previously, but I believe it would improve the insert performance.
Right now, I am trying to test the Psycopg2 Python library's function "copy_from" command with a small CSV file.  The primary key is a sequence integer that I have previously been using the nextval('seq_key') function to find the value for.  Now that I have switched to the "copy_from" command, I have created a CSV file that holds the values:
nextval('seq_key'),4,1573946481,'Test1', now(),now()
nextval('seq_key'),4,1573946481,'Test2', now(),now()
nextval('seq_key'),4,1573946481,'Test3', now(),now()

I am calling the "copy_from" command with the following code:
    cur = conn.cursor()
    cur.copy_from(test_file, "test_master", sep=",", null="NULL")
    conn.commit()

I am getting the error:
"invalid input syntax for integer: "nextval('seq_key')"
When I remove the nextval('seq_key'), the program ends up working and inserts the rows into the table.  I was wondering if there was something special I would have to do in order to get the "nextval()" function working.  Also if someone could explain why the "nextval()" function doesn't work, but the "now()" function does that would be great too.  I could also fetch the current sequence value and insert and increment the value into the CSV and then update the sequence with the "setval()" function, but I figured I would try to learn more about the Psycopg2 library.
Answer: Answering my own question....
It seems that both PostgreSQL's COPY command and Psycopg2's "copy_from" function do not support nextval('seq_key'), but they do support now() and other functions that do not need to make continuous reads from the database.  It is also important to note that if now() is used, all the entries will have the same value for that column even if it takes multiple seconds to copy all the rows into the database (which makes sense since you are trying to insert them all at the same time).
I was able to get around this problem by not specifying a value for the sequence number and allowing it to use the default value of nextval('seq_key').  I am fairly new to PostgreSQL, so there may be useful database functions that would also not be allowed when using the COPY command.  For nextval('seq_key'), this isn't a big deal since you would usually want to use this anyway, but there may be more PostgreSQL functions that don't work with COPY that might not be as straight forward as nextval('seq_key').
Anyway, hope this helps someone out there!  Kind of an obvious question/solution, but if anyone has any questions, just let me know!
Comment: I have a insert in one table in batches. Each batch has its own connection and transaction, but runs in the same program.
Subsequent connection executions are giving me errors because the first one works(think 50000 records), but the second one tries to insert in the 50000 id (which is occupied already).
However if I open a psql session, the sequence only adds one per batch, so after 9 batches, I get a 9 value on the sequence.
So, copy_from internally works with it well, but checking from outside this value does not correlate - but still changes. Any idea?


List the elements and cosets

Question: In the group $\mathbb{Z}_{24}$, let $H=\langle 4 \rangle$ and $N=\langle6\rangle$
a. list the elements in $HN$ (usually write $H+N$ for these additive groups) and $H\cap N$
So I think
$H=\langle4 \rangle= \{0,4,8,12,16,20 \}$ and $N=\langle6 \rangle= \{0,6,12,18 \}$
$H+N= \{0,2,4,6,10,12,14,16,18,20,22 \}.$
I added every element of H to every element of N and obtained these values.
$H\cap N=\{0,12\}$
Since these are the only two commons element in $H$ and $N$.
b. List the cosets in $HN/N$, showing the elements in each coset.
$HN/N=(H+N)/N$
So I am still really confuse about how to find cosets but here's my attempt:
So I need to look at H+N:
$H+N=\{0,2,4,6,10,12,14,16,18,20,22\}$
and $N= \langle 6 \rangle$, so I need to separate it by multiples of $6$:
$N+0=N+6=N+12=N+18=\{6,12,18\}$
and then $N+2=N+8=N+14=N+20= \{6,14,20,26 \equiv 2\}=\{2,6,14,20\}$
and finally $N+4=N+10=N+16=N+22=\{10,16,22,28 \equiv 4 \}= \{4,10,16,22 \}$
and together these 3 cosets make up $H+N$.
c. List the cosets in $H/(H\cap N)$, showing the elements in each coset.
So I think this is similar to above:
$H=\langle 4\rangle =\{0,4,8,12,16,20\}$
$H\cap N=\{0,12\}$
so $H\cap N+0=H\cap N+12=\{0,12\}$
$H\cap N+4=H\cap N+16=\{16,28 \equiv 4\}=\{4,16\}$
$H\cap N+8=H\cap N+20=\{20,32\equiv 8\}=\{8,20\}$
Does this look correct? In particular, am I thinking of cosets correctly?
Answer: It looks pretty good, by and large. There are a few errors that seem to be traceable back to a simple mistake I assume is a typo, but then it made its way into some more of your work.

You're missing $8$ in $H + N$. Essentially, $H + N$ is the set of even residues modulo $24$; it's isomorphic to $\Bbb Z_{12}$.

Looking at $HN/N$:

You're missing $0$ in $N$, when you calculate the first coset of $N = 0 + N$.
Somehow you have $6$ in $N + 2$, when you want $2 + 6 = 8$ (everything in $N + 2$ should be congruent to each other modulo $6$; they should differ by a multiple of $6$).

Everything looks good in $H/(H \cap N)$. In particular, you seem to be thinking of cosets correctly; they're all just subsets of $\Bbb Z_{24}$ here, more specifically 'translates' of certain subgroups (perhaps with your focus restricted to certain subgroups of $\Bbb Z_{24}$, but the idea is the same).

List the elements and cosets


Set downloaded filename in a different directory using wget

Question: I need to download a zip file using wget. To download it in current directory I run in command line:
 $ wget https://github.com/.../[myfile].zip

To download it in a different different directory I add -P <Path of download directory>:
$ wget -P [download directory path] https://github.com/.../[myFile].zip

I want to change to download the file into [download path directory] but with filename [myFileName]. How can I do this?
I've already tried this:
$ wget -P [download directory path] --output-document=[filename.zip]
https://github.com/.../[myZipFile].zip

This downloads the file into current directory with filename chose by me.
Finnaly I will use this into a NodeJS project using spawn.
Currently I have this:
var downloader = spawn("wget", ["-P", zipFile, appUrl]);

Comment: Why don't you download it from node.js ?
Comment: I mean http://nodejs.org/api/http.html or https://github.com/mikeal/request
Comment: `wget` is an almost-always installed tool, probably for this reason it's in whatever indications you're referring to. Using wget from node.js to download a file; is pretty weird.
Comment: What you mean? The zip file is on Github.
Comment: Where did you get these indications from?
Answer: Maybe I'm missing the point of the Q, but what about the -O flag?
wget -O [download directory path]/[filename.zip] https://github.com/.../[myZipFile].zip

The man page has scary stuff about files concatenated, but that won't matter when you are only fetching one file.
To auto-create the directory first:
mkdir -p [download directory path]; wget -O [download directory path]/[filename.zip] https://github.com/.../[myZipFile].zip

the -p (parents) flag on mkdir causes it to exit without an error if the directory already exists.
Comment: mmmkay.  Not really wget's job, we'll use mkdir for that.  But echo Floby's sentiment - always better to use native methods than spawn a sub-process.  Although to make the opposing argument, there may be some smarts within wget that you want to leverage which aren't part of your native method (automagically following redirects comes to mind).  Equally likely these "extra smarts" would bite you though - perhaps you don't want auto-redirects.
Comment: It works **only** if `[download directory path]` exists. What it has be added to autocreate that directory? If you will add this, I will accept your answer. ;-)


Does a simpler representation of this if condition exist?

Question: I used the below conditional statement to ensure that the city name given is of valid length. Length of city should be greater than or equal to 3 and less than or equal to 20.
if(city.length()<3 || city.length()>20) return false;
else return true;

I am told that this conditional statement can be further simplified. Is that right? Then what is the simplified code?
Comment: Drop the whole `if/else` business and just return the thing, also drop some language tags
Comment: The question is actually incomplete.   What type does `city.length()` return?   And, on two consecutive calls like this, is it guaranteed to return the same value?
Comment: return !(city.length()<3 || city.length()>20)
Comment: [Please do not tag multiple languages in your question](https://meta.stackoverflow.com/questions/354931/when-is-it-appropriate-to-tag-multiple-languages-in-my-question/) unless your question is specifically about these languages.  You may think it will lead to a faster answer, but the reality is that it is more likely to lead to a faster closure.
Comment: If the diff. of the two interval endpoints were even, there would be.
Answer: return !(city.length() < 3 || city.length() > 20)

or
return city.length() >= 3 && city.length() <= 20

Answer: I simplified your code from IntelliJ IDEA IDE.
Actually it itself suggested to simplify when I used your code there. I recommend you to try IntelliJ IDEA.
return city.length() >= 3 && city.length() <= 20;

If you are already using the IDE, just move the cursor to the code with warning and press ALT+Enter and simplify it.
Comment: and I would honestly recommend not to rely on an IDE for such simple tasks, it should be obvious that the statements can be simplified and are equivalent.
Comment: @luk2302 we can have different opinions. Refactors using IDE can be helpful to beginners.
Comment: sorry, i haven't used eclipse. please search for that in google.
Comment: on eclipse, does any plugin exists to suggest this
Answer: If city.length() is cheap, then write
return city.length() >= 3 && city.length() <= 20

else you ought to pre-compute city.length() to avoid the potential for two evaluations:
const auto&& /*assuming C++, for Java, you need to use the specific type*/ l = city.length();
return l >= 3 && l <= 20;

Comment: Suggestion: use auto&& for l. This way, if city.length() returns a reference to a complex type (e.g. has a std::string measure;), you won't copy it but have a ref.


How could I see what kind of circle is $x^2 + y^2 - 3y = 0$

Question: In finding Cartesian coordinates to $r = 3\sin t$, I found
$$\frac{y^2}{x^2 + y^2} = \frac{x^2 + y^2}{9}.$$
After graphing it with a computer, I see it's a circle.  I then managed to rewrite the equation to $x^2 + y^2 - 3y = 0$, but with my level of sophistication, I wasn't even sure this was a circle.  But it is!  It's a circle of diameter 3 centered what seems to be (0,\sqrt{3}).
But how come?  Isn't a circle $(x - a)^2 + (y - b)^2 = r^2$?  Where's my $r^2$ in $x^2 + y^2 - 3y = 0$?  Please educate me.  Thank you!

UPDATE $1$: after some excellent answers, I see the circle is centered around 1.5 and not $\sqrt3$.  What I was missing is completing the square (in the $y$ term) so as to write the circle in its standard form.  I don't know which excellent answer should be chosen as the answer.
Answer: The terms in $y$ can be written as $$y^2-3y=y^2-3y+(3/2)^2-(3/2)^2=(y-3/2)^2-(3/2)^2$$
This is called 'completing the square', as in realizing that $a^2+2ab+b^2=(a+b)^2$ would work if you interpret $y^2$ to be the $a^2$, and the $-3y$ to be the $2ab$.
Then, completing the square consists in adding (and subtracting) what should be the term $b^2$.
Answer: Completing the square we have
$$x^2 + y^2 - 3y = x^2+\left(y-\frac32\right)^2-\frac 94=0\implies x^2+\left(y-\frac32\right)^2=\frac 94$$
then

center is in $(0,3/2)$
radius $R=\frac32$
Answer: Note that $\left\{(x,y)\ |\ \frac{y^2}{x^2 + y^2} = \frac{x^2 + y^2}{9}\right\}$ is in fact the union of two circles. Indeed, the equation $$(x^2+y^2)^2=9y^2$$ is equivalent to
$$x^2+y^2={\color{red}\pm} 3y$$
Which gives you $$x^2+\left(y\ {\color{red} -}\ \frac32\right)^2=\frac 94\qquad \text{and}\qquad x^2+\left(y\ {\color{red} +}\ \frac32\right)^2=\frac 94$$
These circles are symmetric with respect to the $x$-axis, where they are also tangent, thus forming a nice figure eight shape.
However, as noted by J.G., this intermediate equation is not equivalent to the original $r=3\sin \theta$, since $$r=3\sin \theta\Longrightarrow y>0$$ as $y=r\sin \theta=3\sin^2 \theta$.
Comment: Although the original $r=3\sin\theta$ implies $x^2+y^2=r^2=3r\sin\theta=3y$.
Comment: @J.G. True. Thanks, I will edit.
Comment: I would say $y > 0$ even without the polar equation.  Taking the square root of the cartesian equation we get $|y| = (x^2 + y^2)/3$, so $y$ must be positive since the right hand side is positive.  So we can drop the absolute value.  Doesn't this proceed?
Comment: @JoepAwinita Definitely no. The RHS being positive tells you that $|y|$ is positive, but doesn't tell you anything about the sign of $y$. That's the meaning of the first part of my post.
Comment: @ArnaudMortier You're perfectly right.  I conclude the same now.  There is no way to tell $y > 0$ unless we look at $r = 3 \sin t$.  Thanks for such great contribution.
Answer: $x^2+y^2-3y=0 \implies x^2+(y-\frac{3}{2})^2=(\frac{3}{2})^2$.
So, the equation represents a circle with centre $(0,\frac{3}{2})$ and radius $\frac{3}{2}$.


Expectation value of random variable conditionally defined

Question: For some $Y, Z$ random variables and $p \in [0, 1]$, consider the random variable $X$ defined as follows
$$ X = \begin{cases} Y & \text{with probability } p \\
Z & \text{with probability } 1-p\end{cases} $$
My question is how do I go on to find $E[X]$? I have a feeling that it should
look like:
$$ E[X] = pE[Y] + (1-p)E[Z] $$
But I am unable to prove it. I would be grateful for any hints on the proof.
Ultimatly I want to find the expected value of a random variable $X$ defined as such for random variables $A, B, C$ and boolean predicate $r$
$$ X = \begin{cases} A & \text{if } r(C) \\
B & \text{if } \neg r(C) \end{cases}$$
which I guess would be something like this
$$ E[X] = \Pr[r(C)] \cdot E[A] + \Pr[\neg r(C)] \cdot E[B]$$
Here's my proof of the second expected value expression assuming
the first expected value expression is correct. Let $\mathcal{F}=\sigma(C)$
then
\begin{align*} E[X] &=
E[E[X\mid \mathcal{F}]]\\&= E[A \cdot \Pr[r(C)] + B \cdot \Pr[\neg r(C)]] \\
&= \Pr[r(C)] \cdot E[A] + \Pr[\neg r(C)] \cdot E[B] \end{align*}
Comment: The hypothesis as written here does not imply the independence so crucial in @BGM's construction, hence the identity E(X)=pE(Y)+(1-p)E(Z) is far from being guaranteed.
Comment: I am not sure if this is the proof you want. Just express $X = WY + (1 - W)Z$ where $W$ is a Bernoulli($p$) random variable, independent to $Y, Z$.
Comment: @BGM Yes, thank you! that's exactly what I was looking for. If you could write up an answer I'll select it as the best answer.
Answer: Express
$$ X = WY + (1 - W)Z $$
where $W \sim \text{Bernoulli}(p)$ and independent of $Y, Z$. Then taking expectation, and by independence, you immediately obtain the desired result.
Answer: It is generally true that $\operatorname{E}(\operatorname{E}(X\mid W)) = \operatorname{E}(X)$ if $X$ and $Y$ are random variables and $\operatorname{E}(|X|)<\infty$.  This is sometimes called the "law of total expectation".  See this article about it.
One can define $\operatorname{E}(X\mid W)$ as follows.  The conditional expected value of $X$ given an event $W=w$ is just ordinary conditioning on an event.  It gives you some function of $w$, thus $\operatorname{E}(X\mid W=w) = h(w)$.  Then $\operatorname{E}(X\mid W)$ is $g(W)$.  Hence $\operatorname{E}(X\mid W) = \operatorname{E}(g(W))$.


How to check the value of a field within the current node?

Question: In order to display a block only above certain nodes of Article content type,
I created a boolean field with two values : On / Off
The goal is to display the block only above the articles that have the Off value
After enabling the PHP filter module, the block gives me this option : "Pages on which this PHP code returns TRUE (experts only)" but i haven't been able to figure out the php code that returns TRUE
( Can the context module achieve this without PHP ? )
Any help is help
Comment: Possibly related https://drupal.stackexchange.com/questions/2509/what-are-the-downsides-of-using-php-filter-code-in-blocks-nodes-views-args-et
Answer: I think you should be able to do this with the (https://www.drupal.org/project/rules_block_visibility) module.


Approximate uniform distribution on high dimensional sphere

Question: Take $X$ to be a multivariate random variable with $n$ iid Gaussian components with mean $0$ and constant variance. Then $X/\|X\|_2$ is uniformly distributed on the $n-1$ dimensional sphere $S^{n-1}$.
Now let the components of $X$ have variance $1/n$. The mean and the variance of $\|X\|_2^2$ are then $1$ and $2/n$ respectively, because $\|X\|_2^2$ is $\chi_n^2$ distributed.
If $n$ is large one could approximate $\operatorname{var}(\|X\|_2^2) \approx 0$, so $\|X\|_2^2 \approx 1$ and finally take $X$ as an approximation of the uniform distribution on $S^{n-1}$.
Which literature can I cite, where the approximation of a uniform distribution on a large dimensional sphere by a multivariate Gaussian distribution is shown rigorously? What errors could I expect, when I do this approximation?
Answer: This question has been answered at https://mathoverflow.net/questions/315221/distribution-of-the-individual-coordinates-of-a-uniform-random-vector-on-a-high.
It was shown that the convergence of the cdf of the components $X_j$ of $X$ to the cdf $\Phi$ of a Gaussian with variance $1/n$ in the supremum norm on $\mathbb{R}$ is at least as fast as $1/n$. In formula
$$
\sup_{x\in\mathbb{R}} |P(X_j \le x) - \Phi(x)| \le \frac{0.24}{n} + \frac{0.16}{n-1} \sim \frac{1}{n}.
$$


What is the difference between hook_menu(), hook_menu_alter(), and hook_menu_link_alter()?

Question: So, I am fairly new to Drupal (about 8 months in) and I am using Profile2 with 3 different profile types. Everything is working excellent, except for I wish I could wrap my head around how I can override the Profile2 view button when you click on, let's say Main Profile. So, if you are familiar with profile2, you know that it comes with a default profile as soon as it's activated, and when you click on it within the User Menu, you are presented with a screen with the local menu tabs called View, and Edit.
All I want to do is keep the View button, but change the path to either a panel page or an actual Views page.
I've explored hook_menu(), hook_menu_alter(), hook_menu_link_alter(), and to tell you the truth, I just don't get how those work. I believe hook_menu actually adds new "nodes" or paths to the system with their own page?
I think I have a lot to learn when it comes to these three functions (hook_menu(), hook_menu_alter(), and hook_menu_link_alter()) I just need a savior to explain these to me.
As a sub question, along with the View, and Edit button, how would I go about adding an extra local menu button to this row? Like upgrade my account, or view your bill?
Any light would be appreciated, and I do have the Devel module installed, as well as the theme developer tool. I tried making a custom module, and running a DPM on those functions above, but it's not even outputting anything.
Answer: hook_menu() -> Define menu items and page callbacks.
hook_menu_alter() -> Alter the data being saved to the {menu_router} table after hook_menu is invoked.
hook_menu_link_alter() -> Alter the data being saved to the {menu_links} table
Answer: To get result from those functions you will have to clear your caches. Use the admin_menu module which has a direct link for clearing caches.
There are a lot of examples available on how to use hook_menu. This is a comprehensive step step example: How should I implement hook_menu()? or another example on how to create tabs: create tabs with hook_menu() for custom module
Comment: Which part of the question is this answer answering.
Comment: I think this is clear. I explain why he does not see results when he uses dpm() I also give him proper references for his subquestion (how would I go about adding an extra local menu button to this row?) The other reference is an answer to his request for 'any light'.
Answer: I was investigating this more and finally came to a solution on how to add a local task menu to a profile2 page. It's kind of dirty, but it will do for now.
I basically added this to my site's glue module to modify the local task items.
function ch_glue_menu_local_tasks_alter(&$data, $router_item, $root_path) {
global $user;

foreach($user->roles as $k=>$v){
    if(preg_match('/paid1|paid2|paid3/i', $v)){
        $is_paid_member = true;
    }
}

if(preg_match('/profile/i', $root_path) && !$is_paid_member){
        // Add a tab linking to node/add to all pages.
        $data['tabs'][0]['output'][] = array(
            '#theme' => 'menu_local_task',
            '#link' => array(
                'title' => t('Upgrade Account'),
                'href' => 'node/3',
                'localized_options' => array(
                    'attributes' => array(
                        'title' => t('Add new content'),
                    ),
                ),
            ),
        );
}
}

Thanks for explaining the menu functions, but it seems like I have A LOT to learn when it comes to the drupal menu system. I'm just glad this will work for right now.


How do I avoid "Not Secure" messages when developing locally with IIS and personal certificates?

Question: I'm developing with ASP.NET and find it annoying that I constantly get "Not Secure" messages when accessing my local website through IIS just because I have a personal self-signed certificate.
Is there a way to avoid this without going through what appears to be a very complicated process of setting up my own certificate server - I am just running on my local IIS however note the site I'm developing does not use localhost, but a specific website configured on my local IIS.
Answer: basically while creating a certificate you need to raise it with your localhost domain(i.e. Hostname that you pass in the Binding of the application). If yes then you wont be getting the Error any more. Or else for your local setup you can change the binding to Http.


How to use multidimensional dict in python

Question: I'm trying to use a multidimensional dict in python. In my case, I need to use a structure like this:
MyDict[domain][IPAddress][UserAgent1, UserAgent2, UserAgent3, ....]

In this structure each key is a String and the last values are in a vector. I'm using this code for the first insertion:
from collections import defaultdict
MyDict= defaultdict(lambda: defaultdict(lambda: defaultdict(3)))

if domain not in MyDict:
    MyDict[domain] = []
if IPAddress not in MyDict[domain]:
    MyDict[domain].append(IPAddress)
if UserAgent1 not in MyDict[domain][IPAddress]:
    MyDict[domain][IPAddress].append(UserAgent1)

But this code doesn't work for this error:
Traceback (most recent call last):
  File "/.../Main.py", line 33, in <module>
    if UserAgent1 not in MyDict[domain][IPAddress]:
TypeError: list indices must be integers, not str

Additionally, I need to iterate on this structure. Do you know how to implement this structure and the best way to iterate on them?
Comment: fyi, you can shorten your code by using the `setdefault` method all dicts have.
Answer: The problem is that you have assigned a list to MyDict[domain] rather than a dictionary, try this instead:
if domain not in MyDict:
    MyDict[domain] = {}
if IPAddress not in MyDict[domain]:
    MyDict[domain][IPAddress] = []
if UserAgent1 not in MyDict[domain][IPAddress]:
    MyDict[domain][IPAddress].append(UserAgent1)

I'm not clear exactly what you want to see in the iteration, but this should get you started:
for domain in MyDict:
    for ipAddress in MyDict[domain]:
        for userAgent in MyDict[domain][ipAddress]:
            # code here will run for each combination of domain, ipAddress and userAgent

or the more 'Pythonic' version:
[[domain, ipAddress, userAgent] for domain in MyDict for ipAddress in MyDict[domain] for userAgent in MyDict[domain][ipAddress]]

Comment: Nice answer so far. The second part of OP's question asks how to iterate over `MyDict`.
Comment: Excellent answer. Can you please tell me also how to iterate on this? Thanks
Comment: @hasmet what values do you want to see in the iteration?
Comment: I need to iterate on all values.....I mean, I need a similar solution like 3 nested for:

for domains in MyDict:
   for IPAddresses in MyDict[domain]:
      for UserAgents in MyDict[domain][IPAddress]:
         ...
Comment: @hasmet but do you just want to see the UserAgents or the domains/ips as well and if so what data structure should they be in?
Comment: @codebox: if you don't understant the code (not well formatted in comment box) I can edit the question
Comment: Sorry for the delay codebox. Your solution is exactly what I need. Thank you very much!!


Asymptotics of partial exponential sum $ \sum\limits_{k=0}^{a n} \frac{n^k}{k!}$

Question: The question Evaluating $\lim\limits_{n\to\infty} e^{-n} \sum\limits_{k=0}^{n} \frac{n^k}{k!}$ has been asked many times here, and there are different approaches for answering it.
Motivated by this other question, I'm interested in how to attack the modified problem where the number of terms grows as a fraction of $n$. I.e. the behaviour  of
$$S(n;a) = \sum\limits_{k=0}^{an} \frac{n^k}{k!}$$
for large $n$, with fixed $a \in (0,1)$ (the upper limit of the summation is understood to be taken as the nearest integer).
Or more specifically,
$$ \lim_{n\to \infty} \left(\sum\limits_{k=0}^{an} \frac{n^k}{k!} \right)^{1/n}$$
Comment: Using the convergence of the Poisson distribution to the normal distribution for large parameters,

$$ e^{-n}\sum_{k\leq an}\frac{n^k}{k!}=\mathbb{P}[\text{Pois}(n)\leq an]\to\mathbb{P}[\mathcal{N}(n,n)\leq an]=\int_{-\infty}^{an}\frac{1}{\sqrt{2\pi n}}e^{-\frac{(x-n)^2}{2n}}\,dx = \frac{1}{2}\left[1+\text{Erf}\left((a-1)\sqrt{n/2}\right)\right]$$
so I would expect
$$ \sqrt[n]{\sum_{k\leq an}\frac{n^k}{k!}}\to \exp\left(1-\frac{(1-a)^2}{2}\right)$$
for any $a\in(0,1]$.
Comment: Notice that $\displaystyle \sum\limits_{k=0}^{an} \frac{n^k}{k!} = \frac{e^n \Gamma (a n+1,n)}{\Gamma (a n+1)}$. I am not quite sure if this will help.
Comment: @JackD'Aurizio Yes, the problem is that (if I'm not mistaken) the probability (that in the original problem corresponded to half the gaussian curve) now corresponds to a vanishing tail, with probability tending to zero - and it becomes more problematic to use the CLT (as $n$ grows, the $z$ value of the normalized CDF decreases...)
Comment: Are you saying that the last limit does not depend on $a$ ? I'm not sure about that, because $f(a)$ tends to zero (as $n$ grows) for fixed $a<1$
Comment: @JackD'Aurizio I've worked out an answer (not rigorous, but it looks right numerically), unless I'm mistaken the limit definitely depends on $a$.
Answer: Ok, here's my (not very rigorous) try. Other approaches or refinements are welcomed.
Let's change notation $m = a n$, $x = n  = mb$ with $b = 1/a$.
Then $$e^x =  \sum_{k=0}^m \frac{x^k}{k!} + R_m(x) \tag1$$
with the remainder:
$$
\begin{align}
R_m(x) &= \int_0^x \frac{(x-t)^m}{m!} e^t dt\\
&=\frac{e^x}{m!}\int_0^x y^m e^{-y} dt\\
&=\frac{e^x}{m!} \left(m!- \int_x^\infty y^m e^{-y} dt\right) \tag2\\
\end{align}
$$
Because $b>1$, we can approximate the complement of the gamma integral by abusing Laplace's method. Namely, for a differentiable positive decreasing function (more in general, a function that has its global maximum at the start of the interval of integration) and for $m\to \infty$ we approximate
$$ \int_c^\infty e^{m h(x)}dx\approx \int_c^\infty e^{m [h(c) + h'(c)(x-c)]}dx=\frac{e^{m \, h(c)}}{m \,|h'(c)|} \tag{3}$$
Then we can write
$$
\int_x^\infty y^m e^{-y} dt =\int_x^\infty e^{m (\log(y)-y/m)}  \approx x^m e^{-x} \frac{b}{b-1} \tag 4\\
$$
Actually we are abusing the method here because our $h()$ depends also on $m$ - this would need some justification. Passing over this and putting all together:
$$\begin{align}
\sum_{k=0}^m \frac{x^k}{k!}  &= e^x - R_m(x) \\
& \approx \frac{x^m}{m!} \frac{b}{b-1} \\ \tag{5}
&= \frac{n^{an}}{(an)!} \frac{1}{1-a} \\
& \approx \left(\frac{e}{a}\right)^{an} \frac{1} {(1-a) \, \sqrt{ 2 \pi a} \, \sqrt{n}}   \tag{6}
\end{align}
$$
Finally
$$\lim_{n\to \infty} \left(\sum\limits_{k=0}^{an} \frac{n^k}{k!} \right)^{1/n}= \left(\frac{e}{a}\right)^a \tag 7$$

Added: As rightly comments @Maxim, if we are interested in correcting the rounding (when $m$ in $(5)$ is not an integer we round down to the nearest integer), we should multiply $(6)$ by the correction factor $a^{\{an\}}$,
where $\{\}$ denotes the fraction part. Of course, this correction is asymptotically negligible ($O(1)$) and does not change the limit $(7)$.
Comment: If $m = n/b \in \mathbb N$, then
$$\sum_{k = 0}^m \frac {(b m)^k} {k!} =
\frac {e^{b m} \Gamma(m + 1, b m)} {\Gamma(m + 1)} =
\frac {(b m)^{m + 1} e^{b m}} {\Gamma(m + 1)}
 \int_1^\infty e^{m (-b t + \ln t)} dt \sim
\frac {b (e b)^m} {(b - 1) \sqrt {2 \pi m}}.$$
The last term in the sum is not negligible, so it appears that without the assumption $a n \in \mathbb N$, we'll get
$$\sum_{k = 0}^{\lfloor a n \rfloor} \frac {n^k} {k!} \sim
\left( \frac e a \right)^{a n}
 \frac {a^{\{a n\}}} {(1 - a) \sqrt {2 \pi a n}}.$$


Caching the Drupal bootstrap

Question: I'm curious if anyone has made attempts to "cache" the bootstrap process in Drupal.
Normally, Drupal will run the 7 bootstrap phases on each request, but perhaps on a deployed production system, one could "do away" with some or all of these?
Possible suggestions I have in mind could be

Serializing the bootstrapped state and sticking it into memcache
A script could generate a patch for bootstrap.inc that will hardcode certain information into the file.
I believe David Strauss has tried to keep a bootstrapped Drupal running on libevent.
Other craziness?

What attempts are there, and which are known to be (somewhat) reliable?
Comment: This is somehow related to my question about compiling Drupal: http://drupal.stackexchange.com/q/11738/2916
Answer: PHP is a shared nothing architecture. That has its advantages and disadvantages.
One disadvantage is that it's not easy to do something like this. There isn't much of a state that can be stored somewhere.
I did some quick tests and when logged in, then the boostrap seems to take about ~17% of the total time and more than 50% of that is actually loading all the .module and .inc files. That's not something that you can store in memcache. Also, it doesn't seem to matter much if I use memcache or the database cache.
I tried to get some results when having the page cache enabled, but Xhprof doesn't seem to return reliable results then; the whole thing simply seems to be too fast. But even then, the biggest part involves executing init/exit hooks and loading files it seems. I found an interesting issue there: It looks like the User module is seriously slowing down the cached page response because it triggers the registry due to the entity controller in the .module file.
That said, David Strauss showed some experimental work in Copenhagen where he created a memory snapshot after bootstrapping and then returning to that once the page was served. He did use Drupal 6 for that. After looking at the numbers above, I imagine that the performance gains of doing this in Drupal 7 would be quite a bit smaller. One reason for this is that the database connection is lazy loaded (And you can get quite far in the bootstrap when using e.g. Memcache before you need to execute the first query) and there's a lot that is cached.
What's really bad in Drupal 7 is the render layer with these huge arrays and endless recursions and loops. That one pretty much undoes all the performance work that went into Drupal 7. Let's see how it looks in Drupal 8, if Twig makes it into core.
Lastly, about the mentioned advantages. One large advantage is that memory leeks are rather irrelevant because everything is freed after each request. I've seen many Java applications where memory usage constantly increases and need regular restarts.
Comment: I _so_ love having you on the site @Berdir ;)
Comment: @Bart: You just re-invented Boost: http://drupal.org/project/boost :)
Comment: Alex Bronstein mentioned it during the sprint that it's somewhat slow to include the tpl.php files even with APC it requires a stat -- but Twig compiles to classes so that will be a win on pages like node + many comments. We will see.
Comment: I guess you could make a system where, for cached pages, you generate a bunch of rewrite rules and put them in .htaccess, accompanied by html pages to bypass PHP completely. Might not be worth the trouble though: IIS, nginx, logged-in users, ...
Answer: Nope, it was David Strauss who was experimenting with kargo-event (now called Kellner) at https://code.launchpad.net/~fourkitchens/pressflow/6-evented but I doubt anything serious came out of it.
Drupal 7 does have a lot of bootstrap already cached, there's a cache_bootstrap bin for that. You can stick it into memcached.
You can go overboard and decrease code loading with moving some / lots of Drupal code into C. Damien and dhthwy created PHP extension at http://drupal.org/project/drupal_php_ext not much is done with it. Or do hiphop. I do not know the current state of hiphop & Drupal 7.
At the end of the day, however, you need to take a good hard look at the engineering costs of, say, getting hiphop work with Drupal 7 (and all the contrib!) and compare it with renting a few more servers. If you can save, say 5% of your servers and you have 100 000 servers, go for it, but are you Facebook? Be careful and economical with optimizations.
Comment: Thanks, I've updated the question and removed your name from it. :) I realize that for many cases, there are far more efficient ways to deal with performance, I was mostly curious.
Answer: Saw an interesting presentation about doh (Dynamic Object Handler) at the High Performance Drupal meetup. In short he talks about bootstrapping drupal fast by using it. Gets interesting around the 15:30 mark. Autoload on steroids in short with runkit functionally as well. QA at the 33:00 mark.

Caching the Drupal bootstrap


Confirmation message not getting rendered when printing the elements seprately in twig and ajax is enabled

Question: Iam using the following twig code in webform-submission-form--newsletter-signup.html.twig

                            {{ form.elements.url }}

                                {{ form.elements.email }}

                         {{ form.elements.actions }}

                    {{ form.form_build_id }}
      {{ form.form_token }}
      {{ form.form_id }}

and Iam using the following code in webform--newsletter-signup.html.twig
 <form{{ attributes.addClass('subscribe-form') }}>

  {{ title_prefix }}
  {{ children }}
  {{ title_suffix }}

</form>

When I try to use the default webform templates the message gets displayed after ajax request infact the message is returned as part of the data in response but when I use these templates the message is not present in ajax response what am I doing wrong here please help
Answer: You need to still include {{ title_prefix }} and  {{ title_suffix }} in your template
In the comments in the default webform.html.twig it gives a short description of the default variables "Additional output populated by modules" which includes essentially anything else apart from the form elements themselves
Comment: These two variables already form the part of my webform--newsletter-signup.html.twig file but message is not rendered


Generating a list of years/months in MySQL

Question: Currently for my blog I get a list of years/dates using the following code:
<code>    $monthList = array(
        1 => 'January',
        2 => 'February',
        3 => 'March',
        4 => 'April',
        5 => 'May',
        6 => 'June',
        7 => 'July',
        8 => 'August',
        9 => 'September',
        10 => 'November',
        11 => 'October',
        12 => 'December'
    );
    $nav = array();
    for ($year = 2009; $year <= date('Y'); $year++) {
        foreach ($monthList as $id => $month) {
            $this->tru->query->runRaw(array(
                'name' => 'get-headline-count',
                'sql' => 'SELECT
                        COUNT(id) as count
                    FROM
                        wp_'.$this->getId().'_posts
                    WHERE
                        post_status = \'publish\' AND
                        post_type = \'post\' AND
                        post_parent = \'0\' AND
                        YEAR(post_date) = '.$year.' AND
                        MONTH(post_date) = '.$id.'',
                'connection' => 'article'
            ));
            $temp = $this->tru->query->getArray('get-headline-count');
            if ($temp['count'] > 0) {
                $nav[$year][$id] = $temp['count'];
            }
        }
    }

    krsort($nav);

    return array(
        'month' => $monthList,
        'nav' => $nav
    );
</code>
This was all great at first, but now that I've got a few years worth of data and a few months this isn't going to cut it anymore.  I've never done anything like this before, I usually deal with timestamps.
Answer: You can replace the loop with this one query
<code>SELECT
   YEAR(post_date) as yr, MONTH(post_date) as mn, COUNT(id) as count
FROM
    wp_posts
WHERE
    post_status = 'publish' AND
    post_type = 'post' AND
    post_parent = '0'
GROUP BY YEAR(post_date), MONTH(post_date)
</code>


spark submit executor memory/failed batch

Question: I have 2 questions on spark streaming :

I have a spark streaming application running and collection data in 20 seconds batch intervals, out of 4000 batches there are  18  batches which failed because of exception :

Could not compute split, block input-0-1464774108087 not found

I assumed the data size is bigger than spark available memory at that point, also the app StorageLevel is MEMORY_ONLY.
Please advice how to fix this.

Also in the command I use below, I use executor memory 20G(total RAM on the data nodes is 140G), does that mean all that memory is reserved in full for this app, and what happens if I have multiple spark streaming applications ?

would I not run out of memory after a few applications ? do I need that much memory at all ?

/usr/iop/22.214.171.124/spark/bin/spark-submit  --master yarn  --deploy-mode
  client   --jars  /home/blah.jar   --num-executors 8  --executor-cores
  5   --executor-memory 20G --driver-memory 12G --driver-cores 8
  --class com.ccc.nifi.MyProcessor    Nifi-Spark-Streaming-20160524.jar
Answer: It seems might be your executor memory will be getting full,try these few optimization techniques like :

Instead of using StorageLevel is MEMORY_AND_DISK.
Use Kyro serialization which is fast and better than normal java serialization.f yougo for caching with memory and serialization.
Check if there are gc,you can find in the tasks being executed.


Erro import import br.gov.frameworkdemoiselle.security.User

Question: Erro ao importar a interface User:
import br.gov.frameworkdemoiselle.security.User;

Versão do demoiselle utilizada: 2.5.0-RC1
De acordo com o GrepCode, na versão 2.5.0-BETA2, esta interface existia (http://grepcode.com/file/repo1.maven.org/maven2/br.gov.frameworkdemoiselle/demoiselle-core/2.5.0-BETA2/br/gov/frameworkdemoiselle/security/User.java?av=f), porém a partir da 2.5.0-BETA3 em diante, ela desapareceu (http://grepcode.com/project/repo1.maven.org/maven2/br.gov.frameworkdemoiselle/demoiselle-core/).
Alguém sabe qual a interface que substitui a 'User' ?
Estou implementando a interface Authenticator:
import br.gov.frameworkdemoiselle.security.Authenticator;
import br.gov.frameworkdemoiselle.security.User;

public class Autenticador implements Authenticator {
    ...
    @Override
    public User getUser() {
        return new User() {
            private static final long serialVersionUID = 1L;

            @Override
            public void setAttribute(Object arg0, Object arg1) {

            }

            @Override
            public String getId() {
                return "usuario X";
            }

            @Override
            public Object getAttribute(Object arg0) {
                return null;
            }
        };
    }

}

Answer: Essa interface foi descontinuada e ao invés dela recomendamos o uso do java.security.Principal que torna a solução mais compatível com outros padrões do Java como o JAAS por exemplo.
Lembrando que o Demoiselle 2.5 ainda não está na versão final, enquanto isso estamos fazendo testes na aplicação de exemplo:  https://github.com/demoiselle/laboratory/tree/master/estacionamento/v25X
ex:
....
@Override
public  Principal getUser() {

    if (authenticated) {

        return new Principal() {

            @Override
            public String getName() {
                return credentials.getUsername();
            }

        };
    }else{
        return null;
    }

}

@Override
public void unauthenticate() throws Exception {
    credentials.clear();
    authenticated = false;
}

Comment: A versão 2.5.0 foi lançada.


Right way to iterate a sequence of XML nodes in PHP

Question: Wich is the right way to iterate these pieces of XML?
Hi there.
I have a webservice that brings me some clients to integrate.
If the werbservice returns only one client, the result is like this:
 <ClientsToIntegrateResult>
    <Client>
       <Id>1</Id>
       <Name>John</Name>
    </Client>
 </ClientsToIntegrateResult>

If the webService returns more than one client, the result is like this :
 <ClientsToIntegrateResult>
    <Client>
       <Id>1</Id>
       <Name>John</Name>
    </Client>
    <Client>
       <Id>2</Id>
       <Name>Peter</Name>
    </Client>
 </ClientsToIntegrateResult>

And I have this piece of PHP code to read the results from webService, where $myWSResponse is the instance of my webService :
 $clients = $myWSResponse->ClientsToIntegrateResult;

 foreach ($clients->Client as $client )
 {
    print_r($client); //Print the client node and its properties if more than one client, and prints each property of client node if only one client.
    print_r($client->Id); //Give me error if only one client, because Id will not exist.
 }

My problem is my code doesn't work when webservice returns only one client.
When I have more than one client my foreach works fine, as I desire, because $clients->Client will be a array.
But when the webService returns only one client $clients->Client will be the node itself and my code will throw errors.
What can I do to solve it?
I'm not accustomed with PHP development, but with .NET development. Any help will be precious.
Comment: To broad — PHP has myriad objects that represent and deal with XML. We'd need to know what sort of object $myWSResponse is (var_dump( get_class($myWSResponse))) can help you there.
Comment: @AlanStorm the result of `var_dump(get_class($myWSResponse))` is `string(8) "stdClass"`
Answer: if(count($clients->Client) > 1){

foreach ($clients->Client as $client )
 {
    print_r($client); //Print the client node and its properties if more than one client, and prints each property of client node if only one client.
    print_r($client->Id); //Give me error if only one client, because Id will not exist.
 }

}else {
echo $clients->Client['id']; //and other child attributes
}


IPV4 somente com PHP com link ipv4/ipv6

Question: Eu sempre usei $_SERVER['REMOTE_ADDR'] para obter o IP do usuário/visitante em alguns sistemas.
Porém comecei a usar um IP FIXO no trabalho, que é IPV4, mas quando vou ver o registro eu acessei com IPV6.
Mas em sites como fóruns e blogs os sistemas em PHP deles registram o IPV4, e analisei os códigos e mesmo assim não consegui entender o algoritmo que consegue passar o IPV6 ou outro método para IPV4.
Então acredito que seja possível somente com PHP, se alguém puder me ajudar.
Edição:
Eu instalei o SMF2 que estou acostumado em uma hospedagem gratuita (fora da minha rede) e testei, ele ao invés de registrar o IPV4 registrou o IPV6 mesmo, isso está me deixando um tanto confundo:

Por que em outros sites pessoais que usam o mesmo sistema mostram o meu IPV4.
Comment: https://tools.ietf.org/html/rfc8305
Answer: Eu consegui descobrir a razão de alguns sites pegarem o IPV4 mesmo usando IPV6 ao mesmo tempo e a minha hospedagem não, as rotas usadas podem variar do servidor onde foi hospedado a script PHP, então não tem realmente como isso ser feito sem uso de serviço de terceiros.
Agora uma solução alternativa, vale lembrar que não vou postar o serviço de terceiros que usei por que acredito que isso seja mal visto pela comunidade, e que também pode ser considerado um sistema mal feito.
Para poder solucionar eu usei este serviço para converter em JSON o IPV4:
http://<ipv4 ipv6 service>/json/widgetdata.php?callback=_jqjsp&_1518034744039=

Ele retorna uma JSON:
_jqjsp({"address":"179.XXX.XXX.XX5","proto":"ipv4","country_code":"BR","country":"Brazil"})

Mas não se pode usar xmlHTTPrequest para receber valores de outra um JSON de outra hospedagem, então através da URL dela eu alterei o termo da variavel callback de maneira que a gambiarra fazendo com que o PHP do serviço criasse uma variavel Javascript usando o sinal de igual (=) assim %3D para URL aceitar:
http://<ipv4 ipv6 service>/json/widgetdata.php?callback=dataipv4%3D

E chamando ele por HTML a variavel fica acessivel ao seu navegador como um objeto JSON:
var dataipv4=({"address":"177.XXX.XXX.XX5","proto":"ipv4","country_code":"BR","country":"Brazil"})

Então agora sim com xmlHTTPRequest bastou usar a variavel dataipv4.address para obter o IP e enviar por método GET pra um PHP que use o IP:
function showIpv(ip) {
     var xmlhttp = new XMLHttpRequest();
     xmlhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
            console.log()
         }
     };
     xmlhttp.open("GET", "write_ipv4.php?ip="+ip, true);
     xmlhttp.send();
}

O arquivo write_ipv4.php que fica na hospedagem:
<?php
if(isset($_GET['ip']) && !empty($_GET['ip'])){
   /* User a variavel global $_GET['ip'] da maneira que preferir
      Por exemplo:
      echo $_GET['ip']
      retorna o IP no formato XXX.XXX.XXX.XXX
   */
}else{
   console.log('Acesso direto proibido');
}
?>

IPV4 somente com PHP com link ipv4/ipv6


Loss function in Supervised Learning vs Statistical Decision Theory

Question: I am confused by the different definitions of Loss Function in statistical decision theory vs machine learning.
In statistical decision theory, a loss function is typically defined as $L(\theta, \delta(X))$, where $\theta$ is the true, unknown parameter, $\delta(.)$ is the decision rule, and $X$ is data (generated from $\theta$?). See for example lectures of the Theory of Statistics class.
In machine learning, it seems the loss function is defined as $L(y, f(X))$, where $y$ is the true label and $f(x)$ is some model. See, for example, Elements of Statistical Learning chapter 2.4.
My question is if they are talking about the same thing. It seems different. For example, if I am going to predict the next coin toss of an unknown coin, I can then model the coin toss as following a Bernoulli distribution with an unknown parameter $\theta$.
Let $X$ be some historical data. Then it appears that the loss function from statistical decision theory is computing my prediction $\delta(X)$ against the unknown parameter $\theta$ whereas in ML, it is computing the same prediction $\delta(X)$ (or $f(X)$) against the true label?
I am having trouble reconciling the two concepts.
Comment: It is helpful to consider the overarching goals of both statistical inference and supervised learning: to choose the estimator (or hypothesis) that minimizes a [*risk function*](https://en.wikipedia.org/wiki/Loss_function#Expected_loss) (also known as the expected loss). Does this answer your question? https://stats.stackexchange.com/questions/540140/what-is-the-difference-between-the-risk-function-used-in-bayesian-inference-and
Comment: A somewhat related question with essentially the same title: https://stats.stackexchange.com/questions/485964
Comment: I checked out that post before I wrote up my question. Although the title seems similar, what is being asked is completely different. That post was talking about the difference in prediction time vs training time. I am talking about the difference in the forms of the loss functions themselves. Thanks for pointing it out.
Answer: I would say this is more a difference in the form of the decision than the loss. The loss function in both cases is Loss(true state of nature, your decision), but it simplifies differently depending on the form of the decision
In point prediction settings (such as a lot of ML), the decision is a potential value of the label, and the state of nature effectively simplifies to the true value of the label, so the loss $L(y, \hat y)$ can be written as the loss from predicting $\hat y$ when the truth is $y$.
In parametric inference settings, the decision is a potential value of the parameter, and the state of nature effectively simplifies to the true parameter value, so the loss $L(\theta, \hat\theta)$ can be written as the loss from estimating $\hat\theta$ when the truth is $\theta$.
There are more complicated settings, too. For example, your decision might be an interval, and the state of nature might be a value, and the loss could be the length of the interval plus the distance from the value to the closest point of the interval (possibly zero)[PDF]. In that setting there isn't the nice correspondence between potential decisions and potential states of nature, and the loss doesn't simplify down to a summary of the error in the decision in the same way. And of course many other possibilities.
Comment: Thanks for the great answer. I still have two questions: 1) what if we can observe not the true state but a stochastic manifestation of it - e.g. a coin toss has an unobservable probability $\theta$ of turning head but we only observe the past history of the coin toss. It can still be formulated as a supervised learning problem (say, there are many coins and we can describe them with features $X$), and the loss function will still be L(head/tail, prediction)? Another example is to predict which team will win a sports match.
Comment: 2) When the true state can be observed, i.e. $\theta$ is the same as $y$, I wonder how some of the concepts of "risk"  in statistical decision theory carry over. Most importantly, the concept of posterior expected loss defined as $E[L(\theta, \delta(X))|X]$ with the expectation taken over the posterior $P(\theta|X)$ seems meaningless, as now $\theta$ is an observed truth. The Bayes risk, $E[L(\theta, \delta(X))]$, with the expectation taken over the joint distribution of $(\theta, X)$ is another something I no longer feel I understand in the ML setting.


UnauthorizedAccessException exception in ItemAdded handler in event receiver

Question: I am having a sharepoint sitecollection which has two sites say site1 and site2.
I have a user, have contribute premission in site1 and read permission in site2.
I wrote a event receiver on item added in a list1 in site1 for moving the same record in a list2 in site2.
public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
 using (SPSite site = properties.OpenSite())
 {
   using (SPWeb web = site.OpenWeb("/site2"))
   {
    web.AllowUnsafeUpdates = true;
    SPList list = web.Lists["List2"];
    SPListItem item = list.Items.Add();
    item["Title"] = "test";
    item.Update();
    web.AllowUnsafeUpdates = false;
   }
  }
 });
   }

I tried with RunWithElevatedPrivileges even AllowUnsafeUpdates but Unfortunately it is not working in my case. on Update() it throws UnauthorizedAccessException.
{System.UnauthorizedAccessException:
    <nativehr>0x80070005</nativehr><nativestack></nativestack>

I am using sharepoint 2010 sp1 with aug2012 cumulative updates.
Comment: As an aside, you don't need the inner `using` clause for the SPWeb as disposing the owning `SPSite` will dispose any webs opened from it, and you should safely revert the `AllowUnsafeUpdates` in a `finally` block in an outer try/finally block. Right now, if it blows up you may leave the unsafe flag active.
Answer: you have to initiate the new context of the spsite from the site id:
SPSecurity.RunWithElevatedPrivileges(delegate() {
 using (SPSite site = new SPSite(item.Web.Site.ID))
 {
  using (SPWeb web = site.OpenWeb("1"))
  {
   SPListItem curritem = web.Lists[item.ListItems.List.Title].Items.GetItemById(item.ID);
  }
 }
});

Answer: In my case, in a dev enviroment, i just put the user that does the deploy, or debug mode in: site collection administrators and it works. No more exceptions like that.
I'm running a PowerShell Script with the user that is in the site collection administrators and doesn't necessary add a SPSecurity or RunWith...


flutter : The argument type 'List' can't be assigned to the parameter type 'UpdateMenuModel'

Question: how to fix this Model issue.
I know This is updateMenuDatabaseHelper.insert(data); storing as UpdateMenuModel thats why I am getting error.
But how to fix it. I am not understanding.
//// updateMenuDatabaseHelper.insert(data); This is my SQFLITE database functionality where I want to add
List<UpdateMenuModel> data = [];
data.add( UpdateMenuModel(
              productId: menuData["ProductId"],
              productTitle: menuData["ProductTitle"],
              description: menuData["Description"],
              defaultPrice: menuData["DefaultPrice"],
              isVeg: menuData["DefaultPrice"] == true ? 0 : 1,
              fileName:menuData["FileName"] != null ? menuData["FileName"] : "",
              image: menuData["Image"],
              categoryId: menuData["CategoryId"],
              toppings:menuData["Toppings"] != null ? menuData["Toppings"] : "",
              itemNo: menuData["ItemNo"],
              sizes: menuData["Sizes"],
              isActive: menuData["IsActive"] == true ? 0 : 1,
              isTopping: menuData["IsTopping"] == true ? 0 : 1,
              addedOn: menuData["AddedOn"].toString(),
              isDelete: menuData["IsDelete"] == true ? 0 : 1,
              quantity: menuData["quantity"]));
        }
        updateMenuDatabaseHelper.insert(data);  ////This is my SQFLITE database functionality where I want to add

Future<UpdateMenuModel> insert(UpdateMenuModel updateMenuModel) async {
    var dbClient = await db;
    await dbClient.insert('updateMenu', updateMenuModel.toJson());
    return updateMenuModel;
  }

Answer: try this :
Future<void> insert(List<UpdateMenuModel> updateMenuModels) async {
  var dbClient = await db;
  final batch = dbClient.batch();
  for (var updateMenuModel in updateMenuModels) {
    batch.insert('updateMenu', updateMenuModel.toJson());
  }
  await batch.commit();
}

Comment: please check again I have add something
Answer: Simply because of your insert method is accepting one modal at a time but as you written was passing the whole list to that insert method so if you want to get rid of it you have loop through each value insert it one by one or if you want to add only last value then use can use data.last.
Answer: this will add the last added UpdateMenuModel from List
updateMenuDatabaseHelper.insert(data.last);


A random variable is mapping from sample space to real numbers. How about random process?

Question: A random variable is mapping from sample space to real numbers.   How about random process?  Can we think of the simplest random process as again a mapping from sample space to real numbers, with the difference that the sample space now contains ordered pair as the sample points - the first index giving the realizaion number and the second index representing the time index?  Is there any thing wrong with this simple explanation?
Comment: before random processes, looking at a random vectors can be useful
Answer: I think of it like this:
A random process is a family of random variables $\{X(t), t \in T\}$ defined on a given probability space, indexed by the parameter $t$ where $t$ varies over an index set $T$.
A random variable is a function defined on the sample space $S$. Thus, a random process $\{X(t), t \in T\}$ is really a function of two arguments $\{X(t, \xi), t \in T, \xi \in S \}$.
For a fixed $t(=t_k)$, $X(t_k, \xi) = X_k(\xi)$ is a random variable denoted by
$X(t_k)$, as $\xi$ varies over the sample space $S$.
On the other hand, for a fixed sample point $\xi_i \in S$, $X(t, \xi_i) = X_i(t)$ is a single function of time, called a sample function or a realization of the process.
Of course, if both $\xi$ and $t$ are fixed $X(t_k, \xi_i)$ is simply a real number.


firewall-cmd on OpenVZ CentOS 7

Question: So I've been trying to set up a webserver on my VPS with CentOS 7.
To do this I've used this tutorial. Installation of MySQL/MariaDB and PHP worked successfully. However I can't access my server, because I haven't allowed external access yet.
To do this I am forced to use these three commands (according to the tutorial):

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

The command firewall-cmd wasn't found because according to this thread, OpenVZ installs a stripped down version of CentOS7, so I used the commands from there.
However, following error message popped up when using systemctl start firewalld:

Job for firewalld.service failed. See 'systemctl status
firewalld.service' and 'journalctl -xn' for details.

systemctl status firewalld.service -l shows this info:

firewalld.service - firewalld - dynamic firewall daemon    Loaded:
loaded (/usr/lib/systemd/system/firewalld.service; enabled)    Active:
failed (Result: exit-code) since Mon 2016-07-18 04:31:46 EDT; 6min ago
Process: 12522 ExecStart=/usr/sbin/firewalld --nofork --nopid
$FIREWALLD_ARGS (code=exited, status=1/FAILURE)  Main PID: 12522
(code=exited, status=1/FAILURE)
Jul 18 04:31:46 Christof2 systemd[1]: firewalld.service: main process
exited, code=exited, status=1/FAILURE Jul 18 04:31:46 Christof2
systemd[1]: Failed to start firewalld - dynamic firewall daemon. Jul
18 04:31:46 Christof2 systemd[1]: Unit firewalld.service entered
failed state.

FYI: I did everything from a fresh installation of CentOS7, if you want I can simply reinstall CentOS and do one step differently, if that helps.
Comment: I'm pretty sure firewalld doesn't work in an OpenVZ container (and a lot of other things don't either). If you rented this from somewhere, cancel it and go get a proper VPS with actual virtualization, such as KVM.
Comment: How does OpenVZ impact this? Isn't there a possibility to get all the missing packages in order to use firewalld?
Comment: Firewalld requires D-Bus, which AFAIK doesn't work inside containers.
Comment: +1 for Michael's reply - in addition to this, you might need some kernel modules which would need to be added by your hosting provider and some hosting providers might not support all the modules you need, so better move to a VPS using KVM or even Xen.
Answer: First of all, check if you have ipv6 access, and if not; alter your etc/firewalld/firewalld.conf to set
IPv6_rpfilter = 0 and run systemctl restart firewalld.service
If still nothing, check journalctl -u firewalld for further details.
However it may well be that your host needs to provide the NETFILTER="full" capability to your container config, also they will need to allocate the beancounter numiptent to something around 500 or more.
Doing those configs should allow firewalld to start on a centos7 OpenVZ container and anything else, your logs will provide the answers to.
Cheers
Answer: Iptables, being a kernel functionality, needs to be explicitly loaded on openvz host and enabled per container.
If you have access to host, follow man vzctl and look for --netfilter. Older openvz versions would have a different procedure to do the same.

firewall-cmd on OpenVZ CentOS 7


error: strings in switch are not supported in -source 1.6 (use -source 7 or higher to enable strings in switch) in Android Studio

Question: I have created one project in Eclipse ,but now i am converting that project to android studio project. But When i am trying to run that converted project, I am getting "error: strings in switch are not supported in -source 1.6 (use -source 7 or higher to enable strings in switch)" in Messeges tab of android studio
my build.gradle file
apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "com.pcs.sliderringtineproj"
    minSdkVersion 10
    targetSdkVersion 19

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.txt'
    }

}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
dependencies {
compile project(':facebookSDK')
compile 'com.android.support:support-v4:19.1.0'
compile 'com.android.support:appcompat-v7:20.+'
compile files('libs/acra-4.5.0.jar')
compile files('libs/aspectjrt-1.7.3.jar')
compile files('libs/commons-io-2.4.jar')
compile files('libs/fmmr.jar')
compile files('libs/isoparser-1.0.1.jar')
compile files('libs/libGoogleAnalytics.jar')
compile files('libs/log4j-1.2.15.jar')
compile files('libs/picasso-2.4.0.jar')
compile files('libs/universal-image-loader-1.9.0.jar')
}

Answer: Open the build.gradle file of your application module and add the following to the section called android:
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

Comment: Sir, i have already tried that but it shows can not resolve symbol sourceCompatibility, also same warning for targetCompatibility
Comment: @SritamJagadev post the content of your build.gradle file.
Comment: And one more question: which version of Android Studio are you using?
Comment: i am using android studio version 1.0.1


Allow users to upload files and attach to specific nodes

Question: Here's what I'd like to do:
I have nodes A, B, C, and D
I want users to be able to upload files using a file field on node D, but then for those files to actually show on nodes A, B, or C, depending on what the user specifies during the upload process. So node D is just acting as an upload form of sorts -- it wouldn't show any of the uploaded files.
Is this possible to do with existing modules?
Answer: In a word, No.
At least not the way you are asking the question...
You need to be thinking about node types as well as just A, B, C & D.
I would look into the views module (tutorial here).
This would mean you could upload your files to nodes of a particular type, and then create several views that display different files uploaded based on your custom parameters.
It's a bit hard to be any more specific based on the broadness of your question.


Java Stopwatch method

Question: I have this script to run through a bunch of queries and writes the results and runtime to a txt file.
This works ok for small data sets, but for larger longer queries I get a lot of
This stopwatch is already running. messages.
Here is the relevant code
Stopwatch timer = Stopwatch.createUnstarted();

    for(String query: qrys) {
        try {
            timer.start();
            resQryES = methodforQrys(url, query);   // query result is saved in the iterable Map resQryES
            timer.stop();

            out.println("Query: " + query);
            out.println("Query execution time: " + timer);
            out.println("Query Results : " + resQryES);

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }

    out.close();

I'm thinking perhaps its because the queries are executing in parallel ,
so a stopwatch is still running - so it works with smaller datasets as queries are so fast it doesn't run any in parallel.
Should I try using a different loop type where I use indexes, and create
a matching map/array of timer values so none get mixed up ...
Comment: Does `methodforQrys` start up a new thread?
Comment: Have you tried putting `Stopwatch timer = Stopwatch.createUnstarted();` inside the `for` loop?
Comment: Yes - that fixed it thanks !  , the reason I didn't try before as I believed I allready had - but I did it before with `Stopwatch.createStarted();`
Answer: As mentioned is the comments as well. Just put this line of code inside your for loop so that it will reset for every iteration.
Stopwatch timer = Stopwatch.createUnstarted();
Your code should look like this:
for(String query: qrys) {
    Stopwatch timer = Stopwatch.createUnstarted();
    try {
        timer.start();
        resQryES = methodforQrys(url, query);   // query result is saved in the iterable Map resQryES
        timer.stop();

        out.println("Query: " + query);
        out.println("Query execution time: " + timer);
        out.println("Query Results : " + resQryES);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

}

out.close();

Comment: yep, thanks a lot all ... btw, just on a side note, if I wanna debug or view each thread created here, whats the best way ?
Comment: @Yozzer There are no separate threads created here particularly. For debugging, you can run your code with a debugger, exacts steps of which will depend on the IDE you are using.

Java Stopwatch method


Elastic Search with json field and priority with other field and filter data on top

Question: I am going to search using elastic search Engine with some selected field priory. Currently, I have the following query for searching selected field (cna == City Name) for "lon" not a priority with limit 10.
I want to add cna (cnm == Country Name) field to filter on top for "lon" search term and then load another match list.
http://{{ELASTIC_SEARCH_URL}}/rezproduction@@city/city/_search?default_operator=AND&q=cna:lon*&size=10

I have added my sample source json here.
{ rid   "3"
    aid "0"
    rnm "North America"
    anm ""
    cid "12051"
    cnid    "55"
    cncd    "US"
    sbid    ""
    sid "50"
    lat "46.35564"
    lon "-124.05565"
    index   0
    type    "City"
    google  "N"
    cna "Long Beach"
    cnm "USA"
    sna "Washington"}

Answer: I have found the solution for above question.
http://{{ELASTIC_SEARCH_URL}}/rezproduction@@city/city/_search?default_operator=AND&q=(cna:lon* AND cnm:{{Country Name which is priority}}) OR (cna:lon*)&size=10

I have add additional query parameter with AND condition for the priority column name. Here I have used Country Name field for the priority.


Solution of a fourth degree equation

Question: Is there a viable strategy to solve the following equation in an analytic way, without using numeric methods?
$(1+\frac{1}{8}x^2)^2=\frac{p^2}{2}(\sqrt{1+x^2}+1)$
Edit: When I substitute $t=\sqrt{1+x^2}$, I get:
$\frac{1}{64}(49+14t^2+t^4)=\frac{p^2}{2}(t+1)$
Comment: Try to substitute $$t=\sqrt{1+x^2}$$
Comment: The quartic equation is the best what you can get.
Comment: @KarnWatcharasupat But didn't you forget the square root in there? Or am I missing something?
Comment: @Dr.SonnhardGraubner I did that and the result seems to be a quartic equation where the term with $t^3$ is missing. But is there a way to solve that type of equation? I'll edit my question to show what I got when I did the substitution you suggested.
Answer: As you wrote, using $t=\sqrt{1+x^2}$, you end with
$$t^4+14 t^2-32 p^2 t-(32 p^2-49)=0$$ which can be solved using Ferrari's method.
If you use the steps given here, you should find that
$$\Delta=-1048576\, p^4 \left(p^2-1\right) \left(27 p^2+98\right)$$ which is negative if $p^2 \lt 1$.
We also get $P=112$, $Q=-256 p^2$, $\Delta_0=784-384 p^2$, $D=-2048 p^2$ and you can analyze the number and nature of roots depending on $p$. Now, play with the radicals (have fun) and come back to $x$ (more fun).
Personally, I would prefer to consider the function
$$f(x)=\frac{\left(1+\frac{x^2}{8}\right)^2}{2 \left(\sqrt{x^2+1}+1\right)}$$ which is symmetric and shows a minimum value equal to $\frac 14$ at $x=0$. This means that if $p^2 \lt \frac 14$ no root at all; if $p^2 = \frac 14$, a quadruple root equal to $0$ and if $p \gt \frac 14$, two real roots (good to know that they are symmetric) and  two complex conjugate  roots.
If you do not want to play with nasty radicals, just use a numerical method considering that you look for the zero of function
$$g(y)=\frac{\left(1+\frac{y}{8}\right)^2}{2 \left(\sqrt{y+1}+1\right)}-p^2$$
If you consider Newton method, series expansion built at $x=0$ or $x\to \infty$ followed by series reversion will give quite good estimates.

Solution of a fourth degree equation


Digital Uncertainty

Question: What am I doing?
I am calculating wind velocity $v$ from the mean of n samples of differential pressure $\Delta P$ using the following formula:
$$v=\frac{1}{n}\sum\limits_{i=1}^n\sqrt{\frac{2\Delta P_i}{\rho}}$$
Where $\rho$ is the air density that can be calculated using this formula:
$$\rho=\frac{1}{T}K$$
Where $T$ is temperature and $K$ is some non important constant containing barometric pressure, humidity and some other goodies.
What am I trying to achieve?
I have an example of how to calculate the uncertainty of the velocity due to the digital conversion of the temperature. But I don't fully understand it, hopefully someone in here can explain it?
What have I done so far?
This is how far I have gotten in understanding the example:
The wind velocitys sensitivity to changes in temperature can be calculated as:
$$\frac{\partial{v}}{\partial{T}}=\frac{\partial{v}}{\partial\rho}\frac{\partial\rho}{\partial{T}}$$
The wind velocity can be written as:
$$v=C\frac{1}{\sqrt{\rho}}$$
where
$$C=\frac{1}{n}\sum\limits_{i=1}^n\sqrt{\Delta P_i}$$
Then:
$$\frac{\partial v}{\partial\rho}=\frac{-C}{2\sqrt{\rho^3}}=\frac{-C}{2\sqrt{\rho^3}}\frac{\sqrt{\rho}}{\sqrt{\rho}}=\underline{\frac{-v}{2\rho}}$$
Next we calculate:
$$\frac{\partial\rho}{\partial T}=\frac{-K}{T^2}=\frac{-K}{T^2}\frac{T}{T}=\underline{\frac{-\rho}{T}}$$
Finaly we combine the two results and get the sensitivity value:
$$\frac{\partial{v}}{\partial{T}}=\frac{\partial{v}}{\partial\rho}\frac{\partial\rho}{\partial{T}}=\underline{\underline{\frac{v}{2T}}}$$
Then the example starts to calculate some uncertainties: Assuming that the temperature is converted into a voltage from 0 to 10 V with 12 bit resolution then the sampled value could be off by the quantisation limit $q$:
$$q=\frac{1}{2}\frac{10V}{2^{12}}=0.00122V$$
Since there is a uniform distribution within that voltage interval the related standard uncertainty $u_T$ at a 95% confidence interval is:
$$u_T=\frac{q}{\sqrt{3}} = 0.000704V$$
From this point I don't understand the example anymore. It continues by saying that the temperature is assumed to be $15^oC$ and this correspondes to 7.6V this is equivalent to:
$$\frac{7.6V}{2q}=3113$$
Then the example states that this means that the uncertainty $u$ has to be less than:
$$u < \frac{u_T}{3113}=0.0000023V \tag1$$
Then it calculates the sensitivity value at 10 m/s as:
$$c_{10}=\frac{\partial v}{\partial T}=\frac{10\frac{m}{s}}{2*0.00244V}=2049 \tag2$$
Then the final uncertainty due to the digitisation of the measure temperature is concluded to be:
$$uc_{10}=0.004\frac{m}{s} \tag3$$
My questions:

Is equation (1) true and why?
In equation (2) shouldn't the inserted T be a temperature and not voltage?
In equation (3) is the unit really $\frac{m}{s}$?
Is there another way of calculating the uncertainty?

Thanks a big bunch!
Answer: The bottom line of your analysis is to calculate a plausible interval within which the true velocity  may lie. If you know the resolution of your digital temperature meter, then if it reads, say, $76.1$ (hence a resolution of $0.1$), then the actual temperature will be between $76.05$ to $76.14$. These are the upper and lower bounds of what the temperature could be, correct? In that case, your uncertainty is just the largest and smallest velocity you can achieve when you vary the temperature between $76.05$ and $76.14$. (It will likely just be the endpoints of this range, but some formulas have internal maxima or minima.)

Digital Uncertainty


Expected lexical element not found

Question: Every time I used 'Count()' to count the duplicate PointIDs' on the query I get this error.
I have narrowed down the problem is with Count() function, used MAX() with Group by and didn't have any problem. This is on access database populated using ODBC connection. All the help is appriciated - I have done all the research and this is my last online resort.
SELECT Event1.PointID, Event1.LogTimeStamp, Count(Event1.PointID) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;

Answer: This is the standard syntax for what you want to do:
SELECT Event1.PointID, Event1.LogTimeStamp, Count(*) AS acount
FROM Event1
GROUP BY Event1.PointID, Event1.LogTimeStamp;

Answer: I suspect you actually want to return all duplicate records which is a 2 step operation.
Step 1 get find the ids which are duplicated
   SELECT Event1.PointID, Count(*) AS NumOfRecords
   FROM Event1
   GROUP BY Event1.PointID
   HAVING COUNT(*) > 1

Step 2 join that result back to the original table to find the records
SELECT e.*, d.NumOfRecords
FROM
    Event1 e
    INNER JOIN (
       SELECT Event1.PointID, Count(*) AS NumOfRecords
       FROM Event1
       GROUP BY Event1.PointID
       HAVING COUNT(*) > 1
    ) d
    ON e.PointId = d.PointId

Comment: Step 1 works pretty good - gives me the counts but Step 2 doesn't return the NumOfRecords col. It just dumbs the whole record from the original data.   Thanks for all your help and time. Regards, -aK>
Comment: yeah it looked like you wanted the records them selves.  you can add NumOfRecords into step 2 easily it will just be a repeated value for each of the actual counts so in step 2 just append ,d.NumOfRecords to SELECT e.*  line so it reads SELECT e.*, d.NumOfRecords

Expected lexical element not found


Usar dois títulos (labels) em um input group bootstrap

Question: Como posso usar dois tílos (label) em cima de um group button usando bootstrap?
imagem:

A ideia é usar "range" numérico de 0 a 10, com um título na ponta esquerda e um título do lado direito.
Comment: Só colocar dois labels e uma `margin` no **label 1**.
Comment: Pode exemplificar com o código? porque o tamanho desse "group button" é variável
Comment: A resposta do Hugo já lhe atende não?
Answer: Opção 1
Usando o próprio grid do Bootstrap vc coloca o btn-group em um row e as labels em um row acima, cada label vc deixa de uma lado do row usando as classes  flex do Bootstrap
Essa opção não tem css customizado, apenas o css nativo do Bootstrap

/* só com o css nativo do Bootstrap */
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />

            label 1

            label 2

            <input type="radio" name="options" id="option1" autocomplete="off" checked> Active

            <input type="radio" name="options" id="option2" autocomplete="off"> Radio

            <input type="radio" name="options" id="option3" autocomplete="off"> Radio

Opção 2
Usando um before e um after no btn-group, no content de cada pseudo elemento vc coloca o texto que quiser e deixa um alinhado a direita e outro a esquerda.

[data-toggle="buttons"] {
  position: relative;
  top: 100px;
}
[data-toggle="buttons"]::after,
[data-toggle="buttons"]::before {
  position: absolute;
  top: calc(-50% - 0.25em);
  font-size: 24px;
  color: blue;
}
[data-toggle="buttons"]::after {
  content: "1";
  left: 0;
}
[data-toggle="buttons"]::before {
  content: "2";
  right: 0;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />

          <input type="radio" name="options" id="option1" autocomplete="off" checked> Active

          <input type="radio" name="options" id="option2" autocomplete="off"> Radio

          <input type="radio" name="options" id="option3" autocomplete="off"> Radio


How to find the derivative of this?

Question: $$f(z) = \frac{\exp(z)}{1+\exp(z)}$$
My thought was to apply quotient rule with the denominator needing chain rule. But I feel like my answer is off.
Here's my working:
$$\frac{exp(z)}{1+exp(z)} = \frac{(1 + exp(z))^{-1}exp(z) - exp(z)(-1)(1 + exp(z))^{-2}(exp(z))}{(1 + exp(z))^2}$$
$$= \frac{exp(z)}{(1+exp(z))^3} + \frac{exp(z)^2}{(1+exp(z))^4}$$
$$= \frac{exp(z) + exp(z)^2 + exp(z)^2}{(1 + exp(z))^4}$$
$$= \frac{exp(z)(1 + 2exp(z))}{(1 + exp(z))^4}$$
Comment: Your suggested approach will work. Why don't you show us what you tried?
Comment: Just show us your answer.
Comment: It's hard to be more accurate without seeing your attempt, but I do want to point out that you don't need the chain rule for the denominator; the derivative of $1 + \exp(x)$ is $\exp(x)$.
Comment: In case you apply the quotient rule there's no need for the chain rule.  The latter must be applied in José's approach.
Comment: Related: https://math.stackexchange.com/questions/78575/derivative-of-sigmoid-function-sigma-x-frac11e-x
Comment: updated. I'm curious why I don't know chain rule for the denominator? I think this is where i don't understand @TheoBendit
Comment: You seem not to be using the quotient rule correctly. The quotient rule states $$\left(\frac{u}{v}\right)' = \frac{u'v - uv'}{v^2}.$$ In this case, $v(x) = 1 + \exp(x)$ and $u(x) = \exp(x)$. All we need are the derivative $u' = \exp(x)$ and $v' = \exp(x)$. No chain rule was required here!
Comment: Ahh i seee. Thank you!
Answer: Your approach is fine. A simpler approach would consist of starting with the equality$$\frac{\exp(z)}{1+\exp(z)}=1-\frac1{1+\exp(z)}.$$
Comment: Another alternative: $\frac{\exp(z)}{1 + \exp(z)} = \frac{1}{1 + \exp(-z)}$.
Answer: Quotient rule approach:  $(\dfrac{e^z}{1+e^z})'=\dfrac {e^z(1+e^z)-e^z(e^z)}{(1+e^z)^2}=\dfrac {e^z}{(1+e^z)^2}$.

How to find the derivative of this?


Book Recommendation: Introduction to probability theory (including stochastic processes)

Question: I'm a first year undergraduate engineering student and we've got a course "Introduction to Probability Theory" which roughly covers the following topics:

addition, multiplication, marginal and conditional probability, joint
  probability, Baye's theorem, random variables, probability mass
  function, probability distribution function, moments and moments
  generation function, binomial distribution, Poisson distribution,
  exponential distribution, Gaussian /normal distribution, gamma
  distribution, Chebyshev's inequality, Schwartz inequality, q function,
  random process, autocorrelation, auto covariance function, stationary
  process, Erlang process, ergodic random process, Markov chain and
  transitional probability, order of Markov chain, Chapman-Kolmogorov
  equation, irreducible state, absorbing state, ergodoic chain, birth
  and death process, Markovian queuing models

It would be very helpful if someone could suggest me a good book which covers all the above topics, because I searched on the net but no book seems to cover all the topics. Also our professor didn't suggest any book as such, but it would be helpful to have one because sometimes the professor's explanations can be confusing.
Comment: Have a look at "Probability Theory" bei Klenke
Comment: @Gibbs This question is asking for a book recommendation, I don't see why this is not a legitimate answer.
Comment: I would say is actually fine as a comment, not as an answer.
Answer: As a mathematics student I've had courses on probability theory and stochastic processes, and for both of those courses used the book Probability Theory and Random Processes by Grimmet and Stirzaker. The explanations were clear, and I remember the exercises in the book to be quite challenging. At least in my experience back then, but I didn't have any previous experience with probability theory at that point.
Just had a look through the Contents, and it seems to cover pretty much all your topics (as far as I can tell from the contents).
Hope that helps.


Multiple carousels in Tabs

Question: I have a bunch of tabs, each tab should contain a carousel (e.g Tab1 to Carousel1, Tab2 to Carousel2 etc.) However only the first tab's carousel renders. I have tried everything from generating unique ids to using class selectors, still can't get it to work as desired. Thanks
Html with razor hooks

        @{int incs=0;}
        @foreach(var row in db.Query(queryCategory))
        {

@row.Category_Name

        }

    @{bool first = true;}

    @foreach(var row in db.Query(queryCategory))
    {
        var ids= @row.Category_ID;
        <div class="@{if (first){<text>tab-pane active</text> first = false;}else{<text>tab-pane</text>}}" id=@ids>

I'm in @row.Category_Name.

                @foreach(var row1 in db.Query("SELECT * FROM Food WHERE Category_ID = @0", ids))
                {

                        @row1.Food_Name

                }

        }

Jquery
$('.carousel').each(function(index) {
    $(this).elastislide();
});

Answer: Try call .elastislide() when tab was changed.
$('#myTab a').click(function (e) {
    e.preventDefault();
    $(this).tab('show');
    $("div.tab-content").find('ul.carousel').elastislide();
});

Comment: Thanks, though still not working right, It duplicates the sliders repeatedly on the first tab. Any ideas?

Multiple carousels in Tabs


In the context of Survival Analysis: When are the covariates for a given patient measured?

Question: Are the covariates taken when a datapoint enters the study or at some other time? For example, is a patient's blood pressure taken at his/her entry into the study or do we take it at some other time? Does this depend on the type of study?
Answer: A Cox survival model bases it calculations on covariate values for each case still at risk at the time of each event. That means you need covariate values for all cases at early times, maybe not exactly at time = 0 but certainly for the time of the first event. From that perspective you should ideally have continuous measurements of all potentially time-varying covariates throughout the study for all individuals.
As such continuous measurements are impractical, investigators have to use their judgment based on knowledge of the subject matter to decide on the measurement times. For example, if a covariate isn't varying quickly with time a value taken a short while after the formal time = 0 might be used for the value at time = 0. For blood pressure (BP), values might be taken at each follow-up visit; the hidden standard assumption then is that the BP value measured at one visit is constant until the next visit.
Time-varying covariates can be handled well in analysis, but they do pose challenges in terms of predictions from survival models. To predict survival for a new case from a model with time-varying covariates, you somehow have to know all the covariate values for that new case over all time into the future. How can you reasonably expect to know those values? So be very careful with time-varying covariates.
Answer: It would be ideal to measure all the co-variates at the time the patient enters the study. If you are using something like a proportional hazards model for survival, then the hazard function is assumed separable in time and the covariates, i.e. $h(x,t) = h_0(t) * f(x)$. Now if you are using co-variates like say gender, these are constant and won't change with time. But if your co-variates are also varying with time, e.g. blood pressure, then it's best to measure the blood pressure at a fixed time. However, since in observational studies, this is not always under your control, you can choose a fixed time for each co-variate. For example, the blood pressure could be considered 2 weeks before the time the patient "enters" the study while blood sugar level could be 3 weeks before. But for a given co-variate, it has to be at the same relative time for all patients. Otherwise you might have to treat it as missing data and use some imputation technique.
Answer: I second dshirodkars answer on this. All variables should be known at the start of your survival period - timepoint 0.
However there are ways to deal with variables that change over the time of observation. For example if you repeatedly measure blood pressure throughout your study. These are called time-dependent or time-varying covariates. There is a great vignette on Cox models that deal with such covariates: https://cran.r-project.org/web/packages/survival/vignettes/timedep.pdf


Secure connection errors without SSL

Question: I have several users who are encountering messages about secure connections
to the server when trying to access my website.

We've had this on Mac (iphone, ipad and macbook w/ safari) & Windows (IE and Chrome).
This is happening on multiple ISPs in different towns.
The website address is a subdomain and does not have an SSL certificate installed - it isn't being accessed via https://.
The main domain does have an ssl certificate (not wildcard), and is showing no errors when checking.

I'm having trouble tracking down what the hell is happening. I suspect it's gotta be on our end with that number of people from different browsers / devices. I'm not even sure what question to ask next or how to isolate the issue. I'm also not able to duplicate the issue locally, which doesn't help.
How can I troubleshoot this??
Edit:
Here's a screenshot from the iphone user. I didn't include it earlier as I didn't think it told anything useful.
Comment: You'll need to get more detailed information/screenshots/etc from your customers that are seeing these errors.
Comment: @eeaa - I've only been able to get a screenshot from the iphone user (added to the post). I'm not even sure at this stage what to ask next?
Comment: Do you by chance have the header `Strict-Transport-Security` being sent from your primary SSL domain with the `includeSubDomains` option?  This would force compliant clients to connect to your subdomain over https even if they try connecting to http
Comment: @drew010 - it would appear that I do! that'll be the problem then most likely. I'll get that sorted and will let you know if fixes. ta!!
Comment: Cool I will post it as an answer and if that turns out to be the case you can accept.
Answer: This could happen to clients if you primary domain sends a Strict-Transport-Security response header with the includeSubDomains flag.
Example header:
Strict-Transport-Security: max-age=31536000; includeSubDomains

If this header is present with includeSubDomains, compliant clients that later access your subdomain will change all HTTP references to HTTPS and attempt to access the subdomain over https.
Unfortunately, if max-age in the header is set to something really high, clients will remember this value and continue to attempt to access your site over HTTPS even if you were to later remove that header.  From the example above, clients will remember for 1 year that you required HTTPS when accessing the site and subdomains.  If this is the case and a large number of users are running into this problem you may need to install an SSL certificate for any subdomains that were previously insecure.
From OWASP:

Use caution when setting excessively strict STS policies. Including
  subdomains should only be used in environments where all sites within
  your organization for the given domain name require ssl. Max-age
  limits should be carefully considered as infrequent visitors may find
  your site inaccessible if you relax your policy.
Before enabling includeSubDomains, also consider the impact of any
  existing DNS CNAME records for CDNs, email services, or other 3rd
  party services. Since includeSubDomains will force such CNAME
  subdomains to https:// it's likely the browser will throw a
  domain-mismatch error, which is hard to reverse because of the browser
  caching nature of HSTS.

References:
Wikipedia - HTTP Strict Transport Security
OWASP - HTTP Strict Transport Security
Comment: still working on getting the setting changed, but yeah the `max-age` is set to a year :/ is there no other way to force that as well? even from the client side?
Comment: Since this security setting was designed to force future connections to be secure (to prevent various types of attacks) I would say first try removing the `includeSubDomains` flag, then consult with one of the users who is getting the error and see if they are later able to access your subdomain after first accessing the primary domain again.  If their clients truly follow the recommendation, they will remember the max age of 1 year and still try to connect over https so the only option may be to obtain an SSL cert for your subdomain.

Secure connection errors without SSL