Add covariates to the random-intercept mixed effect model

Question: I'm working on a study where I want to explore whether two covariates, let's call them C and D, can help explain the differences we observe in our dependent variable, which we'll call B (depression scores). To do this, I'm using another predictor variable, A, which has five different groups (ADHD, ASD, Dyslexia, Dyscalculia, and comparison groups). Additionally, I'm incorporating a random effect variable, E (family variance), into the model.
For my first research question, I'm investigating the differences in B (depression scores) across the five groups within variable A (ADHD, ASD, Dyslexia, Dyscalculia, and comparison groups) while considering the influence of random effect variable E (family variance). This is what I'm referring to as Model 1:
Model1 <- lmer(B ~ A + (1 | E), REML = FALSE, data = data)

Now, for my second research question, I want to understand how much of the variation in B (depression scores) among the groups in variable A (ADHD, ASD, Dyslexia, Dyscalculia, and comparison groups) can be explained by covariates C (rates of school dropout) and D (scores of conduct problems). To achieve this, I've attempted to include these covariates in the model. However, I am not sure about whether this model is correct. Here's the structure of what I've tried, known as Model 2:
Model2 <- lmer(B ~ A + C + D + (1 | E), REML = FALSE, data = data)

Continuing from my first research question, if any substantial group differences are identified in the post-hoc analysis in model, I'm particularly interested in discovering whether covariates C and D can account for any of the observed group differences within variable A (which has five groups). Furthermore, I'm considering the possibility of running this model individually for each pairwise comparison among the groups within variable A. This way, I can assess how well the covariates can elucidate or attribute differences in the dependent variable B for each specific pairwise comparison.
I recognize that the simplest approach for including covariates in the model would be to utilize ANCOVAs, but these methods do not accommodate random effects. Therefore, I would highly value your insights on the correct way to integrate these covariates into the model to address my second research question. Your guidance is greatly appreciated. Thank you for your help.
Comment: By abstracting away the sitatuation, you may be omitting important details such as whether any of A, C or D vary within the E clusters. Also: (a) What does "However, I encountered issues with the results." refer to? (b) For the second research question, have you considered adding interactions between A and C & D?
Comment: Hello @dipetkov, thank you for your replies. To answer your questions (a), I meant that the outputs generated from Model 2 does not answer my research question 2. Also, I did not want to add interactions among all variables.

The variable B represents scores related to depression. Variable A encompasses five distinct groups: ADHD, ASD, dyslexia, dyscalculia, and a reference group for comparison purposes. The random effect E represents family-based variance. Additionally, covariates C and D pertain to two externalizing symptoms, namely conduct problems and school dropouts.
Comment: I agree with @dipetkov.  Context can be important. What are the variables? What issues did you encounter?
Comment: Thank you for the clarifications but it's best to edit the body of your question. Please include as much relevant information as you are comfortable sharing; it's still not clear to me what the variables are and how the data was collected. I mentioned interactions as one possible way to check whether / how the relationship between the outcome B and the predictors C and D varies across the A groups. Fitting a model on subsets of the data has the effect of interacting subset indicators with explanatory variables (in an efficient way).
Comment: Thinking more about this: Are the diagnoses (ADHD, ASD, dyslexia, dyscalculia) exclusive? That is, a person can have at most one of these diagnoses? I have no medical background but I think a person can have both ADHD and dyslexia for example. If that's the case, how are the A labels assigned?
Comment: Thank you @dipetkov for your answers. I've updated my variables information in the question above.
Answer: You can use a likelihood ratio test to investigate the statistical significance of factor A. In particular,
Model1 <- lmer(B ~ A + (1 | E), REML = FALSE, data = data)
Model1_null <- lmer(B ~ 1 + (1 | E), REML = FALSE, data = data)
anova(Model1_null, Model1)

Likewise, for the second model, you could use
Model2 <- lmer(B ~ A + C + D + (1 | E), REML = FALSE, data = data)
Model2_null <- lmer(B ~ C + D + (1 | E), REML = FALSE, data = data)
anova(Model2_null, Model2)

These would test the hypothesis of no difference in the average outcome B among the A factor levels in the second model after including/controlling for C and D. If you have a small sample, it would be best to use F-test with appropriate denominator degrees of freedom (e.g., using the lmerTest package).
As noted in the comments, if you want to assess if the effect of A on the outcome B is different for the different levels of C and D, then you would need to include the corresponding interaction terms.
Comment: Thank you, @Dimitris Rizopoulos, for your detailed response. To clarify, I have five groups (ADHD, ASD, dyslexia, dyscalculia, and comparison) under variable A. My goal is to examine the extent to which the variability in variable B (depression scores) can be attributed to covariate C (school dropout rates) and covariate D (conduct problems), all while considering the random effect (family variance). Given this complex setup, would it be advisable to conduct this analysis separately for each pairwise comparison, such as ADHD vs. ASD, or is there a more efficient approach you'd recommend?


Adições e Deleções por arquivo num commit

Question: Estou usando o comando git log --stat <commit> para obter as adições e deleções feitas em um commit.
Ex:
commit 1a1a
Author: Gabriel Hardoim
Date: 2018-08-20 20:30:40

arquivo.java    | 3 +++
arquivo.css     | 3 ---

2 files changed, 3 insertions(+), 3 deletions(-)

Até aqui tudo bem, mas em casos onde no mesmo arquivo linhas foram adicionadas e removidas ele exibe um número de alterações por arquivo maior que o número de alterações total.
Ex:
commit 1a1a
Author: Gabriel Hardoim
Date: 2018-08-20 20:30:40

arquivo.java    | 5 +----
arquivo.css     | 5 ++++-

2 files changed, 5 insertions(+), 5 deletions(-)

Em casos assim:

Como faço para descobrir quantas adições/deleções foram feitas em cada arquivo?
Existe algum comando ou flag que possa me ajudar nesse sentido?

Ps: Quando o número de alterações é muito grande, a quantidade de + e - ao lado do arquivo não deixa tão claro como nos casos de pequenas alterações.
Answer: Vamos entender porque parece que o git "exibe um número de alterações por arquivo maior que o número de alterações total".
Vamos supor que eu tenho um arquivo com 3 linhas:
primeira linha do arquivo
segunda linha do arquivo
terceira linha do arquivo

Vamos supor também que este é o conteúdo que está no último commit (no meu caso, só tem 1 commit):
$  git log --oneline
792eda8 primeiro commit

Em seguida eu edito o arquivo, mudando a segunda linha e adicionando uma quarta linha:
primeira linha do arquivo
mudando a segunda linha do arquivo, blablabla etc
terceira linha do arquivo
adicionar quarta linha

E faço o commit do mesmo:
$ git add arq.txt
warning: LF will be replaced by CRLF in arq.txt.

$ git commit -m "mudar arquivo"
[master d0be287] mudar arquivo
1 file changed, 2 insertions(+), 1 deletion(-)

$ git log --oneline
d0be287 mudar arquivo
792eda8 primeiro commit

Se usarmos git log para ver as alterações, teremos 2 inserções e 1 deleção:
$ git log --stat d0be287 -1
commit d0be287ddf28aa910d8fa9d002a609aa8056e357
Author: Fulano de Tal <firstname.lastname@example.com>
Date:   2018-09-12 09:07:34

    mudar arquivo

 arq.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

E se usarmos git diff, podemos ver com mais detalhes as alterações:
$ git diff 792eda8 d0be287
diff --git a/arq.txt b/arq.txt
index 7e12196..757a5bd 100644
--- a/arq.txt
+++ b/arq.txt
@@ -1,3 +1,4 @@
 primeira linha do arquivo
-segunda linha do arquivo
+mudando a segunda linha do arquivo, blablabla etc
 terceira linha do arquivo
+adicionar quarta linha

No caso, estou vendo a diferença entre os commits 792eda8 e d0be287.
Agora repare como é mostrada a alteração na segunda linha. Ele mostra que o trecho segunda linha do arquivo foi "removido" (está com - na frente), dando lugar a mudando a segunda linha do arquivo, blablabla etc (que foi "adicionada", pois está com + na frente).
Por isso, embora a alteração tenha sido "editar uma linha", o git considera que foi feita uma inserção e uma deleção.
Já a quarta linha aparece como adicionada (com + na frente). Por isso o resultado são 2 inserções e 1 deleção. Pode-se ter um resumo disso com as opções --stat, --shortstat e --numstat, que mostram a mesma informação em formatos diferentes:
$ git diff 792eda8 d0be287 --stat
 arq.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git diff 792eda8 d0be287 --shortstat
 1 file changed, 2 insertions(+), 1 deletion(-)

$ git diff 792eda8 d0be287 --numstat
2       1       arq.txt

--numstat é a opção mais "compacta", mostrando apenas os números e o nome do arquivo (no caso acima, são 2 inserções e 1 deleção), o que é um bom formato para ser lido por scripts, por exemplo.

Mas se você não quer que uma alteração na mesma linha seja contada duas vezes (como uma inserção e uma deleção) e gostaria de saber apenas se uma linha foi modificada, outra opção é usar --word-diff, que mostra as diferenças levando em conta as palavras (sendo que "palavras" são delimitadas por espaços):
$ git diff 792eda8 d0be287 --word-diff
diff --git a/arq.txt b/arq.txt
index 7e12196..757a5bd 100644
--- a/arq.txt
+++ b/arq.txt
@@ -1,3 +1,4 @@
primeira linha do arquivo
{+mudando a+} segunda linha do [-arquivo-]{+arquivo, blablabla etc+}
terceira linha do arquivo
{+adicionar quarta linha+}

Repare que agora as alterações da segunda linha são mostradas todas em uma única linha, mostrando as palavras que foram adicionadas e removidas.
Você pode ainda usar --word-diff-regex para definir uma expressão regular que define o que é uma palavra. No seu caso, você poderia usar ^.*$, que significa "zero ou mais caracteres (.*) do início (^) ao fim ($) da linha" - ou seja, toda a linha é considerada uma única palavra. O resultado é:
$ git diff 792eda8 d0be287 --word-diff-regex="^.*$"
diff --git a/arq.txt b/arq.txt
index 7e12196..757a5bd 100644
--- a/arq.txt
+++ b/arq.txt
@@ -1,3 +1,4 @@
primeira linha do arquivo
[-segunda linha do arquivo-]{+mudando a segunda linha do arquivo, blablabla etc+}
terceira linha do arquivo
{+adicionar quarta linha+}

A segunda linha continua aparecendo como se tivesse uma deleção e uma inserção, mas pelo menos a informação está toda em uma linha só. Com isso, é possível usar egrep (caso você esteja usando o bash) para contar somente estas linhas.
$ git diff 792eda8 d0be287 --word-diff-regex="^.*$" | egrep -e "^(\[-|\{\+)" -c
2

No caso, estou considerando somente as linhas que começam com [- ou {+. Como eu considero que toda a linha é uma única palavra, isso garante que qualquer linha modificada começará com algum destes caracteres. Em seguida, uso a opção -c para retornar a quantidade de linhas (no caso, 2). Se você não usar a opção -c, o retorno são as próprias linhas modificadas:
$ git diff 792eda8 d0be287 --word-diff-regex="^.*$" | egrep -e "^(\[-|\{\+)"
[-segunda linha do arquivo-]{+mudando a segunda linha do arquivo, blablabla etc+}
{+adicionar quarta linha+}

Também é possível mudar a regex do egrep para trazer as modificações separadamente:

egrep -e "^\[-.*-\]\{\+" - linhas que foram modificadas (começam com [- e possuem também {+)
egrep -e "^\[-.*-\]$" - linhas que foram removidas (começam com [- e terminam com -])
egrep -e "^\{\+.*\+\}" - linhas que foram adicionadas (começam com {+ e terminam com +})

Lembrando que podem haver falsos positivos (caso alguns dos delimitadores [-, {+, etc façam parte da própria linha).

Você também poderia usar git diff d0be287~ - e nesse caso d0be287~ significa "o commit anterior a d0be287" (para mais detalhes sobre esta sintaxe, veja aqui e aqui). No caso, ele verifica as diferenças entre o commit anterior a d0be287 e o seu HEAD (ou seja, o branch que você está atualmente).


How to create a simple form and pass one of its fields to a script in another page

Question: I need to work with a plugin called AffiniBox, which I'm going to integrate it in a mobile app (displayed as a webpage). To authenticate a users login, I'll have to create a form with a couple of fields, one of them being e-mail. Essentially, I'll have to pass the content of the e-mail field as an ID to a script in another page.
The script in the other page will then fetch the e-mail and load itself.
I'm thinking that simple form fields such as "form action" and "input type" could be used, but I wouldn't know how to send this data to another page.
This is the code they've sent to me to work with. "data-email={{EMAIL}}" is the field id which should be filled with the form in the previous page.

According to them, if no e-mail is inserted or if it's an invalid entry, the page should reload to the previous one.
Comment: The hyperlink 'AffiniBox' in your first sentence does not work.
Answer: did u mean to redirect to another page with email ?
document.location.href = 'http://email@example.com';

or u want to send a form ?

1st use form to send data to another page :
here is important for u to name a input as it will be a name u will use on another page
<form action="http://example.com/" method="POST">
<input type="email" name="data-email" value="firstname.lastname@example.com">
</form>

on another_page.php :
u receive a POSTed data with :
$email = $_POST['data-email'];

and u can use it with javascript like this :
let email = <?php echo $email; ?> ;

so u have let email with value u sent with POST
Comment: I want to send a form. The first page should have a simple form with an e-mail field. The user will type his e-mail and, upon submission, will be redirected to the second page, which will run the script. The `data-email={{EMAIL}}` id should be defined by the e-mail form typed in the previous page.
Comment: ok , so if u will use form like above , after sending form u will send a data-email=youremail , and you will be redirecting with these data to page you set as ACTION in form  /// if you need an email in URL use GET method instead of POST
Comment: In this case, how should I edit my script code? `data-email={{EMAIL}}` is an id, how should I format this id code to receive the form from the previous page? Should it be the same name as set in the form? I've tried that but it doesn't work.
Comment: yes u should use data-email name like in previouse page //  but u cannot do that with javascript (as it is client-side language) , u need to use PHP or other server-side language to receive POSTed data , with PHP u receive this like this $email = $_POST['data-email'];   and u can use $email variable /// if u use PHP u can receive these data and put them to js variable like this : var my_javascript_variable = ;
Comment: The script itself is written in javascript. How would I pass the POSTed data received with PHP to the javascript `data-email` id?
Comment: i edited answer to explain steps u want to make to POST and receive email


$r'=f(p)/r^4, p'=g(p)/r^4$ What is the method to analytically solve this system of non-linear differential equations?

Question: I'm working on a physical system in polar coordinates and I'm a bit lost on how to start solving a problem like this. The system has two dependent variables: the distance between two particles (r) and the angle between two particles ($\phi$). These evolve with time and I'm looking for an expression that explicitly shows r and $\phi$ only as functions of time and the initial conditions.
The system is described by two non-linear differential equations:
$r' = {dr \over dt} = f(\phi)/r^4$
$\phi' = {d\phi \over dt} = g(\phi)/r^4$
Where $f(\phi)$ and $g(\phi)$ are integratable/differentiable trigonometric functions dependent only on $\phi$.
What is the best way to go about solving this system of equations?
Comment: If your $f$ and $g$ are only dependent on $\phi$ then the ODEs are separable.You can look at the level curves $\frac{d \phi}{dr} = \frac{g}{f}$.
Answer: Off the top of my head, here's what I would try:
from the equations
$r' = \dfrac{dr}{dt} = \dfrac {f(\phi)}{r^4} \tag{1}$
and
$\phi' = \dfrac{d\phi}{dt} = \dfrac{g(\phi)}{r^4}, \tag{2}$
we obtain upon division of (1) by (2)
$\dfrac{dr}{d\phi} = \dfrac{\dfrac{f(\phi)}{r^4}}{\dfrac{g(\phi)}{r^4}} = \dfrac{f(\phi)}{g(\phi)}, \tag{3}$
which in fact may be integrated to give $r$ as a function of $\phi$:
$r - r_0 = \displaystyle  \int_{r_0}^r ds = \displaystyle \int_{\phi_0}^{\phi}\dfrac{f(w)}{g(w)}dw = \Phi(\phi), \tag{4}$
whence
$r(\phi) = \Phi(\phi) + r_0. \tag{5}$
We may then substitute (5) into (2):
$\dfrac{d\phi}{dt} = \dfrac{g(\phi)}{r^4(\phi)} = \dfrac{g(\phi)}{(\Phi(\phi) + r_0)^4}, \tag{6}$
which, though nasty in appearance, can in principle be integrated to give $t$ as a function of $\phi$:
$t - t_0 = \displaystyle \int_{\phi_0}^\phi \dfrac{(\Phi(w) + r_0)^4}{g(w)}dw =\Psi(\phi); \tag{7}$
in principle, as long as $dt/d\phi \ne 0$, we may invert (7) to obtain $\phi$ as a function of $t$, and insert such $\phi(t)$ in (1):
$\dfrac{dr}{dt} = \dfrac{f(\phi(t))}{r^4}, \tag{8}$
which we can then solve for $r$ in terms or $t$ as well; indeed, we have:
$r^4 \dfrac{dr}{dt} = f(\phi(t)), \tag{9}$
whence
$\dfrac{1}{5}(r^5 - r_0^5) = \displaystyle \int_{t_0}^t f(\phi(s))ds, \tag{10}$
$r^5(t) = 5 \displaystyle \int_{t_0}^t f(\phi(s))ds + r_0^5,  \tag{11}$
leading to the somewhat arcane expression
$r(t) = \sqrt[5]{5\displaystyle \int_{t_0}^t f(\phi(s))ds + r_0^5} = (5\displaystyle \int_{t_0}^t f(\phi(s))ds + r_0^5)^{1/5}. \tag{12}$
In principle this works, but is probably going to be impossible in closed form for most $f(\phi)$, $g(\phi)$.  My advice:  use a computer.


Problemas ao definir tamanhos nos itens flexbox

Question: Sou leiga em flexbox, e to com dificuldade em alinhar os itens dentro do flex, determinei tamanho pra eles, mas na hora de posicionar, eles não ficam iguais, sempre ficam diferentes e quero um padrão pra todos. Se alguém conseguir ver no meu código onde estou errando, agradeço.
Está assim o layout

Precisa ficar parecido com esse

Meu codigo abaixo

Objetivo: <?=$linha['objetivo'];?>

Gatilho/Situação: <?=$linha['situacao_gatilho'];?>

Canais: <?=$linha['canal_proposto'];?>

Fase: <?=$linha['fase'];?>

Código: <?=$linha['codigo'];?>

Situação: <?=$linha['data'];?>

                               <button class="but"> Detalhes </button>

css
* {
    margin: 0;
    padding: 0;
}
header h2 {
    text-align: center;
    margin-top: 10px;
    font-family: 'Times New Roman', Times, serif;
    font-size: 25px;
}
.container{
   flex-wrap: wrap;
   border: 1px solid black;
}
#overflow_ellipsis{
    width:70%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.dltop{
    width:80%;
    font-size: 15px;
    font-weight: bold;
    background-color: orange;
}
.dlbottom{
    width:30%;
}
.dcbottom{
    justify-content: center;
    width: 60%;
}
.flex1{
    flex-direction: row;
}
.gat{
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.but{
    font-size: 14px;
    float: right;
    margin-bottom: 30px;
}
.sit{
    font-size: 14px;
    float: right;
    background-color: tomato;
    width: 20%;
}
.gat{
    font-size: 14px;
    background-color: #34ce57;
}
.fase{
    background-color: #f1b0b7;
}
.canalproposto{
    background-color: chartreuse;
}
.cod{
    background-color: lightcoral;
}

Comment: Precisa ser o css e html, nessa estrutura ?
Answer: Refiz a estrutura html e css, continuei usando flexbox como informado.
Resultado: https://i.stack.imgur.com/1V0Xl.jpg
HTML

Negativa de compra (motivos e providências)

                Situação

VIRGENTE

                1,02%

Eficiência

                MOTOR

CAMPANHA

                Objetivo:
                Informar o motivo da negativa de compra e outras opções para finalizar a transação

                Gatilho/Situação:
                Compra negada por limite de crédito (a vista ou parcelada)

                Canais:

NM

SMS

Fase: fidelização

C05

                <button class="detalhes">DETALHES ></button>

CSS
*{
  padding: 0px;
  margin: 0px
}
body{
  font-family: 'Source Sans Pro',sans-serif;
}
.container{
    margin: 5px;
    padding: 10px;
    background-color: #fff;
    border: 1px solid orange
}
.titulo-situacao{
    display: flex;
    width: 100%
}
.titulo-situacao .titulo{
    width: 70%;
    display: flex;
    align-items: center;
}
.titulo-situacao .titulo h4{
    font-size: 30px;
    padding-left: 5px;
    font-weight: bold;
    color: #126698
}
.titulo-situacao .titulo img{
    width: 77px
}
.titulo-situacao .situacao{
    width: 30%
}
.titulo-situacao .situacao div:first-child{
    width: 100%
}
.titulo-situacao .situacao .s1,
.titulo-situacao .situacao .s2,
.titulo-situacao .situacao .s3{
    display: flex
}
.titulo-situacao .situacao .s1 label{
    width: 50%;
    text-align: center;
    font-weight: unset;
}
.titulo-situacao .situacao .s1 div{
    background-color: #3c763d;
    color: #fff;
    width: 50%;
    text-align: center
}
.titulo-situacao .situacao .s2 label{
    width: 50%;
    text-align: center;
    color: #3c8dbc
}
.titulo-situacao .situacao .s2 div{
    width: 50%;
    text-align: left
}
.titulo-situacao .situacao .s3 label{
    width: 50%;
    text-align: center;
    font-weight: unset;
}
.titulo-situacao .situacao .s3 div{
    width: 50%;
    text-align: center;
    color: #ddd
}

.body-modulo{
    width: 100%;
    display: flex
}
.objetivo-gatilho{
    width: 50%
}
.objetivo-gatilho .objetivo{
    width: 100%
}
.objetivo-gatilho .objetivo label,
.objetivo-gatilho .gatilho label{
    color: #3c8dbc
}
.canais-fase-codigo{
    width: 50%
}
.canais-fase-codigo .canais{
    width: 100%;
    display: flex;
    align-items: center
}
.canais-fase-codigo .canais label{
    color: #3c8dbc
}
.canais-fase-codigo .canais div{
    background-color: #126698;
    color: #e8943d;
    margin-left: 5px;
    padding: 5px 15px;
    border-radius: 3px
}
.canais-fase-codigo .fase-codigo{
    width: 100%;
    display: flex;
    margin-top: 10px
}
.canais-fase-codigo .fase-codigo .fase{
    width: 60%;
    background-color: #80b6d6;
    color: #fff;
    margin: 0px 5px;
    padding: 3px;
    font-size: 17px
}
.canais-fase-codigo .fase-codigo .codigo{
    width: 15%;
    background-color: #80b6d6;
    color: #fff;
    margin: 0px 5px;
    padding: 3px;
    font-size: 17px
}
.canais-fase-codigo .fase-codigo .detalhes{
    width: 25%;
    background-color: #126698;
    color: #fff;
    border: 0px
}


Check private class methods call order

Question: Hello!

Tl;dr: I need to check that public class method calls private class methods in specific order depending on some condition. How can I do this?

Considering having such class:
class SomeClass(object):

    def __init__(self, param: ParamsEnum):
        self._param = param

    def process(self):
        if self._param == ParamsEnum.VALUE_1:
            self._do_intermediate_process()
        self._do_process()

    def _do_process(self):
        raise NotImplementedError

    def _do_intermediate_process(self):
        pass

I want to write a unit test to check that process logic. I tried few approaches but none of them was succeeded.
Approach one - using separate Mock() as container.
@mock.patch.object(SomeClass, '_do_process')
@mock.patch.object(SomeClass, '_do_intermediate_process')
def test_process(self, mocked_do_process, mocked_do_intermediate_process):
    mock_container = Mock()
    mock_container.m0, mock_container.m1 = mocked_do_process, mocked_do_intermediate_process

    instance = SomeClass(ParamsEnum.VALUE_1)
    instance.process()

    mocked_do_process.assert_called_once()
    mocked_do_intermediate_process.assert_called_once()
    mock_container.assert_has_calls([mock_container.m0, mock_container.m1])

As a result, mock_container.method_calls shows nothing.
Approach two - mocking 'process' with keeping original logic as side effect. This test is passing regardless of mocked method order on the last line.
@mock.patch.object(SomeClass, '_do_process')
@mock.patch.object(SomeClass, '_do_intermediate_process')
def test_process(self, mocked_do_process, mocked_do_intermediate_process):
    origignal_process = SomeClass.process
    with mock.patch.object(SomeClass, 'process', autospec=True) as mocked_process:
        def side_effect(self):
            return origignal_process(self)
        mocked_process.side_effect = side_effect

        class_mock = Mock(spec=SomeClass, autospec=True)
        class_mock.process = mocked_process
        class_mock._do_process = mocked_do_process
        class_mock._do_intermediate_process = mocked_do_intermediate_process
        class_mock._param = ParamsEnum.VALUE_1

        class_mock.process(class_mock)

        mocked_do_process.assert_called_once()
        mocked_do_intermediate_process.assert_called_once()
        mocked_process.assert_called_once()
        class_mock.assert_has_calls([mocked_do_intermediate_process, mocked_do_process])

Answer: One mistake first:
@mock.patch.object(SomeClass, '_do_process')
@mock.patch.object(SomeClass, '_do_intermediate_process')
def test_process(self, mocked_do_process, mocked_do_intermediate_process):

should really be:
@mock.patch.object(SomeClass, '_do_process')
@mock.patch.object(SomeClass, '_do_intermediate_process')
def test_process(self, mocked_do_intermediate_process, mocked_do_process):

The order does matter a lot.
https://thadeusb.com/weblog/2010/08/23/python_multiple_decorators/

When you stack multiple decorators onto a python function, there is an order that these decorators will get executed in, which is not mentioned in the documentation.

The decorators are executed from innermost to outermost. For example.
Select
@i_get_called_last
@i_get_called_second
@i_get_called_first
def my_decorated_function():
    return 1+2

So when designing your decorators, make sure that the first and second called decorators return compatible functions that the decorators preceding it would be able to operate with.

using separate Mock() as container.

It simply will not work by creating a Mock object, it has to be called with mock_container.method() to create and call the method.
mock_container = mock.Mock()
>>>mock_container.called
False
>>> mock_container()
<Mock name='mock()' id='140078609813744'>
>>> mock_container.called
True
>>> mock_container.ma()
<Mock name='mock.ma()' id='140078608707880'>
>>> mock_container.ma.called
True

Check private class methods call order


Optimal weighting matrix instrumental variables estimator

Question: The formula for the optimal weighting matrix when you perform regression with more instrumental variables than endogenous predictors is the following:
$W_{opt} = (\frac{1}{N}Z'Z)^{-1} $
This tells us that we only have to look at the variance covariance matrix of the instruments,
But doesn't it make more sense to give more weight to the strongest instruments (or in other words, those who correlate better with the endogenous predictors)?
Thanks in advance!
Answer: Under the assumption of homoskedasticity, $\Omega = \sigma^2 I$, the covariance matrix of the moment conditions becomes
$$S = \frac{1}{n} E(Z'\Omega Z) = \sigma^2 \frac{1}{n} E(Z'Z)$$
and you are right in that we could simply ignore $\sigma^2$ in the weighting matrix and set $W = \left( \frac{1}{n}Z'Z \right)^{-1}$ because the GMM estimator does not change for weighting matrices that differ only by a multiplicative constant. In fact, if the sample is large enough the GMM estimator is consistent for any(!) weighting matrix given that it is positive definite. Hence you can choose arbitrary weighting matrices in theory in order to obtain consistent point estimates. However, this concerns only $\widehat{\beta}$. It also only holds for large enough samples as in small samples the weighting matrix can change the point estimates.
So how can we justify our choice of $W$ in practice?
We choose the weight such that it minimizes the asymptotic variance of the estimator. For this you need $\sigma^2$ which you can obtain as the residuals of the IV estimator $\widehat{\sigma}^2 = \frac{1}{n}\widehat{u}'\widehat{u}$. The variance of our estimator in this case is minimized by setting:
$$\widehat{W} = \widehat{S}^{-1} = \left( \widehat{\sigma}^2 \frac{1}{n}Z'Z \right)^{-1}$$
So in this case the low-variance moments (i.e. instruments that are less correlated with the endogenous variable) receive a smaller weight than the high-variance moments. This way we construct a more efficient estimator. Here comes your question into play: the relative strength of the instruments matters for the variance but not for the point estimates.
Comment: Hey Andy, thanks for your answer. Unfortunately, after some heavy thinking, I still don't get it. Is it possible to say in three words how the weight of an instrument that is not correlated at all with the endogenous predictor gets a weight close to zero?
Comment: Hey Andy I am still doubting over everything, but the pieces are coming together hopefully the next days. Keep you posted!
Comment: I should have been more precise. In small samples the choice of the weighting matrix matters because it influences the point estimates. In large samples the GMM estimator remains consistent for any choice of $W$. In this case you choose the weight such that it minimizes the asymptotic variance. These notes explain it very nicely (https://www.google.co.uk/url?sa=t&source=web&rct=j&ei=5C0fU4b4CJD07AaZl4C4CA&url=http://www.soderbom.net/lec2n_final.pdf&cd=1&ved=0CCwQFjAA&usg=AFQjCNENt4_YWemP_bsFY8AS1qGNVA8xVg) p. 21 onwards, I just found them and I'll update my answer this evening to clarify it.
Comment: I shortened the answer and (hopefully) got rid of unnecessary technicalities. The points about why the choice of the weights doesn't matter for point estimation in large samples and the motivation for giving more weight to stronger instruments in the variance estimation should be clearer now. Let me know if something requires further explanation and if be happy to edit the post again.
Comment: I guess you're still not convinced?
Comment: What is confusing me is the optimal weighting matrix if you have one instrument for each endogenous variable and heteroscedasticity. Is the following correct: each weighting matrix will give you the same estimates, as the system is identified. But you have to correct the variance due to heteroscedasticity (with a white correction for example). In case you have an over identified system, you should not only correct the variance, but also calculate a new weighting matrix and calculate the estimates again. The new estimates should also have a smaller variance. Thx for your help!
Comment: I think I get it now, thanks a lot for your time and answers!!
Comment: Hey Andy, only now I truly get it. I did not get that you start with the covariance of the sample moments, I I read over it. Now I do. You start with the variance of the sample moments and in case of homoscedasticity you end up with the covariance matrix of the instruments. Anyway thanks again!
Comment: That's right Kasper! Sorry it didn't come out clearly enough in the answer but I'm happy that it was eventually useful


Mock a repository while testing an api controller

Question: Im trying to get familiar with using MOQ and mocking in general.  So I want to test an api controller which uses an assembly which serves as a repository for getting/updating data etc.
eg a structure like this.
<code>HomeController
    Index
        Repository.GetSomeData  (returns JSON object)
</code>
This repository class has an interface, and that is what is injected via the .net core startup class. The method in this case, GetSomeData does a number of steps via calls to the Db, as well as reading a file from the file system, parsing it and moving it off to another folder.
Question:  How can a "mocked" Repository work without doing the things that the "real" object does?  All the examples I see are simple addition, returning strings etc.
Comment: Could you share source and interfaces of controller and repository?
Answer: When you mock something like your repository, you going to stub out methods on the repository to return some canned result. Calls to those methods on the repository mock, then, bypass the real methods and instead just do what you've stubbed.
Essentially, you need to first identity what methods will be utilized. Then, you should determined appropriate responses those methods should return based on the particular scenario you're attempting to unit test. Then, you create the mock and add stubs for those methods with those responses.
The whole point of mocking is to remove variables, so you're intentionally trying to get to the "happy path": the set of internal responses that put the action in the state you need it to be in for specific test you're conducting.


how to configure postfix to send local mail to another local server?

Question: I have a pair of mail servers that are comprised mainly of Postfix, Procmail and Cyrus-IMAP and they both work fine. The IMAP servers are configured in a simple master/replica pair. All 'normal' mail is accepted by the master's Postfix which processes it through Procmail for delivery to the Cyrus-IMAP running on the same server. Both servers are capable of running like this but only the master actually receives external email.
At present, local accounts on either box send mail (for example, generated by cron jobs) through its local Postfix which is delivered locally to the Cyrus-IMAP on the same box.
However, Cyrus's replication doesn't like changes happening directly on the replica so I would prefer for the replica to deliver its local mail to be delivered to the master.
I see two ways of achieving this:

change the replica's local transport to deliver mail to Postfix on the master server

or

change the replica's procmail to deliver mail to Cyrus-IMAP on the master server.

There is a third way which is for the replica server to relay everything to the master server (i.e. both internal and external emails) but that seems somewhat unnecessary. I don't need to change how external mail is handled - it's ok for either box to send external email via its local Postfix which then relays it externally.
What I really would like to do is just change how internal emails are processed on the replica so that it delivers them to the master, either into Postfix or into the IMAP server there.
Existing, working, configuration of my local transport in postfix's master.cf:
procmail-local  unix  -       n       n       -       -       pipe
    flags=OR user=cyrus argv=/usr/bin/procmail -t -m RECIPIENT=${mailbox}
    SENDER=${sender} EXTENSION=${extension} /etc/procmailrc

Existing, working, configuration of delivery to Cyrus in procmailrc:
DELIVERMAIL="/usr/lib/cyrus/bin/deliver -r $SENDER -a $RECIPIENT $RECIPIENT

Answer: I'd do it earlier still - by removing your domain name from mydestination, mail for that domain should be delivered to the server listed as MX for your domain. If you want to specify a server, as opposed to using the MX, you should use the file /path/to/postfix/etc/transport for this. That file should then look like this:
# all mails to our own domain should go to our main incoming server
ourdomain.com    smtp:[mainserver.ourdomain.com]

Please note that the [] marks are important - without them, your server will still lookup the MX record for mainserver.ourdomain.com.
After editing the file, use the command postmap /path/to/postfix/etc/transport
You also need to make sure that postfix knows to use that file, by editing /path/to/postfix/etc/main.cf with the following line:
transport_maps = hash:/path/to/postfix/etc/transport

if your system uses db files. If instead it uses dbm files, the line should be
transport_maps = dbm:/path/to/postfix/etc/transport

Comment: I have gotten to the bottom of the name resolution issue. How Postfix resolves smtp hostnames is defined by the main.cf `smtp_host_lookup` and this defaults to ussing dns only. By explicitly configuring this as `smtp_host_lookup = dns,native`, Postfix will use native mechanisms (as defined by `/etc/nsswitch.conf`, typically so that `/etc/hosts` is used). So - problem solved :)
Comment: My domain's MX is actually at an external service provider and my master uses fetchmail to grab mails from there. While I can see the sense in this it wouldn't work in my case (even if I wanted to relay the mail externally, am unable to set up MX records for a subdomain like myhost.mydomain.com).
Comment: OK, I'll amend my answer to match that.
Comment: When I put `.mydomain.co.uk smtp:[master.mydomain.co.uk]` in the transport file it does not work. When I put `.mydomain.co.uk smtp:10.0.2.2` it works as expected. I needed the leading period because the sending server is a subdomain. I have `master.mydomain.co.uk` defined and I can ping it so I know it resolves. I have not fogotten postmap and I have also restarted postfix, just in case. Any ideas why it's failing to map using the name?
Comment: Do your logs give any hint?
Comment: Well I did check my logs and I have been investigating what I found in there (*Host or domain name not found. Name service error for name=master.mydomain.co.uk type=AAAA: Host not found*). First, I wondered why it was trying ipv6 lookups so I added `inet_protocols = ipv4` to main.cf but all that did was change the error message from AAAA to A. I then tried something else and it now works: I added a record for master.mydomain.co.uk to an internal DNS server. So this begs the question of why the `/etc/hosts` lookup does not work but we're probably going beyond the scope of my original question!
Answer: You could try using transport_maps, on the replica you put in main.cf
transport_maps = hash:/etc/postfix/transport

then in /etc/postfix/transport you put
localdomain smtp:yourmasterserver

don't forget to:
postmap /etc/postfix/transport

and restart postfix
Comment: I had been playing with transport maps before posting the question but without success. Seeing your suggestion, I tried what you described but it didn't work for me. I have pasted my main.cf at http://pastebin.com/UP5pJWsK. I created the transport file using the IP address of the master server (as in `localdomain smtp:10.0.2.2`). I did `postmap` and `postfix reload`. I also tried a stop/start cycle).
Comment: Well I've been playing around a little more and I have got it to work by placing `.mymaildomain.co.uk smtp:10.0.2.2` in the transport file (I added the leading dot so it maps subdomains used the actual domain name rather than `localdomain`)
Answer: The setup is:
$ grep domain.com /etc/postfix/*
/etc/postfix/main.cf:relay_domains = domain.com
/etc/postfix/relay_recipients:"@domain.com OK"
Binary file /etc/postfix/relay_recipients.db matches
/etc/postfix/transport:domain.com  smtp:[ip.address.of.the.mail.server]
Binary file /etc/postfix/transport.db matches

Also don't forget:
$ postmap /etc/postfix/transport
$ postmap  /etc/postfix/relay_recipients
$ /etc/init.d/postfix restart

(CentOS,RedHat)
Comment: Are you sure that `relay_domain = domain.com` is safe? On my end I use `mynetwork = IP, IP, IP...` which seems way more secure (although you have to make sure you keep your IPs up to date, of course.)


Derive Equation from the set of values

Question: I have a set of values
$$\begin{array}{|c|c|}
  \hline\text{$X$} & \text{$Y$} \\ \hline
 1 & 2 \\ \hline
 2 & 10 \\ \hline
 3 & 30 \\ \hline
 4 & 68 \\ \hline
\end{array}$$
Looking at the above numbers, I have done trial and error options and found the formula $Y=X^3 + X$ to get the value for $X=5$ to be 130.
Is there any mathematical theorem or algebraic way to find the above value without trial and error method. what is the best way to get the above formula mathematically? Any pointers to related subjects using which we can derive the equation would be very helpful.
Thanks,
Rajesh.
Comment: Look up regression and interpolation.
Comment: If you're interested in math, try to prove that there is an unique polynomial of grade at least $n$ that $f(x_0)=y_0 \ldots f(x_n)=y_n$ for given $x_i$s and $y_i$s.
Comment: I will edit your answer because writing "$1=02$", "$2=10$" is nonsense.
Answer: If a finite number (the first few) of terms of a sequence are given and if that sequence is:-
(1) in Arithmetic Progression; or
(2) in Geometric Progression; or
(3) in some nicely behaved pattern (like a Harmonic Progression or a Fibonacci Sequence ),
then answer is YES – a formula for the general term can be found.
Otherwise is NO, because the values of the first few terms can come up from nowhere without any specific pattern.
Furthermore, there is a bad news for some and unfairness to those who are required to guess the pattern. This is because even if that formula has been eventually "guessed correctly", it is still not unique.
To see why, refer to my answer in
Technique for finding the nth term

Derive Equation from the set of values


Dynamic filters based on another filter

Question: I am looking to create something similar to this http://www.smartoplug.com/partsearch I need my view to sort based off of multiple parameters. So if you select ford in the make, the models dynamically change to only show models that are referenced in ford i.e. F150, F250 etc.
I've looked into hierarchical select, and simple hierarchical select, but it doesn't look like what i am wanting to accomplish
Answer: Views Dependent Filters

Allows the presence of exposed filters on a view to be controlled by
values in another exposed filter. Dependent filters are hidden when
not relevant, and their values are not considered when the exposed
form is submitted.
Comment: Thank you, but I do question this part tho. "Dependent filters are hidden when not relevant, and their values are not considered when the exposed form is submitted."

It sounds to me that it's more of a condition then an actual filter. You select a value in filter A and it either shows Filter B or C, but what i need it to do is if you select a value in A it changes the values inside of B

Dynamic filters based on another filter


Adicionar botões na action bar quando se usa ListActivity?

Question: Bom tenho uma aplicação que gostaria de manter o formato padrão em todas as telas,
eu tenho um menu de acesso rápido com alguns itens na action bar, e outro completo na opção menu.
O problema é que como eu estou usando o ListActivity isso não esta funcionando.
não entendo muito bem essa parte de extensões, se alguém tiver um bom material que explica, em português por favor, eu agradeço.

Meu código XML do menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.starlighting.MainActivity" >

    <item
        android:id="@+id/menugerenceditar"
        android:icon="@drawable/editar"
        android:orderInCategory="1"
        android:title="Editar"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/menuadd"
        android:icon="@drawable/site"
        android:orderInCategory="2"
        android:title="Gerenciar"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/menuremov"
        android:icon="@drawable/site"
        android:orderInCategory="3"
        android:title="Importar"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/menuexportar"
        android:icon="@drawable/site"
        android:orderInCategory="4"
        android:title="exportar"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/menuimportar"
        android:icon="@drawable/site"
        android:orderInCategory="5"
        android:title="Star Lighting"
        app:showAsAction="ifRoom"/>

    <item
        android:id="@+id/menusair"
        android:icon="@drawable/sair"
        android:orderInCategory="100"
        android:title="Sair"
        app:showAsAction="ifRoom"/>

</menu>

Meu código de inflater menu:
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch(NoSuchMethodException e){
            } catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    return super.onMenuOpened(featureId, menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.gerenciar, menu);
    getMenuInflater().inflate(R.menu.gerenciarescondido, menu);
    onMenuOpened(Window.FEATURE_ACTION_BAR, menu);
    return true;
}

//seta as opções de função dos itens do menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();
    if (id == R.id.menugerenceditar) {
        //List<Lista> lista = gerarlista(tabbanco);

        //final Listaadapter listasadapter = new Listaadapter(this,  lista);
        //setListAdapter(listasadapter);

        getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            //Lista tabela = listasadapter.getItem(position);
            Intent intent = new Intent(gerenciar.this, gerenciartabela.class);
            //intent.putExtra("tabela", tabela.getNome());
            startActivity(intent);
        }});
        return true;
    }
    if (id == R.id.menuadd) {
        Intent intent = new Intent(gerenciar.this, adcionartabela.class);
        startActivity(intent);
        return true;
    }
    if(id==R.id.menuremov){
        return true;
    }
    if (id == R.id.menusair) {
        AlertDialog.Builder mensagem =
                new AlertDialog.Builder(gerenciar.this);

        mensagem.setTitle("Atenção!");
        mensagem.setMessage("Deseja realmente sair?");
        mensagem.setIcon(R.drawable.sair);

        mensagem.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                gerenciar.this.finish();
            }});
        mensagem.setNegativeButton("Não",null);
        mensagem.show();
        return true;

    }
    if (id == R.id.menuexportar) {
        return true;
    }
    if (id == R.id.menuimportar) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Meu código 'lista'
        package com.example.app;
    public class Lista {

        private String nome;
    //    private int idade;
     //   private int imagem;

        public Lista() {
        }

        public Lista(String nome) {
            super();
            this.nome = nome;

        }

        public String getNome() {
            return nome;
        }

        public void setNome(String nome) {
            this.nome = nome;
        }

          @Override
        public String toString() {
            return nome;
        }
    }

Meu código 'lista adapter'
    package com.example.app;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Listaadapter extends ArrayAdapter<Lista> {

        private Context context;
        private List<Lista> lista = null;

        public Listaadapter(Context context,  List<Lista> lista) {
            super(context,0, lista);
            this.lista = lista;
            this.context = context;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            Lista tabela = lista.get(position);

            if(view == null)
                view = LayoutInflater.from(context).inflate(R.layout.lista, null);

            TextView textViewNomeLista = (TextView) view.findViewById(R.id.text);
            textViewNomeLista.setText(tabela.getNome());

            return view;
        }
    }

Meu código que trabalha com a lista
    package com.example.app;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.os.Bundle;
import android.support.v7.internal.view.menu.ActionMenuView.ActionMenuChildView;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class gerenciar extends ListActivity{
    boolean editar=false, adcionar=false, remover=false;
    SQLiteDatabase Banco = null;
    Cursor cursor;
    List<Lista> tabelas = new ArrayList<Lista>();
    String tabbanco="Tabela1";
    TextView gerenciar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gerenciamento);
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        gerenciar=(TextView)findViewById(R.id.textViewgerenciar);
        gerenciar.setText("   Escolha a tabela que deseja trabalhar.");
        abrebanco();
        buscardados();
        List<Lista> lista = gerarlista();

        final Listaadapter listasadapter = new Listaadapter(this,  lista);
        setListAdapter(listasadapter);

        getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Lista tabela = listasadapter.getItem(position);
            Intent intent = new Intent(gerenciar.this, MainActivity.class);
            intent.putExtra("tabbanco", tabela.getNome());
            gerenciar.this.finish();
            startActivity(intent);

        }
        });

    }

    public List<Lista> gerarlista() {
        //tabelas.add(criarLista("Tabela1"));
        cursor.moveToLast();
        int x=cursor.getCount();
        while(x>0){
        //nextdado();
        tabelas.add(criarLista(retornadado()));
        dadoanterior();
        x--;
        };
        return tabelas;
    }
    public boolean dadoanterior() {
        try{
            cursor.moveToPrevious();
            return true;

        }
        catch(Exception erro){
            return false;

        }

    }

    public boolean nextdado(){
        try{
            cursor.moveToNext();
            return true;

        }
        catch(Exception erro){
            return false;

        }
    }
    private Lista criarLista(String nome) {
        Lista tabelas = new Lista(nome);
        return tabelas;
    }
    public boolean buscardados(){
        try{
            cursor = Banco.query("tabela",
                    new String [] {"tabelas",}
            , null, null, null, null, null);

            if (cursor.getCount() != 0){
                cursor.moveToFirst();

            }else{
                String sql = "INSERT INTO tabela (tabelas) " +
                          "values (Tabela1) ";
                    Banco.execSQL(sql);
            }

            return true;
        }
        catch(Exception erro){
            Exibirmensagem("BANCO", "erro ao buscar no banco: "+ erro.getMessage(), "ok");
            return false;
        }
    }
    public String retornadado(){
        String dado = cursor.getString(cursor.getColumnIndex("tabelas"));
        return dado;
    }
    public void abrebanco(){
        try{
            Banco = openOrCreateDatabase("banco", MODE_WORLD_WRITEABLE, null);
            String sql ="CREATE TABLE IF NOT EXISTS tabela (ID INTEGER PRIMARY KEY" +
                    ", tabelas TEXT)";
            Banco.execSQL(sql);

        }
        catch(Exception erro){
            Exibirmensagem("BANCO", "erro ao criar banco: =/"+ erro.getMessage(), "ok");
        }
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
            if(menu.getClass().getSimpleName().equals("MenuBuilder")){
                try{
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch(NoSuchMethodException e){
                } catch(Exception e){
                    throw new RuntimeException(e);
                }
            }
        }

        return super.onMenuOpened(featureId, menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.gerenciar, menu);
        getMenuInflater().inflate(R.menu.gerenciarescondido, menu);
        onMenuOpened(Window.FEATURE_ACTION_BAR, menu);
        return true;
    }

    //seta as opções de função dos itens do menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        if (id == R.id.menuadd) {
            Intent intent = new Intent(gerenciar.this, adcionartabela.class);
            startActivity(intent);
            gerenciar.this.finish();
            return true;
        }
        if(id==R.id.menuremov){
            AlertDialog.Builder mensagem =
                    new AlertDialog.Builder(gerenciar.this);

            mensagem.setTitle("Atenção!");
            mensagem.setMessage("Deseja realmente excluir uma tabela?" +
                    "\n Os dados contidos serão excluidos permanentemente!");
            mensagem.setIcon(R.drawable.sair);

            mensagem.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {

                    Intent intent = new Intent(gerenciar.this, excluirtabela.class);
                    startActivity(intent);
                    gerenciar.this.finish();

                }});
            mensagem.setNegativeButton("Não",null);
            mensagem.show();
            return true;

        }
        if (id == R.id.menusair) {
            AlertDialog.Builder mensagem =
                    new AlertDialog.Builder(gerenciar.this);

            mensagem.setTitle("Atenção!");
            mensagem.setMessage("Deseja realmente sair?");
            mensagem.setIcon(R.drawable.sair);

            mensagem.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {

                    gerenciar.this.finish();

                }});
            mensagem.setNegativeButton("Não",null);
            mensagem.show();
            return true;

        }
/*      if (id == R.id.menuexportar) {
            Intent intent = new Intent(gerenciar.this, exportararquivo.class);
            //intent.putExtra("tabbanco", tabela.getNome());

            startActivity(intent);
            gerenciar.this.finish();
            return true;
        }
        if (id == R.id.menuimportar) {
            Intent intent = new Intent(gerenciar.this, importararquivo.class);
            //intent.putExtra("tabbanco", tabela.getNome());

            startActivity(intent);
            //gerenciar.this.finish();
            return true;
        }*/
        return super.onOptionsItemSelected(item);
    }
    public void Exibirmensagem (String titulo,
            String texto, String button)
    {
        AlertDialog.Builder mensagem =
                new AlertDialog.Builder(gerenciar.this);
        mensagem.setTitle(titulo);
        mensagem.setMessage(texto);
        mensagem.setNeutralButton(button,null);
        mensagem.show();
    }

}

Comment: Joannis está usando o `appcompat` para utilizar a ´ActionBar`? Se for infelizmente o `ListActivity` não tem relação com ele, logo não tem ´ActionBar`. O que você pode fazer é criar uma subclasse do `ActionBarActivity` e incluir as facilidades que o `ListActivity` trás, olhando o código fonte: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/android/app/ListActivity.java.
Comment: sim é isso mesmo, como eu faço isso?
Comment: Posso montar uma resposta a noite, mas a ideia é copiar o código do `ListActivity` e fazer uma subclasse de `ActionBarActivity`, para se ter as funcionalidades do `ListActivity` no `ActionBarActivity`.
Comment: eu entendi a ideia, só não sei como criar uma subclasse, nem como usá-la, bom vou tentar se eu conseguir posto aqui a resposta, se não agradeço muito sua ajuda.vlw
Comment: Não precisa chegar ao ponto de copiar código-fonte de `ListActivity` (apesar de ser uma classe pequena), só fazer algo assim já é suficiente: http://stackoverflow.com/questions/2242136/how-can-i-implement-a-listview-without-listactivity-use-only-activity
Comment: É, copiar o código não vai ser simples. Tem um método que o `ActionBarActivity` deixou como `final` mas que o `ListActivity` usa... Acho que a idéia do @Piovezan é mais simples.
Comment: @wakim você pode me dar uma ajuda com isso?
eu deixei de lado por um tempo e agora ta pegando, (só falta isso)
olhei o link do piovezan mas não entendi direito.
vou postar meus códigos na pergunta.
Comment: @Joannis, você precisa trocar a superclasse de `ListActivity` para `ActionBarActivity` e fazer os devidos acertos. Posso preparar uma resposta mais tarde, no momento estou meio enrolado no trabalho...
Comment: ok, tentarei fazer aqui, vou ver o que eu consigo, vou postando meu avanço.
grato
Answer: Como sugeri anteriormente, a ideia é criar uma classe que herde de ActionBarActivity mas que tenha os métodos de suporte da classe ListActivity.
A classe seria essa:
public class ListActionBarActivity extends ActionBarActivity {

    /**
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    protected ListAdapter mAdapter;
    /**
     * This field should be made private, so it is hidden from the SDK.
     * {@hide}
     */
    protected ListView mList;

    private Handler mHandler = new Handler();

    private Runnable mRequestFocus = new Runnable() {
            public void run() {
                    mList.focusableViewAvailable(mList);
            }
    };

    /**
     * This method will be called when an item in the list is selected.
     * Subclasses should override. Subclasses can call
     * getListView().getItemAtPosition(position) if they need to access the
     * data associated with the selected item.
     *
     * @param l The ListView where the click happened
     * @param v The view that was clicked within the ListView
     * @param position The position of the view in the list
     * @param id The row id of the item that was clicked
     */
    protected void onListItemClick(ListView l, View v, int position, long id) {
    }

    /**
     * Ensures the list view has been created before Activity restores all
     * of the view states.
     *
     *@see android.app.Activity#onRestoreInstanceState(android.os.Bundle)
     */
    @Override
    protected void onRestoreInstanceState(Bundle state) {
            ensureList();
            super.onRestoreInstanceState(state);
    }

    /**
     * @see android.app.Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
            mHandler.removeCallbacks(mRequestFocus);
            super.onDestroy();
    }

    /**
     * Provide the cursor for the list view.
     */
    public void setListAdapter(ListAdapter adapter) {
            synchronized (this) {
                    ensureList();
                    mAdapter = adapter;
                    mList.setAdapter(adapter);
            }
    }

    /**
     * Set the currently selected list item to the specified
     * position with the adapter's data
     *
     * @param position
     */
    public void setSelection(int position) {
            mList.setSelection(position);
    }

    /**
     * Get the position of the currently selected list item.
     */
    public int getSelectedItemPosition() {
            return mList.getSelectedItemPosition();
    }

    /**
     * Get the cursor row ID of the currently selected list item.
     */
    public long getSelectedItemId() {
            return mList.getSelectedItemId();
    }

    /**
     * Get the activity's list view widget.
     */
    public ListView getListView() {
            ensureList();
            return mList;
    }

    /**
     * Get the ListAdapter associated with this activity's ListView.
     */
    public ListAdapter getListAdapter() {
            return mAdapter;
    }

    private void ensureList() {
            if (mList != null) {
                    return;
            }
            setContentView(com.android.internal.R.layout.list_content_simple);

    }

    private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {
                    onListItemClick((ListView)parent, v, position, id);
            }
    };

}

Foi preservado o máximo do código que a ListActivity provê, porém apenas um método ficou de fora. Porque a ActionBarActivity sobreescreve o método onContentChanged de adiciona o modificador final, logo não é possível sobreescrevê-lo na classe criada.
Mas pelo seu uso, a ListActionBarActivity deve atender.


Node.js - Pegar parâmetro de URL via Jade

Question: Tenho a seguinte url http://localhost:3000/user/index/1, gostaria de conseguir pegar o último parâmetro. Tentei com - var name = window.location.pathname; colocando no arquivo .jade e não funcionou, mas se for possível fazer um redirect com envio de parâmetros como no .render().
Answer: Solução:
notification: request.params.message

Este modo está em desuso:
notification: request.param('message','')


Como restringir o valor da variável com apenas duas casas decimais?

Question: Quero o r com apenas duas casas decimais.
double r = Math.abs ((aux5 - ((aux1*aux2)/previsoes.length))/ (Math.sqrt(Math.abs((aux3 - ((aux1 * aux1) / previsoes.length)) * (aux4 - ((aux2 * aux2) / previsoes.length))))));

Comment: Possível duplicata de [Formatar double em Java](https://pt.stackoverflow.com/questions/55720/formatar-double-em-java)
Answer: Você pode utilizar esses dois métodos que deixam os valores truncados
Se você quer que o resultado for um double:
public static double truncate(double value) {
    return Math.round(value * 100) / 100d;
}

Se você quer um String:
public static String truncate(double value) {
    DecimalFormat df = new DecimalFormat("#.00");
    return df.format(value);
}

Agora é só chamar o método passando como atributo o valor de r, que ele será transformado para um valor com apenas duas casas depois da virgula
Comment: Obrigado! Tudo certo!


is it possible to disable concurrent workflow execution at same time from admin console?

Question: In our project, at individual workflows, we have enabled concurrent workflow execution, that enables execution of the same workflow at the same time, and this can cause some issues.
There are 100's of such workflows. Is there a way to disable the concurrent workflow execution at the same time from admin console, so that we avoid having to disable it at each workflow?
This would save time and effort.
Comment: Disable concurrent workflow?
Comment: Yes I want to know if it's possible to disable concurrent workflow through admin console as there are 100's of workflows and disabling each of them is time consuming.
Answer: Mass-editing workflows is not supported. And there is no global configuration to enable / disable concurrent execution.
One way might be: exporting => editing XML files => importing. Or using external scheduler to take care of it.


Is it correct to add methods in index.js module?

Question: I know that index.js is tipacally used for importing/exporting stuff and make a clean api. But, can it also be used to implement some functionality?
For example, if I have the following directory
<code>   /mailing
      index.js
      mail.js
</code>
and only one method "sendMail()" in mail.js... is it a correct use to move the function implementation directly to index.js, avoinding importing and exporting mail.js on it?
Comment: Sure. It's fine. Do the simplest thing that gets it done.
Answer: Technically yes, but you should avoid to add any.
To add any method, use the best practices to create a component.


Python ML library for multi categorical + discrete predictors for a discrete outcome

Question: I hope this message will find you well.
First of all, I'd like to apologize for not using the most commonly used words in statistic's field : I have no background at all in that, and my knowledge is limited to memories from math lessons, and some thoughts I got reading articles these past weeks. Most of them would point that it might be necessary to use dummies, or strongly advise for. (like that old one)
I would like to avoid that, due to the extra processing (I would end in having so many columns) and increased difficulty to use the result. Althought, I will have to do it once, in order to look for correlations between them and the outcome, in order to reduce the weight of datas. But once only, as i will have to re-use the algorithm on a regular basis, with the original data set structure, but new datas. I will check once in a while if there is no change in most important factors, but it should not as the environment is destined to evolve really slowly.
My data set has no missing value at all. Most of the variable are categorical (non ordinal), some are discrete. My dependent variable (the outcome I'd like to predict) is one of those discret variable.
I followed some Python tutorial and I'd like to stick to that language (if possible) in order to increase my knowledge and understanding of it.
So my question is : is there a library/module in Python I should read the documentation from in order to be able to predict my outcome according to my data structure ?
Do not hesitate to let me know if I'm not clear enough (said I was far from being a pro !), I would gladly edit my message.
Thank you for reading,
Thank you even more for answering !
have a nice day,
EDIT following jdjame's answer
To explain the context a little bit more, as it looks I was not specific enough. (as expected according to my initial message !)
My data set regroups different characteristics of employee :gender, age, seniority, type of work, type of contract, affectation, and so on,
and number of planned working days with all those characteristics
and number of days of absence on planned working days with all those characteristics
My goal is, using previous behavior, to predict the number of absence days to expect in the next month on a specific job/location. I do not need specific visualization (expect for the first time to determine which determinants I should extract for my training base), as I would rather export the results to Excel-like file. (Might change idea on that point later on, as I learn, but it is not a priority) Every month, I would enrich my data set with the last ended month (and probably delete the oldest data so computer would still accept to run without burning (ie freezing)), so I can get more accurate / actual results.
I hope this edit is useful to you to provide me a more precise answer, even if our previous one already gives me some tracks to follow !
Thank you again.
Comment: I'd be shocked if you couldn't use scikit-learn.
Answer:
There are quite a few available python libraries that could help you work with data. If you could be more specific on the task you wish to accomplish that would help in supplying a better response. Right off the bat, I would suggest using the pandas library. You can look into the documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html. It will help you store your data into a DataFrame object (essentially a table) that will be more tractable. From there, it is a matter of what you wish to do with your data. To visualize it, I would suggest matplotlib which is a great library for graphing points (https://matplotlib.org/). Depending on the classifier you wish to use, you may want to use an sklearn supervised learning module (https://scikit-learn.org/stable/supervised_learning.html). If you wish to make a deep neural network, then I would suggest starting out with keras (https://keras.io/getting_started/) or pytorch (https://pytorch.org/get-started/locally/). It is important to note that all of these libraries are meant to work with machine learning data so many fo the preprocessing steps you might want to do (i.e. processing your categorical values).
Hopefully this provides a helpful start for you.
Comment: Thank you a lot for your time. I did edit my initial message to give some more insight on my task, I hope it will be helpful to you for helping me ! I did vote for your answer, but haven't enough reputation for it to be counted.


Surface integrals and surface areas of arbitrary parameter domains

Question: I'm having trouble evaluating this surface integral. This would be very simple to solve if the parameter domain of the variables $u$ and $v$ was a square region.
However, that isn't the case here. I've tried using a change of variables and saying that $u = r \cos (x)$, and $v = r \sin (x)$. Where $0 < x < 2\pi$, and $0 < r < 2$ for the limits of integration.
However, computing the surface differential (absolute value of the cross product of the partial derivatives) using the new variables has become way to complicating and tedious. I end up with about $6$ or $7$ terms of sines and cosines under a square root that cannot be simplified.
Was this the way to approach this problem? I also thought about parametrizing the region R, but I'm not sure how that would work.
Also, since I'm already here, how would I be able to do this for some general region R in the $u-v$ plane? This is what lead me to think of a parametrization of a region R. So basically a parametrization within a parametrization. Mhmm, sounds interesting. Is that possible?
Thank you.
P.S. Tried looking around and haven't been able to find something that could answer my question.
EDIT: So I found the surface differential, but when I  apply the the integral, am I suppose to add a jacobian since I am integrating in polar coordinates?
Answer: I think that part of the problem might be that you're trying too hard. First, you're using some definition to say that
\begin{align}
I
&= \frac{1}{4} \int_S dS \\
&= \frac{1}{4} \int_u \int_v  \| \frac{\partial \Phi}{ \partial u} \times \frac{\partial \Phi}{ \partial v} | ~du ~dv
\end{align}
where I've left the limits on $u$ and $v$ unwritten because they're a bit of a pain. Let's write out the integrand, though:
\begin{align}
 \frac{\partial \Phi}{ \partial u} &=\begin{bmatrix} 1 \\ 2u \\ 1 \end{bmatrix} \\
 \frac{\partial \Phi}{ \partial v} &=\begin{bmatrix} 1 \\ 2v \\ -1 \end{bmatrix} \\
 \frac{\partial \Phi}{ \partial u} \times \frac{\partial \Phi}{ \partial v} &=\begin{bmatrix} -2u-2v \\ 2 \\ 2v-2u \end{bmatrix} \\
\end{align}
The squared norm of that last item is
\begin{align}
 \frac{\partial \Phi}{ \partial u} &=\begin{bmatrix} 1 \\ 2u \\ 1 \end{bmatrix} \\
 \frac{\partial \Phi}{ \partial v} &=\begin{bmatrix} 1 \\ 2v \\ -1 \end{bmatrix} \\
\| \frac{\partial \Phi}{ \partial u} \times \frac{\partial \Phi}{ \partial v} \|^2
&= (2u+2v)^2 + (2u - 2v)^2 + 4\\
&= 4u^2 + 8uv + 4v^2 + 4u^2 - 8uv + 4v^2 + 4\\
&= 8u^2 +8v^2 + tu^2 + 4.
\end{align}
So that's your integrand, and the integral you want to find is
\begin{align}
I
&= \frac{1}{4} \int_S dS \\
&= \frac{1}{4} \int_u \int_v  \sqrt{8u^2 +8v^2 + tu^2 + 4} ~du ~dv
\end{align}
Now we change variables to $(r, x)$ as you suggested. The Jacobian determinant -- the determinant of the matrix of partials of $u$ and $v$ with respect to $r$ and $x$ is
$$
J = \left| \begin{matrix} \cos x & -r \sin x \\ \sin x & r \cos x\end{matrix}\right| = |r|
$$
and the new integral is
\begin{align}
I
&= \frac{1}{4} \int_S dS \\
&= \frac{1}{4} \int_u \int_v  \sqrt{8u^2 +8v^2 + tu^2 + 4} ~du ~dv \\
&= \frac{1}{4} \int_0^2 \int_0^{2\pi}  \sqrt{8(r \cos x)^2 +8(r \sin x)^2 +  4} J ~dx ~dr \\
&= \frac{1}{4} \int_0^2 \int_0^{2\pi}  \sqrt{8(r \cos x)^2 +8(r \sin x)^2 +  4} r ~dx ~dr \\
&= \frac{1}{4} \int_0^2 \int_0^{2\pi}  \sqrt{8r^2 +  4} r ~dx ~dr\\
&= \frac{1}{4} \int_0^2 \int_0^{2\pi}  4\sqrt{2r^2 +  1} r ~dx ~dr\\
&=  \int_0^2 \int_0^{2\pi}  (2r^2 +  1)^{\frac{1}{2}} r ~dx ~dr
\end{align}
Substituting $s = 2r^2+1; ds = 4r dr; \frac{1}{4}ds = dr$ in that last integral, we get
\begin{align}
I
&=  \int_0^2 \int_0^{2\pi}  (2r^2 +  1)^{\frac{1}{2}} r ~dx ~dr\\
&=  \int_0^{2\pi} \left( \frac{1}{4}\int_{r =0}^2   {s}^{\frac{1}{2}}  ~ds \right)~dx\\
&=   \frac{2 \pi}{4}\int_{r =0}^2   {s}^{\frac{1}{2}}  ~ds \\
&=   \frac{\pi}{2}\int_{r =0}^2   {s}^{\frac{1}{2}}  ~ds \\
&=   \frac{\pi}{2}\left. \frac{2}{3} s^\frac{3}{2} \right|_{r =0}^2  \\
&=   \frac{\pi}{2}\left. \frac{2}{3} (2r^2 + 1)^\frac{3}{2} \right|_{r =0}^2  \\
&=   \frac{\pi}{2} \frac{2}{3} \left( (8 + 1)^\frac{3}{2} -  1)^\frac{3}{2} \right)\\
&=   \frac{\pi}{2} \frac{2}{3} \left( 27 -  1 \right)\\
&=   \frac{\pi}{2} \frac{2}{3} 26\\
&=   \frac{26\pi}{3}.
\end{align}
I probably made an algebra mistake or two in there, but that's the gist of the thing. Hope it helps.


Triangle intersection in raytracer not working

Question: I am trying to make my own raytracer in Javascript. So far the spheres work very well. I now want to expand the capabilities to include triangles, and from there I can go to squares, cubes and beyond. The code I have for finding intersections with triangles is as follows
function triangleIntersection(t, r) {
var norm = triangleNormal(t);
var dist = triangleDistance(t);
var a = Vector.dotProduct(r.vector, norm);

if (a === 0) {
    return -1;
} else {
    var b = Vector.dotProduct(norm, Vector.add(r.point, Vector.negative(Vector.multiply(norm, dist))));
    var d = -1 * b / a;

    var qx = Vector.scale(r.vector, d).x + r.point.x;
    var qy = Vector.scale(r.vector, d).y + r.point.y;
    var qz = Vector.scale(r.vector, d).z + r.point.z;

    var q = new Vector(qx, qy, qz);

    var ca = Vector.subtract(t.points[2], t.points[0]);
    var qa = Vector.subtract(q, t.points[0]);
    var t1 = Vector.dotProduct(Vector.crossProduct(ca, qa), norm);

    var bc = Vector.subtract(t.points[1], t.points[2]);
    var qc = Vector.subtract(q, t.points[2]);
    var t2 = Vector.dotProduct(Vector.crossProduct(bc, qc), norm);

    var ab = Vector.subtract(t.points[0], t.points[1]);
    var qb = Vector.subtract(q, t.points[1]);
    var t3 = Vector.dotProduct(Vector.crossProduct(ab, qb), norm);

    if ((t1 >= 0) && (t2 >= 0) && (t3 >= 0)) {
        return 1 * b / a;
    } else {
        return -1;
    }
}

}
Triangle objects have a point array (points[]) and item 0 is point A, item 1 is point B and item 2 is point C. The parameter t is one of these triangles. The parameter r is a ray object, with properties point which is the origin, and vector, which is the direction.
I also have these functions for finding normal and distance of a triangle.
function triangleNormal(s) {
var ca = Vector.subtract(s.points[2], s.points[0]);
var ba = Vector.subtract(s.points[1], s.points[0]);
var norm = Vector.unitVector(Vector.crossProduct(ca, ba));

return norm;
}

function triangleDistance(t) {
    return Vector.dotProduct(triangleNormal(t, 0), t.points[0]);
}

When I render my scene the triangle I use in my scene is red colored. No matter how far I move my camera back the triangle fills the whole scene red. I do not know why this happens.
Comment: Why not try the *Moller-Trumbore* algorithm? https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
Comment: @meowgoesthedog Okay i will try this. Thx so much :)
Comment: @meowgoesthedog Ugh the same error. i guess i will have to start from scratch again...
Comment: you should test the intersection code with some artificial cases, to make sure that it is not the problem. In fact I recommend doing unit tests like this on all stages in the pipeline until you isolate the source of error.
Comment: see [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) and look for `//compute ray triangle intersection` comment `v0,v1,v2` are the triangle vertexes and `ray[i].dir,ray[i].pos` is the i-th ray direction and position.
Comment: @Spektre i am completely sure the intersection is not the problem... it must be the ray tracer itself... Hmm can you look at [the raytracer tutorial i am following](https://github.com/tmcw/literate-raytracer) and see if u can implement my code in it,but make it work? I am very stumped..
Comment: @Adam what about the *projection* part of your code? i.e. where it converts a screen coordinate to a ray. That could be where the problem lies.
Comment: @meowgoesthedog I checked that, even created a plane with normal 0, 1, 0, and distance -1, then sent a ray originating at 0, 10, 0 with direction 0, -1, 0 and the WinningObjectIndex (index of the collision in an array of all collisions) is still zero. I can send you my source code if you want it :)
Comment: @Adam yeah at this stage there's really no way for us to help you other than to look at your code. Post it on pastebin if you could
Comment: @meowgoesthedog I worked on a new version. see if you can help with this, it doesnt work either... why am i so bad at making stuff.. lol but see if you can find any errors, this doesnt detect any collisions. u can use console.log(wobj); at line 173 to c what i mean. Heres the link https://pastebin.com/7864idGh
Answer: An important error in your plane intersection code is this comparison operation:
a === 0
There are two things wrong with it:

For ray tracing you want the ray to hit planes in front of its source, not behind it, so you need a < 0.
Even if you did want the ray to hit planes behind it, you must never do equality operations between floating point values, because floating point calculations are not exact. (You must do something like abs(a) < 1e-6f or some small value)
Comment: Very useful information. will try and accept if works. :) Thank you sir


How to estimate right-skewed distribution with very small sample size?

Question: Suppose we have a data set consists of, say, 5 or 10 observations. The only thing we know about this set is that it came from a positive right skewed distribution.

How can we fit a probability distribution to this data?
Is there a paper or method (frequentist, Bayesian, nonparametric) that deals with such a problem?
Answer: You have a fairly small number of observations to make any kind of strong statement about the shape of the distribution.  The reaction time literature in psychology has an extensive number of papers on this subject with techniques like QMPE and QMLE developed for developing the RT distribution.  Typically it's examined to see if effects are in the tail or shift of the distribution.  The short story there is that much under 20 observations doesn't yield very reliable results.  You might look at Heathcote et al (2004) on QMPE.
It sounds like if you know what you do know, that you know what kind of data this is.  That may lead to theory that can provide more support for modelling.  The RT literature is specific to theory about how RT's are formed, why they're skewed, etc. (see linear ballistic accumulator model). Data of a different kind might be better served by different modelling techniques.  It really depends on what the shape of the distribution is caused by what model it will best follow.  It might be underlying Poisson, Weibull, Ex-Gaussian, etc.
In short, there is no answer to your question the way you've put it but there is some guidance that can be given on finding one.


Implementation of predictive variance in Gaussian process regression of scikit-learn

Question: I'm studying the implementation of Gaussian Process Regression in scikit-learn to get a better understanding of the topic.
There I've stumbled upon the following snippet:
if return_cov:
    v = cho_solve((self.L_, True), K_trans.T)  # Line 5
    y_cov = self.kernel_(X) - K_trans.dot(v)  # Line 6
    return y_mean, y_cov

It is from the file _gpr.py, around line 340.
The line numbers 5 and 6 in the comments apparently refer to the corresponding lines in algorithm 2.1 of the book  Gaussian Processes for Machine Learning:

\ is the matrix inversion operator like in MATLAB.
My question is Why is it not $\mathbf{v}^T\mathbf{v}$ in sklearn but instead ${\mathbf{k}^\ast}^T\mathbf{v}$?
I think it is wrong from dimensional analysis: $L$ has units of a standard deviation and $\mathbf{k}_\ast$ of a variance, so $\mathbf{v}$ has those of a standard deviation too. But then ${\mathbf{k}^\ast}^T\mathbf{v}$ has units of a standard deviation to the power of $1.5$ and not units of a variance.
Answer: This has to do with the fact, that cho_solve is different from \, and so sklearn's defintion of $v$ somewhat confusingly ends up not being the same as in the book.
In sklearn v_sk = cho_solve((self.L_, True), K_trans.T) would correspond to solving the system
$K v_{sk} = \mathbf{k}_*^\top$ with $LL^\top=K$ being the Cholesky decomposition of training data covariance, self.L_$=L$, K_trans$=\mathbf{k}_*$ and K=$K$.
However line 5 in the book $\mathbf{v} := L \setminus \mathbf{k}_*$ corresponds to solving $ L \mathbf{v} =  \mathbf{k}_*$. Substitute the corresponding definitions in the next line and you will end up with the same posterior covariance matrices.
For a better understanding of how cho_solve corresponds to forward/backward substitution compare how line 3 is implemented in _gpr.py :
$\mathbf{\alpha} := L^\top \setminus ( L \setminus \mathbf{y} )$
self.alpha_ = cho_solve((self.L_, True), self.y_train_)
Comment: Never would have guessed that the $v$ is not the same! Thank you very much!


Igualar linhas de dois DBGrids

Question: Tenho 2 dbgrids em locais distintos.
Porém, o primeiro DBGrid contém todas as informações (todos os títulos que busquei).
E no segundo DBGrid, contém as informações específicas sobre aquele título (quantidade de parcelas).
DBGrid1:

DBGrid2:

Segue código para amostra:
DMRetaguarda.zqConTitulos.Close;
DMRetaguarda.zqConTitulos.SQL.Clear;
DMRetaguarda.zqConTitulos.SQL.Add('SELECT t707tit.CD_LOTE as lote, t707tit.NR_TIT AS tit, ' +
  't707tit.NR_SEU as numdoc, t707tit.CD_CED as codced, t707tit.vr_jrs_dd as ' +
  'jurosaodia, t707tit.mt_am as multaaomes, t707tit.de_msg_1 as msg1, t707tit.vr_ioc as valorioc, t707tit.vr_abt as valorabat, t707tit.vr_dct as valordesc1, t707tit.vr_dct2 as valordesc2, t707tit.dt_dct as datadesc1, t707tit.dt_dct2 as datadesc2, ' +
  't707tit.de_msg_2 as msg2, t707tit.de_msg_3 as msg3, t707tit.id_pre_ipr as preimpresso, t707tit.DT_PAG as dtpag, t707tit.VR_PAG as valorpago, t707tit.STATUS_canc as status,');
DMRetaguarda.zqConTitulos.SQL.Add('t707tit.NR_NOS as nossonum, t707tit.DT_EMS ' +
  'as dtemi, t707tit.PZ_PRT as prazopro, t707tit.motivo_canc as motivocanc,');
DMRetaguarda.zqConTitulos.SQL.Add('t707tit.DT_VEN as dtven, ' +
  't707tit.DV_NR_NOSS as dv, t707tit.cd_ag as codag, t707tit.cd_bco as codbanco, ');
DMRetaguarda.zqConTitulos.SQL.Add('t707tit.VR_TIT as vltit, ' +
  't707cli.CD_IDT_CLI as codcli, t707tit.id_pre_ipr as preimpresso , t707tit.COD_CDC as codcentrocusto,');
DMRetaguarda.zqConTitulos.SQL.Add('t707cli.TP_CLI as tipocli, t707cli.NM_CLI ' +
  'as nomecli, ');
DMRetaguarda.zqConTitulos.SQL.Add('t707cli.CD_CAR as codcart, t707tcar.DE_CAR ' +
  'as descCart');
DMRetaguarda.zqConTitulos.SQL.Add('FROM t707tit, t707cli, t707tcar WHERE ' +
  't707tit.CD_CAR = t707tcar.CD_CAR AND t707cli.CD_CAR = t707tit.CD_CAR AND t707tit.CD_IDT_CLI = ' +
  't707cli.CD_IDT_CLI '+campo+' ORDER BY t707tit.NR_TIT,t707tit.NR_SEU LIMIT '+IntToStr(inicio)+', '+IntToStr(fim));
if maskEmi.Text <> '  /  /  ' then
  begin
    DMRetaguarda.zqConTitulos.ParamByName('pdataemi').AsDate := StrToDate(maskEmi.Text);
  end;
if maskDataVenc.Text <> '  /  /  ' then
  begin
    DMRetaguarda.zqConTitulos.ParamByName('pdataven').AsDate := StrToDate(maskDataVenc.Text);
  end;
DMRetaguarda.zqConTitulos.Open;

Código que capta o documento selecionado acima:
//selecionando todas as parcelas daquele título
DMRetaguarda.zqConParcelas.Close;
DMRetaguarda.zqConParcelas.SQL.Clear;
DMRetaguarda.zqConParcelas.SQL.Add('SELECT * FROM t707tit WHERE cd_idt_cli = ' +
':pcpfcnpj AND nr_seu like :pnumdoc ORDER BY nr_nos');
DMRetaguarda.zqConParcelas.ParamByname('pcpfcnpj').AsString := DMRetaguarda.zqConTitulos.FieldByName('codcli').AsString;
DMRetaguarda.zqConParcelas.ParamByname('pnumdoc').AsString := edtNumDoc.Text+'%';
DMRetaguarda.zqConParcelas.Open;

A diferença entre 1 e outro, é que lá no primeiro, eu selecionei a parcela 3/4, porem no meu segundo DBGrid, ele continuou na parcela 1/4, como faço para igualhá-las?
Comment: Tens algum código para mostrar? Talvez do `click` da primeira tabela.
Comment: @Earendul, fiz as alterações, o codigo é grande, é apenas uma busca para selecionar o num do documento, e o outro pra captar esse numero e colocar todas as parcelas relacionadas a aquele dbgrid.
Answer: Com o que deu para "pescar" sobre os processos que você executa, pensei no Locate.
Execute após o DMRetaguarda.zqConParcelas.Open;.
Exemplo:
DMRetaguarda.zqConParcelas.Open;
DMRetaguarda.zqConParcelas.Locate('numdoc',
    DMRetaguarda.zqConTitulos.FieldByName('numdoc').AsString, []);

Comment: Obrigado pela dica amigo, assim que possível irei testar sim! :D
Comment: Perfeito cara! Obrigado! Funcionou perfeitamente.

Igualar linhas de dois DBGrids


How many $k$-letter words are there such that the letters A and B are not next to each other?

Question: I spent the better part of a long car ride today thinking about the following problem:

Consider an alphabet of $n$ letters. How many $k$-letter words of this alphabet are there such that two certain letters (say A and B) are not next to each other? A word may contain the same letter multiple times.

My thoughts: There are $n^k$ words with length $k$ over the alphabet. We need to find and subtract away the number of words where A and B are next to each other. I modeled any such word as one with the string AB or BA at an index $i$:
$$x_1x_2x_3\cdots x_{i-1}ABx_{i+2}\cdots x_k$$
The $x_j$ here are all letters of the alphabet. We can think of the above word as an $(i-1)$-letter word, followed by AB, followed by a $(k-i-2)$-letter word. Thus, there are $n^i \cdot n^{k-i-2} = n^{k-2}$ words where AB or BA is at an arbitrary index $i$. Summing over all $i$, we get that there are $(k-2)n^{k-2}$ words containing AB and $2(k-2)n^{k-2}$ words containing AB or BA. Our final expression for the number of $k$-letter words such that A and B are not adjacent is
$$n^k - 2(k-2)n^{k-2}$$
There's an issue though. I'm over-counting words that contain AB or BA by a lot. I ran a simulation, and at $n=k=10$, for example, the correct value is $8441614754$, and my answer is $8400000000$. By this method, any word that contains AB or BA more than once is counted more than once. Is there any way I can modify this approach to yield the correct result? If not, how should I proceed?
Answer: You can handle this with coupled recurrences.  Let $A(k)$ be the number of words without $AB$ or $BA$ that end in a character other than $A$ or $B$.  Let $B(k)$ be the number of words without $AB$ or $BA$ that end in $A$ or $B$.  You can add any character to an $A(k)$ word, but only $n-1$ characters to a $B(k)$ word, one of which leaves it as a $B(k+1)$ word.  The recurrences are
$$A(k+1)=(n-2)A(k)+(n-2)B(k)\\
B(k+1)=2A(k)+B(k)\\
A(0)=1\\
B(0)=0$$
For small $k$ you can make a spreadsheet to do these.  For large $k$ you can do the eigenvalue/eigenvector thing on the matrix $$\begin {pmatrix} n-2&n-2\\2&1 \end {pmatrix}$$
The leading eigenvalue is $\frac 12\left(\sqrt{n^2+2n-7}+n-1\right)\approx n$ when both $k$ and $n$ are large.  You want $A(k)+B(k)$
I made a spreadsheet for $n=10$, shown below.  In the line for $n=2$ the $98$ under $A+B$ shows there are $98$ two character strings that are not $AB$ or $BA$.  As there are $10^2=100$ unrestricted strings and we rule out $2$, this is correct.  The $18\ B$ strings have one of $9$ characters (not a $B$) then an $A$, or one of $9$ characters (not an $A$) then a $B$.
Comment: I'm a little confused as to how you came up with the recurrences. What do you mean when you say that you can add any character to an $A(k)$ word, but only $n-1$ characters to a $B(k)$ word?
Comment: Because a $B(k)$ word ends in $A$ or $B$ there is one character you can't add without the word becoming unacceptable.  Because the $A(k)$ words do not end with $A$ or $B$, no addition will result in an $AB$ or $BA$
Comment: That makes sense. Thanks for your answer!
Answer: Consider the following automaton that accepts the strings not containing AB or BA,

From the DFA, using Chomsky-Schutzemberger method, we get,
$$q_0 = 1+xq_1+xq_2+(n-2)xq_0$$
$$q_1 = 1+xq_1+(n-2)xq_0$$
$$q_2 = 1+xq_2+(n-2)xq_0$$
(We omit $q_3$ and $q_4$ because it is a dead state)
On solving the above equations (i.e. substituting $q_1$ and $q_2$) we get the following expression for $q_0$,
$$q_0 = \frac{1+x}{1-(n-1)x-(n-2)x^{2}}$$
This is our generating function. The coefficient of $x^{k}$ gives the number of strings of length k not having AB or BA.
For example, for $n = k = 10$, Wolfram Alpha gives the following Taylor Series,

And as you can see, the coefficient of $x^{10}$ is equal to the answer you have provided.
Answer: This answer is based upon the Goulden-Jackson Cluster Method. We consider words of length $k\geq 0$ built from an alphabet $\mathcal{V}$ with $|\mathcal{V}|=n$ and the set $B=\{AB,BA\}$ of bad words, which are not allowed to be part of the words we are looking for.
We derive a generating function $A_n(z)$ with the coefficient of $z^k$ being  the number of wanted words of length $k$. According to the paper (p.7) the generating function $A_n(z)$  is
\begin{align*}
A_n(z)=\frac{1}{1-dz-\text{weight}(\mathcal{C})}
\end{align*}
with $d=|\mathcal{V}|=n$, the size of the alphabet and $\mathcal{C}$ the weight-numerator with
\begin{align*}
\text{weight}(\mathcal{C})=\text{weight}(\mathcal{C}[AB])+\text{weight}(\mathcal{C}[BA])
\end{align*}
We calculate according to the paper
\begin{align*}
\text{weight}(\mathcal{C}[AB])&=-z^2-\text{weight}(\mathcal{C}[BA])z\\
\text{weight}(\mathcal{C}[BA])&=-z^2-\text{weight}(\mathcal{C}[AB])z\\
\end{align*}
and get
\begin{align*}
\text{weight}(\mathcal{C}[AB])=\text{weight}(\mathcal{C}[BA])=-\frac{z^2}{1+z}
\end{align*}
It follows
\begin{align*}
\color{blue}{A_n(z)}&=\frac{1}{1-dz-\text{weight}(\mathcal{C})}\\
&=\frac{1}{1-nz+2\frac{z^2}{1+z}}\\
&\,\,\color{blue}{=\frac{1+z}{1-(n-1)z-(n-2)z^2}}\tag{1}
\end{align*}

Denoting with $[z^k]$ the coefficient of $z^k$ of a series, we obtain from (1) the number of valid words of length $k$ as
\begin{align*}
\color{blue}{[z^k]}\color{blue}{A_n(z)}&=[z^k]\frac{1+z}{1-(n-1)z-(n-2)z^2}\\
&=[z^k]\sum_{j=0}^\infty z^j\left((n-1)+(n-2)z\right)^j(1+z)\\
&=\sum_{j=0}^k[z^{k-j}]\left((n-1)+(n-2)z\right)^{j}(1+z)\\
&=\sum_{j=0}^k[z^j]\left((n-1)+(n-2)z\right)^{k-j}(1+z)\\
&\,\,\color{blue}{=\sum_{j=0}^k\binom{k-j}{j}(n-2)^j(n-1)^{k-2j}}\\
&\qquad\qquad\color{blue}{+\sum_{j=1}^k\binom{k-j}{j-1}(n-2)^{j-1}(n-1)^{k-2j+1}}\tag{2}
\end{align*}

Plausibility check: Evaluating (2) at $n=k=10$ we obtain
\begin{align*}
[z^{10}]A_{10}(z)=8\ 441\ 614\ 754
\end{align*}
in accordance with OPs calculation.


Convergence in probability of minimum

Question: This is a homework problem. Suppose we have a random sample $X_1,\ldots,X_n \overset{iid}{\sim} F$ with density $f(x) = 2(x-\theta)$ for $x\in (\theta,\theta+1)$. Let $X_{(1)} = \min{\{X_1,\ldots,X_n\}}$. Show $X_{(1)} \overset{P}{\longrightarrow} \theta$.
My attempt:
A few calculations first:
$F(x) = (x-\theta)^2$ for $x\in(\theta,\theta+1)$
$f_{X_{(1)}} = n(1-F(x))^{n-1}f(x) = 2n(x-\theta)(1-(x-\theta)^2)^{n-1}$
$$ \begin{align*} P(|X_{(1)} -\theta| > \epsilon) &= P(|X_1-\theta|> \epsilon, \ldots, |X_n-\theta|> \epsilon)\\
&= P(|X_1-\theta|> \epsilon)^n\\
&=  \end{align*}$$
Not exactly sure where to go from here. Any suggestions would be appreciated.
Comment: Have you noticed $P(|X_{(1)} -\theta| > \epsilon) = P(X_{(1)} -\theta > \epsilon)$ ?
Comment: Do you know of an inequality that allows to bound your last equation from above?
Comment: I see how $P(|X_{(1)}-\theta|>\epsilon) = P(X_{(1)}-\theta>\epsilon)$ but I don't see how this probability goes to zero.
Comment: @michael-mayer perhaps you are referring to chebychev inequality? I don't see how that can help.
Comment: Your solution is obviously easier than mine!
Answer: I think I got it from the hints you guys gave:
$$\begin{align*}
 P(|X_{(1)} -\theta| > \epsilon) &=  P(X_{(1)} -\theta > \epsilon)\\
&= P(X_{(1)} > \epsilon + \theta) \\
&= P(X_1 > \epsilon+\theta)^n\\
&= (1-\epsilon^2)^n
\end{align*} $$
and $\lim_{n\to\infty}{(1-\epsilon^2)^n}=0$ for $\epsilon \in (0,1)$.

Convergence in probability of minimum


Java, Apache Commons Configuration XML attributes

Question: I'm reading config from an XML file using Apache's commons.configuration. In one of the sections, I have several lines in the form of:
<bla attr1="something" attr2="something else" />

Ideally, I would like to do something like this:
List blas = config.getList("section.bla");
String attr1 = blas.get(0).getAttribute("attr1");

(The code above does not work, obviously)
Is this possible somehow, or do I have to query each attribute separately in the XPath-like notation, iterating myself through the list?
Answer: I think this is what you are looking for:
http://commons.apache.org/proper/commons-configuration/userguide-1.2/howto_xml.html
It would be something like this:
config.getString("bla(0)[@attr1]")

Answer: Well, I ended up manually iterating and querying for everything separately.


JSON isn't loaded in selection field on dialog launch

Question: This is my component's _cq_dialog 's .content.xml:
<?xml version="1.0" encoding="UTF-8"?>
                <jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:sling="http://sling.apache.org/jcr/sling/1.0" jcr:primaryType="nt:unstructured" jcr:title="Store Tile" sling:resourceType="cq/gui/components/authoring/dialog" helpPath="en/cq/current/wcm/default_components.html#Carousel">
                    <content jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/container">
                        <layout jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/layouts/tabs" type="nav" />
                        <items jcr:primaryType="nt:unstructured">
                            <titletext jcr:primaryType="nt:unstructured" jcr:title="Store Tile Properties" sling:resourceType="granite/ui/components/foundation/section">
                                <layout jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns" />
                                <items jcr:primaryType="nt:unstructured">
                                    <column jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/container">
                                        <items jcr:primaryType="nt:unstructured">
                                            <storelist jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/foundation/form/select" options="/apps/platform-do/components/general/store-tile/store.json" fieldLabel="Store" name="./jcr:storeID" optionsRoot="mydata" optionsTextField="label" optionsValueField="id" allowBlank="false" defaultValue="1" xtype="selection" type="select" />
                                        </items>
                                    </column>
                                </items>
                            </titletext>
                        </items>
                    </content>
                </jcr:root>

The field gets shown successfully but, the url "/apps/platform-do/components/general/store-tile/store.json" isn't loading...It should return this data:
{"mydata":[
  {"id":"1","label":"one"},
  {"id":"2","label":"two"},
  {"id":"3","label":"three"},
  {"id":"4","label":"four"},
  {"id":"5","label":"five"},
  {"id":"6","label":"six"}
]}

But the field stays empty.
What am I doing wrong?
Thanks!
Comment: Are you seeing the desired JSON back if you hit :/apps/platform-do/components/general/store-tile/store.json in your browser ?
Answer: I think the xtype selection is not able to understand the JSON that your are feeding ("/apps/platform-do/components/general/store-tile/store.json") .
As per the documentation , the format of the JSON has to be like the following one with text , value and/or qtip.
[
    {
        value: "pink", // all types except "combobox"
        text: "Pink",
        qtip: "Real Pink" // "select" and "combobox"
    }
]

Try to convert your store.json in the following structure and see if that works.
[
  {
    "text":"one",
    "value":"1"
  },
  {
    "text":"two",
    "value":"2"
  }
]