Making a prediction using Fixed Effects

Question: I have a simple data set for which I applied a simple linear regression model. Now I would like to use fixed effects to make a better prediction on the model. I know that I could also consider making dummy variables, but in reality is my data over several years and has more variables so I would like to avoid making dummies.
My data and code is similar to this:
data <- read.table(header = TRUE,
                   stringsAsFactors = FALSE,
                   text="CompanyNumber ResponseVariable Year ExplanatoryVariable1 ExplanatoryVariable2
                   1 2.5 2000 1 2
                   1 4 2001 3 1
                   1 3 2002 5 7
                   2 1 2000 3 2
                   2 2.4 2001 0 4
                   2 6 2002 2 9
                   3 10 2000 8 3")

library(lfe)
fe <- getfe(felm(data = data, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 | Year))
fe
lm.1<-lm(ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2, data=data)

prediction<- predict(lm.1, data)
prediction

check_model=postResample(pred = prediction, obs = data$ResponseVariable)
check_model

For my real dataset I will make a prediction based on my test set but for simplicity I just use the trainingset here as well.
I would like to make a prediction with the help of the fixed effects that I found. But it does not seem to match the fixed effect right, anyone who knows how to use this fe$effects?
prediction_fe<- predict(lm.1, data) + fe$effect

Answer: You have to add the estimated fixed effects to the prediciton data frame.
library(lfe)

##data
d <- read.table(header = TRUE,
                   stringsAsFactors = FALSE,
                   text="CompanyNumber ResponseVariable Year ExplanatoryVariable1 ExplanatoryVariable2
                   1 2.5 2000 1 2
                   1 4 2001 3 1
                   1 3 2002 5 7
                   2 1 2000 3 2
                   2 2.4 2001 0 4
                   2 6 2002 2 9
                   3 10 2000 8 3")

##regression
e<-felm(data = d, ResponseVariable ~ ExplanatoryVariable1 + ExplanatoryVariable2 | Year)

##fixed effects data
d.fe<-getfe(e)
##prediction sample
p<-d #could be a different sample, but with the same covariates
#add columns on fixed effects
p<-merge(p,d.fe[d.fe$fe=="Year",],by.x="Year",by.y="idx",all.x=T)
names(p)[grep("^effect$",names(p))]<-"effect.Year"
#  if you have more than one fixed effect,
#  you should continue here, adapting the two lines above. eg. fixed effects on ComanyNumber
#reorder
p<-p[order(p$CompanyNumber,p$Year),]

##predict
#coefficients:
predicted.values<-
  as.matrix(p[,rownames(e$coefficients)]) %*% (e$coefficients) + # covariates * coefficients
  p$effect.Year # fixed effects from years

##test
round(predicted.values + e$residuals- p$ResponseVariable,6) # only works if the order of all observerations conincide

Note, that the data object name is now d not data, to avoide confusion.

Making a prediction using Fixed Effects


Delphi TClientDataset SQL Server RowVersion

Question: Does anyone have any experience using SQL Server 's RowVersion column with TClientDataset? Specifically, I need to get the RowVersion value back on insert. It seems to get the RowVersion value back on updates but a newly inserted row it does not.
Answer: This seems to involve a similar problem to when you need to use a CDS with
a SqlServer table which has an Identity field, where the value in the field
is set by the server, so you don't know it during the CDS-side insert operation.  One way to do that is explained in this article
http://edn.embarcadero.com/article/20847.  It involves
assigning a temporary, negative, value to the Identity field in the CDS
in the OnNewRecord event, and then using the Refresh method of the CDS to
retrieve the server-side identity field value once the new record has been
posted to the server by calling ApplyUpdates.
In D7, which I'm using for this answer as a sort of lowest common denominator,
this technique seems to work fine to a RowVersion field as well.  An
extract from a sample project is below.  To get it working, I had to define
the RowVersion field as an ftBytes field with a size of 8, and to change
the ProviderFlags to exclude pfInWhere and pfInUpdate from the
AdoQuery's identity field, ID.
The RowVerStr field in the CDS is just to display the value legibly in a TDBGrid.
type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSetProvider1: TDataSetProvider;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    ADOQuery1ID: TAutoIncField;  //  The ID field is an Identity field on the server
    ADOQuery1IntValue: TIntegerField;  //  a user field
    CDS1: TClientDataSet;
    CDS1ID: TAutoIncField;
    CDS1IntValue: TIntegerField;
    CDS1RowVersion: TBytesField;
    CDS1RowVerStr: TStringField;  //  just for display, fkInternalCalc, size = 20
    ADOQuery1RowVersion: TBytesField;
    procedure FormCreate(Sender: TObject);
    procedure CDS1AfterPost(DataSet: TDataSet);
    procedure CDS1AfterDelete(DataSet: TDataSet);
    procedure CDS1NewRecord(DataSet: TDataSet);
    procedure CDS1AfterInsert(DataSet: TDataSet);
    procedure CDS1CalcFields(DataSet: TDataSet);
  public
    FID : Integer;  //  To generate temporary value for CDS identity field
    function NextID : Integer;
  end;

[...]

function GetRowVerString(V : Variant) : String;
var
  i,
  Dim,
  Min,
  Max : Integer;
  i64 : Int64;
begin
  Result := '';
  if not VarIsArray(V) then Exit;
  Dim := VarArrayDimCount(V);
  Max := VarArrayHighBound(V, Dim);
  Min := VarArrayLowBound(V, Dim);

  for i := 0 to 7 do
    Int64Rec(i64).Bytes[i] := V[i];
  Result := IntToStr(i64);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //  CDS1ID is an ftAutoInc field, so we need to remove its read-only flag so
  //  that we can assign it a temporary negative value in the OnNewRecord event
  CDS1ID.ReadOnly := False;

  AdoQuery1RowVersion.ProviderFlags := AdoQuery1RowVersion.ProviderFlags - [pfInWhere, pfInUpdate];

  CDS1.Open;
  Caption := IntToStr(CDS1.RecordCount);
end;

procedure TForm1.CDS1AfterPost(DataSet: TDataSet);
begin
  if CDS1.ApplyUpdates(0) = 0 then
    CDS1.Refresh;
end;

procedure TForm1.CDS1AfterDelete(DataSet: TDataSet);
begin
  CDS1.ApplyUpdates(-1);
end;

function TForm1.NextID: Integer;
begin
  Dec(FID);
  Result := FID;
end;

procedure TForm1.CDS1NewRecord(DataSet: TDataSet);
begin
  CDS1.FieldByName('ID').AsInteger := NextID;
  CDS1.FieldByName('IntValue').AsInteger := Random(100);
end;

procedure TForm1.CDS1AfterInsert(DataSet: TDataSet);
begin
  CDS1.Post;
end;

procedure TForm1.CDS1CalcFields(DataSet: TDataSet);
begin
  CDS1RowVerStr.AsString := GetRowVerString(CDS1RowVersion.Value);
end;

Comment: `rowversion` is a [synonym (actually replacement) for `timestamp`](https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql).
Comment: @MartynA: your solution does work.  Seems excessively chatty, but I am not able to implement it in my environment due to restrictions placed upon me beyond my control.


Baixa Performance ao consultar DataTable C# - Plan Coded Ui Test

Question: Estou com um problema de desemepnho ao consultar os números de um DataTable-X com um DataTable-Y, tenho que verificar se o número do DataTable-X existe no DataTable-Y (e caso encontre complementar o DataTable-X com os dados do DataTable-Y), da forma que estou fazendo gasto certa de 50 minutos.
Quantidade de Linhas de cada DataTable:

DataTable-X => 38.258 linhas (10 colunas)
DataTable-X => 61.235 linhas (4 colunas)

Na prática, o loop ocorre DataTable-X x DataTable-X vezes, isso é muito, mas 50 minutos acho tempo de mais.
Segue o código:
for (int i = 0; i < dTx.Rows.Count; i++)
{
    bool ganbis1 = false;
    foreach (DataRow oLinha in dTy.Rows)
    {
        if (oLinha[0] != "")
        {
            if (Convert.ToInt64(oLinha[0].ToString()) ==
                 Convert.ToInt64(dTx.Rows[i][1].ToString().Replace(" ","")))
            {
                // dTx = "Coluna1", "Coluna2", "Coluna3", "Coluna4",
                // "Coluna5", "Coluna6", "Coluna7", "Coluna8", "Coluna9", "Coluna10"

                // dTy = "Coluna1", "Coluna2", "Coluna3","Coluna4"
                // Preenche coluna 7

                if ((Convert.ToString(oLinha[1]) == "") ||
                    (Convert.ToString(oLinha[1]) == "#N/A"))
                {
                    dTx.Rows[i][7] = "#n/D";
                }
                else
                {
                    dTx.Rows[i][7] = Convert.ToString(oLinha[1]);
                }
                // Preenche coluna 8
                if ((Convert.ToString(oLinha[2]) == "") || (Convert.ToString(oLinha[2]) == "#N/A"))
                {
                    dTx.Rows[i][8] = "#n/D";
                }
                else
                {
                    dTx.Rows[i][8] = Convert.ToString(oLinha[2]);
                }
                // Preenche coluna 9
                if ((Convert.ToString(oLinha[3]) == "") || (Convert.ToString(oLinha[3]) == "#N/A"))
                {
                    dTx.Rows[i][9] = "#n/D";
                }
                else
                {
                    dTx.Rows[i][9] = Convert.ToString(oLinha[3]);
                }
                // Preenche coluna 10
                if ((Convert.ToString(oLinha[4]) == "") || (Convert.ToString(oLinha[4]) == "#N/A"))
                {
                    dTx.Rows[i][10] = "#n/D";
                }
                else
                {
                    dTx.Rows[i][10] = Convert.ToString(oLinha[4]);
                }
                ganbis1 = true;
            }
        }
    }
}

if ((ganbis1 == false) && (i != dTx.Rows.Count-1))
{
    // Quando não for encontrado preencher com #n/D
    dTx.Rows[i][7] = "#n/D";
    dTx.Rows[i][8] = "#n/D";
    dTx.Rows[i][9] = "#n/D";
    dTx.Rows[i][10] = "#n/D";
}

Comment: Entendi @D1Dih mas, se você fizesse assim, a leitura de dados jogar em duas tabelas no banco de dados (poderia ser até temporárias) e depois fazer um procedure para agilizar o processo e logo após o resultado gerar novamente os xlsx?
Comment: Não sei dizer pra você e mensurar, mas, é lento realmente ler tantas linhas e ficar comparando dois itens muito grandes na memória, na minha opinião você precisa organizar os dados primeiramente (eu jogaria isso num banco de dados) e depois extrair os dados. Esse processo pode melhorar o desempenho visto que dados organizados e lógicos tende ser mais fácil comparar ... !!!
Comment: Você traz os dados do banco de dados? Se for banco de dados não poderia fazer uma comparação SQL  e fazer essa operação no banco de dados?
Comment: Infelizmente eu tenho que fazer a leitura de N arquivos xlsx, e ao realizar esse tratamento gravo em um novo xlsx com tudo unificado.
Comment: @VirgilioNovic entendi. Essa forma é possível fazer sim. Você acredita não haver melhoria na forma utilizada acima?
Comment: @VirgilioNovic Bem observador e boa sugestão.
Comment: Segui essa linha de organizar os dados e incluir um break quando encontrar o item. Ordenei as colunas que estou comparando em ordem crescente:

DataView dv = dTx.DefaultView;
dv.Sort = "Nome da coluna a ser ordenada Asc";
dTx = dv.ToTable();

DataView dvTwo = dTy.DefaultView;
dvTwo.Sort = "Nome da coluna a ser ordenada Asc";
dTy = dvTwo.ToTable();

Em sequência inclui um break quando encontrar o elemento da busca, para que o laço não continue processando após encontrar.
Comment: haaa... isso reduziu pela metade o tempo de processamento. :)
Answer: Você está iterando sobre esses arrays 2342728630 vezes, convertendo strings e comparando em todas as iterações. Não me surpreende que que esteja demorando.
Acredito que o mínimo que você possa fazer seja criar um array associativo para evitar um for dentro de outro for, dessa forma você só precisaria percorrer cada array uma vez, reduzindo as iterações para 99493.
Dictionary<long, DataRow> dic = new Dictionary<long, DataRow>();

foreach (DataRow oLinhaX in dTx.Rows) {
    dic.Add(Convert.ToInt64(oLinhaX[1].ToString().Replace(" ",""))), oLinhaX);
}

foreach (DataRow oLinhaY in dTy.Rows) {
    if (oLinhaY[0] == "") continue;

    long oLinhaYInt = Convert.ToInt64(oLinhaY[0].ToString());

    if (dic.ContainsKey(oLinhaYInt)) {
        DataRow oLinhaX = dic[oLinhaYInt];
        //seu código aqui
    }
}

Isso deve dar uma boa aliviada, mas ainda espere alguns minutos de processamento. Quer mais desempenho? Parta para uso de múltiplos núcleos.
Edit
Não foi mencionado na pergunta, mas essa solução só funciona se a relação entre as tabelas for one-to-one.
Comment: Muito boa a sua resposta, vi que você é novo no site, depois de uma olhada no nosso código de conduta https://pt.stackoverflow.com/conduct e se inteire de como nossa comunidade trabalha.
Answer: Tem aqui uma outra forma de abordar o problema:
var rowsX = dtX.Rows.Cast<DataRow>().Select(r => r[1].ToString().Trim());
var rowsY = dtY.Rows.Cast<DataRow>().Where(r => r[0].ToString() != string.Empty).Select(r => r[0].ToString());

var rows = rowsX.Intersect(rowsY);
var dataRowsX = dtX.Rows.Cast<DataRow>().Where(r => rows.Contains(r[1]));

foreach (var row in dataRowsX)
{
    var dataRowY = dtY.Rows.Cast<DataRow>().FirstOrDefault(r => r[0].ToString() == row[1].ToString());

    // código...
}

Basicamente compara as linhas dos dois DataTable e depois intersecta num só.
Por fim, utiliza essa intersecção para percorrer o DataTable-X e obtendo, para cada linha, o DataTable-Y para alteração de valores.
Comment: consegue me explicar melhor. Infelizmente pra mim o cast não foi reconhecido no dtX/dtY. Para reconhecer tiver que usar dataset.table[nome da tabela].Rows.Cast... Gostaria de testar sua sugestão para aferir o tempo de processamento.
Comment: Qual dos casts não foi reconhecido? O `dtX.Rows.Cast`?
Comment: Na verdade todos os cast não reconheceram. E sobre meu comentário sobre dataset acima, num tem nada haver, também não reconheceu.
Comment: Ha, provavelmente precisa do `using System.Linq` para funcionar :)
Comment: isso mesmo :D. Conseguir compilar até o foreach, Agora apresenta a msg 'Unable to cast object of type 'System.Double' to type 'System.String'.'. Eu estou debugando para entender sua sugestão, porque ainda não entendi :)
Comment: Parti do princípio que a informação que constava das linhas era texto, mas sendo numérico dá erro mesmo. Alterei tudo para `ToString()`. Experimente de novo.
Comment: Sei que uma pergunta escrota, mas eu não tenho usado expressões lambda e o debug vou bem transparente :D... seria possível explicar o que fez esses trechos?
'var rowsX = dtX.Rows.Cast().Select(r => r[1].ToString().Trim());'
Comment: Basicamente cria um enumerado com todos os valores, convertidos para `String`, da coluna 1 de cada linha da tabela X (nesse caso).


Proof of mutual information estimate from Kraskov et al. (2004)

Question: I'm reading the Kraskov et al. (2004) paper about estimating mutual information using the Kozachenko-Leonenko estimate for Shannon entropies. I'm trying to understand the last step in Eq. 17, which says that:
$$k\binom{N-1}{k}\int_0^1 \rm{d} pp^{k-1}(1-p)^{N-k-1}\log{p}= \psi(k)-\psi(N) \ ,$$
where $k$ and $N$ are positive integers, and $\psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}$ is the digamma function.
I've been searching for integral representations of the digamma function, but I am not able to explain this last result (unlike the normalisation of Eq. 16, which I can understand). It seems like a standard result, but I could not find any clear reference. Could anyone please point me towards the solution, or to a paper that has more details about how this result is derived?
Answer: I was able to derive it; I'll leave the idea here in case it can be useful to others as well. I essentially followed this answer.
We can introduce a variable $\alpha$, generalising our integral to:
$$\frac{k}{k-1} \binom{N-1}{k} \int_0^1 \rm{d}p\frac{\partial{p^{\alpha (k-1)} (1-p)^{N-k-1}}}{\partial\alpha} \ .$$
If we can solve this integral, then we will just need to use $\alpha=1$. The integral above is quite easy, since we can take out the derivative and:
$$
\frac{k}{k-1}\binom{N-1}{k}\frac{\partial}{\partial \alpha} \int_0^1 \rm{d} p p^{\alpha (k-1)} (1-p)^{N-k-1} \ ,
$$
which is the standard Beta function. Thus:
$$
\frac{k}{k-1}\binom{N-1}{k}\frac{\partial}{\partial \alpha} \frac{\Gamma(\alpha(k-1)+1) \Gamma(N-k)}{\Gamma(\alpha(k-1)+1+N-k)} \ .
$$
At this point we can calculate the derivative of the ratio:
\begin{align}
&\frac{k}{k-1} \frac{(N-1)!}{k!(N-k-1)!} (N-k-1)!\frac{\partial}{\partial \alpha} \frac{\Gamma(\alpha(k-1)+1)}{\Gamma(\alpha(k-1)+1+N-k)} = \\
& \frac{(N-1)!}{(k-1)!} \left[\frac{\Gamma'(\alpha(k-1)+1)}{\Gamma(\alpha(k-1)+1+N-k)} - \frac{\Gamma(\alpha(k-1)+1) \Gamma'(\alpha(k-1)+N-k+1)}{\Gamma^2(\alpha(k-1)+1+N-k)}\right] \ .
\end{align}
Setting $\alpha=1$, and with $\psi(x) = \frac{\Gamma'(x)}{\Gamma(x)}$, we conclude:
\begin{align}
& \frac{(N-1)!}{(k-1)!} \frac{\Gamma(k)}{\Gamma(N)}\left[\frac{\Gamma'(k)}{\Gamma(k)} - \frac{\Gamma'(N)}{\Gamma(N)}\right] = \psi(k) - \psi(N) \ .
\end{align}
Comment: (+1) thanks a lot for answering your own question!


How can I adjust the volume of my guitar connected to an iOS device?

Question: I can't find a good way to play my guitar along with music on my iOS device. The problem with GarageBand is that I can't adjust the guitar and the music separately, because I want loud guitar and quiet music. Also the music gets a nasty echo effect.
There is another app called David Ellefson that allows me to select a song from my playlists to play along with, but music quality is awful and the guitar is too quiet even at 100% volume.
Is there any difference when connecting the guitar via the headphones jack vs. with a USB cable and the cam. connection kit?
Comment: garage band is basically a DAW (Digital audio workstation). This question may be better suited to AV stackexchange, since the "musical practice and performance" is incidental to the question.
Comment: Poll questions are off-topic, I'm going to edit this to ask about your problem directly.
Comment: About recording, not about music performance, so it belongs elsewhere.
Comment: Recording music is on-topic, though horatio is right that you may be better served by [http://avp.stackexchange.com/](http://avp.stackexchange.com/).
Answer: Plug the guitar into a pre-amp first, then out from the pre-amp to the iOS. A trip to Radio Shack for headphone jacks may be in your future. (1/4" to 1/8" for example)


Decision Trees and Regression - Can predicted values be outside range of training data?

Question: When it comes to decision trees, can the predicted value lay outside of the range of the training data?
For example, if the training data set range of the target variable is 0-100, when I generate my model and apply it to something else, can my values be -5?  or 150?
Given that my understanding of decision tree regression is that it is still a rules based - left/right progression and that at the bottom of the tree in the training set it can never see a value outside a certain range, it will never be able to predict it?
Comment: For a similar question about gradient-boosted trees, see https://stats.stackexchange.com/questions/304962/is-is-possible-for-a-gradient-boosting-regression-to-predict-values-outside-of-t
Answer: You are completely right: classical decision trees cannot predict values outside the historically observed range. They will not extrapolate.
The same applies to random forests.
Theoretically, you sometimes see discussions of somewhat more elaborate architectures (botanies?), where the leaves of the tree don't give a single value, but contain a simple regression, e.g., regressing the dependent variable on a particular numerical independent variable. Navigating through the tree would give you a rule set on which numerical IV to regress the DV on in what case. In such a case, this "bottom level" regression could be extrapolated to yield not-yet observed values.
However, I don't think standard machine learning libraries offer this somewhat more complex structure (I recently looked for this through the CRAN Task Views for R), although there should really not be anything complex about it. You might be able to implement your own tree containing regressions in the leaves.
Comment: @SorenHavelundWelling: unfortunately, [the `mobForest` package has been removed from CRAN](https://cran.r-project.org/web/packages/mobForest/index.html). I'll take a look at the [`partykit` package](https://cran.r-project.org/web/packages/partykit/index.html) that [Achim Zeileis recommended](http://stats.stackexchange.com/questions/190503/decision-trees-and-regression-can-predicted-values-be-outside-range-of-trainin/190507#comment362283_190507).
Comment: Just a heads up that mobForest is back on CRAN: https://cran.r-project.org/web/packages/mobForest/index.html
Comment: I have sparsely read about mobForest which do support leaf regression in R, http://stats.stackexchange.com/questions/48475/mobforest-r-package
Comment: @SorenHavelundWelling: that does sound interesting. Thanks for the pointer!
Comment: One of the first algorithms to provide linear regression models in the leaves of a tree was Quinlan's M5, an approximation of which is available in M5P() in Weka (interfaced in R through RWeka). An unbiased algorithm for the problem, called GUIDE, was first suggested by Loh. Binaries for his standalone package are on his website. Finally, our model-based (MOB) recursive partitioning algorithm encompasses various such models. It is available in the R package partykit: mob() is the generic tool and lmtree() and glmtree() are its adaptation to trees with (generalized) linear models in the leaves.
Comment: I also assume that this applies to models such as K-nearest neighbors?
Comment: Yes. KNN looks at the $k$ nearest neighbors of your new data point (in terms of some distance measure), then takes the average of their DVs as a prediction. As an average of observations, the new prediction can *never* be outside the observations' range
Answer: Also check out cubist in the caret package. It builds linear regressions in the terminal nodes and can extrapolate predictions above and below  the range of response values in the training data. The terminal nodes can also be averaged based on nearest neighbors that is provided as a hyperparameter, so it has potential to provide extremely accurate cross validated predictions.


Replacing strings using grunt-replace

Question: I would like to replace url("images/ string to some other string e.g. ZZZZZZZZZZZZ using grunt-replace.
I use following configuration:
replace: {
    dist: {
        options: {
            patterns: [
                {
                    match: /"/g,
                    replacement: 'ZZZZZZZZZZZZ'
                }
            ]
        },
        files: [
            {
             expand: true, flatten: true,
             src: ['dist/app/css/app.css'], dest: 'dist/app/css2/'
            }
        ]
    }
}

I've tried match: /"/g and /images/g and both of them were replaced, but none of those worked:

match : /"images/g
match : /\"images/g
match : /("images)/g //worked when tested on http://regex101.com/r/tU5iM3/2
match : /(\"images)/g //worked when tested on http://regex101.com/r/tU5iM3/3

What am I missing here?
UPDATE
I've added files section of my grunt-replace configuration + working /images/g example.
I'm using some other Grunt tasks and Grunt replace is last one:
grunt.registerTask('default', ['useminPrepare', 'copy', 'concat', 'uglify', 'cssmin', 'usemin', 'replace']);

But I don't see how it it could break my regex replace. I'm testing my configuration by running command:
grunt -v --stack && grep -R "ZZZZZZZZZZZZ" .
Comment: All the `regexp` matches correctly your goal (in the `http://regex101.com` each expression generates 2 matches, the difference is the last two also generate matched group due to `()` ). Besides looking on the `grunt-replace` documentation:  https://github.com/outaTiME/grunt-replace your `patterns` definition is totally correct, maybe the error is in another place.
Comment: I've updated my question with some more data. I just can't figure out where this error might be...
Answer: First off, your regex101 links point to code using the Python regex library, rather than JavaScript, which is what Grunt uses.  Next, the ( and \ characters need to be escaped, but none of the others do.  The regexp you're looking for is:
/url\("images\//g

(see https://regex101.com/r/aY9hY0/1 to play with it)

Replacing strings using grunt-replace


How to use result table to make another table

Question: I have the following code:
SELECT CD_NO, COUNT(purchasedate) AS most_sales
FROM Artist
GROUP BY CD_NO

This returns me the amount of sales for each CD, as there is an entry of purchasedate for every sale of CD. However i wish from this to find out which date had the most sales for each CD_NO, and instead of showing a number to showing the date of the most_sales on that day. So it would turn a input of purchase dates against a specific CD, and then count them up and find the maximum number of purchases on a specific purchase_date.
For example turns this input:
CD_NO          purchasedate
1              2012-04-04
1              2012-04-04
1              2012-04-03
2              2012-04-03
2              2012-04-03
2              2012-04-03
2              2012-04-02

And turns it into:
CD_NO           most_sales
1               2012-04-04
2               2012-04-03

This is because it counts up the different dates for the CD_NO and then returns which date had the most.
Comment: Is this the 3rd time you ask this or even more?
Answer: SELECT CD_NO, COUNT(purchasedate) AS most_sales
INTO [NEW TABLE NAME]
FROM Artist
GROUP BY CD_NO

ALL YOU hvae to do it put an into statement and the result will go to a new table on a basic level
Answer: SELECT CD_NO, COUNT(purchasedate) AS most_sales
INTO ARTIST_SALES
FROM Artist
GROUP BY CD_NO

this should work,it will create a table called ARTIST_SALES ( you may change this), also you might consider adding the artist id in the second table, so you can select the summarized sales of each artist.
EDIT:
i didn't realize you were using mysql, select into is not supported, you should create a new table first, for example
CREATE TABLE Artist_Sales (
  CD_NO INT NOT NULL,
  SALES_COUNT INT NOT NULL
);

and then execute a insert into,
for example:
INSERT INTO Artis_Sales (CD_NO,SALES_COUNT)
SELECT CD_NO, COUNT(purchasedate)
FROM Artist
GROUP BY CD_NO

good luck
Comment: When i try this it undeclared variable: Artist_Sales
Comment: ok, you are right, i didn't notice it was mysql, select into is not supported, so you should create a new table,
Answer: Can you not just do this:
SELECT CD_NO, MAX(PURCHASEDATE) FROM ARTIST GROUP BY CD_NO
Comment: NO, and this just returns the most recent purchasedate


Module google_auth_httplib2 not found after pip installing google-cloud How can I fix it?

Question: I used pip to install cloud-storage, like this:
$ pip install --upgrade google-cloud

When I started my application, I got an error that said no module named google_auth_httplib2 was found. I used pip list and verified that the package was installed, yet the import kept failing. Should I install it in my project's folder, like this?
$ pip install google-cloud -t [my_path]

UPDATE:
Just for clarity, the error arises in the google-auth-httplib2 module, not the httplib2 module. I recently installed it in the lib folder of my project: my_project/lib/google_cloud_storage. If I try importing google.cloud.storage from lib.google_cloud_storage, it will work it's way until it gets to google_auth_httplib2 module, where it throws a not found error. I've tried placing that module in the same folder as the one calling it, and not even then will it work.
Comment: Yes, it needs to be in the project.
Comment: Already tried that, and I get the same problem :(
Comment: Can you update the OP to show your project directory tree?  You don't need `google-cloud` in the project.  You need the httplib2 module
Comment: I can even call "from google.cloud import storage" in the IDLE shell and it works. But when I use that same line in my project, it gives me that error. I have no idea what the problem might be.
Answer: I just solved the problem, I'll leave the answer just in case anyone else runs into it. I created a folder named google-cloud in lib and used pip:
pip install google-cloud -t [my_project]/lib/google-cloud

And then in appengine_config.py I added:
from google.appengine.ext import vendor

vendor.add('lib/google-cloud')


scheduled tasks running in %systemroot%\system32\

Question: I have a scheduled task that calls a bat file, problem is it does this in c:\windows\system32 instead of the path it exists in (c:\bak), is there a setting that I can change to set it to run in the correct location?
Comment: Just a thought, but perhaps you should simply **add a command at the start of the script to set the correct working directory** and other environment settings?  Scripts that don't work every time can drive someone else trying to fix your script insane if they don't know what the environment should be like.
Answer: Yes - in the "Edit Action" UI, fill out the Start In (Optional) field.
(If you aren't already, use the Task Scheduler to create your scheduled task.)
Comment: @Beuy - my pleasure, I've got several tasks set up and have had to deal with the differences between this incarnation of task scheduling and previous ones. :)


string to JsonObject in C#

Question: What is the best way to convert a (properly Json-formatted) String object into a System.Json.JsonObject?  Right now I create a StringReader and use System.Json.JsonObject.Load.
(This is really kind of academic, since I think I'll always be dealing with the raw Json in System.IO.Stream form.)
Comment: Why would you deal with the Json raw?
Comment: What I meant was, I'll never have the Json in String format, it will always come from the web service in a Stream.
Answer: You could simply use the System.Json.JsonValue.Parse static method to parse a string directly rather than use a StringReader.
Comment: Whoa, how did I miss that one.  Thanks.

string to JsonObject in C#


verificar consulta em 2 tabelas ao mesmo tempo com mysql

Question: Tenho duas tabelas no meu BD (mysql), onde:
1º Tabela: CADASTROS
Com os seguintes campos: ID, NOME, IDADE, CIDADE
2º Tabela: FUNCIONARIO
Com os seguintes campos: ID, ID_CADASTRO, PROFISSAO, SALARIO
Realizando uma consulta na tabela CADASTROS ordenando pelo NOME:
SELECT * FROM CADASTROS ORDER BY NOME ASC

Como realizar uma pesquisa que me retorne apenas os registro existentes na tabela CADASTROS sem correspondência com a tabela FUNCIONARIO?
Answer: Você poderá fazer assim:
select * from cadastros a
where not exists(select * from funcionario b where a.id = b.id_cadastro)

ou assim:
select * from cadastros a
left join funcionario b on b.id_cadastro = a.id
where b.id_cadastro is null

Comment: Obrigado funcionou 100% aqui :)
Answer: Para trazer todos os registros da tabela CADASTRO menos os IDs que estão na tabela funcionário você deve escrever assim:
SELECT * FROM cadastros where id not in ( select id from funcionario ) ORDER BY nome ASC

Comment: tentei sa sua forma mas não deu certo, a forma so ismael funcionou comigo, de qualquer forma vlw ;)
Comment: Qual é o erro que você recebe ao executar esse select que te passei?
Comment: nao retornou nenhum resultado
Comment: Isso significa que todos os ids que estão na tabela cadastro estão na tabela funcionário.
Answer: Você poderá desenvolver da seguinte forma:
SELECT * FROM cadastro as c JOIN funcionario as f ON (c.id=f.id_cadastro)

Desta forma, você poderá exibir os dados da outra tabela também, você pode colocar no * do FROm, os campos que deseja buscar... ex:
SELECT c.nome, f.profissao FROM cadastro as c JOIN funcionario as f ON (c.id=f.id_cadastro)

Neste caso, irá retornar apenas os campos nome e profissão (nome de uma e profissão da outra).
Espero ter ajudado!


Input type file não funciona na webview

Question: Tenho uma pagina em HTML que tem um form de upload de imagens
no Chrome no pc ou em qualquer navegador funciona
mas no meu aplicativo que tem uma WebView que abre esta página
ai clicar em choose file ele não faz nada.
É possível arrumar isso para escolher um arquivo ou abrir a câmera do aparelho ?
( só escolher o arquivo já resolve )
Segue o código do meu fragmento:
public class F_Adm extends Fragment implements AdvancedWebView.Listener {

    private AdvancedWebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.f_adm, container, false);

        mWebView = (AdvancedWebView) rootView.findViewById(R.id.webview);
        mWebView.setListener(getActivity(), this);

        mWebView.loadUrl("http://desapegogames.com.br/area-administrativa");

        return rootView;
    }

    @SuppressLint("NewApi")
    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume();
    }

    @SuppressLint("NewApi")
    @Override
    public void onPause() {
        mWebView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mWebView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        mWebView.onActivityResult(requestCode, resultCode, intent);
    }

    @Override
    public void onPageStarted(String url, Bitmap favicon) {
    }

    @Override
    public void onPageFinished(String url) {
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        mWebView.loadUrl("file:///android_asset/offline.html");
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
    }

    @Override
    public void onExternalPageRequest(String url) {
    }
}

Comment: você quer que seu site tenho a possibilidade de abrir um método no seu código de android?
Comment: não. Quero apenas fazer upload de arquivos pelo input tyle='file' que tem em uma pagina html so que pela minha webview nao funciona mas pelo chrome do celular sim.
Comment: Esta ativado o javascritp do webview?
Comment: sim ta ativo. ele ate abre para selecionar o arquivo mas quando seleciona ele não preenche o input
Comment: tentei mudar para esse plugin https://github.com/delight-im/Android-AdvancedWebView tbm da a mesma coisa vc escolhe o arquivo e o campo não é preenchido.
Comment: Faz um bom tempo que fiz um **webview** mas me lembro que tive um caso semelhante e precisei importar o motor **webkit**, com isso meu webview rodava como se fosse o **chrome**. Não sei se é a sua solução, mas qualquer coisa vc me diz.
Comment: Já viu as respostas a esta [pergunta](http://stackoverflow.com/q/5907369/2556111) no SOen?
Comment: tentei tbm usar i webkit do chrome e não rolou vou postar meu cod aqui.
Answer: Vou te auxiliar de acordo com a documentação do WebView. Segue imagem esquema do componente visual WebView:

Definindo variáveis:
private static final String TAG = TestActivity.class.getSimpleName();
public static final int INPUT_FILE_REQUEST_CODE = 1;
private WebView meuWebView;
private ValueCallback<Uri[]> mFilePathCallback;
private String diretorioFotoImagem;

Definindo um método para armazenar imagem temporariamente armazenarImagemTemp():
 private File armazenarImagemTemp() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File imageFile = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    return imageFile;
}

Definindo um método para adaptar as configurações padrões do WebView com o nome setWebview:
 private void setWebview(WebView webView) {
    WebSettings set = webView.getSettings();

    // habilita javascript
    set.setJavaScriptEnabled(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // oculta zoom no sdk HONEYCOMB+
        set.setDisplayZoomControls(false);
    }

    // habilita debugging remoto via chrome {inspect}
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
    meuWebView.setWebViewClient(new WebViewClient());
}

Só pode haver um método onActivityResult() em cada Activity. Esse método é chamado na sequência de cada chamada a startActivityForResult.
Ao chamar o método startActivityForResult é passado, no segundo parâmetro, um requestCode. Veja:
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
    if(requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
        super.onActivityResult(requestCode, resultCode, data);
        return;
    }

    Uri[] results = null;

    // verifica se a resposta esta ok com a variável estática {RESULT_OK} do Activity
    if(resultCode == Activity.RESULT_OK) {
        if(data == null) {
            // se não houver dados solicitará que tire uma foto
            if(diretorioFotoImagem != null) {
                results = new Uri[]{Uri.parse(diretorioFotoImagem)};
            }
        } else {
            String dataString = data.getDataString();
            if (dataString != null) {
                results = new Uri[]{Uri.parse(dataString)};
            }
        }
    }

    mFilePathCallback.onReceiveValue(results);
    mFilePathCallback = null;
    return;
}

Finalizando com o onCreate() que é o método responsável por carregar os layouts e outras operações de inicialização.
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    // referencia o webview no layout/activity_main.xml
    meuWebView = (WebView) findViewById(R.id.webview);

    // define as configurações basicas
    setWebview(meuWebView);

    if (savedInstanceState != null) {
        // retaura o webview
        meuWebView.restoreState(savedInstanceState);
    }

    meuWebView.setWebChromeClient(new WebChromeClient() {
        public boolean onShowFileChooser(
                WebView webView, ValueCallback<Uri[]> filePathCallback,
                WebChromeClient.FileChooserParams fileChooserParams) {
            if(mFilePathCallback != null) {
                mFilePathCallback.onReceiveValue(null);
            }
            mFilePathCallback = filePathCallback;

            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                // cria local onde a foto deve ir
                File photoFile = null;
                try {
                    photoFile = armazenarImagemTemp();
                    takePictureIntent.putExtra("PhotoPath", diretorioFotoImagem);
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Log.e(TAG, "Unable to create Image File", ex);
                }

                // Continue only if the File was successfully created
                if (photoFile != null) {
                    diretorioFotoImagem = "file:" + photoFile.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                } else {
                    takePictureIntent = null;
                }
            }

            Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
            contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
            contentSelectionIntent.setType("image/*");

            Intent[] intentArray;
            if(takePictureIntent != null) {
                intentArray = new Intent[]{takePictureIntent};
            } else {
                intentArray = new Intent[0];
            }

            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

            startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

            return true;
        }
    });

    // carrega a URL
    if(meuWebView.getUrl() == null) {
        meuWebView.loadUrl("http://192.168.0.3:8080/api.finan.com/");
    }
}

Obs.: não esqueça de criar o WebView no seu XLM.
Para melhorar o código, confira mais detalhes na documentação.
EDIT
Como você ressaltou nos comentário a questão da possibilidade para funcionar no fragment, faça o download aqui da adaptação básica para funcionar.
Comment: @JasarOrion basicamente você tem que verificar duas coisas, que seria a questão da view, pois no lugar de this, é colocado getActivity() e a questão do onActivityResult() para seu fragment que é diferente um pouco. Tente fazer as modificações, caso não consiga, posso verificar depois pra ti. Você conseguiu testar?!
Comment: @JasarOrion fiz aqui pra você, vou disponibilizar em um link pode ser?! Senão a resposta fica muito grande!
Comment: cara deu certo aqui MUITO obrigado te adicionei no skype se você tiver tempo gostaria de saber quanto é sua hora para consultoria.
Comment: consegui implementar o erro tbm abraços. assim que liberar ja te premio pela ajuda muito obrigado novamente.
Comment: @JasarOrion muito bem cara. Nas minhas horas vagas tento ajudar as pessoas, retribuindo pelas muitas dúvidas que já tive e me ajudaram.
Comment: so uma coisa se possível pode m orientar como adaptar este seu cod para funcionar dentro de um fragment? ou se você puder me manda um ex por favor?
Comment: ok hoje anoite vou mecher nisso e volto a informar .muito obrigado
Comment: poxa obrigado ^^ agradeço muito . sou iniciante no android este é o meu primeiro aplicativo da uma olhada no desapegogames.com.br
Comment: ja estou testando. tenho uma ultima dúvida. em caso de offline ou erro onde eu insiro a opção para carregar um arquivo html que esta la no asstes?
Comment: @JasarOrion meu caro, talvez seria melhor você adicionar uma outra pergunta, porque neste caso já sai fora do contexto.

Input type file não funciona na webview


Pegar Elemento Específico CSS com Sinal de +

Question: Minha pergunta tem relação com essa pergunta:
O que significa o sinal de + no CSS?
Estou usando o sinal de + para fazer um efeito em um formulário que tenho.
Funciona, mas a questão é que meu formulário possui validação. Ou seja, quando clico no botão para submeter as informações do formulário é acrescentado no código uma label.error, com a mensagem de erro.
Daí o efeito no campo é perdido porquê o ul.opcoes não é mais o próximo campo quando aparece a label.error.
.select-area-field:focus + ul.opcoes{
    visibility: visible;
}

Por isso minha pergunta é:

Como fazer o CSS encontrar um elemento específico na árvore seguindo essas regras ?

HTML

    <input type="text" name="area" placeholder="Área Desejada" readonly="readonly" class="select-area-field"/>

        @foreach($area as $item)

id !!}">{!! $item->area !!}

        @endforeach

    <input type="hidden" name="cod-area" id="cod-area" />

Com jQuery se costuma fazer assim:
$(document).find('div.wrapper');
Comment: Na verdade eu tenho controle sobre a posição da `label.error`. Eu posso jogar tudo dentro de um container. Acabei de fazer isso e funcionou. Mas isso que perguntei já é possível com **CSS** ?
Comment: Não sei se esse é o melhor título para essa pergunta. :thinking:
Comment: O titulo ta bem ruim mesmo, ao ler o titulo pensei que você queria um seletor CSS para isso: `+` (um elemento com o conteúdo de um "+"). Hehe. Não sei muito bem como melhorar, mas o comando "+" no selector CSS recebe esse nome técnico: [**Adjacent sibling selectors**](http://www.w3.org/TR/CSS21/selector.html#adjacent-selectors), talvez isso possa lhe ajudar. =D
Answer: O que você pode fazer e considero o mais usual e correto nesse caso onde pode haver um elemento entre eles ou não, e no seu caso onde você conhece e tem controle sobre o elemento (label.error).
É adicionar a regra CSS a possibilidade de haver ou não o elemento label.error entre .select-area-field:focus e ul.opcoes, aplicando o estilo CSS para ambos os casos, a implementação poderia ser algo similar a isso:

ul.opcoes {
  visibility: hidden;
}
label.error {
  color: red;
}

/* aqui está o segredo da solução */
.select-area-field:focus + ul.opcoes,
.select-area-field:focus + label.error + ul.opcoes {
  visibility: visible;
}

  <input type="text" name="area" placeholder="Sem label error" readonly="readonly" class="select-area-field" />

Opção 1

Opção 2

Opção 3

  <input type="hidden" name="cod-area" />

  <input type="text" name="area" placeholder="Com label error" readonly="readonly" class="select-area-field" />

  label.error

Opção 1

Opção 2

Opção 3

  <input type="hidden" name="cod-area" />

Exemplo disponível também no jsFiddle.
Comment: Matou a pau, Vélho. Nem pensei nisso. Obrigado.


Probability: Finding the expected time between events

Question: A deck with 52 cards shuffled has each card turned up in sequence until the deck is empty.  Let $X_1$ be the location of the first ace, and so on until $X_4$ represents the placement of the last ace.  Define $\Delta_1$ to be the number of cards before the first ace, $\Delta_2$ the number of cards between the first and second, and so on until $\Delta_5$ represents the cards after the last ace.  I want to find $\mathbb{E}X_2$ using the fact that $\mathbb{E}\Delta_i = \mathbb{E}\Delta_j$ for $1\leq i,j\leq 5$.
My reasoning is that $4+\sum_{i=1}^5\Delta_i = 52$ so $4+\sum_{i=1}^5\mathbb{E}\Delta_i = 52 = 4+5\Delta = 52$ so
$$\Delta = 48/5$$
From this I reason that $\mathbb{E}X_2$ should be $\mathbb{E}\Delta_1 + 1 + \mathbb{E}\Delta_2+1= 2 + 2\cdot 48/5$.
First, since I've never made an argument that worked quite like this, I'm just a little unsure of whether even this is right.  But assuming it is, I wanted to check it against a more direct calculation of the quantity.  However, the only way I could find to approach the problem directly was a nightmare (which is perhaps the point of the exercise).  In effect, it seems to me to bifurcate into a mess of cases:
$P(\Delta_2 = 0)$ already bifurcates into $P(\Delta_2 = 0 | \Delta_1 = 0)P(\Delta_1=0) + \ldots$
Is there any better direct approach or is the only good way to do this through the indirect approach above?
Comment: I've deleted all my comments, because they didn't make much sense.  You may want to delete your responses also, to avoid confusion.
Answer: To see that the five intervals have the same distribution, add a fifth "ace of crowns" or similar to make a 53-card deck. Shuffle the deck and consider the cyclic order of the cards. In the cyclic order, there are five gaps between "aces". Since these gaps are preserved by permuting the aces, each gap is equally likely to be the gap immediately after the ace of crowns, each is equally likely to be the second gap after the ace of crowns, and so on. Thus for each $i$, the $i$th gap after the ace of crowns has the same distribution (so the same expected value; since their sum is $48$ each expected value is $9.6$).
Now create a linear order on the remaining 52 cards starting with the card immediately after the ace of crowns and continuing in the cyclic ordering. If the original ordering was uniformly random, so is this ordering, and it has $\Delta_i$ equal to the $i$th gap after the ace of crowns in the cyclic ordering.
We have $X_2=2+\Delta_1+\Delta_2$, so its expectation is $2+2\times 9.6$.
Comment: Excellent!  I thought if it was true there ought to be a simple way to see it, but I couldn't come up with anything.
Comment: I take it any direct approach is just not a good idea?


I can install new Wordpress plugins, but I can't update existing ones

Question: I recently migrated my Wordpress site to another server. Everything is working properly now, except I am unable to update Wordpress plugins that were installed before the migration. I am able to install new ones, which confuses me.
Steps I've taken so far:

Set permissions of wp-content and sub-directories to 755
Set permissions of files within wp-content and sub-directories to 644

I am not prompted for FTP information when installing new plugins.
Comment: "_I am unable to update Wordpress plugins that were installed before the migration_". What exactly happens when you try to update an existing plugin?
Comment: Sorry, completely forgot to include the error. It says this: "Update Failed: The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions."
Answer: Can you attempt to delete the "/wp-content/upgrade" directory and just create it as an empty folder?
Wordpress can be a bit tough when it comes to migration, and based on experience this has been the cause of the issue at times.
Comment: I tried this, now I'm receiving a different error: "Update Failed: Could not create directory."


Relation between uniform continuity of a function and boundedness of it's derivative.

Question: The following statement was provided to me by a senior to be used while solving problems of uniform continuity.
Statement : A function $f(x)$ is uniform continuous on only that interval for which $f'(x)$ is bounded.
Although I have studied analysis from Stephen Abott and Tom Apostol , this result is not given there.
Can you please give hints on how to prove it?
Thanks!!
Comment: That's wrong.$\,$
Answer: Theorem: If $f'$ is bounded then $f$ is uniformly continuous.
Proof Suppose $f'$ is bounded then there's $M>0$ s.t.
$$|f'(x)|\leq M\quad \forall x\in\mathbb{R}$$
Using mean value theorem we have,
$|f(x)-f(y)|\leq M|x-y|\quad \forall x,y\in\mathbb{R}$
Note that $f$ is a lipschitzian function on $\mathbb{R}$ $\Rightarrow$ $f$ uniformly continuous on $\mathbb{R}$.

Converse need not be true, Take $f:\Bbb{R}^+\to \Bbb{R^+}$ such that $f(x)=\sqrt{x}$. $f$ is uniformly continuous but $f'$ is not bounded in $\Bbb{R}^+.$


How to get index value from mapping function in JSX passing single parameter or zero parameter

Question: How to print index value in console.log()
  const option=5;
    [...Array(option)].map(() => (

    console.log()
    ))

Answer: #Array.map callback accepts an array index as the second argument.
let new_array = arr.map(function callback( currentValue[, index[, array]]) {
    // return element for new_array
}[, thisArg])

index Optional
The index of the current element being processed in the array.

[...Array(options)].map((_, index) => console.log(index));

Answer: [...Array(option)].map((item, index) => (
  console.log(index)
))

item is each item in your array
index is the each index value of your iteration.


How to compute the standard errors of a logistic regression's coefficients

Question: I am using Python's scikit-learn to train and test a logistic regression.
scikit-learn returns the regression's coefficients of the independent variables, but it does not provide the coefficients' standard errors. I need these standard errors to compute a Wald statistic for each coefficient and, in turn, compare these coefficients to each other.
I have found one description of how to compute standard errors for the coefficients of a logistic regression (here), but it is somewhat difficult to follow.
If you happen to know of a simple, succint explanation of how to compute these standard errors and/or can provide me with one, I'd really appreciate it! I don't mean specific code (though please feel free to post any code that might be helpful), but rather an algorithmic explanation of the steps involved.
Comment: Are you asking for Python code to get the standard errors, or for how the SEs are computed (mathematically / algorithmically) so that you can do it yourself? If the former, this Q would be off-topic for CV (see our [help center](http://stats.stackexchange.com/help/on-topic)), but may be on-topic on [Stack Overflow](http://stackoverflow.com/). If the latter, it would be on-topic here (but you may not get any code suggestions). Please edit your Q to clarify this. If it is the former, we can migrate it to SO for you (*please don't cross-post, though*).
Comment: Thanks, Gung. I purposefully posted here because I'm expecting the latter, but I'll edit to clarify. I mentioned I'm working in Python with scikit-learn in case someone who uses this software can give me tips specific to it.
Comment: Hi @GyanVeda, I am facing the same problem now, what's your final solution, please?
Comment: FYI: [How are the standard errors computed for the *fitted values* from a logistic regression?](http://stats.stackexchange.com/q/66946/12359)
Comment: Looks like you could use bootstrapping too; I think this is what statsmodels does by default. https://web.stanford.edu/class/archive/stats/stats200/stats200.1172/Lecture26.pdf
Answer: The standard errors of the model coefficients are the square roots of the diagonal entries of the covariance matrix. Consider the following:

Design matrix:

$\textbf{X = }\begin{bmatrix} 1 & x_{1,1} & \ldots & x_{1,p} \\ 1 & x_{2,1} & \ldots & x_{2,p} \\ \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n,1} & \ldots & x_{n,p}
 \end{bmatrix}$
, where $x_{i,j}$ is the value of the $j$th predictor for the $i$th observations.
(NOTE: This assumes a model with an intercept.)

$\textbf{V = } \begin{bmatrix} \hat{\pi}_{1}(1 - \hat{\pi}_{1}) & 0 & \ldots & 0 \\ 0 & \hat{\pi}_{2}(1 - \hat{\pi}_{2}) & \ldots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \ldots & \hat{\pi}_{n}(1 - \hat{\pi}_{n}) \end{bmatrix}$
, where $\hat{\pi}_{i}$ represents the predicted probability of class membership for observation $i$.

The covariance matrix can be written as:
$\textbf{(X}^{T}\textbf{V}\textbf{X)}^{-1}$
This can be implemented with the following code:
import numpy as np
from sklearn import linear_model

# Initiate logistic regression object
logit = linear_model.LogisticRegression()

# Fit model. Let X_train = matrix of predictors, y_train = matrix of variable.
# NOTE: Do not include a column for the intercept when fitting the model.
resLogit = logit.fit(X_train, y_train)

# Calculate matrix of predicted class probabilities.
# Check resLogit.classes_ to make sure that sklearn ordered your classes as expected
predProbs = resLogit.predict_proba(X_train)

# Design matrix -- add column of 1's at the beginning of your X_train matrix
X_design = np.hstack([np.ones((X_train.shape[0], 1)), X_train])

# Initiate matrix of 0's, fill diagonal with each predicted observation's variance
V = np.diagflat(np.product(predProbs, axis=1))

# Covariance matrix
# Note that the @-operater does matrix multiplication in Python 3.5+, so if you're running
# Python 3.5+, you can replace the covLogit-line below with the more readable:
# covLogit = np.linalg.inv(X_design.T @ V @ X_design)
covLogit = np.linalg.inv(np.dot(np.dot(X_design.T, V), X_design))
print("Covariance matrix: ", covLogit)

# Standard errors
print("Standard errors: ", np.sqrt(np.diag(covLogit)))

# Wald statistic (coefficient / s.e.) ^ 2
logitParams = np.insert(resLogit.coef_, 0, resLogit.intercept_)
print("Wald statistics: ", (logitParams / np.sqrt(np.diag(covLogit))) ** 2)

All that being said, statsmodels will probably be a better package to use if you want access to a LOT of "out-the-box" diagnostics.
Comment: To avoid memory issues and to account for singular matrix case, you could update your code as follows - `V = np.product(predProbs, axis=1);` `covLogit = np.linalg.pinv(np.dot(X_design.T * V), X_design)`
Comment: Why not use `np.sqrt(np.diagonal(np.cov(x,y)))` directly?
Answer: Does your software give you a parameter covariance (or variance-covariance) matrix?  If so, the standard errors are the square root of the diagonal of that matrix.  You probably want to consult a textbook (or google for university lecture notes) for how to get the $V_\beta$ matrix for linear and generalized linear models.
Comment: Here is one that I found after a few minutes of googling.  My advice is to first understand how the parameter variance is calculated in a basic linear model.  Once you get that, the extension to GLM's is easier.  All the same, knowing how to calculate it and knowing how to get it in a software package aren't the same thing.  www.sagepub.com/upm-data/21121_Chapter_15.pdf
Comment: I haven't been able to find anything online for the generalized linear model case (maybe I don't know the right search terms?). Help?
Answer: If you're interested in doing inference, then you'll probably want to have a look at statsmodels. Standard errors and common statistical tests are available. Here's a logistic regression example.
Comment: Yeah. It's just usually not the goal of machine learning-type toolboxes to provide tools for (frequentist) hypothesis tests. If you run into data-size constraints that don't work well in statsmodels but do work in scikit-learn, I'd be interested to hear about them on github.
Comment: Thanks for the recommendation! I'll look into statsmodels. Too bad that scikit-learn doesn't provide this sort of output.
Comment: @jseabold However, if you want to get some ad hoc notion of feature importance in logistic regression, you cannot just read off the effect sizes (the coefficients) without thinking about their standard errors. So even if you're not doing a frequentist test, and you just want some indication of effect sizes and robustness, the sklearn lack of variance output is challenging.
Answer: Building upon the fantastic work of @j_sack I have two impulses to build on his code:

Since predict_proba is giving you the probability for n-classes, the result is an nD-array and that is why one should(?) specify the class of interest, e.g.:

predProbs[:,0]
(check class of interest with resLogit.classes_)

The diagonal matrix should not include the raw-predicted-probability but $w_{ii}=\pi_i(1-\pi_i) $ e.g:
V = np.diagflat(np.product(predProbs[:,0] * (1-predProbs[:,0]), axis=1))


Why is the Poisson distribution chosen to model arrival processes in Queueing theory problems?

Question: When we consider Queueing theory scenarios where individuals arrive to a serving node and queue up, usually a Poisson process is used to model the arrival times. These scenarios come up in network routing problems. I'd appreciate an intuitive explanation as to why a Poisson process is best suited to model the arrivals.
Answer: The Poisson process involves a "memoryless" waiting time until the arrival of the next customer.  Suppose the average time from one customer to the next is $\theta$.  A memoryless continuous probability distribution until the next arrival is one in which the probability of waiting an additional minute, or second, or hour, etc., until the next arrival, does not depend on how long you've been waiting since the last one. That you've already waited five minutes since the last arrival does not make it more likely that a customer will arrive in the next minute, than it would be if you'd only waited 10 seconds since the last arrival.
This automatically implies that the waiting time $T$ until the next arrival satisfies $\Pr(T>t) = e^{-t/\theta}$, i.e., it's an exponential distribution.
And that in turn can be shown to imply that the number $X$ of customers arriving during any time interval of length $t$ satisfies $\Pr(X=x) = \dfrac{e^{-t/\theta} (t/\theta)^x}{x!}$, i.e. it has a Poisson distribution with expected value $t/\theta$.  Moreover, it implies that the numbers of customers arriving in non-overlapping time intervals are probabilistically independent.
So memorylessness of waiting times leads to the Poisson process.
Comment: Memorylessness says $\Pr(T>t+s\mid T>t) = \Pr(T>s)$.  That's the same as $\Pr(T>t+s\text{ and }T>t) = \Pr(T>s)$.  The event $[T>t+s\text{ and }T>t]$ is the same as the event $T>t+s$.  Hence the conditional probability is $\Pr(T>t+s)/\Pr(T>t)$.  Memorylessness says this is the same as $\Pr(T>s)$.  Hence we have $\Pr(T>t+s)=\Pr(T>t)\Pr(T>s)$.  A monotone function $g$ that satisfies $g(t+s)=g(t)g(s)$ is an exponential function.  And monotocity follows from the fact that $\Pr(T>t+s)$ must be less than $\Pr(T>t)$ because the former event implies, but is not implied by, the latter.
Comment: Whatever the theorems may say, it is an experimental fact that —in normal situations— arrivals are memoryless. You cannot *prove* that the number of costumers arriving in some period nothing, really.
Comment: The intention of the question was not to ask for a formal proof. Many a times, observations are made which lead to a theorem and then intuition is 'developed' to fit the observations and thus help cement the theorem in popular understanding. I was looking for something similar. Have edited my question to include the same.
Comment: thanks for the answer. I did not quite follow how the memory less arrivals leads to $\Pr(T>t) = e^{-t/\theta}$. Could you please elaborate or cite a reference that talks about this in detail. Thanks.
Comment: Shouldn't it be $\Pr(T>t) = 1/\theta*e^{-t/\theta}$?
Comment: @vonjd : No.  The _density_ function is $t\mapsto (1/\theta)e^{-t/\theta}$ for $t>0$.  That is the derivative of the cumulative distribution function $t\mapsto\Pr(T\le t)=1-e^{-t/\theta}$.  So we have $\Pr(T>t)=e^{-t/\theta}$. ${}\qquad{}$
Comment: @MarianoSuárez-Alvarez : It is not merely an empirical fact. Rather, the reason to expect it to be so comes from understanding of mathematics. Suppose a phone call comes in to a switchboard from Wisconsin, then the next one from Pennsylvania, then the next from Oregon, etc. The precise timings of these arrivals are independent of each other for evident reasons. That independence can readily be shown to entail the exponential distribution. That is done via mathematics, not via empirical data.
Answer: Pretty much any intro to queuing theory or stochastic processes book will cover this, e.g., Ross, Stochastic Processes, or the Kleinrock, Queuing Theory.
For an outline of a proof that memoryless arrivals lead to an exponential dist'n:
Let $G(x) = P(X > x) = 1 - F(x)$.  Now, if the distribution is memoryless,
$G(s+t) = G(s)G(t)  $
i.e., the probability that $x > s+t =$ the probability that it is greater than s, and that, now that it is greater than s, it's greater than $(s+t)$.  The memoryless property means that the second (conditional) probability is equal to the probability that a different r.v. with the same distribution $> t$.
To quote Ross:
"The only solutions of the above equation that satisfy any sort of reasonable conditions, (such as monotonicity, right or left continuity, or even measurability), are of the form:"
$G(x) = \exp(-ax)$  for some suitable value of $a$.
and we are at the Exponential distribution.
Comment: Robert Gallager's DRAFT OF  STOCHASTIC PROCESSES: THEORY FOR APPLICATIONS (http://www.rle.mit.edu/rgallager/notes.htm) is a good free alternative for an introduction to stochastic processes including a discussion of the Poisson process
Comment: Thanks for the edits, @Galen!  This is the second question I ever answered on this site, before I "learned" Mathjax.
Comment: Robert Gallager's RAFT OF  STOCHASTIC PROCESSES: THEORY FOR APPLICATIONS


Is $(\mathbb{C},+)$ isomorphic to a matrix Lie group?

Question: I'm wondering if $(\mathbb{C},+)$ is isomorphic to a matrix Lie group; I mean, if it is isomorphic to a closed subgroup of $GL(n,\mathbb{C})$. If it is, what is the isomorphism? And if it is not, why?
Answer: It's isomorphic to the subgroup of $\operatorname{GL}(2,\mathbb C)$ consisting of matrices of the form
$$\begin{pmatrix}1&0\\z&1\end{pmatrix}$$
via the isomorphism
$$\varphi:z\mapsto\begin{pmatrix}1&0\\z&1\end{pmatrix},$$
since $\varphi(0)=E_2$ and
$$\varphi(a)\varphi(b)=\begin{pmatrix}1&0\\a&1\end{pmatrix}\begin{pmatrix}1&0\\b&1\end{pmatrix}=\begin{pmatrix}1&0\\a+b&1\end{pmatrix}=\varphi(a+b).$$
Injectivity and surjectivity should be clear.
Answer: Yes, it is isomorphic to the subgroup of $GL(2,\Bbb C)$ which consists of the matrices of the form$$\begin{bmatrix}1&z\\0&1\end{bmatrix},$$because$$(\forall z_1,z_2\in\Bbb C):\begin{bmatrix}1&z_1\\0&1\end{bmatrix}.\begin{bmatrix}1&z_2\\0&1\end{bmatrix}=\begin{bmatrix}1&z_1+z_2\\0&1\end{bmatrix}.$$


Jazz mouthpiece for flugelhorn?

Question: What are some good options for a jazz flugelhorn mouthpiece? I have the Yamaha Bobby Shew and a nice 3C equivalent (for wind ensemble playing), but I want something that will help my range a bit.
Comment: Nay particular reason you're not satisfied with your current mouthpiece?
Comment: I love my current mouthpiece, I'm just looking for a mouthpiece that would fill a bit of a different role.
Answer: I love my Denis Wick 2F. It is very deep (small bore).
You may be aware that there are different bore size standards, the french standard being much smaller than the US, resulting in a more mellow sound.
I never play only the flugelhorn at a gig, so I appreciate the contrast between my trumpet and my flugelhorn. I don't want to swap instruments and have the same sound. If you are in the same situation, you might consider having contrasting instruments.
The backside is of course that such a deep mouthpiece won't help your range. But I'd argue that if you pick up your flugelhorn, it is not to play in the high register.

"The large bore horns, though still sounding like flugels, play more
  like trumpets, which may or may not be a good thing. You already have
  a trumpet. They have less resistance, take more air easily, and are
  capable of more volume, though I am not sure how important volume is
  in a flugelhorn. The smaller bore horns tend to play easily but can
  get stuffy when pushed and can be easily overblown. They do tend to
  force you to play the flugel differently and maybe more flugel-like.
  There is a reason that the most popular instruments are the small bore
  flugels in the French tradition."

http://www.dallasmusic.org/gearhead/Flugelhorn%20Guide.html

If you really want to have something for the high register, I can only give you standard advice: find something with a rim similar to what you have and like, but more shallow. I'd also recommend to go to a shop and try some out. Just because someone else likes a mouthpiece does not mean that you would.
Note also that a flugelhorn (the horn itself) might not be built for the register above high C, as a trumpet could. With that I mean that whatever the mouthpiece, the high register may be difficult if the horn just does not want. Hence once again, try the mouthpiece for yourself before you buy.
Comment: I am aware that you asked for something to help your range, sorry for my reply that goes against that.
Comment: I appreciate the recommendation, but I already have a great 'normal' mouthpiece. I really do need something that will give  me just a bit more range for the occasional piece that has stuff above C.
Comment: Can you not give me a reply that answers the question?
Comment: I'm sorry, I wouldn't know. I chose a deep mouthpiece at the cost of range, so I have no experience in good flugelhorn mouthpieces for high register.
Comment: I tried to expand my answer, I am not sure it helps much though.
Comment: Thanks. I've got a Yamaha Bobby Shew Custom, so my guess is it can go as high as any other flugel-he sure can play high.
Comment: I don't know, does he usually play high on the flugelhorn too? He sure can play high, but he sure can play soft as well...
Comment: True. thing is, I play my flugel for cornet parts in my HS Wind Ensemble and some of them can get a bit rangy, in addition to jazz pieces.
Comment: Oh. I use to say that two players on the same trumpet sound a lot more different than one player on a trumpet or a cornet. I'd rather play the cornet parts on the trumpet than on the flugel (maybe with a somewhat deeper mouthpiece), I think the sound is closer. Especially if you have a small bore flugel (which I believe the Bobby Shew is).
Comment: We have an eight person section, so the flugel really helps round out the sound from the other trumpets. That isn't the issue.

Jazz mouthpiece for flugelhorn?


How to hide action bar on HTC one using Holo.NoActionBar theme

Question: My app uses the android them Holo.NoActionBar theme to hide the action bar and display the activity .
This works fine on emulator and my Nexus 4 ,device actionbar is hidden.
however when it is run on HTC one , it ignores the android theme settings and displays the action bar. It might also be displayed on some other samsung or sony devices which have custom OEM skins. I have made sure there are no references to action bar or menu options in activity java file or android manifest file.
So i googled how to hide action bar and tried this How to hide action bar before activity is created, and then show it again?
and wrote the code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.activity_my);

and  http://developer.android.com/guide/topics/ui/actionbar.html
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_my);

However in both cases the IDE displays a warning saying the Method invocation'actionBar.hide()' may produce 'java.lang.NullPointerException'
"This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant and situations that can lead to nullability contract violations"
Although I am new to android development I know that NullPointerException should not be ignored.
But in this activity , it is important to hide action bar.
How do i modify this code so that it is not error prone??
UPDATE: and why is Holo.NoActionBar not working with HTC one . It is still displaying the action bar, while installing the same apk does not display action bar on nexus 4
Android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.xxxxxxxx.xxxxxxxxx" >

<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    .
    .
    lots of non-relevant activities
    .
    .

    <activity
        android:name=".Setting"
        android:label="@string/title_activity_setting"
        android:parentActivityName=".MyActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.xxxxxxxxx.xxxxxxxxxx.MyActivity" />
    </activity>
    <activity
        android:name=".t1"
        android:screenOrientation="portrait"
    >
    </activity>
    <activity
        android:name=".t2"
        android:screenOrientation="portrait"
    >
    </activity>
    <activity
        android:name=".t3"
        android:screenOrientation="portrait"
        >
    </activity>
    <activity
        android:name=".t4"
        android:screenOrientation="portrait"
         >
    </activity>
    <activity
        android:name=".t5"
        android:label="@string/title_activity_t5"
        android:screenOrientation="portrait"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.xxxxxxxxxx.xxxxxxxxxx.MyActivity" />
    </activity>
</application>

</manifest>

t1.java, without Actionbar.hide()
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class t1 extends Activity {

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

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rel1);
    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent newactivity= new Intent(getApplicationContext(), t2.class);
            startActivity(newactivity);
        }
    });
}

}

and t2,t3,t4,t5 have similar code
layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xxxxxxxxxx.xxxxxxxx.t1"
android:background="@drawable/finalp2"
android:id="@+id/rel1">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_marginTop="40dp"
    android:background="@drawable/bat"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="40dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewbottom"
    android:background="@drawable/bat"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="40dp"
    android:layout_marginBottom="42dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@android:drawable/ic_media_pause" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView4"
    android:layout_below="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_marginTop="112dp"
    android:background="@drawable/ball1" />

</RelativeLayout>

How i selected the theme : http://s16.postimg.org/6vuyjhwth/noactionbar.png
I have not created any other style.xml file, did i miss something?? If i did miss something how is it working fine on nexus 4 and why is it showing without action bar in IDE?
My app use SDK version 15 and higher
Comment: Yes, that's what you said. You should solve that problem first rather than trying to band-aid over it. Obviously it is working for thousands of other developers so we'd love to figure out what is wrong for your case.
Comment: You should probably first ask a question to figure out why your theme is not working as it certainly should (and does for many other developers).
Comment: @ianhanniballake: the theme is working on nexus devices and emulator, Problem I have faced is on HTC one probably due to some custom skins.
Comment: You'll want to include a bunch of actual code you've written before we can figure out what is wrong - particularly your AndroidManifest.xml, your styles, and what class your Activity extends.
Comment: Please post where `@style/AppTheme` is defined
Comment: in style.xml file  code states
Answer: Your activity t1 does not have any theme applied to it in the AndroidManifest.xml therefore it uses the default for your application - in your case, @style/AppTheme which includes an Action Bar. Set a theme in your Manifest:
<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Holo.Light.NoActionBar"
>
</activity>

Or make a new theme specifically for the no action bar case:

And use it:
<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar"
>
</activity>

Comment: Thanks a lot for your help. I found what I missed out when u asked for styles.xml in comments , I modified my code and it's working now
Comment: However just out of curiosity , if activity t1 does not have any theme applied to it in the AndroidManifest.xml therefore it uses the default @style/AppTheme which includes an Action Bar and I selected no action bar theme in layout_t1.xml design options but did not update manifest,    why is there a difference in behaviour of app on nexus 4 and HTC one
Answer: I was able to solve the problem (thanks to help provided by ianhanniballake, Thanks a lot).
Modified styles.xml
<resources>

</resources>

and Modified android manifest
<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="newtheme"
>
</activity>
<activity
    android:name=".t2"
    android:screenOrientation="portrait"
            android:theme="newtheme"

>
</activity>
<activity
    android:name=".t3"
    android:screenOrientation="portrait"
    android:theme="newtheme"

    >
</activity>
<activity
    android:name=".t4"
    android:screenOrientation="portrait"
    android:theme="newtheme"

     >
</activity>

Code is working perfectly now on HTC one and NEXUS  , Thanks a lot ianhanniballake
Comment: Make sure you are using `Holo.Light.NoActionBar` instead of `Holo.NoActionBar` otherwise other formatting (such as text coloring, dialogs, etc) will be different from your other activities unless that is specifically what you want


Como corrigir fonte serrilhada no chrome?

Question: Estou com um problema no Google Chrome. Não sei se é o CSS, mas as fontes declaradas no CSS estão ficando serrilhadas principalmente no Chrome. Já tentei as seguintes propriedades para diminuir o serrilhamento (anti-alising):
-webkit-font-smoothing;
font-smooth;
text-stroke;
text-shadow;

O site que está com este problema é este.
Comment: Esse problema tinha sido corrigido, vai entender o que se passa no navegador do Google. Uma alternativa é você usar fontes em `svg`.
Comment: Conseguir resolver com ClearType que pode ser visto aqui: http://www.ilista.com.br/blog/136-como-resolver-o-problema-de-fontes-serrilhadas-no-chrome
Comment: Aqui está normal e estou usando Firefox.
Comment: @Patrick, esqueci de acrescentar, o problema é no chrome =/ obrigado.
Comment: Poste um exemplo que não seja um link (use o [stacksnippet](http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) para isto). Links online costumam ser editados e assim não poderá ser visto o problema.
Answer: Este comportamento foi corrigido no Chrome 37. Para simular um resultado comparável ao antialias, você pode utilizar text-shadow, como no exemplo a seguir:

.teste
{
    color: black;
    font-size: 50px;
    text-align: center;
    line-height: 1;
}

.teste.antialias{
    -webkit-font-smoothing: antialiased;
    -webkit-text-shadow: rgba(0,0,0,.01) 0 0 1px;
    text-shadow: rgba(0,0,0,.01) 0 0 1px;
    }
Alias

(pre-Chrome 37)

Antialias

Fonte: How to apply font anti-alias effects in CSS?, Stack Overflow (em inglês)


Browsing a database as the user is typing

Question: This is probably a simple question, but I'm struggling with this.
Basically, how to browse in a database, as the user is typing something?
For example, in a website such as https://www.doesthedogdie.com/, how do they do it to show all the movies beggining with the string of characters you're typing, as you're typing it?
I guess javascript is involved?
Answer: It's called autocomplete
Please do some googling next time yourself.
Answer: You can use jQuery autocomplete  or Bootstrap typeahead to achieve this.
Hope this helps
Answer: My personal preference on how to make these kind of systems is fairly awkward, yet it seems to work just fine for me without requiring too much client-side scripting.I use ASP.NET's Page_Load event to save all content in a SQL database into an XML file. Then, when the user enters some text into the field, I make a hidden div class underneath it unhidden to display filtered results from the XML document. This way, you can update the SQL database at any time fairly simply to add new records which should be listed on the website.This is server-side scripting, which I prefer to client-side because the user can disable features and mess around with it to view your code which is something I personally hate.I hope this helps you somewhat.


What is the required lifetime of struct kevent passed into kevent changelist?

Question: The signature of the kevent system call for OSX and BSD is as follows:
     int
     kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist,
         int nevents, const struct timespec *timeout);

Suppose I have an invocation like the following:
  struct kevent kqueue_event;
  EV_SET(&kqueue_event, fd_to_add, EVFILT_READ, EV_ADD, 0, /*data=*/0, NULL);

  kevent(watcher_fd, &kqueue_event, 1, NULL, 0, NULL);

If the above invocation is inside a function, the kqueue_event structure will go out of scope as soon as the function returns.
Given that I just passed a pointer to this structure into the kevent function, is this going to cause problems for me down the road, or is the structure no longer needed after kevent returns?
The man page does not appear to say anything about how long the array needs to stick around.
Answer: It seems you can forget and don't care about the changelist passed after the successful kevent() call.
Take a look here https://github.com/apple/darwin-xnu/blob/main/bsd/kern/kern_event.c at kevent_internal function.
What they do there, they call kevent_modern_copyin (or legacy_copyin), both with copyin inside, so this structure is copied into kernel memory, and lives there now.


a small accident in.htaccess syntax >_<

Question: Internal Server Error. .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
Redirect URLs with query parameters (files placed in subdirectory)

I tried redirect:
http://www.example.com/index.php?id=1
to:
http://www.example.com/path-to-new-location/
Was found solution by myself:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?g=(.*)\ HTTP
RewriteRule ^ /%2/%4\? [R,L]

Comment: Remove last line that starts with `Redirect URLs...`
Answer: It should be like this
RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/ [L,R=301]
# Redirect URLs with query parameters (files placed in subdirectory)

Comment: Thanks. I was found a problem and edit question >_< can you  answer please?
Comment: You should NEVER change your question after you get an answer. SO is a reference for EVERY programmer, it's not just a convenient method for you to get help.

a small accident in.htaccess syntax >_<


Should I bootstrap at the cluster level or the individual level?

Question: I have a survival model with patients nested in hospitals that includes a random-effect for the hospitals. The random effect is gamma-distributed, and I am trying to report the 'relevance' of this term on a scale that is easily understood.
I have found the following references which use the Median Hazard Ratio (a bit like the Median Odds Ratio), and calculated this.
Bengtsson T, Dribe M: Historical Methods 43:15, 2010
However, now I wish to report the uncertainty associated with this estimate using the bootstrap. The data is survival data, and hence there are multiple observations per patient, and multiple patients per hospital. It seems obvious that I need to cluster the patient observations when re-sampling. But I don't know if I should cluster the hospitals too (i.e. resample hospitals, rather than patients?
I am wondering if the answer depends on the parameter of interest, and so would be different if the target was something that was relevant at the patient level rather than the hospital level?
I have listed the stata code below in case that helps.
cap program drop est_mhr
program define est_mhr, rclass
stcox patient_var1 patient_var2 ///
    , shared(hospital) ///
    noshow
local twoinvtheta2 = 2 / (e(theta)^2)
local mhr = exp(sqrt(2*e(theta))*invF(`twoinvtheta2',`twoinvtheta2',0.75))
return scalar mhr = `mhr'
end

bootstrap r(mhr), reps(50) cluster(hospital): est_mhr

Answer: The answer seems to be that the resampling process needs to take account of the structure of the data. There is a nice explanation here (along with some R code to implement this).
http://biostat.mc.vanderbilt.edu/wiki/Main/HowToBootstrapCorrelatedData
Thanks to the pointer from the UCLA Statistical Consulting Group.
I have written a speedier (but less flexible) version of the code snippet linked to above - check here for updates and details.
rsample2 <- function(data=tdt, id.unit=id.u, id.cluster=id.c) {
require(data.table)

setkeyv(tdt,id.cluster)
# Generate within cluster ID (needed for the sample command)
tdt[, "id.within" := .SD[,.I], by=id.cluster, with=FALSE]

# Random sample of sites
bdt <- data.table(sample(unique(tdt[[id.cluster]]), replace=TRUE))
setnames(bdt,"V1",id.cluster)
setkeyv(bdt,id.cluster)

# Use random sample of sites to select from original data
# then
# within each site sample with replacement using the within site ID
bdt <- tdt[bdt, .SD[sample(.SD$id.within, replace=TRUE)],by=.EACHI]

# return data sampled with replacement respecting clusters
bdt[, id.within := NULL] # drop id.within
return(bdt)
}

Answer: Imagine that you conducted a study about children educational achievements. You took a random sample of schools from some area and from each school one class was included in the study. You conducted analysis and now want to use bootstrap to obtain confidence intervals for your estimates. How to do it?
First, notice that your data is hierarchical, it has several levels: schools, classes within schools, and students within classes. Since there is only one class per school, so the second level is nonexistent in your data. We can assume that there are some similarities within schools and differences between schools. If there are similarities within schools then if you sampled pupils at random, not taking into consideration their school membership you could possibly destroy the hierarchical structure of your data.
In general, there are several options:

sample students with replacement,
sample whole schools with replacement,
first sample schools with replacement and then sample students (a) with replacement, or (b) without replacement.

It appears that the first approach is the worst one. Recall that bootstrap sampling should somehow imitate the sampling process in your study and you were sampling schools rather than individual students. Choosing between (2) and (3) is more complicated, but hopefully you can find research papers considering this topic (e.g. Rena et al. 2010, Field and Welsh, 2007). Generally options (2) or (3b) are preferable as it seems that including too much levels of sampling with replacement leads to biased results. You can find more information about this topic also in books by Efron and Tibshirani (1994) and Davison and Hinkley (1997). Notice that we have similar problem with bootstrapping time-series data and in this case we also rather sample whole blocks of series (e.g. whole season if we assume seasonality) rather than individual observations because otherwise the time structure would get destroyed. In practice there is no one-size-fits-all solution but with complicated data structures you should choose such bootstrap sampling scheme that best fits your data and your problem and if possible use a simulation study to compare different solutions.

Davison, A.C. and Hinkley, D.V. (1997). Bootstrap Methods and Their Application. Cambridge.
Efron, B. and Tibshirani, R.J. (1994). An Introduction to the Bootstrap. CRC Press.
Ren, S., Lai, H., Tong, W., Aminzadeh, M., Hou, X., & Lai, S. (2010). Nonparametric bootstrapping for hierarchical data. Journal of Applied Statistics, 37(9), 1487-1498.
Field, C. A., & Welsh, A. H. (2007). Bootstrapping clustered data. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 69(3), 369-390.
Comment: Accepted your answer (thanks), but for others I have now implemented a function in R to do this in my [answer](http://stats.stackexchange.com/a/186076/7746)


Particular solution to nonhomogeneous difference equation.

Question: I'm working on one of the problems in Edelstein-Keshet's book 'Mathematical models in Biology'. The problem in question asks me to show that for some constant $K$, $X_{n}=K$ is a solution to the equation $aX_{n+2}+bX_{n+1}+cX_{n}=d$, with $d\neq 0$. The excercise also states that for that to be true, 1 must not be a solution of the characteristic equation, and I just can't see why that would be a problem. Unless I'm misinterpreting the problem the only requirement should be that $a+b+c\neq 0$.
Answer: It took me way too long to realize this. $a+b+c=0$ is equivalent to $x=1$ being a solution to $ax^{2}+bx+c=0$.


PDE Initial-Boundary value problem question

Question: The problem: The ends of a stretched string are fixed at the origin and at the point,  $ x=\pi$ on the horizontal x-axis. The string is initially at rest along the x-axis, and then drops under it's own weight. The veritcal displacement $y(x,t)$ satisfies the PDE:
$Y_{tt} = v^{2}Y_{xx} - g$
$ \big( 0 < x <\pi, t>0 \big)$
where $g$ is acceleration due to gravity. Write the above as an initial boundary-value problem, and show that the solution is given by
$y(x,t) = \frac{4g}{\pi v^{2}} \big [ \sum_{n=1}^{\infty} \frac{\sin(2n-1)x}{(2n-1)^{3}} \cos(2n-1)vt - \frac{\pi}{8}x(\pi - x) \big]$
I honestly have no idea how to set this up because the wording of this problem is confusing me.
Much help is appreciated.
Thank you for all of your help.
Answer: From "the string is initially at rest", we know that $\partial_{t}y(x,0) = 0$. From "along the $x$-axis", we have $y(x,0) = 0$. So our initial-boundary-value problem is
\begin{align}
y_{tt}  &= \nu^{2}y_{xx} -g, \qquad
y(x,0) = 0, \qquad
\partial_{t}y(x,0) =  0, \qquad
y(0,t) = y(\pi, t) = 0
\end{align}
You can solve the inhomogeneous part of the equation by taking $y_{p}(x,t)$ to be a second order polynomial in $x$ and $t$, (which will differentiate down to a constant under the action of $\partial_{t}^{2} - \nu^{2}\partial_{x}^{2}$, i.e., take $y_{p}(x,t) = a_{20}t^{2}+a_{10}t + a_{11}tx + a_{01}x + a_{02}x^2 + a_{00}$. The conditions $y(0,t) = y(\pi,t) = 0$ imply that $y_{p}(x,t) = cx(x-\pi)$. Hitting this with $\partial_{t}^{2} - \nu^{2}\partial_{x}^{2}$ and setting the result $=-g$ shows $c = \frac{g}{2\nu^{2}}$, i.e.,
\begin{align}
y(x,t) = y_{h}(x,t) - \frac{g}{2\nu^{2}}x(\pi-x),
\end{align}
where $y_{h}$ solves
\begin{align}
\partial_{t}^{2}y_{h}  &= \nu^{2}\partial_{x}^{2}y_{h}, \qquad
y_{h}(x,0) = 0, \qquad
\partial_{t}y_{h}(x,0) =  0, \qquad
y_{h}(0,t) = y_{h}(\pi, t) = 0
\end{align}
By separation of variables and application of boundary conditions, we see that the eigenfunctions are of the form $\cos(\nu mt)\sin(mx)$, so
\begin{align}
y(x,t) = \sum_{m=1}^{\infty} a_{m}\cos(\nu mt)\sin(mx)  - \frac{g}{2\nu^{2}}x(\pi-x).
\end{align}
Since $y(x,0) = 0$, then
\begin{align}
 \sum_{m=1}^{\infty} a_{m}\sin(mx) =  \frac{g}{2\nu^{2}}x(\pi-x)
\end{align}
Multiplication by $\sin(\ell x)$ and integration over $[0,\pi]$ gives
\begin{align}
a_{\ell} &= \frac{g}{\pi\nu^{2}}\int_{0}^{\pi} x(\pi -x) \sin(\ell x), \mathrm{d}x \\
&=\frac{2g(1-(-1)^{\ell})}{\pi \nu^2 \ell^3}
\end{align}
so that $a_{2n} = 0$ and $a_{2n-1} = \frac{4g}{\pi\nu^{2}(2n-1)^{3}}$, giving
\begin{align}
y(x,t) = \sum_{n=1}^{\infty} \frac{4g}{\pi\nu^{2}(2n-1)^{3}}\cos((2n-1)\nu t)\sin((2n-1)x)  - \frac{g}{2\nu^{2}}x(\pi-x).
\end{align}
Answer: The vertical displacement $y(x,t)$ satisfies the PDE
$$y_{tt}=v^2y_{xx}-g$$ for $x \in (0,\pi)$ and $t>0$, together with the boundary conditions $$y(0,t)=y(\pi,t)=0$$ (since the ends are fixed), and the initial conditions $$y(x,0)=0, \quad y_t(x,0)=0 \qquad\text{for }x \in [0,\pi].$$ Notice that $y(x,0)=0$ since the string is initially at rest along the $x$-axis. Similarly, $y_t(x,0)=0$ since the string has no initial velocity.
Let $w(x,t)=y(x,t)+\frac{g}{2v^2}(\pi x-x^2)$ (the second term is the "steady-state" or "long-time" solution which appears after the Fourier series in the desired answer). Then $$v^2w_{xx}=v^2y_{xx}-g=y_{tt}=w_{tt},$$ and so $w$ satisfies the homogeneous wave equation $w_{tt}=v^2w_{xx}$ with homogeneous Dirichlet BCs (that is, $w(0,t)=w(\pi,t)=0$ for $t>0$), and initial conditions $$w(x,0)=\frac{g}{2v^2}(\pi x-x^2), \quad w_t(x,0)=0 \quad \text{for } x \in [0,\pi].$$ This is now a standard separation of variables problem.


Using for loop for reading a json dict and write into pandas dataframe

Question: I am trying to iterate through a JSON dict and write chosen objects into a pandas Dataframe and then at the and want to calculate the correlation between the two columns.
The Dataframe should look like this:
calc  val
20.1  20
20.2  20
19.8  20
...   ...
10.1  10
10.3  10
9.8   10
...   ...
5.2   5
5.1   5
5.0   5
...   ...

My JSON dict looks like this:
{
    "20um PSL": [
            {
                "imgsize": 20.886688245888603,
                "trigsize": 20.87416236786009,
                ...
            }
                "imgsize": 20.886688245888603,
                "trigsize": 20.87416236786009,
                ...
            {...}
            ...
    ]

    "10um PSL": [
        {
                "imgsize": 10.886688245888603,
                "trigsize": 10.87416236786009,
                  ....
        }
        {...}
            ...
    ]

    "5um PSL": [
        {
                "imgsize": 5.886688245888603,
                "trigsize": 5.87416236786009,
                  ....
        }
        {...}
        ...
    ]
}

this is my code so far:
    sizes = ['20um PSL', '10um PSL', '5um PSL']
    for file in json_data[sizes[0]]:
        particles_0 = pd.DataFrame({'calc': file['trigsize'], 'val': sizes_list[0]})
    for file in json_data[sizes[1]]:
        particles_1 = pd.DataFrame({'calc': file['trigsize'], 'val': sizes_list[1]})
    for file in json_data[sizes[2]]:
        particles_2 = pd.DataFrame({'calc': file['trigsize'], 'val': sizes_list[2]})
    df = particles_0.append([particles_1, particles_2])
    df.reset_index(drop=True, inplace=True)  # reorder index

My difficulty is now that somehow always only the last 'trigsize' value is loaded into the data frame. I am aware that this is somehow a fault with my loop and I need to change the iteration. As I am a Python beginner I seem not to find the logical solution to this problem.
In the end i need to calculate the correlation on how the numbers in Calc correlate with val (which is in this case always 20, 10, 5). Might there be a better solution for this? (instead of creating a dataframe?)
Dataframe so far:
calc  val
20.1  20
20.1  20
20.1  20
...   ...
10.1  10
10.1  10
10.1   10
...   ...
5.0   5
5.0   5
5.0   5
...   ...

Answer: As far as I see it, you are overwriting your particles_0, -_1, -_2 with every iteration of the loop. This also explains why only the last row remains.
You would have to probably first collect this data you are iterating through in a dict or something similar and then build up your data-frame once you are through everything.
Edit:
data = []
w = 0
sizes = ['20um PSL', '10um PSL', '5um PSL']
for size in sizes:
    for file in json_data[size]:
        data.append((file['trigsize'], y_verifcsize[w]))
        w += 1

df = pd.DataFrame(data, columns=["calc", "val"])

Comment: Thanks! it worked. i had to add a counter variable in sizes list so it append to the proper size: ```data = []
    sizes = ['20um PSL', '10um PSL', '5um PSL']
    w = 0
    for size in sizes:
        for file in json_data[size]:
            data.append((file['trigsize'], y_verifcsize[w]))
        w += 1
    df = pd.DataFrame(data, columns=["calc", "val"]) ``` (if you can add the counter i will mark it for you as answerd :) )
Comment: Hei Christian. Yes, I was aware that I am somehow overwriting my last data. But I seem to come to a point where my brain stopped being able to build logical loops. do you have a suggestion on how to do this?
Comment: I'm no pro myself yet but I would do it like this:

`data = []

sizes = ['20um PSL', '10um PSL', '5um PSL']

for size in sizes:

    for file in json_data[size]:

        data.append((file['trigsize'], sizes_list[0])

df = pd.DataFrame(data, index=["calc", "val"])`
Comment: I'm sorry - I have no idea how to tag this code so that it is properly readable. I added it as edit to my main answer.
Comment: That's true - I've overlooked the index of the other variable. Thank you, I added it :)