Nginx reverse proxy for two webapplications

Question: I have two applications.

Laravel Application - Being just a dynamic website. Does not have
data-knowledge. All the data gets fetched from the second application
using AJAX-requests. Runs on 127.0.0.1:8000.
Rust Application - A web-application containing all the business
logic and data-access. Runs on 127.0.0.1:8080.

Both applications need to be accessible from the URL example.com.
For this I want to use Nginx Reverse Proxy. I am already able to reroute all requests to my Laravel Application.
events {}
http {
    server {
        listen 80;

        # Website
        location / {
            proxy_pass http://127.0.0.1:8000;
        }

    }
}

While the Laravel Application works, I need actually need the following Nginx configuration:

Specific pages like /news, /, /about,/contact,... should be redirected to 127.0.0.1:8000.
Note: The pages above can contain GET-parameters like e.g /news?article=abc.
ALL other requests need to be redirected to 127.0.0.1:8080.
Note: 127.0.0.1:8080 can often contain a subdomain. But this subdomain needs to act like a wildcard and is unknown at the time of configuration. Example: bussiness1.example.com, bussiness2.example.com, businnesX.example.com,...

The subdomain returns a personalized page for which an unlimited amount of businesses can request for on example.com.
How would I achieve this configuration? I was thinking something like this?
#PSEUDO
events {}
http {
    server {
        listen 80;

        # Website
        location / {
            proxy_pass http://127.0.0.1:8000;
        }
        location /news {
            proxy_pass http://127.0.0.1:8000;
        }
        location /about {
            proxy_pass http://127.0.0.1:8000;
        }

        # Application
        location * {
            proxy_pass http://127.0.0.1:8080;
        }

    }
}

Edit regarding Tero's answer:
I need a kind of wildcard for subdomains.
 # Application catch-all
    location / {
        proxy_pass http://$subdomain.$application;
    }

abc.example.com needs to go to abc.127.0.0.1:8080
def.example.com needs to go to def.127.0.0.1:8080
...
I know an IP can't have a subdomain but don't worry about that. I solved that with virtual hosts.
Edit 2 - Pass subdomain of request to proxy_pass:
server {
        listen 80;
        server_name *.website.com;
        server_name ~^(?<subdomain>.+)\.website\.com$;

        location / {
            proxy_pass http://$subdomain.vhost.local:8080;
        }
    }

Is this the correct way to redirect dynamicxxx.website.com to dynamicxxx.vhost.local:8080?
Comment: i think you have to replace star with tilde
Answer: Your design is a bit complex and therefore fragile. However, you can try the following. I added upstream blocks so that the different proxy_pass destinations are more readable.
upstream website {
    server 127.0.0.1:8000;
}

upstream application {
    server 127.0.0.1:8080;
}

server {
    server_name example.com *.example.com;

    listen 80;

    # Application catch-all
    location / {
        proxy_pass http://application;
    }

    # Website
    location = / {
        proxy_pass http://website;
    }

    location /news {
        proxy_pass http://website;
    }

    location /about {
        proxy_pass http://website;
    }
}

The = character means an exact match on the URI, and is the first priority in nginx lookup process as described in nginx documentation
An alternative approach is to use regular expression:
server {
    server_name example.com *.example.com;

    listen 80;

    location / {
        proxy_pass http://application;
    }

    location ~ ^(?:/|/news|/about) {
        proxy_pass http://website;
    }
}

The server_name example.com *.example.com tells nginx to process all requests to main domain and any subdomain with these rules.
If the website request rules need to apply only to example.com, then you need to define another server block with server_name *.example.com and application rules only.
Edit
Yes, subdomain can be captured to a variable like that, and used in proxy_pass destination. You need to have all domains in single server_name line:
server_name example.com ~^(?<subdomain>[^.]+)\.example\.com;

Comment: Then you need to do a second `server` block for the subdomains with `server_name *.example.com`.
Comment: I added a `server_name` example.
Comment: Thanks a lot for your reply! This is getting close. However, the Catch-all for the application also needs to handle subdomains. Is a kind of wildcard possible? See post edit.
Comment: Thanks for all the help! Explained it a lot. If you could only look again at my second edit? Regarding the subdomain wildcard.
Comment: I'm really close mate. Only the subdomain wildcard remains.


Integrating with Metropolis-Hastings

Question: I am trying to implement a Metropolis routine to evaluate simple integrals. While it seem that the Markov chains I obtain reproduce the correct function, the area is simply wrong. Suppose I want to integrate the following (picture just below):
$$\int_{-5}^{1}2e^{4(-x^2)} + 0.5e^{-(x+3)^2}\text{d}x \approx 2.65$$

However, when I run my code I get a value of $\approx6.4$. I am not sure if this is due to my script or my choice of the prior interval (I assume it's $6$ from the uniform distribution within the bounds of integration).
This may be related to this issue, because I receive a similar incorrect value  as in that post if I also run it for a sine function from $0$ to $\pi$, although I think I keep track of the rejected samples. I'm just learning the method so would appreciate simple answers.
def integrand(x):
    if -5. <= x <= 1.:
        return 2*np.exp(4*(-x**2)) + 0.5*np.exp(-(x+3)**2)
    else:
        return 0

n = 10000   #number of iterations

#performs algorithm and returns array of the samples
def metropolis(f, iter=n):
    x = 0.   #initial x
    chain = np.zeros((iter, 1))

    for i in range(iter):
        x_new = np.array(x) + np.random.normal(size=1)
        if np.random.rand() < np.minimum(1, f(x_new) / f(x)):
            x = x_new
        chain[i] = x

    return chain

#run algorithm for the function
samples = metropolis(integrand, iter=n)

#evaluate area
area = 0
for i in range(len(samples)):
    area += function(samples[i])
area = 6*(1/len(samples))*area

print(area)

Answer:
The code as given does not run since function is not defined. And the value of the integral is not 2.65.

Furthermore, even if we assume that function=integrand, there is a fundamental difficulty with the approach, namely that, if
$$X_1,\ldots,X_n \sim f(x)$$
where $f$ is a density, then
$$\frac{1}{n}\sum_{i=1}^n f(X_i)$$does not converge to $$\int f(x)\text{d}x$$
but to$$\int f^2(x)\text{d}x$$
Furthermore,

in the current case the density $f$ is known up to a constant $f=\mathfrak c \tilde f$. This does not invalidate the Metropolis-Hastings simulation of
$$X_1,\ldots,X_n \sim f(x)$$but means that$$\frac{1}{n}\sum_{i=1}^n f(X_i)$$converges to $$\int \tilde f(x)f(x)\text{d}x$$
it is rarely possible to estimate the constant $\mathfrak c$ above from a simulation from $f$. Using importance sampling is a feasible alternative. For instance, using a Uniform sample on $(-5,1)$ works.
> 6*mean(2*exp(4*(-(u<-runif(1e6,-5,1))^2) + 0.5*exp(-(u+3)^2)))
 [1] 1.766225
> integrate(fun<-function(x)2*exp(4*(-x^2) + 0.5*exp(-(x+3)^2)),-5,1)
 1.768901 with absolute error < 6.7e-10

Integrating with Metropolis-Hastings


Prove that if an element $a$ of a group is of prime order $p$ then either $a^m=e$ or $a^m$ has order $p$

Question: So for a bit of context I am working by myself through Arnold's "Abel's Theorem in Problems and Solutions" and I have been stuck on this problem for almost a week. I realise that it's elementary and I understand the other problems in that section, but for some unfathomable reason I just can't get past this one. It's incredibly frustrating when you can't understand something this simple, I feel like a complete dunce.
Arnold's proof uses the result from the previous problem, namely that if an element $a$ of the group has order $n$ then $a^m=e$ if and only if $m=nd$ where $d$ is any integer (0). This is basic and self-evident and I have no problem understanding it. For "my" problem he argues as follows:

$(a^m)^p=(a^p)^m=e$ (1), hence (see (0)) the order of the element $a^m$ must divide the number $p$ (2). Since $p$ is prime the statement follows (3).

I understand (1) by itself and why (3) follows from (2). But for the life of me I can't figure out how (2) follows from (1) and (0). I feel like someone is keeping my eyes shut with their hands and teasing me. Can anyone reformulate this in some other way?
Comment: in $ a^m=e\; (0) $ replace $a$ with $a^m$ and $m$ with $p$
Comment: I think I more or less get it now, I've just been looking at this the wrong way. When you put (0) in terms of the "divides" relationship instead of $m=nd$ (which of course is the same) things suddenly click. Thank you!
Answer: It's confusing because $a$ and $m$ have two different roles.
Rewrite $(0)$ as $b^p=e\iff $ord$(b) | p$.
Given $(1):  (a^m)^p=e,$ take $b=a^m$ and therefore ord$(a^m)|p.$
Comment: "|" means "divides"
Comment: I understand what "|" means. Like I said already, this puts things in a different perspective that makes the whole thing suddenly click. Thank you very much!!
Comment: You're welcome.  Thanks for the prompt acceptance!
Answer: Hint: Recall that it suffices to prove that $\text{ord}(a^m)\text{ | }\text{ord}(a)$. What can you say about the subgroups generated by $a$ and $a^m$?
Comment: Thanks for the hint; out of curiosity, can we say that the subgroup generated by $a^m$ is a subgroup of the one generated by $a$, given that the former's order divides the latter's?


Find the energy of a partial differential equation

Question:
Consider the following equation
$$
y_{tt} - y_{xx} - y_{tx} - y_{txx} = 0 , \ \ (x,t) \in (0,1) \times (0,\infty)
$$
with $y(0,t) = y(1,t) = 0$, $t \in (0,\infty)$. I want to find the energy of system.

For this, I am trying to go on usual: Multiplying the equation for $ \overline{y}_{t} $ and integrating about $ (0.1) $, applying parts integration and and taking the real part
$$
\dfrac{d}{dt}\bigg(\dfrac{1}{2}\int_{0}^{1}|y_{t}|^{2}dx + \dfrac{1}{2}\int_{0}^{1}|y_{x}|^{2}dx\bigg) + \text{Re}\int_{0}^{1}y_{t}\overline{y}_{tx}dx + \dfrac{1}{2}\int_{0}^{1}|y_{tx}|^{2}dx= 0
$$
Then
$$
E(t) = \dfrac{1}{2}\int_{0}^{1}|y_{t}|^{2}dx + \dfrac{1}{2}\int_{0}^{1}|y_{x}|^{2}dx
$$
And
$$
E^{\prime}(t) = -\text{Re}\int_{0}^{1}y_{t}\overline{y}_{tx}dx - \dfrac{1}{2}\int_{0}^{1}|y_{tx}|^{2}dx
$$
Am I right? How can I rewrite the term
$$
\int_{0}^{1}y_{t}\overline{y}_{tx}dx ??
$$
I want to show $E^{\prime}(t) \leq 0$.
If the title of the question was not cool, I am sorry
Comment: I suppose it's not too important anyways, since if $y$ satisfies the equation then its real and imaginary parts satisfy it individually. So $y$ can be assumed real without loss of generality.
Comment: Can $y$ be assumed to be real?
Comment: I'm using : $\frac{d}{dt}\int_{0}^{1}|y_{t}|^{2}dx = 2\text{Re}\int_{0}^{1}y_{tt}\overline{y}_{t}dx$
Comment: @eyeballfrog $2\text{Re}\int_{0}^{1}y_{t}\overline{y}_{tx}dx =\frac{d}{dx}\int_{0}^{1}|y_{t}|^{2}dx$. But what to do with the term?
Comment: @eyeballfrog I don't know how to work with this term
Comment: @eyeballfrog I want to show $E^{\prime}(t) \leq 0$.
Comment: @eyeballfrog Why did you ask that?
Comment: @JessicaCaroline I missed the fact that the real, linear differential operator ensures you can on first read.
Answer: The differential operator in this problem is real and linear, so $\operatorname{Re}[y]$ and $\operatorname{Im}[y]$ will also satisfy the equation if $y$ does. That means it's sufficient to solve the problem for real functions, then combine the results at the end if $y$ actually is complex.
Multiplying through by $\partial_t y$ is the correct way to proceed. You get
$$
(\partial_t y)(\partial_{tt}y) - (\partial_t y)(\partial_{xx}y) - (\partial_t y)(\partial_{xt}y)-(\partial_t y)(\partial_{xxt}y) = 0
$$
Now we try to write it in terms of squares of $y$ and its derivatives, and exact $x$ derivatives. Some finagling gives
$$
\frac{\partial}{\partial t}\left[\frac{(\partial_t y)^2+(\partial_x y)^2}{2}\right]-\frac{\partial}{\partial x}\left[(\partial_t y)(\partial_x y)+\frac{(\partial_t y)^2}{2} + (\partial_t y)(\partial_{xt}y)\right]+\frac{(\partial_{xt} y)^2}{2} = 0.
$$
Because $\partial_t y = 0$ on the boundary, the exact $x$ derivative term will vanish when integrating over $[0,1]$, and we have
$$
\frac{d}{dt}\left[\int_{0}^1\frac{(\partial_t y)^2+(\partial_x y)^2}{2}dx\right]+\int_{0}^1\frac{(\partial_{xt} y)^2}{2}dx=0.
$$
Notice that in the second integral, the integrand is always nonnegative. Therefore the value of the integral itself is nonnegative. Moving it over to the other side gives
$$
E'(t) = \frac{d}{dt}\left[\int_{0}^1\frac{(\partial_t y)^2+(\partial_x y)^2}{2}dx\right]=-\int_{0}^1\frac{(\partial_{xt} y)^2}{2}dx\le 0
$$
If $y = y_r + i y_i$ is a complex function, we can add the energies of the real and imaginary parts together get
\begin{eqnarray}
E'(t) &=&  \frac{d}{dt}\left[\int_{0}^1\frac{(\partial_t y_r)^2 + (\partial_t y_i)^2+(\partial_x y_r)^2+(\partial_x y_i)^2}{2}dx\right] = \frac{d}{dt}\left[\int_{0}^1\frac{|\partial_t y|^2 +|\partial_x y|^2}{2}dx\right]
\\&=&-\int_{0}^1\frac{(\partial_{xt} y_r)^2+(\partial_{xt} y_i)^2}{2}dx=-\int_{0}^1\frac{|\partial_{xt} y_r|^2}{2}dx\le 0.
\end{eqnarray}
So the result still holds.
Comment: It's okay with you to justify this issue of the function being complex or real. But the way I did it taking the real part is cool too. What was missing from my explanation was that $2\text{Re}\int_{0}^{1}y_{t}\overline{y}_{tx}dx = \frac{d}{dx}\int_{0}^{1}|y_{t}|^{2}$
which is positive. Thanks for the answer.


Problem SAP Successfactors Non-Standard OCN Provider Integration (duration parameters)

Question: I'm working on an integration project connecting SAP Successfactors with a Non-Standard OCN Provider, I am using the API used by SuccessFactors
<code>...
"program":[
{
"startDate": 1451556622000,
"endDate": 1427796622000,
"active": true,
"duration": "12 weeks"
}
],
"revisionNumber": 1,
"duration": "18 minutes"
...
</code>
The web service reports that everything is correct. But I fail to visualize the duration parameters. Any ideas?
Thanks in advance
Answer: The process is correcta, the info is in:
Viewing Open Content Network Sessions.

Go to SAP SuccessFactors Learning and then go to Learning  Items.
Find and open the learning item that you want to check.
Go to Related  More  Content Network Sessions.

Ciao


Number Theory Modulo

Question: I need to prove that if $k\cdot a \equiv k\cdot b  \ \ (mod \ n)$ then $a \equiv b\left( \mod \ \frac{n}{gcd(k,n)} \right)$
I tried to do this:
$$
k(a-b)=s\cdot n
\\
k=u\cdot \gcd(k,n)   \wedge n=v\cdot \gcd(k,n) \ ; \gcd(u,v)=1
\\
u\cdot \gcd(k,n) \cdot (a-b)=s\cdot v \cdot \gcd(k,n)
\\
u\cdot (a-b)=s\cdot v
$$
and now I'm stuck,
If anyone has some idea on how can I continue from this step or is there another way to start, I will be grateful!
Answer: Now you have that $ua \equiv ub \pmod {v}.$ But $\gcd(u, v) = 1,$ and so $u^{-1}$ exists mod $v$. Multiply by it to get $a \equiv b \pmod{v}.$
Comment: How do I know $u^{-1}$ exists?
Comment: @JollyQ1 Because $\gcd(u, v) = 1$
Comment: yes but how do i prove it?
Comment: @JollyQ1 What number theory do you know? This is a standard but tricky lemma, usually proven with the Euclidean algorithm.
Comment: *Please* strive not to add more dupe answers to dupes of FAQs, cf. recent site policy announcement [here.](https://math.meta.stackexchange.com/q/33508/242)

Number Theory Modulo


Best practice setup for maintaining gems in production

Question: What is the recommended setup for a Rails application running in the production environment with regards to gem management? Bundler handles dependency resolution, installing, updating, etc. well, but still relies on the gem repository being up and running at any given time and any required gem not being pulled (removed) from the repository.
Is there any way to achieve more redundancy (a local mirror of the gem repository, maybe?) with production gem management, so everything can continue to function normally if RubyGems has downtime or a required gem is removed?
Comment: This issue is one of the reasons Bundler was started in the first place. To quote Yehuda Katz: "Above all else, the bundler should provide a reproducible installation of Ruby applications. New gem releases or down remote servers should not be able to impact the successful installation of an application." So `bundle package` will do what you want as mentioned in the answer below.
Answer: How about the command "bundle package": Package the .gem files required by your application into the vendor/cache directory.


Rspec, Scope Namespaces file and folder structure

Question: Right now I have one
#routes.rb
scope module: "api" do
  namespace :v1 do

and in my rspec folder structure I have
spec
 └── requests
     └── v1
         └── files

I want to create another namespace on a different scope
so I'm changing my routes.rb to
scope module: "api" do
  namespace :v1 do
   ...
scope module: "another-api" do
  namespace :v1 do

Would this be the right path here to keep my file structure organized?
spec
└── requests
    ├── api
    │   └── v1
    │       └── files
    └── other-api
        └── v1
            └── files

Answer: Looks about right. Keeping the route definition/source files/test files in the same hierarchical structure helps developers to quickly refer a file, helps the IDE to navigate between test & source file using keyboard shortcuts.


Integrate data from 2 different db

Question: How is the MySQL query for my case :
What I want to do is I had join two table table1_db1 and table2_db1. I would like to insert data from DB1 to DB2. How is the SQL Query for that?
I have 2 database.
Database 1 : DB1
Table name : table1_db1
Column name : name
Table name : table2_db1
Column name : reference
Database 2 : DB2
Table name : table1_db2
Column name : model
Column name : description
Answer: You can try something like:
INSERT INTO DB2.table1_db2 (model, description)
SELECT table1_db1.name AS model, table1_db2.reference AS description
FROM DB1.table1_db1
JOIN DB1.table1_db2 ON table1_db1.name = table1_db2.reference
;

Comment: can you refer to my latest edit? in db2 there are two column

Integrate data from 2 different db


Why is my gridline above x-axis and how can I correct it(matplotlib)?

Question: In the figure (see the link below the code), you can see that the bottom horizontal gridline is above the x-axis whereas I would prefer it to be overlapping the x-axis to make the graph look more accurate. Could anyone please tell me how to achieve that? Also, it would be amazing if someone could tell me how I can start my graph from 0 at the bottom left corner. Here is my code:
import matplotlib.pyplot as plt
import numpy as np
x_coordinates = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
y_coordinates = np.array([0, 5, 10, 15, 20, 25, 30, 35, 40,45 ])
plt.xlabel("extension/mm")
plt.ylabel("tension/ N")
plt.title("extention vs tension correlation")
plt.xticks([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
plt.minorticks_on()
plt.grid(b=True, which="minor", color="black" )
plt.grid(b=True, which ="major",color="black")
plt.plot(x_coordinates, y_coordinates)
plt.show()

Answer: It's plotting the minor ticks, and that looks confusing against the x-axis. If your plot range ends on a major tick, then it will look nicer. Here is one possible solution:
plt.ylim([min(y_coordinates),max(y_coordinates)])
plt.xlim([min(x_coordinates),max(x_coordinates)])

Comment: You are an absolute genius my friend. Thank you so much!


150 days in Beta - Is it picking up speed?

Question:
and together with the 60-days, 90-days and 120-days numbers
\begin{array}{| r | r |}
  \hline
\hline
  \text {Metric} & \text{60 B-days} & \text{90 B-days} & \text{120 B-days} & \text{150 B-days}\\
  \hline
\text {Q per day}  & 5.3 & 5.4 & 4.5 & 7.4\\
\text{% answered}  & \text {84%} & \text {80%} & \text {79%} & \text {80%}\\
\text{avid users | %} & 48 |\text {6.6%} & 54 | \text {5.5%} & 58 | \text {4.8%} & 63 | \text {4.3%}\\
\text {total users} & 730 & 981 & 1210 & 1455\\
\text {Engaged users %(*)} & 33\text{%} & 34\text{%} & 37\text{%} & 37\text{%}\\
\text {answer ratio}  & 1.8 & 1.8 & 1.7 & 1.7\\
\text {Visits/day (median)}  & 202 & 192 & 225 & 308\\
  \hline
\end{array}
(*)"Engaged Users" are those that do not have reputation $1$ or $101$, i.e. those that had at count day performed some reputation-affecting activity on the site (I count out also the $101$'s because the $100$ points are SE-registration bonus if they are active on other sites). I decided to show "engaged" rather than "unengaged" users, since all other metrics in the table have the association "larger value = better".
(P.S. The 30-days numbers can be found here)
COMMENTARY
For the first time in these 150 days the numbers gave me the impression that it is picking up speed.
Q's/day and visits per day have visibly increased while the rate of new registrations per day is unbelievably constant during all these 5-month period (7-9 new accounts per day, month per month). But this means that already registered users are becoming returning customers at a higher rate-which is critical.
Moreover the "% Answered" and the "Answers ratio" hold, despite the increased rate of new Questions -which also means that contributions keep up with the incoming flow.
In light of the above, the "Avid users" metric, although still the most serious threat in my opinion, worries me less now than 30 days earlier. But that's most probably psychology, a framing effect -and as economists we should know better than falling victims to framing effects.
So keep worrying -and keep up the pace.
Comment: I've been meaning to drop by and give this community a couple of thoughts from the SE community team's perspective. Your commitment to tracking your progress has in of itself been a cool exercise. In any case, keep an eye out for a longer answer. :)
Comment: Yes, these look like better numbers. In my eyes, the main challenge is still keeping a good balance of expert-level material as a broader audience comes onto the site.
Comment: @Ana Eyes wide open.
Comment: Only one red criterium and even a green word! Best we've been so far!
Answer: The SE Community Team, of which I'm a member, has been keeping an eye on things on Economics SE, so I thought sharing some observations from our side might be helpful.
tl;dr Economics.SE is coming along nicely.
The whole way along, I've been impressed by the ownership you've all taken of this site. Members of this community have consistently shown that they care enough to post here in meta, raise questions, and reflect on your collective progress. Any site needs an engaged core of users to have any longevity. Seems you've got that covered. As for some specifics...
Your rate of questions/day is strong.
7 questions/day for a relatively new community is very healthy. Nicely done! A disclaimer: don't worry too much about activity dropping off due to the seasonal nature of this topic. It's okay. It's cyclical, its natural; just keep doing what you're doing and trust that it'll all bounce back. Quality is always most important.
Economics.SE needs you to vote more.
Voting activity has fluctuated, and I encourage you to make it a consistent habit. Please vote (up and down) more consistently and readily. Rep is the simplest, most powerful feedback mechanism available on an SE site. Use it!
Band together to address unanswered questions?
You have a sizable number of unanswered questions. Unanswered questions can hinder growth because when new users show up via search engine and don't find answers, they don't tend to stick around. One idea for tackling unanswered questions might be organizing an initiative to address a few of them per week. Rallying everyone around one goal might prove to be helpful.
I believe Economics.SE is indeed picking up steam. Keep up the good work!
Answer: I was looking forward to this post for a few days now. I think however the key variable is the 300 visit, because that seems to be more stable. The questions/day is, iirc, a mean value over a few days, and could potentially change over the course of the next days.
Visits is key to keeping this up, and this time it seems not to be coming from a few super-popular layman questions, which speaks for its stability.
I'm generally positive, but one thing could get you worried: In a month or so, exams start, and then there's gonna be much less activity here from the undergraduates.
Comment: ...or much more, to solve last-minute scratch-heads in their study. Seasonality is to be expected, but indeed, what leaves doesn't necessarily come back, when the site is not yet "established". By the way, I've been meaning to applaud you for the questions you post in the main -they are exactly the sort of questions needed towards a "good balance of expert-level material" mentioned in @Ubiquitous answer.
Answer: Given that there's increasing growth in the number of users, I think that is a positive sign.
I think what this site is suffering from, is generally a lack of users.
You could question how much more the existing users can do (eg. answering more questions, browsing more frequently), but I think what this site really needs is more users.
Comment: Your answer seems to have been cut off abruptly. Should we expect more?
Comment: he suddenly dropped out of being an 'engaged user'
Comment: Totes can't remember what I was writing here.


speed up heatmap and error in aheatmap (NMF package) when providing a hclust object

Question: I try to speed up my heatmap creation and the main plan was to use hcluster function from amap package for parallelising the clustering.
I read in aheatmap documentation, I can provide a hclust-object and I read somewhere on stack overflow, the hierarchical clustering was the main bottle neck. So I wanted to calculate the clustering just once and provide it to my heatmap.
Now I get the following error:

Error in cluster_mat(mat, Rowv, distfun = distfun, hclustfun = hclustfun,  :
    aheatmap - Invalid clustering function: must be a character string or a function

pg_h <- matrix(rnorm(10000),ncol = 10)

d <- dist(pg_h)
h <- hclust(d)
aheatmap(pg_h,
         Colv=NA,
         scale='row',
         distfun=d,
         hclustfun=h)

Can anyone help me with this or is there a different way to create my heatmap. I have around 8000 rows and 15 columns and this takes more than an hour. I only want to cluster the rows.
Answer: At least for the error, I believe you have to pass the method obtained from your hclust class object (h) in this case, like so:
aheatmap(pg_h,
     Colv=NA,
     scale='row',
     distfun=d,
     hclustfun=h$method)

For the following:

You can see the method in the str of the object in question:
> str(h)
#List of 7
#$ merge      : int [1:999, 1:2] -778 -321 -191 -549 -133 -176 -94 -514 -653 -359 ...
#$ height     : num [1:999] 0.914 0.927 0.934 0.951 0.963 ...
#$ order      : int [1:1000] 74 910 12 864 979 849 218 361 478 974 ...
#$ labels     : NULL
#$ method     : chr "complete"
#$ call       : language hclust(d = d)
#$ dist.method: chr "euclidean"
#- attr(*, "class")= chr "hclust"

As for the first part of the question (speeding up, and if this is what you are referring to), passing the method via an hclust object does seem to produce the output faster. Here are  some benchmarks:
system.time(aheatmap(pg_h, Colv=NA, scale='row', distfun=d, hclustfun=h$method))
#   user  system elapsed
#   3.31    0.36    3.87
system.time(aheatmap(pg_h, Colv=NA, scale='row', distfun=d, hclustfun="complete"))
#   user  system elapsed
#   3.51    0.65    4.35

Note that the method for hclustfun ("complete") is the same one in both cases.


Probability in Keno

Question: I've been trying to figure this out for a while now. In keno, 20 different numbers between 1 and 80 are chosen. I then choose 5 numbers between 1 and 80. What is the chance that those 20 numbers contain the 5 numbers I chose? I tried to solve this by factoring it first: 5 in 80 is 1 in 16, 20 in 80 is 4 in 16. This means that there must be a 1 in 4 chance that one of my numbers will be in the 20 chosen. So for all 5, I just said 1/(4^5) which is 1/1024. But this is not right. How could I calculate the correct answer?
Answer: The number of ways to select five numbers out of twenty is $\binom {20}5$ and the number of ways to select five out of eighty is $\binom {80}5$, so your chance of winning is $\frac {\binom {20}5}{\binom {80}5}=\frac {20!}{15!}\cdot \frac {75!}{80!}=\frac {51}{79079}\approx 0.000645$

Probability in Keno


Interpreting Omitted Variables

Question: I do not fully understand how to interpret the difference between two statistical models where they only differ based on whether a certain variable is included on the right hand  side.
If the results do not change much, then do we say that that this omitted variable has little effect on the result?
If the coefficient on the variable of interest changes significantly in magnitude and is no longer significant, we say that the variable of interest on the right hand side is not robust to that omitted variable.  That omitted variable effects the relationship between the variable of interest on the right hand side and the dependent variable.
What is the estimate becomes more statistically significant when this variable is included?  It appears that including these variables makes the results more precisely estimated.  How can this be?  What does that mean about the relationship between the dependent variable and the main variable of interest on the right hand side if the relationship is more precisely estimated only with the additional control variable?
Comment: What you're asking about centers on the concept of partial correlation, which is dealt with, among other places, at http://stats.stackexchange.com/questions/17336/how-exactly-does-one-control-for-other-variables .
Answer: What you are asking is how to determine the direction of the omitted variable bias, or more generally, the interpretation of an omitted variable bias. There exists a couple of good online references that describe the nature of the omitted variable bias quite well. See, for instance the following site:
https://economictheoryblog.com/2018/05/04/omitted-variable-bias
In order to briefly summarize, the direction of the omitted variable bias depends on the correlation between the omitted variable with the dependent variable and the correlation between the omitted variable with the independent variables. Based on these correlations, one can determine the direction of the omitted variable bias.
Answer: I think this hinges on what you means by "not changes much". The estimated parameters could change and so could their standard errors. That's two separate effects. Let's focus on the variance part for now.
Suppose the true DGP is really $y=x\beta+z\alpha+v$, but you leave out the relevant variable $z$ from your model, so you actually estimate $y=x\beta+(z\alpha+v)=x\beta+\epsilon$ using OLS. Your error term is now effectively larger since it includes an extra term. The estimator of the variance-covariance matrix of $\beta$ will be biased upward, because the estimator of $\sigma^{2}$, the variance of the error term, will be biased upward. This causes the inference about $\beta$ to be inaccurate. This is the case even if $z$ is orthogonal to the other explanatory variables. When you add $z$ back in, the variance should go down.
Does that make sense?

Interpreting Omitted Variables


How to see what is returned when a remote script is blocked

Question: I'm using Google hosted jQuery in my webapp (//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js) As part of bug diagnostics I have a window.onerror handler which catches any errors I'm not catching locally and lets the server know about them.
So far so good, but... sometimes i get errors like these:

"Script error.","Error loading script","Unexpected token <"

My assumption is that the Google CDN is blocked in these cases (for whatever reason). I do have a local fallback for jQuery, that I'm fairly sure is working well, but I would like to find out what's being returned so that I can test my assumptions and maybe get some of these users on a white list for Google CDN (if it's company firewall blocking it).
But so far I haven't been able to figure out how to retrieve the returned content. Can't retrieve innerText of a SCRIPT tag if it's a file, can't do an ajax request because of cross-domain policy, etc.
Does anyone have any ideas about how this would be possible?
Answer: It simply isn't possible to get the content of any file referenced by a

If you could access the text of the respnse, you'd be able to do this:
var stolenAuthToken = $('#s').text();

That's obviously bad.  Therefore, you're never allowed to read the content of something brought in by

Unfortunately for you, in my testing, I found that the Google CDN does not send CORS headers.
GET http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js HTTP/1.1
Host: ajax.googleapis.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://fiddle.jshell.net/josh3736/jm2JU/show/
Origin: http://fiddle.jshell.net
Pragma: no-cache
Cache-Control: no-cache

HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/javascript; charset=UTF-8
Last-Modified: Tue, 13 Nov 2012 19:53:02 GMT
Date: Wed, 02 Jan 2013 22:54:25 GMT
Expires: Thu, 02 Jan 2014 22:54:25 GMT
X-Content-Type-Options: nosniff
Server: sffe
Content-Length: 93637
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=31536000
Age: 169036

...

Note the presence of the Origin header in the request (indicating a CORS request), and the absence of an Access-Control-Allow-Origin header in the response.  Thus, even if you put the crossorigin attribute, the CORS check will fail, and your scripts will receive scrubbed error details.
There is a three-year-old issue to enable CORS on the Google CDN server.  I wouldn't hold my breath.

tldr:  If you want meaningful error messages, you must host all JavaScript yourself, on the same origin.
Comment: kinda what I figured. I'll see if I can investigate directly with affected users. Thanks.


GEE vs GLMM in large sample size?

Question: I am running two longitudinal models for two different populations I'd like to compare. N1=4,000 individuals (translated into about 20,000 rows; 18 variables) and N2=400,000 individuals (~4 million rows; 15 variables). Since I am interested in the individual-level inference of my outcome, I have been using GLIMMIX with random intercept in SAS. This works very well for population 1, but not for population 2 (error message: data is too large). I also have tried to work in R, and my model still did not converge. A GEE model, however, runs smoothly for population 2.
It's been a very frustrating experience to work on this large dataset, so my questions are:
(1) Do you know if the results of GEE and GLMM would, perhaps, be similar/comparable in such huge dataset?
(2) Anyone else has worked with such large data using mixed effects model as well?
Anything worked?
Thanks.
Comment: GEE and GLMM answer different questions. I don't use SAS - is it running out of memory ?  I have run GLMMs on huge datasets - if it is too large to run locally then I fire up a large EC2 instance on AWS and run it there (using R)
Answer: A couple of points:

Both the GEEs and GLMMs are used for cluster/grouped data, and account via different ways for the correlations in the measurements within each cluster/group.
The GEEs is a semiparametric approach that does not make any assumptions for the distribution of your data; you only specify the first two moments (in classic GEEs, there are extensions in which you specify higher moments). On the contrary, with GLMMs you have a full specification of the distribution.
Because GEEs are semi-parametric you can only use Wald tests, but no likelihood ratio tests nor information criteria based on the likelihood (e.g., AIC or BIC). Moreover, because its semi-parametric nature, the standard GEE is only valid under the missing completely at random assumption, which seldom holds in practice for all the missing data you may have. The GLMMs, on the other hand, are valid under both missing completely at random and missing at random.
The GEEs provide you with marginal / population-averaged coefficients and inferences, whereas GLMMs with subject-specific ones. For more info on this point check here.
In your specific setting, you may want to consider working with a smaller random sample from the second cohort. Often you do not need all this high-volume data to extract useful conclusions.

GEE vs GLMM in large sample size?


How can I correct hooking my driver?

Question: As of yesterday, I had zeroed in on the driver I wanted for Father's Day; the Callaway Diablo Octane. My local store has an indoor driving range with computerized shot analysis. So, I picked the demo driver of that model off the rack, and hit a few shots. After getting my eye in, a pattern emerged where my shots were curving significantly left in what amateurs would call a "draw" but pros would call a "hook". Not a pull hook or snap hook, where it starts left and goes lefter causing people on the cart path to dive for cover, but a straight shot that feels good and starts off looking great, then curves off about 20-40 yards to the left and drops off shot distance (a very disappointing 180-210yd range; just a few days ago I'd hit a TaylorMade SF 2.0 about 250-260 and MUCH straighter, but that was a few days ago).
I unfortunately had my wife and daughter in the store, and the little one was getting irritated by the irregular sounds of titanium drivers pounding tee shots (including one tin can from the next bay over), so I didn't have the opportunity to try another driver. I just know that my shots were actually more consistent than they had been, they were just... left.
I like the idea of this driver; light, lot of face, I feel I'm making solid consistent contact, but the shots simply aren't going where I want. Before I give up on the driver, I want to make sure it's the driver and not me.
I tried:

Bringing my trailing hand thumb over more to the left in my grip (I was also experimenting with a Vardon overlap instead of my normal baseball). No change whatsoever.
Making sure the ball was teed up even with the inside of my left foot, and that I was bottoming my swing just behind the ball to swing "up" and "out" at the ball). No effect.
Opening the clubface at address. This helped, but they were still curving left even with what I thought was a pretty extremely open clubface, just not as much as when the face was square at address. By contrast, I have to close my irons (typically enough to bring the far leading edge even with the front of the hosel) to correct a natural push.
Making sure it wasn't a "max game improvement" or "draw" driver which is naturally set up with a few degrees offset inside to counter a natural slice tendency. It wasn't; doesn't mean some other hack hadn't mishit it so badly they'd knocked something off-kilter, but it wasn't factory-set to have any draw offset (any more than normal anyway).

I wanted to try (but didn't have the opportunity):

A different example of that exact driver. There was only one 10.5* reg-flex Octane driver in the rack, and I was swinging it, so I couldn't see if maybe someone else had damaged it in some non-obvious way.
The "Tour" version of the Octane. Tour drivers typically direct the shot more outward than a regular "game improvement" driver; depending on the model that's either because they have no offset or a slight outward offset.
A completely different model of driver. Like I said I'd been hitting the SuperFast 2.0 pretty straight, maybe a SLIGHT push draw (heads out slightly to the right and then comes back), and about 50 yards longer, so if switching back to that driver fixed the problem then I should be adjusting my wishlist accordingly.

I have heard since this episode that I should try:

Bringing the ball even further forward in my stance.
Backing away from the ball a smidge. Both of these will help correct an "inside-out" swing, which I've seen with my irons but has never been a problem with my woods (quite the opposite, actually).
Leading with my hands more. I kind of tried this on the day; the clubfitter told me I should swing "out" at the ball as well as "up", which I interpreted to mean bringing my follow-through out beyond the tee more, which in turn probably got my hands in front of the clubhead. However I wasn't concentrating specifically on that aspect of my mechanics at the time.

Obviously you guys haven't seen my swing, but given that if you tell me to "try everything" I'll probably end up changing too much and becoming more inconsistent, which of these things seem like the most likely cause of the problems I was seeing?
EDIT: Thanks for the comments. I went and dried a few different drivers, and while all of them drew/hooked to some degree, the TaylorMade Burner SF 2.0 looked more like a "draw" than a snap hook, and I was also consistently hitting it 270-290. So, I'm going to say that the behavior I was seeing was a combination of the driver and my swing, and the TaylorMade is now at the top of a very short list.
EDIT 2: As a post-script, I received a late Father's Day gift last weekend; a used TaylorMade Burner SuperFast. Not the 2.0, the previous generation. For the same total price that the SF 2.0 would have been new, my wife also got a 2008 TM Burner 3-wood thrown in, which is a rough match to the SF driver. I took both out to the course on Saturday, and while my score shall not be posted for public scorn, the driver and 3-wood were both excellent performers; my drives averaged around 250 yards, and my 3-wood around 210, which is quite good considering most drives faced a headwind and that I'm simply not a tour pro. The best thing was that most went dead straight; I only had to take one stroke-and-distance penalty for slicing a drive.
Comment: If your actual driver make you feel confortable, maybe it was a problem with the fitting??? I would try another fitting before changing anything... maybe you had a bad day ;)
Comment: Well this was less formal; I'd had a basic fitting which suggested the loft and shaft flex I should be looking for, which was consistent with what I'd been seeing at the range. This was a test of a particular driver that I'd been considering which met those basic criteria. As for the driver I own making me feel "comfortable", I wasn't using that driver, and if I am comfortable with that particular driver it means I've adapted some bad habits to control the club (it's a $30 club with a "junior" flex, and I have a 94mph average swing speed).
Comment: I would just try the fitting in a real driving, not in a simulator. Also would try it not only with the driver you have, also maybe another similar one. I have a burner Superfast with 10.5 and flex, but my fitting was on an actual driver (no computer involved :) ).  Remenber that the club helps, but sometimes is not the club, is how you feel with it.
Comment: Unfortunately trying out a variety of clubs on an actual driving range is not an option; such an outing would be expensive (at the price range I'm considering we could be talking about doubling the cost of the club I buy), and we've already determined that the results of a fitting can change from day to day. The fitting I got simply nailed down the "basic" parameters for a driver that would suit me.
Comment: I'll go back and try a few more drivers in the same session, and we can see if the results are consistent or differ between drivers. I do know that my swing has a lot of variables, club selection being just one of them, and I feel that just having a quality driver to practice with will help me to nail down the other particulars and build a consistent, correct swing. THEN we can talk about an advanced, in-person fitting to determine the nitpicky details about this club or that, this shaft or that.
Comment: So on [this image](http://thesandtrap.com/imgs/playing_tips/nine_ball_flights.jpg) your shot would be D? Sounds like your clubface is square but your club path is inside-out at contact, which probably means your weight is in front of the ball. (Note that these are all guesses, hence not an answer yet.)
Comment: Yes, that's generally what it looked like. Actually if the cause really is an inside-out swing it's because I'm overcorrecting a backswing that's too flat, which was causing a slice as I swung out then down; now I'm bringing the club too high and swinging down then out (if what you;'re saying is true). According to various other Interwebz guyz, I may also not be turning my body enough as I swing through, meaning my arms turn too soon and the clubface closes.
Answer: Based on the original question and the following comments, here are some suggestions:

As mentioned by gbianchi, try clubs at an actual golf range. Simulators can be poorly calibrated for one, but also users may feel pressured to swing harder because the distance is displayed for all observers to see. Search for free 'demo days' hosted at your local driving range or golf course.

http://taylormadegolf.com/on/demandware.store/Sites-TMaG-Site/default/Stores-FindFittingCentersDemoDays
http://www.callawaygolf.com/global/en-us/custom-fitting/demo-days.html
Don't forget to bring your current driver to test them side-by-side. You need a reference!

Ball flight "D" in the image from Michael Myers would typically result from a proper swing path (not inside out) and a closed face. First thing I would recommend is to check your stance and setup. Have someone take a picture of your setup from behind and in front and compare it to any pro. Check the direction your feet are aimed, your shoulders, your back, your arms, etc.

If that doesn't change anything or you want a quick fix, try weakening your grip by either changing to an overlap or interlocking grip like you tried (generally, from weakest to strongest: overlapping, interlocking, baseball grip). You can also try to roll your left hand out so that the velcro on your glove faces more toward your target and less upward.
Take these tips with a grain of salt. There's not much we can do without seeing your swing. Which leads me to my final suggestion...

Get a lesson. This should really be 1. Surely, you should probably upgrade from the 30 dollar kiddie club, but maybe buy a used driver, and use the money you saved for a set of lessons (and range balls to practice what you learn). In my experience, it is typically the swing and not the club that is the problem. Newer clubs might be more forgiving if you miss the sweet spot, or may go 5-10 yards further, but they won't fix your swing.

Good luck!
Comment: Great advice, all of it. I went back and found the rack of "pre-owned" clubs, and OMG there are some really good clubs available. I tested a used original Burner driver and matching 3-wood, both of which were in the $60 range (so I could have gotten both for less then the price of the SF 2.0). The original Burner head is much heavier than the new SF 2.0, though, which is slowing clubhead speed (I A/Bed em) and so I'm not getting quite as much distance as I'd been seeing with the SF.
Answer: Physically: My natural shot shape is a pop-up fade. When I hit a draw, I easily hit it 70-80 yards further than my natural shot shape. Can I do that by command? Not at all ;) At one time, I was regularly lifting weights. During this time, I was hitting a draw with every club in the bag, and I lost the ability to hit a fade.  When I stopped lifting regularly, my ability to hit a fade came back. I suspect that biomechanics have something to do with it.
Feel: This thread notes that "the results of a fitting can change from day to day." One day, I can be hitting slight draws, slight fades, straight or I can be hitting duck hooks, slices, and everywhere else in between. Some days, I tee the ball high. Some days, I tee the ball low. My golf game can change from day to day, and it's a matter of feel and working with what you got that day.
Mentally: I got to the point of overanalyzing and mechanisizing my game to the point where I wasn't enjoying golf anymore. Now, I just strategize my shot, step up, and fire away.
Clubs: Adjustable drivers such as a STR8-FIT or R11 driver answer the question of adjusting on-the-fly to accomodate for your game. Also, normal flex for a 94 mph swing speed sounds right. This is your choice, and it seems like you've made a clear-cut one.
In summary, you've done anything any enthusiast golfer would do (been there, done that).

if you tell me to "try everything" I'll probably end up changing too much and becoming more inconsistent

Exactly. This is why my suggestion is to go out there and just bomb a few at the driving range. If you hit a great shot, great. If not, great. Go out there and have fun!

How can I correct hooking my driver?


Finding the position of a person on a grid, when you know the $(x,y)$ coordinates of transmitters and the signal strength at the person

Question: I have a $100\times100$ grid. I have a transmitter on each corner, $4$ in total.
$$\begin{array}{rl}\text{Transmitter (a) is at}&(0,0);\\
\text{(b) is at}&(100,0);\\
\text{(c) is at}&(0,100);\\
\text{(d) is at}&(100,100).\end{array}$$
The user is able to measure the signal power from each transmitter.
I need to determine roughly where the user is on the grid knowing that the power/distance from the user to...
$$\begin{array}{rl}\text{transmitter (b) is}&53\\
\text{transmitter (c) is}&51\\
\text{transmitter (d) is}&72\end{array}$$
There is a margin for error on the transmitter power/distance as I translate itf from decibels into distance on the grid: there is about a $20\%$ margin for error.
It's not important, but I translate the signal power to roughly $1$ unit of distance for argument's sake.
What is the equation I need to apply to these values to get a rough $(x,y)$ coordinate for the user?
Trying to remember back to when I was in school, I vaguely remember getting the points where circles intersect and averaging them out or something like that.
Comment: Is this a hard question or have I not provided enough information in the question? Many Thanks, Code
Comment: It's not an easy question once you introduce the issue of margin of error.  It is possible to do (GPS systems may do something along these lines, though in 3 dimensions), but I don't have a reference offhand for just how to solve the problem with the margin of error.
Comment: If the transmitters are at the same signal level, the measured power at the user should be proportional to the inverse square of the distance (or maybe the inverse distance if the signal follows the ground, or maybe something in between).  The signal levels might be measured in dBm instead of V/m.  Either of these may corrupt your data-you need to understand what it is telling you.
Answer: One approach is to consider this as a probabilistic inverse problem. It may be overkill for purely theoretical purposes where there is a geometric formula for the exact solution, but this is how I would do it in a real-world situation where there is uncertainty. The following framework is probably how it's actually done for real GPS devices.
Probabilistic/Bayesian theory:
Define things as follows:

Call the unknown point location $p=(x,y)$,
the measurement locations $m_1,m_2,m_3$, ($m_1=(x_{m1},y_{m1})$, etc)
the measured distances $d=(d_1,d_2,d_3)$,
and the measurement noise $\eta = (\eta_1,\eta_2,\eta_3)$.

Then the situation can be encapsulated via the following equation:
$$d=g(p)+\eta,$$
where $g(p):=(||m_1-p||,||m_2-p||,||m_3-p||)$. Based on measurements of $d$, we want to estimate $p$. The continuous density version of Bayes' theorem tells us that
$$f_P(p|d)=\frac{f_D(d|p)f_P(p)}{f_D(d)}.$$
Let's break down what this means piece by piece:

$f_P(p|d)$ is the posterior probability density for your reciever to be at location $p$, taking into account that you measured the distances to be $d$.

Our goal is to find either the whole distribution $f_P(p|d)$, or maybe just certain properties of it like the expected value, maximum liklihood point, variance, etc.
The expected value and maximum liklihood point of the posterior $f_P(p|d)$ would be good "best guesses" for where the reciever is, and the variances would say how certain you are of that guess.

$f_D(d|p)$ is the likelihood that you would measure a set of distances $d$ if the true reciever point was $p$, taking into account the noise.

Considering a slight rearrangement of the model equation, $\eta=d-g(p)$, we can express the liklihood as
$$f_D(d|p)=\rho_{\eta}(d-g(p))$$
where $\rho_{\eta}$ is the probability distribution of the noise $\eta$.
The noise distribution $\rho_\eta$ has to be modeled based on your knowledge of the measurement device - more on this later.

$f_P(p)$ is the prior probability distribution on the reciever location - how likely you think it is to be in certain location before you even measure the distances.

For example, if any location is as likely as any other, $f_P(p)$ would be a uniform distribution on the box.

$f_D(d)$ is the prior probability distribution on the distance measurements $d$.

Since $f_D(d)$ doesn't depend on $p$, it's basically just a normalization constant.
Properties of $f_D(d)$ are important in theory to show that the bayesian framework is legitimate, but in practice you can just ignore it. It won't effect the mean, maximum liklihood point, variance, etc.

Now lets address the modeling issues mentioned above. It might be reasonable to say that the measurements are mean-zero normally distributed random variables with standard deviations $\sigma_1, \sigma_2,\sigma_3$ for the distance measurements $d_1, d_2$, and $d_3$ respectively, and the prior distribution is uniform on the box. Then the liklihood is,
$$f_D(d|p)=e^{-\sigma_1^{-2}(d_1-||m_1-p||)^2-\sigma_2^{-2}(d_2-||m_2-p||)^2-\sigma_3^{-2}(d_3-||m_3-p||)^2},$$
so the posterior is,
$$f_P(p|d)=N\begin{cases}e^{-\sigma_1^{-2}(d_1-||m_1-p||)^2-\sigma_2^{-2}(d_2-||m_2-p||)^2-\sigma_3^{-2}(d_3-||m_3-p||)^2}, & p \text{ is in the box} \\ 0 & p \text{ not in the box}\end{cases}$$
where $N$ is some fixed normalization constant.
Extracting information from the posterior:
Given the posterior distribution, there are a few things you might want to do.
Drawing samples:
First, you might want to draw samples from it. Markov-chain monte carlo is a good way to do this since it works without you knowing the normalization constant. MCMC also has advantages for high-dimensional problems, but that doesn't matter since this problem is small. However if you have thousands of measurement devices and thousands of points then such considerations will be important.
Best guess for reciever location:
Second, you might want to find the maximum liklihood point - in other words the point $p$ where $f_P(p|d)$ is maximized. To do that, solve the constrained convex optimization problem,
\begin{align}min_p & ~ \sigma_1^{-2}(d_1-||m_1-p||)^2+\sigma_2^{-2}(d_2-||m_2-p||)^2+\sigma_3^{-2}(d_3-||m_3-p||)^2 \\ \text{such that } & p \text{ is in the box}\end{align}
Uncertainty quantification:
Third, you might want to find the uncertainty. A quick and dirty way to do this is to draw a few, say 5, samples from the posterior and then take their variance. A more sophisticated way is to take the trace of the inverse of the hessian of the optization functional. It turns out that the inverse of the hessian is the covariance of a gaussian approximation to the posterior, though a proof of this is somewhat involved.
For an explanation on Bayesian inversion for a similar but easier example, you might read the excellent explanatory paper, "Estimating parameters in physical models through Bayesian inversion: a complete example" by Allmaras et. al.
Summary: The situation can be framed as a probabilistic inverse problem, where noise-corrupted measured distances are known and the recievers' position is unknown. Under this framework, Bayes' theorem provides a formula for the posterior probability density for the receiver position, based on (1) the prior probability densities, (2) the noise distribution, and (3) the measured data. If the prior is modeled as a uniform distribution and the noise is gaussian, then the posterior takes a particularly simple form. Samples from the posterior can be drawn with Markov chain monte carlo, and the posterior maximum liklihood point can be found by solving a computationally easy convex optimization problem.
Edit: I did a quick code up of the above maximum likelihood calculation in python using an off-the-shelf optimization routine from scipy, and it seems to work well.
Code: (I release this to the public domain if you want to use it. Doesn't use gradient information)
<code>import numpy as np
from scipy.optimize import minimize
from math import sqrt
from math import pow
import matplotlib.pyplot as plt

def recieverMAPpoint(d1,d2,d3):
    #coordinates of constraints box
    m1x=100.; m1y=0.
    m2x=0.; m2y=100.
    m3x=100.; m3y=100.
    #measurement undertainty=20%
    s1=.2*d1; s2=.2*d2; s3=.2*d3
    #optimization functional J
    def J(p):
        x=p[0]; y=p[1]
        r1=d1-sqrt(pow(m1x-x,2)+pow(m1y-y,2))
        r2=d2-sqrt(pow(m2x-x,2)+pow(m2y-y,2))
        r3=d3-sqrt(pow(m3x-x,2)+pow(m3y-y,2))
        return pow(r1/s1,2) + pow(r2/s2,2) + pow(r3/s3,2)
    #constraints box B
    B = ({'type': 'ineq',
      'fun' : lambda p: np.array([p[0], p[1]])},
     {'type': 'ineq',
      'fun' : lambda p: np.array([100-p[0], 100-p[1]])})
    #minimize J over B
    res = minimize(J,np.array([50,50]),constraints=B,options={'disp': True},method='SLSQP')
    #plot constraints box
    plt.plot([0,100,100,0,0],[0,0,100,100,0],color='black')
    #plot distance circles
    c1=plt.Circle((m1x,m1y),radius=d1,ec='blue',fc='none')
    c2=plt.Circle((m2x,m2y),radius=d2,ec='blue',fc='none')
    c3=plt.Circle((m3x,m3y),radius=d3,ec='blue',fc='none')
    plt.gca().add_patch(c1); plt.gca().add_patch(c2); plt.gca().add_patch(c3)
    #plot optimal point
    plt.plot(res.x[0],res.x[1],'ro')
    plt.axis([-20,120,-20,120])
    plt.show()
    return res.x
</code>
Test Cases:
Example from original post:
<code>>>> recieverMAPpoint(53,51,72)
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 6.47506375609
            Iterations: 9
            Function evaluations: 36
            Gradient evaluations: 9
array([ 48.3354663 ,  50.77404997])
</code>

Measurements error overestimates distance:
<code>>>> recieverMAPpoint(70,100,40)
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 0.241068422167
            Iterations: 12
            Function evaluations: 48
            Gradient evaluations: 12
</code>

Measurement error underestimates distance:
<code>>>> recieverMAPpoint(30,100,70)
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 0.472436673945
            Iterations: 13
            Function evaluations: 52
            Gradient evaluations: 13
array([ 85.47617804,  27.93369774])
</code>

Distance measurements exact:
<code>>>> recieverMAPpoint(80.6,67.1,92.2)
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 1.20656973089e-07
            Iterations: 11
            Function evaluations: 44
            Gradient evaluations: 11
array([ 30.012965  ,  39.98444119])
</code>

Optimal point on edge of box (constraint active):
<code>>>> recieverMAPpoint(60,120,130)
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 0.526683755534
            Iterations: 13
            Function evaluations: 52
            Gradient evaluations: 13
array([  3.96901773e+01,  -1.76150977e-13])
</code>
Comment: Nick thank you for the information. It is very much appreciated.


Como substituir o número de letras de uma palavra por um caractere?

Question: Estou fazendo um jogo da forca, vamos supor que eu digite o nome "joão" em uma caixa de text, logo depois aperto um botão e quero que na caixa de "texto 2" apareça "_ _ _ _", então quero que apareçam o mesmo numero de underlines que o numero de letras.
Answer: Existe um construtor pronto para fazer isto então é muito simples.
using System;

public class Program {
    public static void Main() {
        var texto = "carambola";
        var adivinha = new String('_', texto.Length);
        Console.WriteLine(adivinha);
    }
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Se fizer um laço adicionando strings é mais lento e faz um estrago no garbage collector. Pode não ser importante fazer em um joguinho muito simples, mas se aprende errado em algo simples vai reproduzir em algo que importe mais. Pelo menos poderia usar StringBuilder neste caso, mas também seria um exagero.
Answer: Você pode usar o string.Length para saber o tamanho da palavra que você possui.
Depois você pode Iterar em cima desse tamanho escrevendo "_"
Exemplor:
for(int i=0; i< string.Legth; i++)
{
  // aqui você pode criar um string igual ao seu exemplo
  novaString += "_";
}

Ou então com foreach
foreach(Char caracter in SuaString)
{
  // aqui você pode criar um string igual ao seu exemplo
  novaString += "_";
}

Ai você pode usar sua novaString para mostrar na tela o exemplo, existem outras formas de fazer, depende de como você vai mostrar esse texto.
Answer: Além do que já foi respondido anteriormente você pode usar expressão regular 'Regex' com a função replace, substituindo os caracteres.
string txt = "Primeiro Teste";
string forca=   Regex.Replace(txt, "[\\w]", "_");

O resultado é: ________ _____

Se preferir fazer similar a forca basta colocar um espaço após o carácter Undescore:
string txt = "Primeiro Teste";
string teste2 = Regex.Replace(txt, "[\\w]", "_ ");

O resultado é: _ _ _ _ _ _ _ _  _ _ _ _ _


Why does linear probing work with a relatively prime step?

Question: I was reading about linear probing in a hash table tutorial and came upon this:

The step size is almost always 1 with linear probing, but it is acceptable to use other step sizes as long as the step size is relatively prime to the table size so that every index is eventually visited. If this restriction isn't met, all of the indices may not be visited...

(The basic problem is: You need to visit every index in an array starting at an arbitrary index and skipping ahead a fixed number of indices [the skip] to the next index, wrapping to the beginning of the array if necessary with modulo.)
I understand why not all indices could be visited if the step size isn't relatively prime to the table size, but I don't understand why the converse is true: that all the indices will be visited if the step size is relatively prime to the array size.
I've observed this relatively prime property working in several examples that I've worked out by hand, but I don't understand why it works in every case.
In short, my question is: Why is every index of an array visited with a step that is relatively prime to the array size? Is there a proof of this?
Thanks!
Comment: Consider table size=4 and step size=2. You'd visit only the even-numbered (or only the odd numbered) slots.
Comment: Yes, that part he understood. But how about the reverse?
Comment: @wildplasser I understand that. See the paragraph "I understand why..." My question is why (for example) table size=4 and step size=3 visits every index. Why does it work in the general case?
Comment: http://en.wikipedia.org/wiki/Chinese_remainder_theorem will probably help.
Answer: Wikipedia about Cyclic Groups

The units of the ring Z/nZ are the numbers coprime to n.

Also:

[If two numbers are co-prime] There exist integers x and y such that ax + by = 1

So, if "a" is your step length, and "b" the length of the array, you can reach any index "z" by

axz + byz = z
=>
axz = z (mod b)

i.e stepping "xz" times (and wrapping over the array "yz" times).
Comment: Thanks for your answer, but I'm afraid that I don't follow how cyclic groups apply to array indices, or why the equation ax + by = 1 applies to array indices, or what x and y represent, or how you reached axz = z (mod b).
Answer: number of steps is lcm(A,P)/P or A/gcd(A,P) where A is array size and P is this magic coprime.
so if gcd(A,P) != 1 then number of steps will be less than A
On contrary if gcd(A,P) == 1 (coprimes) then number of steps will be A and  all indexes will be visited


C major chord in four-part, close harmony

Question: What tones would you use if told to strike the common chord of C in four-part, close harmony, using the root for the highest tone?
Answer:

A C chord is composed of the tones C, E, and G. The root is C. In a four-part chord, the bass is typically also the root. So, we have a C in the bass and a C in the soprano. A close position chord indicates that the alto should be the closest harmonizing note (beneath the soprano) and the tenor should be the next closest harmonizing note coming down.
Because this is a four part harmony, the bass doesn't have to be the next lowest harmonizing note. The interval between the tenor and the bass can be as large as we wish. In this case it is a tenth.
Comment: Yes, that is what a the close position is, but in a four part harmony, the bass is exempt from the closest harmonizing note rule. If there are three voices in closed position, they will always be within one octave.
Comment: Piston's _Harmony_ is in line with @AmericanLuke, in 4 parts only the SAT are close.
Comment: I would've thought the phrase "close harmony" implied [this meaning](https://en.wikipedia.org/w/index.php?title=Close_and_open_harmony&oldid=591362216), i.e. that the notes are within an octave of one another, rather than a "closed position chord" (which I think is what you're referring to). But I'll admit I'm not entirely sure.
Comment: @LaurencePayne it totally is. See https://music.utk.edu/theorycomp/courses/murphy/documents/PartWritingRules.pdf.
Comment: It's very strange to call C the fundamental, I think that term should be saved for the basis of an overtone series. Root is the better term for the C in a C-major chord.
Comment: @American Luke offers a perfectly good voicing for SATB.  But it isn't 'close harmony'.
Comment: Yes, the rule for SATB writing keeps the upper voices closer than the lower two.  But this question isn't about general principles of SATB.  It's about a 4-note chord in close position.
Answer: You would arrange the 4 notes as closely as possible.
C, E, G, C.   One of these in my picture. Or a further octave transposition to be pedantically complete, but the first one will do for a theory exam question!
Despite the 'Piano' tag and the word 'strike' in the question, some people have assumed this MUST be about SATB writing, and have described a typical SATB voicing that is not 'close position'.  (Though my first example is quite possible as SATB.)
Comment: Laurence, this is incorrect. It's a closed voicing, but not four part SATB. In SATB, only the SAT need to be closed the bass is not expected to be part of that closed voicing. See https://music.utk.edu/theorycomp/courses/murphy/documents/PartWritingRules.pdf. The phrase "strike" does not override the usage in 4 part harmony you can even see it in this other question: https://music.stackexchange.com/questions/37796/open-close-position-chords-what-i-am-missing/
Comment: Close.  Voicing the notes of the chord as closely together as possible.  No gaps. What ISN'T clear is the re-definition of 'close' in @American Luke's answer.
Comment: Why is everyone assuming this is about SATB?  It's tagged 'piano'!   I agree,  SATB doesn't usually DO close harmony.
Comment: Because the OP says "four-part ... harmony". Because of the [tag:piano] tag, the interpretation of the question is ambiguous.
Comment: "The term "four-part harmony" refers to music written for four voices, or for some other musical medium—four musical instruments or a single keyboard instrument" https://en.wikipedia.org/wiki/Four-part_harmony
Comment: SATB is the typical 4 part harmony and even if not writing for vocals the typical voice leading techniques are used. It's not a bad assumption and the fact it's called out should at least be mentioned. Focusing on the alternative interpretation with explanation rather than calling another answer wrong/misleading would be a better path.  I'll also note that Wikipedia itself has flagged that article as: "This article needs attention from an expert in Classical music. The specific problem is: A lot of information on this page seems completely incorrect. See examples on the talk page."
Comment: I agree, SATB doesn't typically do close harmony. But that's what this question is about!   Close voicing.  Not the open voicing more typical of SATB writing.  I've simplified my answer to make this completely clear.
Comment: "Despite...": Indeed, _close harmony_ is usually used to describe harmony sung by all-male or all-female groups rather than mixed groups.


GLM Poisson thinks I have negative values in my dataset, throws error

Question: I am trying to do a poisson GLM, and yet I continue to get this error
<code>Poisson1 <- glm(Number.Flowers ~ Site, data = Flowering2, family="poisson")

Error in eval(family$initialize) :negative values not allowed for the 'Poisson' family
</code>
My data is count data and so is all positive values and zeros. What could be going on?
Is it possible for my CSV file to contain hidden negative values?
Comment: If you provide you CSV file it would be tremendously easier to determine if it contains any hidden negative values :)
Answer: It's possible your CSV might be flawed in some way. Try a different method of importing it into R (fread, read.table, etc). Check for NA or NaN issues. Compare the number of rows.


Java 8 Stream intermediate operation - element processing

Question: In Java 8 Stream API, description of an intermediate operation like filter, map and peek is mentioned as

...elements of this stream...

But, stream intermediate operations like filter seems to process and return an individual 'element' at a time. For example, Java 8 stream operations execution order question(see output) and answers.
The API descripton of filter is

Returns a stream consisting of the elements of this stream that match the given predicate.

But, the code mentioned in Java 8 stream operations execution order question seems to process and return an individual element at a time. Please clarify the disconnect between API's mention of 'elements' and apparent processing of single 'element' of the code.
Thanks.
Comment: As mentioned in comment for an answer - I understand that return type is `Stream` but, why does API mentions - "elements of this stream" rather than "element of this stream". IOW, why does API use plural for element?
Comment: because a stream represents a sequence of elements and in the majority of the cases there are multiple elements that pass through a given intermediate operation.
Answer: Yes, stateless intermediate operations such as filter, map etc. process a given element at a time and the former either does or does not send it on to the next stage where the next stage is the subsequent intermediate operation in the stream pipeline. the latter will always send the element to the subsequent operation after the mapping function is invoked.
for example:
...
.filter(n -> n % 2 == 0) // retain even numbers
.map(n -> n * n) // square it
...

one element at a time will pass through the filter operation and if the current element meets the provided predicate it is then passed to the map operation.
I think you understand this part, but rather your confusion is why in the java doc was it written:

Returns a stream consisting of the elements of this stream that match
  the given predicate.

"Returns a stream" because each intermediate operation returns a new stream.
"consisting of the elements" because a stream represents a sequence of elements and in the majority of the cases there are multiple elements that pass through a given intermediate operation.
this might sound like a stream stores its elements but this is not the case at all. Rather they may be stored in some underlying collection or generated on demand.

It's worth noting that not every intermediate operation will pass one element at a time to the next stage. for example the sorted intermediate operation buffers all the elements until it sees the end of the input, and then it sends the data to the subsequent stage.

As a reading, there is a brilliant post by Brian Goetz regarding Streams under the hood. This really goes into details about streams and there is a good animation courtesy of Tagir Valeev that show how to visualize a stream.
Comment: This answer has better explanation and insight compared to my selected answer.
Answer: I think there is some misunderstanding here because filter always return stream.
See signature for the same as below:
 Stream<T> filter(Predicate<? super T> predicate);

So, any such intermediate operation returns the stream instead of one element at a time and you can execute all operations on them as you can execute on streams.
Same is the case with specification:

Returns a stream consisting of the elements of this stream that match
  the given predicate.
Comment: I understand that return type is `Stream` but, why does API mentions - "elements of this stream" rather than "element of this stream". IOW, why does API use plural for element?
Comment: As per the specs, Returns a stream consisting of the elements of this stream that match the given predicate. This means that this includes multiple elements from the original stream based on the predicate and hence plural.
Comment: Yes, as you mentioned, there was a misunderstanding. Your answer and comment makes sense as I allow things to sink. Upvoted both.
Answer: The map and filter intermediate operations are indeed executed on every single element of the stream, but they return a stream for themselves.
So, when we are looking at the code from the upper question, the filter operation will be executed with each number 1-8, but it will return a new stream which observes only the even numbers {2,4,6,8}.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List<Integer> twoEvenSquares = numbers.stream().filter(n -> {
    System.out.println("filtering " + n);
    return n % 2 == 0;
}).map(n -> {
    System.out.println("mapping " + n);
    return n * n;
}).limit(2).collect(Collectors.toList());

There is no disconnect because like the Stream 8 Stream API says, the intermediate operators like filter(Function<?, Boolean> filteringFunction) return a Stream. They do this by executing the given filteringFunction on every element of the in-stream and return a new out-stream which only observes the elements that have been filtered to true.
Comment: one point to note though. there are no elements _in_ a stream. a stream is simply an object which connects to a source and sees the elements _flow_.
Comment: @Aominè good comment, even `Stream#of` is based on `SpinedBuffer`
Comment: Thank you for clarifying this, I am going to edit my answer.


Pegar valor de ID e colocar dentro do campo hidden

Question: Colegas.
Estou pegando através do Javascript o valor de um resultado e colocando dentro de um ID:
document.getElementById("ValorUnitarioTriplo").innerHTML = " USD "+triplo.toFixed(2)+"";
...............

USD 0.00

Legal. Funciona. Porém gostaria de que esse valor ficasse dentro do value de um input hidden. É possível fazer isso? Dessa forma mando para o banco de dados usando o PHP e Mysql.
Answer: É só fazeres da mesma maneira, usando o ID de um input que cries, e usando .value em vez de .innerHTML:
function gravar(id, val){
    var valor = " USD " + val.toFixed(2);
    document.getElementById(id).innerHTML = valor;
    document.getElementById('input_' + id).value = valor;
}

E tendo o HTML

USD 0.00

<input type="hidden" name="vut" id="input_ValorUnitarioTriplo" />

Podes usar no JS assim:
gravar('ValorUnitarioTriplo', 456.7879);

Não te esqueças de dar atributo name ao input  para o servidor ir buscar o valor.
Comment: Faltaram as aspas, eu receio: `gravar("ValorUnitarioTriplo"`
Comment: @DontVoteMeDown exato, obrigado!
Answer: Evidente! As pessoas fazem isso o tempo todo:
HTML:
<input type="hidden" name="x" id="hidden-field">

Javascript
document.getElementById("hidden-field").value = "seu valor";

Javascript/jQuery
$("#hidden-field").val("seu valor");

Answer: É basicamente da forma que você já está fazendo.
Digamos que você já tem o input, basta buscar pelo id e setar o valor. Ficaria assim:
<input type="hidden" name="txtValor" id="txtValor" value="">
--
document.getElementById("txtValor").value = "1";

document.getElementById("txtValor").value = "1";
alert('Valor do input:' + document.getElementById("txtValor").value);
<input type="hidden" name="txtValor" id="txtValor" value="">
Comment: @Jose.Marcos Ele não aparece o `value` no html. Mas se você inspecionar o elemento (F12), ele estará lá.
Comment: Obrigado a todos. Só mais uma pergunta. Como eu faria para pegar esse valor que pegamos do javascript e colocasse dentro de uma variável em PHP. Ex.: $valor = filter_input(INPUT_POST,'nome_do_campo_value');
Comment: @Jose.Marcos Se você observar o `input` possui um **name** (txtValor). Utilize ele em seu post para pegar o valor. Algo tipo assim: `$valor = $_POST["txtValor"];`
Comment: Desculpe Randrade. Acho que não fui claro na minha dúvida. No seu código o value aparece vazio. . Foi aí que gerou minha dúvida.
Comment: Entendi. Perfeito. Obrigado Randrade.
Answer: Estive com o mesmo problema e fiz conforme o código abaixo
na controller  fiz um método  pra retorna uma lista da busca em Json
 public JsonResult BuscaNomePaciente(string text)
      {
       var list = PRep.Lista().Where(x => x.Nome.Contains(text)).Select(c =>
        new { ID = c.ID, Nome = c.Nome });
           return Json(list, JsonRequestBehavior.AllowGet);
       }

No View Create foi implementado este trecho de codigo a baixo:
//Referência das bibliotecas Jquery, Jequery ui
<link href="~/Content/themes/jquery-ui.min.css" rel="stylesheet" />

    @Html.LabelFor(model => model.Id_Paciente, htmlAttributes: new { @class
 = "control-label" })
    <input type="text" id="busca" class="form-control" placeholder="Busca
Paciente" />
    <input type="text" id="hidden-Nome" name="Id_Paciente" />


app crashing on clicking fragment

Question: I have created a navigation drawer whose last option opens a fragment that sets the wallpaper of the device.However my app keeps crashing whenever i select that fragment.
Here is my code
MainActivity.java
public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));

        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item

            displayView(position);
            switch(position) {
            case 0:
                Intent a = new Intent(getApplicationContext(), Test.class);
                startActivity(a);
                break;
            case 1:
                Intent b = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(b);
                break;
            default:
        }

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();

            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new S();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

The app crashes when I click on this fragment
Fragment S.java
public class S extends Fragment implements OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // to our activity to cover the whole screen

        View rootView = inflater.inflate(R.layout.back, container, false);
        ImageView display;
        int tophone;

            Button but = (Button) getView().findViewById(R.id.setwallpaper);
            tophone = R.drawable.image4;
            but.setOnClickListener(this);
            display = (ImageView) getView().findViewById(R.id.ivdisplay);
            ImageView image1 = (ImageView) getView().findViewById(R.id.IVimage1);
            ImageView image2 = (ImageView) getView().findViewById(R.id.IVimage2);
            ImageView image3 = (ImageView) getView().findViewById(R.id.IVimage3);
            ImageView image4 = (ImageView) getView().findViewById(R.id.IVimage4);
            ImageView image5 = (ImageView) getView().findViewById(R.id.IVimage5);

            image1.setOnClickListener(this);
            image2.setOnClickListener(this);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
            image5.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

Here is my logcat
11-18 21:19:05.112: E/AndroidRuntime(622): FATAL EXCEPTION: main
11-18 21:19:05.112: E/AndroidRuntime(622): java.lang.NullPointerException
11-18 21:19:05.112: E/AndroidRuntime(622):  at info.androidhive.slidingmenu.S.onCreateView(S.java:31)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.os.Handler.handleCallback(Handler.java:615)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.os.Looper.loop(Looper.java:137)
11-18 21:19:05.112: E/AndroidRuntime(622):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-18 21:19:05.112: E/AndroidRuntime(622):  at java.lang.reflect.Method.invokeNative(Native Method)
11-18 21:19:05.112: E/AndroidRuntime(622):  at java.lang.reflect.Method.invoke(Method.java:511)
11-18 21:19:05.112: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-18 21:19:05.112: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-18 21:19:05.112: E/AndroidRuntime(622):  at dalvik.system.NativeStart.main(Native Method)
11-18 21:22:52.642: I/Process(622): Sending signal. PID: 622 SIG: 9

Answer: In the fragment, getView() is null during onCreateView().
You have to find your views on rootView :
public class S extends Fragment implements OnClickListener {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.back, container, false);
    Button but = (Button) rootView.findViewById(R.id.setwallpaper);
    int tophone = R.drawable.image4;
    but.setOnClickListener(this);
    ImageView display = (ImageView) rootView.findViewById(R.id.ivdisplay);
    ImageView image1 = (ImageView) rootView.findViewById(R.id.IVimage1);
    // ... for all images

    image1.setOnClickListener(this);
    // ... for all images
    return rootView;
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
  }
}

Comment: Error rootView is not defined for the type S.
Comment: This have to be executed inside `onCreateView()`, where `rootView` is inflated.
Comment: I have only changed getView.findViewById to rootView.findViewById
Comment: replace `getView()` (the method) with `rootView` (the variable). See edited answer.
Answer: In your Fragment "S", you use "getView()" while the view is created (onCreateView) -> it will return null. Find Views using rootView instead:
          Button but = (Button) rootView.findViewById(R.id.setwallpaper);

Comment: It gives error rootView is not defined for the type S.

app crashing on clicking fragment


Mean and standard deviation comparisons

Question: I have  datasets (one 1997-2001, another 2020, 2021) with means and standard deviations that I need to compare. Two different dats information (mass and total number). It is expected that I compare the two data sets using statistical analyses.
A t-test was suggested. However I am unsure how to go about this when there are no original data  on the individuals themselves for 2001 data set (only have full original data for 2021). I was able to do a manual hypothesis testing using the two means and standard deviations.
I cannot think of any statistical analyses to represent an appropriate comparison without the original  data from the 2001 data set. I really would appreciate any kind of help or recommendations.
Comment: t tests depend only on means, SDs and sample sizes; Otherwise you can plot say means and confidence intervals.
Answer: To follow on what Nick Cox wrote, your situation is you have two populations (1997-2001 vs 2020-2021) with (probably) different variances, and you want to check whether they have the same mean. That's the situation where you would use Welch's t-test.
You have all the numbers to plug into the first formula on the Wikipedia page for Welch's t- test: https://en.wikipedia.org/wiki/Welch%27s_t-test
The thing to be careful of is that with a t-test you're assuming the means are normally distributed. That's not an issue for the 1997-2001 data since the sample size is quite large. For the 2020-2021 data, you'll want to check for normality by looking at the Q-Q plot and seeing if it looks like a straight line.
Comment: (+1) There is an important detail to flag. As not all birds are found to contain plastic, the mean plastic content is a mean over several zeros  and several positive weights. That bimodal distribution is going to be a little awkward as underlying the sampling distribution of the mean. You could do simulations on the conditional distribution or use bootstrapping to get a handle on the consequences.
Comment: You guys have no idea how thankful I am for your input! Your suggestions helped me big time. Thank you both so much :)

Mean and standard deviation comparisons


postfix feed to program as root

Question: I'm trying to get postfix to pipe stdin from an email into a script that needs to be run by root (yes I understand the security issues of doing this. no lectures, please).
Content of aliases file
myalias: |/root/script/thing.rb

Executable script /root/script/thing.rb needs to be run as root, but is currently ran as nobody by postfix.
Any ideas for a workaround?
any attempts at running a root script with postfix results in:
localhost postfix[942]: fatal: file /etc/postfix/main.cf: parameter mail_owner: user root has privileged user ID

Answer: This problem also occurs when you must let dovecot-lda talk with postfix. On this documentation, the author suggest two solutions

Make dovecot-lda setuid-root
Use sudo to wrap the invocation of dovecot-lda.

Option one was well explained by coincoin. The second one will be explained below.

For do that, you should have dedicated user to do sudo. Let assumet the user is rubypostfix.
Set new alias file /etc/postfix/aliasruby, chown to rubypostfix and put the alias.
content of /etc/postfix/aliasruby
myalias: | sudo /root/script/thing.rb

Then set the owner. By default, postfix will pipe email to program with the privilege same as alias file owner.
chown rubypostfix /etc/postfix/aliasruby

Don't forget to run postalias this file.
Then edit /etc/sudoers to allow this user execute only your ruby script.
rubypostfix          ALL=NOPASSWD:/root/script/thing.rb

Comment: In fact this is a really good example for the usage of sudo and the possible rules in the sudoers-file with it's possibility to give permissions on file-location-basis.
Answer: "(yes I understand the security issues of doing this. no lectures, please)" : Sorry, but I have to add one because you should understand the solution is really really dirty from a security point of view.
Here's a solution, but I really advise you not to use it :
chown root:root /root/script/thing.rb
chmod 4755 /root/script/thing.rb

Now, this is really really bad because anyone in your system can execute /root/script/thing.rb with root privilege. That also means any little coding error in /root/script/thing.rb can allow anyone to gain root privilege on your system.
Note: /root/script/thing.rb is "setuid root".
Comment: I get that opening the permissions of a file up would allow any user to run the script. the trick is getting `postfix` (what the question is actually about) to run a root script.
Comment: Does your script needs to be ran by user root (uid is checked inside the script) or does your script needs to run with root privileges?
Comment: as `root`. I installed `rvm` under root, so all of the `gem`s can only be accessed by `root`. setting 4755 lets postfix's `nobody` user execute the program, but since root has access to all of the gems, it throws require "not found" errors. I think I've just gotta bite the bullet and create a new user and setup rvm again.
Comment: "setting 4755 lets postfix's nobody user execute the program" : No 0755 would do that. 4755 does it AND make postfix execute the program as root. There must be missing some environnement variables (hint : "printenv", "env" and "export" as root)

postfix feed to program as root