How to use same static IP from anywhere

Question: Not sure if this belongs here or is a dumb question so feel free to kindly tell me to get out of here(I'm New to all of this).
So I need to be able to have SSH access to a server for pulling down changes/managing some apps. SSH access is only allowed for certain IPs but I want to be moving around sometimes and working from different places. Is there a way that I can have a static IP and use it from anywhere?
I was thinking maybe remote desktop access but that would mean I need another computer that is always running at my current office? Is there any other options? Thanks for any info you can give me!
Answer: One way you could get a static IP is by renting a VPS somewhere. Then you can connect to the VPS using SSH or VPN, and from the VPS to the server with SSH.
One may notice that in this setup the VPS is being used in a way, that is similar to a bastion host or a jump server. However it does not nearly match the security standards you would expect from a bastion host.
In particular communication between bastion host and server is expected to go over a private and secure network. But the communication between VPS and server would go over the public internet.
So you can improve security by ditching the VPS and setting up a real bastion host in the same data center as the target server.
If at this point you were to decide that for extra security, the bastion host will only allow connections from white-listed IPs, you have completed a full cycle.
If you for some reason were to go through this cycle over and over again, you'd end up adding layers of bastion hosts. But layers of bastion hosts don't add security. Having to go through one bastion host could be a good idea, having to go through two is just stupid.
Obviously if you do go through this entire cycle, you are doing something wrong. I can see two possibilities for where the mistake would be, which of the two is the mistake depends on your security policy:

Limiting access to the bastion host to a white-list is stupid, because the bastion host itself is supposed to be the one host kept secure enough, that you don't need that kind of protection.
Trying to access the bastion host from a machine with no static IP address is simply an attempt at bypassing the security policy, and it should not be done. Instead one should get to the white-listed location first, and then start doing maintenance.

How to secure and use a bastion host
If we assume both bastion host and server are accessed using the SSH protocol, then a command to connect to the server could look like this:
ssh -o ProxyCommand='ssh -W %h:%p username@bastion' username@server

In this command it is important that the SSH connection to the server comes directly from the client. The bastion is just used for transport, and even if the bastion should be compromised, you still have a second line of defense in the use of SSH to the server.
In order to have this layer of security, you must:

Never forward an ssh agent to the bastion host
Never ssh to the server from a shell on the bastion host
Always use the ProxyCommand option on the ssh command connecting to the server.

In order to keep the bastion host as secure as possible, the following should be done:

Ensure the bastion has no listening services other than sshd.
Configure sshd to only accept authentication using public key, and nothing else.
Always keep kernel and sshd on the bastion host updated with security updates.
Uninstall all software from the bastion host, which is not strictly needed.

You can run sshd on a non-standard port. This will keep most port scanners away. This servers just one purpose, it significantly reduces the number of failed login attempts you will see in your logs. It doesn't really add any security though, because the vast majority of those finding your server through port scanning are either targeting weak passwords (which won't work on a server that only accept key based authentication) or known security vulnerabilities (which you of course patch before an attack has had time to prepare an attack on the specific vulnerability). If you are facing a targeted attack, then the attacker will find your server regardless of port number.
Answer: Answering initial question: actually you can't use same static IP anywhere.
You can buy VPS and connect via VPN to it or use your current computer as intermediate link.
You can ask administrators of this server to implement port-knocking on server. When you first should telnet to some port and then have ability to login via ssh for some period of time. On linux it is implemented with knockd daemon. Port-knocking is very secure in terms of preventing brute-force attack while providing opportunity to ssh on server from anywhere.

How to use same static IP from anywhere


Are any of these sequents provable using ND rules?

Question: $$\large\begin{array}{ll}1. & \vdash \exists x~(Px\to\forall y~Py))\\2. & \forall x~\exists y~(Px\to Qy)\vdash \exists y~\forall x~(Px\to Qy)\\3. & Sj, \forall x~(\lnot(x=j)\to Sx)\vdash \forall x~Sx\end{array}$$
The first one doesn't seem right to me.
Isn't the second one the quantifier shift fallacy?
And the third one seems reasonable, but I can't find a way to prove it.
Comment: It's probably worth noting that none of those are are constructively provable and the first two aren't even classically provable if we allow an empty domain (which, admittedly, we usually don't).
Comment: You should use [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference) to rewrite the formulas rather than using an image so they are more accessible and discoverable.
Answer: The first does indeed not seem right ... but it is!  It is in fact the famous Drinker Paradox
Why does it hold?  Here is an algebraic demonstration:
$\exists x (P(x) \to \forall y \ P(y)) \overset{Implication}{\Leftrightarrow}$
$\exists x (\neg P(x) \lor \forall y \ P(y)) \overset{Distribution \ \exists \ over \ \lor}{\Leftrightarrow}$
$\exists x  \neg P(x) \lor \exists x \forall y \ P(y) \overset{Quantifier \ Negation, Null\ Quantification}{\Leftrightarrow}$
$\neg \forall x   P(x) \lor \forall y \ P(y) \overset{Replacing \ Variables}{\Leftrightarrow}$
$\neg \forall x   P(x) \lor \forall x \ P(x) \overset{Complement}{\Leftrightarrow}$
$\top$
So, assuming your ND rules are complete, this is provable.
The second sequent also holds! While you are correct that in general you cannot swap quantifiers of different types, in this particular case you can. This is because the $Px$ only makes reference to the $x$, and not the $y$, and with $Qy$ ist is just the other way around. As such, you can bring the quantifiers inside, and both sides turn out to be equivalent to:
$\exists x \ Px \to \exists y \ Qy$
And yes, the third one also holds.
But in all cases: we would need to know exactly what rules you are using in order to know how to prove something like this.
Comment: Wow this is actually crazy! Never thought they were valid since they are so counterintuitive...

I'm using a Gentzen Style ND system, if it helps I could post all the rules.
Comment: @JakobRainer Yeah, these are pretty tricky so don't feel bad!  But yes, if you need help with the proofs, we'll need to know the actual rules, since there are many different proof systems, each with their own set of rules.
Comment: Sorry it took so long!
https://imgur.com/a/nscTO62
If you have any questions, just ask. Also, I would be more than happy with just one of those three :D
Comment: @JakobRainer Thanks for the rules ... this is actually not a style of system I am too experienced with.... also, you should really try and have a go at this yourself before seeking for help. I would recommend creating a new post (maybe even one for each different problem), where you give the rules, and also your attempt.
Comment: Ok, thank you for your help, although my attempts have been less than succesfull. One last question: is this one even valid? imgur.com/a/5lXOp6p
Comment: @JakobRainer  I have a hard time parsing that sentence ... what happens with the $x_1$? It looks like there is a predicate $A$, and a statement $Ax_1$ ... but this has obviously no relationship at all to the rest of the premises: are you sure you copied this correctly?
Comment: Yes, it is correctly copied. It is probably just there to throw one off. You can most than likely just leave it away.
Comment: @JakobRainer  Yeah, that makes sense. Indeed, all you need to prove the conclusion is to use the premise $Fa \lor Fb$. So, it is valid.
Comment: Thank you, you have been a huge help!
Comment: @JakobRainer You're welcome! :)


Model Building: Missing Data or Large Gap between data points

Question: I am currently trying to build a model using a data set that has large gap between data points. When I look for the correlation I clearly see a negative regression line. But I am worried about the gap that exist between the poins.
I build a simple linear model though this has high R squared I don't think simple linear regression is the best model to that fits the data. This looks like it has a negative exponential behavior. I thought to post here to get some expert thoughts on what I should do when you deal with the data that has a large gap between points and does this data has a linear relationship or strong non linear relationship?
Data Set:
<code>   density  co2
1     20.4 38.8
2     27.4 31.5
3    106.2 10.6
4     80.4 16.1
5    141.3  7.7
6    130.9  8.3
7    121.7  8.5
8    106.5 11.1
9    130.5  8.6
10   101.1 11.1
11   123.9  9.8
12   144.2  7.8
13    29.5 31.8
14    30.8 31.6
15    26.5 34.0
16    35.7 28.9
17    30.0 28.8
18   106.2 10.5
19    97.0 12.3
20    90.1 13.2
21   106.7 11.4
22    99.3 11.2
23   107.2 10.3
24   109.1 11.4
</code>
Plot:

Summary of Linear Model:
<code>Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) 38.12948    1.21768   31.31  < 2e-16 ***
density     -0.24247    0.01261  -19.22 3.04e-15 ***
</code>
In addition if I transfer both density and co2 as log transform variables, then I see following behavior. Since data is missing at the middle its really hard to stick to a log transformed model or the base model.
Comment: I noticed your request to migrate this question to SO. Any reason why? This looks like a statistical issue, isn't it? If you have a specific R question, you can just ask on SO (with appropriate link to Cross Validated, if needed).
Comment: Draw two regression lines, one for each set of data.
Comment: Did you mean one with the log and one without?
Comment: I didn't see your log graph when I posted my comment. I meant divide the data set in two where the gap is; but if taking the log of both variables makes sense, that works too.
Comment: Any technique to divide data into two sets?
Comment: Just split them at the big gap.
Comment: @chl yeah, I am new to stats.exchange, but I think users in stackoverflow gives explanations along with R code that can be very useful thats the reason I decided to move the question. I think the community here assumes users here already know R which is not a bad thing I guess stats.exchange is for advance stat users :)
Answer: Presumably co2 means "carbon dioxide" and density means what it says. Even so, it would help to have more detail on what is happening here. Is there no physics or chemistry or engineering background to help us, or you, or everyone?
Why is there a gap? Is there no hint from the background to the data?
Are these the results of an experiment in which one variable is controlled, or something else? Which variable do you want to predict and/or regard as the response or outcome (dependent variable, if you will)? You appear to be regarding co2 as the outcome. Is that prescribed by the problem?
Some rough experiments indicate that logging just one variable might make sense too. Linear is a lousy model because if you extrapolate you soon produce negative predictions for one or other variable, which is surely unphysical.
Comment: co2 ~ density as a power function (log co2 linear in log density, as in your second graph) and as exponential decline (log co2 linear in density) both seem fairly reasonable, with a little more curvature away from the second. The choice between them should depend on the underlying science, especially limiting behaviour of the physical relationship, as well as goodness of fit.
Comment: It is results from an experiment but since I am not the one who did the experiment I don't have much information on that. What I am interest is statistically describing the behavior of the data based on whats given to me. Yes. co2 is the reponse and I want to model how co2 depends on density. One tiny thing. co2 is actually co2 rate per minute


Lebesgue Integral: Integrating the Distribution function

Question: Is there an easy way to see that if $f$ is nonnegative, then
$$\int_E f\,dx=\int_0^\infty |\{x\in E: f(x)>t\}|\,dt$$?
(where $|\cdot|$ denotes Lebesgue measure)
My book proves a more general version $\int_E f^p=p\int_0^\infty \alpha^{p-1}\omega(\alpha)\,d\alpha$, where my question is just the special case $p=1$, but the proof is rather long (using Riemann-Stieltjes integral).
Hence, I am curious if there is a shorter proof for my question, or even just an intuitive way to see it non-rigorously.
Thanks for any help.
Answer: $$\int_0^\infty |\{x\in E: f(x)>t\}|~dt$$
Let's think about what this integral means. It will be handy to draw a picture of the graph of $y = f(x)$ as you follow along. Let's pretend $f$ is a nicely regular function over some interval $[a,b]$ for the sake of illustration.
$|\{x\in E: f(x)>t\}|$ is the superlevel set of $f$, and you can find it by drawing the horizontal line at $y=t$, then projecting the line segments that get cut off by the graph of $f$ down to the $x$-axis. Then $|\{x\in E: f(x)>t\}|dt$ is a union of infinitesimal rectangles, which lie flat horizontally over the $x$-axis. Integrating over $t\in[0,\infty]$ essentially means summing up the areas of these infinitesimal rectangles.
The intuitive picture is much like Riemann integration, except instead of cutting up the graph of $f$ vertically into infinitesimal slices, you are instead cutting it up horizontally. Where Riemann integration is analogous to a loaf of sliced bread, the Lebesgue integral presented in this form is analogous to a layered cake.
Comment: Very nice. I get it now. Thanks!
Answer: Using just Fubini:
\begin{align}
\int_E f(x)dx&=\int_E\left(\int_0^{\infty}\chi_{\{t<f(x)\}}(x,t)dt\right)dx\\
&=\int_0^\infty\left(\int_E \chi_{\{t<f(x)\}}(x,t)dx\right)dt\\
&=\int_0^\infty |\{x\in E:f(x)>t\}|dt.
\end{align}
Comment: Very nice as well. How do we see $f(x)=\int_0^\infty \chi_{\{t<f(x)\}}(x,t)\,dt$?
Comment: @yoyostein it is the integral from zero to infinity of the function which is one if $t<f(x)$ and zero otherwise ($x$ is fixed)


Using Selenium in Visual Studio C# not recognizing menu option dispite sleeps

Question: I am using Visual Studio Selenium with C# to select a menu option currently I using Chrome but I will be using multiple browsers. Here is the code I am using:
{
    var dropdown = driver.FindElement(By.CssSelector(".row:nth-child(5) > .col-md-3 > .input-group > .form-control"));
    dropdown.FindElement(By.XPath("//option[. = 'Slight']")).Click();
}

I have put threads in to slow down the test and keep getting the following error.
 Message:

OpenQA.Selenium.ElementNotInteractableException : element not interactable: Element is not currently visible and may not be manipulated

  (Session info: chrome=79.0.3945.117)

Stack Trace:
RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
RemoteWebElement.Click()
ChromeResizeEditThrusterTest.chromeResizeEditThruster() line 75

Comment: See https://stackoverflow.com/questions/10641535/how-i-can-avoid-element-is-not-currently-visible-and-so-may-not-be-interacted-w/16162026
Answer: First of all when you trying to click on some web element make sure that element is currently visible on the screen, if not, try to scroll the page to the position of the element:
var element = driver.FindElement(By.Id("your_element_id"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);
Thread.Sleep(1000);

Also for easily manage specific web elements try to use Selenium.Support,
for example:
var webElemnt = driver.FindElement(By.Id("your_dropdown_id"));
var dropdown = new SelectElement(webElemnt);
dropdown.Options[1].Click();

Comment: First I am not using a JavaScript executable. Second all of the elements are available when I go into it manually I can see all of the menu options and they are able to be clicked on without any issues. Another point I used a Thread.Sleep(10000); Just to make sure it wasn't the script moving to fast.


How to add data and update in arraylist

Question: I have data to my list and I save it. That's working properly but when I add data again to list it deletes old data. But I don't want that. I want it to add next to the already existing data or append to existing list.
List.add(new Users("name", "surname"));
adapter = new Adapterr(this,List);
saveData();

Respective methods are as follows:
private void saveData() {
    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(gonderilenFallar);
    editor.putString("adapterss", json);
    editor.apply();
}

 private void loadData(){
    SharedPreferences sharedPreferences =
    getActivity().getSharedPreferences("shared preferences",MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("adapterss",null);
    Type type = new TypeToken<ArrayList<Adapterr>>(){}.getType();
    List = gson.fromJson(json,type);

    if(List == null){
    List = new ArrayList<>();
                   }
    }

Answer: To append the data to already existing data you need to

Fetch the list if already exists
Append data to that list
private void saveData() {

SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();

//Fetch List
String json = sharedPreferences.getString("adapterss",null);
Type type = new TypeToken<ArrayList<Adapterr>>(){}.getType();
List<type> list = gson.fromJson(json,type);

//If already exists append to that list
if(list != null && list.size() > 0){
    for (int i = 0; i < newListToBeAppended.size(); i++) {
        list.add(newListToBeAppended.get(i));
    }
} else{
    //If not exists add the list which is fresh
    list = newListToBeAppended;
}

String listJSON = gson.toJson(list);//Assuming gonderilenFallar is your list
editor.putString("adapterss", listJSON);
editor.apply();
}

How to add data and update in arraylist


how to use AngularJS value as parameter in function?

Question: I need to pass an ID value once the button is clicked.
but gives me this error: error message
What is the correct way of using: ng-click="passID({{x.id}})" ?

            {{x.name}} <button class="viewDetails" ng-click="passID({{x.id}})" type="button">VIEW DETAILS</button>

    {{x.size}}
    {{x.storage}}

Answer: Just without {{}}:
ng-click="passID(x.id)"

Comment: ahaha thank youuuuuuuuuuuuuuuuuuuuuuu
Answer: correct way of using it is: ng-click="passID(x.id)"
You don't need to use interpolation: {{}}.
Answer: Simply use
ng-click="passID(x.id)"
instead of ng-click="passID({{x.id}})"
Because ng-click is an Angular directive it can understand the expression, as the element is inside the ng-repeat directive you can directly access the object specified in the ng-repeat


How do I find a missing angle using a reciprocal trigonometric function?

Question: I just attempted this as best as I could, but I'm not sure if I'm correct.
Here's the work:
$$\cot x =\frac{1}{2}$$
$$\frac{1}{\tan{x}} = \frac{1}{\frac{1}{2}}$$
$$\frac{1}{\tan^{-1}\cdot\tan x} = \frac{1}{\tan^{-1}\cdot\frac{1}{2}}$$
$$\frac1x = \frac1{25.56505118}$$
$$x = 0.03911590057$$
Since I wasn't able to successfully search up anything relative to the matter, I'd appreciate if someone would tell me if this is correct. If not, then what are the correct steps that I'm supposed to take?
Comment: So you're looking for $x$, correct?  Are you looking for an analytic solution, or an approximation to some number of decimals?
Comment: @John Yes, I am looking for x, the degrees of the angle.
Comment: Sorry, I was editing my comment when you responded.  I mean, $x = \arctan 2$ is a solution, but I guess you need something else?  Like an approximation, or possibly an exact solution in some closed form?
Comment: @John An approximation is good enough.
Answer: I was unable to edit your question since there are places I cannot determine your intent. But you can proceed this way:
$$\cot x = \frac{1}{2}$$
$$\frac{1}{{\tan x}} = \frac{1}{2}$$
$$\tan x = 2$$
Can you continue from there?
Answer: Here are the steps,
\[ \cot x =\frac{1}{2} \]
\[ \frac{1}{\tan x} =\frac{1}{2} \]
\[ \tan x =2 \]
\[ \arctan(\tan x)=\arctan 2 \]
\[ x =\arctan 2=1.1071487\ \mbox{rad}= 63.43^{\circ} \]
Comment: That was exactly what I was trying to do; thank you!
Comment: @user3758041, no problem. Keep practicing and eventually it'll be easy to you.


Proof by induction that $\sum_{i=1}^n i/2^i=2-(n+2)/2^n$

Question: Prove by induction that
$$\sum_{i=1}^n \frac{i}{2^i}=2-\frac{n+2}{2^n}.$$
this is a question that must be proved by induction. For the base case I used $n=1$ and simplified to $1/2$ and now for the inductive step
$$p(k)\to p(k+1)=2-\frac{(k+1)+2}{2^{k+1}}.$$
I am not sure if this is right step or if I should simply be $p(k)+(k+1)$.
Comment: A basic introduction [here](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)
Comment: You need to use $\sum_{i=1}^{k+1} i/2^i  =\sum_{i=1}^{k} i/2^i  + (k+1)/2^{k+1}$.
Comment: is that implying to sub in(k+1)? @ArcticChar
Comment: A strategy for *all* induction proofs about sums.  To prove $\sum_{k=1}^n a_k = f(n)$, then for the induction step can *ALWAYS* but $\sum_{k=1}^{n+1} a_k = a_{n+1} + \sum_{k=1}^n a_n = a_{n+1} + f(n)\underbrace{=}_{to\ prove}f(n+1)$.   So to do an induction step about sums it is *ALWAYS* sufficient to prove $ a_{n+1} + f(n)=f(n+1) $.  The will *always* work (if it can be done) and that should *always* be your first strategy.
Comment: @fleablood Thanks I think I get it now, instead of adding k+1 I am supposed to add the previous term to the next term since it is the sigma notation
Comment: So if you can prove $\frac {n+1}{2^{n+1}} + (2 -\frac {n+2}{2^n})=2 - \frac {(n+1)+2}{2^{n+1}}$ you are done.  That will prove your induction step.
Answer: Suppose $\sum_{i=1}^n \frac{i}{2^i}=2-\frac{n+2}{2^n}$ for some $n$.
Induction step:  $\sum\limits_{i=1}^{\color{blue}{n+1}} \frac{i}{2^i}=\color{blue}{\frac {n+1}{2^{n+1}}} + (\sum\limits_{i=1}^{\color{red}{n}} \frac{i}{2^i})=$
$\frac {n+1}{2^{n+1}} + (2-\frac{n+2}{2^n}) =$
$.........=$
$2 - \frac {(n+1)+2}{2^{n+1}}$
Can you fill in the missing step?


Differential Equation help: $\frac{dy}{dx}=\frac{y-3}{x^2 +y^2}$

Question: The question is: solve for $y(x)$

$$\dfrac{dy}{dx}=\dfrac{y-3}{x^2 +y^2}$$
  given $y(0)=1$.

I am struggling to find a way to separate the variables.
Also as a side question, if you have to integrate $y$ with respect to $dx$.
Would the answer be $\dfrac{y^2}2\dfrac{dx}{dy}$.
My reasoning is that I have integrated y and divided by the derivative of y with respect to x?? I am really unsure...
Note $y$ is a function of $x$.
Comment: I guess removing the word "urgent" changes the context of the question? That was my rationale for downvoting this question ...
Answer: If you consider the inverse function $x(y)$, the equation $\dfrac{dx}{dy}=\dfrac{x^2 +y^2}{y-3}$ is a Riccati ODE. So, it can be analyticaly solved, but the arduous calculus involves complicated functions ( Kummer and Tricomi functions with complex variables).
Are you sure that there is no mistake in the writting of your equation ?


What is the intuition behind SVD?

Question: I have read about singular value decomposition (SVD). In almost all textbooks it is mentioned that it factorizes the matrix into three matrices with given specification.
But what is the intuition behind splitting the matrix in such form? PCA and other algorithms for dimensionality reduction are intuitive in the sense that algorithm has nice visualization property but with SVD it is not the case.
Comment: You might want to start from the intuition of eigenvalue-eigenvector decomposition as SVD is an extension of it for all kinds of matrices, instead of just square ones.
Comment: SVD can be thought as a compression/learning algorithm. It is a linear compressor decompressor. A matrix M can be represented by multiplication of SVD. S is the compressor V determines how much error you would like to have (lossy compression) and D is the decompressor. If you keep all diagonal values of V then you have a lossless compressor. If you start throwing away small singular values (zeroing them) then you cannot reconstruct the initial matrix exactly but will still be close. Here the term close is measured with Frobenius norm.
Comment: Do you know how to estimate PCA with SVD? If you do, then can you explain why you feel that something missing in your understanding of SVD? See [this](http://www.cs.fsu.edu/~lifeifei/cis5930/lecture12-a.pdf)
Comment: @Cagdas if you do that please carefully define what you're taking "S" "V" and "D" to be mathematically. I've not seen the initials overloaded into the notation itself before (which has the singular values in it, for example?). It seems to be a likely source of confusion,
Comment: There are plenty of notes on internet and answers here on CV about SVD and its workings.
Comment: Just for fun: [the SVD song](https://www.youtube.com/watch?v=JEYLfIVvR9I).
Comment: @kjetilbhalvorsen this song is underrated.
Comment: @Cagdas, +1, would you maybe like to expand this into a proper answer? You will have my +1.
Comment: @Glen_b Yes I haven't followed the UDV* notation. They are just letters, and most sources are bogged down in math without any intuition anyhow.
Comment: Yes, but you then meed to make clear what the symbols represent, especially since you also have a D and a V
Comment: The notation I am used to is $USV^\top$.
Comment: @CagdasOzgenc If it helps, the intuition regarding SVDs that I learned was that it was appropriate as a dimension reduction method for asymmetric matrices -- as already noted -- as well as non-normal, nonlinear as well as categorical scale types. So, if standard PCA is appropriate for continuously distributed, symmetric and linear data with finite moment structures, SVDs are generalizable to a much wider class of input information.
Comment: @DJohnson When you have an autoencoder that has a parameter which lets you throw away information in a decreasing manner with respect to a loss function (Frobenius norm in this case) you can use it as dimensionality reduction purposes. There are many other options. You can for example take Fourier transform of data and keep only terms with large absolute values. Or Discrete Cosine Transform like it is applied in JPEG.
Comment: @CagdasOzgenc My point was about compatibility and tractability of the matrix operations between SVD and the inputs. Your point seems addressed to issues with dimension reduction of those inputs. Are we saying the same thing?
Comment: Not purely intuitive, but should be easily understandable to anyone with basic linear algebra knowlege(or watch 3Blue1Brown's lin algebra videos):
http://www.ams.org/samplings/feature-column/fcarc-svd I found this really helpfull.
Comment: @Aksakal link's broken
Answer: Write the SVD of matrix $X$ (real, $n\times p$) as
$$
   X = U D V^T
$$
where $U$ is $n\times p$, $D$ is diagonal $p\times p$ and $V^T$ is $p\times p$. In terms of the columns of the matrices $U$ and $V$ we can write
$X=\sum_{i=1}^p d_i u_i v_i^T$. That shows $X$ written as a sum of $p$ rank-1 matrices. What does a rank-1 matrix look like? Let's see:
$$
\begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \begin{pmatrix} 4 & 5 & 6 \end{pmatrix} = \begin{pmatrix} 4 & 5 & 6 \\ 8 & 10 & 12 \\ 12 & 15 & 18 \end{pmatrix}
$$ The rows are proportional, and the columns are proportional.
Think now about $X$ as containing the grayscale values of a black-and-white image, each entry in the matrix representing one pixel.  For instance the following picture of a baboon:

Then read this image into R and get the matrix part of the resulting structure, maybe using the library pixmap.

If you want a step-by-step guide as to how to reproduce the results, you can find the code here.

Calculate the SVD:
    baboon.svd  <-  svd(bab) # May take some time

How can we think about this?  We get the $512 \times 512$ baboon image represented as a sum of $512$ simple images, with each one only showing vertical and horizontal structure, i.e. it is an image of vertical and horizontal stripes! So, the SVD of the baboon represents the baboon image as a superposition of $512$ simple images, each one only showing horizontal/vertical stripes.  Let us calculate a low-rank reconstruction of the image with $1$ and with $20$ components:
    baboon.1  <-  sweep(baboon.svd$u[, 1, drop=FALSE], 2,
                  baboon.svd$d[1], "*") %*%
                       t(baboon.svd$v[, 1, drop=FALSE])

    baboon.20 <-  sweep(baboon.svd$u[, 1:20, drop=FALSE], 2,
                baboon.svd$d[1:20], "*") %*%
                       t(baboon.svd$v[ , 1:20, drop=FALSE])

resulting in the following two images:

On the left we can easily see the vertical/horizontal stripes in the rank-1 image.
Let us finally look at the "residual image", the image reconstructed (as above, code not shown) from the $20$ rank-one images with the lowest singular values. Here it is:

Which is quite interesting: we see the parts of the original image that are difficult to represent as superposition of vertical/horizontal lines,  mostly diagonal nose hair and some texture, and the eyes!
Comment: I think you meant low-rank reconstruction, not low range. Never mind. This is a very good illustration (+1). That's why it is a linear compressor decompressor. Image is approximated with lines. If you actually perform a similar autoencoder with a neural network with linear activation functions, you will actually see that it also allows lines with any slope not only vertical and horizontal lines, which makes it slightly more powerful than SVD.
Comment: See https://math.stackexchange.com/questions/92171/what-do-eigenvalues-have-to-do-with-pictures/92187#92187    for some other examples
Comment: @kjetil-b-halvorsen Shouldn't the SVD $X = U \Sigma V^*$ for a $n \times p$ matrix $X$ result in $U$ being $n \times n$, $\Sigma$ being $n \times p$ and $V$ being $p \times p$? Typo?
Comment: @kjetil-b-halvorsen I am interested in knowing how would the decription change if I would have used PCA for denosing application. I would appreciate if you could answer my question here https://stats.stackexchange.com/questions/412123/pca-vs-svd-difference-in-denoising-applications
Comment: @CowboyTrader interesting observation. My understanding of machine learning/neural network is pretty limited. So, I fail to understand that if one has a single noisy image and nothing else to train on, how would the neural network work?
Answer: Let $A$ be a real $m \times n$ matrix. I'll assume that $m \geq n$ for simplicity. It's natural to ask in which direction $v$ does $A$ have the most impact (or the most explosiveness, or the most amplifying power). The answer is
\begin{align}
\tag{1}v_1 = \,\,& \arg \max_{v \in \mathbb R^n} \quad \| A v \|_2 \\
& \text{subject to } \, \|v\|_2 = 1.
\end{align}
A natural follow-up question is, after $v_1$, what is the next most explosive direction for $A$? The answer is
\begin{align}
v_2 = \,\,& \arg \max_{v \in \mathbb R^n} \quad \| A v \|_2 \\
& \text{subject to } \,\langle v_1, v \rangle = 0, \\
& \qquad \qquad \, \, \, \, \|v\|_2 = 1.
\end{align}
Continuing like this, we obtain an orthonormal basis $v_1, \ldots, v_n$ of $\mathbb R^n$. This special basis of $\mathbb R^n$ tells us the directions that are, in some sense, most important for understanding $A$.
Let $\sigma_i = \|A v_i \|_2$ (so $\sigma_i$ quantifies the explosive power of $A$ in the direction $v_i$).  Suppose that unit vectors $u_i$ are defined so that
$$
\tag{2} A v_i = \sigma_i u_i \quad \text{for } i = 1, \ldots, n.
$$
The equations (2) can be expressed concisely using matrix notation as
$$
\tag{3} A V = U \Sigma,
$$
where $V$ is the $n \times n$ matrix whose $i$th column is $v_i$, $U$ is the $m \times n$ matrix whose $i$th column is $u_i$, and $\Sigma$ is the $n \times n$ diagonal matrix whose $i$th diagonal entry is $\sigma_i$. The matrix $V$ is orthogonal, so we can multiply both sides of (3) by $V^T$ to obtain
$$
A = U \Sigma V^T.
$$
It might appear that we have now derived the SVD of $A$ with almost zero effort. None of the steps so far have been difficult. However, a crucial piece of the picture is missing -- we do not yet know that the columns of $U$ are pairwise orthogonal.
Here is the crucial fact, the missing piece: it turns out that $A v_1$ is orthogonal to $A v_2$:
$$
\tag{4} \langle A v_1, A v_2 \rangle = 0.
$$
I claim that if this were not true, then $v_1$ would not be optimal for problem (1). Indeed, if (4) were not satisfied, then it would be possible to improve $v_1$ by perturbing it a bit in the direction $v_2$.
Suppose (for a contradiction) that (4) is not satisfied. If $v_1$ is perturbed slightly in the orthogonal direction $v_2$, the norm of $v_1$ does not change (or at least, the change in the norm of $v_1$ is negligible). When I walk on the surface of the earth, my distance from the center of the earth does not change. However, when $v_1$ is perturbed in the direction $v_2$, the vector $A v_1$ is perturbed in the non-orthogonal direction $A v_2$, and so the change in the norm of $A v_1$ is non-negligible. The norm of $A v_1$ can be increased by a non-negligible amount. This means that $v_1$ is not optimal for problem (1), which is a contradiction. I love this argument because: 1) the intuition is very clear; 2) the intuition can be converted directly into a rigorous proof.
A similar argument shows that $A v_3$ is orthogonal to both $A v_1$ and $A v_2$, and so on. The vectors $A v_1, \ldots, A v_n$ are pairwise orthogonal. This means that the unit vectors $u_1, \ldots, u_n$ can be chosen to be pairwise orthogonal, which means the matrix $U$ above is an orthogonal matrix. This completes our discovery of the SVD.

To convert the above intuitive argument into a rigorous proof, we must confront the fact that if $v_1$ is perturbed in the direction $v_2$, the perturbed vector
$$
\tilde v_1 = v_1 + \epsilon v_2
$$
is not truly a unit vector. (Its norm is $\sqrt{1 + \epsilon^2}$.) To obtain a rigorous proof, define
$$
\bar v_1(\epsilon) = \sqrt{1 - \epsilon^2} v_1 + \epsilon v_2.
$$
The vector $\bar v_1(\epsilon)$ is truly a unit vector. But as you can easily show, if (4) is not satisfied, then for sufficiently small values of $\epsilon$ we have
$$
f(\epsilon) = \| A \bar v_1(\epsilon) \|_2^2 > \| A v_1 \|_2^2
$$
(assuming that the sign of $\epsilon$ is chosen correctly).
To show this, just check that $f'(0) \neq 0$.
This means that $v_1$ is not optimal for problem (1), which is a contradiction.
(By the way, I recommend reading Qiaochu Yuan's explanation of the SVD here. In particular, take a look at "Key lemma # 1", which is what we discussed above. As Qiaochu says, key lemma # 1 is "the technical heart of singular value decomposition".)
Comment: Love this answer. This is the best way to think about the SVD.
Comment: Fantastic answer! You made my day!
Answer: Take an hour of your day and watch this lecture.
This guy is super straight-forward; It's important not to skip any of it because it all comes together in the end. Even if it might seem a little slow at the beginning, he is trying to pin down a critical point, which he does!
I'll sum it up for you, rather than just giving you the three matrices that everyone does (because that was confusing me when I read other descriptions). Where do those matrices come from and why do we set it up like that? The lecture nails it! Every matrix (ever in the history of everness) can be constructed from a base matrix with same dimensions, then rotate it, and stretch it (this is the fundamental theorem of linear algebra). Each of those three matrices people throw around represent an initial matrix (U), A scaling matrix (sigma), and a rotation matrix (V).
The scaling matrix shows you which rotation vectors are dominating, these are called the singular values. The decomposition is solving for U, sigma, and V.
Comment: Another useful lecture:https://www.youtube.com/watch?v=yA66KsFqUAE

What is the intuition behind SVD?


how to change the background color on activity with items in a listview?

Question: I'm trying to change the background color on activity with a listview in android studio, but nothing happens when i click on the items in the listview. Can someone help me?
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));

    view = this.getWindow().getDecorView();
    view.setBackgroundResource(R.color.white);
}

public void onListItemClick(ListView parent, View v, char position, long id) {
    String s = label.getText().toString();
    double l = Double.parseDouble(s);
    double o;
    switch (position) {
        case 0:
            view.setBackgroundResource(R.color.red);
            break;
        case 1:
            view.setBackgroundResource(R.color.green);
            break;
        case 2:
            view.setBackgroundResource(R.color.white);
            break;
        case 3:
            view.setBackgroundResource(R.color.orange);
            break;

    }
}

activity_main.xml
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

Comment: Show ur activity_main xml file
Comment: i have updated the question with the activity_main.xml
Answer: Update your activity_main.xml as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_screen"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

And cast layout in your activity and apply background colour to the layout.This will change the background colour of the screen
private LinearLayout mLayout=null;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));
        mLayout = (LinearLayout) findViewById(R.id.background_layout);
        view = this.getWindow().getDecorView();
        view.setBackgroundResource(R.color.white);
    }

    public void onListItemClick(ListView parent, View v, char position, long id) {
        String s = label.getText().toString();
        double l = Double.parseDouble(s);
        double o;
        switch (position) {
            case 0:
                mLayout .setBackgroundColor(Color.RED);
                break;
            case 1:
                 mLayout .setBackgroundColor(Color.GREEN);
                break;
            case 2:
                  mLayout.setBackgroundColor(Color.WHITE);
                break;
            case 3:
                 mLayout.setBackgroundColor(Color.parseColor("#FFA500"));
                break;
        }
    }

Answer: From:
view.setBackgroundResource(R.color.red);

To:
view.setBackgroundColor(Color.RED);

Answer: The recommended way to do this is that in your project files you go to app/res/values and open colors.xml
In there add colors like this :  <color name="darkGreen">#669900</color>
Then in your mainActivity.java write
Case0: view.setBackgroundColor(getResources().getColor(R.color.darkGreen)); break;
If you need to find the hexidecimals for the colors you want you can use a website color picker like this one :
https://www.w3schools.com/colors/colors_picker.asp
Answer: Within your XML, provide the parent layout with an android:ID attribute and wire it in your java class.
Then onClick change the colour of the layout and it should change the background colour of the parent layout.
Comment: how do i do that?
Answer: This should work
first add id to your layout in activity_main layout
like
 <LinearLayout
       android:id="@+id/LinearLayout1"
   ........

Then in your Activity
 final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);

   public void onListItemClick(ListView parent, View v, char position, long id) {
         if(iscolor)
         {
             layout.setBackgroundColor(Color.BLUE);
             iscolor = false;
         }
         else
         {
             layout.setBackgroundColor(Color.WHITE);
             iscolor = true;
         }

        }

Choose whatever layout type you're using
Comment: That would work if i just wanted the background color on the selected item to change in the listview, but i'm trying to change the background color on the activity
Comment: why do you use position of items then if you want just change the background color of layout and not the row?
Comment: because i tried to follow an example from my teacher, and i'm not good in java... it's an error on "iscolor", how do i fix that?
Comment: you first make a boolean( Boolean iscolor;) before onCreate and then set it to true in your onCreate.Then change colors as you wish
(your teacher gave you example to change row color it seems to me)
Comment: by the way , in this answer you can do what you want just from layout .See the accepted answer but fist try the one i gave you https://stackoverflow.com/questions/5789661/android-relativelayout-change-color-onclick


Is the following definintion of Discrete Random Variable given in the book of Joe Blitzstein correct?

Question: The following is the definition given in the book:

A random variable $X$ is said to be discrete if there is a finite list
  of values $a_1, a_2,\ldots, a_n$ or an infinite list of values $ a_1, a_2,\ldots$ such that $ \ P(X = a_j \ \text{for some} \ j ) = 1 $.

Why should the probability of a random variable taking a value be one?
For example: Let a fair coin be tossed twice. Let $ X $ be the no. of heads. There can only be $P(X = 0) = P(X = 2) = \frac{1}{4}$ and $P(X = 1) = \frac{1}{2} $. There is no $k \in \mathbb{N}$ such that $P(X = k) = 1$. So does that mean from the above definition that $X$ is not a discrete r.v. ?
Comment: $P(X = a_i \,\text{for some}\, j) = P(\cup_j \{X = a_j\}) = 1.$ **Not** For some $j,$ we have $P(X = a_j) = 1.$
Answer: A good way to remedy your problem with the definition may be to think of the statement $\ P(X = a_j \ \text{for some} \ j ) = 1$ in "words" and apply this to your example.
Suppose we define our list of outcomes (the $a_i$) by $a_1 = 0$, $a_2=1$, $a_3 = 2$ which is all possible numbers of heads. What the statement $\ P(X = a_j \ \text{for some} \ j ) = 1$ means in words is that there is a probability of 1 that our random variable $X$ will take on some value in our list. When flipping the coin twice we notice that our outcome will always be a value of either $0,1,$ or $2$ heads and so indeed $\ P(X = a_j \ \text{for some} \ j ) = 1$ holds as we always (with probability 1) take on some value in the list (either 0,1, or 2).
In short, what the author is getting at is that evaluation/observation of our random variable always lies in the range defined by the $a_i$. Using your example, we want to rule out the chance of suddenly seeing 18 heads after two rolls .
Finally, I think the comment by @BruceET does a good job of explaining the logic you don't want to use here!


Using preventDefault in React.js

Question: The validation is working correctly, however when the user fails the validation the default action is still carried out. So validation fails and the joke is still returned. If validation fails, the Joke should not be returned. I have tried to use preventDefault but no luck.
 import React, { useState } from "react";

function Search() {

    const [joke, setJoke] = useState()
    const [firstname, setFN] = useState("sharoze")
    const [lastname, setLN] = useState("khan")

    const newJoke = (first, last) => {
        fetch(`http://api.icndb.com/jokes/random?firstName=${first}&lastName=${last}`)
            .then(result => result.json())
            .then(result2 => {
                console.log(result2)
                setJoke(result2.value.joke)

            })

    }

    function validateForm() {
        var firstname = document.getElementsByName("firstname")[0].value;
        var lastname = document.getElementsByName("lastname")[0].value;
        if (firstname === "" && lastname === "") {
            alert("Please enter atleast one name");

        }
        else if (!(/^[a-zA-Z]+$/.test(firstname + lastname))) {
            alert("'Only alphabets allowed'");

        }

    }

    return (

            <form name="searchForm" >
                <input type="text" name="firstname" placeholder="First name" value={firstname} onChange={(e) => setFN(e.target.value)} />
                <input type="text" name="lastname" placeholder="Last name" value={lastname} onChange={(e) => setLN(e.target.value)} />
            </form>
            <button id="button" onClick={(e) => { validateForm(newJoke(firstname, lastname)); return false; }}>click here for a personalised chuckle</button>

{joke}

    )
}

export default Search;

sorry the  code has been a little butchered!
Answer: The main problem I can see is that you don't pass event argument from onClick to your validation function, hence preventDefault() function is not working.
Try to pass event argument to validation function and then use this argument in function to apply preventDefault().
--Update--
You don't need to use preventDefault() at all. Just modify your validation function to call new joke only if validation is passed.
import React, { useState } from "react";

function Search() {

    const [joke, setJoke] = useState()
    const [firstname, setFN] = useState("sharoze")
    const [lastname, setLN] = useState("khan")

    const newJoke = (first, last) => {
        fetch(`http://api.icndb.com/jokes/random?firstName=${first}&lastName=${last}`)
            .then(result => result.json())
            .then(result2 => {
                console.log(result2)
                setJoke(result2.value.joke)

            })

    }

    function validateForm() {
        var firstname = document.getElementsByName("firstname")[0].value;
        var lastname = document.getElementsByName("lastname")[0].value;
        if (firstname === "" && lastname === "") {
            alert("Please enter atleast one name");
            return false;
        }
        else if (!(/^[a-zA-Z]+$/.test(firstname + lastname))) {
            alert("'Only alphabets allowed'");
            return false;
        }
        newJoke(firstname, lastname);
    }

    return (

            <form name="searchForm" >
                <input type="text" name="firstname" placeholder="First name" value={firstname} onChange={(e) => setFN(e.target.value)} />
                <input type="text" name="lastname" placeholder="Last name" value={lastname} onChange={(e) => setLN(e.target.value)} />
            </form>
            <button id="button" onClick={() => validateForm()}>click here for a personalised chuckle</button>

{joke}

    )
}

export default Search;

Comment: Hi, I have updated the code above to the full script, sorry about that. The search function is what returns the joke so do I need to add in preventDefault into that?
Comment: Hi. You have to add `e` argument to `validateForm` function in your Class component and also pass `e` as an argument when calling in your button `onClick.`
Comment: @SharozeKhan I've modified your code. Please check if works now.
Comment: Hi, Thank you for the help! unfortunately it is still not working, the joke still gets returned.
Comment: I am not sure if the prevent default needs to be in the search function as that is what returns the joke, but when I place it there I start to get errors. So I am not sure what the issue is as the validation is working correctly but the preventDefault seems to have no affect.
Comment: @SharozeKhan sure, then you don't need `preventDefault` at all. You should edit your `validateForm` function. If any of validation rule is not passed then `return false` in `if` block. If everything is ok - then call for a new joke or return joke - as you need.
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203408/discussion-between-serhii-zharkov-and-sharoze-khan).

Using preventDefault in React.js


Fetching alphanumeric values in a table in ascending order

Question: I have a table Food which contains column Quantity(it contains values- 10 kg,32 kg,5 kg). I want to fetch Quantity values in acsending order .
Now I have query:
select Quantity from Food

output=>
10 kg
32 kg
5 kg
But expected result is :
5 kg
10 kg
32 kg
How can I sort this alphanumeric value in SQL?
Comment: Does it contain weights in other units too, like 400gr or, God forbid, 20oz?
Comment: It contains units like gram,kg etc
Comment: If you have units like that, you need to re-calculate them to a single unit, for instance make grammes of all of them. You can do it in the query, but I think it's better to choose one unit and store it like that.
Answer: Try:
SELECT Quantity from Food ORDER BY CAST(Quantity as UNSIGNED) DESC;

This isn't ideal, you should store the quantity as an integer field and the unit as an enum.
Comment: or  **`ORDER BY Quantity+0`**  (implicit data conversion to numeric due to addition operation).
Answer: If the unit is only kg then you can try like below. See a demo fiddle here http://sqlfiddle.com/#!9/e644f/6
select Quantity from Food
order by replace(Quantity,'kg','') * 1;

Answer: Depending on the actual data you can grab the numeric part from the front of the value and than order by it:
SELECT Quantity
  FROM Food
 ORDER BY CAST(LEFT(Quantity,INSTR(Quantity,' ')-1) AS DECIMAL) DESC;

However as @Martin indicates MySQL appear to do that much for you. This is more explicit and there is, perhaps, value in that.


Show $\sum_{k=1}^n \frac{(n-1)!}{k!(n-k)!(10n)^{k-1}} \leq 1.06$

Question: I was reading Lecture 7 of Afternotes on Numerical Analysis by G. W. Stewart, and he mentions without proof the following result:
If $n \epsilon_M \leq 0.1$ and $\epsilon_i \leq \epsilon_M (i = 1,2,...,n)$, then
$(1+\epsilon_1)(1+\epsilon_2)\cdots(1+\epsilon_n) = 1 + \eta$
where
$\eta \leq 1.06n \epsilon_M$.
I wanted to figure out where this comes from, and, from pretty standard manipulations, I found that it suffices to show that
$\sum_{k=1}^n \frac{(n-1)!}{k!(n-k)!(10n)^{k-1}} \leq 1.06$. I graphed this on Desmos, and it does appear that this inequality is true. However, I have no idea how to go about showing this or where to even really start with this. Any insight would be appreciated!
Comment: You can evaluate that sum using the binomial theorem (consider the expansion of $10((1+1/(10n))^n - 1)$). This function is increasing and has a limit at infinity you can derive.
Comment: I have no clue whether my idea works out. I would try the following. You have by the MVT $$(1+\varepsilon_M)^n-1=(1+\zeta)^{n-1} n\varepsilon_M, $$ where $\zeta\in (0, \varepsilon_M)\subseteq (0, 0.1/n).$ Thus, you just have to estimate $(1+1/(10n))^{n-1}\leq 1.06$ (which I am not sure is true).
Answer: The answer above covers the computational part of the question more than adequately but it does not answer how to prove that a bound like that can be rigorously obtained.
Here's an outline of a proof using some basic calculus.

Lemma 1: The function $f(x)=(1+\frac{a}{x})^x$ is increasing for all $x>0$ and $\lim_{x\to\infty}(1+\frac{a}{x})^x=e^a$.
Lemma 2: (Obtaining a bound) $\frac{e^x-1}{x}\leq\frac{2+x}{2-x^2}~,~ x\in(0,\sqrt{2})$

Proof of Lemma 1: One can take a derivative of $f$ noting that
$$f'(x)=f(x)\left[\log\frac{x+a}{x}+\frac{x}{x+a}-1\right]$$
Now use the well known inequality $\log t\leq t-1$, which valid for any argument and plug in $t=\frac{x}{x+a}$. This readily establishes that
$$\log\left(\frac{x+a}{x}\right)\geq-\frac{x}{x+a}+1$$ which in turn shows that f is increasing since it's derivative is positive. The second part of the lemma can be proven in a variety of ways but I will skip the proof here. The monotonicity along with the limit at infinity show that for any $x,a>0$
$$f(x)\leq e^a$$
With Lemma 1 proven we establish that with $x=n, a=1/10$: $$10\left[\left(1+\frac{1}{10n}\right)^n-1\right]\leq \frac{e^{1/10}-1}{1/10}$$
Proof of Lemma 2: For any $n$, one can show using integration by parts and the fundamental theorem of calculus that
$$f(x)=f(0)+xf'(0)+...+f^{(n)}(0)\frac{x^n}{n!}+\frac{1}{n!}\int_{0}^x (x-t)^nf^{(n+1)}(t)dt$$
Select $n=2$ and $f(x)=e^x$. Then
$$e^x-1=x+\frac{x^2}{2}+\int_0^x \frac{(x-t)^2}{2}e^t dt\leq x+\frac{x^2}{2}+\frac{x^2}{2}\int_0^x e^t dt=x+\frac{x^2}{2}+\frac{x^2}{2}(e^x-1)$$
Now if we rearrange and restrict $x$ to be in the range $0<x<\sqrt{2}$ which guarantees that the denominator is positive we can conclude that
$$\frac{e^x-1}{x}\leq\frac{1+\frac{x}{2}}{1-\frac{x^2}{2}}$$
Finally, with $x=1/10$, one obtains a tight upper bound
$$\frac{e^{1/10}-1}{1/10}\leq \frac{210}{199}<1.06$$
and the proof is complete.
Answer: $\sum \limits_{k=1}^{n} \frac{(n-1)!}{k! (n-k)! (10 n)^{k-1}} = \frac{1}{n} \sum \limits_{k=1}^{n} \frac{(n)!}{ k! (n-k)! } (\frac{1}{10n})^{k-1} = 10\sum \limits_{k=1}^{n} \binom{n}{k}(\frac{1}{10n})^{k} = 10((1+\frac{1}{10n})^n-1) \to 10 (e^{\frac{1}{10}}-1) $ as $n \to \infty$ and $ 10 (e^{\frac{1}{10}}-1) \approx 1.05171$
Comment: @SeverinSchraven the limit tells us the inequality is true for all sufficiently large numbers, OP need to due the remaining part convincing himself its true for all numbers.
Comment: Do you know whether the inequality is also true without the limit? I think that is the main point here


Hook or event before a page gets cached

Question: Is there a hook or event I can tie into before a page gets cached. I need a hook/event that I can call so that I can run some logic on the page that is about to be cached to tell Drupal if I want a particular page to actually get cached. This is a very specific use case to the website I'm working on.
Answer: If you need to decide whenever a page should be cached from Drupal, you need to create a service that implements the \Drupal\Core\PageCache\ResponsePolicyInterface interface and it's tagged page_cache_response_policy, as described in Flexible page cache policy.
That page shows code that can be used as example. You can also use code that Drupal core actually implements, as example, like the code for the node.page_cache_response_policy.deny_node_preview service.
The code implementing the service is quite simple. It becomes more complex basing on the logic you need to apply to decide when a page should be cached.
class DenyNodePreview implements ResponsePolicyInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a deny node preview page cache policy.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function check(Response $response, Request $request) {
    if ($this->routeMatch
      ->getRouteName() === 'entity.node.preview') {
      return static::DENY;
    }
  }

}

Hook or event before a page gets cached


Discounted Value at Simple Interest

Question: Struggling with this question:
Mr. A borrows 2000 now and 3000 in 4 months. He agrees to pay X in
6 months and 2X in 8 months (from now). Determine X using a focal
date 8 months from now at simple interest rate r = 12%.
Thanks in advance.
Answer: Answer I get is 1,747.47
Essentially you want to find $X$ such that the discounted value of all cash received equals the discounted value of all cash paid. E.g., discounted value today of $100$ at $m$ months time from now at $12$% (p.a.) (simple interest) is $\frac{100}{1+(12\% \times m/12)}$. The rest is a bit of algebra.
Answer: We have the following cash flows
\begin{array}{|c|c|c|c|c|}
\hline
c_t &-2000 & -3000 & x & 2x\\
\hline
t & 0 & 4 & 6 & 8\\
\hline
\end{array}
so at $t=8$ months, putting $i=\frac{12\%}{12}=1\%$ we have
$$
-2000(1+i\times 8)-3000(1+i\times 4)+x(1+i\times 2)+2x=0
$$
that is
$$
-2160-3120+3.02x=0
$$
and then
$$
x=\frac{5280}{3.02}=1748.34
$$

Discounted Value at Simple Interest


Quando é que as threads (Thread Pool) são inicializadas? - C#

Question: Eu tenho o seguinte código:
public void mainFunc()   {
    for (int i = 0; i < 10; i++)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(funcThread), pto);
    }
    Thread.Sleep(2000) // Thread de teste.
    // Fazer algo quando todas as funcThread chegarem ao fim.
    // .....
    // Form4.doSomething()
}

Ou seja, eu quero que a funcThread seja criada 10 vezes e após todas as 10 funções chegarem ao fim, quero fazer uma rotina que chama outra Form. O que acontece é que primeiro ele executa tudo o que está na mainFunc e só depois ele faz o que está nas threads.
Eu já experimentei colocar um Thread.Sleep de 2 segundos, mas nem assim. Ele deixa passar os dois segundos, faz o que tem a fazer no form4 e só depois avança para as 10 threads...
Porque é que assim que faço o ThreadPool.QueueUserWorkItem(new WaitCallback(funcThread),pto) o programa não começa logo a executar as threads, mas só depois de terminar o mainFunc? Obrigado
Answer: Quando falamos em manipular Threads em C#, o recomendável é utilizar a Task Parallel Library (TPL), que é uma API mais alto nível para manipular o ThreadPool da aplicação. E nem sou eu quem digo isso: a própria Microsoft é quem recomenda porque eles perceberam o quão complicado era controlar algumas coisas através da biblioteca original:

In conclusion, I'll reiterate what the CLR team's ThreadPool developer has already stated:

"Task is now the preferred way to queue work to the thread pool."

De qualquer forma, ao usar um QueueUserWorkItem, você está sugerindo que quando sobrar alguma Thread, ele vai executar um determinado método. O problema é que dessa forma, não temos controle sobre quando isso ocorrerá.
Através da TPL, basicamente temos a classe Task que representa uma operação assíncrona que pode ou não ter sido feita ainda. Dessa forma, no exemplo abaixo, eu mando a TPL executar 10 vezes uma operação e guardo a referência das Tasks para controlar suas execuções:
// utilizei um clique de botão só para testar, tanto faz aqui
private async void button1_Click(object sender, EventArgs e)
{
    // só porque você definia ele como parâmetro do seu método
    int pto = 0;

    var taskList = new List<Task>();

    for (int i = 0; i < 10; i++)
    {
        // manda enfileirar no ThreadPool pra executar o seu método
        var task = Task.Run(() => { funcThread(pto); });

        // adiciona na lista a referência a cada Task criada
        taskList.Add(task);
    }

    // manda esperar que todas as Tasks referenciadas terminem para que o programa continue
    Task.WaitAll(taskList.ToArray());

    // espera uns 5 segundos só pra vermos o output confirmar o teste
    await Task.Delay(5000);

    // chama o seu form sem problemas
    Console.WriteLine(String.Format("{0} - Form4.doSomething()", DateTime.Now));
}

public void funcThread(int pto)
{
    Console.WriteLine(String.Format("{0} - funcThread", DateTime.Now));
}

A saída do meu exemplo:
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:48:55 - funcThread
09/02/2016 23:49:00 - Form4.doSomething()

Observação: só utilizei async/await no método para fazer a espera por 5 minutos, não é realmente necessário que seu método seja marcado como assíncrono para o Task.WaitAll() funcionar


div to touch left and right end of browser

Question: how to move the defie div left and the sales order div right......
i gave float property but not working.....
i wanted one div to touch extremely left and another div to move to the right....
providing my code below.....
http://jsfiddle.net/zbyLy/3/embedded/result/

47657 Lakeview Blvd, Fremont CA 94538

Tel: 510-657-8981 wwww.abcdfg.com

Sales Order

Customer No. ABC01

sales Order No. 100001

Est. Ship Date 2/24/2013

Answer:

demo :http://jsfiddle.net/zbyLy/


Find coordinates of polynom $Q$ with respect to basis $B$

Question:
Let $P_k(x)=\frac {(x-a)^k}{k!},\forall a\in\mathbb{R}$ and $k$ from $0$ to $n$. Find coordinates of an arbitrary polynom $Q\in \mathbb{R}_{\leq n}[X].$

$P_k(x)$ form the basis $B=\{1,x-a,\frac 1{2!}(x-a)^2\,...,\frac {(x-a)^n}{n!}\}$.
Let $Q(x) = a_0+a_1x+a_2x^2+...+a_nx^n.$
so then $Q(x)=\lambda_1\times1+\lambda_2\times(x-a)+...+\lambda_n\times\frac{(x-a)^n}{n!}\implies a_0+a_1x+a_2x^2+...+a_nx^n = \lambda_1\times1+\lambda_2\times(x-a)+...+\lambda_n\times\frac{(x-a)^n}{n!}.$
so we have
$$\lambda_1=a_0$$
$$\lambda_2=a1$$
$$...$$
$$\lambda_n=n!\times a_n$$
and $\lambda_1,....,\lambda_n$ are the coordinates of $Q$ with respect to $B$, have I done something wrong? something doesn't look right for me.
UPDATE: that's for $a=0$.
so I calculated using $Q((x-a)+a)$ and found out that:
$$\lambda_0=\sum_{k=1}^n a_k\times a^k$$
$$\lambda_1=\sum_{k=1}^n C_k^1\times a_k \times a^{k-1}$$
$$...$$
$$\lambda_n=\sum_{k=1}^n a_k.$$
is that correct?
Comment: $\lambda_1$ (which should be $\lambda_0$) isn't the only constant term if you expand the product (for instance, you get $\frac1{n!}a^n\lambda_n$ from the highest degree term).
Answer: From the equality$$\lambda_0+\lambda_1(x-a)+\lambda_2\frac{(x-a)^2}{2!}+\cdots+\lambda_n\frac{(x-a)^n}{n!}=a_0+a_1x+a_2x^2+\cdots+a_nx^n,$$you can deduce that $\lambda_0=a_0$, $\lambda_1=a_1$ and so on if $a=0$.  In the general case, write\begin{align}Q(x)&=Q\bigl((x-a)+a\bigr)\\&=a_0+a_1\bigl((x-a)+a\bigr)+a_2\bigl((x-a)+a\bigr)^2+\cdots+a_n\bigl((x-a)+a\bigr)^n,\end{align}and expand each binomial $\bigl((x-a)+a\bigr)^k$ in order to get the $\lambda_k$'s.
Comment: can you check if i did everything alright?
Comment: It looks fine, except that $C_k^1$ should be $C_1^k$ (or $\binom k1$).
Comment: and the sum changes, right? no longer from $1$ to $n$, but from ..?
Comment: $\lambda_k=\sum_{j=k}^n(\ldots)$.
Answer: You want to write
$$
Q(x)=c_0P_0(x)+c_1P_1(x)+\dots+c_nP_n(x)
$$
You can observe that $P_0'(x)=0$ and, for $k>0$, $P_k'(x)=P_{k-1}(x)$; then
\begin{align}
Q'(x)&=c_1P_0(x)+c_2P_1(x)+\dots+c_nP_{n-1}(x) \\
Q''(x)&=c_2P_0(x)+\dots+c_nP_{n-2}(x) \\
&\;\vdots\\
Q^{(n)}(x)&=c_nP_0(x)
\end{align}
so we immediately get that
$$
c_0=Q(a),\quad
c_1=Q'(a),\quad
c_2=Q''(a),\quad
\dots,\quad
c_n=Q^{(n)}(a)
$$
It's simply the Taylor expansion at $a$.


Joining tables on A or B or C

Question: i have a table:
docname, specialtycode1, specialtycode2, specialtycode3

i have another table
    specialtycode1, specialtycode1desc
specialtycode2, specialtrycode2desc
specialtycode3, specialtycode3desc

the result that i want is:
docname, specialtycode1, secialtycode1desc,
  specialtycode2, specialtycode2desc,
  specialtycode3, specialtycode3desc

i tried to do a query like this:
join dbo.SpecialtyMapping as f on (e.specialtycode1=f.specialtyabbrev
                                or e.specialtycode2=f.specialtyabbrev
                                or e.specialtycode3=f.specialtyabbrev)

i dont think this is the right solution but do not know where to start. please help!
here is the full query:
WITH cte(rejected) AS (SELECT DISTINCT [Specimen ID]
                                                 FROM         dbo.QuickLabDump
                                                 WHERE     (Outcome = 'REJECTED') AND ([Specimen ID] IS NOT NULL))
    SELECT     CONVERT(VARCHAR(8), a.[Date Entered], 1) AS [Full Date], DATEPART(yy, a.[Date Entered]) AS [Year Entered], LEFT(DATENAME(MONTH, a.[Date Entered]), 3)
                            AS [Month Entered], DATEPART(dd, a.[Date Entered]) AS [Day Entered], CASE WHEN DATEPART(WEEKDAY, [DATE entered])
                            = 1 THEN 'Sun' WHEN DATEPART(WEEKDAY, [DATE entered]) = 2 THEN 'Mon' WHEN DATEPART(WEEKDAY, [DATE entered])
                            = 3 THEN 'Tus' WHEN DATEPART(WEEKDAY, [DATE entered]) = 4 THEN 'Wed' WHEN DATEPART(WEEKDAY, [DATE entered])
                            = 5 THEN 'Thu' WHEN DATEPART(WEEKDAY, [DATE entered]) = 6 THEN 'Fri' WHEN DATEPART(WEEKDAY, [DATE entered]) = 7 THEN 'Sat' END AS DOW,
                            CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, a.[Date Entered])) + 6, a.[Date Entered]), 1) AS [Week Ending], COUNT(DISTINCT a.[Specimen ID])
                            AS CountAccns, c.SALESREP AS [Sales Rep], c.NPI AS MLNPI, e.NPIb AS IMSNPI, e.SpecialtyPrimaryCodeb AS [IMS Specialty Primary Code],
                            f.specialtydesc,
                            e.SpecialtySecondaryCodeb AS [IMS Specialty Secondary Code], e.SpecialtyTertiaryCodeb AS [IMS Specialty Tertiary Code],
                            e.ProfessionalID1b AS [IMS Professional ID 1], a.[Requesting Physician] AS Physician, a.[Practice Code], b.[MLIS Practice ID] AS [MLIS Code], a.[Practice Name],
                            c.DATEESTABLISHED AS [Date Established], c.PRACTICEADDRESS1 AS Address, c.PRACTICEADDRESS2 AS Address2, c.PRACTICECITY AS City,
                            c.PRACTICESTATE AS State, d.[MLIS Status] AS Status, MAX(a.[Order Count]) AS [order count]
     FROM         dbo.QuickLabDump AS a LEFT OUTER JOIN
                            dbo.qlmlismapping AS b ON b.[Quicklab ID] = a.[Practice Code] LEFT OUTER JOIN
                            dbo.PracticeandPhysician AS c ON a.[Requesting Physician] = c.DOCTORFIRSTNAME + ' ' + c.DOCTORLASTNAME AND
                            a.[Practice Code] = c.PRACTICECODE LEFT OUTER JOIN
                            dbo.IMSData AS e ON c.NPI = e.NPIb LEFT OUTER JOIN
                            dbo.QLMLISInfo AS d ON b.[MLIS Practice ID] = d.[MLIS Practice ID]
                            join dbo.SpecialtyMapping as f on (e.specialtyprimarycodeb=f.specialtyabbrev
                                or e.specialtysecondarycodeb=f.specialtyabbrev
                                or e.specialtytertiarycodeb=f.specialtyabbrev)
     WHERE     (a.[Date Entered] > '20110101') AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          cte AS cte_1
                                  WHERE      (rejected = a.[Specimen ID]))) AND (NOT EXISTS
                                (SELECT     1 AS Expr1
                                  FROM          dbo.PendingSpecimens
                                  WHERE      ([Specimen ID] = a.[Specimen ID])))
     GROUP BY a.[Date Entered], c.SALESREP, c.NPI, e.NPIb, e.SpecialtyPrimaryCodeb, e.SpecialtySecondaryCodeb, e.SpecialtyTertiaryCodeb, e.ProfessionalID1b,
                            a.[Requesting Physician], a.[Practice Code], b.[MLIS Practice ID], a.[Practice Name], c.DATEESTABLISHED, c.PRACTICEADDRESS1, c.PRACTICEADDRESS2,
                            c.PRACTICECITY, c.PRACTICESTATE, d.[MLIS Status],f.specialtydesc

Comment: Now you know why it is better to use proper normalization than to have 'specialtycode1', 'specialtycode2', 'specialtycode3' in a single table.
Comment: Did it produce an error?  And what is the full query (you haven't posted the SELECT, FROM or the other table)
Comment: @SirCrispalot no error but it has been going for like 20min and still no results
Comment: Could you include the names of the tables and your full query please?
Comment: How do you look at that code without getting vertigo?
Comment: Ugh and the meaningless aliases... `a,b,c,d,e,f...` alphabet soup.
Answer: If you have three "specialty codes" as columns in your first table, you will either need three JOINs, or you could unpivot the data from your docName table and do a single JOIN. I'll run with the solution that won't require too much modification of your original query:
SELECT
    ims.docname
    ,ims.specialtycode1, sm1.specialtycode1desc
    ,ims.specialtycode2, sm2.specialtycode2desc
    ,ims.specialtycode3, sm3.specialtycode3desc
FROM
    dbo.IMSData ims
LEFT JOIN
    dbo.SpecialtyMapping sm1 ON ims.specialtycode1 = sm1.specialtyabbrev
LEFT JOIN
    dbo.SpecialtyMapping sm2 ON ims.specialtycode2 = sm2.specialtyabbrev
LEFT JOIN
    dbo.SpecialtyMapping sm3 ON ims.specialtycode3 = sm3.specialtyabbrev

As long as your SpecialtyMapping table's rows are unique, this should work.
EDIT - Updated to LEFT JOINs as per suggestions in the comments. I also noticed that the column names don't quite exactly match the ones you have in your query, but you'll get the gist.
Comment: Probably what you need are left joins. That aside, I think this is the solution.
Comment: @I__: I have no idea if it will speed anything up; that's a pretty good-sized query and the problem with your performance could be somewhere else.
Comment: I was going to also suggest left joins since we don't know whether the three code columns in the main table are nullable.
Answer: Well there is a lot that could go into a long running query.
Where to start looking.

How much data? If you have masses and masses of data your query much always take long no matter how good your query. However most databases can take pretty huge amounts in their stride so lets assume that is not an issue.
Are you tables index on the columns you are joining? The database need to find the columns that you are joining on, if they are not indexed this process could take really long, index the columns you are joining on something like
create clustered index IX_Your_IndexName on Your_TableName (Column1, Column2, etc)
Learn how to read the execution plan, this will actually tell you why your query is running slow. Great place to start! Here is an article to get you started.

Good luck.

Joining tables on A or B or C


Image field: Force at least/exactly two images per field

Question: I can easily set the maximum number of elements per field, but is there an easy way to specify the minimum value?
For now, I have an Image field, and I want at least two images.
Answer: This question has already answer here
You can do it by using Field validation Module.
The following validators are listed:

Regular expression
Numeric values (optionally specify min and / or max value)
length (optionally specify min and / or max length)
number of words (optionally specify min and / or max words )
Plain text (disallow tags)
Must be empty (Anti-Spam: Hide with CSS)
Words blacklist
number of selections (optionally specify min and / or max selections
  )
Unique
Match against a field
Match against a property
Specific value(s)
Require at least one of several fields
Equal values on multiple fields
Unique values on multiple fields
PHP Code (powerfull but dangerous)
URL (support internal path and external url validation)
Email
Pattern (Regular expression lite)
Date range2

Hope this will help you


Guitar Pro 7.5 - add capo but keep notation

Question: When specifying a Capo is it standard to have the notation change to the actual pitch? My understanding is that the addition of the capo should not change the notation
When I add a capo in Guitar Pro 7.5 the notation changes. Here is an example:
Before Capo:

After Capo at Fret 1:
The E in the notation has become an F

How do you prevent this?
Answer: You can do this via File -> Stylesheet -> Systems and Staves and uncheck Capo and shifted tunings impact standard notation (thanks to this answer)
Answer: It has to change as the tab is the same: by adding a capo at fret 1 and if you do the same fingerings than before, effectively all your notes will be transposed a semi-tone higher.
If you want the standard notation to stay the same you could transpose that part in GP (here, a semi tone down for instance).
Comment: `you could transpose that part in GP` <-- care to share an example?


interaction term in Cox PH model

Question: I have a question with regard to interaction term in Cox PH model.
I'd like to analyze the impact of variable A on cardiovascular (CV) event.
Variable A levels are different according to sex, although the sex-specific cut-offs are not available (If cut-offs are available, I would have been able to categorize variable A according to sex-specific cut-off but it was not possible); therefore, I used interaction term 'variable A * sex'.
And 'sex' is another well known predictor for CV event.
'Variable A * sex' and 'sex' were both significant in the univariate analysis.
So I put these two variables, 'variable A * sex' and 'sex' in the multivariate model at the same time.
And the results showed HR and 95% CI for 'sex' was very high and wide.
I assume this is because I put 'sex' twice in the multivariate model, one is in its original form and one as an interaction term.
So in this case, would it be proper to exclude 'sex' in the multivariate model, although it is a well known risk factor for outcome?
Whether I exclude 'sex' or not, the results are same. Sex is not significant in terms of p-value in the multivariate model.
Thanks.
Comment: Note proper nomenclature: multivariate = multiple dependent variables; multivariable = multiple independent variables.
Comment: @FrankHarrell Thanks for your comment!
Answer:
... the results showed HR and 95% CI for 'sex' was very high and wide. I assume this is because I put 'sex' twice in the [multivariable] model, one is in its original form and one as an interaction term.

You don't specify which software you used, but R would know not to double-count a predictor in the way that you fear. What's more likely is that your variable A is more closely related to outcome than is sex per se. With variable A highly associated with sex, once you've taken variable A into account in your multivariable model it's hard for the model to pin down how much is left for sex to explain.
It's not good practice to remove "insignificant" predictors from models, for several reasons. First, your audience will presumably want to see that you accounted for sex if it is "known" to be associated with outcome. Second, removing any outcome-associated predictor in a Cox model (even if it's not "significant" itself) can bias the coefficients for the included predictors. Third, removing it will make predictions on future data less reliable.
Be glad that you didn't "categorize variable A"; that's not usually a good idea. If you had categorized it, there's a good chance that you might have missed how much of the association of sex with outcome might be due to the association of variable A with both sex and outcome. Your model might further benefit from flexible modeling of variable A, for example with restricted cubic splines, instead of what seems to be a simple linear association with log-hazard in your model.
Comment: Thank you very much. Doing subgroup analysis stratified by sex, as well as drawing restricted cubic spline curves, are actually my next step. I use both R and SPSS and in my question I used SPSS because it's more convenient for me. In case of SPSS, is it needed to remove 'sex' because I put it in the model as an interaction term? I'll try the same analysis with R but wanted to ask. Thanks again!
Comment: @doyle subgroup analysis is less powerful than a combined model that can draw simultaneously from all the data. If the combined model meets the proportional hazards assumption adequately, that really won't help. Cubic splines are a good idea. I don't use SPSS so can't tell you how it deals with multiply specified predictors. If the models are identical in both cases then it knew not to double-count the predictor.
Comment: Thanks very much!

interaction term in Cox PH model


Basic proof involving invariant subspace

Question: Can someone explain the importance of invariant subspace. I am looking at a proof for the existence of an orthonormal basis of eigenvectors of a nxn  symmetric matrix $A$.
It states that $A$ has some eigenvector $v_1, Av_1 = \lambda v_1$ Assume $\|v_1\| = 1$
Then to look at $v_1 ^{\perp}$.
It shows why $v_1 ^{\perp}$ is $A$ invariant.
Using induction since $v_1 ^{\perp}$ has dimension $n-1$, we have an orthonormal basis of V of eigenvectors: $v_2, v_3, \ldots, v_n$ for A in $v_1 ^{\perp}$. Therefore $v_1, v_2, v_3, \dots, v_n$ is an orthonormal basis for V and all basis vectors are eigenvectors for $A$. $\square$
What I don't understand is why we needed $v_1 ^{\perp}$ to be $A$ invariant. If someone can help out, that would be amazing
Comment: Because you need it to be invariant to be able to restrict the action of $A$ to an endomorphism of that space (i.e have a map $L_A\big|_{v_1^\bot}:v_1^\bot\hookrightarrow v_1^\bot$) , and thus apply the induction step. Else you couldn't do that :)
Comment: @b00nheT so are you saying that because $v_1^{\perp}$ is A invariant, A must be symmetric on  $v_1^{\perp}$?
Comment: $A$ is always symmetric, indipendently from the space it acts on as endomorphismus via left multiplication. Thus what you need is exactly this last space to let $A$ act on (of lower dimension), which you find by the above reasoning with the invariance of the orthogonal complement. Because there by the induction step, you CAN find such a basis.

So ask yourself: what does really state the induction argument?
Answer: The Spectral Theorem is given by induction over the dimension $n$ of the space $E$.
The following result is important:
Let $A: E \to  E$ be self-adjoint, if subspace $F \subset E$ that is A invariant, then its orthogonal complement $F^{\perp}$ is also A invariant.
The above result comes from the fact that $F^{\perp}$  invariant by the adjoint operator $A^* : E \to E$. In fact,
$$u \in F, v \in F^{\perp} \implies Au \in F \implies \langle u , A^*v \rangle = \langle Au , v \rangle = 0 \implies A^*v \in F^{\perp}$$
Thus $F^{\perp}$ is $A^*$ invariant  .
With this in mind, on the induction step we find a unitary eingenvector $u_n$ then its $A$ invariant subspace $F \subset E$ has dimension $1$. By the result above $F^{\perp}$ is also $A$ invariant and with dimension $n -1$. From here you may apply the induction hypothesis on $F^{\perp}$.
Comment: This is obviously correct, yet I would furthermore clarify the difference between the Matrix $A$ as stillstanding object, and its role as *linear selfadjoint operator* while considering its action al left multiplication on the base vector space. Considering that we are dealing with basic linear algebra
Comment: @b00nheT Do you mean to make clear that for each selfadjoint operator there exists a matrix $A$ symmetric?
Comment: (I am definitely not the downvoter)
anyway, no: I am not sure the user will fully understand you more general result due to the choice of different terminology, and the difficulty of the double role of a matrix as object and operator. But that can be left to him after all :)
Comment: I as well did not downvote. I found yours and @b00n het 's reply to be very helpful. Thanks alot
Comment: @lllll I'm glad I could help!


What's the meaning of this symbol?

Question: This is a probability
[|, |]
What does this symbol mean? (|)
Thanks in advance <3
Comment: Possibly a similar question to: [https://stats.stackexchange.com/questions/110194/what-do-vertical-bars-mean-in-statistical-distributions](https://stats.stackexchange.com/questions/110194/what-do-vertical-bars-mean-in-statistical-distributions)
Comment: Thank you so much!! <3
Comment: context? ${}{}{}$
Answer: the conditional  probability of $B,C$  given $M$. see here
Comment: Thank you so much, life saver! <3

What's the meaning of this symbol?


Derivation of Chi-Square Distribution using Dirac Delta Distribution

Question: I'm trying to understand a  derivation of the density of a Chi-Square distribution with one degree of freedom that my professor gave in class. Here are the steps:
\begin{align} p(y) &= \int_{-\infty}^{+\infty} p(x,y)dx\\\ &= \int_{-\infty}^{+\infty}p(y|x)p(x)dx\\ &=\int_{-\infty}^{+\infty}\delta(y-x^2)\frac{1}{\sqrt{2\pi}}e^{\frac{-x^2}{2}}dx
\end{align}
Now we use the substitution $u=x^2$ to get:
\begin{align} p(y) &= 2\int_{0}^{+\infty} \delta(y-u)\frac{1}{\sqrt{2\pi}}\frac{e^{\frac{-u}{2}}}{2\sqrt{u}}du\\ &= \frac{1}{\sqrt{2\pi}}\frac{e^{\frac{-y}{2}}}{\sqrt{y}}
\end{align}
The substitution puzzles me for a few reasons. Firstly, it removes integrating over the negative values, therefore how can we evaluate the dirac delta? Secondly, where do we get that extra factor of 2 from which fortuitously cancels the one in the Jacobian? I've been looking at this a while, help anyone?
Answer: Note $\delta(y-(-x)^2) \frac{1}{\sqrt{2 \pi}} e^{-(-x)^2/2} = \delta(y-x^2) \frac{1}{\sqrt{2 \pi}} e^{-x^2/2}$, so  $\int_{-\infty}^\infty \delta(y-x^2) \frac{1}{\sqrt{2 \pi}} e^{-x^2/2} dx$ can be written as $\int_0^\infty \delta(y-(-x)^2) \frac{1}{\sqrt{2 \pi}} e^{-(-x)^2/2} dx + \int_0^\infty \delta(y-x^2) \frac{1}{\sqrt{2 \pi}} e^{-x^2/2} dx$ where the first term corresponds to negative $x$ and the second to positive $x$ in the original integral. Thus, noting the equivalence mentioned, $\int_{-\infty}^\infty \delta(y-x^2) \frac{1}{\sqrt{2 \pi}} e^{-x^2/2} dx= 2 \int_0^\infty \delta(y-x^2) \frac{1}{\sqrt{2 \pi}} e^{-x^2/2} dx$. Then use the sifting property of the Dirac delta.


Browser back button: I always stay on the current page

Question: I am new to Angular and I have this code:
HTML template:
See categories
JS File:
$scope.goToCategories= function () {
                CategoriesService.getCategory().then(function () {
                    $state.go('home.cat');
                });
            };

JS File 2:
$stateProvider.state('home.cat', {
        parent: 'logged',
        url: '/cat',
        templateUrl: 'home/home-cat.html',
        ...
    });

The problem is when we are at home-cat.html, the browser back button doesn't work. I just stay in home-cat.html. How to correct this?
Thanks!
Comment: Can you add a plunker?
Comment: I don'k think that I can put a plunkr for this question... I don't know if I am wrong but, since my problem is related to the back button browser, I don't see how I could reproduce the problem in plunkr...
Answer: First is 'logged' a route that has a URL or is it an abstract route?
Second does the place where you are coming from with the link have a unique URL?
Third are you aware you told $state.go to go to a route that doesn't exist?
$state.go('home.cat');

should probably be
$state.go('home.funnel');

If you could setup a plunkr or jsfiddle that would also help :)
Note: you don't need
See categories

you can simply use
See categories

or
See categories

docs: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-sref
Comment: I can't comment because I just joined
Comment: Normally answers don't include multiple questions about the original question. They should've ben comments on the original question.
Comment: @KeniSteward: 'logged' is an abstract route, the place where I come from with the link has a unique URL, and it's home.cat not .funnel. I will edit my question... Thanks for the explanation.


Identifiying Biased and Unbiased Samples

Question: My little nephew asked me a question about biased/unbiased samples in which is teachers answer is something I disagree with to say the least (I don't agree with the assumption made by the teacher nor the lack of critical thinking within his answer).
The question is, "If Johnny took a handful of photographs and threw them up in the air, and selected 7 that landed on the rug- would this be a biased sampling?" The teachers answer is simply, "No, each photograph has an equal chance of landing on the rug therefore it is an unbiased sample."
I couldn't disagree more. The equal probability of each photographs landing on the rug has many dependent factors, how big is the rug compared to the room, where are you in the room when tossing the photos in the air, what is your current position relative to the rug, in what way are you tossing the photos into the air, and how were you holding them when do did it...
In fact I think we could continue to give factors that would be relevant, was the window open and a breeze coming through, in what direction etc, but in only a few case of the vastly many are there the situation where the probability of landing on the rug is equal for all photographs given that the size of the rug doesn't take up the entire room.
Since the question give very little to no information required to actually determine if the sampling would be biased or unbiased, can we not infer this is likely a biased sample since perfect circumstances would be needed in the room to guarantee each photograph having an equal chance of landing on the rug (because there are infinitely more ways the perfect circumstance could fail to exist if the rug isn't as big as the room)?
My nephew couldn't follow what I was trying to explain so I gave him this to think about.
If I stood in a big room with the rug across the room and toss the pictures in the air, there is zero probability that any will land on the rug.. I repeat this each time, inching closer to the rug, until eventually seven cards landed on the rug. Now just because the sample amount you wish to take has finally landed on the rug- is that enough to guarantee that all photographs tossed up in air during this iteration of the experiment had an equal probability of landing on the rug?
If given the situation where there is enough information loss so that determining the equal distribution of probabilities for an event to occur is impossible then isn't it reasonable to assume there there may exist some bias in the result?
Comment: If you can't predict what will fall on the rug and what won't, isn't that equivalent to a random sample? That is, there are non-random phenomena that can be modeled effectively as random. All models are wrong, but some are useful.
Comment: @soakley: I like to view a random sample as a guarantee that in an event each item had an equal probability of occurring. In your example non-random phenomena, pseudo random number generation, or other seemingly chaotic behavior (like weather patterns) random models shouldn't model these systems well. For instance Markov chains can accurately predict weather within a 5-7 day forecast, pseudo random number generators can be cracked, and the truth behind non-random phenomena can be described given enough empirical data but treating any of these as random models would mask their natural behavior.
Comment: I'm thinking of something much simpler. Toss a coin. In theory it's all physics and perhaps someday we will have a way to predict what the outcome will be. In the meantime I will make my guesses using pseudo-random numbers. It doesn't mask the natural behavior to think of the result as random. As Monty Python says, "it's only a model."
Comment: @soakley: In theory yes, the certainty of a coin toss is intractable given all the data, but if the coin is fair it is hardly random. The coin only acts random in large samples and a coin flip is now be usually not used as a randomizing technique. In fact for large samples a fair coin as three outcomes, heads, tails, and edge. Because the each outcome is not equally likely it isn't "truly" a random event in my opinion. For an event to be random, for no matter how many iterations occur, any outcome needs to be equally as likely. But for all practical purposes you're right.
Answer: You are right in saying that the teacher did not strictly define the experiment and thus it is hard to tell whether or not there is a bias. For example you could also ask was the selection process random. However I think the idea the teacher was trying to  get across was that if I have $n$ objects and randomly partition the set into $A$ and $B$ with the same probability for each object of being in either set. Then I take set $A$ and repeat the process into sets $X$ and $Y$, such that set $X$ has 7 objects and $Y$ has the remainder. Is set $X$ an unbias sample of the original set of objects. In which case the answer is yes. The analogy just wasn't ideal.
Comment: I understnad that the analogy isn't ideal but two things- "n objects and RANDOMLY partition the set into A and B ..." This is exactly the point, there exists no reason to believe that n entities were randomly partitioned into two sets where each entity had an equal probability of being in each set. Also, if n entities were partitioned into two sets, if at least one of the sets were built or constructed with a bias, even if I randomly choose entities from only one of the two sets my sampling will be biased. So I still completely disagree  with what the teacher was trying to get at.
Comment: @seeksUnderstanding I totally see where you are coming from. I am not entirely sure that the teacher is to blame though as it seems to be the way the education system is going - they are will sacrifice rigour for the sake of trying to build intuition. There are many ways of stating this problem but I think teachers are afraid that student won't be able to relate this to "real world" scenarios. I think it is ok to do as long as they put in the caveats that allow the students to make fully informed conclusions. In this case those caveats were missing.
Comment: I agree, looking at the other examples he had it was clear as day what was biased and not, this one simply was an edge case. I would have loved for him to be able to answer free for to state that it couldn't be determined and that he needed more information and why he needed it etc. Randomness is a little over middle schooler's heads I think, and even mine from time to time.
Comment: @seeksUnderstanding to be honest I think he will do fine - keep asking him to question and understand and he will do more than fine! If you are satisfied with our joint conclusion please accept the answer :)
Answer: I understand what you're trying to point out about the factors, but I have to say that I agree with teacher, the factors you mentioned do affect the experiment but without being directly biased to a certain photograph/s since the photos are randomaly held as it's not mentioned otherwise thus I think that it's an unbiased experiment.


Interpolate large irregular grid onto another irregular grid in Python

Question: I am trying to interpolate complex values from one irregular grid to another irregular grid using Python. The grids are in 2D and there are 103,113 data points. I am using Python 2.6.6, Scipy 0.7.2, Numpy 1.3.0, Matplotlib 0.99.3
In Matlab using griddata this is achieved in roughly 5 seconds.
BnGRID2  = griddata(R_GRID1,Z_GRID1,BnGRID1,R_GRID2,Z_GRID2) (MATLAB)

(Note all arrays are 201 x 513)
However, if I try using matplotlib.mlab.griddata I get a memoryError even if I try to work with the real part only:
mlab.griddata(R_GRID1.flatten(),Z_GRID1.flatten(),num.real(BnGRID1.flatten()),R_GRID2.flatten(),Z_GRID2.flatten())

If I try using interp2d I get a segmentation fault and Python exits:
a = interp.interp2d(R_GRID1,Z_GRID1,num.real(BnGRID1))

I have tried using KDTree and this seems to work ok, however, it takes a few minutes compared with the few seconds for Matlab, but I haven't explored this option too much yet.
Was wondering if anyone has any ideas how I can get this done as quickly as Matlab seems to? I noticed that the newer version of Scipy also has griddata, does anyone know if this can handle large irregular grids?
Answer: Scipy's griddata seems to be able to deal with data sets of this size without problems:

import numpy as np
import scipy.interpolate

# old grid
x, y = np.mgrid[0:1:201j, 0:1:513j]
z = np.sin(x*20) * (1j + np.cos(y*3))**2   # some data

# new grid
x2, y2 = np.mgrid[0.1:0.9:201j, 0.1:0.9:513j]

# interpolate onto the new grid
z2 = scipy.interpolate.griddata((x.ravel(), y.ravel()), z.ravel(), (x2, y2), method='cubic')

The griddata step takes about 5s on an old AMD Athlon.
If your data is on a grid (i.e., the coordinates corresponding to value z[i,j] are (x[i], y[j])), you can get more speed by using scipy.interpolate.RectBivariateSpline

z3 = (scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.real)(x2[:,0], y2[0,:])
 + 1j*scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.imag)(x2[:,0], y2[0,:]))

which takes 0.05s. It's much faster, because even if your grid spacings are irregular, a more efficient algorithm can be used as long as the grid is rectangular.


error when using cronTrigger with an expression that contains a year value

Question: I'm observing a strange behavior scheduling a job in Quartz using a CronTrigger that contains a year value.
Here is how I am creating a trigger and scheduling a job with it:
CronTrigger trigger =  cronJobTriggerFactory.getObject();
trigger.setName(triggerName);
trigger.setGroup(triggerGroupName);
trigger.setCronExpression(cronSchedule);
trigger.setVolatility(false);

JobDetail job = schedulableJobFactory.getObject();
job.setName(jobName);
job.setGroup(jobGroupName);
job.setVolatility(false);
job.setDurability(true);
Date scheduleTime1 = scheduler.scheduleJob(job, trigger);
logger.info(job.getKey() + " will run at: " + scheduleTime1);

and then in my unit test I determines the "now" date, add 5 minutes to it, calculate cron expression for this date/time and call my main class schedule a job with this schedule. Here is the out put of the unit test that shows which cron expression is passed :
NotificationSchedulerTest - Today is: 9 May 2012 05:32 PM
NotificationSchedulerTest - 5 min later is: 9 May 2012 05:37 PM
NotificationSchedulerTest - cron schedule is: 0 37 17 * 4 ? 2012

However, when trying to schedule a job with this cron expression - I'm getting the following error:
org.quartz.SchedulerException: Based on configured schedule, the given trigger will never fire.

As you can see, the date is in the future relative to the date/time I am running the test... So, it should not be a problem with trying to schedule a job to run at a time in the past.
Now the next strange thing: notice that I do specify the year value in my cron expression: " 0 37 17 * 4 ? 2012".
If I modify generation of the cron expression and leave the year field as unspecified (since it is optional): " 0 37 17 * 4 ?"
Then the scheduling DOES succeed, however, the scheduler shows that the next time the job will fire is in the year 2013! (one year later.... - and of course I cannot wait that long to verify it is fired...):
NotificationSchedulerTest - Today is: 9 May 2012 06:11 PM
NotificationSchedulerTest - 5 min later is: 9 May 2012 06:16 PM
NotificationSchedulerTest - cron schedule is: 0 16 18 * 4 ?
...
NotificationScheduler - myJobKey will run at: Mon Apr 01 18:16:00 EDT 2013

Am I missing something in these cron expressions?
Answer: Months in cron expressions are 1-based. That's why 0 37 17 * 4 ? 2012 is never executed: today is 10th of May and you want it to run on every day of April. When you remove the year it prints next scheduled date in 2013, but in April! myJobKey will run at: Mon Apr 01 18:16:00 EDT 2013.
Obviously your expression should be:
0 37 17 * 5 ? 2012

or to avoid confusion in the future:
0 37 17 * May ? 2012

Comment: Thanks! I knew it was something stupid... The confusion came from the way I am generating cron schedules - directly from Java Calendar instances: 'code'Calendar cal1 = Calendar.getInstance(); ... cronString.add(" " + String.valueOf(cal1.get(Calendar.MONTH));'code' And in the Calendar months start at 0 ! Now I have to add 1 to it for Quartz. Thanks a lot, Tomasz! :)
Comment: I did the same mistake. This was a quick find, thanks to this answer.
Comment: Hello @Tomasz, I have `0 48 15 10 1 ? 2020` of *ONCE* type which fire at *At 15:48:00pm, on the 10th day, in January, in 2020*. Can you please suggest what I am doing wrong here, as I am getting the same message


How to create footer links

Question: Does anyone know how to add link such as about us in the footer region. i am using the zen theme. Further, How do you redirect people to the login or register page from pages such as upload, thus if they are not logged in.
Answer: You place your menu through the admin/structure/blocks form. Relocate the menu block from the disabled category at the bottom of the form to wherever you wish (footers).  To get rid of the name, select configure for the menu block and enter  in the Title box.
Comment: Go to the block, and manage it (edit the content). Damn I just missed a fabulous prizes! ;-)
Comment: thanks again. Having done that how do i add links to the block? i've tried adding a link to my 'About Us' page through /admin/content but i can't find the block i've created.
Comment: This sounds like a new question, so go ahead and ask it.  People answer questions and gain reputation which entitles them to untold glory and fabulous prizes! :}
Answer: You can edit your footer in the "Manage block" section ("admin/structure/block").
Comment: thank you. i have tried but to no avail. i tried adding a new block but i couldn't add any links. i've also tried creating a new menu, which worked but i cannot remove the menu title. Any suggestions?
Comment: Go to the menus section and click on the menu you are trying to change click on list links and add link....
Answer: Footer Map block is a good way. Its just a block of text links associated with certain menus... Or you could create a field view of the type of nodes or entitys you need and just create text links for the titles.

How to create footer links