Mixed-Effect Poisson Regression Model without count data

Question: I have gone in circles reading about this for days and still not quite understanding.
I have aggregated data at the state level and by year estimating breast cancer incidence rate per 100 k, mortality rate per 100 k, mortality-to-incidence ratio, and a number of aggregated socioeconomic predictors including number of hospitals, percent minority, and average income.
I would like to fit a mixed effect model where my outcome variable is the mortality-to-incidence ratio, which ranges from 0 to 1.
If my model meets poisson model assumptions, is this valid even though Im not using a count variable as my dependent?
Comment: You might want to take a look at this thread : https://stats.stackexchange.com/questions/538576/which-family-in-glm-in-r-is-most-appropriate-for-a-response-variable-of-propor
Answer: If it were me I would fit a binomial glm with the response variable being the rate written with the cbind function.
If you were to analyze the rates of breast cancers as well as the mortality rate for 100k, it would require that you have one column with number of breast cancers, then one column with number of non breast cancers. Both would then add up to 100k. The same for the mortality rate, you could express it as one column for the number of deaths, one column for the number of survivors.
Then the model in R would look something like : glm~cbind(cancer,no_cancer)~number of hospitals + percent minority + average income, data=yourdata,family=binomial("logit")
Or maybe you want to check for interactions, in which case you can replace the + by *.
Now in the case of the mortality to incidence ratio, I hope I picture it correctly : I imagine it being the number of death among the number of breast cancers. Then I suppose I would express it as one column being number of deaths, an other being number of survivors (number of breast cancers-number of deaths). Then I would build a similar glm :
glm~cbind(deaths,survivors)~number of hospitals + percent minority + average income, data=yourdata,family=binomial("logit")


Learning Curve to Know Underfitting or Overfitting

Question: I want to know if the model I am using tends to be overfitting or underfitting. I am using SVM and Random Forest algorithms. How to figure it out?

Answer: I suggest plotting your observed versus predicted values as a first indication of whether your model may be overfitting or underfitting. The plots at https://bookdown.org/gmli64/do_a_data_science_project_in_10_days/models-underfitting-and-overfitting.html show good visual examples of overfitting and underfitting. An indication of overfitting is if your performance metrics are better for the training data than the test data. High bias and low variance can indicate underfitting.


Android Debug Bridge is unable to connect to Android 4.2.2

Question: I try to connect my mobile phone via adb connect to my pc. but there is always a error message:
C:> adb connect ip
unable to connect to ip:5555

I am using Windows 7 and a Samsung Galaxy S4 mini (GT-I9195) with android 4.2.2.
If i connect vie USB it works. Also it works with an Asus Tablet with Android 4.0.3
I've installed the latest version of the SDK
Comment: Is debugging enabled on the device? Enabling debugging on 4.2.2 requires a special procedure.
Comment: debugging is enabled. The reason was that the adb wasn't running on port 5555. adb tcpip 5555 with the device connected via USB solves the problem.
Answer: Try this:
adb tcpip 5555

Make shure that your device is connected via USB when lauching the command. Then adb connect should work.


Let $z$ be any point in $A\cap B\cap C$ and let $w$ be any point satisfying $|w-2-i|<3$.Then find $|z|-|w|+3.$

Question: Let $A,B,C$ be three sets of complex numbers as defined below:
$A=\{z:\text{Im} z\geq 1\}$,
$B=\{z:|z-2-i|=3\}$
$C=\{z:\text{Re}((1-i)z)=\sqrt2\}$
Let $z$ be any point in $A\cap B\cap C$ and let $w$ be any point satisfying $|w-2-i|<3$.Then find $|z|-|w|+3.$

Let $z=x+iy$.Then set $A$ is the set of points on and above the line $y=1$.
Set $B$ is the set of points on the circle $(x-2)^2+(y-1)^2=9$ and $C=\text{Re}((1-i)(x+iy))=\sqrt2$
$x+y=\sqrt2$
Hence $A\cap B\cap C$ has only one point of intersection.
I do not know how to solve it further.I am stuck here.
Comment: If any of the answeres below were useful to you, then you should [upvote](http://math.stackexchange.com/help/why-vote) all answers you find useful and [accept](http://math.stackexchange.com/help/accepted-answer) the one that was most useful to you. It is a way to show that you have found the answer to your question and it shows your appreciation. Now it seems like you still need help. If answers are not useful to you, then it helps if you say why not. This helps others to help you. For more information [read this](http://math.stackexchange.com/help/someone-answers).
Answer: As you wrote, we have
$$y\ge 1$$
$$(x-2)^2+(y-1)^2=9$$
$$x+y=\sqrt 2$$
Solving the system gives that
$$z=x+yi=\frac{1+\sqrt 2-\sqrt{7+6\sqrt 2}}{2}+\frac{-1+\sqrt 2+\sqrt{7+6\sqrt 2}}{2}i$$
By the way, the intersection points between the cirlce and the line passing through $(0,0)$ and $(2,1)$ are
$$\left(\frac{10\pm 6\sqrt 5}{5},\frac{5\pm 3\sqrt 5}{5}\right).$$
Hence, noting that $(0,0)$ is inside the circle,
$$0=\sqrt{0^2+0^2}\le |w|\lt \sqrt{\left(\frac{10+ 6\sqrt 5}{5}\right)^2+\left(\frac{5+ 3\sqrt 5}{5}\right)^2}=3+\sqrt 5.$$
Thus, the answer is
$$|z|-\sqrt 5\lt |z|+3-|w|\le |z|+3,$$
i.e.
$$\sqrt{5+3\sqrt 2-\sqrt{7+6\sqrt 2}}-\sqrt 5\lt |z|+3-|w|\le \sqrt{5+3\sqrt 2-\sqrt{7+6\sqrt 2}}+3.$$


Prove that $\bar z_1 z_2+z_1 \bar z_2=2\Re(\bar z_1 z_2)$

Question: Let us consider $z_1, z_2\in \mathbb C$; we have:
$$\bar z_1 z_2+z_1 \bar z_2=2\Re(\bar z_1 z_2)$$
it is easy to prove if we put $z_1=x_1+iy_1$ and $z_2=x_2+iy_2$. But suppose we do not want to use $z_1=x_1+iy_1$ and $z_2=x_2+iy_2$. How is it possible to prove the following relationship?
Comment: $z+\bar{z}=2Re(z)$
Answer: Hint: $z_1\bar{z}_2 = \overline{\bar{z}_1z_2}$.


Definition of the Transversal of a Graph

Question: Basic definition question that seems hard to find. Can someone provide me the definition for a transversal of a graph $G=(V,E)$? Specifically, I am considering bipartite graphs.
Answer: In combinatorics in general, given a family of sets $\mathcal S = \{S_1, \dots, S_n\}$, a transversal $T$ of $\mathcal S$ is a set with the property that $T \cap S_i \ne \varnothing$ for all $i$. (Often but not always, we require that $|T \cap S_i|$ be exactly $1$ for all $i$.)
There are plenty of graph-theoretic problems in which transversals come up; for example, any cut in $G$ is a transversal of the set of spanning trees of $G$ (considered as sets of edges).
If this is the definition you're looking for, then without knowing what set family you're thinking about to begin with, there's no more specific definition of what a transversal is.
Answer: Do you mean graph traversals? Or transversals in bipartite graphs?
Given a bipartite graph $G$ with bipartition $V(G)=X \cup Y$, a transversal in $G$ is a set of $|X|$ independent edges (ie a complete matching from $X$ to $Y$).  A transversal is also called a system of distinct representatives (SDR) because the bipartite graph can be viewed as representing a set system, with the vertices of $X$ correspond to the subsets.  Hall's marriage theorem asserts that the obvious necessary condition for a graph to have a transversal is also sufficient.
Graph traversals methods visit each vertex of the graph exactly once. Examples of graph traversal algorithms are BFS (breadth first search) and DFS (depth first search).
Comment: I do mean transversal.


Good resources for visualizing time-to-event(s) data where event is a continuous variable

Question: I am analyzing patients' rehabilitation use over a 5-year period. Describing their rehabilitation is challenging since:

I should describe the proportion excluded from rehabilitation (zero-inflatation)
I should describe amount (hours) received by receivers
Time-to-event analysis needed since rehabilitation's start may delay for a subproportion
Time-to-event analysis needed since rehabilitation may be short-living for a subproportion, meaning that I should also analyze its end (duration of rehabilitation)

Do you have good suggestions for visualizing such data? Kaplan-Meier curves with cumulative rehabilitation amount received? Any good resources for visualizing such data?
Comment: Need to distinguish visualization of summary measures vs. raw data.  For the latter see http://hbiostat.org/rflow/descript.html
Comment: This R package might be good. [survminer](https://cran.r-project.org/web/packages/survminer/vignettes/Informative_Survival_Plots.html)
Comment: Good point. I need to visualize summary data only as I have way too many to show individual-level estimates.
Comment: You are listing 4 different variables. What is your actual outcome? What is it you want the plot(s) to show? If you want to visualize 4 different things, you should probably use (at least) 4 different plots, not 1.  Can you provide an example dataset for people to work with?
Answer: I am not sure if it is what you are looking for, but if you are trying to visualize the effect of a continuous variable on a time-to-event outcome you may be interested in the contsurvplot R-package https://robindenz1.github.io/contsurvplot/


Com Javascript é possível executar automaticamente uma função em outro arquvio a parte?

Question: Tenho duas páginas separadas:

A primeira contém um formulário com dois campos input do tipo
date, dentre outras informações.
A segunda página recebe um dado dentre as informações do formulário que
citei antes, mas não as datas.

A primeira envia esses dados para o Mongo DB Atlas. A segunda mostra um desses dados, que é um título em forma de botão. Nessas duas páginas estou usando o Node.js também.
Tenho uma função que verifica se a data atual é maior que a data final passada pelo usuário. Se sim, quando esta condição for atendida eu gostaria que esse botão, que por sua vez está na segunda página, ou seja, em um arquivo html diferente, fosse desativado automaticamente.
Parece algo avançado para mim e não sei se é possível fazer tal coisa, pois envolve dois arquivos html diferentes, aonde um possui os input do tipo date - e consequentemente é neste arquivo que devo incluir o script que faz esta verificação - e outro, onde está o botão que preciso ter desativado quando esta condição - se a data atual for maior que a data final - for verdadeira.
Em resumo, preciso fazer com que essa função escrita em um arquivo a parte realize uma ação automática em outro arquivo html diferente.
Fiz uma pergunta aqui e um amigo do SOpt me ajudou a arrumar esta função, porém é só uma função e ainda não resolve este novo desafio que descrevi acima. Meu maior desafio agora é intercambiar esta função entre dois arquivos.
Comment: Olá, as informações estão salvas no banco de dados? se sim, no segundo arquivo html não é possível fazer uma consulta ao banco e verificar se a condição para exibir/desabilitar o botão é verdadeira?
Comment: @JhonataDesouza Sim, as informações estão em um banco de dados. Eu ainda não implementei a função por não saber como fazê-la rodar no segundo arquivo. No caso eu suponho que ela deve vir no primeiro arquivo, aonde estão os campos de data. Fiz uma pesquisa e acho que o import e export do javascript podem me ajudar, ainda não sei como mas irei testar.
Answer: Só com o frontend, você pode passar as datas para o outro arquivo usando localStorage, em formato de timestamp. Exemplo:
No primeiro arquivo, você salva no localStorage:
var minhaData = new Date();
localStorage.setItem( 'minhaData', minhaData.getTime() );

No segundo, você lê a data com:
var minhaData = new Date( localStorage.getItem('minhaData') );


Idea + Play Framework - cannot resolve method/symbol

Question: I use IntelliJ IDEA 12, Play 2.2.0, I have Scala plugin installed, Play 2.0 support plugin installed.
I created java play application via termina (play new). Andas you can see in this pictures I always get this error: "cannot resolve method ok()" or "cannot resolve symbol at"
http://sluchatka.kvalitne.cz/pics/1.png
http://sluchatka.kvalitne.cz/pics/2.png
I searched the web for this problem the whole day, but I didn't find solution.
Can you please help me?
Answer: The first problem (unable to resolve reference to template) is because your template probably has not been compiled by the moment (and thus IDEA does not see compiled template and can't resolve reference to it).
I usually develop Play apps having ~run command launched in Play console. So that any changes to templates are immediately compiled and changes detected by IDEA.
As for the second one it seems like a deficiency in Play IDEA plugin. It does not work seamlessly within templates I should say. Lots of glitches. The best way I found is to have ~run launched so that small problems are detected immediately.
Answer: According to intelliJ support it is better to import the command line created play 2 project using the IDE import project wizard instead.

File -> Import Project
Select play2 project location
Import as SBT project

This was due to the "play idea" command being outdated or not fully compatible with the current play 2 and scala plugins for intelliJ.
A bonus is that errors start to appear properly, we had trouble with that earlier.
Comment: It dows not work. After importing the project as SBT, the same errors appear.
Answer: I was having a similar issue with Play 2.4.2 and Intellij 14.1.4.  I could run the basic "play-java" example/starter app in Intellij, even though the editor showed errors like "cannot resolve method ok()".
Here's what solves the problem for me:

Before starting, make sure your project is not open in Intellij.
Delete the ".idea" folder from your existing app, OR just create a new app using "activator new".
On the command line, cd to the app's folder and compile using "activator compile".
In Intellij, File -> Open, and select your app's folder.  For the SBT import options, I left the boxes unchecked (e.g. "Use auto-import", "Create directories for empty content roots automatically", etc.) but I don't think it matters.

Once everything finishes loading, you shouldn't see the errors any more.
Comment: this is working for me, intelliJ Ultimate 14 + play 2.4.3
Answer: This click in the Project Structure is your answer.


typescript: Getting Argument of type 'any[]' is not assignable to parameter of type 'SetStateAction'

Question: I have a react app using typescript. In one of the sections, I need to combine a set of default users with negative userIds with other users returned by getUsers(orgId) api. Code is below:
   ....
   const [assignedUserOptions, setAssignedUserOptions] = useState([]);

   const getUsersAsync = async () => {
    const defaultUsers = [
      { value: "-1", label: "Developer" },
      { value: "-2", label: "Quality Analyst" },
      { value: "-3", label: "Senior Developer" },
      { value: "-4", label: "Senior Quality Analyst" },
      { value: "-5", label: "Manager" },
      { value: "-6", label: "Director" },
      { value: "-7", label: "SVP" },
      { value: "-8", label: "Support" },
    ];

    // getUsers Api call which returns an array of object like
    /*
    [
      {
        "userId": "https://userapi.mydomain.com/employee/id/1",
        "orgId": 1,
        "firstName": "Jeff",
        "lastName": "Smith"
      }
    ]
    */
    await getUsers(orgId).then((response) => {
      if (response) {
        const assignedToOptions = response.map(
          (x: { userId: string; firstName: string; lastName: string }) => ({
            value: x.userId.substring(x.userId.lastIndexOf("/") + 1),
            label: `${x.firstName} ${x.lastName}`,
          })
        );

        setAssignedUserOptions([...defaultUsers, ...assignedToOptions]); // warning here
      }
    });
  };
  ...

I get a warning as below.
Argument of type 'any[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
  Type 'any[]' is not assignable to type 'never[]'.
    Type 'any' is not assignable to type 'never'.ts(2345)

My company policy is to keep the "strict": true in tsconfig.json. I see the defaultUsers is showing up as {value: string; label: string;}[] and assignedToOptions as any. How can I fix the arrays to make the spread operator to work?
Answer: Since you did not pass a generic parameter explicitly, this empty array is inferred as never[]:
const [assignedUserOptions, setAssignedUserOptions] = useState([]);

The fix is simple: just add a generic parameter:
const [assignedUserOptions, setAssignedUserOptions] = useState<{ value: string; label: string }>([]);

Comment: Thank you! But after I made that change, I am getting `Argument of type 'never[]' is not assignable to parameter of type '{ value: string; label: string; } | (() => { value: string; label: string; })'.ts(2345)` on that line.


Vue Quassar Standalone example not working

Question: I am trying a simple standalone vue/quasar example by copy pasting all code from code pen into a single html file,
but its not working.

  <link href="https://firstname.lastname@example.com/dist/quasar.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">

      <q-btn style="background: #FF0080; color: white" label="Fuchsia"></q-btn>
      <q-btn flat style="color: #FF0080" label="Fuchsia Flat"></q-btn>
      <q-btn style="background: goldenrod; color: white" label="Goldenrod"></q-btn>
      <q-btn outline style="color: goldenrod;" label="Goldenrod"></q-btn>
      <q-btn color="grey-4" text-color="purple" glossy unelevated icon="camera_enhance" label="Purple text"></q-btn>

Comment: add the .js links in index.html at the end of `body` tag, after ``
Comment: Your given example is working ?
Comment: @NikleshRaut https://codepen.io/pen/?&editable=true&editors=101=https%3A%2F%2Fquasar.dev%2Fvue-components%2Fbutton , this one i followed
Comment: @IgorMoraru it worked thanks, but why it required after body  ?
Comment: @Chanky it's common practice to put javascript files at the bottom of body tag, because almost every js script relies on DOM being fully loaded to function properly. if you put them in `` tag, it will load before the DOM is ready, and this is prone to errors.
Answer: Here its working!
Head tag loads all style and script before Body and its HTML and QUASSAR or any other stand alone library which need to put application/plugin into a HTML element that must load after Body because.
Body and its element loads before script so script will be able to access those Html.
Here in your case the QUASSAR script needs   to boot app inside that

  <link href="https://email@example.com/dist/quasar.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">

      <q-btn style="background: #FF0080; color: white" label="Fuchsia"></q-btn>
      <q-btn flat style="color: #FF0080" label="Fuchsia Flat"></q-btn>
      <q-btn style="background: goldenrod; color: white" label="Goldenrod"></q-btn>
      <q-btn outline style="color: goldenrod;" label="Goldenrod"></q-btn>
      <q-btn color="grey-4" text-color="purple" glossy unelevated icon="camera_enhance" label="Purple text"></q-btn>


Validation não retorna array com erros

Question: Estou tentando utilizar o validation do laravel para validar um formulário, porém não estou recebendo o array de erros
Controller
<code>use Illuminate\Http\Request;
class Curriculos extends Controller {
public function store(Request $request){

    $this->validate($request, [
      'nome' => 'required|min:5'
    ]);

    $curriculos = $request->all();

    $curriculos['dt-nascimento'] = str_replace('/', '-', $curriculos['dt-nascimento']);
    $curriculos['dt-nascimento'] = date('Y-m-d', strtotime($curriculos['dt-nascimento']));

    $curriculos['dt-inicio'] = $this->formatDate($curriculos['dt-inicio']);

    $curriculos['dt-conclusao'] = $this->formatDate($curriculos['dt-conclusao']);

    $curriculo = new Curriculo;

    if($curriculo->create($curriculos)){
      return redirect('/');
    }

}
</code>
View
<code> <?php var_dump($errors->all()); ?>
 <form class="" action="{{url('/api/send')}}" method="post">
   @csrf
  ......
</code>
Resultado do Var_dump()
<code> array(0) { }
</code>
Comment: Onde está definir essa rota em web.php ou api.php
Comment: Em api, como Post
Answer: Quando são utilizadas rotas definidas em api.php, como referido pelo Luhan Salimena não é aplicado o middleware group ShareErrorsFromSession (ver configuração em Kernel.php).
Sem aplicação desse middleware group a variável $errors não é passada para a View
Veja a documentação em https://laravel.com/docs/5.6/validation
A rotas em api.php são adequadas para pedidos via Ajax nesse caso por defeito quando existe um erro de validação vai obter uma resposta Json com o código 422 com todos os erros encontrados.
Sugiro que altere a sua rota para web.php que parece ser mais adequado ao seu caso.
Comment: Funcionou aqui, muito obrigado :D

Validation não retorna array com erros


Contextual Filters within Block Display?

Question: using d7.4
I am trying to setup a block on my node pages that will show:
View Count
Comment Count
Flag Count
My node paths are setup as:
category/categoryname/nodetitle
I know I need a Contextual Filter to link this view to the Node Title (right???) - but how do I designate a path for that within a block? I know I can name a path with a View-Page Display- but I need this to be shown inside a block, not a page.  What am I missing here?
Thanks for the reply!  This newbie appreciates it!
Answer: In the Block Display, create a contextual filter with these settings:
Content: Nid (not title)
Under "When the filter value is NOT available" check "Provide default value" then choose "content id from url"
Under "When the filter value IS available or a default is provided" check "Specify validation criteria" then choose "Basic Validator" or any other you like.


How to repeat a VBA code through many rows using a cell in the same current row as reference

Question: I want to use a code to repeat itself through many rows because it would take too much time writing one by one, i dont know if there's a code that takes from the chosen cell that i put the macro the column and row of it.
now in column 1 and row 1 as reference

if (current Column (1), current row (1) ) = 1 then

column number 3, current row = 1
end if

next

now in column 1 and row 2 as reference

if (current Column (1), current row (2) ) = 1 then

column number 3, current row = 1

end if

I wanted to copy paste to code to go on in the entire column 1, but i dont have any idea how to do that, could someone help please?
Answer: We can loop over rows:
Sub kayky()
    Dim N As Long, i As Long
    N = 123
    For i = 1 To N
        If Cells(i, 1).Value = 1 Then Cells(i, 3).Value = 1
    Next i
End Sub

This handles 123 rows; modify to suit your data.


How to get form values in URL as keys of an array

Question: I have a custom form with min and max price value:
<code>    $form['price']['min'] = [
      '#type'          => 'hidden',
      '#default_value' => isset($_GET['price']['min']) ? $_GET['price']['min'] : '',
      '#attributes'    => ['id' => 'price-min'],
    ];

    $form['price']['max'] = [
     '#type'          => 'hidden',
     '#default_value' => isset($_GET['price']['max']) ? $_GET['price']['max'] : '',
     '#attributes'    => ['id' => 'price-max'],
    ];
</code>
and I would like the URL after the form is submitted to look like this:
<code>/search?price%5Bmin%5D=5000&price%5Bmax%5D=10000
</code>
but actually it's coming out like this:
<code>/search?min=5000&max=10000
</code>
How can I get the form values to appear in the URL in the required format?
Comment: Apart from your 'question' missing some details and the actual question, you should not use `$_GET` in your form, but get the parameters from the request object.
Comment: Ok Mario like this: '#default_value' => $form_state->getValue('price[min]'); ?
Comment: This makes no sense. As soon as you have a form state with a valid value (after a submission of this form), it will be the actual value of this field anyway. If you like to initialize the default values from the request URL, you'd have to use the request object. See https://drupal.stackexchange.com/questions/207044/how-to-get-post-and-get-parameters
Comment: There was a point to this question, which is addressed in my answer, but it does need editing for clarity.
Answer: Try adding:
<code>$form['price']['#tree'] = TRUE;
</code>
This tells the form builder you want to nest field names. Otherwise it will just use the most immediate element keys for the field names, as you have found.
This is actually useful behaviour most of the time, as it means you can group fields in containers such as fieldsets without affecting their name attributes.


Why is a par-4 playing 140 yards?

Question: According to Tiger Tracker, Tiger Woods drove the par-4 10th hole at the Accordia Golf Narashino Country Club in Japan. The catch is, it was only 140 yards.
I had to double check to see it wasn't a par-3. What gives? Why is a par-4 playing 140 yards?
https://twitter.com/GCTigerTracker/status/1187956379190874112
Answer: Rain, specfically flooding.
The PGA Tour released a statement regarding this:
"Given the rain that the golf course received, the maintenance team and the golf course itself responded remarkably. After spending the morning repairing bunkers and working tirelessly to get the course in appropriate condition, Rules and Competition staff have assessed the playability of 18 holes and have determined that due to the excessive amount of rain the par-4 10th will be significantly shortened for round 2. Preferred lies will be in effect for the second round. Should all players finish the second round on Saturday, the third round will begin Sunday morning at 6:30 a.m. local time."
The hole was shortened to 140-yards and par was unable to be changed to reflect this change.

Why is a par-4 playing 140 yards?


Forecasting discrete non normal data

Question: Today I was thinking about how one would treat what I'm guessing must come up constantly in statistics so apologies if it's trivial.
Some statistics such are easy to forecast, such as length, thanks to their normalish properties. But what if I'm interested in forecasting something like the number of children someone will birth? This seems to be a rather weird case to me. It's not only skewed, but it also has a lower bound of zero. Also, even if we're disregarding the time of pregnancy, the probability of birthing a child in a given year isn't independent, i.e. $P(X>0) \neq P(X>1|X>0)$, so it shouldn't be Poisson either.
How would one treat a phenomenon like this?
Comment: Since the normal is continuous, specifying 'discrete' already means it can't be normal. It is *not* a general property of the Poisson that $P(X>0) = P(X>1|X>0)$ (you may be partially confusing it with the geometric?)
Comment: You seem to be confusing conditions for a Poisson process with the a distribution observed at some time. While a Poisson process with constant rate will produce a Poisson distribution, a different process, or mixture of different processes might also do it. Further, the independence of events in a Poisson process is not the same as $P(X>0) = P(X>1|X>0)$. They're talking about different things.
Comment: To clarify: I agree that births over time *for one individual* don't follow a Poisson process. That doesn't *imply* that the total births for some person aren't Poisson - that doesn't automatically follow. However, total births won't be Poisson in any case, just because they won't be any named distribution in particular. Poisson might be a reasonable approximation in some cases, but probably not in others.
Comment: The distribution of numbers of births to a woman is very different in South Korea and in Niger - not just the average but also the distributional shape differs. It's unlikely that any country has a distribution of births that matches closely any particular named distribution.
Comment: You might find this information helpful: http://blogs.discovermagazine.com/gnxp/2012/08/what-is-the-distribution-of-offspring-per-individual/  (distributional information for the US)
Comment: "*solution*" may not be the word you're seeking. There's no equation here to solve. Real processes tend not to have named distributions, except as approximations. There are many discrete distributions that serve as useful approximations
Comment: @Glen_b I only meant that the probability of having a kid in a given year is dependant on if you already have a kid. A poisson process needs to have independent probabilities, no?
Comment: So you're basically saying there's no general solutions to these types of distributions?
Answer: Let's take a look at a specific country; data for the USA
For all women, 15 to 44:
47.1% had 0 kids
16.9% had 1
20.4% had 2
10.4% had 3
3.4% had 4
1.5% had 5 or 6
0.3% had 7 or more.

which averages (making a couple assumptions) to $1.13$ kids per woman.
A Poisson with $\lambda = 1.13$ gives:
0 = 32%
1 = 37%
2 = 21%
3 = 7.5%
4 = 2.1%
5 = 0.4%
more = 0.1%

so, at least for the USA, it's not Poisson. Perhaps a zero inflated negative binomial would work.

Forecasting discrete non normal data


Cannot configure sources

Question: I got in trouble with installing app from source code. From console I started configure script, fixed some missing packages but when I got
configure: WARNING: freetype2 development libraries not present (Debian package is libfreetype6-dev)
I executed command
sudo apt-get install libfreetype6-dev
but I still got the same error. What wrong? I use Linux 15.04
Comment: No you don't, Linux just arrived at major version number 4.
Answer: A configure script is a strong indication that GNU autotools are used. In that case, you will find what went wrong in the (huge) config.log file. Look near the end (several pages up from it because the end is cluttered with not so helpful general output about this run of the script).
The autotools are complicated (some would say, a mess) and it's easy to get something subtlety wrong, so my guess would be the check for freetype2 somehow fails to detect it the way the package installed on your system. If you find the source of the problem, you could hack the configure script (attention: huge again, it's auto-generated) to make it work. Sometimes it's enough to pass configure some CFLAGS making the compiler look for include files in the correct locations (e.g. CFLAGS='-I/usr/include/freetype2' ./configure  -- don't take this verbatim, analyze config.log and compare with dpkg -L libfreetype6-dev)

Cannot configure sources


Spark - LinearRegressionWithSGD on Coursera Machine Learning by Stanford University samples

Question: Software Version: Apache Spark v1.3
Context: I've been trying to "translate" Octave/MATLAB code to Scala on Apache Spark. More precisely, I work on ex1data1.txt and ex1data2.txt from coursera practical part ex1. I've made such translation into Julia lang (it went smoothly) and now I've been struggling with Spark...without success.
Problem: Performance of my implementation on Spark is very poor. I cannot even say it works correctly. That's why for ex1data1.txt I added polynomial feature, and I also worked with: theta0 using setIntercept(true) and with extra non-normalized column of 1.0 values(in this case I set Intercept to false). I receive only silly results.
So, then I 've decided to start working with ex1data2.txt. Below you can find the code and the expected result. Of course Spark result is wrong.
Did you have similar experience? I will be grateful for your help.
The Scala code for the exd1data2.txt:
import org.apache.spark.mllib.feature.StandardScaler
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.optimization.SquaredL2Updater
import org.apache.spark.mllib.regression.{LinearRegressionModel, LinearRegressionWithSGD, LabeledPoint}
import org.apache.spark.{SparkContext, SparkConf}

object MLibOnEx1data2 extends App {
  val conf = new SparkConf()
  conf.set("spark.app.name", "coursera ex1data2.txt test")

  val sc = new SparkContext(conf)
  val input = sc.textFile("hdfs:///ex1data2.txt")

  val trainData = input.map { line =>
    val parts = line.split(',')
    val y = parts(2).toDouble
    val features = Vectors.dense(parts(0).toDouble, parts(1).toDouble)
    println(s"x = $features y = $y")
    LabeledPoint(y, features)
  }.cache()

  // Building the model
  val numIterations = 1500
  val alpha = 0.01

  // Scale the features
  val scaler = new StandardScaler(withMean = true, withStd = true)
    .fit(trainData.map(x => x.features))
  val scaledTrainData = trainData.map{ td =>
    val normFeatures = scaler.transform(td.features)
    println(s"normalized features = $normFeatures")
    LabeledPoint(td.label, normFeatures)
  }.cache()

  val tsize = scaledTrainData.count()
  println(s"Training set size is $tsize")

  val alg = new LinearRegressionWithSGD().setIntercept(true)
  alg.optimizer
    .setNumIterations(numIterations)
    .setStepSize(alpha)
    .setUpdater(new SquaredL2Updater)
    .setRegParam(0.0)  //regularization - off

  val model = alg.run(scaledTrainData)

  println(s"Theta is $model.weights")

  val total1 = model.predict(scaler.transform(Vectors.dense(1650, 3)))

  println(s"Estimate the price of a 1650 sq-ft, 3 br house = $total1 dollars") //it should give ~ $289314.620338

  // Evaluate model on training examples and compute training error
  val valuesAndPreds = scaledTrainData.map { point =>
    val prediction = model.predict(point.features)
    (point.label, prediction)
  }
  val MSE = ((valuesAndPreds.map{case(v, p) => math.pow((v - p), 2)}.mean()) / 2)
  println("Training Mean Squared Error = " + MSE)

  // Save and load model
  val trySaveAndLoad = util.Try(model.save(sc, "myModelPath"))
    .flatMap { _ => util.Try(LinearRegressionModel.load(sc, "myModelPath")) }
    .getOrElse(-1)

  println(s"trySaveAndLoad result is $trySaveAndLoad")
}

STDOUT result is:

Training set size is 47
Theta is (weights=[52090.291641275864,19342.034885388926],
  intercept=181295.93717032953).weights
Estimate the price of a 1650 sq-ft, 3 br house = 153983.5541846754
  dollars
Training Mean Squared Error = 1.5876093757127676E10
trySaveAndLoad result is -1
Comment: It would be useful if you omit input parsing and provide example input. If you already have working model you can simply generate some points according to the parameters.
Comment: @zero323, I've asked coursera whether I have rights to put here any samples from coursera site.
Maciek
Answer: Well, after some digging I believe there is nothing here. First I saved content of the valuesAndPreds to text file:
valuesAndPreds.map{
   case {x, y} => s"$x,$y"}.repartition(1).saveAsTextFile("results.txt")'

Rest of the code is written in R.
First lets create a model using closed form solution:
# Load data
df <- read.csv('results.txt/ex1data2.txt', header=FALSE)
# Scale features
df[, 1:2] <- apply(df[, 1:2], 2, scale)
# Build linear model
model <- lm(V3 ~ ., df)

For reference:
> summary(model)

Call:
lm(formula = V3 ~ ., data = df)

Residuals:
    Min      1Q  Median      3Q     Max
-130582  -43636  -10829   43698  198147

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   340413       9637  35.323  < 2e-16 ***
V1            110631      11758   9.409 4.22e-12 ***
V2             -6650      11758  -0.566    0.575
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 66070 on 44 degrees of freedom
Multiple R-squared:  0.7329,    Adjusted R-squared:  0.7208
F-statistic: 60.38 on 2 and 44 DF,  p-value: 2.428e-13

Now prediction:
closedFormPrediction <- predict(model, df)
closedFormRMSE <- sqrt(mean((closedFormPrediction - df$V3)**2))
plot(
   closedFormPrediction, df$V3,
   ylab="Actual", xlab="Predicted",
   main=paste("Closed form, RMSE: ", round(closedFormRMSE, 3)))

'
Now we can compare above to SGD results:
sgd <- read.csv('results.txt/part-00000', header=FALSE)
sgdRMSE <- sqrt(mean(sgd$V2 - sgd$V1)**2)

plot(
   sgd$V2, sgd$V1, ylab="Actual",
   xlab="Predicted", main=paste("SGD, RMSE: ", round(sgdRMSE, 3)))

Finally lets compare both:
plot(
   sgd$V2, closedFormPrediction,
   xlab="SGD", ylab="Closed form", main="SGD vs Closed form")

So, result are clearly not perfect but nothing seems to be completely off here.
Comment: I've made the same excercise as you for Julia. That is what I've got: [link](http://1drv.ms/1GN1NKy). The predictions on Spark are different. OK, but how can we interpret it more precisely?
Comment: In addition, cost function (MSE) for 1500 iterations, alpha=0.01 is as follows:
Spark ->  s=1.5876093757127676E10;
Julia -> j=2.0432835766373606e9;
s > j -> true;
s/j = 7.769 (~8 times larger).
Comment: Continuing: iterations=18000, alhpa=0.003; Spark MSE -> s2 = 1.4826734808130558E10; s2/j = 7.256 (7.25 times larger than Julia). Don't you think Spark cannot find global optima, but instead of this it falls into local optima?
Comment: Since least squares problem means convex function a local minimum is not a problem. MSE  you report for Julia is simply not realistic. It would mean you can do better than exact analytical solution. Given a step pattern we see when compared Closed form and SGD solution I am pretty sure that problem is related to the second feature. I it is relatively strongly correlated to the first one (0.5599672) and it is not significant in a model obtained from closed form solution.
Comment: Another problem mentioned by [Till Rohrmann](http://stackoverflow.com/users/4815083/till-rohrmann) [here](http://stackoverflow.com/a/31610206/1560062) is that gradient descent  is sensitive when it comes to step size. Have you tried to use grid search to optimize parameters? From what I remember from Machine Learning class step was adjusted with iterations, am I correct?
Comment: Let me remind that ex1data.txt works also poorly (there is only one feature). If you say that cost function is convex for multiple features you probably are right. I'm not so sure right now:). The same cost as for Julia is for Octave code in ML practical part (bug in teaching materials?). For Spark I've tried even alpha=0.001, 0.002.
Comment: I've finished ML course in early 2013 so there could be some change in a materials. Could you provide coefficients from Julia/Octave?
Comment: iterations: 1500; alpha=0.01; octave_theta=[340412.563014;110540.566546;-6558.990548 ]; julia_theta=[340412.563014392,110540.56654627321,-6558.99054811402]


Custom taxonomy page that lists content

Question: In Drupal 7, I'm trying to create a page that shows all terms in a specific taxonomy, along with node content that uses the taxonomoy.
What I'm hoping to do is create what's essentially a grid of views, where each term in the taxonomy displays: an image associated with the taxonomy term (the image field was added to this particular taxonomy), and then below it a list of nodes tagged with that term, ideally with each list having its own mini pager (although a complete listing or a partial listing with a "more" link would also work if a pager is not achievable).
I'd also like to do this without Panels, since I'm not using it anywhere else on the site.
Can someone point me in the right direction as to how I should approach this?
Answer: The quickest way I know of is by using Views.
I would create a special "content type" with a special image field, a text field to describe the meaning of the Tag, and a Term Reference field to include the applicable tag later. Then I would enter one image + text for each existing tag and assign it to the tag itself. Later with the help of views I would customize display-views like the one you have mentioned which would take just a few minutes to do.
Comment: Ok, I can attach an image and a text field to a taxonomy term directly in Drupal 7 (by doing "Manage Fields" on the taxonomy itself). Unfortunately, I don't know where to go from there - when creating a view I can return taxonomy terms (where I can display the image and text, but not nodes with a particular tag), or I can display nodes with a particular tag, but then can't pull the image/text associated with the tag. Even then, I don't know how to display multiple views of that type on one page, even if I *could* make the appropriate view.
Comment: Learning to use Views is an extensive subject and it takes more than a paragraph to explain. I suggest to get a book about it and follow it from begin to end in order to grasp the whole concept. The one I'm most familiar with and I highly recommend is "Drupal 7 Essential Training" by Tom Geller which is a video course from Lynda.com

Custom taxonomy page that lists content


How do we prove that the probability remains the same?

Question:
From an urn containing $M$ white balls and $N-M$ black balls, $r$ balls have been lost. The probability of drawing a white ball is: Given ($N\gt M\gt r)$.

My Thinking: We know that before the balls have been lost $P$(white)=$\frac{M}{N}$.
Now when $r=1$, $$P_{white}=(\frac{M}{N})(\frac{M-1}{N-1})+(\frac{N-M}{N})(\frac{M}{N-1})=\frac{M}{N}$$ but now for $r=2$ ($3$ possibilities), the situation gets bit complicated and further for $r=3$ gets further tough. So I want to derive(I think it will involve binomial maybe) a general term for this probability and also to prove that the probability always remains i.e,  $P$(white)=$\frac{M}{N}$ independent of the balls drawn. How can we prove this?
Answer: The probability that a particular while ball is drawn is the probability that it was not lost, times the conditional probability that it is drawn, given that it wasn't lost.
The probability that it wasn't lost is $$\frac{N-r}{N}$$ and the conditional probability that is drawn, given that it wasn't lost is $$\frac1{N-r}$$ so that probability that it is drawn is $$\frac{N-r}{N}\cdot\frac1{N-r}=\frac1N$$
Summing over all white balls, we find the probability that a white ball is drawn is $$\frac MN$$
Of course, I'm assuming that each ball has an equal probability of being lost, and that all relevant events are independent.
Comment: can you please explain how $\frac{N-r}{N}$ for white?
Comment: @Community There are $N-r$ balls that aren't lost.  If each ball has the same probability of not being lost, that probability must be $\frac{N-r}{N}$
Comment: But the $N-r$ balls will include both white and black, shouldn't it be $\frac{x}{n-r}$ where $x$ is number of white balls remaining
Comment: No.  We are computing the probability that any given ball is drawn.  that is $\\frac1N$ no matter what the color.  Then we sum over all the white balls.
Answer: Assuming we have w white balls and b black balls, define the following event:
$A_{w,b}^{r}$:= after losing r balls from an urn containing w white and b black balls, we choose a ball at random and it is white.
Now by induction on $r$, assuming that for all $r'<r, \mathcal{P}(A_{w,b}^{r'})=\frac{w}{w+b}$, we write:
$\begin{equation} \begin{split}
\mathcal{P}(A_{w,b}^{r})&=
\mathcal{P}(A_{w,b}^{r}|\text{first lost ball was white})\cdot\mathcal{P}(\text{first lost ball was white}) + \mathcal{P}(A_{w,b}^{r}|\text{first lost ball was black})\cdot\mathcal{P}(\text{first
lost ball was black})\\&=\mathcal{P}(A^{r-1}_{w-1,b})
\cdot\frac{w}{w+b} + \mathcal{P}(A^{r-1}_{w,b-1})
\cdot\frac{w}{w+b}\\&=\frac{w-1}{w+b-1}\cdot\frac{w}{w+b}+\frac{w}{w+b-1}\frac{b}{w+b}\\&=\frac{w\cdot(w-1+b)}{(w+b-1)\cdot(w+b)}\\&=\frac{w}{w+b} \end{split}\end{equation}$
Of course the base of induction is true for r=1.
Answer: First note that $\binom{n}{k}(n-k)=n\binom{n-1}{k}$, which we use twice below. (This binomial coefficient identity can be proved combinatorially by counting, from among $n$ people, committees of size $k+1$ that include a chairperson.  The LHS chooses everybody but the chair and then chooses the chair from the remaining $n-k$ people.  The RHS chooses the chair and then chooses the remaining $k$ members.)
By conditioning on the number $w$ of white balls lost, we obtain that the probability of white is
$$\sum_{w=0}^r \frac{\binom{M}{w}\binom{N-M}{r-w}}{\binom{N}{r}}\cdot \frac{M-w}{N-r}
=\frac{M}{N}\sum_{w=0}^r \frac{\binom{M-1}{w}\binom{N-M}{r-w}}{\binom{N-1}{r}}
=\frac{M}{N},$$
where the last equality follows from the hypergeometric distribution or via Vandermonde's identity.
Answer: I regard this is an intuition question rather than a math question.
Consider the following two scenarios:

Scenario 1: First you choose a ball at random.  Then, from the remaining $(N-1)$ balls, you set $(r)$ balls aside.

Scenario 2: First, you set $(r)$ balls aside, chosen at random.  Then, from the remaining $(N-r)$ balls, you choose one at random.

Clearly, in scenario 1, the chance that the (first) ball chosen is white is unaffected by your having subsequently set aside $(r)$ balls.  Therefore, the challenge is to demonstrate, by analysis, that the chance of a white ball chosen in scenario 2 is identical to the chance of a white ball chosen in scenario 1.
I am assuming that the $(r)$ balls set aside are always distinct from the single ball chosen to see if the single ball is white or black.
Vary scenario 2 into scenario 3:
Instead of choosing $(r)$ balls at random, you choose $(r + 1)$ balls at random.  Then, from the $(r + 1)$ balls, you randomly select one and examine whether that ball is white or black.
Is this equivalent to scenario 2?
In scenario 2, you first chose $(r)$ balls and set them aside.  Then, you chose a ball at random from the remaining $(N-r)$ balls.  Before any selections were made, each of the $(N)$ balls was just as likely to be white as any other ball.  Therefore, choosing a ball at random from the $(r+1)$ balls is just as likely to produce a white ball as choosing a ball at random from the $(N-r)$ balls.  That is, since the $(r)$ balls being set aside are chosen at random, none of them is more or less likely to be white than any of the $(N-r)$ balls that remain.
This means that the chance of a white ball is the same, in scenario 3 as scenario 2.
What about scenario 3 versus scenario 1?
In scenario 3, you first choose $(r + 1)$ balls, and then choose one at random from them.  As discussed, since the $(r + 1)$ balls are chosen at random, the chance of any one of them being white is the same as the chance of any of the $(N)$ balls being white.  Therefore, with respect to the chance of choosing a white ball, scenario 3 is equivalent to scenario 1.


How to view in-memory graph in Neo4j?

Question: Is it possible to call an in-memory graph (projected graph) in a way that returns a graph (not only Table, Text & Code) in Neo4j database?
Answer: Currently not possible.
A feature request phase.
https://community.neo4j.com/t/visualizing-in-memory-graph/44436

How to view in-memory graph in Neo4j?


What does "uniquely determined" mean in a particular statement?

Question:
The objects of a category are in bijective correspondence with the
identity morphisms, which are uniquely determined by the property
that they serve as two-sided identities for composition. Thus, one can
define a category to be a collection of morphisms with a
partially-defined composition operation that has certain special
morphisms, which are used to recognize composable pairs and which serve as two-sided identities.

I know that such expression usually means that the determined object is a function of the other, but I feel that here, there is a logical meaning that I'm ignoring. Because of it, I can't see how this fact implies that such an alternative definition of a category is possible.
Comment: It means that if two morphisms both have this property, then they are equal. Hence, the property "uniquely determines" the morphism.
Comment: @Thorgott In that sense, can I say that the category axioms don't distinguish between two morphisms with this property?
Comment: In a way, yes, but perhaps that's a suboptimal way of thinking about it. To say "they don't distinguish" implies that there are things that could be distinguished in some other ways and the category axioms just don't choose to do so, but it's just the case that there literally is nothing to distinguish.
Answer: I would take it to mean that for each object, the identity morphism is the only morphism that has that property.
I.e. if for every morphism $f$ from an object to itself, one has $\iota \circ f = f = f\circ\iota$ and $\varepsilon\circ f=f = f\circ\varepsilon,$ then $\iota=\varepsilon.$
Comment: @JohnMars : A partially defined function from a set $A$ to a set $B$ is a function whose domain is some subset of $A$ (just _which_ subset may be a hard problem, and I think that's a big part of the reason why this concept exists) and whose image is a subset of $B.$ To say in this context that composition of morphisms is partially defined would mean there are some morphisms $f,g$ for which the composition $f\circ g$ is defined and some for which it is not. If $g$ is a morphism from $A$ to $B$ and $f$ is a morphism from $C$ to $D$ and $B\ne C,$ then $f\circ g$ is not defined.
Comment: $\quad\uparrow\quad$ This is like the idea of the matrix product $AB$ being defined only if the number of columns of $A$ equals the number of rows of $B. \qquad$
Comment: I see. On the other hand, what do you understand by partially-defined composition?
Comment: It looks to me as if the author of that passage is attempting to talk about categories without beginning by saying there are objects and morphisms, but rather by saying only that there are morphisms and then defining objects based on properties of morphisms.


Cannot connect to mysql database on my website

Question: I have a website that I should put on my domain.I have encountered an issue when I tried connecting to MySQL database so my code is not working. When I put
<code>define("DB_HOST","http://domain");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","dbName");

$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(!$connection){
    die();
}
mysqli_set_charset($connection,"utf8");
</code>
I get this error in log

Failed to connect to MySQL: Plugin http could not be loaded: /usr/lib64/mysql/plugin/http.so: cannot open shared object file: No such file or directory

otherwise, I tried to remove that HTTP and just use the domain as DB_HOST, but then I get error
<code> define("DB_HOST","mydomain.com");
    define("DB_USER","user");
    define("DB_PASS","password");
    define("DB_NAME","dbName");

    $connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    if(!$connection){
        die();
    }
    mysqli_set_charset($connection,"utf8");
</code>

PHP Warning:  mysqli_connect(): (HY000/2002): Can't connect to MySQL server on 'domain.com' (115) in /home3/domain/public_html/a/i.php on line 7

Can anyone please help me find the solution to make this work?
Comment: `http://domain` is wrong, and `domain.com` is wrong if the mysql server is not open to the internet and the user is allowed to connect from any host. If you're on shared hosting enter the details provided by your host. If your working on one server like a VPS and mysql is installed locally use localhost or 127.0.0.1 for the domain. You need to provide more details about the setup.
Answer: Here's how I define a MYSQLI connection;
<code> $con = mysqli_connect("localhost","username","password","database");
</code>
When you want to run an operation you just invoke the $con variable.
If you have issues check the permissions for the MYSQL user, they should at the very least include;
delete
insert
select
show view
update


Is this an abuse of OOP and Java?

Question: Let's say you have this class:
import java.util.ArrayList;

public class Array
{
  private ArrayList<Object> a = new ArrayList<Object>();

  public void add(Object element)
  {
    a.add(element);
  }

  public void addAll(Object elements[])
  {
    for (int i = 0; i < elements.length; ++i)
      a.add(elements[i]); // this line is going to be changed
  }
}

And this derived class:
public class ArrayCount extends Array
{
  private int count = 0;

  @Override
  public void add(Object element)
  {
    super.add(element);
    ++count;
  }

  @Override
  public void addAll(Object elements[])
  {
    super.addAll(elements);
    count += elements.length;
  }
}

If in the base class, we make the following change:
  public void addAll(Object elements[])
  {
    for (int i = 0; i < elements.length; ++i)
      add(elements[i]); // this line was changed
  }

I would expect the add() method in the base class to be called, not the overridden add() in the derived class. Why is this not the case? And is this example an abuse of OOP?
Comment: Abuse?  That is polymorphism.
Comment: Although `Array` is a terrible name for a user class: https://docs.oracle.com/javase/9/docs/api/java/lang/reflect/Array.html
Comment: That would be polymorphism, not abuse :)
Comment: If you want to explicitly call a method from the base class, use `super.add(element)`
Comment: @Philipp the issue about calling `super.add(element)` is that the method that was changed was in the parent class. Therefore, there might not be a `super.add(element)` to call.
Answer: Java will always call the method implementation of the runtime class (the real object class at runtime), as long as it's a valid method override. That's called dynamic binding.
You are not abusing, but exploiting a guaranteed feature.
What you have as design is legal in Java, but is a bad practice for many reasons, of which you highlighted an important one. This is one of the primary reasons why implementation inheritance is sometimes discouraged, in favor of interface inheritance.
Comment: related: https://softwareengineering.stackexchange.com/questions/315344/is-overriding-concrete-methods-a-code-smell
Answer: At the end of the day, it boils down to what happens at runtime.
Since the function is add(), it'll invoke the add() for this object which is the overridden function during runtime.
This is the whole point of encapsulation, a child class should have total control over what its methods do.
Answer:
I would expect the add() method in the base class to be called, not the overridden add() in the derived class. Why is this not the case?

Java uses dynamic dispatch, also known as dynamic binding, to bind methods at runtime. In simple terms, this means that when a method is called on a base class, the method that's executed is that of the actual type of the object. See the Wikipedia article or the Java reference on polymorphism.

Is this example an abuse of OOP?

For a contrived example that demonstrates dynamic dispatch, it's not bad. For anything else, it's pointless.
Answer: I think the following change fixes the problem:

If in the base class, we make the following change:

  public void addAll(Object elements[])
  {
    for (int i = 0; i < elements.length; ++i)
      super.add(elements[i]);     // <-- new use of SUPER
  }

Basically, if you want certain methods to be invariant (always do the same thing) you have to declare them final to prevent overriding.  Otherwise, a derived class can implement anything they want.
(The fix to that is to write documentation explaining what the contract is that any derived method must follow.)

Is this an abuse of OOP and Java?


How to save a complex object in Entity Framework 6

Question: I'm writing unit tests for a C# / SQL Server application. MS recommends using SQLite when not using a production db for testing (https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database#inmemory-provider). I have SQLite setup and I'm trying to fill the db with test data. I exported a sample of production data into JSON format and saving that with the test project so I can update the test data fairly easily. The code to setup the SQLite context I took from the MS article.
There's no line numbers to ref but this block
var memoryData = new ReqestDataInMemory();
var data = memoryData.AllRepositoryRequests();

This gets the json and returns the complex object structure as an IEnumerable. I then try and save it to the DbContext:
public class TestDbContext
{
    public SqliteConnection _connection {get; set;}
    public DbContextOptions<TaxonomyContext> _contextOptions { get; set;}

    public TestDbContext()
    {
        // Create and open a connection. This creates the SQLite in-memory database, which will persist until the connection is closed
        // at the end of the test (see Dispose below).
        _connection = new SqliteConnection("Filename=:memory:");
        _connection.Open();

        // These options will be used by the context instances in this test suite, including the connection opened above.
        _contextOptions = new DbContextOptionsBuilder<TaxonomyContext>()
            .UseSqlite(_connection)
            .Options;

        // Create the schema and seed some data
        using var context = new TaxonomyContext(_contextOptions);

        if (context.Database.EnsureCreated())
        {
            using var viewCommand = context.Database.GetDbConnection().CreateCommand();
            viewCommand.CommandText = @"
                CREATE VIEW AllRequests AS
                SELECT Url
                FROM Request";
            viewCommand.ExecuteNonQuery();
        }

        var memoryData = new ReqestDataInMemory();
        var data = memoryData.AllRepositoryRequests();

        context.AddRange(data);
        context.SaveChanges();
    }
}

The AddRange() throws an error

System.InvalidOperationException : The instance of entity type 'RequestType' cannot be tracked because another instance with the same key value for {'RequestTypeId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

which makes sense if it's trying to save the lookup table over and over. Hence my problem.
Here is the object I'm working with. RequestType is where I'm running into a blocker and its a simple lookup table RequestType.Id and RequestType.Name. Status is a one-to-many-lookup table. All have FK relationships back to Request
The ChangeDetail object is details about the request. It has unique IDs and a FK to the Request.
I tried NULLing both RequestType and Statuses but it s
public class Request
{
    public int RequestId { get; set; }
    /// changed from "navarchar(max)" to "text" for sqlite testing db
    [Required]
    [MinLength(50)]
    [Column(TypeName = "text")]
    public string Justification { get; set; } = string.Empty;
    [Required]
    public int SubmitUser { get; set; }
    [Required]
    public DateTime SubmitDate { get; set; }
    public int? ModifyUser { get; set; }
    public DateTime? ModifyDate { get; set; }
    public virtual IEnumerable<ChangeDetail> ChangeDetail { get; set; } = new List<ChangeDetail>();
    public virtual IEnumerable<Status> Statuses { get; set; } = new List<Status>();
    public virtual RequestType RequestType { get; set; } = new RequestType();
}

My queries work great so I think all my relationships are solid.  Just in case it helps, here are the relationships.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Request>()
            .HasOne(x => x.RequestType)
            .WithMany(x => x.Requests);

    modelBuilder.Entity<Request>()
            .HasMany(x => x.ChangeDetail)
            .WithOne(x => x.Request);

    modelBuilder.Entity<Request>()
            .HasMany(x => x.Statuses)
            .WithMany(x => x.Requests);

    modelBuilder.Entity<Status>()
            .HasOne(x => x.RequestStatusType)
            .WithMany(x => x.Statuses);
}

To solve the problem I tried this:
LINQ query to return distinct field values from list of objects
I thought this showed promise, using Entity.Unmodified but couldn't get it to work
Save detached entity in Entity Framework 6
This talks about the FK, which I have setup but maybe its not referencing them on save?
How do I stop Entity Framework from trying to save/insert child objects?
I appreciate any suggestions you may have.
Comment: Problem in method that you have hidden. `AllRepositoryRequests ` generates duplicate objects `RequestType` which have the same Id.
Comment: yes, that is correct.  In Request Type is repeated because its a lookup in the prod db.  If I try and "seed" the in-memory db with the lookup values, it still tries to save the Request Type object which I'm trying to stop.
Comment: Do not create entity with the same id. Create one and reuse in other references. Or if it is already in database, mark it as `Unchanged` in ChangeTracker. But it still should be one instance.
Answer: I thought I would close the loop on this.  It took me a while but it was a data issue in one of the joining tables for the many-to-many relationships.  Once I fix


Counting permutation problem

Question:
Suppose you have $n$ boxes, each with a ball inside. If you randomly change the place of the balls such that afterwards, there is still a single ball in each box, what is the probability that exactly $k$ balls remain in its original box?

Attempt:
I know that there are $n!$ different possible scenarios. Let $X$ the random variable defined by the number of balls that remain in its original box, its obvious that (by Laplace probability),
$P(X=n)=\frac{1}{n!}$, and,
$P(X=n-1)=0$,
$P(X=k)=0$ if $k\notin \{0,1,\ldots, n\}$
However, I don't know any discrete distribution with this form and it's so hard to deduce the value of $P(X=k)$ for an arbitrary $k$. $P(X=0)$ looks so hard also.
Comment: Choose the $k$ balls that are to be fixed in $\binom{n}{k}$ ways. The remaining balls must all be in the wrong boxes. Such an arrangement is called a derangement, and they can be counted exactly. http://en.wikipedia.org/wiki/Derangement#Counting_derangements
Comment: One way to approach this would be to consider how many ways  the other $(n - k)$ balls can be rearranged while $k$ balls remain in their original places.  (I'm assuming that all of the balls are "dumped out" of their boxes and then returned randomly.)
Comment: @RecklessReckoner yes, but, in this case we have less than $(n-k)!$ favorable cases for eack posible choice of $k$ balls. Since we need to substract for $(n-k)!$ the cases when one of the $n-k$ balls remain in their original boxes since we want only $k$ balls in its boxes.

Sounds too hard.
Answer: Expanding on the answer of Austin Mohr:
There is a classical problem of counting derangement. A permutation $\pi$ of $\{1,2,\dots,n\}$ is called a derangement if it moves all of the elements: $\pi(i) \neq i$ for all $i$. You can find the exact number of derangement, and it comes out as:
 $$n!(1 - 1 + 1/2 - 1/6 + \dots \pm 1/n!) = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}$$
This is the same as if you were computing $n! e^{-1}$, only truncated the series for $e^{-1}$ at $n$-th place, so roughly $1/e$ permutations are derangement.
If you want to figure out this solution yourself, use inclusion/exclusion. Let $A_i := \{\pi \in S_n \ : \ \pi(i) \neq i \}$; then the set of derangement$\bigcap_{i=1}^n A_i$, and for $I \subset \{1,\dots,n\}$ with $|I| = m$ you have $\bigcap_{i \in I} A_i^c = (n-m)!$, so inclusion/exclusion gives:
$$ | \bigcap_{i=1}^n A_i| = \sum_{m} (-1)^m \sum_{|I| = m} |\bigcap_{i \in I} A_i^c | = \sum_m (-1)^m {n \choose m} (n-m)!$$
Now, for exactly $k$ balls to land in their boxes, you first need to choose these $k$ (this is done in ${n \choose k}$ way), and then you need to rearrange the rest (this is precisely the derangement of $n-k$ elements). Hence, the number of ways for exactly $k$ balls to remain fixed is:
$${n \choose k}(n-k)! \sum_{l=0}^{n-k}\frac{(-1)^l}{l!} $$
The total number of permutations is, of course, $n!$, so the probability is:
$$\frac{1}{n!}{n \choose k}(n-k)! \sum_{l=0}^{n-k}\frac{(-1)^l}{l!}
= \frac{1}{k!}\sum_{l=0}^{n-k}\frac{(-1)^l}{l!} \simeq \frac{1}{e k!}
$$
Comment: Great explained, thanks.
Comment: @GastónBurrull: You're welcome!

Counting permutation problem


WPF DataGrid binding to navigation property

Question: I'm using Entity Framework 6 in Visual Studio 2015 to create a WPF app with a DataGrid. It needs to bind to search results that contain Entity Framework navigation properties 3 levels deep: An Employee can have any number of EmployeeStatus, and an EmployeeStatus can have an EmployeeStatusDescription entity. So it looks like this:

Employee > EmployeeStatus > EmployeeStatusDescription

I have the following Include()
var comparison = Expression.Lambda<Func<Employee, bool>>(
    Expression.Equal(
        Expression.Property(paramEmployee, selectedColumnValue),
        Expression.Constant(SearchValue)),
        paramEmployee).Compile();

var query = (from e in Context.Employees
             .Include("EmployeeStatus.EmployeeStatusDescription")
             .Where(comparison)
             select e);

I try to bind the EmployeeStatusDescription like this:
<DataGrid ItemsSource="{Binding SearchResults}">

    <DataGridTextColumn Binding="{Binding
      EmployeeStatus.EmployeeStatusDescription.description}" Header="Status" />
</DataGrid>

But Status column comes up blank for an Employee that has an EmployeeStatus and an associated navigation property EmployeeStatusDescription.
However, on the database, the following SQL brings back the EmployeeStatusDescription just fine:
select esd.* from employee e
    left join EmployeeStatus es on e.employeeID = es.employeeID
    left join EmployeeStatusDescription esd on
       es.employeeStatusDescriptionID = esd.employeeStatusDescriptionID
where e.employeeID = '30299'

What am I doing wrong? Thanks for your help.
Update 1: EmployeeStatus is defined in my Employee model like this, so it's a collection, not just a single value:
public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }

Update 2: Here's the EmployeeStatusDescription class:
public partial class EmployeeStatusDescription
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public EmployeeStatusDescription()
    {
        this.EmployeeStatus = new HashSet<EmployeeStatu>();
    }

    public int employeeStatusDescriptionID { get; set; }
    public string employeeStatusAbbreviation { get; set; }
    public string description { get; set; }
    public bool isActive { get; set; }
    public System.DateTime createdDate { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
}

Comment: Is the EmployeeStatusDescription property on the EmployeeStatus object just called EmployeeStatusDescription or something else?
Answer: If EmployeeStatus is a collection, you won't be able to just say EmployeeStatus.EmployeeStatusDescription.description.  .NET doesn't know which one to spit back.  You could create an IValueConverter that takes in a collection and returns the first.
Or, you could create a property on your viewmodel that returns the first item from the EmployeeStatus collection
    public EmployeeStatus FirstStatus{
       get { if (EmployeeStatus != null) { return EmployeeStatus.First(); } return null; }
    }

Or you could add a content control that would be bound to all the items in the EmployeeStatus collection and list all of them.
Edit
To show all the values inside the datagrid for the employee, you'll need another control to iterate the collection.  Inside the column that would show the EmployeeStatus, you'll need to make it a template column and add a control that can do the iteration:
<DataGridTemplateColumn Header="Employee Status">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <ListBox ItemsSource="{Binding Source=EmployeeStatus}">
                   <ListBox.ItemTemplate>
                       <DataTemplate>
                           <TextBlock Text="{Binding EmployeeStatusDescription.description}" />
                      </DataTemplate>
                   </ListBox.ItemTemplate>
                </ListBox>
          </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Items Control Binding
<DataGridTemplateColumn Header="Employee Status">
         <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                    <ItemsControl ItemsSource="{Binding EmployeeStatus}">
                         <ItemsControl.ItemTemplate>
                             <DataTemplate>
                                  <TextBlock Text="{Binding EmployeeStatusDescription.description}" />
                             </DataTemplate>
                         </ItemsControl.ItemTemplate>
                     </ItemsControl>
                <DataTemplate>
           <DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

Comment: Thanks, @Josh. How would I return the `EmployeeStatus` collection and its associated `EmployeeStatusDescription`s for each employee in my query?
Comment: Thanks, @Josh. It's returning a bunch of blank ListBox elements in the Status column. Also, Intellisense doesn't recognize `Source=EmployeeStatus` inside the ListBox control, nor does it recognize `EmployeeStatusDescription.description`
Comment: Intellisense won't pick it up at design time.  Try changing EmployeeStatus to ObservableCollection.  Post your EmployeeStatusDescription class.
Comment: Please see above, @Josh.
Comment: Thanks, @Josh. Got it working! I had to remove `Source=` from ``. Don't ask me why. I've had this happen before.


Probability of selecting a correct answer given that there is a chance the student had seen the exam question

Question: Suppose a student sits for an exam and in a question, there are 5 choices where one is correct. There is also a 1/4 chance that the student may have seen the answer to the question previously. If the student randomly selects an answer for the question if he hadn't seen the answer and selects the correct one if he had seen the answer, what is the probability that the student selects the correct answer? What I think is that this is a conditional probability whereby. the probability of selecting the correct answer is the probability of selecting the correct answer given he had seen the answer or probability of selecting the correct one given he hadn't seen the answer. However, adding the two probabilities gives me a value greater than one. This is because, to me, the probability that he selects the correct answer given he had seen the answer is 1. In what other way can I approach this question?
Comment: https://en.wikipedia.org/wiki/Law_of_total_probability
Comment: Ben, the example in the link given by @Mark describe a situation that is almost identical to yours.
Comment: You are on the right track when you think you need to add the probabilities from the two configurations, but you have the wrong probability on one of the branches of your scenario, fixing this branch's probability should give you the right answer.
Comment: Do yo understand the Law of Total Probability? If not, learn it, and use it.
Comment: Thank you Antoine. What branch might be wrong? Am trying to figure it out and am getting back to what I have already
Answer: For any given question, you can split it into two situations: the student has seen the question and the student has not.
\begin{align*}
P(\mbox{correct})&=P(\mbox{correct}|\mbox{seen})P(\mbox{seen})+P(\mbox{correct}|\mbox{not seen})P(\mbox{not seen})\\
&=1\cdot (1/4)+(1/5)(3/4)\\
&=2/5.
\end{align*}


Problem with an integral and integration by part

Question: I have a little problem with this question.. :
We have :
$$ G(t) = \int_0^t  f(e^t)e^{-t} \, dt $$
With f continue in the domain [0,$+\infty$].
and the question is :
Prove that
$$\int_0^t G(u) \,du = t\int_0^t e^{-u}f(e^u)  \,du  \, - \int_0^t ue^{-u}f(e^u)  \,du$$
Thank you for yours futur answer
Comment: Did you mean $G(t)=\int_0^tf(e^u)e^{-u}du$?
Answer: Note that
$$ G'(t) = f(e^t)e^{-t}, $$
so that integration by parts gives
\begin{align}
t\int_0^t f(e^u)e^{-u} \, du - &\int_0^t uf(e^u)e^{-u} \, du = \int_0^t (t-u)G'(u) \, du\\ &= [(t-u)G(u)]_{u=0}^t - \int_0^t (-1) G(u)\,du\\
&= \int_0^t G(u) \, du.
\end{align}
Answer: Set $H(t)=\int_0^tG(u)\,du$ then $H'(t)=G(t)$ and $H''(t)=f(e^t)e^{-t}$. Now the linear Taylor formula with integral remainder term tells us that
$$
H(t)=H(0)+H'(0)t+\int_0^t(t-u)H''(u)\,du,
$$
which directly leads to the claimed formula.
Comment: Nice one!${}{}$


How do I get the menu ID based on the menu title?

Question: I am looking for a function to call like in the following snippet:
get_menu_id("my menu title");

Do you have any tips?
Comment: Found a nice snippet here http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal#comment-23
Answer: In short, you can't.
The title you see on the page and the title in the database are the same only in the case you are running an English-only website and the title_callback is t().  If you are using Drupal 7, you aren't overriding string in settings.php.
Otherwise, the title is generated dynamically and it's literally impossible to find its value in the database.
Answer: I guess you could loop through the results of menu_get_names(), as in:
function get_menu_id($menu_title) {
    $menu_names = menu_get_names();
    foreach ($menu_names as $menu_name) {
        $menu = menu_load($menu_name);

        if ($menu['title'] == $menu_title) {
            return $menu_name;
        }
    }
}


Plotting the regions where $\log_{x}|y-2|-\log_{|y-2|}x>0$

Question:
Draw the set: $ S=\{(x,y): \log_{x}|y-2|-\log_{|y-2|}x>0\} $

We know that $x>0$ (base of the logarithm). Also, $$\log_{|y-2|}x=\frac{1}{\log_{x}|y-2|},$$ so we have $$\log_{x}|y-2| - \frac{1}{\log_{x}|y-2|}>0$$ and so $$((\log_{x}|y-2|)+1)((\log_{x}|y-2|)-1)>0\;.$$
What should I do next, though? $(\log_{x}|y-2|)+1>0$ or $(\log_{x}|y-2|)-1>0$?
Comment: if $x-\frac{1}{x}>0$, is it really true that $(x+1)(x-1)=x^2-1>0$? What happens if $x<0$?
Answer: Hint: First $$((\log_{x}|y-2|)+1)((\log_{x}|y-2|)-1)(\log_{x}|y-2|)>0 .$$ Consider the regions where each of these factors is positive and negative.
Comment: @Didier: Multiply both sides of $\log_{x}|y-2|-\log_{|y-2|}x>0$ by $\left(\log_{x}|y-2|\right)^2$ to get the inequality I gave. Then all one needs to do is to look at the level curves of $\log_{x}|y-2|$ for $\{-1,0,+1\}$.
Comment: Hmmm... clever. Maybe such a hint is more destined to the answerer and their kin than to the asker.
Comment: @Didier: The asker erroneously got to $((\log_{x}|y-2|)+1)((\log_{x}|y-2|)-1)>0$, and I commented that that ignored the sign of $\log_{x}|y-2|$. I thought that perhaps they could put that together with my hint to get an answer. The question seemed like a homework problem, and, in any case, the asker was asking for help, not an answer, so I didn't want to give too much.
Comment: Sorry but I understand neither why this inequality should hold nor how it would help.
Answer: I'd start by letting $u=y-2$, and asking when $\log_x |u|-\log_{|u|}x > 0$. Now convert the logs to the same base: $\log_a b = \frac{\ln b}{\ln a}$, so your inequality is $$\frac{\ln |u|}{\ln x} - \frac{\ln x}{\ln |u|} > 0\;.$$ Be a little careful in solving this: $\ln x$ and $\ln |u|$ can be negative.
Answer: You can start by drawing the important contour lines of $f(x,y) = \log_x |y-2| - \log_{|y-2|} x$ in the plane.
There are several lines where $f$ has a discontinuity, namely $x=0$, $y=2$, $y=1$, and $y=3$. Do you see why?
Next, you can solve for the curves where $f(x,y)=0$. Use Didier's hint. You should get four different curves.
Once you have these lines and curves, plot them in the plane. Inside each region $f$ is either positive or negative; you can determine which one by testing a point.


Sustain Pedal connecting Slurs

Question: In this simplified Chopin's Prelude Op. 28 No. 20, the sustain pedal connects notes belonging to two separate slurs in the first and second measures.
Should the sustain pedal be left off for a moment before the following slur to "separate" the two phrases instead?

EDIT: Highlighted in the picture the part of the pedal section interested by the question.
Answer: Ultimately, this is a musical decision, so either way is fine. However,...
If you want to play the music literally as written, then you would hold the pedal through, so that there's no break between the two slurred passages.
My personal preference, both in this simplified arrangement as well as the original, is to leave a small break — a "breath" — to separate the two.

The answers by LaurencePayne and JohnChernoff prompted a look at some various editions (IMSLP). The first edition, complete works, Mikuli, and Debussy editions give no pedal indications; however, the Cortot edition gives the same pedaling as in the OP arrangement: pedal across the two slurred segments.
Comment: @Tim The question and answer just concern pedaling between the final chord or the first bar and the first chord of the second. The remaining pedal indications — up/down for each chord — are played as written.
Comment: I don't see it that way - the pedal markings show up and down for each chord - not for each bar, and with different harmonies, holding over two bars can't be good. Am I missing something?
Answer: The usual way is to hold the pedal down to sustain one chord, then play the next, and after that next has been played, change pedal. Rather like the sign shows - the 'pedal up/down' comes directly after the next chord is played, not before or as it's played. Slowing it all down a lot will allow understanding of the actual timing - it's an aquired art !
By cancelling that pedal before playing the next chord, there will be a slight gap in the sound.Which really isn't what this kind of pedalling is about.
Holding the pedal down for a whole bar here is not a good idea - the different harmonies will bleed into each other, making everything sound muddy and confused. Hence the pedal sign for each chord.
Half pedalling would be an option, but here, with 'ff' shown, extra volume would be afforded by using full pedalling.
EDIT: - just realised OP thinks the pedal actually stays down for the whole phrase! Then changes and is down for all of the next phrase. No! That's NOT what the (simplified?) dots say.They say, as earlier in my answer, to re-pedal every chord. Note the arrow heads in the pedal markings.
Comment: I don't think the OP thinks the pedal actually stays down for the whole phrase, not even in the unedited version. I just think the OP has noticed that the pedalling notation is exactly the same between the slurs as within them.
Comment: @Qvantvm - now, I'm considering deleting this! Since the pedal mark for that particular bit is exactly the same as those previous (and after), continuing exactly the same seems like a good move, but now that I see the point of the question, since there are two distinct phrases (slurs), then yes, a very slight 'breath' would designate the end of one, and the beginning of the next.
Comment: @Tim To clarify the question is specifically related to connecting the 2 slurs, so in this case how the pedal is carried from the first to the second bar. NOT how to pedal within the slur or how to pedal in general.
Comment: Thanks @Tim, for answering. I think it is fine if you want to delete. But may I suggest updating a new answer? I am interested in your experience and perhaps to find a general "rule".
Answer: Up-down as each chord is played. A subtle lift at the end of each phrase is not a ridiculous idea, but I don't think it's needed here.
Rubinstein legato-pedals through. So do all the other YouTube versions I just looked at.  That's good enough for me.
Answer: Deliberately changing pedal before a new phrase can sometimes result in something not entirely dissimilar to when a choir collectively breathes before a barline - e.g., it can easily create a somewhat stilted/mechanical effect.  I'd guess the written pedal marks are an attempt (presumably by an editor and not Chopin) to avoid that to some degree, though my own experience has been that following any non-overarching (i.e., several per measure) pedal markings too slavishly can easily cause that sort of thing regardless of intent.
My suggestion is therefore to note the reason for the marking and follow it as is natural for your own performance/interpretation.

Sustain Pedal connecting Slurs


So, what does this tenuto mark mean in Bernstein's piano music?

Question: This is a bar from the fifth of Bernstein's Five Anniversaries, "For Susanna Kyle".
It's a nice and easy tune that sounds vaguely like what Satie would have composed in his youth were he not wasted all the time.

I am wondering what I am to do with those tenuto markings.
I would assume that I'd have to let the chords "clash" against each other for a bit, but I'm finding it very weird, and also kind of difficult (not impossible) to do without using the pedal, which, however, would apply to the bass line as well - which I don't think I am allowed to do (besides sounding not too good).
I have read and heard that tenuto in context might mean "slight rubato", but it also says (I think) "rit" for "ritmato" (EDIT: no it doesn't, stupid me), which is kind of at odds with the idea of rubato.
So, what am I to do with these?
Comment: Tenuto [can mean ...   *play the note slightly louder*](http://en.wikipedia.org/wiki/Tenuto).
Comment: I'm voting to close this question as off-topic because it originates from my utter stupidity when at some point "ritenuto" mixed with "rubato" in my head while reading a piece and doesn't add much to the site.
Comment: Think how you would normally play a phrase of three slurred notes. The first with a slight accent, then a diminuendo to a slightly shortened final note? The tenuto marks are telling you NOT to do this, but to give each note equal and ample weight.
Answer: Rit is probably ritenuto, an immediate slowing down, as opposed to rubato, and tenuto means hang on for full note value - or even a touch longer: which makes sense as one will make the other happen.So, the whole bar should come at a slightly slower pace than the preceding bars.
On the assumption it's written in C at that point - I'm guessing - you can play the first pair of notes, change finger on the lower one, to get to the middle pair smoothly. Then do the same trick again, obviating pedal usage.
Comment: Thank you, the meaning is much clearer now. Didn't think of "ritenuto", thank you. I am not sure what fingering you are suggesting, though - at some point I run out of fingers (consider that I can land on that preceding E with either 3-4-5, where 3 feels more comfortable)
Comment: Re-thinking the fingering - 2+5 on E and C, 1 and 4 on D and Bb, changing 1 to 2 on D, then a smooth transition with 1 (thumb) onto C. A little gap with the top notes will go unnoticed. Works for me.
Answer: In conjunction with the slur and the rit., it means bring out the right hand figure slightly. The slur already specifies that the figure is to be played legato and as a group (which means that the last crochet should probably be slightly detached from whatever follows it, although it will be held reasonably long anyway due to the rit.). From an interpretive point of view, you have a slow down (probably more a short ritardando than a ritenuto), a diminuendo and a descending line, so those three crochets should sound just a touch weighty.


Alocação dinâmica dando problema em c++

Question: Tenho este problema para resolver no Hacker Rank e o meu código para a solução deste problema ficou da seguinte maneira:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int **arr, arrqtd, queries;

    cin >> arrqtd >> queries;

    arr = (int **) malloc(sizeof(int)*arrqtd);
    for (int i = 0; i < arrqtd;i++){
        int size;

        cin >> size;
        arr[i] = (int *) malloc(sizeof(int)*size);

        for(int j = 0; j < size;j++){
            int value;
            cin >> value;
            arr[i][j] = value;
        }
    }

    for (int i = 0; i < queries;i++){
        int row,col;
        cin >> row >> col;
        cout << arr[row][col] << endl;
    }
    return 0;
}

O problema é que num exemplo de entrada como este:
10 10
3 916135 272188 794963
3 178997 502468 671251
1 122266
3 223511 996043 990104
3 319694 335208 200789
2 867809 273793
1 925358
1 71140
1 862238
1 994309
6 0
5 0
5 0
7 0
5 0
6 0
3 2
3 1
0 0
9 0

O valor inicial da minha matriz muda a partir da sexta iteração, gostaria de saber oque fiz de errado mas até agora não encontrei nada (e também gostaria de saber pq esse valor inicial muda do "nada").
Comment: Considerando que até tem o `#include ` no topo seria muito mais fácil utilizando `vectors`, a menos que seja uma restrição do problema. Ainda assim do que executei e vi no código pareceu me correto. A plataforma da-lhe algum erro na submissão ?
Comment: Se é C++, por que não usar `new int[]`?
Comment: Na minha máquina, funcionou. Slackware Current, GCC 7.2.0 e Clang 5.0.0.
Comment: @lemoce, ao executar na minha maquina aparece esse print que coloquei na pergunta.
Comment: @Isac não uso o vector por dois motivos, sou novo em c++ e não entendo e por que queria entender o pq de não funcionar da forma que escrevi sendo que não vi nenhum erro na lógica. E sobre a submissão, quando não dá erro dá tempo limite excedido.
Comment: @JeffersonQuesado pois não entendo como o mesmo funciona.
Answer: De facto tem um pequeno erro bem subtil no seu programa, e é na alocação de memoria do array bidimensional:
arr = (int **) malloc(sizeof(int)*arrqtd);

Que deve ser
arr = (int **) malloc(sizeof(int*)*arrqtd);
//------------------------------^

Está a fazer um array de ponteiros, logo é o sizeof de int* que interessa. Apenas fará diferença numa maquina onde o tamanho de int seja diferente de int*, e por isso pode não se deparar com o erro, dependendo de onde executa o código.
Com essa alteração o programa produz a saida desejada:
925358
867809
867809
71140
867809
925358
990104
996043
916135
994309

Confirme no Ideone
Recomendações:
A menos de fins educativos/ludicos, será mais apropriado utilizar quer <vector> ou new int[] como o @JeffersonQuesado mencionou por 2 motivos:
1 - Porque se tornam bem mais simples e evitam erros tal como este que tinha.
2 - São mais idiomáticos no mundo do C++. O código que escreveu é basicamente código C utilizando couts e cins
Comment: Realmente, agora deu totalmente certo. A respeito do código estar escrito "totalmente" em C se dá pelo motivo de eu saber somente C e estar aprendendo agora C++, por isso não sei e nem utilizei o `` e nem o `new int []`

Alocação dinâmica dando problema em c++


Rank Value getting different,while Give same boosting value in solr query

Question: i have 3 field in my solr index database and i search two queries but different field
Indexed data

employeeid : 220232
pskills : JSP, Servlets, HTML, Java
oskills : DB2, Oracle, JDBC, JNI, JSP, VC++, C, C++, Java, SQL, XML, Palm OS, UNIX, PALM OS, AIX, Linux, Solaris, Windows 2000,
TCP/IP, IP, IDS, Asset Liability Management, Enterprise Application
Integration

schema.xml
<code><field name="employeeid" type="string" indexed="true" stored="true" required="true" />
<field name="pskills" type="text" indexed="true" stored="false" required="false" />
<field name="oskills" type="text" indexed="true" stored="false" required="false" />
</code>

Query 1 = employeeid : 220232 AND (pskills : ( ( "java" ) )^3000.00)
Score:  0.6169528
Query 2 = employeeid : 220232 AND (oskills : ( ( "java" ) )^3000.00)
Score: 0.32307756

My question is Both field having "Java" keyword then why given different value
Answer: A number of reasons!  Particularly:

If the fields are different lengths, the score will be impacted (matches in shorter fields are weighed more heavily) (Definitely a factor here)
More than one match is found in one of the fields, giving that field a higher tf (say java appears once in oskills, but twice in pskills, for instance) (Doesn't appear to be the case here, but bears stating)
The terms java is more common across all documents in one field than the other.  If, for instance, across all documents, "java" appears in oskills in 1000 documents, but it only appears in pskills in 100 documents, then the match in pskills scores higher due to idf. (Don't know if this has an effect, since I don't know what's in the rest of the documents)

For some documentation on lucene scoring, see TFIDFSimilarity.
The scores you get are specific to the query and the state of the index at the time it was run.  They aren't intended to be compared to the scores of other queries.


Formatting a JSON Date with javascript

Question: I am returning a JSON object from my web service method. The object has some dates in it and so the generated JSON is like the following:
{"d": [
    {"PeriodID":8,"Period":"072011","BeginDate":"\/Date(1294268400000)\/"},
    {"PeriodID":2,"Period":"052011","BeginDate":"\/Date(1293836400000)\/"}
]}

I am trying to convert this data in a string to be added as <option> elements in an HTML select. This is my code:
var rtypes = data.d;
$.each(rtypes, function (key, value) {
    var text = value.Period + " - " + "from " + eval(value.BeginDate.slice(1, -1));
    var option = $("<option></option>").attr("value", value.PeriodID).text(text);
    $('#rpCombo').append(option);
});

Now the questions:

Can I format the date contained in the Period field (e.g. 072011) as a "July 2011"?
How can I convert the result of eval(value.BeginDate.slice(1, -1)) that is for instance something like "Wed July 14......" into something like "14/07/2011"?

Thanks for helping
Comment: Don't use eval. Instead use Date.parse...
Comment: Could you please post a sample? Thanks
Answer: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse
For example
// create 1 of June 2011 from Jun 2011
var period = new Date(Date.parse("1 "+period));

Here is what I think you want

Answer: Not sure on the scale of your project, but I was doing a lot with dates recently and benefitted by implementing javascript extensions on the javascript Date object. This will make your life soooo much easier as it has for me and will take care of the above scenario and then some.
There is a very good article here: Javascript/Json Date Parsing
I did need to tweak it a little, but no looking back since implementing this approach.

Formatting a JSON Date with javascript


Encapsulamento e Modificadores de Acesso Java

Question: Uma pergunta bem simples:
Dado essa classe:
public class User {

  private String nome;

  //get/set

  public boolean fazQualquerCoisa(){
    **duvida**.equals("algumacoisa");
 }
}

Dentro do método fazQualquerCoisa() eu devo fazer acesso ao nome diretamente ou usando o método get?
nome.equals ou getNome().equals?
Answer: Depende muito do que o seu método fazQualquerCoisa() faz e de como funciona o getter da sua String, se dentro do getter ele apenas tem um return nome; então tanto faz, caso haja algum tratamento específico da sua String que deve ser feito antes de ser chamado dentro do método, então o acesso deve ser feito através do getter.
Como podemos ver nesse tópico: Getters e setters são uma ilusão do encapsulamento?, muitas vezes as pessoas fazem os getters e setters única e exclusivamente pois estão acostumados a fazer assim sendo que na verdade não estão encapsulando nada, se for esse o caso, se encaixa no primeiro caso que eu citei: tanto faz.
Comment: Muito obrigado por me esclarecer isso!


Dynamically position a control from the center of a panel

Question: I have a panel with its width x height is 1200 x 550. In this panel, I added controls dynamically during runtime. I was wondering, if I could dynamically position the controls from the center of the panel and as each new control added, the position of the previous controls would also dynamically reposition itself. Can anybody suggest me something? I am kinda blurred right now ohohohoho.
Thank you very much for your kind attention.
Comment: Use a `FlowLayoutPanel` or `TableLayoutPanel` control. They handle this automatically.
Comment: @snurre I'm working on WinForms
Comment: @CodyGray I already tried to use FlowLayoutPanel. But the first control created appears at the upper left part of the FlowLayoutPanel and the subsequent control would follow the first one (FlowDirection = LeftToRight). I also tried to set the start point of the control but it refuses to follow it. Very wierd. But I will try out TableLayoutPanel. Thank you for you suggestion Cody :)
Answer: The way I get stuff to center in the TableLayoutPanel is to have two extra columns or rows that I have set to 50% each.  Any columns or rows between the two on the outside will automatically center based on the percentage.  Of course, this option works best with columns or rows that are set to specific sizes or for auto adjust types.


Can anyone offer the proof of sample skewness result from normal distribution tend to be normally distributed of $N(0,\frac{6}{n})$

Question: From wiki, I found that the natural method of moments estimator of Skewness:
$$b_{1}={\frac{m_{3}}{s^{3}}}={\frac{\frac1n\sum_{i=1}^n(x_i-\overline {x})^3}{\sqrt{{\frac1{n-1}}\sum_{i=1}^{n}(x_i-\overline{x})^2}^3}}={\frac{{\frac{1}{n}}\sum_{i=1}^{n}(x_i-\overline{x})^3}{\left[\frac1{n-1}\sum_{i=1}^{n}(x_i-\overline{x})^2\right]^{3/2}}}$$
Along with another estimator (maybe unbiased):
$$G_1=\frac{k_3}{k_2^{3/2}}=\frac{n^{2}}{(n-1)(n-2)}\frac{m_3}{s^3}=\frac{\sqrt{n(n-1)}}{n-2}\left[\frac{\frac1n\sum_{i=1}^n(x_i-\bar{x})^3}{\left(\frac1n\sum_{i=1}^{n}{{\left(x_i-\bar{x}\right)}^2}\right)^{3/2}}\right]$$
Then it says that under the assumption of $X$ normal $\sqrt {n}b_{1}\xrightarrow{d}N(0,6)$ which indicate that $\operatorname{Var}(b_1)=\frac{6}{n}$
Furthermore, it also gives out the variance of $\operatorname{Var}(G_{1})={\dfrac {6n(n-1)}{(n-2)(n+1)(n+3)}}$
Now I would have the following 2 questions:
1, Can anyone help to provide the proof of $\operatorname{Var}(b_1)=\frac{6}{n}$
2, As far as I know $\operatorname{Var}(Cx)=C^2Var(x)$ this indicate that $\operatorname{Var}(G_1)=\operatorname{Var}\left(\dfrac{n^{2}}{(n-1)(n-2)}b_1\right)=\dfrac{6n^3}{(n-1)^2(n-2)^2}$. Why the variance on wiki different.
Answer: You don't have $\def\Var{\operatorname{Var}}\Var(b_1)=\frac6n$, only that $\Var(b_1)\sim\dfrac6n$ (i.e. $n\Var(b_1)/6\to 1$ as $n\to\infty$) coming from the weak convergence $\sqrt{n}b_1\xrightarrow{d}N(0,6)$.
Comment: Thanks for your answer, Sorry for the silly mistake I've made, I understand that when n is large the 2 estimators tend to be the same. But my another question lies on how to prove it. So if you could be so kind to answer these, since I have searched the internet and cannot find any clue.
Comment: Please any possible clue is thankful. I've been searching for 2 days but without any result.


How To Check Email Field For Duplicate/Existing Email Address Prior To Submitting Sign-Up Form

Question: Update: The email address check for the email field is returning "email address is already in use" upon entering an address regardless if the email address is new or exists in the mySQL database. I would like it to only return the error when an email exists in the email field of the users table in the database.
The current code for the signup page is:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<?php
   include('includes/inc-public.php');
   include('includes/classes/class.user.php');
 $user = new User();
 $email = $_GET['su_email'];
 $valid = false;
 header('Content-Type: application/json');

 if(!is_null($email)){
  $is_valid = $user->get_email($email);
  if($is_valid){
     echo json_encode($is_valid);
  }
    else{
   echo json_encode($valid);
  }
   };
?>

signup form when a new email address is being entered in the email field
users table of mysql database
Comment: This is not possible with a server side language, you will need some client side as javascript
Comment: I am using the jscript from jquery.validate.js if that is what you are referring to.
Comment: this is the screenshot from when an existing email address is being entered in the email field  [2]: http://i.stack.imgur.com/Y1bTP.jpg
Comment: is the remote: "/email_check.php" file in a subfolder of the jquery file? what is output of the $is_valid can you check this.
Comment: @dumb_moron the "/email_check.php" file is on the public_html/ path of my server which is the root where my index and signup pages are. the output of $is_valid is "public function get_email($email)
  {
   $this->sql = "SELECT 'vemail' FROM 'wmldir_users' WHERE vemail ='".$email."'";
   $this->data = $this->fetch_row_assoc($this->sql);
   if(!empty($this->data))
    return $this->data;
   else{
    return false;
   }
  }" which is set in my class.user.php file
Answer: From documentation:
The serverside response must be a JSON string that must be "true" for valid elements, and can be "false", undefined, or null for invalid elements, using the default error message. If the serverside response is a string, eg. "That name is already taken, try peter123 instead", this string will be displayed as a custom error message in place of the default.
So you need to change your is_valid to true.
Comment: by changing the is_valid to true you mean from this line of code "public function get_email($email)
  {
   $this->sql = "SELECT 'vemail' FROM 'wmldir_users' WHERE vemail ='".$email."'";
   $this->data = $this->fetch_row_assoc($this->sql);
   if(!empty($this->data))
    return $this->data;
   else{
    return false;
   }
  }
" ?
Comment: Yes instead of returning $this->data return true Else the validation will not work
Comment: I changed it to true now I get false when I enter any email address whether it is new or exists in the mysql database
Comment: Ok can You echo the $is_valid variable
Comment: so instead of returning $this->data return true I should echo the $is_valid variable, or am I doing so in conjunction with returning true?
Comment: don't know is this the plugin u are using http://jqueryvalidation.org/remote-method/
Comment: yes it is the plugin i am using...it checks and validates every field on my signup form properly except for the email address field


Some tips on 3D Trig?

Question: I understand this isn't a MATHS question specifically, however, sometimes I have trouble identifying when to use 3D trig, and trouble with it in general. Can some of the experienced/advanced people provide me with any information? ;)
Thanks!
Answer: Of course it is a Maths question. What three dimensional (3D) trigonometry means is just a twist of the usual trigonometric functions; sine, cosine and tangent, and how you can view all the plane parts in a 3D figure. Thus, you use 3D trig to identify 2 or more linking triangles in the 3D figure, for example, in a pyramid. The first thing you need to do is to identify the right angles in your figure.

Some tips on 3D Trig?