What does "units" mean in epiR package?

Question: From epiR manual for epi.2by2 function:

units: multiplier for prevalence and incidence estimates.

In examples it is set to 100, units=100. What does it mean?
Note: I am working on ~5K case ~5K control data, no stratas, outcome vs exposure - 2by2 tables.
Update: This PDF might be relevant.
Comment: I think this will become apparent if you set it to 1, then set it too 100, and compare the results.
Answer: One can find the answer to this by digging into some of the examples:

A study was conducted by Feychting et al (1998) comparing cancer
  occurrence among the blind with occurrence among those who were not
  blind but had severe visual impairment. From these data we calculate a
  cancer rate of 136/22050 person-years among the blind compared with
  1709/127650 person-years among those who were visually impaired but
  not blind.

library(epiR)
dat <- as.table(matrix(c(136,22050,1709,127650), nrow = 2, byrow = TRUE))
rval <- epi.2by2(dat = dat, method = "cohort.time", conf.level = 0.90, units = 1000,  homogeneity = "breslow.day", outcome = "as.columns")
summary(rval)$ARe.strata

The incidence rate of cancer was 7.22 cases per 1000 person-years less
  in the blind, compared with those who were not blind but had severe
  visual impairment (90% CI 6.20 to 8.24 cases per 1000 person-years).
  Confidence intervals for this attributable risk estimate are from
  Rothman (2002, p 137).

Essentially, for many rate-based estimates in epidemiology, your estimate is multiplied by some number in order to get something like "Cases per X person-years", as these are often more intelligible than "Small Decimal per 1 person-day" or however else your data is estimated.
In this example, the units = 1000 yields an estimate of -7.22, which would be interpreted as 7.22 less cases per 1000 person-years. Changing units to equal 100 yields -0.72, which is interpreted as 0.72 less cases per 100 person-years.

What does "units" mean in epiR package?


Expression correct for polynomials inside a mixed model?

Question: I'm asking for the mathematically correct formulation when using polynomials of 3rd degree for the fixed and as well as for the random effects of a linear mixed model:

I wonder, if the polynomials are the same for the fixed and random effects. As the fixed and random effects get dedicated estimators I assume the corresponding polynomials are not the same. To be precise, I believe it must be:

The index of gamma changed here.
Can you please help me to understand the precise expression? Thank you!
Answer: The mathematical formulation of a linear mixed models with a 3-rd degree polynomial in both the fixed- and random-effects parts will be:
$$\left\{
\begin{array}{}
y_{ij} = (\beta_0 + b_{i0}) + (\beta_1 + b_{i1}) x_{ij} + (\beta_2 + b_{i2}) x_{ij}^2 + (\beta_3 + b_{i3}) x_{ij}^3 + \varepsilon_{ij},\\\\
b_i \sim \mathcal N(0, D), \quad \varepsilon_{ij} \sim \mathcal N(0, \sigma^2), \quad i = 1, \ldots, n, \quad j = 1, \ldots, n_i,
\end{array}
\right.$$
where $\beta^\top = (\beta_0, \beta_1, \beta_2, \beta_3)$ denotes the fixed-effects vector, $b_i^\top = (b_{i0}, b_{i1}, b_{i2}, b_{i3})$ denotes the random-effects vector for subject $i$, and $D$ is the $4 \times 4$ variance-covariance matrix of these random effects.
Comment: Under a maximum likelihood framework they are parameters and don't have a distribution.
Comment: ah, ok.. so it seems my lecturer added the "p-"letters only for a better understanding. Thank you!
Comment: Hi, can you please answer me another question? The expectation value of the random effects is zero as well as the expectation value of the errors. An expectation value of the fixed effects does not make sense, right?


How to find regression coefficients $\beta$ in ridge regression?

Question: In ridge regression, the objective function to be minimized is: $$\text{RSS}+\lambda \sum\beta_j^2.$$
Can this be optimized using the Lagrange multiplier method? Or is it straight differentiation?
Comment: What is the connection between the title (which focuses on $\lambda$) and the question (which appears to be only about the $\beta_j$)? I am concerned that "be optimized" could have distinctly different interpretations depending on which variables are considered the ones that can be varied and which ones are to be fixed.
Comment: thanks modified the question. I have read that the $\lambda$ is found by cross validation -- but I believe that means you have the $\beta_j$ already and use different data to find the best $\lambda$ Question is -- how do you find the $\beta_j$'s in the first place when $\lambda$ is an unknown?
Answer: There are two formulations for the ridge problem. The first one is
$$\boldsymbol{\beta}_R = \operatorname*{argmin}_{\boldsymbol{\beta}} \left( \mathbf{y} - \mathbf{X} \boldsymbol{\beta} \right)^{\prime} \left( \mathbf{y} - \mathbf{X} \boldsymbol{\beta} \right)$$
subject to
$$\sum_{j} \beta_j^2 \leq s. $$
This formulation shows the size constraint on the regression coefficients. Note what this constraint implies; we are forcing the coefficients to lie in a ball around the origin with radius $\sqrt{s}$.
The second formulation is exactly your problem
$$\boldsymbol{\beta}_R = \operatorname*{argmin}_{\boldsymbol{\beta}} \left( \mathbf{y} - \mathbf{X} \boldsymbol{\beta} \right)^{\prime} \left( \mathbf{y} - \mathbf{X} \boldsymbol{\beta} \right) +  \lambda \sum\beta_j^2 $$
which may be viewed as the Largrange multiplier formulation. Note that here $\lambda$ is a tuning parameter and larger values of it will lead to greater shrinkage. You may proceed to differentiate the expression with respect to $\boldsymbol{\beta}$ and obtain the well-known ridge estimator
$$\boldsymbol{\beta}_{R} = \left( \mathbf{X}^{\prime} \mathbf{X} + \lambda \mathbf{I} \right)^{-1} \mathbf{X}^{\prime} \mathbf{y} \tag{1}$$
The two formulations are completely equivalent, since there is a one-to-one correspondence between $s$ and $\lambda$.
Let me elaborate a bit on that. Imagine that you are in the ideal orthogonal case, $\mathbf{X}^{\prime} \mathbf{X} = \mathbf{I}$. This is a highly simplified and unrealistic situation but we can investigate the estimator a little more closely so bear with me. Consider what happens to equation (1). The ridge estimator reduces to
$$\boldsymbol{\beta}_R = \left( \mathbf{I} + \lambda \mathbf{I} \right)^{-1} \mathbf{X}^{\prime} \mathbf{y} =  \left( \mathbf{I} + \lambda \mathbf{I} \right)^{-1} \boldsymbol{\beta}_{OLS}  $$
as in the orthogonal case the OLS estimator is given by $\boldsymbol{\beta}_{OLS} = \mathbf{X}^{\prime} \mathbf{y}$. Looking at this component-wise now we obtain
$$\beta_R = \frac{\beta_{OLS}}{1+\lambda} \tag{2}$$
Notice then that now the shrinkage is constant for all coefficients. This might not hold in the general case and indeed it can be shown that the shrinkages will differ widely if there are degeneracies in the $\mathbf{X}^{\prime} \mathbf{X}$ matrix.
But let's return to the constrained optimization problem. By the KKT theory, a necessary condition for optimality is
$$\lambda \left( \sum \beta_{R,j} ^2 -s \right) = 0$$
so either $\lambda = 0$ or $\sum \beta_{R,j} ^2 -s = 0$ (in this case we say that the constraint is binding). If $\lambda = 0$ then there is no penalty and we are back in the regular OLS situation. Suppose then that the constraint is binding and we are in the second situation. Using the formula in (2), we then have
$$ s = \sum \beta_{R,j}^2 = \frac{1}{\left(1 + \lambda \right)^2} \sum \beta_{OLS,j}^2$$
whence we obtain
$$\lambda = \sqrt{\frac{\sum \beta_{OLS,j} ^2}{s}} - 1 $$
the one-to-one relationship previously claimed. I expect this is harder to establish in the non-orthogonal case but the result carries regardless.
Look again at (2) though and you'll see we are still missing the $\lambda$. To get an optimal value for it, you may either use cross-validation or look at the ridge trace. The latter method involves constructing a sequence of $\lambda$ in (0,1) and looking how the estimates change. You then select the $\lambda$ that stabilizes them. This method was suggested in the second of the references below by the way and is the oldest one.
References

Hoerl, Arthur E., and Robert W. Kennard. "Ridge regression: Biased estimation for nonorthogonal problems." Technometrics 12.1 (1970): 55-67.
Hoerl, Arthur E., and Robert W. Kennard. "Ridge regression: applications to nonorthogonal problems." Technometrics 12.1 (1970): 69-82.
Comment: @Minaj Ridge regression has constant shrinkage for all coefficients (other than the intercept). That's why there is only one multiplier.
Comment: @amoeba This is a suggestion by Hoerl and Kennard, the people who introduced ridge regression in the 1970s. Based on their experience - and mine -  the coefficients will stabilize in that interval even with extreme degrees of multicollinearity. Of course, this is an empirical strategy and so it is not guaranteed to work all the time.
Comment: You could also just do the pseudo-observation method and get the estimates with nothing more complicated than a straight least squares regression program. You can also investigate the effect of changing $\lambda$ in a similar fashion.
Comment: @amoeba It is true that ridge is not scale invariant, that's why it is common practice to standardize the data beforehand. I have included the relevant references in case you want to take a look. They are immensely interesting and not so technical.
Comment: @JohnK in effect ridge regression shrinks each $\beta$ by a different amount, so the shrinkage isn't constant even though there is only one shrinkage parameter $\lambda$.
Comment: @FrankHarrell In the orthogonal case that is often used for illustration the shrinkage is constant. In fact it can be shown that $\beta_R = \frac{\beta_{OLS}}{1+\lambda}$. Of course, I agree that in the nonorthogonal case, the shrinkage is not constant and depends on the degeneracies of the $\mathbf{X}^{\prime} \mathbf{X}$ and so directions of small variance will receive greater shrinkage.
Comment: I don't know why I would have orthogonality, but good point.  But even in the orthogonal case if the $X$'s have different pre-scaling you may get different shrinkage anyway.
Comment: @FrankHarrell It's an unrealistic situation that is only possible when you get the design the experiment yourself, but since it is quite easy to obtain the estimators in that case (not just ridge, lasso too) it is used for illustration. Thank you for adding that this is not true in general, though.
Comment: I thought the lagrange formulation of the problem would involve a lagrange multiplier $\alpha_i$ for each $\beta_i$. Here we have only one $\lambda$ which is global ad independent of the coefficients $\beta_i$
Comment: +1. Yes, but can you elaborate on why this is the Lagrange multiplier formulation? What would be the equivalent formulation with an explicit constraint?
Comment: @amoeba I have included some extra details, is that what you had in mind?
Comment: Yes, exactly, thanks. One minor nitpick: in the last paragraph, why do you say that one should look for $\lambda$ in the (0,1) range? Can't it be larger than 1, possibly much larger?
Comment: I should probably look in H&K, but shouldn't it depend on the scale of eigenvalues of X'X, which in turn can depend simply on the scale (units) of variables in X? Perhaps the advice is for standardized variables?
Comment: @Glen_b Can you please clarify on what you have called the pseudo-observation method? From your point, what I understand is that this would as a first step involve doing an ordinary least squares to obtain the $\beta_j$'s? How exactly does one then proceed to constrain the obtained $\beta_j$'s in accordance with the requirement, $\Sigma \beta_j^2<s$
Comment: @Minaj I see no constraint in your question.
Comment: @Glen_b In ridge regression literature, the term $\lambda\Sigma \beta_j^2$ in the optimization expression that I have given as part of my question is equivalent to $\Sigma \beta_j^2<s$. Thats the constraint I am referring to.
Comment: The formulation I referred to directly solves the optimization problem in your question. Any equivalence you want to draw to a constrained formulation of ridge regression from there, you can do from that optimization problem. The details are [here](http://stats.stackexchange.com/questions/137057/phoney-data-and-ridge-regression-are-the-same/137072#137072); as you see it works directly with $\lambda$. If you want to work with $s$, you can use the relationship between $s$ and $\lambda$ to figure out $\lambda$ (taking it back to the form in your question) and then proceed from there.
Comment: @JohnK, you mentioned that there is a one-to-one correspondence between s and λ. However, they are not equal. Do I misunderstand something?
Answer: My book Regression Modeling Strategies delves into the use of effective AIC for choosing $\lambda$.  This comes from the penalized log likelihood and the effective degrees of freedom, the latter being a function of how much variances of $\hat{\beta}$ are reduced by penalization.  A presentation about this is here.  The R rms package pentrace finds $\lambda$ that optimizes effective AIC, and also allows for multiple penalty parameters (e.g., one for linear main effects, one for nonlinear main effects, one for linear interaction effects, and one for nonlinear interaction effects).
Comment: +1. What do you think of using leave-one-out CV error, computed via the explicit formula (i.e. without actually performing CV), for choosing $\lambda$? Do you have any idea about how it in practice compares to "effective AIC"?
Comment: That formula works for the special case of OLS, not for maximum likelihood in general.  But there is an approximate formula using score residuals.  I do realize we are mainly talking about OLS in this discussion though.
Comment: I haven't studied that.  LOOCV takes a lot of computation.
Comment: Not if the explicit formula is used: http://stats.stackexchange.com/questions/32542.
Answer: I don't do it analytically, but rather numerically. I usually plot RMSE vs. λ as such:

Figure 1. RMSE and the constant λ or alpha.
Comment: Does this mean you fix a certain value of $\lambda$ and then differentiate the expression to find the $\beta_j$'s after which you compute RMSE and do the process all over again for new values of $\lambda$?


Finding uniform minimum variance unbiased estimators

Question: $X_1, X_2, X_3, \ldots{}, X_n$, i.i.d., follow $\mathcal{N}(0, \theta^2)$, $\theta > 0$.
What are the UMVUEs (Uniform Minimum Variance Unbiased Estimators) of $\theta$ as well as $\Phi(\tfrac{1}{\theta})$, where $\Phi$ is the cumulative distribution function of $\mathcal{N}(0,1)$?
Comment: Please explain why you cannot solve the homework and which level of help you require.
Comment: I have added the 'homework' tag.
Answer: Background:

Find a sufficient statistic
Show it is complete
Find an arbitrary unbiased estimator
Apply the Rao-Blackwell and the Lehmann-Scheffé theorems

Hint:
$$
\Phi(1/\theta) = \mathbb{P}(Z\le 1/\theta) = \mathbb{P}(\theta Z\le 1) = \mathbb{P}(X \le 1)\quad Z\sim\mathcal{N}(0,1),X\sim\mathcal{N}(0,\theta^2)
$$
More hints:

Write down the joint density of $(X_1,\ldots,X_n,S^2)$
Derive the conditional distribution of $(X_1,\ldots,X_n)$ given $S^2$ as a uniform distribution on a sphere
Compute $\mathbb{E}[\mathbb{I}(X_1\le 1)|S^2]$ as the surface of a truncated sphere.
Comment: Do you mean $\mathbb{E}[\mathbb{I}(X_1\le 1)|S^2]=\mathbb{P}(X_1\le 1|S^2)$? This is indeed the UMVUE.
Comment: @br69 Please [register your account](http://stats.stackexchange.com/users/login) and stop using answers for comments.
Comment: sigma(Xi^2) is sufficient and complete, because it is exponential family with full rank.
But what is p(X1<=1|sigma(Xi^2)), I know that sigma(Xi^2) follows theta^2(chi square n).
Comment: but i want the explicit form of UMVUE. i.e given data values I can compute the value of S(x1,x2,.. xn), the UMVUE.


Neighborhood basis in product topology

Question:
Let $x = (x_1, x_2, \dots) \in \{0,1\}^{\mathbb{N}}$. Show that the sets $$B_n(x)=\{(y_1, y_2, \dots) \mid y_i = x_i \text{ for all } i =1,2,\dots,n\}$$ form a neighborhood base at $x$.

Let $O$ be a neighborhood of $X$, then $O$ contains a basic open set $U = \prod_{n \in \Bbb N} U_n$ such that $x \in U \subset O$.
Now for $U$ we have that $U_n = \{0,1\}$ except for finitely many indices say $\{n_1, n_2, \dots, n_k\}$.
I'm wondering how I should use these indices for $B_n(x)$. If I let $m = \min\{n_1, n_2, \dots, n_k\}$, then do I have $B_m(x) \subset U \subset O$?
Alternative way for this would be to express $$U = \{f : \Bbb N \to \{0,1\} | f(n_1) \in U_{n_1}, \dots, f_{n_k} \in U_{n_k} \}$$ and $B_n(x) = \{f \in \Bbb N \to \{0,1\} \mid f(x_1) \in\{y_1\}, \dots ,f(x_n) \in \{y_n\} \}$ but it isn't that helpful.
Answer: Using your notation,what you want to show is that for every generic neighbourhood $O$ of your point $x=(x_1,x_2...)$, there is a $B_n()$ contained in it. Let
$U=\prod_{n\in{\Bbb N}}U_n$ the open set contained in O by definition of neighborhood. Since $x\in U$, calling F= {$n_1,...,n_k$}, $U_j=x_j$ $\forall j\in F$. So lets take $M=max{F}$ then $x\in B_M(x)$ and $B_M(x)\subset U \subset O$ .

Neighborhood basis in product topology


How to calculate the conditional variance for AR(2) model?

Question: I need to find the conditional variance of $Y_t$ given information up to time t.
$Y_t$ = $\mu $ + $\phi_1$$Y_{t-1}$ + $\phi_2$$Y_{t-2}$ +$\epsilon_{t}$
Need to find the conditional variance of this equation.
I need to find three step ahead forecast.
Was able to do the first, but got stuck at second step.
Var($Y_{t+1}$|$\Omega_t)$ = $\sigma^2$
But need to find Var($Y_{t+2}$|$\Omega_t)$?
Comment: Is that $\mu_{t+1}$ or $\epsilon_t$?
Comment: should be et. sorry.
Answer: Consider that all random variables at time t are constant once you condition on $\Omega_t$.
Then, you have the following:
$\begin{aligned}
Var(Y_{t+2}|\Omega_t) &= Var(\mu + \phi_1 Y_{t+1} + \phi_2 Y_t + e_{t+2}|\Omega_t)\\
& = Var(\phi_1 Y_{t+1}|\Omega_t) + Var(e_{t+2}|\Omega_t)\\
& = \phi_1^2 Var(Y_{t+1}|\Omega_t) + \sigma^2\\
& = \phi_1^2 \sigma^2 + \sigma^2 \qquad \text{using your result for the 1-step ahead}
\end{aligned}$
You can compute $Var(Y_{t+3}|\Omega_t)$ with the same procedure using the results for the 1-step and 2-steps ahead conditional variance.
Comment: How do you calculate a Covariance in the 3 step ahead?
Comment: Welcome @Germania, please open another question for the covariance. In any case, try to use the equation of $Y_{t+3}$ as I did for $Y_{t+2}$.
Comment: https://math.stackexchange.com/q/4079759/817548 @Ale


Icefaces Actionlistener Pass Parameter To Another Page

Question: I have have icefaces datatable and when user clicks a row, I would like to pass that row's value to another page. The value could row's one column which is the primary key. I am planning to use

How can I do this?
Thanks
*Follow up**
Kindly suggest the approach I am following is the right for passing paramter from my icefaces
datatable to another page.
I have the following in jsf page
<h:commandLink actionListener="#{bean.setSelectedItem}">

And in my bean of setSelectedItem(ActionEvent event) method I have the following code
 selectedRows.put(dataTable.getRowIndex(), true);
 (selectedRows is of Map<Integer, Boolean> selectedRows = new HashMap<Integer, Boolean>();
 List<class> selectItems = new ArrayList<class>();
             for (int index = 0; index < dataList.size(); index++) {
                if (isSelectedRow(index)) {
                    selectItems.add(dataList.get(index));
                }
            }
            newbean.method(selectItems);
            selectItems.clear();
            selectedRows.clear();

Correct me if I am doing anthing wrong above.
Thanks
Answer: Either bind the table component with UIData property in backing bean
<h:dataTable binding="#{bean.table}">

with
private UIData table;

or bind the table value with DataModel property in backing bean
<h:dataTable value="#{bean.model}">

with
private DataModel model;

Either way, they offer a getRowData() method which gives you the row back during bean action method where the UICommand component is been clicked/pressed.
<h:commandLink action="#{bean.view}">

with
public String view() {
    Object rowData = table.getRowData(); // Or model.getRowData();
    return "otherpage";
}


Java FileWriter not writing to file

Question: I'm creating an app which requires to write a file for every user (in JSON format).
The app successfully creates the file, But it's empty. In the code I added a finer output to see how the converted JSON String looks like, and I noticed that it's complete (it contains everything, so the conversion is ok). But the string isn't present in the file.
//create a new FileWriter C:/.../42.guser
        FileWriter writer = null;

        //for every User
        for(int i=0; i<users.size(); i++) {

            try {
                File f = new File(Users.usersDir.getPath()+"/"+i+".guser");

                //Create new file
                f.createNewFile();

                writer = new FileWriter(f);

                //convert to json and write to file. Here we get the object with KEY = keys[i].
                String stringedUsr = gson.toJson(users.get(keys[i]));

                logger.finer("Converted user: \""+stringedUsr+"\""); //Output seems ok

                //Write (NOT WORKING)
                writer.write(stringedUsr);

                logger.fine("Wrote updated user \""+keys[i]+"\" to file "+f.getCanonicalPath());

            } catch (IOException e) {
                FAILS++;
                e.printStackTrace();
            }

        }
        //End of for

What am I doing wrong?
Note: i gave to the app all the necessary Permissions. No AccessControlExceptions
Comment: try `flush()` method, also close the writer on completion
Comment: Just closing it should be fine. That will flush.
Comment: @AxelMontini No because it wont call flush or close ;-)
Comment: That whole re-assignment business is a code smell. Why don't you declare `writer` inside of the block where you use it and assign it on declaration? Always keep variable scopes as small as possible. (That alone won't help you here, of course, you need to `finally` close).
Answer: First of all think about closing your writer.
When closing, it should flush() your data first (as mentioned in the doc here)
You can also flush() manually.

Java FileWriter not writing to file


Daily temperature simulation/forecast with Arima/HW

Question: I have a time series of daily temperature from 1960 to 2015, and would like to forecast for 2016. My goal is to use a simple method while still be able to capture the trend and seasonality of the data over time. (I actually only care about the weather condition from May to August, but not sure if I can just use data from May to August in the past years to get a convincing forecast, so I am forecasting for a year)
I tried auto.arima, it suggested an order of (2,0,1), but the results seemed pretty bad (see plot)[forecasts from arima][1]
In addition, I tried HoltWinters smoothing method and got a seemingly reasonable result. However I don't know if this method is good at forecasting temperature.
[forecast from HW][2]
Comment: Your question is much too broad. Please focus it. Perhaps read [ask].
Answer: I too am hesitant to provide an answer without more information; however, I will suggest that for someone without a lot of experience in time series forecasting, the auto.arima function in the "forecast" package is excellent.  It has a built in optimization that searches for the best ARIMA order (p,d,q).  Here is some example code:
<code>install.packages("forecast")
library(forecast)

set.seed(1234)
tsdatav <- (seq(1:300) + rnorm(300, 1000, 10))
myts <- ts(tsdatav, frequency = 365, start = c(2017, 6))
mytsfit <- auto.arima(myts)
mytsfit  #to my example data, fit an ARIMA(5,1,0) with drift
mytsforecast <- forecast(mytsfit, 50, level = c(80, 95))
plot(mytsforecast)
</code>
Notice that in the forecast function, you can set how many periods you want to forecast out as well as confidence intervals (in addition to the point forecast).
Duke has an excellent website on ARIMA forecasting at https://people.duke.edu/~rnau/411arim.htm
Again, this is just one suggestion.  There are many forecasting approaches that work better than others given problem specifics.
Comment: Even though the question was probably too broad, this is a good answer. +1
Comment: I used auto.arima function to predict the next 365 days temperature, however, it seems that the prediction did not capture the seasonality. I also used HoltWinters smoothing method and the result seems ok by plotting. But it is not really convincing. I will edit my question above.


startMonitoringSignificantLocationChanges Não funciona em background

Question: Estou fazendo um aplicativo que utiliza o serviço de localização do dispositivo. Ele necessariamente precisa estar capturando as posições quando há uma mudança de local e deve capturar em todas as situações (background/foreground/killed), então estou utilizando o método startMonitoringSignificantLocationChanges(), porém quando eu envio ele para background, a captura de posições roda apenas por mais algum tempo e para, depois disso não captura mais. Eu me locomovo a distância que a documentação sugere para atualização (500m) de posição e nada acontece. O que posso estar fazendo de errado? Obs: No simulador funciona perfeitamente, porém no dispositivo fisico não funciona.
O arquivo Info.plist está com a configuração de background habilitada
<key>UIBackgroundModes</key>
  <array>
    <string>fetch</string>
    <string>location</string>
  </array>

AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if (launchOptions != nil) {
        let locationManager = CLLocationManager()
        locationManager.delegate = ViewController()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.activityType = CLActivityType.other
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

    return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
    let locationManager = CLLocationManager()
    locationManager.delegate = ViewController()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = CLActivityType.other
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.startMonitoringSignificantLocationChanges()
}

ViewController.swift
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //Trato a captura da nova localização
}

Comment: Qual é o dispositivo que você está usando? Modelo e versão do iOS
Comment: @FelipeRicieri testei em um iPhone 5 e um iPhone 6, ambos com a versão 10.3
Comment: locationManager.delegate = ViewController() não esta correto voce precisa fazer isso dentro do seu view controller e coloca `locationManager.delegate = self`.  ViewController() vai gerar uma instance que não é a mesma do view controller do seu app
Comment: @LeoDabus Esse código está dentro do _AppDelegate.swift_ para que eu possa usar os callbacks que estão na minha _ViewController.swift_. No simulador ele funciona perfeitamente, mas no dispositivo fisico não funciona
Comment: "para que eu possa usar os callbacks que estão na minha ViewController.swift" não faz sentido nenhum. Faz o que eu te disse se não entendeu edita a sua pergunta
Comment: Esse seu código no AppDelegate não tem como funcionar. Se tem algo funcionando não é isso. Além do problema da instancia do ViewController não ser a mesma, voce declarou locationManager como uma variável local dentro do método, quando esse método applicationDidEnterBackground acabar de ser executado ela vai simplesmente ser descartada pelo compiler. Ela simplesmente deixa de existir.
Answer: Duas sugestões...
Notei que, no código enviado, você não tá solicitando a permissão para pegar a localização do usuário... Talvez isso seja um problema. Para resolver isso, basta colocar no viewDidLoad: locationManager.requestWhenInUseAuthorization()
Outra possibilidade é que na documentação consta que essa função não manda notificações seguidas em menos de 5 minutos. Ou seja, ainda que você se desloque mais de 500 metros, se ainda não passaram 5 minutos da última notificação, o app não vai te notificar novamente até que passem esses 5 minutos.
Se você precisar verificar mais frequentemente que isso as mudanças de localização do usuário, acho que você vai ter que usar o startUpdatingLocation() e calcular se o usuário se mexeu o suficiente ou não...
Comment: Eu solicito a permissão para pegar a localização do usuário na hora que ele entra no aplicativo, na  _ViewController_. O teste que eu fiz foi caminhar durante 30 minutos, e durante o trajeto eu parei por 10 minutos. Será que já seria o suficiente? Então eu queria o seguinte comportamento, quando está com o aplicativo aberto eu quero que capture a localização mais frequentemente, então eu uso o `startUpdatingLocation` e quando o aplicativo vai para background eu quero utilizar o `startMonitoringSignificantLocationChanges()`, por isso o meu código está no _AppDelegate_ será que é possível?
Comment: Desculpa, só verificando... Você pediu a permissão só para quando o app estiver em uso ou pra sempre? Como você vai usar o app em background teria que ser "sempre".
Comment: Outra coisa... Você fez as configurações no projeto pra permitir a atualização no background? Eu nunca precisei fazer nada que rodasse em background, mas pesquisando achei o tutorial do link no fim desse comentário. E acho que o código não precisa estar no appDelegate, precisa?  https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started
Comment: Sim, eu solicito para o usuário a permissão "sempre". Também habilitei no _capabilities_ para que o app rode em background. No simulador ele funciona perfeitamente, com todos os comportamento que preciso. Porém quando vou testar com um dispositivo real, ele não funciona como esperado.


The support of a Fourier transform of a function in the positive real axis

Question: I vaguely remember reading a theorem stating that if a function in $L^2(\mathbb{R})$ has support in the positive semiaxis, then the support of the Fourier transform must contain points in the negative semiaxis. Can someone provide a reference for this?
Answer: In fact if $f\in L^2$ is supported on $(0,\infty)$ then the Fourier transform extends to be holomorphic in the upper half-plane (hence the boundary values cannot vanish on $(-\infty,0)$ unless $f=0$). See for example the chapter on "Holomorphic Fourier Transforms" in Rudin Real and Complex Analysis.


Create a loop on react JSX component

Question: I am learning ReactJS by trying to coding a game.
In this game i want that at a specify period of time/frequency a random number between one and four, of enemy (represented by images) appear at the top of the screen and fall toward the bottom of the screen. When they arrived at the bottom of the screen the relevant row disapear.

Until now i ve succeeded to get that at each load of a page a random number of ennemies appears at a random offset at the top of the screen and they fall to the bottom of the screen.
The problem is that there is only a unique row that is generated and it does it only one time.
Here my actual code (that works):
class EnemyManager extends React.Component{

    constructor(props) {
        super(props);
        this.state={
            enemyNbr: Math.floor(Math.random() * 5) + 1
        }
    }

    generate_enemy_fct(){
        console.log("enemyNbr:" + this.state.enemyNbr);  // Test--------------------------------------------------------
        let enemyArr = new Array(4);
        if (1 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[0] = <Enemy enemySrcImg="./gameData/nig01.webp"/>;
        }
        if(2 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[1] = <Enemy enemySrcImg="./gameData/nig02.webp"/>;
        }
        if(3 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[2] = <Enemy enemySrcImg="./gameData/nig03.webp"/>;
        }
        if(4 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[3] = <Enemy enemySrcImg="./gameData/nig04.webp"/>;
        }
    return enemyArr;
    }

    render(){
        let enemyArrTmp = this.generate_enemy_fct(true);
        return(

                {enemyArrTmp[0]}
                {enemyArrTmp[1]}
                {enemyArrTmp[2]}
                {enemyArrTmp[3]}

        )
    }
}

And i would like that it would behave like this:
class EnemyManager extends React.Component{

    constructor(props) {
        super(props);
        this.state={
            enemyNbr: Math.floor(Math.random() * 5) + 1
        }
    }

    generate_enemy_fct(){
        console.log("enemyNbr:" + this.state.enemyNbr);  // Test--------------------------------------------------------
        let enemyArr = new Array(4);
        if (1 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[0] = <Enemy enemySrcImg="./gameData/nig01.webp"/>;
        }
        if(2 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[1] = <Enemy enemySrcImg="./gameData/nig02.webp"/>;
        }
        if(3 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[2] = <Enemy enemySrcImg="./gameData/nig03.webp"/>;
        }
        if(4 <= this.state.enemyNbr && this.props.loopEnemyBool === true){
            enemyArr[3] = <Enemy enemySrcImg="./gameData/nig04.webp"/>;
        }
    return enemyArr;
    }

    render(){
        let enemyArrTmp = this.generate_enemy_fct(true);
        return(

                for(let i=0; i<10**6; i++){
                    if(i%1000===0){ // For exemple generate a new row of enemies at each 1000th iteration
                let enemyArrTmp = this.generate_enemy_fct(true);
                    }
                {enemyArrTmp[0]}
                {enemyArrTmp[1]}
                {enemyArrTmp[2]}
                {enemyArrTmp[3]}
            }

        )
    }}

I am really stuck on this problem thank for your help.

EDIT: As it had been asked me here the code of the <Enemy/> component.
class Enemy extends React.Component{
    constructor(props) {
        super(props);
        this.enemyImg = this.props.enemySrcImg;
        this.state={
            posYenemy:0,
            posXenemy:0,
        }
        this.refDivEnemy=React.createRef();
    }

    componentDidMount () {
        this.setState({posXenemy: Math.floor(Math.random() * (window.innerWidth- this.refDivEnemy.current.offsetWidth*1.15))});

        this.timerID = setInterval(() => {
            if(this.state.posYenemy+this.refDivEnemy.current.offsetHeight*1.15<window.innerHeight){
                console.log("posYenemy:" + this.state.posYenemy +"- windowHeight:" + window.innerHeight + "- enemyImgHeight:" + this.refDivEnemy.current.offsetHeight);  // Test--------------------------------------------------------
                this.setState((prevState) => ({
                    posYenemy: Math.floor(prevState.posYenemy + window.innerHeight * 0.05)
                }))
            }
            }, 500);
    }

    componentWillUnmount(){
        clearInterval(this.timerID);
    }

    render(){
        return(
            <div className="enemyDiv"
                 ref={this.refDivEnemy}
                 style={{
                     position: "absolute",
                     top: `${100*this.state.posYenemy/window.innerHeight}vh`,
                     left: `${100*this.state.posXenemy/window.innerWidth}vw`
                 }}>
                <img className="enemyImg"
                     alt="enemy_img"
                     src={require(`${this.enemyImg}`)}
                     style={{
                          height: "10vh"
                      }}
                />

        )
    }
}

Comment: can you share the `` component?
Comment: one solution may be to add a condition to the `` component, when its position passes a certain point, give it a new random position at the top of the page.
Comment: I see the problem now. I don't know how how to fix it.
Comment: @HasanAli I ve added it thk for your help
Comment: I ve tried it but it just generate the same row again at the top of the screen, or/and just one row appears. But i will try it again asap.
Answer: The problem is, your Enemy Manager class isn't doing enough managing.  As of now, it does not know where its children (Enemy) are on the screen, so it can't make a decision on whether or not to render them.  In addition to creating new enemies, your Enemy Manager class needs to decide where each Enemy is on the screen, and determine if it should continue to exist.
Here's what you can do to fix this:

Create a EnemyProps object, something like:

{image: 'foo.webp', xPos: 0, yPos: 0}

Remove the interval from the Enemy component.  You don't need state there anymore either.  Position will now be passed into the component via props.
Store an array of EnemyProps in the Enemy Manager's state.  It should start off empty.
On the Enemy Manager, on Component Did Mount, set an interval.  On each iteration:

Clone the EnemyProps array
Update the Y position of each object in the array.  If the object's new Y position indicates that it is at the bottom, remove it from the cloned array.
If this is the 1000th iteration (or whatever you decide), call the generate_enemy function, which you will modify to create EnemyProps objects instead of <Enemy> components.  The function will assign an image and a random X position to each object it creates, then push it onto the cloned array.
Set state to use the cloned EnemyProps array.

Your Render function should do this: state.enemies.map(enemy => <Enemy props={enemy} />

Create a loop on react JSX component


How to PHP ldap_search() to get user OU if I don't know the OU for base DN

Question: I have an Active-Directory structure where User objects reside in OU for example, IT, Technical, HR, Accounts etc.. I want to write a PHP script that authenticates the user with AD and depending on their Group to provide the aproperiate web services.
ldap_search() requires base DN.
I tried to search with
ldap_search($ldap, "dc=country,dc=company,dc=co,dc=uk", "(samaccountname=$username)", array("memberof"));

but PHP gives "Operation Error". If instead i specify the OU
ldap_search($ldap, "ou=sales,dc=country,dc=company,dc=co,dc=uk", "(samaccountname=jake)", array("memberof"));

then the search is ok.
Is there a wildcard I can use?
On a side note, should user objects be in OU at all? Because I am the noob who moved them inside in the first place!
EDIT:
With credits to JPBlanc for guiding me in the right direction and http://blog.redbranch.net/?p=76
The solution is to add 2 lines between connect and bind.
ldap_connect(..)
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind(..)

Thanks =)
EDIT 2 - Fullcode:
<?php

namespace ldap;

abstract class AuthStatus
{
    const FAIL = "Authentication failed";
    const OK = "Authentication OK";
    const SERVER_FAIL = "Unable to connect to LDAP server";
    const ANONYMOUS = "Anonymous log on";
}

// The LDAP server
class LDAP
{
    private $server = "127.0.0.1";
    private $domain = "localhost";
    private $admin = "admin";
    private $password = "";

    public function __construct($server, $domain, $admin = "", $password = "")
    {
        $this->server = $server;
        $this->domain = $domain;
        $this->admin = $admin;
        $this->password = $password;
    }

    // Authenticate the against server the domain\username and password combination.
    public function authenticate($user)
    {
        $user->auth_status = AuthStatus::FAIL;

        $ldap = ldap_connect($this->server) or $user->auth_status = AuthStatus::SERVER_FAIL;
        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
        $ldapbind = ldap_bind($ldap, $user->username."@".$this->domain, $user->password);

        if($ldapbind)
        {
            if(empty($user->password))
            {
                $user->auth_status = AuthStatus::ANONYMOUS;
            }
            else
            {
                $result = $user->auth_status = AuthStatus::OK;

                $this->_get_user_info($ldap, $user);
            }
        }
        else
        {
            $result = $user->auth_status = AuthStatus::FAIL;
        }

        ldap_close($ldap);
    }

    // Get an array of users or return false on error
    public function get_users()
    {
        if(!($ldap = ldap_connect($this->server))) return false;

        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
        $ldapbind = ldap_bind($ldap, $this->admin."@".$this->domain, $this->password);

        $dc = explode(".", $this->domain);
        $base_dn = "";
        foreach($dc as $_dc) $base_dn .= "dc=".$_dc.",";
        $base_dn = substr($base_dn, 0, -1);
        $sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(|(mail=*)(telephonenumber=*))(!(userAccountControl:1.2.840.113522.214.171.1243:=2)))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title"));
        $info = ldap_get_entries($ldap, $sr);

        for($i = 0; $i < $info["count"]; $i++)
        {
            $users[$i]["name"] = $info[$i]["cn"][0];
            $users[$i]["mail"] = $info[$i]["mail"][0];
            $users[$i]["mobile"] = $info[$i]["mobile"][0];
            $users[$i]["skype"] = $info[$i]["ipphone"][0];
            $users[$i]["telephone"] = $info[$i]["telephonenumber"][0];
            $users[$i]["department"] = $info[$i]["department"][0];
            $users[$i]["title"] = $info[$i]["title"][0];

            for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++)
                $users[$i]["othertelephone"][$t] = $info[$i]["othertelephone"][$t];

            // set to empty array
            if(!is_array($users[$i]["othertelephone"])) $users[$i]["othertelephone"] = Array();
        }

        return $users;
    }

    private function _get_user_info($ldap, $user)
    {
        $dc = explode(".", $this->domain);

        $base_dn = "";
        foreach($dc as $_dc) $base_dn .= "dc=".$_dc.",";

        $base_dn = substr($base_dn, 0, -1);

        $sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(samaccountname=".$user->username."))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title"));
        $info = ldap_get_entries($ldap, $sr);

        $user->groups = Array();
        for($i = 0; $i < $info[0]["memberof"]["count"]; $i++)
            array_push($user->groups, $info[0]["memberof"][$i]);

        $user->name = $info[0]["cn"][0];
        $user->dn = $info[0]["dn"];
        $user->mail = $info[0]["mail"][0];
        $user->telephone = $info[0]["telephonenumber"][0];
        $user->mobile = $info[0]["mobile"][0];
        $user->skype = $info[0]["ipphone"][0];
        $user->department = $info[0]["department"][0];
        $user->title = $info[0]["title"][0];

        for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++)
                $user->other_telephone[$t] = $info[$i]["othertelephone"][$t];

        if(!is_array($user->other_telephone[$t])) $user->other_telephone[$t] = Array();
    }
}

class User
{
    var $auth_status = AuthStatus::FAIL;
    var $username = "Anonymous";
    var $password = "";

    var $groups = Array();
    var $dn = "";
    var $name = "";
    var $mail = "";
    var $telephone = "";
    var $other_telephone = Array();
    var $mobile = "";
    var $skype = "";
    var $department = "";
    var $title = "";

    public function __construct($username, $password)
    {
        $this->auth_status = AuthStatus::FAIL;
        $this->username = $username;
        $this->password = $password;
    }

    public function get_auth_status()
    {
        return $this->auth_status;
    }
 }
?>

Usage:
$ldap = new ldap\LDAP("192.168.1.123", "company.com", "admin", "mypassword");
$users = $ldap->get_users();

Comment: @LarryG.Wapnitsky I have posted my code that works for me on PHP 5.3.x. Hope it for you.
Comment: got it working about 2 hours after I posted.  Thanks!
Answer: If you try to perform the searches on Windows 2003 Server Active Directory or above, it seems that you have to set the LDAP_OPT_REFERRALS option to 0:
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

Without this, you will get "Operations error" if you try to search the whole AD (using root of the domain as a $base_dn).

In LDAP Directories in general any node can be under any node (a user is a node, an ou is a node).
But Active-Directory behave in a different way the SCHEMA define in which container an object can exist. So, if you look for a user, superiors allowed are: builtinDomain, domainDNS and organizationalUnit as you can see here under:
Comment: Thanks, but I get a different warning: ldap_search(): Search: Partial results and referral received
Comment: I Edit the answer can you try please.
Comment: I was able to solve the problem so I marked your post as the answer already. Additionally I edited my question too. Thanks for your help!
Comment: Just perfect, this is what i was looking for.


Waiting for a countdowntimer to finish

Question: I need help with writing a code,
How do I make a code to wait until the countdown timer is finished?
I tried with boolean says if countdown timer is running and in the code, it was like this:
<code>---part 1 of code---

While(running){}

---part2 of the code---
</code>
It makes the screen freeze... Idk how to solve this.. help someone?
Comment: Shouldn't you do the opposite?  Write the code you want to execute in the trigger of the countdown timer.
Comment: I need to run X times a code that include a countdown timer than changing some things an another countdown timer.
Answer: CountDownTimer already has that, You just need to put the codes that you want to make them work after the timer finished, inside the onFinish(), like below
<code> countDownTimer = new CountDownTimer(10000, 1000) {
        @Override
        public void onTick(long l) {

        }

        @Override
        public void onFinish() {
          //this part will work after timer have finished
        }
    }.start();
</code>

Waiting for a countdowntimer to finish


PHP Warning: preg_match()

Question: segue o codigo:
<code>$ER = '[\/]?([?]?pag=[a-zA-Z0-9_-]*)(&var1=[a-zA-Z0-9_-]*)?(&var2=[a-zA-Z0-9_-]*)?(&var3=[a-zA-Z0-9_-]*)?(&var4=[a-zA-Z0-9_-]*)?'; // A ER que separa as vars
$wURL = W_URL;
$http = 'http://';
if( $https || $_SERVER["SERVER_PORT"] != 80 ){
    $url_site =  str_replace('http:', 'https:', URL_SITE);
}else{
    $url_site =  URL_SITE;
}
$_save_pag = '';
//Executa o rewrite somente quando estiver definido para tal
if($doRewrite == true){

    if(empty($str) && preg_match($ER,$str,$registers)){
</code>
erro:

PHP Warning:  preg_match(): Unknown modifier '?' in /home/sisv2/sis_adm/includes/funcoes/rewrite.func.php on line 17
Comment: Qual é a expressão regular?
Comment: Complemente a pergunta com a expressão regular.
Comment: $ER = '[\/]?([?]?pag=[a-zA-Z0-9_-]*)(&var1=[a-zA-Z0-9_-]*)?(&var2=[a-zA-Z0-9_-]*)?(&var3=[a-zA-Z0-9_-]*)?(&var4=[a-zA-Z0-9_-]*)?';
Comment: pronto coloquei la
Answer: Esse erro está acontecendo porque sua expressão deve ser delimitada por '/' ou '#'.
Logo, ela ficaria assim:
<code>$ER = '/[\/]?([?]?pag=[a-zA-Z0-9_-]*)(&var1=[a-zA-Z0-9_-]*)?(&var2=[a-zA-Z0-9_-]*)?(&var3=[a-zA-Z0-9_-]*)?(&var4=[a-zA-Z0-9_-]*)?/'; // A ER que separa as vars
</code>
Isso acontece porque quando você especifica opções para sua expressão regular (multi-linha, linha simples, case sensitive etc.) elas ficam "fora" da expressão, depois do segundo delimitador.

PHP Warning: preg_match()


How to add a conditional unique constraint in Liquibase?

Question: Problem: When a user is deleted the associated record is not deleted from the database. Instead, I set user.delete column to true. Now I need to put a unique constraint on user.email but only for active users (not deleted).
How can I do this?
databaseChangeLog:
  - changeSet:
      id: add-user-unique-constraint
      author: me
      changes:
        - addUniqueConstraint:
            columnNames: email, delete
            constraintName: unique-email-not-deleted
            tableName: users

The above naive approach puts a composite constraint on (email, delete) columns, but it doesn't work because in the following flow user is blocked from deleting and creating an account:

register a new user with email "E"
delete the user with email "E"
register again new user with email "E"
delete the user with email "E" -> error: constraint violation
register new user with email "E" -> error: constraint violation

ps. databases are H2 and PostgreSQL
Answer: This feature is DB-specific and currently liquibase doesn't support it.
For PostgreSQL you may use direct SQL:
databaseChangeLog:
  - changeSet:
      id: add-user-unique-constraint
      author: me
      changes:
        - sql:
            "sql": "CREATE UNIQUE INDEX user_email_idx ON user (email) WHERE delete = 'false'"

however this will NOT work in H2.
Possible options for H2:

if it's used in tests then you may migrate to PostgreSQL using testcontainers technology (but your build environment will depend on docker)
have custom changesets for PostgreSQL and H2 - you may use preconditions


Haskell Error: No instance for (Num [[a0]] arising from a use of `it`

Question: I'm stuck on an error which is given when I test this function:
maxWeight n wc f =
let ps (x:xs) = case x of
    [] -> [[]]
    otherwise -> ps xs ++ map (x:) (ps xs)
in maximum $ filter (<wc) $ map sum $ ps $ map f [1..n]

This is the full error:
No instance for (Num [[a0]]) arising from a use of `it`
In a statement of an interactive GHCi command: print it

I'm testing the function by calling it in GHCi with
maxWeight 5 10 (\i -> i + 1)

Answer: I find it difficult to fathom what your function (and the helper ps)  is supposed to do (this usually helps to get better answers... :-) but it looks like you are confusing two idioms in your definition of ps
You can write
ps y = case y of
  [] -> [[]]
  (x:xs) -> ps xs ++ map (x:) (ps xs)

or just
ps [] = [[]]
ps (x:xs) = ps xs ++ map (x:) (ps xs)

but as it stands now, the definition of ps is missing a base case (what is ps []?)
With this out of the way, the definition compiles, and maxWeight 5 10 (\i -> i + 1) yields 9
Comment: Oh I see, I had assumed that matching the first element being an empty list is the same as matching the entire input being an empty list
Comment: An empty  list has no first element,  but `[[]]` *does* have an empty list as it s first element. So for the compiler, your `ps` works on lists of lists, and hence returns lists of lists of lists. When you sum these, you are treating lists of lists as if they were numbers, which is (in ordinary English) what the error messeage says.


how to remove the column that having a null value with min and max

Question: I want the all null values in my column to disappear or not be displayed. The not value in where didn't work for my code:
<code>select A.EmpID [X_TMSHT_EMPID] ,convert (varchar,(A.[RecordDate]),1) [X_TMSHT_APPLY],
  , (convert(varchar,(A.[RecordDate]),1) + ' ' +( MAX(IIF(ISNULL(CHARINDEX('O', A.[Entries], 0), -1) > 0,SUBSTRING(A.[Entries], LEN(A.[Entries]) - 5, 5), NULL)))) AS [TimeOut]
FROM Employees[B]
inner JOIN[DTR Upload] [A] ON B.EmployeeNo = A.EmpID
GROUP by B.LastName, B.FirstName, B.MiddleName, A.EmpID, A.[recordDate]
order by A.[recordDate] asc, B.LastName + ','+B.FirstName + ','+ B.MiddleName asc
</code>
Comment: As already asked please show us - text description isn't clear.
Comment: [Bad habits to kick : using table aliases like (a, b, c) or (t1, t2, t3)](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3)
Comment: Can you share some table structure with data and what is your expected output.
Comment: ok wait a post a picture
Comment: in short i dont see the all null value
Comment: Please show the results you want to obtain (it can be hard to people to visualise what you want based on a description - much easier if you show us).
Comment: remove the 1 column because it is null this is i want the answer
Answer: As the OP is not helping with any data samples and what i understand is OP want to remove the Timeout which contains NULL value. So i just used the derived table to filter out the Timeout with NOT NULL value.
<code>     SELECT * FROM (select A.EmpID [X_TMSHT_EMPID] ,convert (varchar,(A.[RecordDate]),1) [X_TMSHT_APPLY],
      , (convert(varchar,(A.[RecordDate]),1) + ' ' +( MAX(IIF(ISNULL(CHARINDEX('O', A.[Entries], 0), -1) > 0,SUBSTRING(A.[Entries], LEN(A.[Entries]) - 5, 5), NULL)))) AS [TimeOut]
    FROM Employees[B]
    inner JOIN[DTR Upload] [A] ON B.EmployeeNo = A.EmpID

    ) AS d
    WHERE d.TimeOut IS NOT NULL
GROUP by d.LastName, d.FirstName, d.MiddleName, d.EmpID, d.[recordDate]
    order by d.[recordDate] asc
</code>
Comment: Please add some text explaining what you have changed in the query to solve the answer the question.


htaccess remove .php and keep query string

Question: Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]

This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test

The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1

Answer: Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]

Answer: Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Answer: If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1

now you can access
domain.com/test?id=1

from
domain.com/test/1

Comment: what if this variable always changes or there are different variable names, also I would need to pass data on other pages as well. There isn't just a rule saying take out the .php and leave everything else?
Comment: you can also pass id=2, id=3... so on.. to access id=2, use `domain.com/test/2`
Comment: and if you are different variable, or 2 variable, just change rule to `RewriteRule ^test/(.*)/(.*)$ /test.php?id=$1&othervariable=$2`


Hijack output from aspx

Question: I want to convert an aspx page to PDF using a component that can convert Html to PDF. Is it possible to, during post back, redirect the output from the aspx-page and send it as a stream or string to a HtmlToPdf method?
Answer: <code>protected override void Render(HtmlTextWriter writer)
{
    // setup a TextWriter to capture the page markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the page into our surrogate TextWriter
    base.Render(htw);

    // convert the TextWriter markup to a string
    string pageSource = tw.ToString();

    if (convertToPDF)
    {
        // convert the page markup to a pdf
        // eg, byte[] pdfBytes = HtmlToPdf(pageSource);
    }

    // write the page markup into the output stream
    writer.Write(pageSource);
}
</code>
Comment: Tanks Luke. This was exactly what I was looking for.
Answer: Have you tried to send the value returned from "HttpContext.Current.Response.OutputStream;" in the postback ?
Answer: Hi I think that the way to do this would be to use the Reponse.Filter property to intercept and alter the HTML being sent to a page.
There's a tutorial video and sample code in both VB.net and C# on this page on the ASP.net website:
http://www.asp.net/learn/videos/video-450.aspx
Answer: You would write an HttpFilter that is attached to the request. This is code that can change the output after it has been written by the ASP.NET Page's Render step.
This article shows how to do this (they change the output from HTML to valid XHTML, but the idea is the same).

Hijack output from aspx


Frequentist Regression Analysis & Pearson's $r$

Question: I find that a polynomial trend line gives a better $r^2$ value. Is Pearson's Correlation Coefficient $r$ still a good indicator in this scenarios between the strength of correlation between my two variables?
If not - is there a better indicator for strength of relationship between two variables when using a polynomial trend line?
Answer: The square root of $r^2$ is in effect the correlation between observed values and those predicted by the polynomial. (That's why it is denoted $r^2$.)
Whether the polynomial is a better fit than a straight line is a more challenging question. A polynomial with quadratic and possibly other terms can wiggle much more than a straight line but whether it is a better idea overall is harder to say.
Positively, a polynomial can capture important features of a relationship that a straight line just can't match, such as a turning point. Negatively, and this is the advantage reversed, a polynomial can wiggle in ways that make no substantive sense and/or would be treacherous if taken literally or extrapolated incautiously.
What you should always do includes

Comparing $r^2$ from a polynomial fit with the square of plain Pearson's $r$ to see how much improvement is implied by the polynomial.

Plotting the polynomial fit together with the original data to gauge the improvement.

Thinking about the substantive interpretation of the polynomial fit, especially whether explicit or implicit turning points, inflections and axis crossings make sense.

(I find it hard to imagine that a Bayesian would say anything different in spirit.)
Comment: Thank you so much! re:1 Are you recommending comparing a linear r-squared vs a polynomial r-sqaured and seeing the improvement through the delta?
Comment: Yes; that is #1 of my suggestions (now edited). Equivalently compare the root of $r^2$ with your Pearson $r$.
Comment: Thank you Nick for taking the time to educate me and in explaining the material thoroughly. I  appreciate it.


Violin-Piano Self Duet - which part do you record first?

Question: During the lockdown many professional musicians are recording and posting videos of them playing. Some of them, ingeniously, record self-duets. One such example is "Augustin Hadelich plays both parts of Rachmaninoff Vocalise" showing the artist playing the violin part on one side of the screen and the piano part on the other.
In such situations which part is it better to record first?
Answer: In the description for piece you listed, Augustin Hadelich states it was the piano part first, and why:

In case you are wondering, I recorded the piano part first - I played the piano part while imagining singing the violin line. I don't think it could work as well if I'd recorded the violin part first, because all of the timings and nuances in the violin part are a result of what happens harmonically in the piano part.
Comment: Why is this community wiki?
Comment: @Voldemort'sWrath I might be misunderstanding the point of community wiki. I just copy-pasted information from the description of the video linked in the question, so I didn't really feel right taking any credit for the answer.
Answer: I would go for the piano part first.  It's the backbone of the piece and plays throughout.  Even though there is a fair amount of rubato, he cleverly synchronises the two parts.  He would have recorded the piano part and then studied it until he knew he could respond to every nuance.


Eclipse Subversive revision numbers on multiple project commit

Question: If I have 2 projects in Eclipse that refers to the same repository location.

repository location: svn://server
  project-module1 ->
  svn://server/trunk/project-module1
  project-module2 ->
  svn://server/trunk/project-module2

So if I sync the project change with Subversive and have a change in module1 and module2 that refers to the same context I select all files and perform one commit, but if I look into my project revisions after that I see that 2 revisions were created. One for module1 and one for module2 with the same comment.
How can I change the behave that only one revision number is created?
Answer: Do you have the command line version of Subversion installed?
What's the output when you do:
<code>$ svn log --verbose svn://server/trunk
</code>
This will give you a better idea what's happening.
If both modules are considered separate Eclipse projects and each has their own separate checkout, Eclipse will create separate commits for both of them. Is this the case?


Consulta com datadiff retornando 0 no resultado sql server

Question: Tenho a seguinte consulta:
SELECT
DATEDIFF(hh, MIN(BATIDA), MAX(BATIDA))AS HORAS
 FROM   ARELBATIDATRANSITOVIEW
WHERE  CHAPA = 3187
  AND BATIDA IS NOT NULL
  AND SEQUENCIALBATIDA IN (2,3)

  GROUP BY DATA

ORDER BY DATA DESC

Exemplo:
HORAS
2
2
2
1
2
2
2
3

Só que ela só me retorna as horas,preciso dos outros campos que seria:
SELECT
DATA,
CHAPA,
CODCOLIGADA,
BATIDA,
STATUS,
NATUREZA,
SEQUENCIALBATIDA,
DATEDIFF(hh, MIN(BATIDA), MAX(BATIDA))AS HORAS
 FROM   ARELBATIDATRANSITOVIEW
WHERE  CHAPA = 3187
  AND BATIDA IS NOT NULL
  AND SEQUENCIALBATIDA IN (2,3)

  GROUP BY DATA,CHAPA,CODCOLIGADA,BATIDA,STATUS,NATUREZA,SEQUENCIALBATIDA

ORDER BY DATA DESC

Mais quando rodo essa consulta com todos os campos o campo:   DATEDIFF(hh, MIN(BATIDA), MAX(BATIDA))AS HORAS retorna com 0
Exemplo:
                                                                   HORAS
2016-10-17 00:00:00.000 123  1  2016-10-17 12:10:00.000 C   1   2   0
2016-10-17 00:00:00.000 123  1  2016-10-17 14:20:00.000 C   0   3   0
2016-10-14 00:00:00.000 123  1  2016-10-14 11:48:00.000 C   1   2   0

O que poderia ser ?
Qual meu interesse nesse campo hora,listar resultados > 2
Answer: Isso deve ocorrer porque DATEDIFF returna INT, e pode estar fazendo algum arredondamento.
Tente retornar em segundos e vai funcionar, veja o exemplo:
declare @d1 datetime, @d2 datetime

set @d1='2016-10-19 15:40:52.847'
set @d2='2016-10-19 15:42:52.847'

-- Aqui retorna zero
select DATEDIFF(hh, @d1, @d2) AS HORAS

-- Aqui retorna certo, 2horas
select DATEDIFF(ss, @d1, @d2)/60 AS HORAS

No seu código:
DATEDIFF(ss, MIN(BATIDA), MAX(BATIDA))/60 AS HORAS

Comment: Entendi e deu certo, Obrigado.


What kind of SSL cert do I need for this vhost configuration?

Question: If I have my virtual host configured as:

<VirtualHost *:80>
    ServerName foo.com
    ServerAlias www.foo.com
    Include conf.d/foo.conf
</VirtualHost>

Can I get by with a single domain SSL certificate or must I purchase a wildcard SSL certificate to handle the www subdomain?
Answer: To prevent a Common Name mismatch, you will need to purchase either a wildcard certificate or a Subject Alternative Name (SAN) certificate.  The SAN certificate will allow you to specify multiple CN.  SAN certificates are also known as Unified Communications Certificates.
If cost is an issue, it is not unreasonable to only use SSL for www.domain.com and not domain.com.  It is often more difficult to justify the additional cost for wildcard certificates.
Comment: Standard GoDaddy certificates provide both www.domain.com and domain.com in the "Certificate Subject Alt Name" field. Other vendors may do this as well. In practice this does prevent the need for using a wildcard certificate to deal with the www and non-www case.
Comment: Comodo, DigiCert, and Globalsign add a free www as well


Weak induction: $\log(n!) < n\log(n)$

Question: I'm currently stuck on the next step in proving this inequality $\log(n!) < n\log(n) \\$. These are the steps I have so far. I can't quite seem to get the LHS to the RHS. Can I get some advise on how to work trough it?

Assume: $\log(k!) < k\log(k)$
Prove: $\log((k+1)!) < (k+1)\log(k+1)$
LHS = $\log((k+1)!) = \log(k!(k+1)) = \log(k!) + \log(k+1)$
$\ < k\log(k^1) + \log(k+1)$  This is where I start to get lost
$\ < (k+1)\log(1) + \log(k+1)$ Not sure this step is entirely correct
Not sure what step to do next to get it to the RHS
Comment: $\log (n!)=\log n +\log (n-1)+\cdots +\log 1$.
Answer: $k\log(k)+\log(k+1)<k\log(k+1)+\log(k+1)=(k+1)\log(k+1)$
Comment: +1. Without the logarithms this could be $(k+1)\,k^k < (k+1)\, (k+1)^k = (k+1)^{k+1}$, which I find more intuitively obvious
Answer: For a complete prove by induction we need to consider the base case and the induction step. In this case we have

base case: $n=1\implies \log 1!<1\log 1$ which is true

then condider the

induction step: assuming that $\log n!<n\log n$ is true we need to show that $\log (n+1)!<(n+1)\log (n+1)$ then

$\log (n+1)!=\log (n+1)+\log n! <\log (n+1)+n\log n <\log (n+1)+n\log (n+1)=(n+1)\log (n+1)$
Answer: This can be alternatively proved like this:
$k\log{(k)}=\log{(k^k)}$
${k^k\over k!}$
$={k\over k-1}\cdot{k\over k-2}\cdot\ \cdots\ \cdot{k\over1}$
$\gt1$
So, $k^k\gt k!$
Which proves that $\log (k!)\lt k\log(k)$

Weak induction: $\log(n!) < n\log(n)$


How to: for loop minimum value dictionary python?

Question: I have a problem with creating a tuple with a minimum value from a dictionary. I found a way with the lambda function but my teacher says that I need to create a for loop where i "unpack" the dictionary, find the minimum value of the dictionary, and return it.
I'm lost, this is what I have now but I need to change the fastest_travel_mode(distance):
travel_times = {"Walk": (2, 5, 0),
                "Bike": (4, 18, 3),
                "Bus": (8, 35, 8)}

def travel_time_per_mode(distance):
    fastest_times = {}
    for mode in travel_times:
        setup_time, speed, finish_time = travel_times[mode]
        fastest_times[mode] = setup_time + 60 / speed * distance + finish_time
    return fastest_times

def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    for fastest_mode in sorted(travel_time.items(), key=lambda kv: kv[1]):
        return fastest_mode

def main():
    distance = float(input("How many kilometers do you have to travel? "))
    print("When you have to travel", distance, "km, taking a", fastest_travel_mode(distance)[0], "will be the quickest way to your destination with an approximate time of", int(fastest_travel_mode(distance)[1]), "minutes.")

if __name__ == "__main__":
    main()

I tried:
def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    for fastest_mode in sorted(travel_time.items(), key=lambda kv: kv[1]):
        return fastest_mode

It works but i need to do it another way including the steps creating a for loop "unpack" the dictionary, find the minimum value of the dictionary, and return it.
Comment: You don't need to use `sorted()`, use `min()`.
Comment: *"I have a problem with creating a **tuple** with a minimum value from a dictionary"*. Do you expect a tuple or a single value?
Comment: Sorry, this is all new for me. fastest_travel_mode(distance) should return a tuple indicating the quickest mode of travel and the total travel time. So for example ("Walk", 8.0) when the distance is 0.5
Answer: You can use min method. By default it searches minimum by looking at dict keys, but you can provide key argument to search by something else. In that case we use get function which tells find the key with minimum value
def fastest_travel_mode(distance):
    travel_time = travel_time_per_mode(distance)
    mode = min(travel_time, key=travel_time.get)
    return mode, travel_time[mode]

And now you do the calculation twice, you can do it once and store the result:
def main():
    distance = float(input("How many kilometers do you have to travel? "))
    mode, time = fastest_travel_mode(distance)
    ...

Comment: Thanks for the quick help! mode = min(travel_time, key=travel_time.get)
    return mode, travel_time[mode]  Did the job!


Transfering the approach of GLM/GLM.NB to FEGLM in R to find the best dispersion parameter

Question: I would like to analyze my dataset with around 1 million observations and 10 thousand fixed effects with a negative binomial regression model. Due to the high number of fixed effects I cannot apply 'glm' / 'glm.nb' from 'MASS' package.
To handle high dimensional fixed effects I found 'feglm' ('alpaca' package). It is working, but I have to provide a value for theta as in 'glm'. To find the optimal theta you can use 'glm.nb' (if you don't have that much variables or fixed effects). A similar function is missing for 'feglm'.
I tried to transfer the approach of 'glm.nb'. This is my result:
i=1
theta=c(1.2345)

repeat {

feglm_negbin <- feglm(y ~ x1 + x2 + x3 + x4 | fe1 + fe2 , data=dataset, family = negative.binomial(theta[i]))

i=i+1
theta.value=theta.ml(data$Aenderungen_Technik,fitted(feglm_negbin), limit=1000)
theta[i]=theta.value[1]
print(theta[i])

if (theta[i]-theta[i-1]<=0.00001) {break} }

As stopping criterion I chose a small value (0.00001), because I do not know the statistical correct definition of it.

Does this procedure make sense? Is it correct?
What is the statistical stopping criterion in 'glm.nb'? Can I transfer it to my    approach?
Answer: your idea was fine. Find attached an example that shows that glm.nb() and feglm() produce the same estimates.
library(alpaca)
library(MASS)
library(pglm)

### Load data set ###
data("PatentsRD", package = "pglm")

### benchmark MASS glm.nb() ###
nb1 <- glm.nb(patent ~ rdexp + spil + factor(firm) + factor(year) + 0, data = PatentsRD)
summary(nb1)

### using alpaca feglm() ####
i <- 1
theta <- 1
repeat {
  nb.alpaca <- feglm(patent ~ rdexp + spil | firm + year, data = PatentsRD,
                     family = negative.binomial(theta))
  theta.old <- theta
  theta <- theta.ml(PatentsRD$patent, fitted(nb.alpaca), limit = 1000)
  print(theta)
  if (abs(theta - theta.old) <= sqrt(.Machine$double.eps)) break
  i <- i + 1
}

### Final estimation with converged \theta ###
nb.alpaca <- feglm(patent ~ rdexp + spil | firm + year,
                   data = PatentsRD, family = negative.binomial(theta))

### Compare ###
coef(summary(nb.alpaca))
coef(summary(nb1))[1:2,]
```


Setting up Jasmine & Karma to test Angular

Question: I'm just trying to get Karma set up to test my Angular app with Jasmine. I'm getting this error:
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 30.0.1599 (Mac OS X 10.9.0)]: Connected on socket pxDaOUXMfGXOVL7ZOuPb
Chrome 30.0.1599 (Mac OS X 10.9.0) ItemCtrl should work FAILED
    TypeError: Object [object Object] has no method 'off'
        at null.<anonymous> (/Users/macbookpro/Sites/groceries/spec/angular-mocks.js:1917:36)

Here's that function in angular-mocks.js:
afterEach(function() {
    var injector = currentSpec.$injector;

    currentSpec.$injector = null;
    currentSpec.$modules = null;
    currentSpec = null;

    if (injector) {
      injector.get('$rootElement').off();
      injector.get('$browser').pollFns.length = 0;
    }

    angular.mock.clearDataCache();

    // clean up jquery's fragment cache
    angular.forEach(angular.element.fragments, function(val, key) {
      delete angular.element.fragments[key];
    });

    MockXhr.$$lastInstance = null;

    angular.forEach(angular.callbacks, function(val, key) {
      delete angular.callbacks[key];
    });
    angular.callbacks.counter = 0;
  });

I think I'm just requiring files in the wrong order in my karma config. Here's my file structure:
karama.config.js is in the the same directory /spec as the files it's requiring:
// list of files / patterns to load in the browser
    files: [
      'angular.min.js',
      'angular-mocks.js',
      'app_testable.js',
      'app_spec.js'
    ],

app_testable.js is a stripped down version of my app with no dependencies except angular. Here's my test. I'm just trying to get true to be true.
'use strict';

describe('ItemCtrl', function(){
  var scope;
  beforeEach(angular.mock.module('Grocerease'));
  beforeEach(angular.mock.inject(function($rootScope, $controller){
      scope = $rootScope.$new();
      $controller('ItemCtrl', {$scope: scope});
    })
  );

  it('should work', function(){
    expect(true).toBe(true);
  });

});

How do I need to set up karma so that it is able to run the tests correctly?
Comment: Are you using jquery?
Comment: No. The only files I'm using are listed above. As an aside, I didn't include angular-resource because I'm not using the router module. Is that required?
Comment: angular-resource is not for router module but to handle "resources" from REST APIs. Recheck that you are not missing any dependency that might be using at index.html
Comment: btw, did you check at console of the browser for any additional info?
Comment: Indeed I did. Same error as above, referencing the same line in `angular-mocks`.
Comment: what version of angular you are working with? angular and angular-mocks files are both same version?
Comment: @dimirc, I was wrong. The `angular-mocks` was a newer version. Would you submit your answer and I'll mark it correct? Thanks a lot for suggesting that.
Answer: The problem seems like a dependency was incorrectly loaded.
What version of angular are you working with? The 'angular' and 'angular-mocks' files should be the same version.


Como funciona hibernate.hbm2ddl.auto?

Question: Quais são os values que posso utilizar nessa propiedade? ex: Update
<prop key="hibernate.hbm2ddl.auto">update</prop>

Como funciona? Quando devo utilizar? é uma boa prática?
Answer: As opções são essas:

validate: validar o schema, não faz mudanças no banco de dados.
update: faz update o schema.
create: cria o schema, destruindo dados anteriores.
create-drop: drop o schema quando ao terminar a sessão.

Ai você tem que avaliar o que é melhor para seu projeto, geralmente eu utilizo o update.
Link da resposta no SOen link
Answer:
Como funciona?

Respondida pelo nosso amigo Rafael.

Quando devo utilizar?

Isso é um pouco ambíguo, pois pode desencadear boas discussões, mas é bom pensar nisso como dar responsabilidade a uma tecnologia que teoricamente vai cuidar de todas as ações de desenvolvimento no database.
Será que é uma boa idéia deixar acontecer de forma automática, a criação, atualização ou remoção de qualquer entidade pelo Hibernate?
Não, por mais que o todo o desenvolvimento para o database forme o mesmo codebase, não significa que a automatização de um sobre o outro irá funcionar. Validar seus scripts de uma forma manual é sempre uma forma mais segura para o desenvolvimento. Nunca utilize isso em produção, pode ser um erro fatal.
Mas podemos falar em contexto, se o seu contexto é fazer um projeto próprio de estudo e teste, talvez sim, mas eu não acredito que seja uma boa prática. Em minha experiência com ferramentas que costumam fazer algo relacionado a desenvolvimento de forma automática, tendem a falhar miseravelmente.

é uma boa prática?

Não. Vejo como uma péssima prática, tanto do lado do desenvolvedor quanto do lado do cliente que vai utilizar o produto. Dar responsabilidades de desenvolvimento a qualquer ferramenta não é uma boa prática. Boa prática é você ter total controle do produto, desde a forma como é entregue, até a sua arquitetura e code quality. Pense o quão simples é para o hibernate replicar uma linha de código errada sua para o database e dessa forma causar o caos e isso esteve totalmente fora do seu alcance.
Mas como falei, podemos gerar boas discussões, pois se formos focar em boas práticas não utilizaríamos ORM, pois é com certeza um anti-pattern para Orientação a Objetos.
Answer: Esta é a lista completa:
validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.
none: does nothing with the schema, makes no changes to the database
Comment: No caso ai, o ideal é adotar none, e montar o esquema de tabelas manualmente via ddl.

Como funciona hibernate.hbm2ddl.auto?


How to get rendered source code of an html page from code behind so I can send it in mail

Question: I created an aspx page wit a button send.
on that button click a function code behind goes on.
What I want is that when I click on the button to get all the html source code rendered of the aspx page in a variable so I can send it in a mail. how can I get the rendered html
source code from code behind function.
Answer: I actually just wrote a method for this not too long ago. It gets the view at a provided relative path, passes in a provided model, and renders the view as a string.
<code>public static string RenderViewToString(string relativePathToControl, object viewData)
{
    ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };

    viewPage.ViewData = new ViewDataDictionary(viewData);
    viewPage.Controls.Add(viewPage.LoadControl(relativePathToControl));

    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            viewPage.RenderControl(tw);
        }
    }

    return sb.ToString();
}
</code>
You may need to add a few using statements.
Comment: I should add that I only used this for partial views (.ascx). I'm not sure how it would work for a .aspx file.
Comment: I suppose this solution works only for MVC, not for WebForms. Am I right?


Check my Kernel for SELinux Support

Question: I want to know if the kernel i am running has support for SELinux. What commands would i use to check this ?
Answer: Pretty sure if you are running a 2.6 kernel, you have it.  I use sestatus to check if SELinux is on or not.  If the output of uname -r shows a 2.6.something kernel you probably have selinux support.  Sestatus will show you if it is enabled or not.
Answer: If you are running a 2.6 or later kernel, it is available. To know for sure, you have to look at the configuration for your kernel, look for the CONFIG_SECURITY_SELINUX_... configuration options (kernel configuration is probably a file /boot/config...).
If it is a distribution kernel, it is most probably enabled.
Comment: Running a kernel that was compile by someone else so the version won't matter to me, still might be disabled. I just check the config file in boot and it showed it as disabled ty : )
Comment: `cat /proc/config.gz | gunzip > running.config` makes a copy of running config then i just did a search on it `grep -i selinux /root/running.config`

Check my Kernel for SELinux Support


Another sufficient condition for two matrices having a common eigenvector ?

Question: Let $A , B$ be complex $n \times n$ matrices such
that $AB = BA^2$, and assume $A$ has no eigenvalue of absolute value $1$ , then is it true that $A$ and $B$ have a common (nonzero) eigenvector ?
Answer: Suppose that $\lambda$ is an eigenvalue of $A$ and $v$ an eigenvector. It follows that
$$ABv=\lambda^2 Bv.$$
Repeating this argument,
$$A(B^nv)=\lambda^{2^n}(B^nv).$$
It follows that $\lambda^{2^n}$ are all eigenvalues of $A$. Thus, $\lambda^{2^k}=\lambda^{2^l}$ for some $k<l$.
If $\lambda=0$, then $Bv$ is also an eigenvector of $\lambda=0$. If $V$ is the subspace of eigenvectors of $0$ of $A$, then $B$ can be viewed as a linear operation on $V$. It implies that $B$ has an eigenvector in $V$. Thus $A$ and $B$ has a common eigenvector.
If $\lambda\neq0$, then $\lambda^{2^{l}-2^k}=1$ contradicting $|\lambda|\ne1$.
Comment: I don't understand , $Bv$ is also an eigenvector of $\lambda =0$ .... from this how does it follow that $A,B$ have a common eigenvector ?
Comment: If $\lambda=0$, then $A(Bv)=0$. It shows that $B$ sends a vector in $V$ to $V$. Then it's standard result saying that $B$ must have an eigenvector in $V$. I'm not sure how to explain better :-)
Comment: @QuangHoang How do you know that $Bv\ne0$ in order to conclude that $\lambda^2$ is also an eigenvalue of $A$?
Comment: @user26857 thanks for pointing that out. At any point that $B^kv=0$, then $B^{k-1}v$ is the common eigenvector of $A$ and $B$.
Comment: @QuangHoang Of course! My point was that this should be inserted into the proof.


How can I tell which location is the best?

Question: I have a list of the scores from various locations on different exams listed below.
How can I compare the different test and scores to show which location is the best over all?
It would really help if someone could inform which type of test to do and why.  I first thought to use the Wilcoxon signed-rank test, but the data is not in pairs.
<code>             English  AP CalcBC    AP PhyscsB   AP Chem     AP Econ
City      Rank  Score Rank Score  Rank  Score Rank  Score  Rank Score

(Beijing)   6   36.33   4   39.14   1   35.54   1   47.94   3   46.55
(Hefei)     5   38.95   11  26.31   9   18.61   8   27.83   8   30.52
(Hexi)     na      na   8   33.04   na     na   6   37.7    7   32.46
(Huzhou)    4   39.06   1   45.47   na     na   na     na   na     na
(Jiangyin)  9   29.9    9   30      5   24.4    7   31.75   6   36.7
(Jinan)     1   47.72   6   37.2    3   28.91   3   41.55   2   47.94
(Nantong)   3   39.67   5   38.45   2   32.59   5   38.9    4   45.05
(Shanghai)  na     na   3   39.44   6   23.18   2   44.4    5   43.86
(Suzhou)    8   30.19   10  27.5    7   21.75   9   26      9   19.87
(Yangzhou)  2   43.59   2   40.45   4   26.7    4   41.45   1   50.73
(Yixing)    7   33.26   7   35.91   8   21.48   10  24.7    na     na
</code>
Comment: it seems to me that this is about judgment more than stats.  are all of the subjects equally important?  or do you KNOW that some are more important than others such that you can assign a vector of weights?  if they are all equal and graded on the same scale, why not simply sum them?
Comment: So you are just saying taking the mean of the scores for each site and use that?
Comment: Why do the ranks for 'AP Economics' start with 11? Is this a typo?
Comment: @jokel an artifact of the way markdown does lists: specifically, there was a space at the beginning of the AP Economics line, so markdown assumed that the Econ list was a continuation of the previous list and automatically renumbered it.
Comment: @gung how did you get the data to look like that?
Comment: CV's [Editing help](http://stats.stackexchange.com/editing-help) page guides you through some of the markdown options that are available. To do a code block, you highlight the section & click on the "{}" above the text field. To put the data in a table, I did some quick editing in Excel.
Answer: This is a multivariate problem, and there's not a single criterion you can you that will tell you which one is "best" in general without you putting weights on which subjects you think are more/less important. For example, consider a simplification with just two cities and two subjects: Shanghai scores 40 in Math, 50 in Physics; Beijing scores 50 in Math, 40 in Physics. Which is better? It depends on whether you think Math or Physics is more important. Suppose you decided Shanghai was better, and I give you updated scores: the next years Shanghai scores exactly the same, but Beijing scores 50 in math and 40.01 in Physics. Is Shanghai still better, or is Beijing better now? What if Beijing's Physics score had gone up to 41? 42? How high would it have to go before you would decide Beijing were better? This tells you how you weight math vs. physics.
The problem is that you are trying to rank the cities along a single dimension--"goodness"--but you have multivariate data. In order to rank along a single dimension, you have to reduce your data to one dimension, and how you do that depends on how important you think each subject is. Your ranking will end up being according to a weighted average of each city's scores in each subject. If you want to assume that each subject is of the same importance, then you just take the regular average where all subjects are weighted equally.
If you're interested in reading more about the problem, the general topic is multivariate optimization.
Answer: If you do not care about the scores of individual subjects (Chemistry, Economics, etc.) you can just average the scores for every city and compare them as suggested in the comment. Otherwise you could use multinomial logistic regression which allows you to predict a nomial outcome variable ('city' in your case) by your independent variables ('subject' and 'mean score').


For any finite abelian group $G$, there is an integer $m$ with $G$ isomorphic to a subgroup of $U(\mathbb{Z}_{m})$.

Question: I want to prove if the following assertion from Rotmans Advanced Algebra page 205 is true:
For any finite abelian group $G$, there is some integer $m$ with $G$  isomorphic to a subgroup of $U(\mathbb{Z}_{m})$, where $U(\mathbb{Z}_{m})$ are the units of Integers module m.
Is this sentence true or false? The book I'm studying says this is not true, but I cannot find a proper counterexample to understand the mentioned claim. Thanks
Comment: Already edited. I'm studying Rotmans Advanced Algebra and U(Zm) means units of Integers module m
Comment: Probably follows from the [Kronecker-Weber Theorem](https://en.wikipedia.org/wiki/Kronecker%E2%80%93Weber_theorem) and [this](https://math.stackexchange.com/questions/132665/every-finite-group-is-isomorphic-to-some-galois-group-for-some-finite-normal-ext).
Comment: What book are you studying?
Comment: What is $U(\Bbb Z_m)$?
Comment: units in the ring of integers modulo $m$ ?
Answer: This is, in fact, a true statement.
There are two essential facts that I use here without proof.

Any finite abelian group is a product of cyclic groups. Look up the structure theorem for modules over a PID for more on this.
For any $ n \geq 2 $ there are infinitely many primes $p$ such that $ p \equiv 1 \text{ mod } n $. This is a special case of Dirchlet's theorem on primes in an arithmetic progression.

Now, take such a finite abelian group $ G $. We have $ G = \prod_{i=1}^n \mathbb Z/n_i \mathbb Z$, some integers $n_i$. Let $p_i$ be a prime such that $p_i \equiv 1 \text{ mod } n_i$. This exists by the second fact listed above. In fact, as there are infinitely many such primes we can take these $p_i$ to be distinct. Now, as $p_i \equiv 1 \text{ mod } n_i$, we have $ n | p_i - 1 $. Thus, we have $\mathbb Z / n_i \mathbb Z \subseteq \mathbb Z/(p_i - 1) \mathbb Z$. Strictly speaking, this means that there is an injective homomorphism between these groups, but identitying a group with its isomorphic image doesn't affect us in this case. As $p_i$ is prime, we know that $\mathbb Z / (p_i-1) \mathbb Z = (\mathbb Z/p_i \mathbb Z)^{\times}$. Hence, we have $\mathbb Z/ n_i \mathbb Z \subseteq (\mathbb Z/ p_i \mathbb Z)^{\times}$ Thus, we have $ G = \prod_{i=1}^n \mathbb Z/n_i \mathbb Z \subseteq \prod_{i=1}^n (\mathbb Z/p_i \mathbb Z)^{\times}$. One can show easily that the unit group of the product of rings is the product of their unit groups, so we have $\prod_{i=1}^n (\mathbb Z/p_i \mathbb Z)^{\times} = \left(\prod_{i=1}^n \mathbb Z/p_i \mathbb Z\right)^{\times}$. We took the $p_i$ to be distinct, hence they are pairwise relatively prime. By the Chinese Remainder Theorem, $\prod_{i=1}^n \mathbb Z/p_i \mathbb Z = \mathbb Z/ p_1 p_2 \dots p_n \mathbb Z$. Thus, $G \subseteq \left(\prod_{i=1}^n \mathbb Z/p_i \mathbb Z\right)^{\times} = (\mathbb Z/p_1 p_2 \dots p_n \mathbb Z)^{\times}$.
Field theory is listed as a tag for this, so I should mention that once you have that $\mathbb Q(\zeta_n)$ has Galois group $(\mathbb Z/n \mathbb Z)^{\times}$, this result proves the inverse Galois problem for finite abelian groups, i.e. all finite abelian groups are Galois groups over $\mathbb Q$.
Comment: @QuangHoang In the copy of Rotman I have, the remark on page 205 says "We cannot conclude more from the proposition; given any finite abelian group G, there is some integer m with G isomorphic to a subgroup of U(Im)". It seems to me that Rotman is saying that the result is true, but it isn't a corollary of the result he just proved (that $\text{Gal}(\mathbb F(\zeta_n)/F)$ is a subgroup of $(\mathbb Z/m \mathbb Z)^{\times})$. The phrasing is kind of poor, so I see where the confusion arises.
Comment: You didn't explicitly answer the question "yes" or "no" (it is conventional to do so at the start of the answer).
Comment: And I'm wondering why Rotman would say otherwise.
Comment: @BillDubuque I've added this, thanks for catching that.


What is the difference between pairwise kernels and pairwise distances?

Question: What is the difference between pairwise kernels and pairwise distances?
I frequently came across terms like pairwise kernels and pairwise distances while learning about Pairwise metrics, Affinities, and Kernels, but I don't get the real meaning of these words and their differences.
Answer: I think to start; we should understand concerning Kernel(-functions):

How to intuitively explain what a kernel is?
Is a kernel function just a mapping?
What is a kernel function?

Pairwise distance provides distance between two vectors/arrays. So the more pairwise distance, the less similarity while cosine similarity is:
$cosine\_similarity = (1 - pairwise\_distance)$, so the more cosine similarity, the more similarity between two vectors/arrays.
Please see the following example to find Cosine similarity performance on two random quotes/strings using TF-IDF:

from sklearn.feature_extraction.text import TfidfVectorizer

documents = (
    "The Worst Distance Between Two People is Misunderstanding",
    "A Mutual Misunderstanding"
)

tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)

#method 1:
from sklearn.metrics.pairwise import cosine_similarity
print(cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)[0,1])
#0.1505569696020495

#method 2:
from sklearn.metrics.pairwise import pairwise_distances
print(pairwise_distances(tfidf_matrix[0:1], tfidf_matrix, metric='cosine')[0,1])
#0.8494430303979505

Note 1: that according to documentation: Cosine distance is defined as 1.0 minus the cosine similarity.
$$Cosine\_distance = (1 - cos(similarity))$$
That explains the difference between the two methods is used for similarity computation for quotes.
Note 2: based on scikit-learn documentation valid metrics for Pairwise distance are :

['cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan']. These
metrics support sparse matrix inputs. ['nan_euclidean'] but it does
not yet support sparse matrices.

From scipy.spatial.distance documentation, valid metrics for Pairwise distance matrix are:

['braycurtis', 'canberra', 'chebyshev', 'correlation', 'dice',
'hamming', 'jaccard', 'kulsinski', 'mahalanobis', 'minkowski',
'rogerstanimoto', 'russellrao', 'seuclidean', 'sokalmichener',
'sokalsneath', 'sqeuclidean', 'yule']

Note 3: According to this answer, definitions for cosine distance that they used are different in scipy & sklearn. The one used in sklearn is a measure of similarity while the one used in scipy is a measure of dissimilarity
Concerning Pairwise distance measures, which many ML-based algorithms (supervised\unsupervised) use the following distance measures/metrics:

Euclidean Distance
Cosine Similarity
Hamming Distance
Manhattan Distance
Chebyshev Distance
Minkowski
Jaccard Index
Haversine
Sørensen-Dice Index
Gower's Distance

There is also interesting visualization from sklearn here.
This article by M. Grootendorst gives you better intuition to understand the choices of distance metrics as well as the advantages and pitfalls of 9 frequently used distance measures in Data Science.
In the case of categorical data, you must convert it to numeric data, either by ranking (for an ordered factor) or by encoding it as a set of binary (dummy) variables. If the data consists of mixed continuous and binary variables, you will usually want to scale the variables so that the ranges are similar; one way is to use Gower's distance. Selecting the proper distance measure resulted in a much better model!
Comment: This doesn't seem to address the original question, which was about the difference between distances and kernel functions
Comment: @user20160 The title of the question is a bit vague. I assumed that OP is interested in the context of **distance metrics** between pairwise kernels and pairwise distances as the link in question discusses this; otherwise, the definition/difference of kernel/pairwise distance does not make any sense! If you have a better explanation for OP, feel free to edit my answer. The understanding of kernel has been addressed at the beginning of the answer and followed by an example to address how distance could be measured pairwisely and finally ended up with some discussion and some references.


Test accounts and products in a production system

Question: Is it worth designing a system to expect test accounts and products to be present and active in production, or should there be no contamination of production databases with test entities, even if your shipping crew knows not to ship any box addressed to "Test Customer"?
I've implemented messaging protocols that have a test="True" attribute in the spec, and wondered if a modern schema should include metadata for tagging orders, accounts, transactions, etc. as test entities that get processed just like any other entity--but just short of the point where money gets spent. Ie: it fakes charging an imaginary credit card and fakes the shipment of a package.
This isn't expected to be a substitute for a fully separated testing, development, and QA database, but even with those, we've always had the well-known Test SKU and Test Customer in the production system. Harmless?
Answer: Having testing accounts in production is something I usually frown upon because it opens up a potential security hole.  One should strive to duplicate as much of the production environment in testing as possible but there are obviously cases where that isn't possible.  Expensive production only hardware is a prime example.  I would say as a general practice it should be discouraged but as with all things if you can provide a reason which makes sense to you then you might overlook a hard and fast rule.
Comment: Personally I think having a mechanism to simulate transactions is essential to not only smoke test a production system, but also to help analyse specific production issues.
Answer: I imagine the Best Practice Police would state the mantra "never ever test in prod" and maybe even throw in "developers should not have access to prod".
However, I work on a mainframe-based system where there are huge differences between production and test/qa/qc; the larger the system, the more likely such a situation is. Additionally, the more groups that have a stake in the application, the more likely this is.
I need more than two hands to count how many times we could only duplicate a problem in the production environment. The option then becomes creating test tables/users/data or using live customer data.
At times we do also create test records in production tables, as some users/clients like having something they can search/retrieve that is always there.
So my advice is that it is OK to put test accounts/products into production if it will help to troubleshoot after go-live.
Answer: If your database is created from scripts in an automated fashion, then this becomes a non-question.
In my environment we use cruise control for continuous builds. The SQL Scripts for generating the database are checked into CVS with everything else, and the database is rebuilt from those scripts on a daily basis.
Our test data is a second set of sql scripts, which are run for the test database and are not run for the production database.
Given our environment test data never touches the production database.
This solution really works great for us.
Answer: I wouldn't put any test data in a production system nor would I want to have access to this system as a developer.
I'm working in an industry with very sensitive medical and financial information and having such information would make it impossible to distinguish productive from data out of the testing system.
IMHO the best practice is to completely separate these two worlds and invest in setting up a procedure to prepare a comprehensive testing environment.
Comment: a noble goal, but not always feasible in my experience, as @tomasso points out
Comment: I successfully managed not ever touching a production system and don't allow it for any of my developers. I only permit sitting next to somebody in the operations or support team advising on some interventions on live data. I treat it like a holy grail, this makes everybody much more responsible.
Answer: In out ERP systems (internally accessible only) we have test data so that when we move changes from test to production environments we can test the whole process.  I view that data as a necessary evil, since  subtle configuration differences between systems can cause catastrophic results, so once a change is in production we test is fully before "releasing" it to the users.
As I said though, these are internal apps only, so the security risks are lessened somewhat - that's a very valid concern.
Answer: Never ever test in prod, even though that is where all the revenue is generated/stats are collected/magic happens...?
Always have a production test plan.  There are going to be problems that happen on prod, or, if you are unlucky, only happens on prod.  If you don't have anything in place, the first time you need to test on prod (which are usually high-stress cases) you'll be up the creek without a paddle.
It's not harmless to have test data on prod, you do need to be careful.