How to Handle Actual Time with Durations in C#?

Question: What's the preferred approach to compare a complete DateTime instance with an hour, minute, and second which represents an actual time of day, with the ability to operate over those triplets (eg add hours, minutes seconds..)?

My current approach is something like
DateTime startHour = new DateTime(1900,1,1,12,25,43);
DateTime endHour = new DateTime(1900,1,1,13,45,32);

// I need to, say, know if a complete DateTime instance
// is later than startHour plus 15 minutes

DateTime now = DateTime.Now();

startHour = startHour.addMinutes(15);

if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour,
                    startHour.Minute, startHour.Second)) > 0)
{
    //I can do something now
}

This is very cumbersome and even failure prone. TimeSpans are not a solution as far as I can see, because they represent spans and aren't bound by the 24 hours limit (a TimeSpan of 56 hours 34 minutes is valid.)
What's the preferred approach for this type of calculations?
Comment: What problem are you trying to solve? Is it merely that TimeSpan can exceed 24 hours? Because you can easily check for that.
Answer: It's not at all clear what you mean by "is greater than startHour"... but taking
TimeSpan startHour = new TimeSpan(12, 25, 43);
if (endHour.TimeOfDay > startHour)
{
    ...
}

... works pretty simply.
By all means add argument checking to make sure that you don't specify a value for startHour which is < 0 or > 23 hours, but that's all pretty easy.
.NET's date and time API is quite primitive (even in 3.5) compared with, say, Joda Time - but in this particular case I think it's not too bad.
Comment: This is the first time I've heard someone refer to the .NET date/time API as "primitive" in a negative tone, and I couldn't disagree more. :)
Comment: @280Z284: Until 3.5 you couldn't even use non-local time zones! Compared with Joda, .NET really is quite primitive. There's no type representing an instant with an associated time zone - just a UTC offset. Blech. Would consider porting Joda if I had more time.
Comment: Yeah, Joda rocks. Especially when compared to native Java Datetime.
Comment: Are there any .NET libraries out there that can help?
Comment: Or, perhaps, a .NET conversion for Joda Time?
Comment: @Erik: Not as far as I know. I keep wondering about a .NET port of Joda Time, but I don't have enough expertise or time...
Answer: A little hint - .NET supports arithmetic operations on DateTime objects, and returns a TimeSpan object. Thus, you can do the following:
DateTime fromDate = ....
DateTime toDate = ....
TimeSpan diff = toDate - fromDate;

and you can expand this to:
DateTime fromDate = DateTime.Now;
DateTime toDate = DateTime.Now.addMinutes(x);

if ((toDate - fromDate).TotalMinutes > 15) {
    ...
}

Answer: You should use TimeSpan for startHour and endHour.
When comparing with now, you should "convert" them to a full DateTime or get the Time with DateTime.TimeOfDay as mentioned by Jon Skeet.

TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;

if (now.CompareTo(DateTime.Today.Add(startHour)) > 0) {
    //...
}

or

TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;

if (now.TimeOfDay.CompareTo(startHour) > 0) {
    //...
}

Comment: Why do it that way instead of taking the current time of day? And why use CompareTo instead of the handily-overloaded "greater than" operator?
Answer: So you're only interested in the time component of the date.
if(DateTime.Now.TimeOfDay > startHour.TimeOfDay)
{
  // do stuff
}

What's wrong with doing this?
Comment: It will work, but there's no need for the Date part of startHour and endHour.
Comment: Yes, but that's if you assume that the snippet that was provided is the only part of the code that uses startHour. But what if startHour's date value is used elsewhere?


how to open HTML page stored on client machine from aspx page

Question: I m developing asp.net application in which I m opening HTML page that is stored on client machine on that page I have a link which will open aspx page on server, On that aspx page I have a button that will open another html page stored on client machine.
Since I m new to web development Plz help me and suggest me some solutions for this ASAP.
Thanks....
Edited to add:
Is it possible to navigate between HTML Pages on Client machine and ASPX pages on server without uploading HMTL file on server. Can I use some javascript code on aspx page to open my locally stored HTML page...
Comment: You design have problem - maybe you create the all the pages asap. Redesign your procedure to do your work other way.
Answer: try this

click to open whatever

Comment: My HTML page is on client machine and  not allow aspx server page to open this file.
Comment: @shania: actually this *will* work **IF** you know what directory contains the user's html file...which you probably don't.
Answer: The only way you can access files on a client's machine is by them uploading it to you.
Comment: I don't *think* they want access to the file, but instead want to fire an "open-file" command for the user.
Answer: You cannot do this reliably, because the server doesn't know where the HTML page is (what drive, what directory).
If you explain why your are trying to do this, perhaps we can suggest an alternative design.
Answer: Not quite sure what you are asking, but  should create an open file dialogue.  If you want the ASPX page to read the HTML file on users local machine then you need to upload it.
Answer: If I understand correctly, you have 2 problems:

you need to redirect from your aspx page to a local html page
you need to redirect from a local html page back to your server page

To solve nr 1 you will need to know the absolute location path to the html page on the client machine (ie. C:\SomePath\LocalFile.html) then do a link there:

And no, it's not possible to access local files from javascript.
At most you can ASK the user to point to the local file (ie.via a file upload input) then intercept that event and use the path to that file.
   window.location.href=document.getElementById('fileInput').value;

but even that is riddled with problems as it will not work in some browsers,etc.
To solve nr2 you will probably need to add some javascript (which by default is turned off for local html files!!) and use "history.previous" to detect the path to your public aspx page. Something in the order of :
   window.location.href= history.previous.replace("myfirstpage.aspx","redirectedpage.aspx");

which will send someone that came to the current page from ,say, "http://www.publicserver.com/myfirstpage.aspx", will be redirected to "http://www.publicserver.com/redirectedpage.aspx",


executar git pull sem solicitar senha

Question: Gostaria de saber se existe a possibilidade de  eu armazenar a minha senha em cache para o meu usuário do git para que não seja solicitado a senha toda vez que eu realizar um git pull do servidor , lembrando que eu não tenho permissão de alterar as configurações do servidor
//Atualização
Utilizo command line via shell do linux  e não estou utilizando git hub  o git server esta instalado em um server e sempre solicita senha no momento do pull
Objetivo eu tenho um Shell Script que atualiza 5 projetos de uma vez mas ele solicita 5 vezes a msma senha gostaria de passar algo por parametro tal como
git pull origin master --nopassword
ou git pull origin master --password 12345
Comment: Você está usando um terminal comum ou algum aplicativo? No terminal normalmente você só precisa colocar a senha uma vez por sessão.
Comment: Você utiliza o repositório no github?
Comment: atualizei as informações
Comment: veja se `expect/send` te resolve: http://stackoverflow.com/a/16007759/1001109
Answer: Tem comando pra guardar senha temporariamente: git config credential.helper cache, ou comando pra guardar a senha de forma definitiva: git config credential.helper store. O primeiro guarda as credenciais em cache por 5 minutos (padrão). O segundo guarda permanentemente.
A diferença entre um e outro é o risco de segurança: se você não é o único a acessar o computador, talvez git config credential.helper cache seja melhor, pois suas credenciais não ficarão expostas a outros usuários da máquina depois que vc terminar suas edições.
Determine o modo de armazenamento das credenciais: git config credential.helper storeou git config credential.helper cache.
Na próxima vez que fizer um git push, suas credenciais ficarão armazenadas.
Fontes: git-credential-store, git-credential-cache.
Answer: Lembrei que já tive esse problema e encontrei a solução aqui: https://stackoverflow.com/a/7773605/1001109
Pode ser porque você está com a URL errada, usando https ou git ao invés de ssh:
A URL correta:
email@example.com/username/repo.git

Outras comuns mas que podem gerar esse erro:
https://github.com/username/repo.git
git://github.com/username/repo.git

Comment: atualizei as informações

executar git pull sem solicitar senha


Is Bayes' theorem useless if $A$ is a subset of $B$?

Question: Using Bayes' theorem, $P(A|B) = \frac{P(B|A)P(A)}{P(B)}$
wouldn't this just be the percentage of $A$ in $B$ if $A$ is a subset of $B$?
If A is a subset of B:
$$P(B|A) = 1,$$
$$P(A|B) = \frac{P(A)}{P(B)}$$
Therefore Bayes theorem becomes $\frac{P(A)}{P(B)}$ if $A$ is a subset of $B$.
So you don't really need to use Bayes' theorem in these situations, right?
Comment: I don't know how Macro deduced that James meant P(A|B)=1 if B⊂A rather than P(B|A)=1. Also James' equation P(A)/P(B)=A/B makes no sense and I really did not know what he meant. But Macro knew that he meant P(B|A) = P(B)/P(A) which is correct and would make sense that it is what James really meant.  Kudos to Macro!
Answer: Bayes' Rule is still valid when $A \subset B$, but sometimes the conditional probabilities can be deduced intuitively in these situations. For example, it makes sense that if $A \subset B$, then $P(B|A) = 1$, since if $A$ happens, we know $B$ must have happened - you don't need Bayes Rule to see that. You are also right that in this case $P(A|B)=P(A)/P(B)$, which is just a special case of Bayes Rule, so I'd still say you "need" Bayes Rule in this case.
To make this more concrete, take an example: suppose you roll a fair die and $B$ is the event that the roll is even, while $A$ is the event the roll is a '6'. Clearly $A \subset B$. $P(B|A) = 1$, since if you rolled a '6', you clearly rolled an even number. Also
$$ P(A|B) = \frac{ P(B|A) \cdot P(A) }{ P(B) } = \frac{P(A)}{P(B)} = \frac{\frac{1}{6}}{ \frac{1}{2} } = \frac{1}{3} $$
This makes intuitive sense - conditioned on the fact that the roll is even, there are three equally likely outcomes - $\{2,4,6\}$ - therefore, the probability of rolling a '6' is $1/3$.
Comment: I don't see how the solution is different (aside from my meaning to say A is a subset of B). Your dice example is the same as the frequencies of A/B = 1/3.

I wrote P(A)/P(B) = A/B because you divide them both by the same number. In the dice example you would divide the frequencies both by 6 to get the probability.
Comment: @James, can you clarify what you mean by $A/B$? $A$ and $B$ are events, not numbers - what does their ratio mean?
Comment: I was responding to the apparent statement that Bayes' Theorem did not apply when one event was a subset of the other, and I've demonstrated that it still holds in that case. You don't *need* to use Bayes' rule in this case because the conditional probabilities are intuitively clear, but the theorem still holds nonetheless.
Comment: I mean frequencies. For example, say you have 1000 dogs. 100 of them are black, and 50 of those black dogs are mutts (all mutts are black). The probability of a dog being a mutt, given it is black, is A/B = 50/100 = 50%.
Comment: Ok that's what I thought.


What are the instrument parts, that goes under the song lyrics

Question: What do we call the instrument parts, that goes under the song lyrics?
The parts that doesn't equal to the melody of the lyrics but add lot of  fullness.
EDIT
Here I am talking about music parts that are normally written for violins and other strings, and some times lead guitars.
Comment: probably you mean right hand of the piano accompaniment. I would call it harmonisation, arpeggios or even counterpoint.
Comment: @AlbrechtHügli I edited the question
Comment: "...normally written for violins and other strings..."  you need to get out more :-) . A jazz singer has an entire swing band behind them, for example.  In any case,  the three terms in @AlbrechtHügli 's comment are correct.
Answer: The accompaniment and the bass line are the usuals. Accompaniment as it accompanies the melody, the bass line as it's played by the bass, or the left hand on piano. Sometimes all that is called the chords which would be played on guitar or piano - the main two instruments that play multiple notes. Occasionally there is a drum pattern.
EDIT: now a different question, partially. The guitar part could be the riff, and strings/horns be the accompaniment again.


Permission denied after start container jenkins

Question: When creating a jenkins container, the following errors appear. What could be the problem?
jenkins_1  | touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
jenkins_1  | Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

It is my docker-compose:
version: '3.7'

services:

jenkins:
    image: jenkins/jenkins:lts
    ports:
      - 7080:8080
      - 50000:50000
    privileged: true
    volumes:
      - /tmp/jenkins-test:/var/jenkins_home

Answer: This is explained in the issue.
sudo chown 1000 /tmp/jenkins-test
If the directory already contains files:
sudo chown -R 1000 volume_dir

This will store the jenkins data in /your/home on the host. Ensure
  that /your/home is accessible by the jenkins user in container
  (jenkins user - uid 1000) or use -u some_other_user parameter with
  docker run.
You must set the correct permissions in the host before you mount
  volumes sudo chown 1000 volume_dir

or you can try
Resolved albeit with torture involved.
Create a jenkins user on the host, note it's uid
docker run -u <jenkins-uid> ...

Do NOT docker run -u 'jenkins' - This causes the container's own
  jenkins user to continue to be used. Either choose a different name on
  the host and pass this through or pass through the resultant uid.

A bash script that can you try to run
#!/bin/bash
mkdir $PWD/jenkins

sudo chown -R 1000:1000 $PWD/jenkins

docker run -d -p 8080:8080 -p 50000:50000 -v $PWD/jenkins:/var/jenkins_home --name jenkins jenkins

Comment: how can i do this with docker-compose.yml?
Comment: Hi @АлексейЗемцов you can add this to the Dockerfile
https://stackoverflow.com/questions/43787360/error-with-jenkins-ecs-and-efs
Answer: if after doing all the permission and all things still if its not working then just change the volumes mapping like this..
./your_folder:/var/jenkins_home

it will work fine..some time pwd is not works that will the create issue.


How set Http setHeader?

Question: I use below code:
class Test extends AsyncTask < String, Void, String > {

 @Override
 protected String doInBackground(String...urls) {
  String response = "";
  for (String url: urls) {
   DefaultHttpClient client = new DefaultHttpClient();
   HttpGet httpGet = new HttpGet(url);
   try {

    HttpResponse execute = client.execute(httpGet);
    InputStream content = execute.getEntity().getContent();

    BufferedReader buffer = new BufferedReader(
     new InputStreamReader(content));
    String s = "";
    while ((s = buffer.readLine()) != null) {
     response += s;
    }

   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return response;
 }

 @Override
 protected void onPostExecute(String result) {
  TestLogin.textView.setText(result);

 }

}

What changes should be modified in above code to set header?
I want to get access to my webpage jsonObjcets "http://apimobile.dev2.rtbtracker.com/api/v2/users"
Comment: `DefaultHttpClient` has been deprecated now. Avoid using it!
Answer: You can set headers on the httpGet object.
See the documentation for setHeader before client.execute(httpGet)
Answer: Use the setHeader() method on the HttpGet
httpGet.setHeader("Header-Name", "Header Value");

In your code :
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

//Setting the header
httpGet.setHeader("Header-Name", "Header Value");

try {

   HttpResponse execute = client.execute(httpGet);

   //process

} catch (Exception e) {
            e.printStackTrace();
}

Answer: httpGet.addHeader('headerName', 'headerValue'); // will work

This line adds the header to the HttpGet Object which goes to the request url when you run HttpClient.execute()
Comment: Code only answer are not recommended, please add some information on why this code would work - From Review
Answer: response.setHeader("name", "value");

How set Http setHeader?


I get the following error message while updating a Drupal site

Question: I keep getting this error when I try to update Drupal. I have no clue what it means; it seems I have a missing column.

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base.access_exposed' in 'field list':
  SELECT base.id AS id, base.name AS name, base.label AS label, base.plugin AS plugin, base.active AS active, base.weight AS weight, base.status AS status, base.dirty AS dirty, base.module AS module, base.access_exposed AS access_exposed, base.data AS data FROM {rules_config} base WHERE (base.status IN (:db_condition_placeholder_0, :db_condition_placeholder_1, :db_condition_placeholder_2)) ; Array ( [:db_condition_placeholder_0] => 3 [:db_condition_placeholder_1] => 2 [:db_condition_placeholder_2] => 6 ) in EntityAPIController->query() (line 152 of /opt/readyby21.org/public_html/sites/all/modules/contrib/entity/includes/entity.controller.inc).
Comment: It looks like you've got a module that may not be updating correctly.  Are you getting this message when you go to update.php, or all the time?  Also, just to make sure, you're on Drupal 7, and not Drupal 8 or something like that?
Comment: I am on Drupal 7, I am only getting it when I go to update.php - when I go to default (index.php), I get a white screen, with nothing on it.
Comment: This http://drupal.org/node/158043 explains how to deal with a white screen of death.
Answer: According to http://drupal.org/node/1489394 some users experienced this problem when updating their rules module.  It looks like you'll need to update your rules, entity and internationalization modules all need to be updated to the latest versions (7.x-1.5 for internationalization and 7.x-1.0-rc2 for entities).  If you already have, try only updating those modules, before attempting to update the rules module (you can disable the rules module.  This should also solve the problem, but I assume you're using it for something or other).
As BetaRide points out above, checking into your web logs may shed some additional light on the problem, in case the above doesn't solve the problem.


How can I verify an emails DKIM signature in Python?

Question: Given a raw email, how can I validate the DKIM signature with Python?
Ideally I'd like more than just a pass / fail result, I'd like to know details of any issues.
I've found the dkimpy package, but the API isn't obvious to me.
Answer: For a simple pass/fail validation:
import dkim # dkimpy

# Returns True/False
res = dkim.verify(mail_data.encode())

For something more nuanced you can do this:
d = dkim.DKIM(mail_data.encode(), logger=None, minkey=1024, timeout=5, tlsrpt=False)

#
try:
    d.verify()
    # If it fails, a `dkim.ValidationError` exception will be thrown with details
except dkim.ValidationError as e:
    print(e)

# dkim.ValidationError: body hash mismatch (got b'PXUrNdoTzGcLtd4doJs+CufsiNvxoM5Q3SUPGi00C+I=', expected b'ax9SInd7Z3AQjRzcZSnY6UK392QEvjnKrjhAnsqfDnM=')


Treatment effect on categorical dependent variable - changed to numeric

Question: I conducted an experiment where I tested the effect of copper on irrigation dripper blockage.
I had dripper types "copper" and "normal" and once a week I measured whether they are working (1) or not (0).
All the drippers started working (1) and all of them were blocked (0) by the end of the experiment.
I need to analyze if there was a significant effect of the type and when.
I also suspect that some type of debris might block the drippers based on their proximity to the water source so I added their position to the data.
A sample of the data:
> head(data)
# A tibble: 6 × 6
  Date                Lateral Position `Distance (m)` Type   Working
  <dttm>              <fct>   <fct>             <dbl> <fct>  <fct>
1 2022-08-10 00:00:00 1       1                   0.7 Normal 1
2 2022-08-10 00:00:00 1       2                   1.2 Normal 1
3 2022-08-10 00:00:00 1       3                   1.7 Copper 1
4 2022-08-10 00:00:00 1       4                   2.2 Copper 1
5 2022-08-10 00:00:00 1       5                   2.7 Copper 1
6 2022-08-10 00:00:00 2       1                   0.9 Normal 1
> dput(head(data))
structure(list(Date = structure(c(1660089600, 1660089600, 1660089600,
1660089600, 1660089600, 1660089600), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Lateral = structure(c(1L, 1L, 1L, 1L, 1L, 2L), levels = c("1",
"2", "3", "4"), class = "factor"), Position = structure(c(1L,
2L, 3L, 4L, 5L, 1L), levels = c("1", "2", "3", "4", "5", "6"), class = "factor"),
    `Distance (m)` = c(0.7, 1.2, 1.7, 2.2, 2.7, 0.9), Type = structure(c(2L,
    2L, 1L, 1L, 1L, 2L), levels = c("Copper", "Normal"), class = "factor"),
    Working = structure(c(2L, 2L, 2L, 2L, 2L, 2L), levels = c("0",
    "1"), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))

So I calculated the days each dripper worked (days_worked) as suggested by @sashahafner. The new dataframe:
structure(list(id = 1:6, days_worked = c(15, 25, 15, 21, 25,  25), last_date = structure(c(1661385600, 1662249600, 1661385600,  1661904000, 1662249600, 1662249600), tzone = "UTC", class = c("POSIXct",  "POSIXt")), lateral = structure(c(1L, 1L, 1L, 1L, 1L, 2L), levels = c("1",  "2", "3", "4"), class = "factor"), position = structure(c(1L,  2L, 3L, 4L, 5L, 1L), levels = c("1", "2", "3", "4", "5", "6"), class = "factor"),  type = structure(c(2L, 2L, 1L, 1L, 1L, 2L), levels = c("Copper",  "Normal"), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df",  "tbl", "data.frame"))
I want to check whether the position (lateral and position columns) has an effect and if the type has  an effect on the days_worked.
What kind of test should I use?
Any pointers as to what to read or how to solve this will be much appreciated.
Thanks
Comment: This makes sense. I'll try and see if it gives the results I'm after. I don't think I understand the part about error distribution. Also, why would I need to use lm()? I'm trying to show difference between treatments not predict time to blocked drippers
Comment: Why not simply calculate how many weeks the drippers worked before becoming blocked and use that as your response variable? Of course you have to think about an appropriate error distribution but even a simple transformation could work, and allow you to use `lm()` or a wrapper.
Answer: What you have is a type of survival analysis: you have an event and you want to know if some treatment (with other variables taken into account) is associated with the time to the event. Your initial data format could have been appropriate for formal survival analysis.
In this case, with all cases eventually having the event, survival analysis can be just a simple linear regression (via lm() in R) of time-to-event against your set of predictor variables, as @sashahafner suggested in a comment. That would evaluate the association between time-to-event and treatment with the other predictors taken into account. You could extend that to assess the possibility that the effect of treatment depends on the values of the other variables, by adding interaction terms among them.
More generally, you would need to take "censoring" into account: for example, it would be "right censoring" if some cases never developed the block before the end of your study so you only had a lower limit to the time-to-event. There is one caution related to survival analysis that you should consider: if you only evaluated blockage "once a week" but your time-to-event values are on the order of 20 days, you don't really have precise time-to-blockage data, just times to when you found the blockage. That's called "interval censoring"; the link points to over 90 questions about that on this site, with more available via a text search for "interval censor*".
Example
Here's some sample data, with type=copper taking 10 days longer to block on average and each extra unit of dist leading to 1 day faster to block. Add in some random error with a standard deviation of 3 days. Also, provide for a situation when observations stopped at 35 days so longer event times are right censored.
pipeData <- expand.grid(dist=1:5,type=c(rep("normal",4),rep("copper",4)))
set.seed(101)
pipeData[,"blockedTime"] <- 30 - pipeData$dist + 10*(pipeData$type=="copper") +rnorm(40,0,3)
pipeData[,"event"] <- as.numeric(pipeData$blockedTime<35)
pipeData[,"censTime"] <- pipeData$blockedTime
pipeData[pipeData$event==0,"censTime"] <- 35

If there are no censored event times, this is nicely handled by a simple linear model with both type and dist as predictors.
summary(lm(blockedTime~type+dist,data=pipeData))
#
# Call:
# lm(formula = blockedTime ~ type + dist, data = pipeData)
#
# Residuals:
#    Min     1Q Median     3Q    Max
# -6.008 -1.791  0.395  1.913  4.575
#
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept)  30.2971     1.1233   26.97  < 2e-16 ***
# typecopper   10.2769     0.8812   11.66  5.9e-14 ***
# dist         -1.1963     0.3115   -3.84 0.000465 ***
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#
# Residual standard error: 2.787 on 37 degrees of freedom
# Multiple R-squared:  0.8029,  Adjusted R-squared:  0.7923
# F-statistic: 75.38 on 2 and 37 DF,  p-value: 8.908e-14

The coefficient for typecopper is the estimated difference in days to blockage between normal and copper, at the same value of dist. The "significance" of that coefficient represents the "significance" of type. The coefficient for dist is the change in number of days to blockage per unit of dist.
If there is right censoring, you can use survival analysis. For example:
library(survival)
summary(coxph(Surv(censTime,event)~type+dist,data=pipeData))
# Call:
# coxph(formula = Surv(censTime, event) ~ type + dist, data = pipeData)
#
#   n= 40, number of events= 24
#
#                coef exp(coef) se(coef)      z Pr(>|z|)
# typecopper -3.92281   0.01979  0.74573 -5.260 1.44e-07 ***
# dist        0.49910   1.64724  0.17492  2.853  0.00433 **
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#
#            exp(coef) exp(-coef) lower .95 upper .95
# typecopper   0.01979    50.5425  0.004588   0.08533
# dist         1.64724     0.6071  1.169131   2.32086
#
# Concordance= 0.87  (se = 0.024 )
# Likelihood ratio test= 47.21  on 2 df,   p=6e-11
# Wald test            = 28.35  on 2 df,   p=7e-07
# Score (logrank) test = 46  on 2 df,   p=1e-10

Here, the coefficients are the log-hazard differences associated with type and dist. The negative coefficient for typecopper means that the hazard of blockage is lower for copper than for the reference normal.
Comment: Thanks, I guess I'll start reading about survival analysis. The blockage checking interval was actually determined by when we had time to go and check the drippers considering that a week difference between treatments isn't meaningful in RL and so a "precise" time can be a week. What I don't understand is how `lm()` can be used to determine significant difference between the treatments?
Comment: @sdabach I've added examples showing how `lm()` can be used if there's no censoring, and `coxph()` can be used if there is.
Comment: Thanks for the great help.
Answer: In accord with the suggestion above, whatever the time granularity you used to check the dripper (day, week, month) I would use that as the basis, used the difference between the deployment date and the date of failure as you test variable.
Then you could use a straight up Z-Test or T-Test to calculate if there is a difference between the two, dependent on how many observations you had. The important thing is that all the drippers should have received the same treatment for this to be meaningful.
EDIT:
To clarify, the reason that you use a t-test or z-test is because you were comparing treatments, the difference between the copper and the regular drippers. The assumption is that all else is held constant.
You can do other types of analysis, such as survival.
But the comparison of two samples with different treatments is asking are these two samples from the same population, based on this one factor, time to failure?
If they are essentially the same then they difference in dripper's material will have no affect on the mean length of time until failure.
Comment: Thank you too. Doesn't the number of observations depend on variability? Can the Z or T tests consider several variables (the position)? or should I use them only if I find the position has no effect?
Comment: The number of observations depend on your experimental design. How many of each material do you have? If both groups are greater than 30 you should use the Z-test, if less than 30 or not normally distributed, use the t-test (in reality most people us the t-test either way)
Comment: Thanks. I'll try to eliminate the position effect in the experiment itself.


What is going on with Drupal Packagist?

Question: I was using
<code>  "repositories": {
   "drupal": {
       "type": "composer",
       "url": "https://packagist.drupal-composer.org/"
   }
  },
</code>
in my composer.json file, but now it started saying that it can't load it and it is deprecated so go to https://www.drupal.org/node/2822344 instead. However, the info on there suggests changing it to point to https://packages.drupal.org/7 but this also doesn't seem to exist.
Can anyone shed some light?
Comment: https://medium.com/@dcorbacho/packagist-drupal-composer-has-been-shut-down-9a6c47b9eaa4
Answer: https://packages.drupal.org/7 is the correct url, its just that drupal.org does not server any content under this url. Composer will call https://packages.drupal.org/7/packages.json
Answer: packagist.drupal-composer has been shut down
https://medium.com/@dcorbacho/packagist-drupal-composer-has-been-shut-down-9a6c47b9eaa4#.ylaxgo3n9
You must use the official Package Repository from Drupal.org, see https://www.drupal.org/docs/develop/using-composer/using-packagesdrupalorg
for Drupal 7 use https://packages.drupal.org/7
for Drupal 8 use https://packages.drupal.org/8
Comment: according to my comment :D
Comment: But with more relevant info.

What is going on with Drupal Packagist?


FIPS 140-2 compliant random number generator - Java

Question: I need to write a random number generator that uses an algorithm that is FIPS 140-2 compliant/certified. I am having a tough time finding anything that will work for me. Anyone done this before? I don't want to pay an arm and a leg to do this.
Using Java 8. Development environment is Mac OSX and servers are CentOs 7.
UPDATE:
Code looks like this to date.
<code>import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class LongRandom {
public LongRandom(){}
private Long minLong = new Long("1000000000000000000");

public List<Long> getRandomLongList(int numberOfRandom){
final SecureRandom random = new SecureRandom();
LongStream longs = random.longs(numberOfRandom, minLong, Long.MAX_VALUE).distinct();
long[] arrayLong = longs.toArray();
List<Long> list = Arrays.stream(arrayLong).boxed().collect(Collectors.toList());

return list;
}

public Long getRandomLongWinner(List<Long> potentialWinners){
final SecureRandom random = new SecureRandom();
Collections.shuffle(potentialWinners,random);
IntStream ints = random.ints(1,0,(potentialWinners.size() - 1));

return potentialWinners.get(ints.findFirst().getAsInt());
}
</code>
}
Comment: Where are you blocked? What have you currently written? (please post your code so we can provide an accurate help)
Comment: An algorithm can be FIPS compliant, an implementation can be FIPS certified (which means it passes all test vectors, does startup tests etc.)
Comment: Have you read the Javadoc for `SecureRandom`?
Comment: I have something like this so far... I'm just not sure if it's random enough. I did read the SecureRandom Javadocs that states that it is minimally FIPS 140-2 compliant.
Comment: The Java Docs say that it minimally complies, but I do not see it in the NIST validation database, so in my mind if you need a FIPS 140-2 module, this will not do.  Maybe it validated and the NIST database is too cryptic, so to speak, for me to interpret properly?
Answer: Here's the official list of adequate RNGs.  BSAFE is widely used.
http://csrc.nist.gov/groups/STM/cavp/documents/rng/rngval.html
Comment: BSAFE *was* widely used for random number generators until the Dual EC DRBG debacle. Other providers had the same PRNG, but only BSAFE made it their default, as far as I know.


How to click-through a div without losing its hover events?

Question: I have 2 divs stacked upon one another. I want to be able to receive click events for the bottom element without losing hover events for the top element.
If I assign pointer-events: none top the top element, the click-through works, but then I lose the hover events for the top element.
How can it be done?
Comment: would explain morer if you share some code, or img.
Comment: A Short, Self Contained, Correct (Compilable), Example would be helpful (http://sscce.org/)
Answer: The MDN docs clearly says that's what is supposed to happen:

none

  The element is never the target of mouse events; however, mouse events
  may target its descendant elements if those descendants have
  pointer-events set to some other value. In these circumstances, mouse
  events will trigger event listeners on this parent element as
  appropriate on their way to/from the descendant during the event
  capture/bubble phases.

https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
What you can do is write a jQuery script that captures the clicks and transmits them.
<code>jQuery("top-div").click(function(e)
  {
     e.preventDefault();
     jQuery("bottom-div").click();
  });
</code>
Something like that...


javascript paste text into a Textarea remove (CR) from CRLF

Question: I need to preserve the line breaks of an text input inserted in my javascript front-end page, after being pasted into a Textarea input
this is what happens if I debug the value contained in the onSumbit() method of the textarea

I need to have CRLF but the image clearly shows that the CarriageReturn (CR) have been removed from the text inserted and only LineFeed (LF) is still present.
I've tried to add the css property white-space: pre-wrap to the textarea but that didn't work.
How can I preserve the CRLF of the text?
thanks
Answer: Where are you trying to encode the text to base64? If you are doing it in the frontend using Javascript to take the value, you should know that the value contains only LF. If you are doing it in the backend after a body submit by POST then yes, the value contains CRLF and something is wrong.
But my guess from your issue description is that you are in the first -frontend- case, so it's not the encoding to blame.
So, when you are getting the value from the textarea, you can do something like
<code>value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n")
</code>
to always get CRLF.
Comment: There seems to be no way of it. My suggestion is that, when you read the text, 1) convert all CRLF to LF and then 2) convert all LF to CRLF. This way you'll always get CRLF lines in every case (content edited or not, initial value contained CRLF or LF). Something like value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") should be enough
Comment: yes I'm in the front-end case, could you explain me why the value contains only LF? Before putting it into the textarea it does contains CRLF so why and when is javascript removing it?
Comment: That's what the standards are saying. If you place CRLFs, they are converted to LFs after editing. You can verify  with a console.log after a value change. You can type something like console.log({v: e.target.value}), so you can clearly see the control characters. You can also try to add a default value and then to convert to base64 without editing. You should have your precious CRs.
Comment: Is there a way to change this standard ?


GUI for building JanusGraph DB

Question: I am exploring JanusGraph DBs and require visual editing, like MySQL workbench.
I found some visual editors for Neo4j. Are there any visual editors for JanusGraph as well? or can those tools be used with JanusGraph?
Answer: Per JanusGraph's README:

To visualize graphs stored in JanusGraph, you can use any of the
following tools:

AWS Graph Notebook - Open source
Cytoscape
Gephi
plugin for Apache TinkerPop
Graphexp
Graph Explorer
KeyLines by Cambridge Intelligence
Linkurious

Of these, at least Cytoscape and Graphexp also support visual graph editing.
Comment: @JamesWatkins I had the same problem: Gephi works as a server, and gremlin comes to be the client. So, I had to start Gephi on windows, as per http://tinkerpop.apache.org/docs/current/reference/#gephi-plugin , check firewall on both sides (mandatory!), start gremin on Linux, and follow the instructions there. When issueing ":remote connect tinkerpop.gephi", it says "...localhost...", so I just added ":remote config host 192.168.1.15", the windows-gephi-server IP.
Comment: I was not able to figure out how to use Gephi with JanusGraph; Linkurious seems promising but is not free.
Comment: @JamesWatkins — take a look at other options on https://github.com/JanusGraph/janusgraph/#visualization such as Cytoscape or GraphExp.
Comment: Sir . the quesiton requires editing, but your answer is mentioning  "visualize" only.  Or actually the five tools provided are all able to edit graph?
Comment: @LukAron – thanks, good point! I've added a clarification about at least two of the visualization tools that support editing (I couldn't easily find a page for the others that say that they also support editing; they may or may not).
Comment: @MishaBrukman   oh! sorry that I miss the last sentence! now I saw it!  thanks so much for your answer!   I have spent tons of time finding tools like this to edit neo4j also, and I found that graphileon is exactly what I want.

GUI for building JanusGraph DB


IIS URL Rewrite to querystring parameter

Question: I need to capture requests to folders that aren't file folders to be redirected to a default page with the requested path as the querystring.
For example, if the user requests
www.mysite.com/IT

I want it to redirect to
www.mysite.com/default.aspx?q=IT

However, if they request a page that is in a real sub-folder, that shouldn't be redirected. For example, requests to www.mysite.com/projects/new.aspx shouldn't try to do any re-directions as it's a physical folder and file in the site.
Answer: Use the IIS Rewrite module and the set it up in the web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirector" patternSyntax="Wildcard">
                    <match url="*" />
                    <conditions>
                        <add input="{PATH_INFO}" pattern="/*" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/default.aspx?q={PATH_INFO}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Comment: Awesome! That did it
Answer: You could define a 404 error page that would redirect to your default page with the "q=" that you desire


Angular 7 (click) is not working with [hidden] attribute

Question: Angular 7 (click) event is not working with [hidden] attribute, any reason for this?
this is my sample code

    <button *ngFor="let item of devices; let i= index" (click)="Save()" class="btn btn-danger">{{ item.serialNumber }}</button>

Comment: show save function
Comment: https://stackblitz.com/edit/angular-wi77wq

i just added a stackblitz that works fine with hidden. please share your save function
Comment: Toggle-able hidden and working:
https://stackblitz.com/edit/angular-dtwo8y
Comment: Save() {
     console.log('Save Button Clicked');
  }
Comment: Are you 100% sure it's because of the "[hidden]"? if you remove it, it works?
Comment: this is working without [hidden] tag
Comment: It may happen that you might not have defined `showdeviceDropDown` in your component
Answer: It works fine, you probably have a run time error some where else in your code. Debug in the browser or view the errors in the browser console to see why it is not working.
Working demo code
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  numOfClicks = 0;
  showdeviceDropDown = true;
  devices = [{serialNumber: '123'}];

  Save(){
    this.numOfClicks++;
  }
}

app.component.html

 Showing = {{showdeviceDropDown}}

   <button *ngFor="let item of devices; let i= index" (click)="Save()">{{ item.serialNumber }}</button>

 Number of clicks:{{numOfClicks}}

Answer: use ngIf instead of hidden as follows -

Comment: @SachilaRanawaka: Got it, I misunderstood the question
Comment: can u create a demo. Or edit this https://stackblitz.com/edit/angular-rdkyhp?file=src%2Fapp%2Fapp.component.ts
Comment: IT will, yes it will remove from DOM. but once the conditioned get true, it will be attached itself to the dom. it is much more lightweight to the DOM since we are removing element instead of hiding/
Comment: Not a good way. NgIf should be use only if u want to remove or add component in the dom element itself. If u simply want to hide/show the container better use [hidden]
Answer:
Logically your code is perfectly fine. There is nothing to change with your hidden also.Please ensure that you've defined your showdeviceDropDown variable in your component & you are checking your output in the console, because you printed your click event on the console.

Though, if you want to alter your hidden attribute, you could simply try replacing it with [class.d-none]. So your script will be -

    <button *ngFor="let item of devices; let i= index" (click)="Save()" class="btn btn-danger">{{ item.serialNumber }}</button>

Suggestion: Do use lower camel case for your methods & properties. Here's a reference to the official style guide.


Move location of drush

Question: Currently there is a .drush folder in my user profile on my mac. However, is it possible to move it into my git repo? How does one move it? If I drag and drop it, then run a drush command, the folder is remade in my user profile folder.
Comment: What are you specifically looking to move?  If it's just your Drush aliases, then they you can create a project specific `SITENAME.aliases.drushrc.php` and put it in the *sites/all/drush* folder of your Drupal install. [Source](http://drush.ws/examples/example.aliases.drushrc.php)
Comment: Hi, what does this file do?
Comment: If you don't know what it does, then chances are this isn't what you wanted to move.  Check out the source though (it's linked to in the above comment), it's a really neat feature.
Answer: No.  Your Mac is running a version of the Unix operating system.
Files and directories starting with a dot (.) in Unix are hidden system files/directories and should not be moved or deleted unless you know what you're doing.
Comment: .drush files can be moved. and for example, there is also a .gitignore file and this contains rules that need to be modified sometimes and could easily be moved to copy config.
Comment: I am not saying that you cannot move these files, only that you should not do this *unless* you know what you're doing *and* have a specific purpose in mind.  The way your question is written suggests that this is not the case here.

Move location of drush


Multiple protocols

Question: I'm declaring that my app delegate conforms to the protocols
<UIApplicationDelegate, SKPaymentTransactionObserver>
in the app delegate header file (i.e. the app delegate interface).
The compiler tells me at numerous places in my code that:
warning: type 'id <UIApplicationDelegate>' does not conform to the 'SKPaymentTransactionObserver' protocol
What's up?
Comment: Have you declared that your delegate conforms to the two protocols in both the class declaration and the variable declaration (or however you access the variable)?
Comment: @Bavarious: Only in the app delegate interface.
Comment: Post the code where you add the transaction observer.
Answer: You're probably doing something like [[SKPaymentQueue defaultQueue] addTransactionObserver:[[UIApplication sharedApplication] delegate]]. Since -[UIApplication delegate] is declared as returning id<UIApplicationDelegate>, the compiler has no way of knowing that the object returned will implement the SKPaymentTransactionObserver protocol. Try assigning the delegate to a variable that's statically typed as your delegate class and pass that to the method you're trying to call and the compiler should be happy.
Comment: @Bavarious, @Chuck: Adding a cast with the variable declaration seems to fix this, thanks!
Answer: Your ApplicationDelegate seems to be missing some of the required delegates, for example:
- (void)applicationWillResignActive:(UIApplication *)application

Check what delegate methods SKPaymentTransactionObserver requires, and add them to your AppDelegate.m
Also, check over the SKPaymentTransactionObserver Protocol Reference
Comment: @WrightCS: Thanks for your post. I can replace SKPayment... with any other protocol and still have the same problem. It's not this.

Multiple protocols


JSF ui:debug causing NullPointerException

Question: My JSF application has a <ui:debug> tag in the top level template.  I have a data item creation page that saves the item to the DB.  The DB then returns the saved item which I store in the JSF flash.  I then redirect to a new page and display the item that I retrieved from the flash.  When I view the page, the Debug page is shown with a NullPointerException and sometimes there is no stack trace and other times there is a stacktrace similar to (don't pay attention to the specific line numbers.
NullPointerException
at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:158)
at com.sun.facelets.util.DevTools.writeVariables(DevTools.java:144)
at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:135)
at com.sun.facelets.tag.ui.UIDebug.writeDebugOutput(UIDebug.java:92)
at com.sun.facelets.tag.ui.UIDebug.encodeBegin(UIDebug.java:81)
...

If I remove the <ui:debug> tag, then my page is successfully displayed.  What is causing the NullPointerException?
Answer: Part of the job of the <ui:debug> tag is to display the contents of all the various JSF scopes including the "flash scope".  DevTools.writeVariables is a helper function that is used to turn the objects in the scopes into something readable to display in the debug page.  It uses methods like toString() to display the objects.  The object that was stored in the flash, overrode the toString() method with the following boilerplate code
@Override
public String toString() {
  //TODO: Supply implementation
  return null
}

Since the toString() was returning null, it later caused a NullPointerException.  If you properly implement toString(), this error will not occur.


Restrict a string literal union based on an other string literal union

Question: I'm tracking user activity in my app and I'm using typescript to restrict which events can be tracked. First property is the name of the event, the second property (called params: {[key: string]: string | number | boolean | null}) is any combination of key value pair.
interface Event {
  name:
    | 'User Signup'
    | 'User Login'
    | 'User Logout'
    | 'Like Artwork'
    | 'Skip Artwork';
  params: {
    [key: string]: string | number | boolean | null;
  };
}

Then I can call my tracking function like so:
trackEvent('User Login', {
  user_id: 123
});

trackEvent('Like Artwork', {
  artwork_id: 456
});

Now I'd like to add some more constraints to prevent calling some events with the wrong set of params. For instance this should not be allowed:
trackEvent('User Login', {
  artwork_id: 456
});

Any idea how I can do this with typescript? Thanks in advance!
Comment: [Discriminated unions](https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions)
Answer: If you define a type that maps event names to their params like this:
type EventMap = {
    'User Login': { user_id: number };
    'Like Artwork': { artwork_id: number };
};

then one option is to use generics so that the params are associated with the given event:
function trackEvent<E extends keyof EventMap>(event: E, params: EventMap[E]) {
    /** epic implementation here */
}

and now you get the desired behavior:
trackEvent('User Login', {
  user_id: 123 // OK
});

trackEvent('Like Artwork', {
  artwork_id: 456 // OK
});

trackEvent('User Login', {
  artwork_id: 456 // ERROR
});

Playground


Understanding hypothesis testing-compare two series

Question: This is rather simple, but am always confused so perhaps somebody can explain me once for all.
I have two sets of observations coming from two sources that I want to compare, say $e_1$ and $e_2$ both having $N$ points. Three things could happen:
i) $e_1$ is consistently larger than $e_2$,
ii) $e_1$ is consistently smaller than $e_2$,
iii) there is no significant difference between $e_1$ and $e_2$.
I compute the difference in the two series, $e_1 - e_2$, create a test statistic. This could be for example the mean of  $e_1 - e_2$ divided by the standard deviation of  $e_1 - e_2$ (divided by square root of $N$), or it could be another test statistic. If I wanted to do the Diebold-Mariano test it would be another test statistic. But the point is I get some test statistic. I don't know how to interpret this statistic.
I know I need to compare it against say the extreme value of a $T$ distribution or Normal distribution. But know my test statistic could be >1.96, <-1.96, or between -1.96 and 1.96. Help me understand how to interpret the three cases to answer the question- which is consistently larger, $e_1$ or $e_2$.
Comment: This looks like a very generic question on hypothesis testing and it is likely a duplicate of one or more older threads on the topic. Other than the sketch in my answer, I suggest consulting an introductory statistics textbook.
Answer:
I don't know how to interpret this statistic.

The test statistic is a summary characteristic of your dataset. It can be treated as a realization from a random variable with a known distribution under a point-valued null hypothesis $H_0$ or a known family of distributions under a set-valued $H_0$. You can evaluate how atypical this realization looks from the perspective of the null distribution(s). Taking the distributions under the alternative hypothesis $H_1$ into consideration, this yields a $p$-value that you can compare to a predetermined significance level $\alpha$ to help you decide between keeping (i.e. not rejecting) $H_0$ or rejecting it in favour of $H_1$.
Alternatively, you can refer to the critical values that again are determined by the distribution(s) under $H_0$ in light of $H_1$. Compare your test statistic to them and see if the statistic is in a critical region (suggesting to reject $H_0$ in favour of $H_1$) or not (suggesting not to reject $H_0$).


What does menu blocks "Block title as link" option do?

Question: The Menu block module has an option on each block titled "Block title as link". My interpretation of the description combined with some testing, is that if I leave the "Block title" setting out, the name of the menu will be used as the block title, and it should also be a link to the currently viewed menu item.
However, when I check the option and try it, I never get any links. I've been trying to follow the code, but I couldn't make any sense of it, so I believe my expectation on the functionality is wrong.
Comment: Block title as link: An overridden block title will not be a link.
Did you override block title?
Comment: Good catch, but the question text explicitly says I did not override it. :) If I wasn't clear on that point, please suggest an edit.
Answer: For me it doesn't show the menu name, but the link to the parent menu item of the block. IE with a menu hierarchy like this:

-- a
---- b
------ c
------ d
------ e
-- f
-- g

If the block looks like this:
Title
c
d
e

Then having the block title as link makes the title a link to menu item b.


nonlinear regression two equivalent models on paper, but different estimated parameters

Question: I measured one response variable
Y1

as a function of two measured independent variables
X1 and X2

It is common practice in my field of research to transform Y1 into another response variable:
Y2 = 10*X1 * ((-1)+1 / Y1)

Then to fit the following model (M1):
Y2 ~ a * X1^b * X2^c + error

in order to estimate (a, b, c).
However, it is also possible to estimate these three parameters witout transforming Y1 into Y2, using this equivalent model (M2) (equivalent on paper at least):
Y1 ~ 10*X1 / (a * X1^b * X2^c + 10*X1) + error

The problem is, these two ways of estimating (a, b, c) yields different estimates. I personnally believe M2 is to be preffered over M1, because calculating Y2 from Y1 and X1 introduces uncertainties from X1 into the nonlinear response variable.
I wish / need to know which model (M1 or M2) to use, and why.
Thanks in advance.
PS: I did not present any reproducible example nor graphs because I think this question is sufficiently general and clear, and does not need it. Thanks for your understanding.
Comment: It'd probably help to write out the models properly *with error terms*.
Comment: Now I understand the relevance of the very first comment.
Comment: Sorry I'll add error terms.
Answer: Model 2 is
$$Y_1 = \frac{10 X_1}{a X_1^b X_2^c + 10X_1} + \delta$$
whereas Model 1 is
$$10X_1 \left(-1 + \frac{1}{Y_1}\right) = a X_1^b X_2^c + \varepsilon,$$
which can be solved for $Y_1$ to read
$$Y_1 = \frac{10 X_1}{a X_1^b X_2^c + 10X_1 + \varepsilon}.$$
Implicitly it is supposed the errors $\varepsilon$ or $\delta$, as the case may be, are independent, have identical distributions, and are centered at zero.
To compare the two models let's assume the variability of $\varepsilon$ is substantially less than the magnitude of $aX_1^bX_2^c + 10X_1$.  We may then use the Binomial Theorem (or, equivalently, a Taylor series) to approximate the right hand side of Model 1 (to first order in $\varepsilon$) as
$$Y_1 \approx \frac{10 X_1}{a X_1^b X_2^c + 10X_1}\left(1 - \frac{\varepsilon}{a X_1^b X_2^c + 10X_1} + \cdots\right).$$
Comparing to Model 2, we see the difference between them lies in the error terms:
$$\delta \approx \frac{-10 X_1}{\left(a X_1^b X_2^c + 10X_1\right)^2} \varepsilon.$$
These are different models because if the $\varepsilon$ have identical distributions, the $\delta$ cannot--since they rescale the $\varepsilon$ by factors that depend on the variables $X_1$ and $X_2$. Conversely, if the $\delta$ have identical distributions then the $\varepsilon$ cannot.
To decide which one to use (if either), you will need additional information concerning the distributions of the errors.  This can be obtained in many ways, including

Theoretical considerations.  For instance, if the error is intended to represent measurement variability of $Y_1$ and that variability is known to be (roughly) constant across a range of values of $Y_1$, the Model 2 is a good choice.
Analysis of repeated measurements.
Review of diagnostic information from each model (related to the possible heteroscedasticity of the residuals).

The red curves show the correct underlying relationships.  The dots show simulated data.  Their vertical deviations from the red curves represent the errors. The dispersion of the errors in Model 1, at the left, visibly varies with the independent variables. The dispersion in Model 2, at the right, does not.
This figure shows data simulated with the R code below.  To simplify the presentation, all values of $X_2$ were set to a constant value, causing all variation in $Y_1$ to be associated with variation in $X_1$ only.  This simplification does not change the nature of the differences between the two models.
a <- 1
b <- 2
c <- 3
n <- 250
sigma <- 2
#
# Generate data according to two models.
#
set.seed(17)
x1 <- rgamma(n, 2) + 1
x2 <- rep(1, n)
epsilon <- rnorm(n, sd=sigma)
y.m1 <- 10 * x1 / (a * x1^b * x2^c + 10*x1 + epsilon)

# (Make them have comparable errors on average.)
tau <- mean(abs(-10 * x1 / (a * x1^b * x2^c + 10*x1)^2))
delta <- rnorm(n, sd=tau)
y.m2 <- 10 * x1 / (a * x1^b * x2^c + 10*x1) + delta
#
# Plot the simulated data.
#
reference <- function() curve(10 * x / (a*x^b + 10*x), add=TRUE, col="Red", lwd=2)
par(mfrow=c(1,2))
plot(x1, y.m1, main="Model 1", xlab="X1", ylab="Y1", col="#00000070")
reference()
plot(x1, y.m2, main="Model 2", xlab="X1", ylab="Y1", col="#00000070")
reference()

Comment: You're almost correct. We have no basis to suppose the errors will have Normal distributions, and in practice this is rare. The models that are simplest to interpret and mathematically most tractable tend to be those with nearly *symmetrical* distributions of residuals that have nearly identical shapes and dispersions across all values of the independent variables. Although there is no guarantee, often it is the case that a model with one of these characteristics (symmetry or homoscedasticity) also has the other one, at least to a practical approximation.
Comment: This is really just an elaboration of @Scortchi's comment on the question.
Comment: Thank you very much ! I would not have been able to elaborate on @Scortchi's comment, but now I understand it and why it was so relevant. Now, I know WHY I must choose one model (because these two models are fondamentally different from each others), and HOW to choose the right model for my datas. If I understand you well, residuals estimated by one of the two models will be normally distributed, while residuals estimated from the other model will not. And I should choose the model with normally distributed residuals.
Answer: There are NOT equivalent least squares models. The error in one model is a transformation of the error in the other. Whichever model's error is closer to being Normally distributed should be the better model.  Edit: see whuber's answer for more elaboration on the error transformation.
There is a second matter as well, and that is the question of what numerical solution is obtained by the nonlinear least solution algorithm. The solution obtained can depends on the algorithm used to solve it as well as the starting value (initial guess) for the parameters being estimated. Depending on the algorithm and starting value, it's possible that the algorithm will exit without finding a local optimum. It's possible that a local optimum will be found which is not the global optimum.
You should want the globally optimal solution.  Whether you find it is another matter.  That's where it helps to know what you're doing in nonlinear optimization, which unfortunately most people performing nonlinear least squares don't.
Comment: I edited out my statement about the errors being reciprocal - see whuber's answer for more info on that. Regarding normality of errors, see whuiber's answer, go back to first principles on the model if possible, and also do this https://en.wikipedia.org/wiki/Normal_probability_plot .
Comment: I use R's nlsLM function from minpack.lm package for nonlinear parameter estimation using Levenberg-Marquardt fitting algorithm because it is said to be more robust against bad starting values. Also because the starting values are common estimated values from previous experiments, and finally because L-M algorithm gives equal estimates to default Gauss-Newton algorithm from the more conventional nls function in stats package.
Comment: The greater robustness of Levenberg-Marquardt (L-M) vs. Gauss-Newton (G-N) pertains to finding a local minimum. There really is not an improved robustness to finding a global minimum, other than that at least L-M has a better chance of finding a local minimum, which may turn out to be a global minimum. than G-N. They both make the same approximation to the Hessian, based on J'*J, where J is the Jacobian, for a model in the form 1/2* (sum squared residuals). If you this in BARON http://www.minlp.com/ , you can be reasonably assured of getting a global minimum if it claims to have found it.
Comment: If you don''t use a global optimizer such as BARON, your best prospects are if you use a trust region Newton's algorithm, such as in KNITRO http://www.ziena.com/knitro.htm with hessopt 1 (not the nonlinear least squares routines under MATLAB using L-M or G-N). Even then, you need to use multiple starting values, and there is no guarantee you will find the global optimum. The most robust algorithms for nonlinear least squares (which I referenced above) are not specialized to nonlinear least squares, rather, they are general purpose nonlinear optimizers.
Comment: Thank you very much for your answer. I understand now these models are not equivalent, although I do not quite so well understand what does it mean for an error term to be the reciprocal of another one. However, you give me a way to choose between these two models (the one whose residuals are closer to being normally distributed). However, if I know how to test residuals for normality, I don't know, currently, how to compare two sets of residuals and choose the most normal one. Any advice, keyword, is welcome.
Comment: About the second part of your answer, I have reasonable certitude that both model's estimated parameters are their respective global optimum.
Comment: What is the basis for your reasonable certitude that both model's estimated parameters are their respective global optimum?  What software/algorithm did you use to find the optimum? Did you use a "rigorous" global optimizer (not for instance the cr@p in the MATLAB global optimization toolbox), or at least a semi-rigorous global optimizer such as BARON?  Did you use multiple starting values?  Did you use the solution from one model as a starting value to solve the other model?
Comment: The convergence criteria http://cran.r-project.org/web/packages/minpack.lm/minpack.lm.pdf look unreliable. Nevertheless, it's quite possible the algorithm converged to a local optimum. The fact that you got the same solution with different but closely related algorithms, as well as using starting values from previous experiments, should NOT provide any confidence you are finding the global optimum, as opposed to a local optimum. Also note that LM and Gauss-Newton both make the same approximation to the Hessian which is valid only if the residuals at the (local) optimum are sufficiently small.
Comment: The previous experiments may have all found the same local, but not globally optimal solution.  So when you provide similar data and start in the same region, you may get basically the same local optimum which is not  the global optimum.  I am not saying however that you did not find the global optimum - just that you shouldn't have "reasonable certitude" based on the reasons you provided.


Appache solr results multi sites search sorting based on specific feild

Question: i have four sites and use appache solr module to execute search indexing in these four sites ,
all is ok with me , but the results sort not as i want
i want to sort by fields ......for ex:- get all result that have [field 1] first , then get all results that have [ field 2] ....
in my case , i have some results from [ field 1] then [ field 2] then [field 1] , ....
for note (my field type is taxonomy term and all contents in all four sites are the same )
Thanks
Comment: i used all available modules and edited in Bias , but still the sorting not as i want , i think there is specific case for taxonomy terms
Answer: A quick Solution may be to use Apache Solr Sort Module.

Per environment setting of grouping per content type/taxonomy/field
  value/... Allow for a custom amount of items per group (global)

But the best way is to setup a Apache Solr View :
Search API, has a Solr integration module, which will allow you to setup a View with exposed filters and full text search. With the flexibility of Views, you should be able to setup the view to allow the users to sort as they wish.


How can I change the items order in a pulldown list of filter in views?

Question: Hello Drupal community.
I have a question about Views.
I have a "People" view with a role filter and the pulldown list for this filter look like this:

Administrator
Editor
Approver

I would like to know if it is possible to change the order of items in the pulldown list to make it look, for example, like this:

Administrator
Approver
Editor

Thank you.
Answer: I guess you could probably change the roles order in People>Permissions>Roles (simple drag and drop) and the Views filter will use it by default.
Comment: Thank you very much! It works. It turned out to be more simple than I thought.


Quantum Scalar i80 - I see the tape drive, but not changer

Question: I have a Quantum Scalar i80 connected via SAS to a CentOS 6 server. When I look in /proc/scsi/scsi, I see the tape drive, but I don't see the changer.
From /proc/scsi/scsi:
Host: scsi8 Channel: 00 Id: 01 Lun: 00
  Vendor: IBM      Model: ULTRIUM-HH6      Rev: E6R7
  Type:   Sequential-Access                ANSI  SCSI revision: 06

In /dev, I see nst0, nst0a, nst0l, and nst0m (and also the st0 variants.) I don't see anything that appears to be a changer. Any ideas as to what I'm missing here?
Comment: What does the Control Path configuration in the i80 Setup menu show?
Comment: The i80 has 2 drives. The tapes are divided into 2 partitions. In the Control path, one partitions is assigned to one drive, and the other partition to the other drive. One drive is cabled up to the server in question. The other drive is cabled up to a Windows server.
Answer: I know this is an ancient post , but the changer will be accessed by an 'sg' device , ie scsi generic. If you are running Linux , it will already have sg support.
sg provides a lower level interface to SCSI devices than st. st is for scsi tapes.
you need to install the mtx command line tool as well to control the media changer device.
sg devices for the tape drives will be created as well
system messages will show the devices , with a type '1' being a tape device , a type '8' being a media changer , a type '0' being a disk ...
see sg2 , is type 8 and it's a 'ch' ....
[    6.145348] st 2:0:0:0: Attached scsi generic sg0 type 1
[    6.145449] st 4:0:0:0: Attached scsi generic sg1 type 1
[    6.145542] ch 4:0:0:1: Attached scsi generic sg2 type 8
[    6.145696] st 5:0:0:0: Attached scsi generic sg3 type 1
[    6.146526] st 6:0:0:0: Attached scsi generic sg4 type 1
[   43.891764] scsi 7:0:0:0: Attached scsi generic sg5 type 0
[   43.898283] scsi 7:0:1:0: Attached scsi generic sg6 type 0
[   43.908209] sd 7:0:2:0: Attached scsi generic sg7 type 0
[   43.918352] sd 7:0:3:0: Attached scsi generic sg8 type 0
[   43.930734] sd 7:1:0:0: Attached scsi generic sg9 type 0


ESXi Server with 12 physical cores maxed out with only 8 cores assigned in virtual machines

Question: I have an ESXi 5 server running on a 2-processor, 12-core system with hyperthreading enabled. So: 12 physical cores, 24 logical ones. On this server are 4 Windows 7 VMs, each configured for 2 processors, each running VMware Tools.
Looking at my stats in vSphere, my "core utilization" is constantly maxed out. Yes, these machines are working hard, but only 8 cores have been allocated. How is this possible? Should I look into reducing the processor count per machine as in this post: VMware ESX server?
I checked to ensure that hardware virtualization is enabled in the BIOS of the machine (a DELL R410). I've also started reading up on configuration, but being a newbie there's a lot of material to catch up on. It also seems I should only bother with advanced settings and pools if I'm really pushing the load, and I don't think that I should be pushing it with so few VMs.
I suspect that I have some basic, incorrect configuration setting, but it's also possible that I have some giant misconceptions about virtualization. Any pointers?
EDIT:
Given the responses I've gotten so far, it seems that this is a measurement problem and not a configuration problem, making this less critical. Perhaps the real question is: How does the core utilization of the server reach a higher percentage than all individual cores' core utilization, and given that this possibility makes the metric useless for overall server load, what is the best global metric for measuring CPU load on hyper-threaded systems?
Comment: What version of ESX?
Comment: ESXi 5 (I added it to the original post, thanks)
Comment: Have you installed VMware Tools into the guest OS (Win7)?
Comment: Yes, that as well. I have to get better at this specifying my environment thing.
Answer: Even though you have 2 x 6-core CPUs with Hyperthreading enabled, I think this is a matter of how you're monitoring the system. Are you doing this from the vSphere client or are you using esxtop? See this post on the VMWare forums.
Also, if you're looking at esxtop, see the CORE UTIL portion of this VMWare document.
If hyperthreading is enabled, the CORE UTIL% field will also appear, which displays only the utilization percentage of each core and not the individual threads. So if a host has eight cores and 16 threads, it displays only the eight-core values, and if only one thread of a core is at 100% utilization, the core will show as 100% utilized. This gives you a view of core utilization as a whole regardless of thread utilization.
Are you saying that the machines are working hard? What does the CPU Usage on the vSphere client console look like? How does the load vary with Hyperthreading off?
Comment: I'll see what turning hyperthreading off will do. I am using vSphere and checking out the "Core Utilization" metric, which I believe corresponds to the CORE UTIL% metric in esxtop.
Answer: Overall host core utilization metric will often hit 100% on lightly loaded hosts. I don't have any document that would confirm it, but my hunch is that this metric is based on whether or not any core on the host is used at any given moment.
Instead, select all numbered objects in the Customize Performance Chart dialog box. Those are individual cores, and the results will reflect their actual load.
Better yet, ignore this metric altogether if you want to see overall host load. The Usage value, which is the default in the performance graph for CPU, will tell you exactly what you're looking for.
Comment: When hyperthreading is enabled, it seems like CORE UTIL is the appropriate metric. See: http://communities.vmware.com/docs/DOC-9279, section 2.3. That being said, it looks like if I look at the core utilization for individual cores, they max out around 80%. That doesn't seem to match what the linked document states for CORE UTIL: "The percentage of CPU cycles per core when at least one of the PCPUs in this core is unhalted, and its average over all cores. It's the reverse of the "CORE IDLE" percentage, which is the percentage of CPU cycles when both PCPUs in this core are halted."
Comment: If you have 12 cores and allocate 8 cores total to VMs that run at 100% CPU utilization, the 80% result is about right (allowing for hypervisor overhead). I'm not sure what your objection is. And note that your original issue was with the metric as applied to the host as a whole, which is not discussed in the referenced document, and I can only reiterate that the number you see is pretty much useless. Look at CPU Usage value, like I and ewwhite suggested previously. Hyperthreading is a red herring, you should keep it enabled.
Comment: I'm not objecting to the 80%, I'm objecting to every core being at about 80%, but the overall number being reported at 100%, which is what threw me off (it's useless, you guys are right). I just won't look at it any more. I think this answers my question.


Why is Math.round() not rounding up when the value is 0.5?

Question: Currently have a formula which essentially looks like this:
Math.round((20.49-14.99)*5);

The resulting number is 27.5 and AFAIK, Math.round() should round UP if the number is 0.5 and above and round down if it is below. Unless I've misunderstood, I was assuming the result would be rounded up to 28 (as it does in Microsoft Excel, for example), but instead, it rounds down to 27.
At first I assumed it had something to do with my code, but when I console logged it, the result was exactly the same.
Am I missing something and if I have, how would I round-up from 0.5 onwards?
Comment: @ruby_newbie It's not "because JavaScript", it's because of that's how IEEE-754 floating-point values works.  Other languages will exhibit this same behavior.
Comment: Well, it's fun when it comes to js math, check the result of `20.49-14.99`, you can jsut type it in console and see the magic
Comment: (20.49-14.99)*5 = 27.499999999 which rounds to 27 because of how IEEE-754 floating values are handled.
Comment: `(20.49-14.99)*5` evaluates to `27.499999999999`, not `27.5`, due to how floating point values are handled.
Comment: This occurs because `14.99` and `20.49` cannot be represented exactly in binary.
Comment: Thanks for pointing this out. I always assumed it just rounded up.

This totally answers the question for me.

For anyone that stumbles upon a similar issue, the solution that worked for me is adding `myvariable.toFixed(2)` which rounds up and gives me the desired result!
Answer: Due to the way floating points work, the result of (20.49-14.99)*5 is actually 27.499999999999993, which is closer to 27 than 28
Answer: (20.49-14.99)*5 is 27.499999999999993, so JavaScript will round this to closer value. You should use Math.ceil to round above value
Comment: Currently using this formula to display prices and ceil(or floor) is not going to accurately represent the Excel Spreadsheet I recieved. Personally, I have no issues with it rounding accurately (which it is), but apparently, the disparency with the Spreadsheet is an issue.


Installing OpenCPU on Amazon Linux instance

Question: I have a an Amazon Linux instance which uses R to write a function to do some data analysis. I now want to call this function using a REST API. It seems that OpenCPU can get the job done for me.
I have found links to install OpenCPU on Ubuntu, but is there an easy to way to install it on Amazon Linux instance.
PS: I tried
sudo yum -y install opencpu
sudo yum -y install opencpu-server
It says no such package available.
Answer: I managed to get it installed from this script https://github.com/jeroenooms/opencpu-server/blob/master/rpm/buildscript.sh.
Answer: We now have rpm packages for CentOS 6 and 7 at https://archive.opencpu.org/


level of significance for binary data

Question: I'm trying to measure the level of significance of devoiced vowels in different linguistic contexts; whether or not certain contexts cause vowels to be devoiced.
Basically my data right now is a table with the contexts in the horizontal line (4 of them), and speaker on the vertical (speakers are not a variable, I just keep it for identification).
The values filled in are yes or no (I left "no" as blanks), meaning yes this context caused devoicing , or no (no value) this context doesn't devoice this vowel.
How can I calculate level of significance of my data? If the result was actually significant due to different contexts or not.
This is the last part of my results chapter but I'm having trouble figuring it out.
Comment: Was each context tested with different speakers? Than that would be a contigency table and a Chi-square-test of independence. I guess however, that the same speakers were examined in all 4 contexts, thus resulting in dependent samples. See https://en.wikipedia.org/wiki/Cochran%27s_Q_test whether Cochran Q test fulfills your requirements (let "context" be "treatment" and "block" be "speaker").
Comment: no the same speakers for tested four times. im reading about Cochran Q test  and it seems right, thank you.
Answer: If your observations are independend, i. e. each speaker in only one condition, than you can count your observations and build a contingency table like this (assuming, it's all about the moon)
<code>                      Moon
          full   waning   new   waxing
        +------+--------+-----+-------
voiced  |  15  |   22   |  10 |   13
devoiced|  10  |   12   |  11 |    7
</code>
A Chi-Square-Test of independence could answer the question, whether voiced/unvoiced has anything to do with the moon phase. The Q-Test I mentioned in the comments is for dependend samples, Chi-Square is vor independend samples.

level of significance for binary data


Polimorfismo em Typescript

Question: Estou criando minha aplicação com a seguinte estrutura: Uma classe abstrata Pessoa, e herdando de Pessoa, as classes PessoaFisica e PessoaJuridica, E por fim, uma classe Cliente recebendo Pessoa como um atributo.
abstract class Pessoa {
    id?: number;
    nome?: string;
}

class PessoaFisica extends Pessoa {
    public cpf?:string;
    public rg?:string;
    public sexo?:string;
    public profissao?: string;
    public data_nascimento?: Date

}

class PessoaJuridica extends Pessoa{
    public razaoSocial?: string;
    public cnpj?: string;
    public ie?: string;
    public im?: string;
}

class Cliente{
    id?: number;
    pessoable?: Pessoa;

}

O problema esta sendo que quando instancio a classe desta forma:
let c:Cliente = new Cliente();
c.pessoable = new PessoaFisica();

Eu não consigo ter acesso no atributo cpf  utilizando c.pessoable.cpf.
Onde estou errando?
Comment: `pessoable` é do tipo `Pessoa` que só tem duas propriedades `id` e `nome`. Para acessar o propriedade `cpf` do seu objeto `PessoaFisica` você tem fazer o type casting `(c as PessoaFisica).cpf ="000000000";` ou então `( c).cpf ="000000000";`
Answer: Cast
Até onde eu sei (pode ser que tenha uma solução que eu desconheço no TS) não tem solução boa, assim funciona:
abstract class Pessoa {
    id?: number;
    nome?: string;
}

class PessoaFisica extends Pessoa {
    public cpf? : string;
    public rg? : string;
    public sexo? : string;
    public profissao? : string;
    public data_nascimento? : Date

}

class PessoaJuridica extends Pessoa {
    public razaoSocial? : string;
    public cnpj? : string;
    public ie? : string;
    public im? : string;
}

class Cliente {
    id? : number;
    pessoable? : Pessoa;
}

let c : Cliente = new Cliente();
c.pessoable = new PessoaFisica();
let x = c.pessoable as PessoaFisica;
x.cpf = "123";

Só fazendo o cast o código tem acesso ao campo daquele tipo, porque foi declarado que pessoable (que nome feio) é do tipo Pessoa e este tipo não tem um campo chamado cpf. Mesmo que seja colocado um tipo concreto que exista para o compilador só vale o tipo explicitado na declaração. Fazendo o cast o tipo passa ser PessoaFisica e aí tem acesso so campo que deseja. E não vi uma forma de fazer tudo em um passo só.
Se fizer as devidas abstrações isso pode não ser tão feio porque esconderia essa "gambiarra" dentro da classe.
Dois campos
Uma solução que poderia ser: ter dois campos um para cada tipo e aí usa só um deles conforme o tipo (no final vou mais fundo nessa ideia). Isso tem um outro problema de vazamento de abstração, mas essa classe toda está cheia de vazamentos de abstração, por isso tudo é anulável.
Se a abstração for certa não é problema seu, consumidor da classe, saber que tem que fazer isso, você terá um meio de acessar esse dado de forma natural.
Tipo genérico
Uma solução pouco melhor seria parametrizar o tipo porque afinal ou o cliente é pessoa física ou jurídica, não faz sentido aceitar os dois em campos separados ou juntos.
abstract class Pessoa {
    id?: number;
    nome?: string;
}

class PessoaFisica extends Pessoa {
    public cpf? : string;
    public rg? : string;
    public sexo? : string;
    public profissao? : string;
    public data_nascimento? : Date

}

class PessoaJuridica extends Pessoa {
    public razaoSocial? : string;
    public cnpj? : string;
    public ie? : string;
    public im? : string;
}

class Cliente<T> {
    id? : number;
    pessoa?: T;
}

let c : Cliente<PessoaFisica> = new Cliente();
c.pessoa = new PessoaFisica();
c.pessoa.cpf = "123";

Veja funcionando no repl.it. E no TS Playground. Também coloquei no GitHub para referência futura.
Ter que criar o objeto da pessoa na mão assim é um enorme vazamento de abstração. Uma das coisas que precisaria mudar é ter construtor do objeto.
Dois tipos de cliente
Outra possibilidade é criar uma classe que seja ClienteFisica e outro tipo ClienteJuridica, mas não acho que seja melhor que a versão genérica, a não ser que as operações de cada um seja muito diferente. Para tomar a decisão correta precisa conceituar tudo direito. Sem saber para onde quer ir, qualquer lugar serve. Isso pode trazer vários problemas em outros pontos, mas não quero descartar nada.
Aceitar a dinamicidade do dado
Mais uma possibilidade é usar o tipo any ali. Desta forma tudo seria acessível, porém está programando em JavaScript e não TypeScript. Por que adotar uma linguagem se vai usar recursos para contorná-la? Tem caso para usar, mas quase sempre é abuso. Fazendo isso tudo seria acessível, até o que não pode acessar.
Tipo em união
Uma outra forma que vejo possível que é um pouco melhor mas sofre do problema do any, em menor grau. Pode usar um union type, algo assim:
pessoa? : PessoaFisica | PessoaJuridica;

O problema é que se criar uma pessoa física e tentar acessar um CNPJ o código aceitará, mas na execução quebrará. É uma solução, mas te obriga ser cuidado o tempo todo.
Sem herança
Uma alternativa seria não fazer a herança. Não estou dizendo que é bom sempre, mas poderia ter só a Pessoa e não diferenciar se é física ou jurídica. Já que você tem tantos membros anuláveis, coloca os membros de ambos no mesmo objeto e só preenche uma parte dos membros dependendo do tipo de pessoa é, o que deve ter um campo para isso. Claro que você não poderá acessar todos os campos sempre porque alguns não estarão preenchidos, é igual ao union type, mas aqui vai consumir mais espaço, apesar de simplificar por ter apenas um tipo. Pode fazer sentido porque em muitos casos o tipo da pessoa é só um detalhe. Se fizer boas abstrações não terá que lidar com isso no consumo. E uma alternativa é ter objetos auxiliares só para manter os dados que variam de acordo com o tipo, assim não precisa ter campos nulos.
Outras questões
Por que tem classe que preferiu explicitar a visibilidade pública e outras preferiu deixar o default?
Por que uma pessoa tem nome? Todas tem? Ok, pose ser, mas por que PessoaJuridica tem razaoSocial? Não é o mesmo que o nome? Se não é, precisaria justificar.
Desenvolver software não é só codificar, é pensar corretamente sobre o problema. Não pense que listei todos os problemas desse código.
Pode pensar o que isso te ma ver com o foco da pergunta. Quando conceitua certo talvez nem tenha esse problema.
Legal que montou a herança e composição do jeito certo, poucas pessoas fazem isso. Ou quase. Não é que esteja errado, mas pode inverter a questão. Se o principal for a pessoa e o cliente for considerado um dado secundário para a pessoa, isso pode ficar melhor. E aí faria sentido ter vários campos de papéis porque a pessoa pode realmente ter vários papéis. Não estou afirmando nada, depende do que precisa.
Tudo são alternativas. Não sei qual é melhor para você.
Comment: @MarcosLibanori eu estava pensando se TS tinha algo do tipo e achei até que tinha por ter sido criado pelo Anders, essa parece ser uma forma viável sim, mas ainda pode ter um problema de modelagem, como eu disse, depende do caso. No fundo o que ele está fazendo é um *cast* de forma assegurada. Não sei se precisa ser tão complexo assim, eu acho que não, pelo menos deveria poder ser mais simples.
Comment: você abriu muito meu horizonte de ideias e me ajudou até mesmo com outros problemas em outras modelagens. Estudando um pouco mais afundo o typescript encontrei a ferramenta typeguards que poderia ajudar nesse caso.
`let documento:string = (c.pessoa.isPessoaFisica()) ? c.pessoa.cpf : c.pessoa.cnpj`
Comment: `public isPessoaFisica() : this is PessoaFisica{
        return 'cpf' in this;
    }`

Polimorfismo em Typescript


apt upgrade error : The following packages have unmet dependencies:

Question: I'm trying to do an apt update apt upgrade but I'm having an error.
terminal error
"The following packages have unmet dependencies:
dpkg : Breaks: libapt-pkg5.0 (< 1.7~b) but 1.6.14 is to be installed
E: Broken packages
"
I have already tried a lot of things like clean, autoclean,  dpkg --configure -a... but nothing helped me.
Thanks,
Regards.
Answer: In my organization, this error was solved with this sequence:
<code># apt remove ubuntu-advantage-tools
# apt install -f
# apt update
# apt upgrade
</code>
A reboot after these steps should complete the updates.
The outputs are below:
<code>root@host:~# **apt remove ubuntu-advantage-tools**
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  distro-info
Use 'apt autoremove' to remove it.
The following packages will be REMOVED:
  ubuntu-advantage-tools ubuntu-minimal ubuntu-server update-manager-core update-notifier-common
0 upgraded, 0 newly installed, 5 to remove and 9 not upgraded.
After this operation, 4812 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 94121 files and directories currently installed.)
Removing ubuntu-server (1.450.2) ...
Removing update-notifier-common (184.108.40.206) ...
Removing update-manager-core (1:20.04.10.10) ...
dpkg: warning: while removing update-manager-core, directory '/var/lib/update-manager' not empty so not removed
Removing ubuntu-minimal (1.450.2) ...
Removing ubuntu-advantage-tools (27.8~20.04.1) ...
Processing triggers for man-db (2.9.1-1) ...

root@host:~# **apt install -f**
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  distro-info
Use 'apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.

root@host:~# **apt update**
Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 https://download.docker.com/linux/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
9 packages can be upgraded. Run 'apt list --upgradable' to see them.

root@host:~# **apt upgrade**
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  distro-info
Use 'apt autoremove' to remove it.
The following packages will be upgraded:
  ca-certificates cloud-init containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin
  intel-microcode snapd
9 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 145 MB of archives.
After this operation, 5364 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 ca-certificates all 20211016~18.04.1 [144 kB]
Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.6.6-1 [28.1 MB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 snapd amd64 2.55.5+18.04 [35.6 MB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:20.10.17~3-0~ubuntu-focal [40.6 MB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 cloud-init all 22.2-0ubuntu1~18.04.3 [493 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 intel-microcode amd64 3.20220510.0ubuntu0.18.04.1 [4063 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:20.10.17~3-0~ubuntu-focal [21.0 MB]
Get:8 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:20.10.17~3-0~ubuntu-focal [8171 kB]
Get:9 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-compose-plugin amd64 2.6.0~ubuntu-focal [6560 kB]
Fetched 145 MB in 4s (36.3 MB/s)
Preconfiguring packages ...
(Reading database ... 93863 files and directories currently installed.)
Preparing to unpack .../0-ca-certificates_20211016~18.04.1_all.deb ...
Unpacking ca-certificates (20211016~18.04.1) over (20210119~20.04.2) ...
Preparing to unpack .../1-containerd.io_1.6.6-1_amd64.deb ...
Unpacking containerd.io (1.6.6-1) over (1.6.4-1) ...
Preparing to unpack .../2-docker-ce-cli_5%3a20.10.17~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-cli (5:20.10.17~3-0~ubuntu-focal) over (5:20.10.16~3-0~ubuntu-focal) ...
Preparing to unpack .../3-docker-ce_5%3a20.10.17~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce (5:20.10.17~3-0~ubuntu-focal) over (5:20.10.16~3-0~ubuntu-focal) ...
Preparing to unpack .../4-docker-ce-rootless-extras_5%3a20.10.17~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:20.10.17~3-0~ubuntu-focal) over (5:20.10.16~3-0~ubuntu-focal) ...
Preparing to unpack .../5-docker-compose-plugin_2.6.0~ubuntu-focal_amd64.deb ...
Unpacking docker-compose-plugin (2.6.0~ubuntu-focal) over (2.5.0~ubuntu-focal) ...
Preparing to unpack .../6-snapd_2.55.5+18.04_amd64.deb ...
Unpacking snapd (2.55.5+18.04) over (2.54.3+20.04.1ubuntu0.3) ...
Preparing to unpack .../7-cloud-init_22.2-0ubuntu1~18.04.3_all.deb ...
Unpacking cloud-init (22.2-0ubuntu1~18.04.3) over (22.1-14-g2e17a0d6-0ubuntu1~20.04.3) ...
Preparing to unpack .../8-intel-microcode_3.20220510.0ubuntu0.18.04.1_amd64.deb ...
Unpacking intel-microcode (3.20220510.0ubuntu0.18.04.1) over (3.20210608.0ubuntu0.20.04.1) ...
Setting up cloud-init (22.2-0ubuntu1~18.04.3) ...
Installing new version of config file /etc/cloud/cloud.cfg ...
Setting up intel-microcode (3.20220510.0ubuntu0.18.04.1) ...
update-initramfs: deferring update (trigger activated)
intel-microcode: microcode will be updated at next boot
Setting up ca-certificates (20211016~18.04.1) ...
Updating certificates in /etc/ssl/certs...
rehash: warning: skipping ca-certificates.crt,it does not contain exactly one certificate or CRL
7 added, 8 removed; done.
Setting up containerd.io (1.6.6-1) ...
Setting up docker-compose-plugin (2.6.0~ubuntu-focal) ...
Setting up docker-ce-cli (5:20.10.17~3-0~ubuntu-focal) ...
Setting up docker-ce-rootless-extras (5:20.10.17~3-0~ubuntu-focal) ...
Setting up snapd (2.55.5+18.04) ...
Installing new version of config file /etc/apparmor.d/usr.lib.snapd.snap-confine.real ...
snapd.failure.service is a disabled or a static unit not running, not starting it.
snapd.snap-repair.service is a disabled or a static unit not running, not starting it.
Setting up docker-ce (5:20.10.17~3-0~ubuntu-focal) ...
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for rsyslog (8.2001.0-1ubuntu1.3) ...
Processing triggers for systemd (245.4-4ubuntu3.17) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for dbus (1.12.16-2ubuntu2.2) ...
Processing triggers for initramfs-tools (0.136ubuntu6.7) ...
update-initramfs: Generating /boot/initrd.img-5.4.0-110-generic
Processing triggers for ca-certificates (20211016~18.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
</code>
Answer: When apt says dependencies have conflicting versions, you need to manually specify all dependencies in the command line. This tells apt that you want also to upgrade all dependencies. Apt doesn't by default allow downgrading packages. If any package still conflicts and it is not required by other installed packages, it will be automatically suggested for removal.
Version conflicts can be caused by either having two repositories with same package, but different version numbers, or trying to upgrade packages while a repository metadata is still being updated.


Posterior of binomial and mixed prior

Question: I'm currently studying posterior distribution with likelihood $y|\theta \sim B(n,\theta)$ and mixture of prior distribution $\theta \sim \pi Beta(\alpha_1, \beta_1) + (1-\pi)Beta(\alpha_2, \beta_2)$. The posterior distribution is as follows:
$$
\begin{aligned}
p(\theta|y) & \propto \theta^y (1-\theta)^{n-y} \{\pi\frac{\Gamma(\alpha_1 + \beta_1)}{\Gamma(\alpha_1)\Gamma(\beta_1)}\theta^{\alpha_1 -1}(1-\theta)^{\beta_1 - 1} + (1-\pi)\frac{\Gamma(\alpha_2 + \beta_2)}{\Gamma(\alpha_2)\Gamma(\beta_2)}\theta^{\alpha_2 -1}(1-\theta)^{\beta_2 - 1}\} \\
 & = \pi\frac{\Gamma(\alpha_1 + \beta_1)}{\Gamma(\alpha_1)\Gamma(\beta_1)}\theta^{y + \alpha_1 -1}(1-\theta)^{n-y +\beta_1 - 1} + (1-\pi)\frac{\Gamma(\alpha_2 + \beta_2)}{\Gamma(\alpha_2)\Gamma(\beta_2)}\theta^{y + \alpha_2 -1}(1-\theta)^{n-y + \beta_2 - 1} \\
\end{aligned}
$$
Now, I have to show $p(\theta|y)$ is  another mixed beta distribution; that is,
$$p(\theta|y) \propto w*Beta(y+\alpha_1, n -y + \beta_1) + (1-w)*Beta(y+\alpha_2, n - y + \beta_2)$$
But,  I'm currently stuck to here. How can I induce this formula from the above relationship?
Thank you.
Answer: Imagine an unobserved variable $Z\in\{1,2\}$ that tells you which component of the mixture to use.  Your question is a bit ambiguous as to whether $Z$ is chosen once or chosen independently $n$ times, but your computed posterior fits with 'chosen once'.
$Y|Z=1$ will have a Beta$(\alpha_1,\beta_1)$ distribution and $Y|Z=2$ will have a Beta$(\alpha_2,\beta_2)$ distribution.  So $$(\alpha_1,\beta_1|Z=1,Y)\sim Beta(\alpha_1+y,\beta_1+n-y)$$ and
$$(\alpha_2,\beta_2|Z=1,Y)\sim Beta(\alpha_2,\beta_2)$$
and similarly, mutatis mutandis for $Z=2$.
That is, the posterior is the same sort of mixture as the prior. The mixture components are updated using the formulas for conjugate priors.  We also need to update the probability $\pi$ to get the new component probabilities $w$. This is slightly more complicated, but you aren't asking for an explicit formula so that's ok. [Basically, $\pi$ will be updated using the likelihood ratio for getting the observed $y$ from component 2 vs component 1]
Comment: Thank you. Then, could you explain a bit more about how to update the $\pi$ to obtain $w$? Now I see that I was wondering how to calculate the $w$ from all the $\Gamma$ stuff in the above expressions. For reference, I was looking at this site https://stats.stackexchange.com/a/100732/372718 to generalize the expression $p(\theta|y)$.
Answer: If you follow the same reasoning as in the answer you mentioned (https://stats.stackexchange.com/a/100732/271601), you will obtain:
$$
p(\theta|y) \propto \pi \frac{B(y+\alpha_1,n-y+\beta_1)}{B(\alpha_1,\beta_1)}B(\theta;y+\alpha_1,n-y+\beta_1) + (1-\pi) \frac{B(y+\alpha_2,n-y+\beta_2)}{B(\alpha_2,\beta_2)}B(\theta;y+\alpha_2,n-y+\beta_2)
$$
where $B(\cdot,\cdot)$ refers to the Beta function and $B(\cdot;\cdot,\cdot)$ refers to the Beta distribution. That is to say, the posterior $p(\theta|y)$ is equal to the above expression, up to a multiplicative constant. Since the pseudo weights $\pi \frac{B(y+\alpha_1,n-y+\beta_1)}{B(\alpha_1,\beta_1)}$ and $(1-\pi) \frac{B(y+\alpha_2,n-y+\beta_2)}{B(\alpha_2,\beta_2)}$ do not add up to 1, we can normalize them (i.e. divide both of them by their respective sum), so that their sum effectively adds up to 1. Hence, the new weights on your mixture will be
$$
w_1=\pi \frac{B(y+\alpha_1,n-y+\beta_1)}{B(\alpha_1,\beta_1)} \left(\pi \frac{B(y+\alpha_1,n-y+\beta_1)}{B(\alpha_1,\beta_1)}+(1-\pi) \frac{B(y+\alpha_2,n-y+\beta_2)}{B(\alpha_2,\beta_2)}\right)^{-1}
$$
and
$$
w_2=(1-\pi) \frac{B(y+\alpha_2,n-y+\beta_2)}{B(\alpha_2,\beta_2)}\left(\pi \frac{B(y+\alpha_1,n-y+\beta_1)}{B(\alpha_1,\beta_1)}+(1-\pi) \frac{B(y+\alpha_2,n-y+\beta_2)}{B(\alpha_2,\beta_2)}\right)^{-1}
$$
(i.e. the above pseudo weights, but normalized by their sum). You can now write
$$
p(\theta|y) = w_1 B(\theta;y+\alpha_1,n-y+\beta_1) + w_2 B(\theta;y+\alpha_2,n-y+\beta_2)
$$
(which is an equality, and not a proportionality relation), with $w_1 + w_2 = 1$.

Posterior of binomial and mixed prior


@SuppressWarnings for the invocation of the varargs with un-cast null

Question: What string should I use in @SuppressWarnings("....") to remove the warning I get in this unit test of a method with varargs:
/**
 * Calling {@link #readFiles(String, String...)} with just one arg and a null
 * works, but you have to do a null check on the varargs in case it is null.
 */
@Test
public void callMethodWithOneArgAndNull() {
   readFiles("C:/Temp", null);
}

Eclipse gives the following warning for this:

The argument of type null should explicitly be cast to String[] for
  the invocation of the varargs method readFiles(String, String...) from
  type TestVarArgs. It could alternatively be cast to String for a
  varargs invocation

This is good; it is expected. I am also unit testing what happens when I cast the null.
But for the purposes of this single test, I want to suppress that specific warning, i.e. I don't want to use @SuppressWarnings("all"). I have tried every string mentioned in this answer to What is the list of valid @SuppressWarnings warning names in Java? and none of them seem to catch this warning.
Answer: To my understanding, this is not a warning you can suppress with a specific annotation. You have to make an explicit cast to make the warning go away - or suppress all warnings.
Also, consider testing both possible casts, i.e. (String[]) {null} and (String) null, to make sure your method doesn't choke on "no files" nor on a "null filename" input.
Comment: Good pointers @einpoklum, thanks. I wrote up these tests here: http://robertmarkbramprogrammer.blogspot.com.au/2013/03/nulls-and-varargs.html
Answer: You can use @supresswarnings (unchecked) annotation.