Validation messages are displayed when page load

Question: I have a problem with validation in ASP.NET MVC 2.0. I use the same Action in Controller to perform user request.
For example:
public ActionResult Index(ReportModel model)
{
    if (!model.IsInitialDisplay && ModelState.IsValid)
    {
        model.Result = service.GetResult(model);
    }
    return View(model);
}

In the ReportModel, I define a flag IsInitialDisplay to determine whether the page is initial displayed or not:
public class ReportModel
{
    [Required(ErrorMessage = "*")]
    public string Criteria { get; set; }
    public bool IsInitialDisplay { get; set; }
    public ReportResult Result { get; set; }

    public ReportModel()
    {
        IsInitialDisplay = true;
    }
}

And in the View, I use the following code:
<% using (Html.BeginForm())
   { %>

            Criteria:

            <%= Html.TextBox("Criteria", "") %>
            <%= Html.ValidationMessage("Criteria") %>

<input type="submit" value="Submit" />
<%= Html.Hidden("IsInitialDisplay", false) %>
<% } %>

As I expect, if users don't input any value for Criteria and click Submit button, the error message for validation will be displayed.
But the validation error message always displayed on initial page load, I don't know how to prevent it?
Does anyone know? Thanks,
[Updated]
I have updated my Action method as below and it's seem to be fine:
public ActionResult Index(ReportModel model)
{
    // Collecting some commons data here...

    if (model.IsInitialDisplay)
    {
        ModelState.Clear();
    }
    else if (ModelState.IsValid)
    {
        model.Result = service.GetResult(model);
    }
    return View(model);
}

Answer: The reason an error message is displayed on initial page load is because your controller action takes ReportModel model as argument. When you first access this action with /Home/Index you are passing no arguments and when the default model binder tries to bind to a ReportModel instance it triggers validation errors.
It is a bad practice to use the same action for both rendering and handling the form submission but if you really want to do it you could try like this:
public ActionResult Index(bool? isInitialDisplay)
{
    if (isInitialDisplay.HasValue && !isInitialDisplay.Value)
    {
        var model = new ReportModel();
        UpdateModel(model);
        if (ModelState.IsValid)
        {
            model.Result = service.GetResult(model);
        }
        return View(model);
    }

    // Initial request
    return View(new ReportModel());
}

In this case you no longer need the IsInitialDisplay property on your model nor the constructor which sets it to true.
This being said, here's the recommended way:
public ActionResult Index()
{
    var model = new ReportModel();
    return View(model);
}

[HttpPost]
public ActionResult Index(ReportModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    model.Result = service.GetResult(model);
    return View(model);
}

Comment: I'm wondering why this answer was NOT marked as answer. This is was very helpful for me. Thanks :)
Comment: Thanks for your suggestion, but in my project, I need to collect some commons data for _ReportModel.Criteria_. So, I don't want to duplicate these business logic in both two actions (with & without model).
Comment: this is curious to me as I am getting this error for the reason you described but the Action is accepting a model because it is part of a series of forms and was POSTed to, and then the actual page posts to the next one... so I am not sure why it would be a bad practice
Comment: Why is it a bad practise to "use the same action for both rendering and handling the form"? What's wrong?
Comment: @SandRock see the very question this answer was posted to, that is one example of a complication that arises when you try to multitask with a single controller action.
Answer: Here's a simple solution that combines some good answers:
[HttpGet]
public ActionResult Index(ReportsModel model)
{
    ModelState.Clear(); //clears the validation

    return View(model);
}

[HttpPost]
public ActionResult Index(ReportsModel model, string unused)
{
    ...
}

This uses the http method to determine if it's first load (like Darin's solution).
Most importantly, it has MVC build your controller instead of manually newing one up yourself. This is important if you use dependency injection or if you had other contextual data coming in through the query string (like a nested resource id).
Answer: Model
public class ReportModel
{
     [Required(ErrorMessage = "*")]
     public string Criteria { get; set; }
}

View
<% Html.EnableClientValidation(); %>

<% using (Html.BeginForm())
{ %>
     <%= Html.TextBoxFor(model => model.Criteria) %>
     <%= Html.ValidationMessageFor(model => model.Criteria) %>

     <input type="submit" value="Submit" />
<% } %>

Work fine
Comment: In my project, I use complex model object for _ReportModel.Criteria_. So, your suggestion is not suitable.
Answer: If you need to handle form submission with the same action name, you should always use this technique:
[HttpGet]
public ActionResult Index()
{
    /* Just returning the view. No validation will happen because we are not passing a parameter*/
    return View();
}

[HttpPost]
public ActionResult Index(ReportsModel model)
{
    //Another post action for validation
}

Comment: While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply.


Accessing Parent Model from child element of ManyToManyField (Django)

Question: I am having a models.py as follows

class Comment(models.Model):
    auther_name = models.CharField(max_length=20, null=False, blank=False)
    comment_body = models.TextField(max_length=200, null=False, blank=False)

class Post(models.Model):
    comments = models.ManyToManyField(Comment)

So let's say i have a comment Object Instance which's id is 2 and raw content
How to get parent post_id which contains this comment??
Comment: You use a `ManyToManyField`, hence there can be zero, one, or more related `Post`s. It does not seem to make much sense to model this with an m2m field.
Answer: Use related_name. Small change adding: related_name='posts': think of it like "if we start from the opposite side, what would be the name to use" (it's always plural).
class Comment(models.Model):
    author_name = models.CharField(max_length=20, null=False, blank=False)
    comment_body = models.TextField(max_length=200, null=False, blank=False)

class Post(models.Model):
    comments = models.ManyToManyField(Comment, related_name='posts')

Now you can do:
for post in Comment.objects.get(pk=12).posts.all():
    print(post.xxx)


How can I consolidate multiple rows into a single row based off their values in a Pandas Dataframe?

Question: I have a dataframe called Traffic:
   Source   Method    Destination    Weight
0     LA    Ground             NY        20
1     LA    Ground             NY        15
2     LA    Ground             LV        10
3     LA       Air             LV         5

I'd like to end up with a dataframe like so:
   Source            Ground        Air
0      LA    LV: 10, NY: 35      LV: 5

Where the 4 rows are combined into 1 based off the Source. The traffic methods are then further broken up by their destinations in ascending order. If there are multiple entries from say LA->NY of type Ground, add the weights.
Ground/Air columns would be strings following the format of "Destination:TotalWeight".
This pattern would continue all the way down for any other Sources such as 'WA'...
Answer: One way, not so 'concise' though.
Groupby to sum weight. Create a column that dicts destination and total weight per group. Another groupby to put the dicts in list. Then pivot.
g=df.groupby(['Source','Method','Destination']).agg(Weight=('Weight','sum')).reset_index()
g['dict']=g.Destination.str.cat(g.Weight.astype(str),sep=':')
k=g.groupby(['Source','Method'])['dict'].agg(list).apply(','.join).reset_index()
print(k.pivot(index='Source', columns='Method', values='dict').reset_index())

Gives:
    Method Source   Air       Ground
0          LA       LV:5   LV:10,NY:35

Answer: This code should give you what you are looking for.
ndf = df.groupby(['Source','Method','Destination']).sum()
ndf = ndf.reset_index()
ndf['Weight'] = ndf['Weight'].astype(str)
ndf = ndf.groupby(['Source','Method']).agg({'Source':lambda x: x.max(),'Destination':
lambda x: ','.join(x),'Weight':lambda x: ','.join(x)})
fdf = ndf[['Destination','Weight']].apply(lambda x: x.str.split(',').explode())
fdf.reset_index(inplace=True)
fdf['Combined'] = fdf['Destination'] + ':' + fdf['Weight']
fdf = fdf[['Source','Method','Combined']]
fdf = pd.pivot_table(fdf,index='Source',columns = 'Method',aggfunc = lambda x: ',
'.join(x)).reset_index()

Answer: initialize
df = pd.DataFrame(data= [["LA","Ground","NY",20],
                         ["LA","Ground","NY",15],
                         ["LA","Ground","LV",10],
                         ["LA","Air","LV",5]],
                  columns=["Source","Method","Destination","Weight"])

do stuff
# add weights when same source, method & destination
df_ = df.groupby(["Source","Method","Destination"], as_index=False)['Weight'].sum()
# conatenate destination and weight strings
df_['Dest_Wt'] = df_['Destination'].str.cat(df_['Weight'].astype(str), sep = ': ')

shipping_summary = (df_.groupby(["Source","Method"])['Dest_Wt']
           .apply(', '.join)
           .unstack(level=1)
           .reset_index())
shipping_summary.columns.name = None

print(shipping_summary)

output
   Source      Air            Ground
0      LA    LV: 5    LV: 10, NY: 35


Como descobrir o tamanho renderizado de uma curva no html5 canvas 2d?

Question: Preciso descobrir o tamanho renderizado de uma curva no canvas 2d
context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);

com esse código, por exemplo
// pontos de controle
var cp1x = 200,
    cp1y = 150,
    cp2x = 260,
    cp2y = 10;

var x = 0,
    y = 0;

// calculação
var curveWidth = cp1x > x ? cp1x - x : x - cp1x,
    curveHeight = cp1y > y ? cp1y - y : y - cp1y;

Porém, o ponto cp2 pode alongar mais a curva se ultrapassar um ponto inicial ou final (isso é que acontece nesse código, cp1x = 200, cp2x = 260). Por exemplo, vamos supor que o ponto cp2 é marcado com a bolinha vermelha nessa imagem e que sua coordenada x é maior que a coordenada x de cp1, qual aparenta ser o ponto final da curva:

Sendo assim, como posso considerar a largura do ponto cp2 no valor de curveWidth e no valor de curveHeight para ser exato?
Answer: Camarada achei sua pergunta muito interessante, nunca tinha trabalhado com bezier do canvas e fiz um teste pra brincar.
Entendi da sua pergunta que você quer saber a distância do lado esquerdo até o ponto da curva mais à direita, sendo essa a largura da curva.
Gostaria de poder dizer algo do tipo curveWidth = cpx1 + ( (cpx2 - cpx1)/2 )
mas na realidade é que nos meus testes não consegui descobrir uma relação matemática que se mantivesse constante, então essa relação muda muito de acordo com os valores das variáveis a todo instante.
Mas reparei que na sua pergunta você não está vendo no gráfico os 2 pontos de controle da bezier e acredito que isso faça toda diferença pra melhorar o seu entendimento de como você pode encontrar essa distância.
Apenas como ferramenta pra debug, fiz um código que talvez possa te auxiliar nesse entendimento e ajudar a encontrar uma resposta adequada, segue abaixo:

<!DOCTYPE html>

 <title></title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 <canvas id="meucanvas" width="500" height="400" style="border:1px solid #d3d3d3;">
 Your browser does not support the HTML5 canvas tag.</canvas>

 cp1x: <input type="text" id="cp1x">
 cp1y: <input type="text" id="cp1y">

 cp2x: <input type="text" id="cp2x">
 cp2y: <input type="text" id="cp2y">

 x: <input type="text" id="x">
 y: <input type="text" id="y">

 <input type="button" value="Redesenhar" onclick="redesenhar()">

Se essa resposta te ajudar em alguma coisa dê um +1, mas não marque minha resposta como aceita porque eu não consegui respondê-la.
Gostaria que você publicasse a resposta do seu problema quando conseguir resolvê-lo.
Boa sorte e abraços,
Qualquer coisa vamos trocando ideia aqui na pergunta.
Comment: Não sei se no aplicativo do celular aparece o botão de executar o código, mas indo pelo Chrome do meu celular não apareceu. De qualquer forma boa sorte com o projeto e com o notebook. De repente é só bateria descarregada.
Comment: Eu consegui executar o código no https://jsfiddle.net,  só foi um pouco tricky para copiá-lo :v. Hum..., creio que a bateria do notebook esteja danificada. Quando eu puder voltar à mexer nele usarei seu utilitário para ver as diferenças entre os pontos de controle!
Comment: Parece ser útil! Infelizmente meu notebook não está ligando, mas vou testar seu código no celular, vlw pela atenção
Answer: Você precisa implementar a própria função de Bézier em JS para encontrar um ponto no segmento da curva Bézier q vc quer.
A parte mais importante pra vc é a própria implementação da curva Bézier. Vc pode usa-la para criar uma lista de segmentos e depois criar os boundaries com a informação coletada dos seguimentos.
Existe uma constante chamada limit que define a quantidade de pontos que quero da curva, menos pontos significa menos precisão, porém mais rápido. Ai fica ao seu critério a melhor forma de obter esses pontos.
Abaixo o demo completo que vc pode rodar em um REPL Svelte ou extrair as partes relevantes pra seu código (já que Svelte é praticamente agnóstico).
// App.svelte

info

      {info}

  <canvas bind:this={canvas} width="800" height="500" />
  <form>

Control Point 1

      <PointModifier name="cp1x" bind:value={cp1x} />
      <PointModifier name="cpyx" bind:value={cp1y} />

Control Point 2

      <PointModifier name="cp2x" bind:value={cp2x} />
      <PointModifier name="cp2x" bind:value={cp2y} />

  </form>

// PointModifier.svelte

  {name}: {value}
  <input type="range" min="0" max="750" bind:value />


Scrapy Shell XPath

Question: I am trying to get links and category from this http://www.npr.org/rss/#feeds news feed website.
This is my xpath  in scrapy shell:
a = sel.xpath('//ul[@class="rsslinks"]/li/a/@href').extract()

b = sel.xpath('//ul[@class="rsslinks"]/li/a/text()').extract()

But length of b is one lesser than length of a. I don't know what I am missing here. But this is causing problems in data.
From the image below,the category name is "Most Emailed Stories" but link is for "News Headlines"
Any help would be appreciated
Answer: This is because of the first link in the results:
News Headlines

As you can see, there is no direct child "text" nodes, only one strong element. Your xpath would not match it.
Add an another slash to get all text nodes from the a tag:
//ul[@class="rsslinks"]/li/a//text()
                         HERE^

Comment: In my opinion, the wording of your answer is misleading because it suggests that there is a `text` element in the sense of an element node named "text", in the same way as `strong.` Please make it clear that text nodes are not element nodes.
Comment: @MathiasMüller very good point, thanks, I think it should be better now. And, btw, thanks for contributing to `xpath` tag - learning a lot from your answers.
Comment: + 1, it's fine now. And dito, I can only return the compliment!
Answer: The text for /rss/rss.php?id=1001 with the label of News Headlines appears to be another level down under  while the other links are not.
Comment: Yes,my bad ! Didn't notice that,thanks for the help !

Scrapy Shell XPath


Call a parent method before(or after) any child method

Question: Most likely I'll get negative answer here, but I'd like to ask my question.
Is there a way in C# to call the parent method before (or after) ANY child class method.
public class Base
{
    protected static void ThisWillBeCalledBeforeAnyMethodInChild()
    {
        //do something, e.g. log
    }

    protected static void ThisWillBeCalledAFTERAnyMethodInChild()
    {
        //do something, e.g. log
    }
}

public class SomeClass : Base
{
    public static void SomeMethod1()
    {
        //Do something
    }

    public static void SomeMethod2()
    {
        //Do something
    }
}

So, I want to method ThisWillBeCalledBeforeAnyMethodInChild from Base class to run before SomeMethod1 or SomeMethod2 in SomeClass. Similar with after method.
Is there any way to do it without calling methods with reflection?
Comment: Here's the answer you were expecting: No. Not without rewriting the code at compile-time, or generating proxy types at runtime (for instance methods).
Comment: static would be hard to do that.
Answer: So, aside from in your child method calling base.MethodNameYouWantToCall() before or after, you could take a different approach.
This is just an idea, and might not be what you're trying to achieve, but if I needed every child class to call parent functions before and after something I might do this:
class Parent
{
   protected void Before() { /* does things */ }
   protected void After() { /* does things */ }
   abstract void OtherCode();

   public void PubliclyExposedMethod {
        Before();
        OtherCode();
        After();'
}

class Child : Parent {
      OtherCode { Console.Write("Hello world"); }
}

In the above, the method you define in the child will be ran after the before method, and before the after. I think this is cleaner because it reduces the number of times you need to write base.()
Comment: The common name for this pattern is "template method", in case you want to read up on it more. https://en.wikipedia.org/wiki/Template_method_pattern
Comment: I've seen this a lot, where `OtherCode()` is called `PubliclyExposedMethodCore()` and is `abstract` or `protected virtual`.
Comment: `before` and `after` doesn't need to be `protected`, only `OtherCode` needs to be `protected`. Other then that, good answer.
Answer: One idea that strikes my mind would be to write a wrapper method which gets an action as parameter. There you can call your before and after methods:
public class SomeClass : Base
{
    public static void SomeMethod1()
    {
        //Do something
    }

    public static void SomeMethod2()
    {
        //Do something
    }

    public static void WrapperMethod(Action action)
    {
        Base.ThisWillBeCalledBeforeAnyMethodInChild();
        action();
        Base.ThisWillBeCalledAFTERAnyMethodInChild
    }

}

You would call it this way:
SomeClass.WrapperMethod(()=> SomeClass.SomeMethod1());

Answer: First of all, static methods don't participate in inheritance.
You have total control over this by using the base pointer, which is a reference to the base class of the this pointer.
class Program
{
    static void Main(string[] args)
    {
        var s = new SOmeClass();

        s.SomeMethod1();
        s.SomeMethod2();
    }
}

public class Base
{
    protected  void ThisWillBeCalledBeforeAnyMethodInChild()
    {
        Console.WriteLine("ThisBefore");
    }

    protected  void ThisWillBeCalledAFTERAnyMethodInChild()
    {
        Console.WriteLine("ThisAFTER");
    }
}

public class SOmeClass : Base
{
    public  void SomeMethod1()
    {
        base.ThisWillBeCalledBeforeAnyMethodInChild();
        Console.WriteLine("SomeMethod1");
    }

    public  void SomeMethod2()
    {
        Console.WriteLine("SomeMethod2");
        base.ThisWillBeCalledAFTERAnyMethodInChild();
    }
}

Output:
ThisBefore
SomeMethod1
SomeMethod2
ThisAFTER
Comment: "First of all, static methods don't participate in inheritance." How did you arrive at this conclusion? change the `protected` in the `Base` class to `public` and you can see that now you can access and call `SomeClass.ThisWillBeCalledBeforeAnyMethodInChild`
Comment: That's not inheritance. That's just access.
Comment: static members are inherited, but can't be overriden. same as events.


CCK fields don't appear on the "node add" form for authorized user

Question: I would like to allow logged-in users to create nodes, and I gave them the permission to create "some_content," but on "add node form" users have only one field, which is for custom node title, without any other CCK fields that this content type have. Could anybody tell me how to add those fields to this form?
Answer: Most likely you have a module enabled that adds access control for each field. Check your permissions setup.
Comment: Thanks, I add permissions for editing those fields and now all works ok.
Answer: Check the "Display fields" link on your CCK type. Did you make sure to display them?


Computing the joint moment generating function for two functions of two random variables

Question:
Let $X$ and $Y$ be i.i.d random variables in the plane with a pdf
$$f(x) = \frac{1}{\sqrt{2\pi}} \cdot \text{exp}(-x^{2}/2) \hspace{1cm}
-\infty < x< \infty.$$
Let $U = X + Y$ and $V = X^{2} + Y^{2}$. Compute the joint mgf of $U$
  and $V$.

My first step was to compute the MGF of $U$ as follows:
\begin{align*}
\mathbb{E}[e^{sU}] = \mathbb{E}[e^{s(X + Y)}] = \mathbb{E}[e^{sX}e^{sY}] \\
= \mathbb{E}[e^{sX}] \cdot \mathbb{E}[e^{sY}] \\
= M_{X}(s) \cdot M_{Y}(s) \\
= (e^{s^2/2})^{2} \\
= e^{s^{2}},
\end{align*}
where $t \in \mathbb{R}$. Note that we can separate expectations due to independence.
Now I can compute the MGF of $V$ as follows:
\begin{align*}
\mathbb{E}[e^{tV}] = \mathbb{E}[e^{t(X^{2} + Y^{2})}] = \mathbb{E}[e^{tX^{2}}] \cdot \mathbb{E}[e^{tY^{2}}] \\
= M_{X^{2}}(t) \cdot M_{Y^{2}}(t),
\end{align*}
To proceed, I need the mgf of $X^{2}$ and $Y^{2}$. So, maybe is there an easier way to approach this problem? If not, how would I proceed. Then, at the end, I can just multiply the two mgf I found individually? I'm not sure if this approach is correct though, as it would assume $U$ and $V$ to be independent.

The joint moment generating function of $U$ and $V$ is given by
$$\mathbb{E}[e^{sU + tV}] $$
Comment: Why don't you write down the definition of the joint MGF of $U$ and $V$ and start from there? As you noted at the end, computing the individual MGFs might not help with the joint MGF since $U$ and $V$ are not independent.
Comment: I tried starting with that approach. I edited my post with the joint moment generating function. I don't know how to proceed from just that, though. Maybe conditioning?
Comment: It looks like $X$ and $Y$ are standard normal, and then $X^2,Y^2$ are chi-squared with one degree of freedom.
Comment: Yeah I saw that on wikipedia but I have not learned chi-square distribution.
Answer: You have written down the joint MGF correctly. Now just plug in the definitions of $U$ and $V$, and keep going, using independence where you can to simplify things.

 $$E[e^{sU + tV}] = E[e^{sX+tX^2 + sY + tY^2}] = E[e^{sX + tX^2}] E[e^{sY + tY^2}] = E[e^{sX + tX^2}]^2$$

To compute the last term, I believe you can compute the integral directly, i.e.
$$E[e^{sX+tX^2}] = \int_{-\infty}^\infty e^{sx+tx^2} f(x) \, dx = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^\infty e^{sx + tx^2 - x^2/2} \, dx.$$
You might need to do some casework (e.g., when $t=1/2$ this integral diverges, etc.).

Edit:
When $t \ge 1/2$, I believe the integral $\int_{-\infty}^\infty e^{sx + (t - \frac{1}{2}) x^2} \, dx$ diverges.
When $t < 1/2$ we can write
$$sx + tx^2 - x^2/2 = -\frac{(x-\mu)^2}{2\sigma^2} + c$$
where $\mu = \frac{s}{1-2t}$ and $\sigma^2 = \frac{1}{1-2t}$ and $c = \frac{s^2}{2(1-2t)}$.
So the integral is
$$\frac{1}{\sqrt{2\pi}}\int_{-\infty}^\infty e^{sx + tx^2 - x^2/2} \, dx = \frac{1}{\sqrt{2\pi}} e^c \int_{-\infty}^\infty e^{-\frac{(x-\mu)^2}{2\sigma^2}} \, dx
= \frac{1}{\sqrt{2\pi}} e^c \sqrt{2 \pi \sigma^2} = \frac{1}{\sqrt{1-2t}} \exp\left(\frac{s^2}{2(1-2t)}\right).$$
It seems like the final answer is thus
$$\frac{1}{1-2t} \exp\left(\frac{s^2}{1-2t}\right), \qquad t < 1/2$$
but I may have made some typos.
Comment: How come you were able to interchange the $s$ and the $t$'s?
Comment: @joseph Sorry about that mistake.
Comment: how can I solve that integral? it looks challenging


Como fazer uma aplicação híbrida para Android rodar em background?

Question: Gostaria de saber se é possível fazer uma aplicação híbrida para Android ter um serviço rodando em background onde mesmo com a aplicação fechada eu consiga receber informações de um servidor e notificar o usuário. Essa aplicação é desenvolvida em JavaScript utilizando o framework IONIC.
Answer: Então, imagino que voce esta utilizando phonegap/cordova, para criar o aplicativo vc vai precisar fazer seu app rodar em background e utilizar a Push Notifications, que é configurado no aplicativo e no google dashboard.
Push Notifications
google dashboard
E para manter seu app em background, coloque no arquivo config.xml
<code><preference name="exit-on-suspend" value="false" />
</code>
Comment: No caso no xml de configuração do android o android manifest
Comment: Se vc estiver usando apenas phonegap build é config.XML se não é o manifest. XML sim


como posso retirar todos os espaços de uma string em lua?

Question: eu quero pegar uma string e retirar os espaços em branco contidos nela.nome = algumacoisa imagine que isso é uma string, eu quero retirar os espaços em branco entre o "nome" e o "=" e o "=" e o "algumacoisa"
Answer: Use gsub. Assim s=s:gsub("%s+","").
O padrão %s+ significa "um ou mais caracteres de espaço (whitespace)".


how to impliment background process in iphone phone gap sdk

Question: I am new in iphone phone gap developement. and i have a task to work with back ground process.
I have to impliment the background task to run the background and at the spacific time i have to send the latitude and longitude to the server. but the main problem is that i have a task to develop whole application in phone gap,so do not have idea about how to impliment background process in iphone phone gap sdk.
Please help me and provide me any code or example if it possible.
Thanks in advance.
Answer: Thanks i have solved their is method to solve this problem and with the help of javascript we can also create background process.
Comment: How to create the background process using java script. because as per my knowledge java script can not run in the background
Comment: please provide some links or at least some conceptual examples


The Sheaf of Regular Functions on an Affine Algebraic Set

Question: So, I'm trying to show that the ring of regular functions on an affine algebraic set forms a sheaf. I have no problem showing that they form a presheaf, that much is clear, but the two additional things you have to prove I find particularly annoying. To quote Hartshorne, p. 61:

(4) if $U$ is an open set, if $\{ V_i \}$ is an open covering of $U$, and if we have elements $s_i \in \mathcal{F}(V_i)$ for each $i$ with the property that for each $i,j$, $s_i |_{V_i \cap V_j} = s_j |_{V_i \cap V_j}$, then there is an element $s \in \mathcal{F}(U)$ such that $s|_{V_i} = s_i$.

Fair enough I figure, only to then ask the question, what if the overlap between the different $V_i$ are always the empty set? After all, is there anything compelling $U$ to not be disjoint?
Consider then the following schematic:
$U \subset X$ has an open covering of $\{ V_1 , V_2, V_3 \}$.">
Since $V_i \cap V_j = \varnothing$ for all $i,j$, it trivially follows that $\mathcal{F}(V_i \cap V_j) = \{ 0 \}$ and so $s_i |_{V_i \cap V_j} = s_j |_{V_i \cap V_j} = 0$ for all $s_i \in V_i, s_j \in V_j$. But that then would imply that the restriction morphisms $\rho_{UV_i}$ must all necessarily be surjective. And that just strikes me as odd and deeply suspicious.
What, and where, am I doing something that must be terribly wrong?
Look forward to your responses!
Answer: There is nothing suspicious about the restriction maps being surjective. In fact, a sheaf with this property has a special name: flasque sheaf.
As for the open cover having trivial intersections, the sheaf condition becomes vacuously true as you point out. Note that if the underlying topological space is irreducible, then open subsets are always dense, and therefore intersections of nontrivial open subsets aren't empty.
Comment: Define $s = a$ on $V_1$, $s = b$ on $V_2$, and so on. Since regularity is a local property, we end up with a regular function.
Comment: For a point $P \in V_1$, you only need to find a open neighborhood of $P$ such that $s$ can be written as a ratio of polynomials. It doesn't have to apply globally.
Comment: Does it really become vacuously true? Because I don't see how there necessarily must exist $s \in \mathcal{F}(U)$ such that $s|_{V_i} = s_i$ for all $V_i$. Indeed, it would appear that if $U$ is irreducible and the intersections are trivial, that then would imply that for arbitrary $a \in \mathcal{F}(V_1), b \in \mathcal{F}(V_2) , c \in \mathcal{F}(V_3)$ there would necessarily have to exist some $s \in \mathcal{F}(U)$ such that $s|_{V_1} = a$,  $s|_{V_2} = b$, and  $s|_{V_3} = c$. Am I misinterpreting it?
Comment: Wait, you can do that? For instance, let $X = \mathbb{C}$ and let $U=V_1 \cup V_2$, with $V_1 = \{ z \in \mathbb{C} | -2< \Re(z)<-1 \}$ and $V_2 = \{ z \in \mathbb{C} | 1< \Re(z)<2 \}$. Let then $a \in \mathcal{F} (V_1)$ be $a(x) = x^2+1$ and $b \in \mathcal{F} (V_2)$ be $b(x) = x+43$. How do I then find a ratio of polynomials $s = \frac{f}{g}$ that on $V_1$ looks like $a(x)$ and on $V_2$ looks like $b(x)$ but that can still be written as $s$ everywhere on $U$?
Comment: Huh! I was under the impression that the definition of regularity of a function $f$ over an open subset $U$ was such that $f$ could be written as a ratio of polynomials globally on $U$. I take this from that there is a bijection between elements of a coordinate ring $A(X)$ and functions that can globally be written as fractions of polynomials everywhere on $X$.
Comment: Ah! Now I get it! Took me a little while to find all the stuff I needed. On a Zariski closed set (that is, an algebraic set), a regular function (defined locally) can always be globally described as the restriction of a polynomial. On an open set, such as this, that is not the case! **Everything checks out!**


how to compile lex/yacc files on windows "without cygwin"

Question: We need to create .c file from .l (lax) and .y (yacc) file using visual studio 1o on windows 2008.
We cant use Cygwin at all.
Where does Flex and bison come into picture here ? Even Flex and Bison use cygwin internally or not ?
how are we supposed to go about it ?
Comment: Did you try something like http://sourceforge.net/projects/winflexbison/ ?
Comment: mingw comes with flex/bison packages that don't use cygwin at all.  Bison is GPL so will always be under a GNU license.  Flex is BSD-like licensed, and not GPL.  [Berkeley yacc](http://en.wikipedia.org/wiki/Berkeley_Yacc) is available under a BSD-like license
Answer: You can download a windows version of flex from sourceforge and bison from sourceforge, you then need to add them to the PATH environment variable. Be sure not to have a space in the install directory (i.e. do not put in C:\Program Files\ etc). You can then build the output with CL from visual studio command line tools if you wish.
As previously mentioned MinGw is another option.
I have even made a YouTube Video showing the install of these packages!


Entity Reference: Filter by an entity reference view not working as expected

Question: I am attempting to get a taxonomy reference field select widget that shows a list of taxonomy terms, including some extra data from their fields. I have attempted to use the "Reference method" = "Views: Filter by an entity reference view" and then select an entity reference view I created, then in my entity reference view I have done the following:

However, this gives me only the taxonomy term name despite the settings indicating it might show all the fields inline ("Note: In 'Entity Reference' displays, all fields will be displayed inline unless an explicit selection of inline fields is made here.").
Comment: For future reference: this is a core bug. Work is being done here: https://www.drupal.org/node/2174633
Comment: The same thing happens to me using references to users and content.
I filled a bug report at drupal.org.
You can find it [here](https://www.drupal.org/node/2714089#comment-11128121).
Comment: for future reference, that issue was closed as duplicate, current issue is: https://www.drupal.org/node/2174633
Answer: You can't at the moment.
See \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::getReferenceableEntities(), it just uses the view to get the results and then calls $entity->label(). Which means you have no way to override that.
Which means that if you want this, you need to provide your own plugin. You can either subclass from ViewsSelection and still a view, or just extend from the default and override that method.
Might also be worth opening a feature request, I've already had this use case multiple times, and a new checkbox or option somewhere would be a nice, backwards compatible way to introduce this feature in a minor release.
Comment: I was going through the code while waiting on an answer here and found exactly the same as what you described. I actually view this issue as a core views bug and might open it as such. The UI in views states usage that is contrary to what it actually does. But yes, overriding it seems like the way to go. I would think maybe even a contributed module that "corrects" the way it works might be in order.
Comment: Here we go: [Entity refrence views selection does not use views output \[#2997025\]](https://www.drupal.org/project/drupal/issues/2997025)
Comment: and for a working patch see https://www.drupal.org/project/drupal/issues/2174633
Answer: The module Entity Reference Views Select supports additional fields in select lists and checkboxes when selecting entities in an entityreference field. We've used it successfully with ECK entities.
Answer: There might be a way to get around this, but it also has restrictions as indicated in this issue filed by me :
https://www.drupal.org/node/2841330#comment-11854353
In other words, write your own display handler to deal with the result of an entityreference view result.
The webform_references developers have done that but as you can see in my issue, they also delegate their webform select option theming to the views rendering outcome.
Where in my opinion (see second comment) it should be the responsibility (or choice for that matter) of the one who is requesting the entity reference view.
In other words, use the views_get_view_result() function instead of $view->execute_display().
Answer: There is a working patch for this: View output is not used for entityreference options
Answer: In Drupal 8, you can use the View Entity Reference Field Formatter module to use the views module to display the referenced entities.


CVS Web frontend

Question: Is there any free WEB application, that can be installed and pointed to any CVS server, that would show commits as they arrive, allow to search them, and possibly present nice diffs, and visual representation of branches.
Answer: http://www.gjt.org/src/cvsweb/
http://www.viewvc.org/

CVS Web frontend


How do you notate the augmented 6th chords with chord symbols?

Question: If we I want to analyze a a piece by Beethoven e.g. with an Italian 6th chord (iv aug6) the notes are the first inversion of F#,Ab,C => Ab,C,F#.
So what chord symbol is correct? Is it F#b3b5/Ab ...  or what I am missing?
Comment: Related to this question: https://music.stackexchange.com/questions/80684/do-augmented-six-chords-always-have-to-be-bvi
Comment: I haven't seen slash notation in analysis of classical, common practice, or "art" music much. Seems mainly. a jazz, rock, pop thing.
Comment: Perhaps the issue is that Neapolitan chord(s) refers to a very specific voicing, while chord symbols normally give lot's of place for interpretation
Answer:
Italian Augmented 6th - It6, It+6, or #iv6 - In C Major, spelled Ab7(no 5)
German Augmented 6th - Ger6, Ger+6, or Ger65 - In C major, Ab7
French Augmented 6th - Fr6, Fr+6, or Fr43 - In C major, Ab7(#5)
and Neapolitan 6th -  N6 - In C major, Db/F

The last isn't really augmented, but I included to complete the list of unusual chord symbols.
What you might be missing is that when we look at the function of the augmented 6th chords and how they are analyzed for the common practice period, we consider there to be an augmented 6th interval contained in the chord, which is enharmonic to a minor seventh interval. If you want to spell the chord for easy reading for performance, it's better to base the chord name on the dominant seventh chord of the lowered 6th scale degree, rather than an altered first inversion of the raised fourth scale degree.
As with all chord spellings, once you go enharmonic, it can get very flexible. For example, the C major Fr6 could also be spelled D7b5/Ab instead of Ab7#5.
See: https://en.wikipedia.org/wiki/Augmented_sixth_chord
Comment: I don't believe this is the notation Albert asked about.
Comment: @user1079505 Those are commonly used chord symbols for those chords. If he's asking for something other than chord symbols, I find the use of the phrase "chord symbols" confusing.
Comment: I understand he's looking for a chord symbol (like root with whatever upper structure and inversion). What you provide are function symbols.
Comment: @user1079505 If we are not talking about function, then we might not even consider it to be an augmented 6th chord. It might be some other chord. But I think there are ways to enharmonically spell them. I'll add that.
Comment: @user1079505 Looks like the term "chord symbol" means different things to different people or in different contexts.
Comment: Yeah, I think that's right... you often see chord symbols that don't necessarily correspond to functions. For example cadence with dominant 6/4 would be probably spelled C/G – G – C, even if the first chord is technically a form of dominant, not tonic
Comment: @user1079505 - Todd's symbols are correct and widely used for Roman Numeral Analysis. You might be thinking of Macro-Analytical notation, which would / can also show function, at which point this is a matter of semantics. That point notwithstanding, it's also perfectly viable to use this notation to delineate chords with no observable function, which actually has been the case with Beethoven's work.
Answer: I consider jazz symbols as based on a hypothetical dominant 13th chord, in major, with all the extensions and modifications from that point of reference.
So, the root is given as the specific pitch F#, then add notation to mod from a dominant seventh chord: drop the seventh, the major third needs to be lowered by two half steps (3), the perfect fifth lowered a half step (♭5), the bass is given as the specific pitch (/A♭), so that would be F#3♭5/A♭. Or, you could add min for one of the half steps on the lowered third, like F#m♭3♭5/A♭. Or, maybe base it on a diminished triad F#dim♭3/A♭.
I'm not very confident in those as a jazz/pop chord labels, because the classical augmented sixth chord is considered the same as the jazz tritone substitution which would simply call the chord A♭7 with maybe the notation "omit 5th" to specify the Italian augmented sixth voicing.
The usual RNA symbol is It+6 regardless of inversion, but it seems that #iv+6 should be clear for the first inversion form.
I found an online textbook that shows both the jazz tritone label and RNA label used alternately: Hutchinson, Music Theory for the 21st-Century Classroom

http://musictheory.pugetsound.edu/mt21c/LeadSheetAnalysisOfAugmentedSixthChords.html
http://musictheory.pugetsound.edu/hw/MusicTheory-Nov-2019.pdf

That author seems to prefer labeling it A♭7(no 5th).


Have I found a counterexample in this question?

Question: If $f:X\rightarrow \mathbb{R} \,$ is a function with $x_0 \in \overline{X}   \,\setminus \partial(\overline{X}) $ such that :
$$\exists \,\,\,\,f'_-(x_0)=\lim_{x\rightarrow x_0^-}\dfrac{f(x)-f(x_0)}{x-x_0},$$
$$\exists \,\,\,\,f'_+(x_0)=\lim_{x\rightarrow x_0^+}\dfrac{f(x)-f(x_0)}{x-x_0}$$
 but with possibly $f'_-(x_0) \not= f'_+(x_0)$,  does this still imply continuity of $f$ ?
if so then why can I have a function such as $$f(x) =       \begin{cases}       3x \, , \text{  if}  \,\,\, x<x_0 \\       10x+1 \, ,\text{  if}  \,\,\, x=x_0 \\       -2x \, , \text{  if}  \,\,\, x>x_0       \end{cases} $$
that does have $f'_-(x_0)=3$ and $f'_+(x_0)=-2$ but is not continuous, does that mean that only the existence of the left and right side derivatives on a point do not guarantee that $f $ is continuous at $x_0$ ?
Answer: You haven't found the limits right.  I will take the example of $x_0=0$. There $$f'^+=\lim_{x\rightarrow 0^+} \frac{2x-1}{x}   =\lim_{x\rightarrow 0^+} 2 - \frac{1}{x} = -\infty$$ and
$$f'^-=\lim_{x\rightarrow 0^+} \frac{3x-1}{x}   =\lim_{x\rightarrow 0^+} 3 - \frac{1}{x} = -\infty$$
Actually, continuity is implied because $\lim_{x\rightarrow x_0^+} f(x) = \lim_{x\rightarrow x_0^-}f(x) = f(x_0)$ since the two one-sided limits of your question exist (and are finite).
Comment: I think you cannot use the value of the function at point $x_0$ to calculate the limit in general.
Comment: @FelipeDilho What do you mean? That is exactly the definition of these two limits. Perhaps you are confusing with using the function value of f to compute the limits of f itself (as opposed to the derivative limits which I have here).
Comment: Yes you are right i got confused when i asked the question.
Answer: Since the limits exist, you have
$$
\lim_{x\to x_0^+}f(x)=
\lim_{x\to x_0^+}\left(\frac{f(x)-f(x_0)}{x-x_0}(x-x_0)+f(x_0)\right)=f(x_0)
$$
and similarly for the limit from the left.
You are confusing
$$
f'_+(x_0)=\lim_{x\to x_0^+}\frac{f(x)-f(x_0)}{x-x_0}
$$
with
$$
\lim_{x\to x_0^+}f'(x)
$$
which can be different.


If $\sum_{i=1}^{n} n a_n$ converge ,does $\sum_{i=1}^{n} a_n$ converge?

Question: If $ a_n \geq 0 $, then is easy to get.
Cause $$\lim_{n \to \infty} \frac{a_n}{n a_n} = 0$$ , so $\sum_{i=1}^{n} n a_n$ converge, then $\sum_{i=1}^{n} a_n$ also converge.
When $ a_n $ not always positive, does $\sum_{i=1}^{n} a_n $ still converge? I guess $ \sum_{i=1}^{n} n a_n $ construct a not absolute convergence $ \sum_{i=1}^{n} n a_n $, then maybe $\sum_{i=1}^{n} a_n $ will diverge. Seem not so easy.
Comment: The statement in the title is false even if $a_n \geq 0$. Perhaps you have made  a typo.
Answer: If the series $\sum_{n=1}^\infty na_n$ converges, then the series $\sum_{n=1}^\infty a_n$ converges too, by Abel's criterion:$$\sum_{n=1}^\infty a_n=\sum_{n=1}^\infty\frac1n(na_n),$$and the sequence $\left(\frac1n\right)_{n\in\Bbb N}$ is monotonic and its limit is $0$.
Comment: Maybe my previous question is confused. I mean if If $\sum_{i=1}^{n} n a_n$ converge ,does $\sum_{i=1}^{n} a_n $ converge? Contrary to your answer
Comment: @hstk In the original version of your question, you wrote "iff", not just "if".
Comment: Now, I change it. I used 'iff' wrongly.
Comment: I've edited my answer. What do you think now?
Comment: Oh, I forget to use Abel'ss criterion. It's easy. I'm careless.


how to know the type of reference referring to a object in java?

Question:  package example;
    class Machine
    {
        public void start()
       {
            System.out.println("MACHINE IS STARTING");
       }
    }
    class Camera extends Machine
    {
        public void snap()
        {
            System.out.println("SNAP");
        }
    }

    public class App {
        public static void main(String args[])
        {
            Camera cam1 = new Camera();
            Machine mach1 = cam1;

        }

    }

in the above code camera object is being refereed by both cam1 which is of
    type Camera and mach1 of type Machine .
            Is there any function which can tell the type of references that are being referred to that object???
Comment: Yes. You can use `instanceof`. That said, it's not OO to use `instanceof` :)
Comment: No. [filler to make this comment 15 characters long]
Comment: Can you tell us more about what you're trying to achieve?
Comment: Just to clear up one thing: references have no type; *objects* do, and *variables* do.
Answer: If you want to know the class of the instance referenced by the mach1 variable, use :
mach1.getClass().getName()

If you want to test if mach1 is of a specific type, use instanceof
if (mach1 instanceof Camera)

Answer: You can use the getclass method for it
java.lang.Object.getClass()

mach1.getClass().getName()

it will return the runtime class of an object.


Pandas read '\0' in CSV column as NULL character and print as Unicode in JSON

Question: I have a csv with one columns has '\0' value. I want to print it in json as \u0000 value. I used pandas read_csv and set the dtypes of the column as str to read the file. When I print it to json, it printed as \\0, which is \0 with the backslash escaped.
How can I properly read it as NULL character and print it as unicode \u0000?
This is the csv example
id,data,timestamp
1,\0,2018-03-17 03:12:58
2,\0,2018-03-19 09:00:40
3,\0,2018-03-19 09:26:05

The output that I expect
{ "id": 1, "data": "\u0000", "timestamp":"2018-03-17T03:12:58.000Z"}
{ "id": 2, "data": "\u0000", "timestamp":"2018-03-19T09:00:40.000Z"}
{ "id": 3, "data": "\u0000", "timestamp":"2018-03-19T09:26:05.000Z"}

How I read it using pandas
csvfile = open('somefile.csv', 'r')

col_names = ['id', 'data']
dtypes = { 'data': str }
df = pd.read_csv(csvfile, names=col_names, dtype=dtypes)

And how I write it to json
jsonfile = open('somefile.json', 'w')
df.to_json(jsonfile, orient='records', lines=True, date_format='iso')

Comment: Are you only using Pandas to read the csv and convert to JSON?  Are you using the Dataframe for anything else?
Comment: @ZakiIndra if an answer resolves your problem, please consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it.
Comment: (Wow, that was fast :-) )
Answer: \0 is not a valid representation of a null byte in CSV. Instead, it expects a literal null byte (which is okay since the parser only looks for the specified delimiters to separate the stream into fields).
To process a file with escape sequences, you can specify encoding='unicode_escape' in pd.read_csv(). This encoding will decode the escape sequence into a literal null character.


Image of Lipschitz map measure zero

Question: Let $f\colon\mathbb{R}^{n-1}\to\mathbb{R}^n$ be Lipschitz, i.e., $|f(x)-f(y)|\leq C|x-y|$ for some $C>0$.
How do we show that the image $f(\mathbb{R}^{n-1})$ has Lebesgue measure zero?
I can see that $\mathbb{R}^{n-1}$ itself has measure zero in $\mathbb{R}^n$, for instance $\mathbb{R}$ is the line which is measure zero in the plane $\mathbb{R}^2$.
However I am not sure how to prove the above rigorously.
Thanks!
Answer: Below are two proofs of this fact. Neither is self contained; the first is more classical, but I added the second as I find it very interesting.

Let $\mu$ denote the $n$-dimensional Lebesgue measure, and let $\mathcal{H}^n$ be the $n$-dimensional Hausdorff measure. Note that

In $\mathbb{R}^n$, $\mu=c\mathcal{H}^n$, for some positive constant $c$.

Depending on the normalization of $\mathcal{H}^n$, the constant can be made $1$. The proof of this result is not trivial; I can tell you more about it, if you are interested.
Recall that, for $E \subset \mathbb{R}^n$, $\delta>0$
$$
\mathcal{H}_{\delta}^n(E)=\inf \left\{\sum_{i=1}^{\infty} \operatorname{diam}(E_i)^n \,\middle|\, \bigcup_{i=1}^{\infty}E_i \supset E, \ \operatorname{diam}(E_i)\leq \delta\right\}
$$
and
$$
\mathcal{H}^n(E)=\sup_{\delta>0}\mathcal{H}_{\delta}^n(E)=\lim_{\delta \to 0} \mathcal{H}_{\delta}^n(E).
$$
Moreover, the infimum can be taken over open balls $B_i$.
We have, since $f$ is Lipschitz,
$$
\mathcal{H}_{\delta}^n(f(\mathbb{R}^{n-1})) \leq \sum_{i=1}^{\infty} \operatorname{diam}(f(B_i))^n \leq C^n \operatorname{diam} (B_i)^n.
$$
By taking the infimum over all coverings with balls of radius less or equal to $\delta$, and then taking the limit for $\delta \to 0$, we get
$$
\mathcal{H}^n(f(\mathbb{R}^{n-1})) \leq C^n \mathcal{H}^n(\mathbb{R}^{n-1})=0
$$
and so we are done.

Let $A=f(\mathbb{R}^{n-1}) \subseteq \mathbb{R}^n$.
If we can prove that in a set of full measure $A$ has Lebesgue density smaller than $1$, then it follows that $\mu(A)=0$, as by Lebesgue density theorem, we know that for almost every point in $A$, $A$ has density $1$.
By Rademacher's theorem, $f$ is differentiable at almost every (with respect to $n-1$ dimensional Lebesgue measure) point of $\mathbb{R}^{n-1}$. The images of such points have full measure in the image, and at those point, $A$ has a tangent plane.
Let $y=f(x)$ and let $v$ be a unit vector normal to the tangent plane to $A$ at $y$.
Let $r>0$ be small enough. Note that $B\left(y+\frac{r}{2}v, \frac{r}{4}\right) \cap A = \emptyset$ (a simple picture will clarify this statement).
We have that $\mu(B(y,r)\cap A) \leq c \mu(B(y,r))$, where $c<1$ does not depend on $r$. In fact,
$$
B(y,r)\cap A \subseteq B(y,r) \smallsetminus B\left(y+\frac{r}{2}v, \frac{r}{4}\right),
$$ and the latter ball has measure a fixed proportion of $B(y,r)$.
Then,
$$
\lim_{r \to 0} \frac{\mu(B(y,r)\cap A)}{\mu(B(y,r)}=c <1.
$$
Comment: Yes, the second proof I wrote does not involve it. Also I'm sure there's a natural generalization of the usual one dimensional proof, it's just annoying to write down.
Comment: Thanks for your answer. Is it possible to prove it without reference to Hausdorff measure?

Image of Lipschitz map measure zero


Probability that event one and event occurring simultaneously

Question: The sample space for different ratings of food is given in the table below:

For the rating by two person, what is the probability that one of the two will rate the meal as excellent and the other will rate it as good?
Any hint(s) for this question is appreciated.
Answer: Assuming that each person's rating is independent of another person's rating, this can be solved using the notion of independence. Some hints:
if event A and event B are independent:
1) P(event A occurring AND event B occurring) = P(event A occurring) $\times$ P(event B occurring)
2) Be careful though, in this question, they don't specify who is person A and who is person B. That is, either person could be A or B. This adds a slight wrinkle that you need to be aware of. For example, imagine Joe is person A and Jane is person B. But Joe can also be person B and Jane could be person A.


How to handle with missing values in order to prepare data for feature selection with LASSO?

Question: My situation:

small sample size: 116
binary outcome variable
long list of explanatory variables: 44
explanatory variables did not come from the top of my head; their choice was based on the literature.
most cases in the sample and most variables have missing values.

Approach to feature selection chosen: LASSO
R's glmnet package won't let me run the glmnet routine, apparently due to the existence of missing values in my data set. There seems to be various methods for handling missing data, so I would like to know:

Does LASSO impose any restriction in terms of the method of imputation that I can use?
What would be the best bet for imputation method? Ideally, I need a method that I could run on SPSS (preferably) or R.

UPDATE1: It became clear from some of the answers below that I have do deal with more basic issues before considering imputation methods. I would like to add here new questions regarding that. On the the answer suggesting the coding as constant value and the creation of a new variable in order to deal with 'not applicable' values and the usage of group lasso:

Would you say that if I use group LASSO, I would be able to use the approach suggested to continuous predictors also to categorical predictors? If so, I assume it would be equivalent to creating a new category - I am wary that this may introduce bias.
Does anyone know if R's glmnet package supports group LASSO? If not, would anyone suggest another one that does that in combination with logistic regression? Several options mentioning group LASSO can be found in CRAN repository, any suggestions of the most appropriate for my case? Maybe SGL?

This is a follow-up on a previous question of mine (How to select a subset of variables from my original long list in order to perform logistic regression analysis?).
OBS: I am not a statistician.
Comment: (1) The best approach to imputation depends on the proportion & pattern of missing values, the relationships between the variables, & what assumptions you're prepared to make about the reasons for missing values. (2) Any single imputation method can be used to provide input to LASSO; the difficulty's in assessing how imputation affects the results. I don't know how to combine multiple imputation with LASSO (doubtless someone does), but an informal comparison of results from different imputation runs (are the same predictors usually selected?) could still be informative.
Comment: @Scortchi: Most of my missing values fall in the category 'not-applicable'. E.g.: In the variable 'age of adult female in the household', cases in which the adult male is a widow. Actually, I guess I need to take a step back here: should I treat values of 0 in continuous variables as missing values? E.g: 0 years of education, 0 household members between 14 and 60 years old.
Comment: It's hard to imagine a situation in which you'd want to treat that as an unknown fact about an absent female rather than a known fact about the household. The question about zeroes is hard to understand: are you asking whether e.g. no years of education is an implausible value, or 0 might be used to code a missing value? (And then how would I know?) There's certainly no *general* reason to treat 0 as indicating missingness.
Comment: @Scortchi: About the female age variable, I see what you mean. But the issue than becomes: how would I code the absent female case if not as NA? About zeroes: yes, that was exactly my question, sorry if it was not clear. I thought that the program might have some problem in handling zero values and that it might not 'understand' what I meant with it.
Comment: You could code it as any constant value & introduce an indicator variable for presence/absence (& use group LASSO). No reason why LASSO or any other regression program should handle zero values for predictors wrongly. [Please don't take this the wrong way, but these are very basic questions, suggesting that if this is for fun you might want to start off with simpler problems, or that if it's for real you might want to consult a statistician.]
Comment: @Scortchi: I have just come across [this](http://www.uk.sagepub.com/upm-data/45664_6.pdf) reference, according to which the example above about female age when female is absent would be a case of _legitimate_ missing value. According to that reference, legiminate missing values should not be imputed; one of the suggestions offered is the analysis in that particular variable is carried out only with the sample where the female is not absent. How do I do that, for example, when I run LASSO?
Comment: @Scortchi: I can assure that my problem refers to the analysis of real data, collected in my own research. I thought questions of all ranges (even basic ones) would be appropriate here, sorry if I was wrong.
Comment: Basic questions are most welcome. The answers will be useful to a lot of people, & often there's more to them than is apparent at first sight; I don't want at all to discourage you from asking them. Just to warn you that you may be trying to run before you can walk. That doesn't affect the quality of the questions you're asking as far as CV's concerned, but might concern you if the results of the analysis are important. Sorry if the warning was unnecessary.
Comment: @Scortchi: I see what you mean, I think this is a consequence of me having to learn things almost from scratch - as I am not an statistician - in order to be able to run the analysis I need.
Comment: @Scortchi: By coding as a constant variable, do you mean assiging a value (e.g. 9999) and not ask the program to treat it as a missing value?
Comment: Coding as 999 or 0 or -1 doesn't matter for maximum-likelihood fits if there's also an indicator variable for that unique value. For LASSO you'd need to do some research, as (1) you don't want to lose just one of these coefficients, & (2) the absolute magnitude of coefficients affects the penalty. Group LASSO might resolve those issues.
Comment: @Scortchi: Would your suggestion (coding as constant variable and adding indicator variable) apply both to continuous and to categorical variables? In the latter case, it would be equivalent to creating a new category, right? I am just wary because I have read that this may introduce bias.
Comment: @Scortchi: I read [here](ftp://ftp.stat.math.ethz.ch/Manuscripts/buhlmann/lukas-sara-peter.pdf) that group LASSO is more approapriate than LASSO for handling categorical variables as it deals with whole factors rather than individual dummy variables. Is it in that sense that you suggest the use of group LASSO? Also, do you know if R's glmnet package supports group LASSO? If not, would anyone suggest another one that does that in combination with logistic regression? Several options mentioning group LASSO can be found in CRAN repository, any suggestions of the best one? Maybe SGL?
Comment: You may also consider the simple approach presented in the following paper: Loh, P. L., & Wainwright, M. J. (2011). [High-dimensional regression with noisy and missing data: Provable guarantees with non-convexity](http://papers.nips.cc/paper/4454-high-dimensional-regression-with-noisy-and-missing-data-provable-guarantees-with-non-convexity). *In Advances in Neural Information Processing Systems* (pp. 2726-2734).
Answer: When a continuous predictor $x$ contains 'not applicable' values it's often useful to code it using two variables:
$$
x_1=\Big{\{}
\begin{array}{ll}
c  & \text{when $x$ is not applicable}\\
x  & \text{otherwise}
\end{array}
\Bigg{.}
$$
where $c$ is a constant, &
$$
x_2=\Big{\{}
\begin{array}{ll}
1  & \text{when $x$ is not applicable}\\
0  & \text{otherwise}
\end{array}
\Bigg{.}
$$
Suppose the linear predictor for the response is given by
$$\eta = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \ldots$$
which resolves to
$$\eta = \beta_0 + \beta_1 x_1 + \ldots$$
when $x$ is measured, or to
$$\eta = \beta_0 + \beta_1 c + \beta_2 + \ldots$$
when x is 'not applicable'. The choice of $c$ is arbitrary, & does not affect the estimates of the intercept $\beta_0$ or the slope $\beta_1$; $\beta_2$ describes the effect of $x$'s being 'not applicable' compared to when $x=c$.
This isn't a suitable approach when the response varies according to an unknown value of $x$: the variability of the 'missing' group will be inflated, & estimates of other predictors' coefficients biased owing to confounding. Better  to impute missing values.
Use of LASSO introduces two problems:

The choice of $c$ affects the results as the amount of shrinkage applied depends on the magnitudes of the coefficient estimates.
You need to ensure that $x_1$ & $x_2$ are either both in or both out of the model selected.

You can solve both of these by using rather the group LASSO with a group comprising $x_1$ & $x_2$: the $L_1$-norm penalty is applied to the $L_2$-norm of the orthonormalized matrix $\left[\vec{x_1}\ \vec{x_2}\right]$. (Categorical predictors are the poster child for group LASSO—you'd just code 'not applicable' as a separate level, as often done in unpenalized regression.) See Meier et al (2008), JRSS B, 70, 1, "The group lasso for logistic regression" & grplasso.
Comment: Does anyone know if R's glmnet package supports group LASSO? If not, would anyone suggest another one that does that in combination with logistic regression? Several options mentioning group LASSO can be found in CRAN repository, any suggestions of the most appropriate for my case? Maybe SGL?
Comment: So, would you say that if I use group LASSO, I would be able to use the approach you suggest to continuous predictors also to categorical predictors?
Answer: Multiple Imputation is never a bad approach. You could also do Full Information Maximum Likelihood. Good review and comparison here and here.
But if you're going that route, consider using Stan to fit the ML imputation simultaneously with your regression as a single Bayesian model, since LASSO is a special case of Bayesian regression anyway.
Comment: You can run Stan through R (see [RStan](http://mc-stan.org/rstan.html)).
Comment: There's an R package `mi` that might help you.
Comment: Additional multiple imputation packages for R include `Amelia` and `mice`.
Comment: I had misunderstood the multiple imputation method, now I see that it would be applicable for my case. I edited my question in order to reflect this. Do you know if either SPSS or R run two options that you mentioned?
Answer: The CATREG command in Statistics handles missing data with LASSO.  You can exclude cases listwise or have the procedure impute  Although it's name suggests that it is for categorical variables, you can set the scale to Numeric to handle the continuous case.
Comment: this is SAS `PROC CATREG`, I'm guessing?
Comment: @JKP: I had come across this command, actually. However, considering that my outome variable is binary, I am assuming logisic regression would be more appropriate than categorical regression (CATREG) - am I right? Also, the options in CATREG seem rather limited - you can only choose between excluding cases, replacing missing values with mean values or creating an extra category.


repetition of permutations problems bingo

Question: for a game bingo, organizers place one marble with 0 marble on it, one marble with 1, and one marble with 2 and so on up to one marble with 9. Each time a number is called one number is drawn, the digit showing recorded, the marble replaced and another marble is drawn, the digit recorded, the marble replaced and another marble is drawn, the digit recorded and the marble replaced, the number is then :
10 x first digit  + second digit, for example 3  7 is 37
If the number has already been called that draw is considered null. If the first digit is 9 the number is only called if the second digit is 0, otherwise the draw is null. (there are only 90 numbers in a game of bingo.)
is this reasonable way of drawing numbers 1-90 for a bingo game? if not, suggest modifications.
my solution: yes, because 1-90 are all even chances
Comment: What are your own thoughts?
Comment: quite a long question, eh~ >_< haha
Comment: ooh oops sorry, i'll add it now
Answer: No, since odds of getting $90$ is $\large\frac{1}{10}$ as opposed to other numbers from $1$ to $89$ whose odds are clearly less than that of $90$
And drawing balls in your case is actually affected by results in past. For example if you've recorded all numbers from $20$ to $29$ Now you're not allowed to draw marble marked $2$ on it for first draw, I mean even if you're drawing it you're making it a NULL draw
Comment: @carry Thanks You're first to upvote since I've reached $1$k
Comment: don't worry, i am the who should say thanks for helping :D


Trying to prove this inequality involving exponential function

Question: Given :

A continuous non-negative function $f(x)$ on $\mathbb R^{+} \cup \{ 0\}$
$\alpha > 0$
$h(x) = \displaystyle \int \limits_0^x f(u) du + \alpha$
$\forall x$, $f \leq h$

To prove : $$f(x) \leq \alpha e^x$$
Attempt :
$$\dfrac {d}{dx} [\ln h(x)] = \dfrac {h'(x)}{h(x)}$$
By the fundamental theorem of integral calculus, $h'(x) = f(x)$ and $\therefore $
$$\dfrac {d}{dx} [\ln h(x)] = \dfrac {f(x)}{h(x)}$$
From the 4th given point, $f \leq h$,
$$\dfrac {d}{dx} [\ln h(x)] \leq 1$$
i.e. the function $\ln [h(x)]$ never grows faster than the linear function, with slope 1.
From this step, I wrote $$\ln [h(x)] \leq x + C, \text{ for some } C$$
Is the above step correct?
$$\therefore h(x) \leq e^{x+C} = e^C e^x = B e^x$$
$$\therefore f(x) \leq h(x) \leq B e^x$$
Also, am unable to get in $\alpha$ in this inequality.
Answer: This is just a consequence of Grönwall's inequality, see for instance: https://en.wikipedia.org/wiki/Gr%C3%B6nwall%27s_inequality.
In your case, you can take $u(x) = h(x)$, $b(x) = f(x)/h(x) > 0$. Your condition then becomes:
\begin{equation}
u(x) = \alpha +  \int_{0}^{x} b(s)u(s)ds.
\end{equation}
By your first three assumptions,  $\alpha$ is positive and non-decreasing, $b$ is continuous and non-negative. Therefore, Grönwall's inequality yields:
\begin{equation}
u(x) \leq \alpha \exp \left( \int_{0}^{x} b(s)ds \right).
\end{equation}
By your final assumption, $b(s)\leq 1$,  which yields the inequality you wanted.
Answer: Your strategy is a good one. Since $\ln h(x)\le x+\ln h(0)$, $h(x)\le h(0)e^x$. But $h(0)=\alpha$.


Como personalizo um Navbar somente quando ele estiver "collapsed" com bootstrap 4?

Question: Eu tenho o seguinte código:

    <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#nav-content"
    aria-expanded="false" aria-label="Toogle Navigation">

    </button>

                    Home

                    Estus Flasks

                    Capra Demon

                    Areas

                    Legendary Items

        <form class="form-inline my-2 my my-lg-0">
            <button type="button" data-toggle="modal" data-target="#modal-login" class="btn btn-outline-info">Logar</button>
        </form>

Este código faz com que crie uma navbar parecida com esta:

A função toggler do bootstrap faz com que ela fique assim em um celular/tablet:

O que eu quero? Trabalhar com a div quando ela estiver na forma "toggled", eu quero manipular a aparência dela somente quando ela estiver visualizando no celular por exemplo, porque quando eu vou alterar as propriedades no css, acaba alterando também quando ela não está "collapsed" ou em tela cheia.
Eu quero melhorar a usabilidade fazendo com que o menu da navbar preencha a tela toda do celular, e não somente no cantinho esquerdo, de forma que aumente o campo de seleção para atingir o conteúdo desejado.
Comment: Na documentação https://getbootstrap.com/docs/4.0/layout/grid/ ctrl+f "class prefix". Voce pode configurar as divs para tamanhos de telas especificos. Talvez te ajude
Answer: Para isso você deverá tornar seu site responsivo introduzindo o uso de @media para configurar a aparência do site em função do tipo de dispositivo utilizado ao acessá-lo.
Basicamente, você pode configurar da seguinte forma:
1) Para estilos aplicáveis ao site acessado em resolução igual ou superior a 700px (do seu computador por exemplo):
@media screen and (min-width: 769px){
}

2) Para dispositivos menores:
@media all and (max-width: 768px){
}

A propriedade "min-width" determina que o estilo será aplicado quando a resolução da tela tiver dentro do limite especificado.
Existem várias outras opções de configuração da tag @media que poderão ser muitos úteis também. Visite depois o link abaixo.
https://www.w3schools.com/cssref/css3_pr_mediaquery.asp
Answer: Finalmente consegui obter o resultado desejado com a dica do @Caiuby Freitas. Agora o item da lista (link) da nav está preenchendo a linha toda para facilitar o acesso em dispositivos menores.

@media all and (max-width: 765px) {

.nav-cel {
    display: inline-grid;
}}


Interpret a ANOVA table with treatment taking basliene value into account

Question: I have the example data below. I wonder how I should interpret the BL value from the ANOVA table, 0.0194391. It means it is significantly different from zero, but putting that into the context, does it mean that it is significantly different from the treatments (trt)?. Does anyone have a good explanation? Thanks in advance! :)
<code>id <- rep(c(300,450), each=6)
visit <- rep(1:6,2)
trt <- rep(c(0,"A",0,"B",0,"C"),2)
q1 <- c(20,100,40,89,30, 60,50,85,10,40,15, 20)
df <- data.frame(id,visit,trt,q1)
dfnoll <- df[df$trt==0,]
meanBL <- tapply(dfnoll$q1, dfnoll$id, mean, na.rm=TRUE)  # funkar bara när det är numreriskt
BL <- rep(meanBL, each=3)
dftrt <- df[!df$trt==0,]
dftrt$BL <- BL
dftrt
    id visit trt  q1 BL
2  300     2   A 100 30
4  300     4   B  89 30
6  300     6   C  60 30
8  450     2   A  85 25
10 450     4   B  40 25
12 450     6   C  20 25
str(dftrt)
'data.frame':   6 obs. of  5 variables:
 $ id   : num  300 300 300 450 450 450
 $ visit: int  2 4 6 2 4 6
 $ trt  : Factor w/ 4 levels "0","A","B","C": 2 3 4 2 3 4
 $ q1   : num  100 89 60 85 40 20
 $ BL   : num  30 30 30 25 25 25
dftrt$id <- as.factor(dftrt$id)
dftrt$visit <- as.factor(dftrt$visit)
dftrt$BL <- as.numeric(as.character(dftrt$BL))
library(lme4)
df.lm <- lmer(formula =q1 ~ trt + BL +(1|id), data=dftrt)
library(car)
Anova(df.lm)
Analysis of Deviance Table (Type II Wald chisquare tests)

Response: q1
      Chisq Df Pr(>Chisq)
trt 17.7895  2  0.0001371 ***
BL   5.4616  1  0.0194391 *
</code>
Answer: No, it does not mean that BL is significantly different from treatment. Treatment and BL are two different variables, the first is a factor the second is numeric. An ANOVA tells you, that a model with both variables treatment and BL is a significantly better model than a simpler model with just treatment variable (in terms of fit).
These two variables cannot be directly compared since they represent different concepts. If you want to compare the slopes for BL for the various treatments in trt then you need a linear model.


How to make a for loop only apply to certain columns

Question: I have a data frame with 5 columns. The function below creates and outputs 5 small, 3-column datasets featuring the first two columns of my dataset ("country" and "year") and one each of the other 5 columns.
library(dplyr)

# My data (sample)
country <- c("GR", "GR", "GR", "AL", "AL", "AL", "GE", "GE", "GE")
year <- c(1990, 1991, 1992, 1994, 1997, 1996, 1991, 1992, 1993)
pop <- c("i", "i", "j", "j", "j", "i", "i", "i", "i")
category <- c("1", "2", "2", "2", "2", "2", "1", "1", "2")
age <- c(14, 13, 12, 18, 19, 17, 20, 21, 19)

sample_data <- data.frame(country, year, pop, category, age)
rm(country, year, pop, category, age)

# My function
new.datasets <- function(df, na.rm = TRUE, ...){
  i=1
  for (c in df){
    new_df <- select(df, country, year, i)
    assign(paste("df_new_", i), new_df, envir = globalenv())
    i=i+1
  }
}
new.datasets(sample_data)

Using my current function, the first two datasets produced by my function only contain two columns: "country" and "year". The next three datasets produced contain "country", "year" and one each of the remaining columns ("pop", "category" or "age").
I would like to modify my function so that it DOES NOT produce the first two datasets, which only contain "country" and "year". Rather than creating these first two and then removing them, I'd like them to never be produced at all, if possible. Can you help me out?
(Unfortunately, I can't take any easy workarounds like using rm() to remove these datasets afterward, because this is a very simplified version of my actual problem/code, which requires me to remove these datasets as such.)
Thanks! -- New R User
Comment: `for (c in df[-(1:2)])`? But I agree, forget that `assign` exists.
Comment: Why do you assign them to your global environment? Much better to put them in a list and modify the list as per your requirements
Comment: I agree, I'm not actually doing this in my real function, I just tried to create some kind of basic function that shows my problem for the purposes of this question. My overall goal is to see if there's some way to modify the "for (c in df)" bit to exclude the first two columns "c".
Comment: `for (c in df)` - is that a typo?
Comment: Is there any way to use the @Roland solution but to apply something like `[-(1:2)]` to `c` instead of to `df`? Your solution works super well in this situation but when I try to apply it to a for loop with many more steps, modifying the dataframe used with the `[-(1:2)]` makes some of the steps not work.
Answer: In R it is always recommended to avoid explicit loops and instead use the  apply family of functions which are similar to a "for loop" but generally much faster. You also don't need to create a function for this. You can lapply through the column names:
new.datasets <- lapply(colnames(sample_data)[3:ncol(sample_data)], function(x){
    new_df <- cbind(sample_data[,1:2], sample_data[,x])
    assign(paste("df_new_", x), new_df, envir = globalenv())
}

as Sotos has mentioned in his comment you don't need to assign each dataframe to a new variable. Instead you can keep them in a list and perform your next steps on this list. So what I would recommend is:
new.datasets <- lapply(colnames(sample_data)[3:ncol(sample_data)], function(x){
        new_df <- cbind(sample_data[,1:2], sample_data[,x])
    }

This will give you a list of dataframes named "new.datasets".
Comment: `apply` being faster than `for` is an old myth. [See here for lots of detail](https://stackoverflow.com/a/2276001/903061). In fact, since R version 3.6 included just-in-time compilation, `for` loops are often **slightly** faster than `apply`. But the speed differences are tiny. What's nice about `apply` is that, for simple functions, it can be shorter for a human to read and write.
Comment: Please see `fortunes::fortune(98)` on why `apply` is normally "discouraged." In fact, this as I found out applies to python too. `apply` I read is only good for a margin of 1. There is almost certainly a vectorised alternative.
Comment: @Gregor, I am not an expert in R, but I have some experience in working with large datasets and I saw significant differences between for and apply loops. Maybe I am biased, but even the post you linked does not clearly state that "apply being faster than for is an old myth".
Comment: @Sbamo, the first sentence of the accepted/most upvoted answer is *"The apply functions in R don't provide improved performance over other looping functions (e.g. for).*" That seems pretty clear to me. The "old myth" part is my language - as that question and answer are almost 10 years old, I think the "old" part is justified too.
Comment: If you find examples lying around in blogs, gists, etc., where `apply` family functions are faster than `for` loops, it's usually a poorly written for loop. Often the examples will use a vectorized function in apply, and a non-vectorized function in the for loop. Or a problem of not pre-allocating a container for the results: `c()` or `rbind()` or similar used in each iteration of a loop to accumulate a result will kill performance. There are plenty of bad ways to write a slow for loop, but that doesn't mean for loops are inherently slow.
Comment: I would advise a slightly modified `lapply` approach here. `lapply(3:ncol(sample_data), function(x) sample_data[, c(1, 2, x)])` is a little more concise, and more importantly keeps nice column names in the result. In your version, the name of the 3rd column in each result is `sample_data[,x]`


How to set a deployed printer as default using group-policy?

Question: Using a windows-server-2008-r2 I added the print-management role to it.
Using that, I added a new printer

and attached it to a currently set GPO

Assuming I done things right, looking at the linked GPO - I see the correct values

In the past, I used the pushprinterconnection.exe to set a printer as default.
Q: Is that STILL the way to set a deployed printer as default?
Answer: You can't do it through Deployed Printers. If you use Group Policy Preferences then you can choose which printer is default. Give that a go instead.
Comment: What GP preferences?
Comment: Printer preferences...


Is inheritance between content types possible?

Question: I just read Should I use a seperate content type?, and it's got me thinking.
Say I had a site with twenty-five content types, with some of the content types sharing enough of the features of some of the other content types to make some kind of "inheritance" worthwhile: I'm thinking in terms of object-oriented PHP here. Is content type inheritance possible in Drupal 6? Is it possible in Drupal 7? Or 8? Could a module be written to make it possible?
Comment: http://drupal.org/project/bundle_inherit :)
Comment: Potential duplicate of http://drupal.stackexchange.com/questions/27741/inheriting-fields-from-a-base-content-type
Comment: I can't answer my own question for another 7 hours, apparently / Thanks for the link, very useful :)
Answer: For Drupal 7 there's the Bundle Inherit module.
Comment: Thanks! This was pretty easy...I wrote a few more details here: http://drupal.stackexchange.com/questions/11645/how-to-copy-content-type-in-drupal-7/75421#75421
Answer: You might consider using References module which is exactly what it sounds like, the ability for one node to reference another node for which you can easily retrieve the information of the referenced node/user/etc. through Views or the PHP API. This is very similar to using taxonomies except they are treated as a node would generally be treated.
You can set up automatic back references with a module or simply do a back reference through views computationally upon request.
edit: This references system is less like inheritance and more like composition which is generally the preferred method in object orientation computer science theory.


Integral of a function with uniform kernel

Question: I am trying to understand question 9-1 on p.334 in Cameron & Trivedi (link) where I have to calculate the bias of a kernel density estimate at $x=1$ and $n=100$, where we assume that the underlying density is standard normally distributed, $N(0,1)$. The kernel is uniform, that is we have
$$K(v)=0.5 \times 1(|v|<1)$$
where $v$ is defined as $(x_i-x)/h$ and $\mathbf1$ is the indicator function, that is either $1$ if its argument is true and $0$ otherwise; $h$ is the bandwidth, given to be $1.0$ in this example.
The bias of the kernel is then "simply" obtained as
$$\text{bias} = 0.5h^2f''(x)\int v^2 K(v)dv$$
This doesn't seem to difficult at first. I know $h$, I can calculate $f''(1)$ from the normal distribution analytically.
However, I am stuck on finding $\int v^2 K(v)dv$.
Is there a standard trick, a table or anything that can help me calculating it? I could do it numerically but I believe it should be possible to do it analytically too.
ps this is not a homework question, just curiosity how to apply Kernel density estimates without relying on numerical computations.
Comment: I answered my own question though already. So, it's not really necessary and it's a stupid question. Now I am stuck at something else. At the inflexion point x=1, the bias must be zero right? This is because f''(1)=0 and thus the bias disappears?
Comment: I don't think it's a stupid question. It's a simple question, but that's not at all the same thing; it may still be very useful to later readers, if the question is clear enough. I'll fix it myself.
Comment: ok. Do you understand my other question? Does the bias at x=1 for a standard normal distribution become zero since f''(1)=0? Is that correct?
Comment: Yes, the bias formula you give is a function of $x$, and $x$ only enters there via $f''(x)$, so if that's zero the bias must be zero.
Comment: Someone else mentioned it converges as h^4 but did not give any details. That's why I am confused. Note, however, the bias stems from a Taylor expansion of f(hv+x) around x, so it may be that this becomes zero not entirely true... Intuitively I believe the bias must be zero as there is no inflexion at x=1 for the standard normal distb.
Comment: That would be a lot of more to explain, I'd have to roll out all the nonparametric theory of density estimation. The bias is a formal shortcut for iid data and a cutoff of the Taylor expansion after f''(x). This is pretty basic and widely used in the literature. If someone knows their way around nonparametric density estimation he or she would know how to answer this question.
Answer: I posted that question on math exchange too as it seems more to be maths related. Well, anyhow it seem that this is indeed trivial as shown by a another user.
As the Kernel is 1 within the Integral, you apparently just integrate
$$0.5 \int v^2 dv $$ between -1 and 1 and end up with 1/3.
I was confused how exactly to treat a Kernel under an integral and thought it was going to be more complex with chain rules etc.
Secondly, at x=1, the second derivative of the standard normal distribution is zero. Hence, the bias is zero regardless of h or the choice of Kernel.


MS SQL New Publication Wizard - Unable to connect to server 'Server Name'

Question: I am trying to create a new publication.  SQL Standard 2008 windows 2008 r2.
I am logged into the sql server management studio on the server that sql is installed.
I have logged into the sql server using the computer name.
I right click on local publications under the replication folder and select new publication.
It says:
SQL Server is unable to connect to server 'ServerName'
Additional infromation:
  Sql Server replication requires the actual server name to make a connection to the server.  Connections through a server alias, IP address, or any other alternate name are not supported.  Specify the actual server name, 'ServerName'. (Replication.Utilities)
I am putting the computer name of the server name I am on but it still doesn't work.
I tried created an alias through the sql configuration.
I put an entry in the hosts file point back to itself.
I tried changing my computer name and sql server name.
Comment: I change the server name to be exactly the same as the computer name and that fixed it.
Comment: could you please tell me how can i change the server name ?
Answer: If you are using SSMS and specifying anything but what @@servername returns on the server as your connection string, that's your problem.  Maybe you've put in a port number, using a DNS alias for the server or anything else like that.  RMO (replication management objects, the technology that SSMS is using for the new publication wizard) is silly about the server name.
Comment: Your right, I was using the computer name to try to connect to it.  As soon as I changed the sql server name to match the computer name that fixed it.
Comment: Could you please direct me how can i fix each one of those reasons ? like "PortNumber" or "DNS Alias" ?
Comment: @Mostafa: There's nothing to fix. If you're using anything but what the server thinks it's name is, Management studio simply won't allow you to look at replication stuff.  So, check do a "select @@servername" on the server and compare that with what name you used to connect to the server.  If they're not the same, just connect with whatever @@servername returned.


where is the kernel size and stride specified in convolution?

Question: I am currently a bit in doubt about the definition of a convolution...
I just been reading the paper which describes convolution as way of transforming
input image -> "convolution" -> Convolution map
But the way they describe it in the math seems a bit off in my head -  They seem to describe it as it was an neural network would get an input * weight + bias.
This is how they describe it math wise.
$$q_{j,m} =\sigma\left(\sum_{c = 0}^{\text{total}~ \text{number} ~ \text{column}} \sum_{r = 0}^{\text{total}~\text{number}~\text{rows}} \text{img}(r,c) \text{W}_{c,j,r} + W_{0,j}\right)$$
And the complete convolution map $Q_j$, is computed as:
$$Q_j = \sigma \left( \sum_{c = 0}^{\text{total}~\text{number} ~\text{columns}} \text{img}(:,c) \ast W_{c,j} \right) $$
img(r,c) is the pixel unit stored in row = r and column = c.
img(:,c) is a row vector  for column = c  of the image.
And W is a weight matrix of size $\left( (C \cdot R) \times J\right)$
\begin{bmatrix}
    w_{111} & w_{121} & x_{131} & \dots  & x_{1J1} \\
    \vdots & \vdots & \vdots & \vdots & \vdots \\
    w_{C11} & w_{I21} & x_{C31} & \dots  & x_{CJ1} \\
    \vdots & \vdots & \vdots & \vdots & \vdots \\
    w_{C12} & w_{C22} & w_{C32} & \dots & x_{CJ2} \\
    \vdots & \vdots & \vdots & \vdots & \vdots \\
    w_{C1R} & w_{C2R} & w_{C3R} & \dots & x_{CJR} \\
\end{bmatrix}
$W_{c,j,r}$ is one unit in the weight matrix
and $W_{c,j}$ is a 1d vector of length $\left(C \cdot R \right)$
Where is the actual convolution being stated?.. where is kernel size specified, and the stride.   I guess the formula is a standard convolution, but where in the formula is the stride and kernel size defined?
I guess they are using multiple convolutions, which J describes.
So the weight asscociated with one convolution is with weight vector $W_{c,j}$
About weight sharing!
enter image description here
Given an image - Convolution is applied which reduces the depth of the image
Is the weight connecting the convoluted image to the an output neutron the same or different?
Meaning will the weight be updated as one unit, or as an individual unit,
Comment: All you need to know is that in convolutional neural networks, we can compute the convolution of the output of the layer $L$ with a filter $h$ to obtain $L \ast h$ that we use as the input for the neurons of the next layer. Of course, the weights of $h$ are optimized as if they were the weights of a neuron.
Comment: ... That isn't that helpful..   Thanks for trying though.. But i wanted to know how the weight are shared.  I guess CNN reduces the depth of the image, and doing the convolution gives a reduced number of neuron which is then connected to a conv_feature map.   Would that then  mean that if the convolution result in one value,  there would only be one connection?
Comment: No, the convolution doesn't give a reduced number of number of values, but you can add a decimate step. $L$ is an array of $n \times m$ values (the output of $nm$ neurons) and $L \ast h$ (decimated or not) is another array $n_2 \times m_2$ used as the input of $n_2 m_2$ neurons. That's what you need to understand. During the backpropagation, you'll need to compute the gradient with respect to the coefficients of $h$ accordingly.
Comment: hmm... i guess I will add a visual representation of how i understand it..
Answer: In a general neural networks, two consecutive layers $u \in \mathbb{R}^N,v\in \mathbb{R}^M$ are related by $v = f(M u)$ (or $v = f(Mu+b)$ if you want a bias) where $M$ is a linear operator $\mathbb{R}^N\to \mathbb{R}^M$ (a matrix $n \times m$) and $f(x)_i = \tanh(x_i)$. The weights are the $M_{i,j}$.
In a convolutionnal layer, it is the same except $Mu$ is replaced by $u \ast h\ \ $ (or  $d(u\ast h)$ where $d$ is a decimation operator).
So a convolutionnal layer is just a particular case of the general layer, the only difference being the number of weights, how they are shared among the layer, and how you'll compute the gradient (during the backpropagation) accordingly.
Comment: but how are they shared..  Is my visualisation correct?
Comment: @ASD Share what with what ?.. I can't be more clear than what I wrote.
Comment: It is the last three lines I am referring to.. How are the weight being shared, the number of weight? and whether backpropagation trains all weight or see them as the same weight? This ultimately leading me to defining the kernel size.
Comment: @ASD I can't help you deciphering it, you need to read your .pdf with what I wrote in mind. In convolutional neural networks there are a lot of parameters indicating the "topology of our network" ie the number of layers, the size of the filter, $n$ and $m$, ...


Erro Visual Studio 2015

Question: Quando eu rodo uma aplicação, ou paro ela. Me deparo com este erro:

O que é isso, e como resolver ?
Answer: Amigo este erro não aconteceu comigo, mas pesquisando na internet encontrei algo que pode te ajudar
Pelo jeito o arquivo PerformanceDebuggerSwimLanesView.html possa estar corrompido e ele fica na pasta **c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Platform\DiagnosticsHub\WebViews** então você deverá subscrever o arquivo por este Arquivo no github
A resposta do Ties foi aceita pelo autor do tópico, verifique você mesmo.
Link que encontrei apos pesquisa na internet

Erro Visual Studio 2015


Pegar classe CSS de outros sites usando PHP HTML DOM Parser

Question: To tentando pegar uma classe de um site de notícias, mas não estou conseguindo.
<code>$titulo = $html->find('feed-post-body-title gui-color-primary gui-color-hover', 0)
               ->innertext;

echo $titulo;
</code>
Ta dando esse erro:

Notice: Trying to get property of non-object

Como faço pra conseguir pegar classes ou ids?
Comment: Olha a resposta dessa dúvida http://stackoverflow.com/questions/15761115/find-div-with-class-using-php-simple-html-dom-parser acho que é exatamente isso que você deve fazer.
Comment: Amigo, a variável `$html` está recebendo o que exatamente?
Comment: $html = file_get_html('url_do_site');
Comment: Ta recebendo a url do site que eu quero pegar a classe
Comment: Amigo, tente como este exemplo

`foreach($html->find('img') as $element)
       echo $element->src . '';`

No entanto, talvez seu retorno do file_get_html está dando erro, ou a variável `$html` está como array, tem que buscar saber como é exatamente o retorno da função e em que estado ou qual conteúdo existe dentro da variável.
Comment: Pegar os objetos normais HTML eu consigo... O problema ta sendo pegar algum objeto com determinado ID ou Class css
Comment: Obrigado amigo! Ajudou bastante!
Answer: Você precisa especificar o elemento que está procurando, se é uma div, um  p e etc...
Se fosse uma div por exemplo, ficaria assim:
//Usando o "." (ponto) para especificar que é uma classe
<code>$titulo = $html->find('div.feed-post-body-title', 0);
</code>
ou
<code>$titulo = $html->find('div[class=feed-post-body-title]', 0);
</code>
//Se você estivesse procurando por um elemento com id no lugar da classe:
//Usando  "#"  para especificar que é um id
<code>$titulo = $html->find('div#feed-post-body-title', 0);
</code>
ou
<code>$titulo = $html->find('div[id=feed-post-body-title]', 0);
</code>


ReactJS + Redux: Infinite render loop with (functional) Component wrapper + dispatch (state) in child Component

Question: i created an infinite render loop with an Component wrapper and a child Component that calls a dispatch function (to change the state in redux), but I do not see why this component has to be rerendered, because the props do not change imo.
Wrapper:
export default function(ComposedComponent){
    class Authenticate extends Component {
        componentWillMount(){
            //If not logged in
            if (!this.props.loggedIn){
                this.context.router.history.replace({ pathname: '/login' });
            }
            console.log("Mount parent");
        }
        render(){
            return(
                    <ComposedComponent {...this.props} />
                );
        }
    }
    Authenticate.propTypes = {
        loggedIn: PropTypes.bool.isRequired
    }
    // Provide router in context for redirect
    Authenticate.contextTypes = {
        router: PropTypes.object.isRequired
    }
    const mapStateToProps = state => ({
        loggedIn: state.user.loggedIn
    })

    return connect(mapStateToProps)(Authenticate);
}

Child Component:
class InternalArea extends Component {
    componentDidMount(){
        this.props.setTitle(this.props.title);
        console.log("Mount child");
    }

    render(){

        return(

                This is an internal area for logged in users.

        );
    }

}
const mapDispatchToProps = dispatch => bindActionCreators({
        setTitle
    }, dispatch)

export default connect(
    null,
    mapDispatchToProps
 )(InternalArea);

The Wrapper is integegrated as a Route (PropsRoute just passes additional props) in App.js:
 <PropsRoute exact path="/internal-area" component={requireAuth(InternalArea)} title="Internal Area" />

Removing this.props.setTitle(this.props.title); in the child component solves the loop error, but I need to dispatch it there. Or should i move it? Why does it change the props?
Answer: Finally I figured it out how to not create this loop by not using a functional wrapper but a Wrapper component that receives the Route Component as child in App.js. This solution is suggested in: https://www.cuttlesoft.com/infinitely-dispatching-actions-react-redux-react-router-v4/
Nevertheless, I do not 100% understand why this loop is created. I m a bit confused that the functional wrapper is still an often suggested method for redirection, although it seems not to work with dispatching a redux action in the child component.