Dúvida com Rotas e Controllers Laravel 4

Question: Sou iniciante em Laravel e estou tendo um problema com rotas e controllers. Criei um formulário com a seguinte action:
{{ Form::open(array('action' => 'MatriculasController@formulario' }}

Criei uma rota para esta action:
Route::post('formulario', 'MatriculasController@formulario');

O método formulario do MatriculaController faz algumas operações com os dados recebidos do formulário e deveria retornar uma View:
    //função que recebe e manipula os dados do formulario
public function formulario(){
    //recebe os dados do formulario
    $ra=$_POST['ra'];
    $aluno=$_POST['nome'];
    $nasc=$_POST['nascimento'];
    $responsavel=$_POST['responsavel'];
    $cpf=$_POST['cpfResponsavel'];

    //verifica se o RA foi fornecido
    if (strlen($ra>0)){
        $siga = new Siga();
        $consultaRA=$siga->conecta("select NOM_PESSOA from supervisor.PESSOA where COD_PESSOA='$ra'");
        $resultado=mssql_fetch_array($consultaRA);
        $nome=utf8_encode($resultado['NOM_PESSOA']);
        return View::make('confirmaIdentidade');
    }
    .
    .
    .
}

A View confirmaIdentidade:
@extends('templates.templateVeterano')

@section('conteudo')

teste

@stop()

O problema é que não está retornando essa view e eu não entendo o motivo. Está retornando uma página em branco com a url /formulario.
Alguém sabe o que posso estar fazendo errado?
Comment: Se eu fosse advinho, teria acertado de prima, hein!
Comment: Pode ser muitas coisas: Você está usando uma section que não existe na view pai. Você está acessando um método que não retorna nada.
Comment: +1 Porque eu amo Laravel!
Comment: Amanda Lima, já aconteceu muito comigo. Para poder te ajudar, você vai ter que postar parte do seu controller. Pelo menos os métodos envolvidos
Comment: Vou editar e postar todo o código
Answer: Amanda, pelo visto o que faz retornar a página em branco no seu código é o View::make que está dentro do if.
Se não atender a condição, como ela não é retornada, então seu código retornará uma página em branco.
Para verificar se essa afirmação está correta, experimente fazer isso depois do if do $ra.
return 'Alguma coisa errada';

Se isso aparecer, então o motivo alegado acima está correto.
O que pode ser feito - é o que eu faria - é colocar o return View::make fora de qualquer condição if. Se houver necessidade de mandar uma mensagem de erro, você pode fazer assim:
if ($erro) {
     // Como a requisição é post, volta para mesma página
     // com uma mensagem flash na sessão
     return Redirect::back()->with('mensagem', 'Mensagem de erro');
}

return View::make('x');

Na view:
@if(Session::has('mensagem'))
   {{ Session::get('mensagem') }}
@endif

Outro problema que pode ocasionar a página em branco no Laravel 4 é a incompatibilidade de nome das sessões.
Por exemplo:
#view_pai
@yield('content')

 #view_filho
   @extend('view_pai')
   @section('content_')
   {{-- Errei o nome de propósito pra demonstrar --}}

Alguma coisa

   @stop

Se você fizer View::make('view_filho'), nada será retornado, já que você não definiu a section com o valor correto.
Comment: Esse era exatamente o problema! A yield no template era `@yield('content')`
Comment: Sabia, só podia ser uma coisa ou outra. Vou adicionar na resposta.
Comment: Coloquei o return como você sugeriu e retornou "Alguma coisa errada". Independente se o return está dentro ou fora do if está funcionando quando mando retornar uma string. Mas não retorna a View nem dentro e nem fora do if :(
Comment: E estou cuidando para que o if sempre seja satisfeito
Comment: @AmandaLima, então verifique se no seu "templateVeterano" existe a `@yield('conteudo')`


How can I call a method from onSubmit functon in Vue?

Question: I have a button that is used to log in the user via onSubmit function when a form is filled in. However, I need to call yet another method that will get some additional data about the user, such as privileges. However I cannot make it to work. I've tried to put the code from desired method directly in the onSubmit() function, but it did not work. Here is my code:
This is the form where the button is located.
<form @submit.prevent="onSubmit">
 Some code
 <button class="btn btn-success" type="submit">Log in</button>
</form>

This is my script.

Comment: Use `this.$store` instead of `useStore()` if you are not using the [Composition API](https://v3.vuejs.org/guide/composition-api-introduction.html) and actually use `methods`. In your case, you are mixing both `setup` and `methods`. Don't use `methods` at all, if you are using Composition API. You can return the method in the setup function (like a variable).
Answer: I now found the solution to my problem. I moved setUserState() as a function under setup() and deleted the methods. Setup is setting up the store now and returns setUserState. Following code works:


Determine the solution set of an inequality by transformations and sign table

Question: $$
\frac{x^{2}-x-2}{x-1} >0
$$
"What is the solution set of the inequality? Show by doing appropriate transformations, and through a sign table."
I would like to know how to solve this question, the solution is below, but I am not understanding how they arrived at this answer. Could someone simplify the explanation? Especially the sign table?
1.
$$
\begin{gathered}\frac{x^{2}-x-2}{x-1} >0\\ \Leftrightarrow \left( x^{2}-x-2<0\  \wedge \  x-1>0\right)  \vee \left( x^{2}-x-2>0\  \wedge \  x-1>0\right)  \\ \Leftrightarrow \left( -1<x<2\  \wedge \  x<1\right)  \vee \left( \left( x<-1\  \vee x>2\right)  \wedge x>1\right)  \\ \Leftrightarrow -1<x<1\  \vee x>2\\ \end{gathered}
$$
The solution set, S, is
$$
S=\{ x\in R|-1<x<1\  \vee x>2\}
$$
And through the sign table we get:
2.
$$
\begin{array}{cccccccc}x&&-1&&1&&2&\\ x^{2}-x-2&+&0&-&-&-&0&+\\ x-1&-&-&-&0&+&+&+\\ \frac{x^{2}-x-2}{x-1} &-&0&+&nd&-&0&+\end{array}
$$
Thus, the solution set, S, will be
$$
S=\{ x\in R|-1<x<1\  \vee x>2\}
$$
Answer: Personally, I am not happy with how this material was apparently presented to the OP (i.e. Original Poster, AKA Math student).  It seems unnecessarily convoluted.  So, I will provide an alternative way of making sense out of the concepts.
Given two well defined functions, $f(x)$ and $g(x)$, the only values of $(x)$ that will satisfy the inequality
$$\frac{f(x)}{g(x)} > 0,$$
are those values of $x$ that satisfy both of the following requirements:

$g(x) \neq 0.$
If the value of $x$ does not satisfy this requirement, then the expression $~\dfrac{f(x)}{g(x)}~$ is not well defined.

Either
$f(x)$ and $g(x)$ are both positive
or
$f(x)$ and $g(x)$ are both negative.

Note that although the first bullet point requirement is actually implied by the second bullet point requirement, I still listed it separately, for emphasis.
Further, for the second bullet point, as illustrative examples,

$\dfrac{7}{3} > 0, ~~\dfrac{-7}{-3} > 0.$

$\dfrac{-7}{3} < 0, ~~\dfrac{7}{-3} < 0.$

In this particular instance,
$f(x) = x^2 - x - 2,~~$ and
$g(x) = (x - 1)$.

So, since $g(x)$ is easier to analyze than $f(x)$, that will be the first step.  Here, you have three cases to consider:

$x = 1 \implies g(x) = 0.$
Per the discussion, $x = 1$ is automatically an outlawed value.

$x > 1 \implies g(x) > 0.$
So, in this instance, you will be looking for all values of $x$ such that $~x > 1~$ and $f(x) > 0.$

$x < 1 \implies g(x) < 0.$
So, in this instance, you will be looking for all values of $x$ such that $~x < 1~$ and $f(x) < 0.$

At this point, there is no avoiding that the behavior of the function $f(x)$ needs to be analyzed.  Fortunately, $f(x)$ is only a 2nd degree polynomial (rather than a polynomial of higher degree).
Although the presented question did not specifically outlaw using Calculus to study the behavior of $f(x)$, I feel that in this problem, it will be more effective if Calculus is outlawed.
One approach to the problem would be to attempt to graph the function $f(x)$.  Then, you would study the behavior of the portion of the graph where $x > 1$ and the behavior of the graph where $x < 1.$
If (for example) $f(x)$ was a polynomial of degree higher than $(2)$, and if Calculus methods were outlawed, then this might be the way to go.  Since $f(x)$ is a polynomial of degree $(2)$, the behavior of $f(x)$ can readily be scrutinized by Algebra.
The method to be used, completing the square, is similar to the method used to derive the quadratic equation.
Anyway, to analyze the behavior of the function $f(x)$, the first step is to ask:
with $f(x) = x^2 - x - 2$,
what first degree polynomial $(x - r)$ will be such that
the first two terms in $(x - r)^2 = x^2 - x.$
Since $(x - r)^2 = x^2 - 2rx + r^2$, you need $(2r)$ to equal $(1)$.
This means that you begin analyzing the behavior of $f(x)$ by first constructing the function
$$\left(x - \frac{1}{2}\right)^2 = x^2 - x + \frac{1}{4}.$$
This implies that
$$f(x) = \left(x - \frac{1}{2}\right)^2 - \frac{9}{4}. \tag1 $$
Therefore,
$$f(x) > 0 \iff \left(x - \frac{1}{2}\right)^2 > \frac{9}{4} = \left(\frac{3}{2}\right)^2. \tag2 $$
Also,
$$f(x) < 0 \iff \left(x - \frac{1}{2}\right)^2 < \frac{9}{4} = \left(\frac{3}{2}\right)^2. \tag3 $$
So, the behavior of $f(x)$ has been reduced to determining which values of $x$, where $~x \neq 1,~$ satisfy the inequalities in (2) and (3), above.
In order to do this, the OP needs the following Algebraic result:
Given two real values $R$ and $S$, where $~S > 0~,$you have that

$R^2 > S^2$ if and only if
either
$R > S$
or
$R < -S.$

$R^2 < S^2$ if and only if
$R < S$
and
$R > -S.$

Using the above Algebraic result, the behavior of $f(x)$, with respect to the inequalities in (2) and (3) can now be analyzed.

$f(x) > 0~$ if and only if
$\displaystyle \left(x - \frac{1}{2}\right) > \frac{3}{2}$
or
$\displaystyle \left(x - \frac{1}{2}\right) < \frac{-3}{2}.$

$f(x) < 0~$ if and only if
$\displaystyle -\frac{3}{2} < \left(x - \frac{1}{2}\right) < \frac{3}{2}.$

These results may be re-expressed as

$f(x) > 0~$ if and only if
$\displaystyle x > 2$
or
$\displaystyle x < -1.$

$f(x) < 0~$ if and only if
$\displaystyle -1 < x < 2.$

Now, it is time to put this together with the behavior of $g(x)$.  The simplest way to do this is to break the analysis of the behavior of $g(x)$ into the two cases of $g(x) > 0$ and $g(x) < 0.$
$\underline{\text{Case 1} ~g(x) > 0}$
Case 1 will apply if and only if $x > 1.$
So, in Case 1, you are looking for values of $(x)$ such that

$x > 1$

$f(x) > 0.$

From the analysis, this will specifically be represented by those values of $(x)$ such that
$$~x > 2.$$

$\underline{\text{Case 2} ~g(x) < 0}$
Case 2 will apply if and only if $x < 1.$
So, in Case 2, you are looking for values of $(x)$ such that

$x < 1$

$f(x) < 0.$

From the analysis, this will specifically be represented by those values of $(x)$ such that
$$-1 < x < 1.$$

Putting Case 1 and Case 2 together, the entire solution set of the inequality
$$\frac{f(x)}{g(x)} > 0$$
will be those values of $(x)$ where

Either $~x > 2$

Or $~-1 < x < 1.$
Answer: The continuous function
$$\frac{(x-2)(x+1)}{x-1}$$
may only change its sign at its zeroes, $x=2$ or $x=-1$, or where it's not defined, $x=1$.  All three linear factors of the fraction have an odd multiplicity, hence a change of sign will occur at the zeroes of each factor.  As for $x=0$ the fraction is positive, it will be positive for $-1<x<1$, negative for $x<-1$ and for $1<x<2$ and positive for $x>2$.


What is the sum of the following series with a prime denominator

Question: $$\sum_{j=1}^{546}\left[ \frac{(5j)}{(1093)}\right ]$$
Where the brackets are the floor function. I'm not even sure how to start this question besides just figuring out for how many numbers the floor of the function equates to 0, 1, 2, etc by trial and error.
Comment: You are just adding a bunch of constants over some subset of indicies.  For $j$ up to 218 (now for you, how did I get 218?), the floor function gives a term of 0.  So from j from 1 to 218, you are just adding 0.  Now, for the next set of indices, you will get 1... how do you know where to stop and where the sum changes to terms involving 2?
Comment: By the way, I don't see anything wrong with that approach... sometimes trial and error is a solid way to get a handle on what is happening with each summand.
Answer: First, note that
$$0<\frac{5j}{1093}<1$$
leads to
$$0<5j<1093$$
which in turn leads to
$$0<j<218.6$$
This tells us that all $j$ from $1$ to $218$ will produce a summand of $0$
Now, do the same thing for
$$1<\frac{5j}{1093}<2$$
and find those $j$ that will give a summand of 1 for each $j$... rinse and repeat until you've gotten to your upper bound of 546...


Why the Jumbotron seems too large to not fit the window?

Question: I'm working on a web application in Laravel 5.4 which uses Bootstrap 4.0.0 beta2 for front-end designing.
The problem is that Jumbotron is too large and I have to scroll down and left to see the entire content. And I want it to fit the window (or to seem like a small beautiful photo frame on a large wall!).

Here is my blade file

        Get Started

                 Choose services
 We are much happy to serve you! Please choose  services from below

                     Carpet cleaning
                     Tile cleaning

                    Next

In the above, I had used CDN for generating Jquery, Popper.js, and Bootsrap.min.js. And CSS is generated from the project itself( Bootstrap 4.0.0 beta2 has installed and compiled via npm).
The funniest thing is that the Jumbotron works fine when using CDN for implementing CSS( followed the official docs ).

The below section is from my app.css file (mylaravelproject/public/css/app.css ).

@import url(https://fonts.googleapis.com/css?family=Raleway:300,400,600);

/*!
 * Bootstrap v4.0.0-beta.2 (https://getbootstrap.com)
 * Copyright 2011-2017 The Bootstrap Authors
 * Copyright 2011-2017 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

.jumbotron {
  padding: 2rem 1rem;
  margin-bottom: 2rem;
  background-color: #e9ecef;
  border-radius: 0.3rem;
}

@media (min-width: 576px) {
  .jumbotron {
    padding: 4rem 2rem;
  }
}

.jumbotron-fluid {
  padding-right: 0;
  padding-left: 0;
  border-radius: 0;
}

I doubt that there is something with the CSS, but can't figure out. Have I missed anything?
Answer: The problem was with the font-size. Size of the Jumbotron increased with font-size of text inside it.
This happens due to an accidental change of the variable $font-size-base: 14rem; inside  mylaravelproject/resources/assets/sass/_variable.scss file.
So,

I have to change the variable value to default. i.e. $font-size-base: 1rem;.
Once again compiled the whole installed packages: npm run dev

Now everything works fine!
Answer: It is beacause you are setting padding to class .jumbotron. This property increase size of .jumbotron element(1rem left side, 1rem right side).
Comment: Where I should make the change?


Problema ao setar um objeto de um ejb remoto

Question: Estou com problema ao usar um Set de um objeto de um EJB remoto, esse objeto será enviado para o mesmo EJB e lá fará uma conexão com o banco ou um webservice(Por ser uma aplicação da empresa não posso colocar o código aqui, por isso colocarei um exemplo).
Ex:
<code> ClasseDTODoEJB.setAtributo1("StringAleatoria");//Recebe o valor normalmente
 ClasseDTODoEJB.setAtributo2("StringAleatoria");//Causa um erro apesar de receber string e não possuir uma regra de negócio

 ClasseWebServiceDoEJB.ChamaWebService(ClasseDTODoEJB);//Classe que recebe o DTO pra fazer alguma tarefa ao qual não possuo acesso
</code>
Tentei criar um DTO igual na aplicação local e usar BeanUtils.CopyProperties() pra copiar o valor de um para ou outro porém os atributos não são passados corretamente apesar das duas classes serem iguais e a local estar recebendo todos os atributos. Isso pode ser algum erro no build path? O jar está declarado lá e no tomcat no shared.loader.
Answer: O problema estava no shared.loader do Tomcat, eu acabei esquecendo de adicionar o caminho desse EJB.


Practical thoughts on explanatory vs. predictive modeling

Question: Back in April, I attended a talk at the UMD (University of Maryland) Math Department Statistics group seminar series called "To Explain or To Predict?". The talk was given by Prof. Galit Shmueli who teaches at UMD's Smith Business School. Her talk was based on research she did for a paper titled "Predictive vs. Explanatory Modeling in IS Research", and a follow up working paper titled "To Explain or To Predict?".
Dr. Shmueli's argument is that the terms predictive and explanatory in a statistical modeling context have become conflated, and that statistical literature lacks a a thorough discussion of the differences. In the paper, she contrasts both and talks about their practical implications. I encourage you to read the papers.
The questions I'd like to pose to the practitioner community are:

How do you define a predictive exercise vs an explanatory/descriptive
one? It would be useful if you could talk about the specific
application.
Have you ever fallen into the trap of using one when meaning to use the other? I certainly have. How do you know which one to use?
Comment: Rather than saying "this should be closed. Someone should defend it" how about starting with explaining why you want it closed. Too vague? Then ask for clarification. This seems a reasonable question to me. The asker presents a paper and asks about the difference is between predictive and explanatory statistics. The only change I would make to the question is to clarify exactly the question thus making it easier to vote.
Comment: This question is strongly related to the matter of significance tests after generalization-error--based model selection. See:
http://stats.stackexchange.com/q/17825/6961
Comment: @Srikant @JD I'll beef up the question. Thanks for the feedback. I do think that this is a topic that merits discussion.
Comment: @rolando2 Although parts of this invite a range of answers, the core of the question asks for a distinction between a "predictive" and "explanatory/descriptive" exercise.  That potentially has an objectively correct answer.  CW doesn't look like an appropriate choice for this question.
Comment: I have already offered a reason on the meta thread. I feel that 'meta discussions' about the question would clutter up this particular page.
Comment: Your question would good for the community if, instead of telling us your life,  you could define what is (according to you) predictive model and explanatory model. I think nice debates start with clear definitions...
Comment: Could you pose a question here?  Is the question whether the paper is right?
Comment: @JD Perhaps. But, in order to get some control on the process of closing questions there is a meta thread devoted to this issue. If I do not make a mention of the fact that this question is proposed to be closed then the community will not get a chance to have a say whether it should stay open or not. One other issue is consider a person who stumbles on this question in the distant future. All this discussion about whether and why we should keep the question open is a bit irrelevant. I feel that comments should be used to clarify the question not to debate its merits.
Comment: Very productive question, given that we treat it as one with no correct answer.
Comment: @srikant you seem to not understand the nature of comments under the questions. They are, by definition, meta. They are not answers. They are not questions. They are meta. Having a convention where comments become a pointer to meta conversations in some other location is wasteful and silly.
Comment: @srikant. That's articulately stated and clear. This discussion probably belongs in the Meta area, however, as it is not specific to the question above. :) yeah, ok, that was a bit of a jackass comment... I could not resist it!   You make a good point. I think we can agree that @wahalulu needed to clarify his question. I think he's moving in the right direction.
Comment: possible duplicate of [Practical thoughts on explanatory vs predictive modeling](http://stats.stackexchange.com/questions/18896/practical-thoughts-on-explanatory-vs-predictive-modeling)
Comment: It seems me that here, and below, explanation and description are erroneously used as synonyms. Read my comments below on the reply of Prof Shmueli.
Answer: In one sentence
Predictive modelling is all about "what is likely to happen?", whereas explanatory modelling is all about "what can we do about it?"
In many sentences
I think the main difference is what is intended to be done with the analysis.  I would suggest explanation is much more important for intervention than prediction.  If you want to do something to alter an outcome, then you had best be looking to explain why it is the way it is.  Explanatory modelling, if done well, will tell you how to intervene (which input should be adjusted).  However, if you simply want to understand what the future will be like, without any intention (or ability) to intervene, then predictive modelling is more likely to be appropriate.
As an incredibly loose example, using "cancer data".
Predictive modelling using "cancer data" would be appropriate (or at least useful) if you were funding the cancer wards of different hospitals.  You don't really need to explain why people get cancer, rather you only need an accurate estimate of how much services will be required.  Explanatory modelling probably wouldn't help much here.  For example, knowing that smoking leads to higher risk of cancer doesn't on its own tell you whether to give more funding to ward A or ward B.
Explanatory modelling of "cancer data" would be appropriate if you wanted to decrease the national cancer rate - predictive modelling would be fairly obsolete here.  The ability to accurately predict cancer rates is hardly likely to help you decide how to reduce it.  However, knowing that smoking leads to higher risk of cancer is valuable information - because if you decrease smoking rates (e.g. by making cigarettes more expensive), this leads to more people with less risk, which (hopefully) leads to an expected decrease in cancer rates.
Looking at the problem this way, I would think that explanatory modelling would mainly focus on variables which are in control of the user, either directly or indirectly.  There may be a need to collect other variables, but if you can't change any of the variables in the analysis, then I doubt that explanatory modelling will be useful, except maybe to give you the desire to gain control or influence over those variables which are important.  Predictive modelling, crudely, just looks for associations between variables, whether controlled by the user or not.  You only need to know the inputs/features/independent variables/etc.. to make a prediction, but you need to be able to modify or influence the inputs/features/independent variables/etc.. in order to intervene and change an outcome.
Comment: +1, nicely done! I hate to nitpick, but I want to note that prediction doesn't have to be about the future.  For example, an archeologist may want to determine (i.e., predict) the level of rainfall in an area at a point in the past by knowledge of traces (i.e., effects of rainfall) that are left.
Comment: Nice answer. I think we need in many cases to know what the future looks like and why. Suppose, when studying customer churn, you want to know how many customer (and exactly which customer) churn next N month and then why they churn so marketing can intervene to retain them. Then we need both predictive (to learn future number and customers) and explanatory to tell us why, so we can reduce the churners. So, do we have hybrid model of both or one suffices? Varty  touches it up by saying "The known relationship may emerge from an explanatory/descriptive analysis or some other technique"
Comment: @gung - I thought I worded my response so that this didn't happen.  Clearly, I missed a spot :-)
Comment: @gung I *love* to nitpick: the archaeologist desires to predict experiences in *her* future (i.e., predicting where she will at some point in the future find traces of past high rainfall).
Comment: @Alexis, that's certainly possible, but it's also possible that that isn't the archaeologist's primary research interest, & that those data have already been gathered by other researchers (paleoclimatologists) & the archaeologist simply wants to use those data to test theories that are their primary theoretical interest ([Gill, 200](https://www.worldcat.org/title/great-maya-droughts-water-life-and-death/oclc/43567384)).
Answer: In my view the differences are as follows:
Explanatory/Descriptive
When seeking an explanatory/descriptive answer the primary focus is on the data we have and we seek to discover the underlying relationships between the data after noise has been accounted for.
Example: Is it true that exercising regularly (say 30 minutes per day) leads to lower blood pressure? To answer this question we may collect data from patients about their exercise regimen and their blood pressure values over time. The goal is to see if we can explain variations in blood pressure by variations in exercise regimen.
Blood pressure is impacted by not only exercise by wide variety of other factors as well such as amount of sodium a person eats etc. These other factors would be considered noise in the above example as the focus is on teasing out the relationship between exercise regimen and blood pressure.
Prediction
When doing a predictive exercise, we are extrapolating into the unknown using the known relationships between the data we have at hand. The known relationship may emerge from an explanatory/descriptive analysis or some other technique.
Example: If I exercise 1 hour per day to what extent is my blood pressure likely to drop? To answer this question, we may use a previously uncovered relationship between blood pressure and exercise regimen to perform the prediction.
In the above context, the focus is not on explanation, although an explanatory model can help with the prediction process. There are also non-explanatory approaches (e.g., neural nets) which are good at predicting the unknown without necessarily adding to our knowledge as to the nature of the underlying relationship between the variables.
Comment: +1 This reply largely avoids confusing association with causation by using the language of explanation, description, and relationship.  This lends it a desirable degree of clarity.
Comment: Under Explanation you wrote "the primary focus is on the data we have" -- I think you are trying to say that the task is retrospective (as opposed to the prospective nature of prediction). In explanation (read "causal explanation") there is actually a large focus on theory and domain knowledge and the data are used to test these assumptions/theories. In contrast, in prediction it is more data-driven and you are more open-minded about relationships, because you are not searching for causality but rather for correlation.
Comment: @varty I agree with your point: in explanation/description you are interested in an overall/average relationship/effect whereas in prediction you are interested in predicting individual values (not necessarily extrapolation)
Comment: @GalitShmueli Reg theory/domain knowledge- yes, I agree with that point. I was simply trying to contrast prediction vis-a-vis explanation by focusing on what seems to me the key distinction- extrapolating a variable's value vs unearthing the relationship between variables. In the process, I am of course guilty of neglecting subtle nuances between the two paradigms.
Answer: One practical issue that arises here is variable selection in modelling. A variable can be an important explanatory variable (e.g., is statistically significant) but may not be useful for predictive purposes (i.e., its inclusion in the model leads to worse predictive accuracy). I see this mistake almost every day in published papers.
Another difference is in the distinction between principal components analysis and factor analysis. PCA is often used in prediction, but is not so useful for explanation. FA involves the additional step of rotation which is done to improve interpretation (and hence explanation). There is a nice post today on Galit Shmueli's blog about this.
Update: a third case arises in time series when a variable may be an important explanatory variable but it just isn't available for the future. For example, home loans may be strongly related to GDP but that isn't much use for predicting future home loans unless we also have good predictions of GDP.
Comment: Why/how would an important explanatory variable reduce predictive accuracy?
Comment: @Srikant. This can happen when the explanatory variable has a weak but significant relationship with the response variable. Then the coefficient can be statistically significant but hard to estimate. Consequently, the MSE of predictions can increase when the variable is included compared to when it is omitted. (The bias is reduced with its inclusion but the variance is increased.)
Comment: A statistically sig. but weak predictor is rarely effective either for prediction or explanation.  E.g., if a linear regression solution has an RSQ of .40 without including the predictor X1, and if inclusion of X1 adds .01 to that RSQ, then X1 is "important" neither for prediction nor explanation.
Comment: forgive my ingorance, but isn't rotation normally a part of PCA as well as FA?
Comment: First paragraph is a very, very good point. Still sometimes is even worse; here PMID: 18052912 is a great example that sometimes a better model can be made on the noise part of the set than on a true one -- it is obvious that one can do a good model on random data, but this is a bit shocking.
Comment: Regardinging your first paragraph, you can show this by example - the mean square error of a prediction usually consists of two parts - parameter estimation uncertainty and unexplained variation uncertainty.  For OLS regression these components are given as $s^2x_i^T(X^TX)^{-1}x_i$ (parameter) and $s^2$ (unexplained).  Here $s^2$ is the estimated variance in the residuals and $X$ is the design matrix used to fit the model and $x_i$ is a vector of regressors for the predictions.
Comment: @rolando2 - this may be true for the quality of the prediction, but including a new variable may greatly affect the predictions themselves.
Comment: @probabilityislogic - Could you expand on that point? thx.
Comment: Above, when I wrote "'important' neither for prediction nor explanation," I neglected the case in which an important causal variable is almost completely predictable based on a set of other, less causal variables.
Comment: @rolando2 - my point was that including X1 may lead to different predictions.  So you may have $|\hat{y}_{X1}-\hat{y}_{not X1}|>>0$.  So although we expect both predictions to be similar distances to the "true value" (from similar $R^2$) we do not expect them to be close to each other. Additionally, if you have $1000$ data points, then increasing $R^2$ from $0.4$ to $0.41$ means a highly significant predictor (The "conservative" BIC is $10$ units smaller under the large model, or Bayes Factor of about $20000$ in favour of the larger model).
Comment: @probabilityislogic - thanks for your additional points.  I am more swayed by the first, which involves different substantive  conclusions about predicted values, than the second, which is about stat. sig. rather than anything substantive, if I am reading it correctly.
Answer: Although some people find it easiest to think of the distinction in terms of the model/algorithm used (e.g., neural nets=predictive), that is only one particular aspect of the explain/predict distinction. Here is a deck of slides that I use in my data mining course to teach linear regression from both angles. Even with linear regression alone and with this tiny example various issues emerge that lead to different models for explanatory vs. predictive goals (choice of variables, variable selection, performance measures, etc.)
Galit
Comment: Out of curiosity, is it intentional that in your discussion of regression for prediction (starting p. 33) you choose predictors (step 1) *before* partitioning into training and validation datasets (step 3)?  I have thought that the most objective and honest procedure would be to partition at the outset, even before looking at scatterplots (step 2).  If regressors are chosen based on the entire dataset, wouldn't that inflate apparent significance levels on many tests even when they are subsequently applied to the validation data?
Comment: I think the more general question is whether you perform data visualization before keeping a holdout. When the dataset is large, then it doesn't really matter. With a small sample, using visualization to choose predictors is indeed dangerous. In my slides I don't mean using visualization for variable selection. The "select predictors" is more generally "select a potential set of available predictors that are reasonable". It's more about incorporating domain knowledge for selecting a reasonable set.
Comment: Continuing the topic "To explain or to predict", I have a related question [here](http://stats.stackexchange.com/questions/177391/paradox-in-model-selection-aic-bic-to-explain-or-to-predict). I would appreciate if you took a look since the question is mostly based on your paper.
Comment: Professor Shmueli,  you say on page 291 of your paper on this topic that you are considering only 'nonstochastic prediction' as defined by Geisser, 1993.  Where would I go to find the full definition of nonstochastic prediction?  Glad to start a new post as well, but thought I would ask here first.
Comment: @GalitShmueli, Professor, your article was greatly useful for me. In my opinion It put light on several obscure points about statistical models and tools, regression above all. Thanks! However there is here a point not clear to me. Your article are focused on explanatory models, intended as causal, and predictive one. You only mention a third type of models, the descriptives: " Although not the focus of this article, a third type of modeling, which is the most commonly used and developed by statisticians, is descriptive modeling." pag 291.
Comment: However in the slides that you shared here explanation and description seems used as synonym. Why?
Moreover, I read another set of your slides in which the three concept are clearly distinct. I wrote a related question here (https://stats.stackexchange.com/questions/464261/regression-causation-vs-prediction-vs-description) your comments would be greatly appreciated.
Comment: @markowitz - thank you for re-alerting me to this thread. Here's a recent deck of slides that distinguishes between explanation/prediction/description, and specifically you'll see a regression example. https://www.slideshare.net/gshmueli/to-explain-to-predict-or-to-describe
Comment: @GalitShmueli, Thanks for have shared those recent deck of slide here. However I already read them, also for this reason I was convinced that explanation and description are not synonyms. Moreover I have yet some doubts about the role of description in regression. As already said in my previous comment (with link), I wrote a question about this. Your suggestions about that would be greatly appreciated.
Answer: Example: A classic example that I have seen is in the context of predicting human performance.
Self-efficacy (i.e., the degree to which a person thinks that they can perform a task well) is often a strong predictor of task performance. Thus, if you put self-efficacy into a multiple regression along with other variables such as intelligence and degree of prior experience, you often find that self-efficacy is a strong predictor.
This has lead some researchers to suggest that self-efficacy causes task performance. And that effective interventions are those which focus on increasing a person's sense of self-efficacy.
However, the alternative theoretical model sees self-efficacy largely as a consequence of task performance. I.e., If you are good, you'll know it. In this framework interventions should focus on increasing actual competence and not perceived competence.
Thus, including a variable like self-efficacy might increase prediction, but assuming you adopt the self-efficacy-as-consequence model, it should not be included as a predictor if the aim of the model is to elucidate causal processes influencing performance.
This of course raises the issue of how to develop and validate a causal theoretical model. This clearly relies on multiple studies, ideally with some experimental manipulation, and a coherent argument about dynamic processes.
Proximal versus distal: I've seen similar issues when researchers are interested in the effects of distal and proximal causes. Proximal causes tend to predict better than distal causes. However, theoretical interest may be in understanding the ways in which distal and proximal causes operate.
Variable selection issue: Finally, a huge issue in social science research is the variable selection issue.
In any given study, there is an infinite number of variables that could have been measured
 but weren't. Thus, interpretation of models need to consider the implications of this when making theoretical interpretations.
Comment: There is also a problem in social science of "weak hypothesis" (eg effect is positive vs negative).  And in that "self efficacy" example, you could view that as an internal predictor of performance that each person has built up.  So it is probably similar to using a "black box" prediction as an explanatory variable.
Answer: Statistical Modeling: Two Cultures (2001) by L. Breiman is, perhaps, the best paper on this point. His main conclusions (see also the replies from other prominent statisticians in the end of the document) are as follows:

"Higher predictive accuracy is associated with
more reliable information about the underlying data
mechanism. Weak predictive accuracy can lead to
questionable conclusions."
"Algorithmic models can give better predictive
accuracy than data models, and provide better information about the underlying mechanism."
Comment: Just to make a link with an earlier related question: [The Two Cultures: statistics vs. machine learning?](http://stats.stackexchange.com/q/6/930)
Comment: The problem with algorithmic models is that they are hard to understand.  This makes it hard to diagnose and fix potential problems that arise.  A structural model is much easier to assess because you know what each component should look like.
Answer: I haven't read her work beyond the abstract of the linked paper, but my sense is that the distinction between "explanation" and "prediction" should be thrown away and replaced with the distinction between the aims of the practitioner, which are either "causal" or "predictive". In general, I think "explanation" is such a vague word that it means nearly nothing. For example, is Hooke's Law explanatory or predictive? On the other end of the spectrum, are predictively accurate recommendation systems good causal models of explicit item ratings? I think we all share the intuition that the goal of science is explanation, while the goal of technology is prediction; and this intuition somehow gets lost in consideration of the tools we use, like supervised learning algorithms, that can be employed for both causal inference and predictive modeling, but are really purely mathematical devices that are not intrinsically linked to "prediction" or "explanation".
Having said all of that, maybe the only word that I would apply to a model is interpretable. Regressions are usually interpretable; neural nets with many layers are often not so. I think people sometimes naively assume that a model that is interpretable is providing causal information, while uninterpretable models only provide predictive information. This attitude seems simply confused to me.
Answer: I am still a bit unclear as to what the question is. Having said that, to my mind the fundamental difference between predictive and explanatory models is the difference in their focus.
Explanatory Models
By definition explanatory models have as their primary focus the goal of explaining something in the real world. In most instances, we seek to offer simple and clean explanations. By simple I mean that we prefer parsimony (explain the phenomena with as few parameters as possible) and by clean I mean that we would like to make statements of the following form: "the effect of changing $x$ by one unit changes $y$ by $\beta$ holding everything else constant". Given these goals of simple and clear explanations, explanatory models seek to penalize complex models (by using appropriate criteria such as AIC) and prefer to obtain orthogonal independent variables (either via controlled experiments or via suitable data transformations).
Predictive Models
The goal of predictive models is to predict something. Thus, they tend to focus less on parsimony or simplicity but more on their ability to predict the dependent variable.
However, the above is somewhat of an artificial distinction as explanatory models can be used for prediction and sometimes predictive models can explain something.
Comment: +1 for mentioning **complexity** which was not directly mentioned by the top answers.  However, the challenge arises when explanatory models are used for interventions.  How does one ensure that the estimated coefficients are not biased which is a common problem resulting from parsimony?
Answer: as others have already said, the distinction is somewhat meaningless, except in so far as the aims of the researcher are concerned.
Brad Efron, one of the commentators on The Two Cultures paper, made the following observation (as discussed in my earlier question):

Prediction by itself is
  only occasionally sufficient. The post
  office is happy with any method that
  predicts correct addresses from
  hand-written scrawls. Peter
  Gregory undertook his study for
  prediction purposes, but also to
  better understand the medical basis of
  hepatitis. Most statistical surveys
  have the identification of causal
  factors as their ultimate goal.

Certain fields (eg. Medicine) place a heavy weight on model fitting as explanatory process (the distribution, etc.), as a means to understanding the underlying process that generates the data. Other fields are less concerned with this, and will be happy with a "black box" model that has a very high predictive success. This can work its way into the model building process as well.
Answer: With respect, this question could be better focused.  Have people ever used one term when the other was more appropriate?  Yes, of course.  Sometimes it's clear enough from context, or you don't want to be pedantic.  Sometimes people are just sloppy or lazy in their terminology.  This is true of many people, and I'm certainly no better.
What's of potential value here (discussing explanation vs. prediction on CV), is to clarify the distinction between the two approaches.  In short, the distinction centers on the role of causality.  If you want to understand some dynamic in the world, and explain why something happens the way it does, you need to identify the causal relationships amongst the relevant variables.  To predict, you can ignore causality.  For example, you can predict an effect from knowledge about its cause; you can predict the existence of the cause from knowledge that the effect occurred; and you can predict the approximate level of one effect by knowledge of another effect that is driven by the same cause.  Why would someone want to be able to do this?  To increase their knowledge of what might happen in the future, so that they can plan accordingly.  For example, a parole board may want to be able to predict the probability that a convict will recidivate if paroled.  However, this is not sufficient for explanation.  Of course, estimating the true causal relationship between two variables can be extremely difficult.  In addition, models that do capture (what are thought to be) the real causal relationships are often worse for making predictions.  So why do it, then?  First, most of this is done in science, where understanding is pursued for its own sake.  Second, if we can reliably pick out true causes, and can develop the ability to affect them, we can exert some influence over the effects.
With regard to the statistical modeling strategy, there isn't a large difference.  Primarily the difference lies in how to conduct the study.  If your goal is to be able to predict, find out what information will be available to users of the model when they will need to make the prediction.  Information they won't have access to is of no value.  If they will most likely want to be able to predict at a certain level (or within a narrow range) of the predictors, try to center the sampled range of the predictor on that level and oversample there.  For instance, if a parole board will mostly want to know about criminals with 2 major convictions, you might gather info about criminals with 1, 2, and 3 convictions.  On the other hand, assessing the causal status of a variable basically requires an experiment.  That is, experimental units need to be assigned at random to prespecified levels of the explanatory variables.  If there is concern about whether or not the nature of the causal effect is contingent on some other variable, that variable must be included in the experiment.  If it is not possible to conduct a true experiment, then you face a much more difficult situation, one that is too complex to go into here.
Comment: I wonder about the role of causality.  For instance, suppose we have a dataset of the dimensions and volumes of a set of blocks, $(x,y,z,v)$, and we are modeling their association by regressing $z$ on $(x,y,v)$ (and interactions thereof).  In what sense can it be said that two dimensions and a volume "cause" the third dimension?  The distinction, therefore, between explanation and prediction appears to be based on something fundamentally different; namely, the *purpose* of the analysis.  Re your last paragraph, there are many accounts on this site attesting to sharp differences in strategy.
Comment: You're right this hinges on the purpose of the study. I suppose I didn't make that explicit (I only talked about what you *want* to achieve). It's also true that explanation needn't be concerned with causality exactly--something analogous to causality fits as well (e.g., the dimensions - volume case is one of logical / mathematical implication). However, most explanatory modeling centers on causality; I guess I thought I could skip that sort of thing for the sake of simplicity. Finally, strategy does differ during study design & data collection, but regressing y on x is pretty much the same.
Comment: @whuber - a good example of soft causailty is "smoking causes cancer" -though i'm sure you could find a chain smoker who doesn't have cancer.  The notion of causality is inter-linked with the timing of events.  The cause must happen before the effect - which explains why the cube example makes no sense.
Comment: Thank you for the reply.  From other exchanges on this site I have learned to understand universal statements like "most explanatory modeling centers on causality" to reflect the writer's background and experience, rather than as being literally true.  In the physical and "hard" sciences this statement may be correct, but in the social and "soft" sciences I doubt practitioners would make such a strong claim.  Often, in fact, the relationships under study are believed to have common hidden causes but do not reflect direct causation between regressors and the regressand.
Comment: @whuber it's certainly true that my ideas are influenced by my background and experience.  If this answer is not useful (I notice that is hasn't gotten any votes), I can delete it.  A number of others have provided answers that cover the ideas I meant to convey.
Answer: Most of the answers have helped clarify what modeling for explanation and modeling for prediction are and why they differ.  What is not clear, thus far, is how they differ.  So, I thought I would offer an example that might be useful.
Suppose we are intereted in modeling College GPA as a function of academic preparation.  As measures of academic preparation, we have:

Aptitude Test Scores;
HS GPA; and
Number of AP Tests passed.

Strategy for Prediction
If the goal is prediction, I might use all of these variables simultaneously in a linear model and my primary concern would be predictive accuracy.  Whichever of the variables prove most useful for predicting College GPA would be included in the final model.
Strategy for Explanation
If the goal is explanation, I might be more concerned about data reduction and think carefully about the correlations among the independent variables.  My primary concern would be interpreting the coefficients.
Example
In a typical multivariate problem with correlated predictors, it would not be uncommon to observe regression coefficients that are "unexpected".  Given the interrelationships among the independent variables, it would not be surprising to see partial coefficients for some of these variables that are not in the same direction as their zero-order relationships and which may seem counter intuitive and tough to explain.
For example, suppose the model suggests that (with Aptitude Test Scores and Number of AP Tests Successfully Completed taken into account) higher High School GPAs are associated with lower College GPAs.  This is not a problem for prediction, but it does pose problems for an explanatory model where such a relationship is difficult to interpret.  This model might provide the best out of sample predictions but it does little to help us understand the relationship between academic preparation and College GPA.
Instead, an explanatory strategy might seek some form of variable reduction, such as principal components, factor analysis, or SEM to:

focus on the variable that is the best measure of "academic
performance" and model College GPA on that one variable; or
use factor scores/latent variables derived from the combination
of the three measures of academic preparation rather than the
original variables.

Strategies such as these might reduce the predictive power of the model, but they may yield a better understanding of how Academic Preparation is related to College GPA.
Comment: Regarding the counter-intuitive sign, i wonder if it is because our intuition is interpretting the wrong covariate - like a main effect as if it was nested or interaction effect.
Answer: I would like to offer a model-centered view on the matter.
Predictive modeling is what happens in most analyses. For example, a
researcher sets up a regression model with a bunch of predictors. The
regression coefficients then represent predictive comparisons between
groups. The predictive aspect comes from the probability model: the
inference is done with regard to a superpopulation model which may
have produced the observed population or sample. The purpose of this
model is to predict new outcomes for units emerging from this
superpopulation. Often, this is a vain objective because things are
always changing, especially in the social world. Or because your model
is about rare units such as countries and you cannot draw a new
sample. The usefulness of the model in this case is left to the
appreciation of the analyst.
When you try to generalize the results to other groups or future
units, this is still prediction but of a different kind. We may call
it forecasting for example. The key point is that the predictive power
of estimated models is, by default, of descriptive nature. You
compare an outcome across groups and hypothesize a probability model
for these comparisons, but you cannot conclude that these comparisons
constitute causal effects.
The reason is that these groups may suffer from selection bias. Ie,
they may naturally have a higher score in the outcome of interest,
irrespective of the treatment (the hypothetical causal
intervention). Or they may be subject to a different treatment effect
size than other groups. This is why, especially for observational
data, the estimated models are generally about predictive
comparisons and not explanation. Explanation is about the
identification and estimation of causal effect and requires well
designed experiments or thoughtful use of instrumental variables. In
this case, the predictive comparisons are cut from any selection bias
and represent causal effects. The model may thus be regarded as
explanatory.
I found that thinking in these terms has often clarified what I was
really doing when setting up a model for some data.
Comment: Well, in my view, most modeling of observational data is predictive, even if the aim is explanatory. If you do not randomize the attribution of the treatment and actually induce a change in an experimental setup, your regression coefficients will only have a descriptive value, ie, they only provide the means to predictive comparisons. For example, you can predict success at school based on demographic characteristics but that does not mean that these demographics are explanatory causal effects. The reason is that comparative predictions are exposed to selection bias.
Comment: +1, there's good information here. I would be cautious regarding the statement "Predictive modeling is what happens in most analyses", however. Whether or not predictive modeling is more common will vary by discipline, etc. My guess would be that most modeling in academia is explanatory, & that a lot of modeling / data mining that is done in the private sector (eg identify potential repeat customers) is predictive. I could easily be wrong, but it will be tough to say, a priori, which happens most of the time.
Answer: We can learn a lot more than we think from Black box "predictive" models. The key is in running different types of sensitivity analyses and simulations to really understand how model OUTPUT is affected by changes in the INPUT space. In this sense even a purely predictive model can provide explanatory insights. This is a point that is often overlooked or misunderstood by the research community. Just because we do not understand why an algorithm is working doesn't mean the algorithm lacks explanatory power...
Overall from a mainstream point of view, probabilityislogic's succinct reply is absolutely correct...
Comment: It is unclear what "explanatory insights" can be gleaned in this way, if by that phrase you imply causality.
Answer: There is distinction between what she calls explanatory and predictive applications in statistics. She says we should know every time we use one or another which one exactly is being used. She says we often mix them up, hence conflation.
I agree that in social science applications, the distinction is sensible, but in natural sciences they are and should be the same. Also, I call them inference vs. forecasting, and agree that in social sciences one should not mix them up.
I'll start with the natural sciences. In physics we're focused on explaining, we're trying to understand how the world works, what causes what etc. So, the focus is on causality, inference and such. On the other hand, the predictive aspect is also a part of the scientific process. In fact, the way you prove a theory, which already explained observations well (think of in-sample), is to predict new observations then check how prediction worked. Any theory that lack predictive abilities will have big trouble gaining acceptance in physics. That's why experiments such as Michelson-Morley's are so important.
In social sciences, unfortunately, the underlying phenomena are unstable, unrepeatable, unreproducible. If you watch nuclei decay you'll get the same results every time you observe them, and the same results that I or a dude one hundred years ago got. Not in economics or finance. Also, the ability to conduct experiments is very limited, almost non existent for all practical purposes, we only observe and conduct random samples of observations. I can keep going on but the idea's that the phenomena that we deal with are very unstable, hence our theories are not of the same quality as in physics. Therefore, one of the ways we deal with the situation is to focus on either inference (when you try to understand what causes what or impact what) or forecasting (just say what you think will happen to this or that ignore the structure).
Answer: A Structural Model would give explanation and a predictive model would give prediction. A structural model would have latent variables. A structural model is a simultaneous culmination of regression and factor analysis
The latent variables are manifested in the form of  multi collinearity in predictive models (regression).
Answer: Explanatory model has also been used in medicine and the health area as well, with a very different meaning. Basically what people have as internal beliefs or meanings can be quite different from accepted explanations. For example a religious person may have an explanatory model that an illness was due to punishment or karma for a past behaviour along with accepting th biological reasons as well.
https://thehealthcareblog.com/blog/2013/06/11/the-patient-explanatory-model/
https://pdfs.semanticscholar.org/0b69/ffd5cc4c7bb2f401be6819c946a955344880.pdf
Comment: Could you please give at least a summary of what's to be found at these links? - else this is more of a comment than an answer.
Comment: I didn't think people would be interested. Since this is quite a mathsy group. So I kept my answer limited.


How to use date_make_date() in drupal 7?

Question: I am porting custom module from drupal 6 to drupal 7
I have the following code:
<code> $local_date = new DateTime($dateTxt);
 $iso_date_string = date_format_date($local_date, 'custom', 'Y-m-d\TH:i:s');
 $utc_date = date_make_date($iso_date_string, $user_timezone_name, DATE_DATETIME);
</code>
What can be the alternative for date_make_time() in drupal 7?
https://www.drupal.org/node/1075476

this error is due to a change in the Date API module, instead of
  date_make_date you need to use the new DateObject class. see "D7
  Changes" here: http://drupal.org/project/date
I fixed this as follows, but not sure if I'm using the DateObject
  properly.
Edit this block of code in agenda.module, line 354

<code>  // If the Date API is installed, we can do better at DST times   if (module_exists('date_api')) {
    $start               = date_make_date($event['start timestamp'], NULL, DATE_UNIX);
    $event['start time'] = date_format_date($start, 'custom', $block->timeformat);
    $end                 = date_make_date($event['end timestamp'], NULL, DATE_UNIX);
    $event['end time']   = date_format_date($end, 'custom', $block->timeformat);   }
</code>
replace with
<code>  // If the Date API is installed, we can do better at DST times   if (module_exists('date_api')) {
    $start               = new DateObject($event['start timestamp'], NULL);
    $event['start time'] = $start->format($block->timeformat);
    $end                 = new DateObject($event['end timestamp'], NULL);
    $event['end time']   = $end->format($block->timeformat);   } alternatively, just comment out the whole block.
</code>
I looked in the above node but not sure about the new DateObject() ?
new DateObject()
Comment: It's declared in the date_api.module file...
Answer: The alternative to the date_make_date function in D7 is the DateObject, which extends the PHP DateTime class.
To make your code work properly, you'd do something like this (untested):
<code>$local_date = new DateTime($dateTxt, $timezone); //$timezone can be string or object
$iso_date_string = $local_date->toIso(); //returns the ISO string for the date
$utc_date = clone $local_date; //don't want pass by reference
$utc_date->setTimezone(new DateTimeZone('UTC')); //set the new timezone to return
$utc_date_iso = $utc_date->toIso(); //returns $local_date converted to UTC as an ISO string.
</code>
I'm not totally sure what you want to do, but this, hopefully, will get you on the right path.


M/Monit how to see current disk space?

Question: In the admin interface of M/Monit under Reports -> Analytics I can chose to show Space %.

How can I make the Monit clients submit this info?
Is there a way to display Disk Space percentage on the Status page besides %Cpu and %Mem?

I have found that I can set alerts if disk space is running out (will do that, but would like to have an overview as well).
Answer: You can see this information on the Reports -> Analytics page.

Your Monit config needs to have disk checks for the filesystems you require:
check device root with path /
    if SPACE usage > 80% then alert

check device var with path /var
    if SPACE usage > 80% then alert

check device usr with path /usr
    if SPACE usage > 80% then alert

check device tmp with path /tmp
    if SPACE usage > 80% then alert

There's no way to add disk space to the overview graph. It wouldn't make sense from a design or UI perspective.
Comment: Thx! Quick answer :-) Got it to work. A bit confusing that I have to set an alert to monitor a specific info. Why doesn't it make sense in the UI? It would fit easily in just another column.

M/Monit how to see current disk space?


entering text into UI textfield by pressing return button

Question: let's say I want to enter some data into text field, text for example, I want to press done or return button in keyboard to enter data into UI text field.
how I can do that ?
Answer: There is a way to do this without implementing UITextFieldDelegate. You can add "target" for your UITextField which will call a method as user returns. Something like this:
    func init(){
        //Initialize everything else

        textField.addTarget(self, action: "textFieldReturned", forControlEvents: UIControlEvents.EditingDidEndOnExit)
    }

    //This method will be called after user "returns".
    //Note: Name this method whatever you like, just make sure it's
    //the same name you pass to 'addTarget' method as 'action:' parameter
    func textFieldReturned(){
        textField.resignFirstResponder() //Hides keyboard

        //Do everything you need after user presses "Done" button
    }

Answer: This should be as simple as using the UITextFieldDelegate to process when the return key has been pressed.
So you can adopt that protocol, assign the delegate on your UITextField and then process data in the delegate method textFieldDidEndEditing: when the return key is pressed. The delegate method I mentioned can be found here. Make sure you have enabled the processing of the return key by returning true from textFieldShouldReturn:.


Separation of two disjoint closed subsets in a compact Hausdorff space.

Question: Suppose $X$ is a compact Hausdorff space and $C,D$ are disjoint closed subsets of $X$. I want to show that there exist open disjoint $U,V$ with $C\subseteq U, D\subseteq V$.
Since $C,D$ are disjoint, we can take a point $c\in C$ and for any point $d\in D$ find a pair of disjoint open sets $U_{c,d}, V_{c,d}$ such that $c\in U_{c,d}, d\in V_{c,d}$. Taking the union of $U_{c,d}$'s we can form an open cover of $C$:
$$
C\subset \bigcup_{c\in C, d\in D} U_{c,d}
$$
It is also possible to get an open cover of $D$ in a similar way, but I don't know how to use compactness or the fact that the sets $C,D$ are closed.
Comment: Start by proving that you can separate a point from a closed set and, then, use this result to prove what you want.
Answer: Do it in two steps. What you're trying to show is that every compact Hausdorff space is normal; start by showing that every compact Hausdorff space is regular.
Let $X$ be a compact Hausdorff space, $C$ a closed subset of $X$, and $p$ a point not in $C$. $X$ is Hausdorff, so for each $x\in C$ there are open sets $U_x$ and $V_x$ such that $p\in U_x$, $x\in V_x$, and $U_x\cap V_x=\varnothing$. Let $\mathscr{V}=\{V_x:x\in C\}$; clearly $\mathscr{V}$ is an open cover of $C$. $C$, being a closed subset of a compact space, is compact, so there is a finite $F\subseteq C$ such that $=\{V_x:x\in F\}$ covers $C$. Let $U=\bigcap_{x\in F}U_x$ and $V=\bigcup_{x\in F}V_x$; then $U$ and $V$ are open, $p\in U$, $C\subseteq V$, and $U\cap V=\varnothing$.
Now let $C$ and $D$ be disjoint closed subsets of $X$. For each $x\in D$ there are open sets $U_x$ and $V_x$ such that $x\in U_x$, $C\subseteq V_x$, and $U_x\cap V_x=\varnothing$. Use a slight modification of the argument in the preceding paragraph to find disjoint open nbhds of $C$ and $D$.
Answer: You can use the fact that closed subsets of a compact Hausdorff space are themselves compact. Then you can construct a finite collection of subsets $U_c$, so that each $U_c$ is disjoint from every other $V_d$. The finiteness of each collection allows you to separate each of the $U_c$'s from each of the $V_d$'s, so that the respective unions are disjoint from each other.


The "--" operator for list subtraction in Scala

Question: In "S-99: Ninety-Nine Scala Problems" they use -- on a List in graph's equals method. The problem is, that in Scala I use (2.10.2), the -- operator isn't present (or I'm missing some import).
scala> List(1) -- List(1)
<console>:8: error: value -- is not a member of List[Int]
              List(1) -- List(1)
                      ^

Expected result is empty list.
In older versions of Scala it was working fine (according to this post).
Is there a subtract operator for Lists in Scala's standard library or do I need to cook one myself?
Answer: scala> List(1,2,3,4) filterNot List(1,2).contains
res2: List[Int] = List(3, 4)

or
scala> List(1,2,3,4) diff List(1,2)
res3: List[Int] = List(3, 4)

Comment: Note that these two snippets aren't equivalent (try with `List(1,1,2,3,4)`)


Euler's formula question

Question: I saw this formula in my book:
$$2i \sin(w)=e^{iw}-e^{-iw}$$
but I don't understand why this is true? isn't $\,\sin(w)=\frac{e^{iw}-e^{-iw}}{2}$?
Answer: No. It's$$\sin(w)=\frac{e^{iw}-e^{-iw}}{2i}.\tag1$$Therefore, that formula from your book is correct.
If you want to know why we have $(1)$, note that$$e^w=1+\frac w1+\frac{w^2}{2!}+\frac{w^3}{3!}+\cdots$$So,$$e^{iw}=1+i\frac w1-\frac{w^2}{2!}-i\frac{w^3}{3!}+\cdots$$and$$e^{-iw}=1-i\frac w1-\frac{w^2}{2!}+i\frac{w^3}{3!}+\cdots$$and so\begin{align}e^{iw}-e^{-iw}&=2i\left(\frac w1-\frac{w^3}{3!}+\frac{w^5}{5!}-\cdots\right)\\&=2i\sin(w).\end{align}
Comment: @Sebastiano Thank you.
Comment: Very nice explanation.
Answer: If you're not sure you can do the proof:
We know that : $e^{iw} =\cos{w} +i\sin{w}$ and $e^{-iw} =\cos{w} -i\sin{w}$
If you do the difference of those two terms, you'll find the first formula.

Euler's formula question


Classifying Hausdorff spaces

Question: Can a topological space be Hausdorff and separable, but neither Lindelof nor first countable?
Can a topological space be Hausdorff and Lindelof, but neither separable nor first countable?
Can a topological space be Hausdorff, but not separable, Lindelof, or first countable?
Are all second countable spaces Hausdorff?
Sorry for posting several questions in one post, but I am just trying to figure out how the Hausdorff property relates to Lindelof, Separability, and First Countability in topological spaces.
Comment: @NateEldredge . As a professional nitpicker, I must point out that the trivial topology on a set with less than 2 members is the only topology on it, and it IS Hausdorff.
Comment: The last question is particularly easy (at least in the sense that I already know the answer): let $X = \mathbb{R} \times \{0,1\}$ (where $\{0,1\}$ has the discrete topology) modulo the equivalence relation generated by $(x,0) \sim (x,1)$ for all $x \in \mathbb{R} \setminus \{0\}$. Then $X$ is second-countable but not Hausdorff ($X$ is known as the "line with doubled origin").
Comment: Or even easier: the trivial (indiscrete) topology on any set.
Comment: Separable & Lindelof & 2nf-countable are "global" properties but 1st-countable is "local".  If you take the topological sum of a large family of spaces you can get a space with arbitrarily large "global" values without disturbing the "local" values of the members of the family. You can find much about this in various places in General Topology by R. Engelking (which has a thorough index) under the topic Cardinal Functions.
Answer: For questions like this, you probably want to get a copy of the book Counterexamples in Topology, by Steen and Seebach. The "general reference chart" in the back of that book allows you to search the counterexamples by combinations of properties they do and don't satisfy.
Fortunately, this chart (and more) now exists in an online searchable form: $\pi$-Base.

Can a topological space be Hausdorff and separable, but neither
Lindelof nor first countable?

Yes: The strong ultrafilter topology.

Can a topological space be Hausdorff and Lindelof, but neither
separable nor first countable?

Yes: Many examples, including the ordinal space $[0,\omega_1]$ and the countable complement extension topology (the topology generated by the standard topology on $\mathbb{R}$ together with the countable complement topology on $\mathbb{R}$).

Can a topological space be Hausdorff, but not separable, Lindelof, or
first countable?

Yes: Many examples, including the product topology on a product of uncountably many copies of an infinite discrete set.

Are all second countable spaces Hausdorff?

No. Many examples, including the cofinite topology on $\mathbb{N}$.
Comment: +1.... For the 3rd Q we can also, from the 1st or 2nd Q take a Hausdorff space X that's not 1st-countable and then  take the topological sum of uncountably many copies of X.
Answer: Looking in large products (easy way to get non-first countable spaces with varying properties):

$\mathbb{R}^\mathbb{R}$ in the product topology is separable, Hausdorff (even Tychonoff), but not Lindelöf, nor first countable.
$[0,1]^I$ (product topology) is not separable if $|I| > |\mathbb{R}|$, Hausdorff, compact (so certainly Lindelöf) and not first countable for uncountable $I$.
$\mathbb{R}^I$ with $|I| > |\mathbb{R}|$ is Tychonoff, not separable, not normal (so certainly not Lindelöf as Lindelöf plus regular implies normal) and not first countable.

The final question on non-Hausdorff spaces is different (most "natural" examples as above are Tychonoff, so Hausdorff), and there even the indiscrete topology suffices as an example, which is trivially second countable.
Comment: +1... For Q # 3, the box-product topology on $\Bbb R^{\aleph_0}$ is another  Tychonoff example.
Comment: @DanielWainfleet Yes, nice one too. (We cannot eliminate Lindelöf via the normality route (at least, that's open in ZFC) but $\{0,1\}^{\aleph_0}$ is uncountable closed and discrete so that does it.). And then $|X|=\mathfrak{c}$ whch is a bonus).
Comment: It seems oddly irritating to me that the normality of the box product topology on $\Bbb R^{\aleph_0}$ is still unknown. It just doesn't feel like a hard Q. Maybe someone will find a Forcing argument concerning it.

Classifying Hausdorff spaces


The line $x = a$ is a vertical asymptote of the graph of the function$ y = f(x) $ if at least one of the following statements is true:

Question:
The line $x = a$ is a vertical asymptote of the graph of the function$ y = f(x) $ if at least one of the following statements is true:
$$\lim_{x\to a^+} f(x)=\pm \infty$$
$$\lim_{x\to a^-} f(x)=\pm \infty$$

Now how to recognize point $a$ .
for ex :
$$f(x)=\frac{\tan x+2}{\cos x+1}$$
$$\lim_{x\to a^{\pm}} \frac{\tan x+2}{\cos x+1}=\pm \infty$$
what is $a$ ?
Answer: $$f(x) \to \pm \infty \implies \frac{1}{f(x)} \to 0$$
Thus we need -
$$\frac{\cos x+1}{\tan x+2}\to 0 \implies \cos x+1\to 0 \implies x \to 2k\pi \pm \pi $$
Thus, $a=2k\pi \pm \pi$
Comment: do for all functions We have to write :?

$$f(x) \to \pm \infty \implies \frac{1}{f(x)} \to 0$$
Comment: @Almot1960 Yes, it's true for all kind of functions. You can think this logically too. We need the denominator to be tending to $0$ so that the function's value become so large that it tends to infinity.


linear mixed model - post hoc tests for categorical variables against fixed value null-hypothesis

Question: I'm fitting an LME (with lmer in R) with one categorical variable that has many (80) different values. A fitting example for my problem would be how weight loss after fasting is distributed across different body parts and organs. I.e. my model would be: weight change ~ 1+ bodypart + (1|subject)
A likelihood ratio test tells me that it makes sense to include the categorical variable as predictor.
Now my key question is at what values of the predictor there is a significant/relevant deviation from 0, i.e. in which organs is there a significant change in weight? I first thought the estimates of the predictors for the individual levels of the categorical variables will tell me this. However, if I understand correctly this would only be true if the intercept would be 0.
My intuition is that perhaps estimated marginal means (ie. using emmeans in R) could help me here but I would not know how to ask the question correctly (in R and in general).
Answer: Ben is correct. But if interested, the emmeans way is
emm <- emmeans(model, "bodypart")
contrast(emm, "eff")

This compares each EMM with the grand mean. If you have more than one fixed factor in the model, this'd be the way to go.
Comment: are `~ bodypart` and `"bodypart"` equivalent as second arguments?  (BTW, you have smart-quotes in your answer, which would mess up cutting & pasting - was that intentional?)
Comment: Sorry about the smart quotes. My phone is definitely not smarter than me, though I guess it's catching up as I get older. Yes, the formula and character specs are equivalent.
Answer: Yes emmeans can do this via emmeans(fitted_model, ~ bodypart). However, in this particular case you can get the same results by fitting the model
weight_change ~ 0 + bodypart + (1|subject)

(a -1 would work just as well in place of 0); this instructs R to suppress the model intercept, and in this case would correspond to  the model $\Delta w_{ij} = \beta_i + \epsilon_{1,j} + \epsilon_{2,ij}$ where $j$ denotes subject and $i$ denotes body part.
As a little bit of a frame challenge, you might want to consider treating body part as a random effect; that would make it harder to do classical inference about individual body parts (e.g. get p-values), but it might lead to more accurate estimates and could get you around some of the multiple-testing challenges you'll get from looking at 80 separate hypothesis tests. (If your data set is huge it might not make that much difference.)


During times when a maintenance window overlaps with business hours does SCCM perform operations?

Question: SCCM administrators set maintenance windows for devices.  Users set business hours for the same devices.  It seems inevitable that maintenance windows and business hours will overlap.  During that overlap, will SCCM perform the operations that support maintenance windows?
This technet blog post is the best resource I have found for understanding maintenance windows and business hours.  It is, however, silent on the matter of overlap between the two.
Answer: From my understanding; (fairly new to SCCM 2012, been working with SCE 2010 for a while though) the elements in play here:
The Business Scenario
Let's say you are trying to deploy "Java 8 Update xx" to end user machines in a single device collection. Why? Because Java.
The two in question:
Business Hours: This is a rotating weekly calendar which the end user is allowed to configure. I.E. end-user Mary wants all software updates and installations to occur when she leaves the office at 5:00 PM on the days she works Monday through Friday.
She will go into the Software Center console and set her Business Hours like...

Monday       8:00 AM - 5:00 PM
Tuesday      8:00 AM - 5:00 PM
Wednesday    8:00 AM - 5:00 PM
Thursday     8:00 AM - 5:00 PM
Friday       8:00 AM - 3:30 PM (Mary likes to take off early on Fridays)
Saturday     none
Sunday       none

We will call this $BizWindow, it will evaluate to TRUE during the hours specified and FALSE when outside of the set time frame.
Maintenance Windows:  This can be a Monthly, Weekly, or Daily schedule where the administrator is allowed to set a range of hours when an installation will be available to computers. These settings are configured at the Device Collection level.

Daily FROM 4:00 PM to 8:00 PM

We will call this $MaintWindow, it will evaluate to TRUE when it is 4:00 PM - 8:00 PM every single day.
Necessary Conditions:
So for the software to begin installing on the client device, the computer needs to meet two conditions:
1.) It is not in the Business Hours
2.) It is in the Maintenance Window
I.E.
<code>     $BizWindow = FALSE && $MaintWindow = TRUE
</code>
So for the above example
Mary's computer will install Java during:

5:00 PM - 8:00 PM Monday - Thursday
4:00 PM -8:00 PM Friday (Because of Mary's slightly shorter day on Friday)

Honorable Mentions
When publishing Apps and Packages, there is a way to supersede any sort of Maintenance Window and Business Hours rules in the deployment settings. Then the software will become available/install immediately after it has been distributed to the respective DP's and the SCCM client updates the list.
Hope that helps.
Comment: Did you test the "necessary conditions"?  [I'm pretty sure they're wrong](https://serverfault.com/a/700347/115232).
Comment: I am curious now that you mention that, because I have never had to test those conditions. It has just always worked the way I intended when I operated under those assumptions. I am curious though @alx9r; on your chart, when you say TRUE for InBusHours; do you mean, when Business Hours are TRUE as defined by the user (or defaults)?
Comment: When I ran the tests, the business hours had not been changed from the defaults.  Why would that make a difference?
Comment: Because there are a couple of special conditions in which SCCM will not respect Business Hours and begin an install regardless of whether or not the Business Hours are set and you **are** in them. Otherwise, SCCM will respect the wishes of the end user and **not** install anything when you **are** in Business Hours. If you could start a quick chat, I'd love to get to the bottom of this.
Comment: What are the "special conditions"?
Comment: For instance (I'm pretty sure there are other examples but I can't think of them now): an application/package deployment targeted at your collection reaches it's installation deadline; then it will install in the maintenance window *regardless* of when the Business Hours are set. This will happen if you configure it to in the deployment properties on a per-package basis.
Answer: Empirical Results
I just ran a few tests to see how SCCM behaves.  The results are as follows:
<code>Test InBusHours InMaintWin UserLoggedIn OpsRun
---- ---------- ---------- ------------ ------
1          True      False        False  False
2          True       True        False   True
3          True       True         True   True
</code>
Note: All of the above tests were run after the "available time" with the installation deadline set to "As soon as possible after the available time."
The rule seems be as follows:

During times when business hours and maintenance windows overlap, operations that support maintenance windows are performed.


Limit question as $x$ and $y$ approach infinity?

Question: I have to prove that the limit of the function $\frac{x^2}{x^2+y^2}$ as $x$ approaches infinity and as y approaches infinity does not exist.
I thought about finding the side limits, and if they are not equal, bam! I have solved it. But what should I take as side limits here? $+$ and $-$ infinity? Thank you in advance :)
Comment: For two-dimensional limits, there are not just 2 sides.
Comment: Try looking as $x$ goes to infinity, then as $y$ goes to infinity, and vice versa.
Comment: @rschwieb, I don't think that's possible: both variables must go infinity, this or that way or rate, but both at the same time.
Comment: But the paths you gave @rschwieb are forbidden in this case as *both* variables must go to infinity. If you had chosen $\,y=x^2\;,\;x=3y-8\,$ or something else then fine, but not when you *fix* one variable that should be running to infinity
Comment: @DonAntonio Yes I see what you mean. I had some picture in my head about observing different paths "away from the origin", but I think you're right.
Comment: I think I understand: while writing down my answer I almost proposed $\,y=0\,\,,\,\,x\to\infty\,$ ...
Answer: $$(1)\;\;\;\;y=x\;\;\;\;\Longrightarrow\;\;\; \lim_{x,y\to\infty}\frac{x^2}{x^2+y^2}=\lim_{x\to\infty}\frac{x^2}{2x^2}=\frac{1}{2}$$
$$(2)\;\;\;\;\;\;\;\;\;\;\;\;\;y=x^2\;\;\;\;\Longrightarrow\;\;\; \lim_{x\to\infty}\frac{x^2}{x^2+x^4}=0$$
Comment: Gracias amigo :)
Comment: DonAntonio. I just got confused. what about if $x,y\to \infty$ independently,  means that there is no relationship between $x$ and $y$? because the cases that you considered $y-x=0$, means that, $x$ and $y$ are dependent.
Comment: @00GB I'm not sure what you mean "independently": it must be that both $\;x,\,y\;$ go to infinity together, otherwise you may be talking about iterated limits, which are not usually a good idea to evalaute the real limit. In the cases above, there's a relation between $\;x,\,y\;$ in which both variables tend to infinity whenever one of them does. That's enough to show the limit doesn't exist.
Comment: @DonAntonio, find $\lim \frac{x}{y}$ as $x,y\to \infty$ independently.
Comment: Well, when $\;y\to\infty\;$ but not $\;x\;$ (which can be any constant), the limit is obviously zero. When $\;x\to\infty\;$, and $\;y\;$ any non-zero constant, the limit is $\;\pm\infty\;$, depending on the sign of $\;y\;$ . This already shows the limit $$\lim_{(x,y)\to (\infty,\infty)} \frac xy\;$$ doesn't exist...$$
Answer: ok at first we say we observe $x\to \infty$ and $x=y$ than the limit is the same as
$$\lim_{x\to \infty}\frac{x^2}{2x^2}=\frac{1}{2}$$
Now we look at $x=2y$ than the limit is
$$\lim_{y \to \infty} \frac{4y^2}{4y^2 +y^2}=\frac{4}{5}$$ but the limits must be same so they don't exists.
Answer: Let $x=r\cos\theta$ and $y=r\sin\theta$. Then
$$\frac{x^2}{x^2+y^2}=\cos^2\theta$$
if $(x,y)\ne (0,0)$. Thus even for very large positive $x$ and $y$, the function $\frac{x^2}{x^2+y^2}$ can take on any value in the interval $(0,1)$.
Answer: The point you need to know, and glean from excellent examples posted, is to check the limit of your function as $(x, y) \to \infty$ along two or more different curves.
You are working in two dimensions, so the choice of curves you can test can is not one dimensional lines. Put, say, $y = x$, and $y = x^2$, perhaps even $y = x^3$.
Then, write the function as a function of $x$ (replace $y$ in the function $f(x)$ for each curve, depending on the curve $y$) and evaluate limit of the function as $x \to \infty$.
E.g., with your function, if we let $y = x$, then substitute $x$ whenever $y$ appears in you function, and evaluate: $$\lim_{x\to \infty} \frac{x^2}{2x^2} = 1/2\tag{1}$$
And if $y = x^{-2}$, we substitute $x^{-2}$ for $y$ and then evaluate $$\lim_{x\to \infty} \frac{x^2}{1 + x^2} = 1\tag{2}$$
If the limits as $x\to \infty$ of the functions defined in terms of different curves, are not equal, as we have with $(1), (2)$, you know the limit does not exist.
Answer: I like Andre Nicolas' answer and mine is similar. Since the degree of the terms in the denominator are of equal degree, let $$y=ax.$$
Then we have $$1/(1+a^2).$$
Suppose you have $$x^2 +y^4$$ in the denominator, with $$y^4$$ in the numerator. Then let $$x=ay^2.$$
So you see that this approach is generalizable.


C# Client Server TCP Client Listening

Question: I'm in coding 3D a while, and now, i wan't to create a game which works with Networking. I use the System.Net and System.Net.Sockets.
My Server is stupid as hell, it's just able to hold a single connection (later on, i will change it to multi-connection-abilities). It has a TcpListener which listens at 127.0.0.1 on Port 10000. (Just to test). In there, i have a DO While Loop which checks for received Data and Displays that.
My Client is a TcpClient. It connects to the Server and Sends a Message. Nothing special here, it's done with the TcpClient.GetStream().Write(). This works great. I'm also able to read one Answer right after the Send.
But what if the Server will Send some infos without the Client's Asking? For Example: All Players on the Server receive an item. How do i have to set up my Client, to be able to receive such messages?
Does my client have to ask in a loop? Or do i have to make a delegate somehow? I can create a loop which asks for such infos every 200 miliseconds or something, but how will this change the Performance of a 3D Game?
How is this done in Professional Game Developement?
Answer: Use the asynchronous capabilities of the TcpClient and TcpListener classes.
In your client:
private TcpClient server = new TcpClient();

async Task Listen()
{
    try {
        IPAddress IP = IPAddress.Loopback // this is your localhost IP
        await server.ConnectAsync(IP,10000); // IP, port number

        if(server.Connected) {
           NetworkStream stream = server.GetStream();

           while (server.Connected) {
               byte[ ] buffer = new byte[server.ReceiveBufferSize];
               int read = await stream.ReadAsync(buffer, 0, buffer.Length);
               if (read > 0 ){
                    // you have received a message, do something with it
               }
           }
        }
    }
    catch (Exception ex) {
         // display the error message or whatever
         server.Close();
    }
}

Answer: Use Asynchronous Client Socket:

" The client is built with an asynchronous socket, so execution of the
  client application is not suspended while the server returns a
  response."

http://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx
private static void ReceiveCallback( IAsyncResult ar ) {
        try {
            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            StateObject state = (StateObject) ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0) {
                // There might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

                // Get the rest of the data.
                client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
                    new AsyncCallback(ReceiveCallback), state);
            } else {
                // All the data has arrived; put it in response.
                if (state.sb.Length > 1) {
                    response = state.sb.ToString();
                }
                // Signal that all bytes have been received.
                receiveDone.Set();
            }
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }

In this ReceiveCallback you can create a switch that handles the messages of the server.
For example:
First byte of the message is a command and after the command is the right data. Within an switch you can handle that command and execute some code.

C# Client Server TCP Client Listening


Benchmarking hard clustering results against soft clustering results (ground truth)?

Question: Well, I don't have labels (ground truth) for my data points. However, by domain knowledge, I am certain that a particular soft clustering algorithm will produce satisfactory results. Hence, I may use its output, denoted by C, as a surrogate of the ground truth. Let's say
$$C=\{(0.1, 0.9), (0.2, 0.8, \ldots)\},$$
where $C_1=(0.1, 0.9)$ means that the 1st data point is in Cluster 1 with 0.1 probability and in Cluster 2 with 0.9 probability.
Now, I want to benchmark some other hard clustering algorithms against my "ground truth", $C$.
What is the rightful way of doing so? Is there an already-formed convention for this?
Answer: Most "external" (supervised) evaluation measures such as ARI should work, if you take element weights into account. Most are based on an agreement of pairs; and you may argue that a pair exists with $p_1\cdot p_2$ in your soft clustering, if $p_1$ and $p_2$ are the probabilities of objects 1 and 2.
They probably just become much more computationally intensive, as you can't just use the cluster intersection sizes anymore.


Change FloodFill-Algorithm to get Voronoi Territory for two data points?

Question: I got a grid with two points. I want to calculate the amount squares each point can reach before the other. Currently I implement a FloodFill-Algoritm, which can calculate the amount of squares one point can reach.
How can I change that algorithm to do the "flooding" for both points simaltaneuosly or at least one after another?
Comment: Greetings, Google AI contestant :)
Comment: Thanks, did you already implement such thing or is your strategy different?
Comment: I did, just using kind of a simultaneous BFS from two points - similar to IVlad's answer.
Answer: What do you mean by "each point can reach before the other"?
It looks to me like you need a BF search. Use a FIFO queue like so:
Let p1 and p2 be the positions of the two points.
let f be the first element in the queue and l the last. Initially f = 0, l = 1. Let Q be the queue.
<code>Q[f] = p1
Q[l] = p2
while ( f <= l )
{
   poz = Q[f];
   ++f;

   for each neighbour poz' of poz
      if poz' hasn't been marked yet
      {
         mark poz'
         add poz' to Q: Q[++l] = poz
      }
}
</code>
Q will need to be the size of your grid (rows x cols). You can use two matrices: one with the positions p1 can reach and the other with the positions p2 can reach, or you can use one matrix and mark the squares p1 reaches with positive numbers and the squares p2 reaches with negative numbers. If you are interested where they meet, you just need to check if you are about to mark a positive value from a negative value (poz negative and poz' positive) or the other way around. This will basically do your flooding in turns: flood one square from p1, then from p2, then from p1, then from p2 and so on.


Install Active Directory on Windows Server 2003 R2 SP2 without Event Log Errors

Question: I was wondering if anyone has a way to install Active Directory on a Windows 2003 R2 Server, without any tricks, and have no errors/warnings in the event log.
I have traditionally used an about 15 step "manual" process for installing AD, but I was doing a new server with Windows 2003 R2 and thought I'd try the "Server Manager" way. So after I installed a clean copy of Windows 2003 R2 and loaded SP2, I clicked on Add Role from the Manage Your Server window and ran the Configure Your Server wizard. I chose Typical Configuration for a First Server.
After restarting, I noticed a few issues in the event logs (some familiar):

The DSRestore Filter failed to connect to local SAM  server. Error returned is <id:997>. Fixing this involved removing "dsrestor" from HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\LSA\Notification Packages, then resetting the Directory Services Restore Mode Administrator Account per Q322672 and rebooting.
MS DTC could not correctly process a DC Promotion/Demotion event. MS DTC will continue to function and will use the existing security settings. This is related to KB923977 and involved just going into Component Services->MyComputer->Properties->MSDTC->Security Configuration->Click OK then stopping and restarting MSDTC.
Time Provider NtpClient: This machine is configured to use the domain hierarchy to determine its time source, but it is the PDC emulator for the domain at the root of the forest, so there is no machine above it in the domain hierarchy to use as a time source. Fixing this involved a lengthy process outlined in this article.

I didn't see the DNS error that I usually see that makes you set HKLM\System\CurrentControlSet\Services\NTDS\Parameters\Repl Perform Initial Synchronizations to 0, which was nice, but, still, 3 issues with a clean install "by the book".
EDIT: The DNS Error just appeared (Error 6702 DNS server has updated its own host (A) records...) so I had to implement the above fix. So that makes FOUR issues.
So my thought was...Windows 2003 has been around since, well 2003...so over six years ago. It has had innumerable patches, two service packs, even an R2 update. Is it really not possible to have an error free install of Active Directory without tricks? What am I missing?
Answer: I've been installing Active Directory on W2K and W2K3 for years without any of the gyrations you're talking about.
Item 1 - That's an event ID 1005 from source DSrestor (per http://www.eventid.net/display.asp?eventid=1005&eventno=4658&source=dsrestor&phase=1). I've got a W2K3 R2 test box in my lab, installed from a slipstreamed W2K3 SP2 VL media and DCPROMO'd immediately after install. I have the full events log back to the OS load and this event isn't in any of them. I also checked out the archived event logs from the first W2K3 domain controller (installed with RTM W2K3 media in 2004) at a Customer site and I don't find this error anywhere. I don't know exactly what you're doing to make this error occur, but I've never seen it anywhere.
Item 2 - Microsoft has never fixed this. I tend to just ignore it. My various event log notification applications have been configured to ignore it, too. Yeah, you can resolve it if you want to. It doesn't bother me.
Item 3 - This isn't an error at all! This is telling you to do something. Configure a time source external to the domain. It's a one command-line procedure, if you don't mind using public NTP servers: "NET TIME /setsntp:pool.ntp.org" (specify any NTP server you want). I have no idea why the article you refer to makes such a big deal about it. You need an external-to-the-forest time source. (BTW: This only happens on the PDC emulator FSMO role-holder in the forest root domain. All subsequent DCPROMOs of all other DCs in the forest won't generate this message...)
You mention an "Item 4" that's a "DNS error", but you're really talking about event ID 1555, source "NTDS replication" I think. This isn't an error either. This article describes how the feature works and it's actually in the product for a utility purpose. If the initial synchronization isn't happening on your FSMO role-holder DC(s) you should really fix the replication problem instead of just squelching the error message. I can understand doing this in a test environment or in a DR dry-run, but in real life you should be fixing initial synchronization issues with your FSMO role-holders.
In summary: I don't know what you're doing to make item 1 happen. I can't tell you why Microsoft hasn't fixed item 2 but I don't care about it. Item 3 isn't an issue. Item 4 is an issue insofar as it's telling you that you need to fix broken initial synchronization.
Comment: when you do NET TIME /setsntp:pool.ntp.org, do you have to add 0x1 to the end of that? I tried without and it didn't seem to work. According to the docs, the registry key requires 0x1 if it is a domain name as opposed to an IP address.
Comment: If you have time, it would be interesting to see if you get the same thing with a clean install of R2 and then loading SP2 (not slipstreamed). For #1. Yeah that is the first time I've seen it. For #3, is there a quicker way then 6 or so registry entries? For #4 (the DNS)...that issue happens when the AD starts before DNS. It's not 1555, it's 6702. You haven't seen that? It's not the first time that's happened to me. I think what caught me off guard was that I figured by this point it would just work without any tweaks with a clean R2 / SP2.
Comment: Re: the multiple registry entries, I'm referring to things like Config\AnnounceFlags (5), \TimeProviders\NtpClient\SpecialPollInterval (900), Config\MaxPosPhaseCorrection (3600), Config\MaxNegPhaseCorrection (3600)
Comment: I'll see if I can find some time to do what you mention above. re: NTP - You don't _have_ set any of those registry entries to get a working NTP situation. Like I said in my posting (and "mh" said in his), use "NET TIME /setsntp:public.ntp.server.name.here", make sure you can resolve that NTP server name, and make sure that your firewall allows NTP out (UDP 123) and you're golden. No registry tweaking needed.
Comment: If no one comes up with anything else I'll mark this as an answer tomorrow.
Answer: The NTP one seems to trip a few people up, so here's the deal.  If you don't join a domain during the Windows install process, Windows will "helpfully" set your time server to time1.windows.com (or similar).  This can happen e.g. if you need to load your NIC driver post-install.
The trick to a clean AD installation is to get all the prep work done before installing AD.  This includes setting your FQDN properly, ensuring that the machine can resolve IP-to-name and name-to-IP over your DNS, and setting your time source properly.  Most AD install errors happen because one or more of these steps was either missed or done wrong, so doing them right and ensuring that they're right before installing AD is the only sensible solution.
If the prospective DC isn't the first in a new domain, joining it to the domain as a standard member server before installing AD never hurts either.
Regarding time specifically, the way that's tried and trusted for me involves at most two steps, both at the command-line.
net time /setsntp:

This is required for all domain-members and just clears whatever NTP server setting may be there.  No need for any registry fiddling.  This is a correct config for a domain member, as with no NTP server set it will just pick up it's time from the domain hierarchy.
net time /setsntp:whatever.wherever.whoever

This is only required on your PDC emulator, you should never explicitly set an NTP server on any other domain member, whether a DC or not.  If you do, strange and wonderful things might happen.
Comment: Yeah I noticed "time.windows.com" in there...what's the deal with that? I set it to time.nist.gov...is time.windows.com not an NTP server?
Comment: It is, but it may not have the same time as is on your domain, which can cause things to go out of whack.