Testing the race model inequality in R

Question: Lets say we have hypothetical participant that is presented with 3 stimuli conditions: a flashing dot ($C_x$), a sound blip ($C_y$) and combination of both ($C_z$). We ask this participant to respond as quickly as possible to the onset of the stimuli and we measure the reaction time (RT). It's been observed that responses are often observed to be substantially faster when both stimuli are presented simultaneously ($C_z$), compared to condition when they are presented separately ($C_x$ and $C_y$), and this effect has been labeled redundant signals effect (e.g. Hershenson, 1962). Different models account for this effect, one of those models being race model (Miller, 1982). Quoting Colonius et al (2006) definition:

Raab (1962) was the first to propose a race model for simple RT such that (a) each individual stimulus elicits a detection process performed in parallel to the others and (b) the winner's time determines the observable RT. This model suggests that RSE is generated by statistical facilitation: If detection latencies are interpreted as (nonnegative) random variables, the time to detect the first of several redundant signals is faster, on average, than the detection time for any single signal.

Urlich et al (2007) continues that:

According to race models, the observed RT distributions should
  satisfy, for every value of $t$, the so-called race model inequality
  (Miller, 1982):
$F_z(t) \le F_x(t) + F_y(t), t > 0$
where $F_x$ and
  $F_y$ are the cumulative density functions (CDFs) of RT in the two
  single-stimulus conditions $C_x$ and $C_y$, respectively, and $F_z$ is
  the CDF of RT in the redundant-stimulus condition $C_z$.

I wanted to test this model, but I found it hard to translate available Matlab code into R. Matlab code is available in the appendix of this paper by Urlich et al (2007), which can can be downloaded here, in case you don't have access to Behavioural Research Methods journal. To summarize Urlich et al (2007) approach, his algorithm goes through the follwing steps:
Step 1: Estimating empirical cumulative density functions (CDFs) for every participant (we got just one in the example here) and every stimulus condition ($C_x$, $C_y$, $C_z$). As Urlich et al (2007) points out:

these CDFs will be denoted as the individual CDFs. Specifically, let $G_x$, $G_y$, and $G_z$ be the individual estimates of two single-stimulus CDFs, $F_x$ and $F_y$, and the redundant CDF, $F_z$, respectively.

I thought I managed to compute $G_x$, $G_y$, and $G_z$, but I might be wrong:
#example reaction times for single hypothetical participants for 3 experimental  conditions
#this is the same dat set as one used by Urlich et al 2007
cx <- c(244, 249, 257, 260, 264, 268, 271, 274, 277, 291)
cy <- c(245, 246, 248, 250, 251, 252, 253, 254, 255, 259, 263, 265, 279, 282, 284, 319)
cz <- c(234, 238, 240, 240, 243, 243, 245, 251, 254, 256, 259, 270, 280)

#here I calculate quantiles corresponding to the given probabilities
#I use the default quantile algorithm (type 7 see R manual for 'quantile')
#it seems right, but I'm not 100% sure
psq <- seq(0,1,0.1)
gx <- quantile(cx, probs=psq, names=F)
gy <- quantile(cy, probs=psq, names=F)
gz <- quantile(cz, probs=psq, names=F)

#here I visualize the quantiles, looks more or less like in Urlich etal 2007
xyz <- data.frame(rt=c(gx,gy,gz),
                  perc=rep(psq,3),
                  cond=rep(c("gx(t)", "gy(t)","gz(t)"), each=length(psq)))
require(lattice)
xyplot(perc ~ rt, groups = cond, xyz, col=1, lty=3:1, type=c("g","l","p"),
       key=list(lines=list(lty=3:1), text=list(labels=levels(xyz$cond))))

Step 2: From $G_x$ and $G_y$, one computes the bounding sum $B(t) = G_x(t) + G_y(t)$ (for each participant), which provides an estimate for each participant of the upper bound on the right side of race model equation above. But then, I'm not sure how to compute the race model bounds, which seems trivial, but doesn't really work. The authors simply show the equation as , and I applied it in R as:
b = gx + gy

but then when I visualize it, it's wrong:
xyz <- data.frame(rt=c(gx,gy,gz,b),
                  perc=rep(psq,4),
                  cond=rep(c("gx(t)", "gy(t)","gz(t)","gx(t)+gy(t)"), each=length(psq)))
require(lattice)
xyplot(perc ~ rt, groups = cond, xyz, col=1, lty=4:1, type=c("g","l","p"),
       key=list(lines=list(lty=4:1), text=list(labels=levels(xyz$cond))))

Step 3 and 4: Percentile values are computed from $G_z$ and $B$ for each participant at certain prespecified probabilities that must be the same for all participants. Finally, the percentile values are aggregated across participants, which is omitted in Urlich etal (2007) example.
I hope my explanation is a bit clearer now then it was before.
References
Miller, J. (1982). Divided attention: evidence for coactivation with redundant signals. Cognitive psychology, 14(2), 247–79.
Ulrich, R., Miller, J., & Schröter, H. (2007). Testing the race model inequality: an algorithm and computer programs. Behavior research methods, 39(2), 291–302.
Colonius, H., & Diederich, A. (2006). The race model inequality: interpreting a geometric measure of the amount of violation. Psychological review, 113(1), 148–54. doi:10.1037/0033-295X.113.1.148
Raab, D. H. (1962). Statistical facilitation of simple reaction times. Transactions of the New York Academy of Sciences, 24, 574-590.
Hershenson, M. (1962). Reaction time as a measure of intersensory
facilitation. Journal of Experimental Psychology, 63, 289-293.
Comment: It may indeed be clearer, but it still doesn't specify well-defined questions.  I assume the issue is that somewhere in either step 1 (estimating the CDF's) or step 2 (calculating race model bounds).  With that said, looking at step 2, I don't see how that would possibly be a race model bound.  It implies that the bound is as high as BOTH reaction times combined?  I would think that the bound would be similar to min(X,Y) not X+Y.
Comment: You have not made it easy for us to understand the question here.  What, for example, is the "race model"?  Unless someone has read the articles you cite and done the actual analysis themselves how could we help?  Your original question appears to be mostly copied (without citation) from an article in the behavioural psychology literature.  Something that is "well known" there will not necessarily be well known to CV readers.
Comment: Ok, you're right @PeterEllis - thank you for your feedback. I've tried to revise the question with much more detail, I hope it's a bit clearer now.
Answer: UPDATE: All the custom versions of functions rewritten in R and used in this answer are added in the bottom for better clarity.
I guess. You probably knew where's gone wrong. As you said

it seems right, but I'm not 100% sure

Yes. This is the start of the differences between your data and Ulrich et al's.
cx <- c(244, 249, 257, 260, 264, 268, 271, 274, 277, 291)
cy <- c(245, 246, 248, 250, 251, 252, 253, 254, 255, 259, 263, 265, 279, 282, 284, 319)
cz <- c(234, 238, 240, 240, 243, 243, 245, 251, 254, 256, 259, 270, 280)

Here is the correct psq:
psq <- probSpace(10); psq
[1] 0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95

Here is your psq:
psq <- seq(0,1,0.1); psq
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

Also (or should say therefore), your gx, gy and gz are different from Ulrich et al's. Here I used Ulrich et al's ties function (off course, after rewriting it in R) to generate a data frame that stores (1) the unique response time (RT) data, (2) the number of iteration for each RT datum, and (3) the cumulative frequency.  This is in line with Urlich et al's MATLAB function (note that they stored them as a structure, I suppose). These three pieces of information are then served as inputs to Ulrich et al's CDF function, which differs from the R's quantile function. I rewrote their MATLAB CDF function to work with my ties data frame output. They in fact wrote their CDF function, following the equation in Appendix A, not the equation (2) in the main article. Here is probably the place you might not notice, so did not work out the question.  Basically, they wanted to handle the problem of ties, so they wrote their variation of CDF function.
dfx <- ties(cx)
dfy <- ties(cy)
dfz <- ties(cz)
tmax <- max(cx,cy,cz)

gx <- cdf.ulrich(data=dfx, maximum=tmax)
gy <- cdf.ulrich(data=dfy, maximum=tmax)
gz <- cdf.ulrich(data=dfz, maximum=tmax)

I then added gx and gy together and stored them to b. Because R does element-by-element calculation, if one uses the operator as usual, I did not bother to use a for loop, as Ulrich et al.'s did. I then used Ulrich et al's GerPercentile function (rewrote it in R) to calculate the percentiles for each individual CDF, including the b.  Also note that you need tmax to confine the domain of RT.  This was possible the place you had not noticed.
b <- gx + gy
xp <- GetPercentile(psq, gx, tmax);
yp <- GetPercentile(psq, gy, tmax);
zp <- GetPercentile(psq, gz, tmax);
bp <- GetPercentile(psq, b, tmax);

Finally, construct a data frame to draw the figure.  Here is the panel F in their Figure 1.
gdf <- data.frame(RT =c(xp,yp,zp,bp), Probability =rep(psq, 4),
                  Condition =rep(c("gx(t)", "gy(t)","gz(t)","gx(t)+gy(t)"), each=length(xp)))
panelf <- ggplot(gdf, aes(x = RT, y = Probability, group=Condition,
          colour=Condition, shape=Condition)) +
          geom_point() + geom_line()
panelf + coord_cartesian(xlim = c(230, 330), ylim=c(-.01,1.01)) +
         theme(legend.position= c(.85, .20),
               legend.title = element_text(size=12),
               legend.text = element_text(size=12))

I hope these would be helpful.

Also I should mention that you may need to load the required libraries (here I have used ggplot2 and grid) and source those home-made functions, if you did not put them together in one RaceModel function as Ulrich et al did.  I suggest write individual file for each function, which was what I did and source them.  Some of the functions you could translate directly from the equations in Ulrich et al's article and others from their MATLAB or Pascal code. MATLAB uses parentheses, (), to index vector, but R uses square bracket, [].
FUNCTIONS
Here is the probSpace function. I wrote it according to Ulrich et al's equation (3) (p. 293) by myself, so you probably won't be able to find it in any R packages.
probSpace <- function(len){
# PROBSPACE Determine the interval of percentiles
#   The function used equation (3) in Ulrich, Miller and Schroter (2007)
  P <- numeric(len);
  for(i in 1:len){
    P[i] <- (i - .5) / len;
  }
return(P)
}

Here is Ulrich et al.'s (2007) CDF function (p. 296 and 299). I wrote it according to their MATLAB code and the equation in Appendix A.
cdf.ulrich <- function(data=NULL, maximum=3000){
  # Create a container, whose length is the longest data vector
  # data is the output from `ties` function
  U <- data[,1]
  R <- data[,2]
  C <- data[,3]
  G <- numeric(maximum);

  # The length of the processed data vector, trimming off ties, if there is any.
  k <- length(U);  # U contains data in millisecond, e.g., 320 ms etc.

  # The last element of the cumulative frequency supposely is the
  # length of the data vector.
  n <- C[k]

  for(i in 1:k) { U[i] <- round(U[i]) }

  # from 1 ms to the first value of the data set, their probability should be 0.
  for(t in 1:U[1]) { G[t] <- 0 }

  for(t in U[1]:U[2]){
  G[t] <- ( R[1]/2 + (R[1]+R[2]) / 2*(t-U[1]) / (U[2] - U[1]) ) / n;
  }

  for(i in 2:(k-1)){
    for(t in U[i]:U[i+1]){
      G[t] <- (C[i-1] + R[i] / 2+(R[i] +R[i+1]) / 2*(t-U[i]) / (U[i+1] - U[i])) / n;
    }
  }

  for(t in U[k]:maximum){
    G[t] <- 1;
  }
  return(G)
}

Below are the ties() and GetPercentile() functions written by Ulrich et al. (2007). I merely rewrote them into R, based on their description in the article, the equations and MATLAB codes they provided.
Firstly, the ties function:
ties <- function(W){
  # Count number k of unique values
  # and store these values in U.
  U <- NULL; W <- sort(W); n = length(W); k = 1; U[1] <- W[1]
  for (i in 2:n) {
    if (W[i] != W[i-1]) {
      k <- k+1;
      U <- cbind(U, W[i])
    }
  }
  U <- U[1,]

 # Determine number of replications R
 # k is the length of the vector, after trimming off the ties
  R <- numeric(k)
  for (i in 1:k){
    for (j in 1:n){
      if (U[i] == W[j]) R[i] <- R[i] + 1;
    }
  }

  # Determine the cumlative frequency
  C <- numeric(k)
  C[1] <- R[1]
  for(i in 2:k){
    C[i] <- C[i-1] + R[i];
  }
  res <- list(U, R, C)
  names(res) <- c("U", "R", "C")
  return(as.data.frame(res))
}

Then, the GetPercentile function:
GetPercentile <- function( P, G, tmax ){
# Determine minimum of |G(Tp[i]) - P[i]|
  np <- length(P);
  Tp <- numeric(np)
  for( i in 1:np) {
    cc <- 100;
    for(t in 1:tmax) {
      if ( abs(G[t] - P[i]) < cc ) {
        c <- t;
        cc <- abs(G[t] - P[i]);
      }
    }

    if( P[i] > G[c] ){
      Tp[i] <-  c + (P[i] - G[c]) / (G[c+1] - G[c]);
    } else {
      Tp[i] <- c + (P[i] - G[c]) / (G[c] - G[c-1]);
    }
 }
 return( Tp )
}

Comment: Thank you, excellent replication or Urlich et al (2007) method but using R! I slightly edited your answer to make it flow a bit better. Cheers mate!
Comment: this is such an excellent answer.
Answer: In short, I think that at the stage of b you find yourself combining RTs when what you should be combining are proportions.  This is speculative, but I took the stab below to get things rolling.  I do love me some race models and have been meaning to implement one form or another in R for a while, but never got around to it.  If you do complete this project, I hope you might share.
With those disclaimers in place, I'll add... I don't know MATLAB code... but I love speculating.
The MATLAB code to calculate the bounding sum is:
for t=1:tmax;
   B(t)=Gx(t)+Gy(t);
end

This looks like a loop for all possible values of RT ranging from 1 to the maximum of cx, cy, and cz.  The bounding sum therefore is not the sum of the RTs at various quantiles as suggested by your code above.  In fact, in the MATLAB code it looks like Gx is the result of the CDF function across the full time-span.  For example, from the mathworks website I can see that cdf('Normal',-2:2,0,1) is expected to yield 0.0228  0.1587  0.5000  0.8413  0.9772.  But what the code actually calls is CDF, which is defined in Appendix B.  I'd try to turn it into an R function, but without understanding MATLAB data structures it is kind of a nightmare.  But, the description of what is happening is in the referred to text... "Following the standard procedures for percentile estimation (see, e.g., Gilchrist, 2000), each step function is next used to generate a corresponding cumulative frequency polygon... [etc]" ... "these values are stored, millisecond by millisecond, in the vectors Gx, Gy, and Gz in the MATLAB program shown in Appendix B".  Hey, cool, Gx, Gy, and Gz are clearly results from CDF, so... we know how we need to specify CDF (in abstract).  Now it is just a coding problem.  I bet you can do that, if you don't, I love co-authorship (half - j/k).
At any rate, I think this means that at this stage in your code b should be giving you something like the sum of proportions correct for various levels of RT not the sum of RT values (as you have coded).  That seems a little odd to me and I imagine I'm missing a step because the proportion that will have completed the race when .5 are due to have completed race 1 and .5 are due to have completed race 2 is certainly not 1 for the joint race...  but I might be wrong because the authors claim that "Because the sum is only compared against the values of Gz, the computation of the sum can be terminated at the first value of t for which the sum exceeds a value of 1.0"... which seems a little scary, but is certainly beyond the scope of the question and would mean presuming I know something the authors don't which seems unlikely.

Testing the race model inequality in R


UnRaid software raid - CentOs

Question: I have a linux ( CentOS ) server that has 2 hard drives, and it configure by software raid1,
Now I need to un-raid hard drives, and use both of them.
How can I Un-raid them ? ( I just has SSH access )
Answer: While the filesystem type will be "linux-raid" rather than "linux" you can still break the mirror and reformat the 2nd drive. Then you can either copy all the data across and reformat the 1st drive too, or run the raid drive in degraded mode forever. I'm not sure there's a performance hit doing this.
You'll want to use mdadm to break the mirror, by marking one of the disks as failed. Then format it. Remember to reset the bootloader or it'll still try to boot into the raid partition, rather than your new plain partition.
A quick google says that you can shrink your raid array, so you could keep a small RAID1 array, and then use the remainder of the 2 drives as 2 new partitions.
Answer: Your only solution is to dump the content somewhere else, destroy the RAID array and reinstall your OS.
You cannot "unraid" an array because it destroys the structure underlying your filesystem.

UnRaid software raid - CentOs


How to SyncLock a Shared Integer

Question: I am pretty new to multi-threading in general and I would like to know how can I lock a shared integer variable to not be accessed by other threads while it is read/updated by an other thread.
When I try do do SyncLock myInteger, I get that error : Error 6 'SyncLock' operand cannot be of type 'Integer' because 'Integer' is not a reference type.
Here a simplified example of what I mean
Private Shared myInteger As Integer

Private Sub MySub()
  SyncLock myInteger ' This does not work
    ' read/write a new value to myInteger
  End SyncLock
End Sub

MySub is called by multiple instances of the class, and running in multiple threads
Answer: I suspect you don't fully understand what SyncLock does. Even if you could do it (if Integer were a reference type, for example) you wouldn't want to do it - because when you replaced the value of myInteger, the lock would be useless.
You should either lock on something else (personally I generally declare a separate, read-only variable purely for the sake of locking, usually of type Object) or if you only want to operate on myInteger at any time (rather than some compound operation which needs to read or update several variables at the same time), use the Interlocked class.
Comment: Your suspicions are right, I don't fully understand what SyncLock does ;-) But with your explanation that it could be any trivial Object, just to keep the lock, I think I get a better idea. Thank you SO Grand Master ;-)

How to SyncLock a Shared Integer


How to use Search API with Solr?

Question: I am new to drupal , I have installed SEARCH API . I have opened an account with open solr and following the process suggested in https://drupal.org/node/2137651
since I am not using apache solr  search I am going to search api setting (last para in help )
the setup was succesfful and I was able to connect to solr server and infact was able to delete the index , However I am geeting the error message in config->search api->server as" You are using an incompatible schema.xml configuration file. Please follow the instructions in the INSTALL.txt file for setting up Solr"
I am also not able to index , it gives error 400
I dont know where I went wrong in follwing document of https://drupal.org/node/2137651
.I also copied the schema.xml from (open solr ) to drupal/sites/all/modules/search_api_solr/solr-conf/4.x
Any help is highly appreciated
Regards
Carol
Answer: Copy the schema.xml from the Search API Solr module into Open Solr. Also be warned that compatibility with 4.x is not stable, so consider using solr 3.x configuration instead.
Answer:
I also copied the schema.xml from (open solr ) to drupal/sites/all/modules/search_api_solr/solr-conf/4.x

You have to copy all the configuration files from "drupal/sites/all/modules/search_api_solr/solr-conf/4.x" to your conf directory of the solr core (eg. "$SOLR_HOME/collection1/conf").
Answer: I was having the same issue, and receiving the  "Please follow the instructions in the INSTALL.txt file for setting up Solr". The solution was to install apache Solr manually, following this tutorial https://www.digitalocean.com/community/tutorials/how-to-install-solr-on-ubuntu-14-04. After that everything went well.

How to use Search API with Solr?


How to fetch a date parameter from the SQL Task and pass it on to Data Flow tasks

Question: I need to fetch a parameter from "Execute SQL Task" and pass it on to 10 different Data Flow tasks,
so that all these 10 DF uses the same parameter which is been generated by Execute SQL task flow.
So far what i have done is this;

I have the SQL Task editor and used the connections type as ODBC, SQLSourceType=Direct Input & SQL Statement = select max(date) from table where package = 'PackageName';
This SQL Task gets connected to 10 different Data Flows.
Comment: From the SQL Task, is the date put into a variable? How does the SQL task "get conmected" ro the other data flow tasks?
Answer: I think what you are trying to do is execute the sql script and return the value (the date) into a variable. Then have that variable available to the data flow tasks so they all have the same value.
Assuming this is the case, you need to first declare a variable in the package to hold the date returned from the query. Make sure the scope is available to the entire package.
Next, in order to setup your sql task correctly, you need to map the results of the sql script to the variable. In the SQL Task Editor, in the General Tab, set the result set to Single Row.  Next, add a variable under the Result Set tab to receive the date from the query.  For Result name, that will be the column returned from the query that is executed (you should give a name to column that returns max date).  The variable name is where you put the variable you declared in the package.  Now, when you execute the package the value from the query is stored in variable and is available to be used wherever you want.
I can't comment that much about how to apply that variable in the dataflow tasks without knowing which type of tasks you will use it in.  But from this point, you can use the variable just like any other static variable that you would create.
Hope that helps.
Comment: Thanks Jason. Much appreciated.


Onde aplicar a função sleep() em python para limitar requisições

Question: O endpoint onde consulto as informações que eu quero possui uma limitação de requisição, precisa esperar 30s pra fazer 10 requisições.
Queria usar a função 'sleep()' mas não sei onde colocar.
import requests

aprovados = []

for id_vaga in lista_id:

  url = "https://teste.teste/teste"

  headers = {
      "Accept": "application/json",
      "Authorization": bearer
  }

  response = requests.get(url, headers=headers)
  aprovados.append(response.text)

Answer: Você precisa usar o sleep logo antes de fazer a requisição, a cada 10 requisições. Pra isso você precisa saber quantas requisições já foram realizadas. A solução seria:
import requests
import time

aprovados = []
total_requests = 0

for id_vaga in lista_id:

  total_requests++
  url = "https://teste.teste/teste"
  headers = {
      "Accept": "application/json",
      "Authorization": bearer
  }
  if (total_requests % 10 == 0)
    time.sleep(30)

  response = requests.get(url, headers=headers)
  aprovados.append(response.text)

assim, a cada dezena de requests, haverá um delay de 30 segundos para a próxima.


How to keep a TCP connection open and perform multiple Writes/Reads in C# .NET?

Question: There are multiple posts that describe the performance benefit of keeping a TCP connection open, instead of closing and opening each time you need to read or write. For example:
Best practice: Keep TCP/IP connection open or close it after each transfer?
I'm communicating with an RPC based device that takes json commands. The example I have from the device vendor opens and closes a connection each time they send a command. This is what I currently do via TcpClient in a using statement, but I'd like to see if there's anyway I could improve upon what I've already done. In fact, I had attempted this when starting the project, but couldn't figure out how to do so, so closed each time out of frustration and necessity. My latest experiment using sockets because all posts indicate doing so as a necessity for lower level control:
<code>public class Connection
{
    private Socket tcpSocket = null;
    public string IpAddress = "192.168.0.30";
    public int Port = 50002;

    public Connection(string ipAddress, int port)
    {
        this.IpAddress = ipAddress;
        this.Port = port;
    }

    public void Connect()
    {
        DnsEndPoint ipe = new DnsEndPoint(this.IpAddress, this.Port);
        Socket tempSocket =
            new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        tempSocket.Connect(ipe);
        if (tempSocket.Connected)
        {
            this.tcpSocket = tempSocket;
            this.tcpSocket.NoDelay = true;
            this.tcpSocket.
            //this.tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,true);
            Console.WriteLine("Successfully connected.");
        }
        else
        {
            Console.WriteLine("Error.");
        }

    }

    public void Disconnect()
    {
        this.tcpSocket.Disconnect(true);
        this.tcpSocket.Dispose();
        Console.WriteLine("Successfuly closed.");
    }

    public string SendCommand()
    {
        string path = @"C:\Users\me\Desktop\request.json";
        string request = File.ReadAllText(path);
        Byte[] bytesSent = Encoding.UTF8.GetBytes(request);
        this.tcpSocket.Send(bytesSent);
        this.tcpSocket.Shutdown(SocketShutdown.Send);
        var respBytes = ReceiveAll();
        string s = System.Text.Encoding.UTF8.GetString(respBytes, 0, respBytes.Length);
        return s;
    }

    public byte[] ReceiveAll()
    {
        var buffer = new List<byte>();
        var currByte = new Byte[1];
        var byteCounter = this.tcpSocket.Receive(currByte, currByte.Length, SocketFlags.None);
        while (this.tcpSocket.Available > 0)
        {
            currByte = new Byte[1];
            byteCounter = this.tcpSocket.Receive(currByte, currByte.Length, SocketFlags.None);
            if (byteCounter.Equals(1))
            {
                buffer.Add(currByte[0]);
            }
        }

        return buffer.ToArray();
    }

}
</code>
Console app:
<code>    static void Main(string[] args)
    {
        Connection s = new Connection();
        s.Connect();
        Console.WriteLine(s.SendCommand());
        Console.WriteLine(s.SendCommand());
        Thread.Sleep(5000);
        s.Disconnect();
        Console.ReadKey();
    }
</code>
This approach works once. The first time I call send command. It doesn't the second time (throws an exception), because I call socket.Shutdown() on Send in my SendCommand(). I do so because of this post:
TCPClient not receiving data
However, there doesn't seem to be a way to re-enable the ability to Send after calling Shutdown(). So now I just don't know if it's even possible to keep a tcp connection open if you have to both read and write. Moreover, I can't really find a useful example online. Does anyone know how to do so in .NET? Is this even possible?
Comment: Why do you think keeping the connection open is an improvement? Unless there is an actual need for this that you can point to, it is very likely not an improvement, and you'll likely be wasting a lot of your time for no good reason. There's also a good chance you'll run into problems in deployment you won't know how to fix quickly enough. Do you know whether the protocol in question is designed to be used like that? If not, you may be asking for trouble.
Comment: Ultimately, the device I'm using will be incorporated in a manufacturing environment for the factory production of other devices. The speed in which devices can be made has a direct correlation with the economics of manufacturing, and these types of systems are online and running nonstop for long periods of time. So any type of performance boost (which I assume is execution speed in the case of opening and closing connections) is desirable. But I'm coming to realize, I probably just need to open and close a connection each time.
Comment: It is extremely common that smaller companies release TCP based protocols that are hopelessly flawed, and has inherent performance problems. If this turns out to be the case here too, then keeping the connection open will not solve the problem. In such a case the solution is to ask the creator to fix the protocol.
Comment: The normal problem with flawed protocols is that they rely on the client to open, send and close, or to open, receive and close. Since TCP is a stream and not packets, that won't work. You can miss data, or worse, you're slowed down. I won't explain why. At a minimum the client should open, send, receive, and close. Is your protocol doing this? If yes, great! Both sides must know how much is expected, and keep reading, maybe more than once, until ALL that is expected is received. How much to expect is usually given by a length field in a header, or by text markers (e.g. eol) in text protocols.
Comment: I'm not an expert on TCP, despite having a lot of experience creating protocols and figuring out issues. If somebody sees a reason for correcting my explanation, please do so.
Comment: There may be another option, since you're communicating with a device. Often UDP is an alternative to TCP in industrial devices. That means you don't have to worry at all about the intricacies of streams in TCP. It does require however that the message is no longer than the maximum packet size. This can be as much as around 9000 bytes on a local network where jumbo packets are allowed. UDP is also fast.
Comment: I had investigated this previously using nmap to scan the device and see what protocols were available. I only saw TCP via nmap, and failed to connect/kept getting timeout exceptions when just trying to connect anyway.
Comment: Take a look at the answers [here](https://stackoverflow.com/questions/5523565/socket-flush-by-temporarily-enabling-nodelay) if you have speed problems. Not sure what they're worth. There are plenty of other advice on SO too I'm sure.
Answer: If it is possible to keep the connection open for more messages depends on the application protocol. There is no way to enforce this if the protocol does not supports it. Thus, ask the vendor or look into the protocol specification (if it exists) for information if and how this is supported.

However, there doesn't seem to be a way to re-enable the ability to Send after calling Shutdown().

There is no way. TCP write shutdown means that one does not want to send any more information. It is impossible to take this back. If the protocol supports multiple message exchanges then it needs to have a different way to detect the end of a message than calling Shutdown.
Comment: @hallibut: Application protocols are something like HTTP, where each message (HTTP request, HTTP response) has a clearly defined syntax and a clearly defined end - which makes it possible to send multiple message using the same TCP stream since it is clear where one ends and the other starts.
Comment: Thanks for the clarification, I believe I understand now. The vendors api documentation does include expected json response from a user provided json request. But from further reading on framing protocols, I believe in order for the server to understand the end of the message I'm sending, it either needs to follow an expected length prefixing or delimiter. And I guess the reason, I must call shutdown is because it does neither, so the the server doesn't know when the outgoing message ends until the clients outgoing stream closes?
Comment: That makes sense about calling Shutdown. But I'm not sure I follow what you mean about  the application protocol (but I will reach out to the vendor). From your response, I take it that TCP is a means of communication, but not necessarily the application protocol. The application protocol, would be how the device (server) handles incoming requests? Just trying to understand what to ask.
Comment: The entire specification should contain a description of how the TCP server and client interacts with messages, and what the messages look like. A more complex protocol will have transactions. A simple transaction could be "What is your name?", "My name is Server1." A complex transaction could be a long list of back and forth messages to e.g. transfer your list of mail messages from the MS Exchange server to your Outlook client. A dirt simple protocol for a device might not have anything else than one transaction, which is "Give me your data." and "{some json...}" and "Thanks, now I close".
Comment: Is there nothing in the example that shows what the possible missing part(s) might be? Often the json is prefixed by some non-json length expressed as text. Worst case, there's some binary header. Also, line endings can be in various forms.
Comment: But I think I may be wrong. You've already got it working, but it's slow?
Answer: TCP/IP is a streaming protocol.  To pass messages with it you need a "framing protocol" so peers can determine when a message is finished.
One simple way to signal the end of a message is to close the socket when you've sent the last byte.  But this prevents socket reuse.  See the evolution of HTTP for an example of this.
If this is what your device does, there's no way to reuse a socket.


help me 'Matlab' for bisection method.

Question: Pb1) use a matlab.
Graph the function
$$f(x) = 32x^6 −48x^4 +18x^2 −1$$
on the interval $[−1, 1]$ and find all roots of $f (x) = 0$ accurate to within $\varepsilon = 1e−10$ by using the bisection method.
The true solutions are $\cos[(2n−1){\over}]$ for $n=1,2,...,6$.
Report the number of iterations and errors for each roots in tables.
My script is
f=@(x) 32*x.^6-48*x.^4+18*x.^2-1;

a=-1; b=-0.8; c=(a+b)/2; eps=1e-10; it=0;

g=@(n) cos((2*n-1)*pi./12);

n=1;

    while abs(a-c)>eps

        if f(a)*f(c)>0

            a=c;

        else

            b=c;

        end

   c=(a+b)/2; it=it+1;

    end

   fprintf('Iteration is %.d. And solution is %.10f \n',it,c);

while (abs(c-g(n))>eps)

    n=n+1;

end

    fprintf('And exist such that True solution is %.10f\n',g(n));

and result
Iteration is 30. And solution is -0.9659258262
And exist such that True solution is -0.9659258263
since the graph of $f(x)$ is
so the script divide the interval of $[-1,1]$ for using bisection method.
but for solving a Pb1, this script is rewritten for each sub-interval.
can you help me?
I hope to NO rewritten for each sub-interval.
Comment: You should include the case whether $f(a)f(c)=0$, and a sanity check w.r.t. $f(a)f(b)>0$.
Finally, you might follow @Daryl suggestion.
Answer: One option is to put your bisection code in a for loop that loops over the subintervals.
point = [-1;-0.9;-0.5;0;0.5;0.9;1];

for interval = 1:6
    start = point(interval);
    finish = point(interval+1);

    % regular bisection method
    ....
end

help me 'Matlab' for bisection method.


Estimating effect after Benjamini-Hochberg procedure

Question: I run an AB test and I have results at city level (keep in mind I run a one-sided test):
<code>city,diff_means,p_value,ci_lower,ci_upper
 TGN,       0.1,  0.001,    0.09,    0.11
 BCN,       0.0,   0.78,    -1,1
 REU,      -0.1,   0.99,   -0.11,   -0.09
...
</code>
I run the Benjamini-Hochberg procedure and find a new significance cut-off (using 5% FDR), which is 0.01. Therefore, I select all cities that have a p-value of 0.01 or less and decide to apply the treatment on them.
In that case, what is the best estimate of the impact of the rollout (both point estimate and confidence interval)? I believe if we just take cities that we decide to rollout and run the analysis in them will give an optimistic (biased) estimate of the impact.
Comment: You are likely to overestimate the effect size. It's called the M-type error. See for several questions about it https://stats.stackexchange.com/search?q=type+M+error The M-type error is a bit difficult to compute. It depends on the true effect size, which is unknown. It is more an hypothetical value.
Comment: The over-estimation of effect size is a consequence of the selection on the basis of p-value, which is equivalent to selection on the basis of (scaled) effect size. Nonetheless, the observed effect size is still your best estimate of the effect size... Statistical test error rates and evidence do not play well together as they are in different universes. See here for more: https://link.springer.com/chapter/10.1007/164_2019_286
Comment: @MichaelLew better according to what standard? Wouldn't a shrinkage estimator have a lower squared error.
Comment: I guess so, maybe.
Answer: You are correct that selecting cities below a $p$-value threshold, like $p = .01$ in your example, and re-running the analysis on just those cities would give you an optimistic estimate of the treatment effect. The most common names for this effect is "selection bias" though other terms like "the winner's curse" are also used.
To see this quickly, imagine taking a thousand samples from a normal distribution $X_i \sim N(0,1)$, taking the 10 most positive observations, and taking the average of only those ten observations. This average will clearly be much larger than 0, the true mean.
Unbiased Estimation
Assuming you fit a model to the whole sample to select the cities as you described, the answer is that you can't get an unbiased estimate without making assumptions and the methods will be assumption-specific.
Taking a step back, you can get an unbiased estimate if you change your approach. The simplest way is to split your data into two data sets, and then use one data set to select the cities that get treatment and estimate the treatment effect in the selected cities using the other data set. The point estimates will be unbiased and you can construct confidence intervals with the correct coverages without any special methods. While simple, this isn't the most efficient use of all the data in the sample. There are numerous methods that build on this general approach but make more efficient use of the sample such as by repeating this procedure and combine the results in a way that preserves the unbiasedness property.
Related Literature
Three veins of work you can look for on this problem are "sample splitting", "post-selection inference", and "double/debiased machine learning" though there there are others. The literature on sample splitting is likely the least technical place to start because the other two settings often concern complications like high-dimensional problems. The problem is related to type M error as Sextus commented, but that concept is more for analyzing study designs since it involves considering hypothetical effect sizes (and sample sizes if done while planning a study).
Here are a few review paper:
Kuchibhotla AK, Kolassa JE, Kuffner TA. Post-selection inference. Annual Review of Statistics and Its Application. 2022 Mar 7;9:505-27.
Kennedy EH. Semiparametric doubly robust targeted double machine learning: a review. arXiv preprint arXiv:2203.06469. 2022 Mar 12. (pdf link)
Fithian W, Sun D, Taylor J. Optimal inference after model selection. arXiv preprint arXiv:1410.2597. 2014 Oct 9.(pdf link)
And an early paper on the subject that's incredibly short and readable:
Cox DR. A note on data-splitting for the evaluation of significance levels. Biometrika. 1975 Aug 1;62(2):441-4.


Cross validation for feature selection: still possible to overfit?

Question: I would like to find a good pair of predictors out of about
400 available pairs. To do this I am using LOO cross validation.
Since there are so many pairs available, don't I run into the
issue that the best pair could be a spurious finding? I'm thinking
this is much like multiple testing.
To be more concrete, for a given pair of predictors I perform LOO
cross validation to estimate the loss function using those predictors.
That is, for each of the n folds I leave out a sample, build the model using the given predictor pair, and then compute its loss function on the held out sample. These n loss values are then averaged to determine the merit of the predictor pair. I select the pair with the lowest average loss.
Much of what I have read about CV in optimizing models is to optimize over a few parameters (like lambda in Lasso) rather than many (e.g. whether to include a feature). So is there a rule of thumb for how many such tests one can perform with CV?
Comment: What do you mean by "pair of predictors"? It isn't a problem to use the LASSO with an arbitrarily large number of predictors to select only 2, but those 2 wouldn't necessarily be part of some pre-specified pair.
Comment: Let's say I have 30 predictors and want a classifier that uses two of them in a glm or other model.  Then I have (30*29/2)=435 possible pairs, any of which would be acceptable. I'm wondering if that number, 435, is such a large number of different cross validations that you are bound to get a spuriously low loss that doesn't actually validate.Thanks for the LASSO info; I will try adjusting lambda to get 2 predictors...
Comment: Why are you limited to a single pair of predictors?
Comment: For the moment just because it is easy to visualize on a scatter plot. Longer term, fewer predictors is better since we would need to purchase antibodies to make the measurements. We'd like to keep it cheap. :)
Answer:
don't I run into the issue that the best pair could be a spurious finding? I'm thinking this is much like multiple testing.

Yes, you are right: this is a multiple testing situation.
Here are some things you can do in order to still draw valid conclusions.

Before starting on the comparison, do a rough calculation whether your sample size allows you to do comparisons, e.g. by calculating binomial confidence intervals for some typical outcomes of the testing. This is particularly critical in case of classification if you measure and compare performance by sensitivity, specificity, overall accuracy or in fact any proportion of tested cases.
If this indicates that you cannot do even single comparisons, you should refrain from data-driven optimization and go directly for a suitable regularization, e.g. the LASSO or PLS (see below).
Compare using a so-called proper scoring rule (they are "better behaved" than those proportions and have among other beneficial statistical properties like reacting continuously and sensitively to slight improvements also lower variance.
All conclusions you draw about the predictive ability of the chosen pair of predictors should be drawn from independent test data. So either set aside a test set to validate the final two-predictor-model (if you have enough cases), or use a nested (aka. double) cross valiation / out of bootstrap: the inner loop to optimize the predictor pair, the outer loop to validate the final model.
Compare this outer validation performance to the "inner" optimization performance. If there's a large discrepancy (particularly if the optimization loop performance was (nearly) perfect), the optimization probably did not work, and you ended up more or less accidentally with a combination that just looks well for LOO and your data set.
check whether there were many predictor combinations that all yielded practially the same optimal performance
As the outer testing was nevertheless independent, even though you cannot claim that those 2 predictors are the best among the studied, you can still claim that you observed that performace on unknown test individuals for this predictor combination.

Even though for the LASSO typically λ is optimized using an inner optimization CV/bootstrap for optimal predictive performance, this is not necessary in your case: you just need a λ so that 2 coefficients stay non-zero. Finding this λ does not require any performance testing, so you can do this without the need for a nested validation design.
If the restriction is only because of visualization, you could also try PLS instead of the LASSO. With PLS you can select beforehand that you want to have the first 2 latent variables only, i.e. the two linear combinations that are (according to the PLS criterion) most useful for prediction. In contrast to the LASSO, PLS tends to include most predictors with differing weights into linear combinations and works well in situations with highly correlated predictors, so you'd need to think whether this makes sense for your data qnd application.
(As few as possible antibodies sounds more like LASSO, though)
Comment: Thanks @cbeleites. I had to spend a little time on Wikipedia to understand them, but these comments are helpful. I think I see your point on the LASSO. Let me be sure I understand, though. Basically you are saying to use LASSO to do the variable selection (it is essentially a one-parameter "fit" so doesn't suffer from the multiple comparisons issue). Then once those are chosen I can find the optimal classifier based on those two variables (e.g. using glm). By nesting this process within a CV framework I can also estimate my performance. Sound like an OK approach? BTW, is there a book to read?
Answer: This is the wrong way to do cross-validation.
Cross-validation should be used to select the best process for selecting important features. The feature-selection process should only have access to the training set of each fold. You then select the best process based on the performance on the validation folds.


Iterated Elimination of Weakly Dominated Strategies with Unknown Parameters

Question: The game-table is given below
$~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~$ $G = \begin{bmatrix} (5,2) & (3,1) \\ (3,5) & (\delta, 5)\\ (1,2) & (5,1) \end{bmatrix} $
It is quickly established that no pure strategies is strictly dominating. $\delta$ is unknown with $\delta$ $\in$ $\mathbb{R}$, so there is a basis for elimination of weakly dominating strategies.
How would we proceed from here?

For $\delta > 5$ its clear that $E[u(R2)]>E[u(R3]$ $\forall$ p.

However, this doesn't hold for the assumption made. And we are back to start.
Solving for $p$ with inequality between $E[u(R2)]>E[u(R3]$ we are restricting $\delta$ which goes against the assumption.
And likewise when solving for $\delta$.
Answer: Check the definition of what it means for a strategy to be weakly dominated by another strategy.
Say the strategies for Player 1 and 2 are $A=\{a_1,a_2,a_3\}$ and $B=\{b_1,b_2\}$ respectively. You'll find that $b_1$ weakly dominates $b_2$, so we may eliminate $b_2$. Then $a_1$ strictly (and therefore weakly) dominates $a_2$ and $a_3$, so we may eliminate those two.
Then $(a_1,b_1)$ is the only remaining strategy, which we obtain without knowledge of $\delta$.
Comment: This would also be understanding of it, although, I would hesitate to eliminate a weakly dominated strategy.
Comment: I mean your question was about elimination of weakly dominated strategies. Suppose you can only eliminate strictly dominated strategies. You can't eliminate anything unless you assume $\delta>5$, and even then you are left with four remaining strategies.
Comment: I appreciate your input. This is highly valued!


Send information in Soap Header to WCF service in C#

Question: i want a web application to create a service reference to my WCF service, insert information to the header of the soap call and call my WCF method.
i read about MessageContract attribute and declared one in the interface file:
<code>[MessageContract]
public class BasicServiceHeader
{
    [MessageHeader]
    public string myString;
}
</code>
my WCf interface is:
<code>   [ServiceContract]
public interface IBasicService
{

    [OperationContract]
    [WebGet(UriTemplate = "GetData?value={value}")]     // Add support for HTTP GET Requests
    string GetData(int value);}
</code>
i don't want the BasicServiceHeader to be passed as a parameter of GetData function , i want to keep the function as it is and to extract the BasicServiceHeader inside the function, can i do that ?
Answer: Client side, you can pass a header prior invoking the operation:
<code>MessageHeader messageHeader = MessageHeader.CreateHeader(_headerName, _headersNameSpace, _headerValue);
OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
</code>
and extract it using FindHeader service side
Comment: I don't think so but it supports the method I wrote earlier (using the OutgoingMessageHeaders property)
Comment: thank you, but i don't think my client has Framework 3.5. he works in the 'asmx way' , how can my client add a new header information to the soap method call in Framework 2.0?
Comment: Ah OK, does this help? http://blogs.msdn.com/b/kaevans/archive/2007/08/06/programmatically-insert-soapheader-into-soap-request-with-asmx-soapextensions.aspx
Comment: Before i get inside Soapextensions which seems as a very complex subject, can you tell me if WCF supports the SoapHeader attribute?. i found a simple example of using SoapHeader: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapheader.aspx , but i can't get it to work.


Using Select with ruby sockets what is a good way to ignore a given socket if a certain condition is met?

Question: A little background:
I've written a script that will act as a receiver for incoming Syslog/Email/Snmp(v1) traps.  It receives the data and stores it as a custom defined object.  My "messageSocket" is a means to control the behavior from a script outside of this general listener.  I want to send messages to this to either start/stop on command.  The start / stop essentially just change the state of the @running variable.
All of this works fine, except when I stop - if I trigger an event that creates an e-mail/trap etc it won't be registered while @running = false.  However, as soon as I set @running = true it will still see this socket data and analyze it.
I tried removing the given socket from my @descriptors array, I tried closing sockets, I tried setting res to different values.  I'm guessing I'm just missing something at this point, any assistance or guidance would be greatly appreciated!
def run()
   while 1 == 1

     res = select(@descriptors, nil, nil, nil)

     if res != nil then

        for sock in res[0]
          if sock == @mailSocket && @running == true then
            accept_new_connection
          elsif sock == @sysSocket && @running == true then
            get_syslog_message
          elsif sock == @snmpSocket && @running == true then
            get_snmp_message
          elsif sock == @mailSocket && @running == false then
            reject_mail
          elsif sock == @messageSocket then
            get_message()
          elsif sock == @snmpSocket && @running == false then
            #do something
          elsif sock == @sysSocket && @running == false then
            #do something
          end
        end
      end
    end
 end
   #run end
  end

Answer: If I recall, select is (sort of) level driven, not edge driven.  In other words, if select returns descriptors and you do nothing with those descriptors, the next time you call select it will tell you again.
So, if you are not running, you still have to read the data (or whatever else is appropriate for the event), or select will just tell you again later.
To confirm my hypothesis, print "foo" right after the call to select, make @running be false and then try to establish a connection.  If you see "foo" scolling by over and over, showing that select is not blocking, then I'm right.  If you don't, I'll eat my words/delete my answer.
As an aside, you can (and should) simply say @running instead of @running == true, and !@running instead of @running == false
Comment: I can tell you that you are right because that is similar to something I had done before.  I also just verified it, select doesn't remove anything from @descriptors.  So I guess the real question is with a UDP socket how do I get rid of that data?  For e-mail I just spit back the expected Failure response.  When I tried to just receive all the data for syslog/snmp and not do anything it didn't seem to work.
Comment: I'm going to accept your answer.  Thanks for making me revisit something I swear I had done :P  One of those days where I want to cry because its solved but also cry because I swear I tried it ;)

I created a reject_sys() method that just reads from the port and does nothing with the data.  Thanks!


Is a bijective continous map with open domain an open map?

Question: My doubts concerning this question appeared in the context of differential geometry, and my knowledge of topology is limited. My attempts at counterexamples all ended up being based on a non open domain, so I believed my conjecture was true.
I came to the conclusion (using the metric topology, the only one I know) it is equivalent to the continuity of the inverse function (and a quick search after confirmed this), but the counterexamples that use the metric topology (like Functions which are Continuous, but not Bicontinuous) all use non open domains.
The question is:
Let $f:U \rightarrow V$, with both $U$ and $V$ open sets (of $R^n$, if needed) be a continuous bijection. Is $f(A)$, where $A \subset U$ is an open set, an open set?
Comment: Specifically if $U$ and $V$ are open subsets of $\mathbb{R^n}$ then this is true. This is a very non trivial result called invariance of domain, and its proofs require some advanced tools.
Comment: But every function on an open subset of a topological space is a function on a topological space. And every topological space is an open subset of some other typological space. You seem to want open subsets of a particular type of topological space, like $\mathbb R^n.$ "Open domain" is meaningless, unless you are talking about subsets of a *particular* space.
Comment: @Mark I saw it now on wikipedia, thanks. I thought it should be a more trivial result. Please post your comment as answer for me to accept it
Comment: What do you mean by open domain? All topological spaces are open as subsets of themselves.
Comment: @ThomasAndrews Because I didn't mean to define a function in a topological space, but on an open subset of it. I could define a function on a closed subset of the space, for exemple.
Answer: This is true specifically for open subsets of $\mathbb{R^n}$. Even more can be said: if $U\subseteq\mathbb{R^n}$ is open, and $f:U\to\mathbb{R^n}$ is an injective, continuous function, then $f(U)$ is open in $\mathbb{R^n}$ (that is, you don't even need to assume that, it follows), and $f$ is an open map. This nontrivial result is called invariance of domain.
Answer: In general, this is false. Take $X$ to be any  set with at least two elements and let $\mathscr T_1=\mathcal P(X)$ be the discrete topology and $\mathscr T_2=\{X,\emptyset\}$ the trivial topology.
Let $f:(X,\mathscr T_1)\to (X,\mathscr T_2)$ be the identity function. Then $f$ is continuous (every function is), bijective, and given any nontrivial $X_0\subset X$ we have $X_0$ open and $f(X_0)$ not open.


Is it reasonable to delete a large number of outliers from a dataset?

Question: I need some advice on what is a reasonable number of cases to be deleted as outliers.
I have applied outlier detection methods to identify univariate and multivariate outliers from my dataset. Alltogether 30% of the data was classified as outliers.
If I delete all of these outliers, my results appear to improve. Also, after deleting the outliers my sample size is still good (i.e., n=300).

Is it reasonable to delete all the cases classified as outliers?
Comment: Why do you think that you need to remove the outliers? Do you have reason to believe that they are "bad" data, i.e. data entry errors, etc? In general, 0 is a reasonable number of outliers to remove.
Comment: Good point, @Aniko.  Without further information demonstrating that an "outlier" is mistaken or irrelevant, 0 is the *only* defensible number of outliers to remove.  However, it's possible (and usually a good idea) to conduct analyses both with and without the outliers to assess how much the outliers influence the results.
Comment: "to delete a large number of outliers from a dataset " is the same thing as "number of cases to be deleted as outliers"  ?
Answer: I would be more than suspicious, if someone told me that 30% of my sample are outliers ...
Rather than blindly trusting a canned routine I would carefully analyze the data and try to find out why an outlier is an outlier. Is it a "bug" or a "feature"? Is it measurement error? Does your sample cover different sub-populations (mixture)?
Moreover, the detection of outliers involves the more or less arbitrary definition of a threshold, which separates "good" and "bad". You should assess if these thresholds are sensible. It could thus be a good idea to move the goalposts and to see what happens.
Also note that rather than dropping observations, you could use robust statistical techniques if you are concerned about outliers.
Answer: Absolutely not: Outliers are points that are distant from the bulk of other points in a distribution, and diagnosis of an "outlier" is generally done by comparison to some assumed distributional form.  Although outliers can occasionally be caused by measurement error, diagnosis of outliers can also occur when the data follows a distribution with high kurtosis (i.e., fat tails), but the analyst compares the data points to an assumed distributional form with low kurtosis (e.g., the normal distribution).
The entire concept of an "outlier" really does far more harm than good.  All that is really needed is to recognise that it's okay to remove data points that have been measured incorrectly, but it's not okay to remove data points that are legitimate observations (except for the limited purposes of sensitivity analysis).  Unless the statistical analyst has evidence to conclude that an "outlier" has occurred due to measurement error, it is almost always the case that it is identified because the data follows a distribution with high kurtosis (i.e., fatter tails) than the assumed distributional form.  To conclude that this reflects some problem with the data is tantamount to claiming that reality must conform to your statistical assumptions, and when it does not it has made an unfortunate error, that you will rectify in your analysis by removing parts of reality that are non-compliant with your assumptions.
In any case where an analyst identifies a large amount like 30% of the data as "outliers", it is likely either that the outlier test has been incorrectly applied, or the outlier test is based on a distributional assumption that assumes much thinner tails than the data, and is therefore falsified by the data.  In either case, it is a sure sign that something has gone wrong.  Personally, I would never trust any analysis that has removed a large proportion of the data as "outliers".
In view of this, I would suggest that you first consider whether there are any data points that have incorrect values due to measurement error.  If you have good reason to think this has occurred, it is legitimate to remove these and note their removal in your analysis.  (Bear in mind that unless the people making the observations are extremely incompetent, then realistically you should not have measurement errors for more than a small number of your points.)  If you still find you have high numbers of "outliers" then this almost certainly means you are using a statistical model with a distribution that has thinner tails than is warranted by the data (e.g., you are assuming a normal distribution, but there is substantial excess kurtosis).  Find the sample kurtosis of the residuals in your data and compare this to your assumed distributional form to check.  If your assumed form does not match the data, consider replacing this with a distribution with higher kurtosis (e.g., you might replace the normal distribution with a t-distribution or generalised error distribution).


data.table merge with no obvious / unique join key

Question: Problem: Suppose I have the below data.table object (ex) which I want to merge with date_frame object such that I get the desired output. The desired output shall be in the form such that the C_ states show only up once per CURRENT_DATE. However, if there is more than one E state per iid then these E states (with respective start date) shall show up multiple times per CURRENT_DATE. Any idea how to do that? Many thanks!!
## Desired Output
iid   state   start       CURRENT_DATE
1        E    2020-01-02   2020-03-31
1        C_1  2020-01-03   2020-03-31
1        C_2  2020-01-05   2020-03-31
1        E    2020-01-06   2020-03-31
1        E    2020-01-08   2020-03-31
2        E    2020-01-10   2020-03-31
1        E    2020-01-02   2020-04-01
....

Toy example
library(data.table)

ex <- data.table(iid = c(1, 1, 1, 1, 1, 2),
                 state = c("E", "C_1", "C_2", "E", "E", "E"),
                 start = c("2020-01-02", "2020-01-03", "2020-01-05", "2020-01-06", "2020-01-08", "2020-01-10"))

s <- seq.Date(from = ex[, min(ymd(start))],
              to = Sys.Date(),
              "days")

date_frame <- CJ(CURRENT_DATE = s, iid = unique(ex$iid))

## not working
merge(ex, date_frame, all = TRUE, by = "iid")

Answer: Are you looking for something like:
ex[CJ(CURRENT_DATE=seq(max(start), min(start), by="-1 day"), iid, unique=TRUE),
    on=.(iid), allow.cartesian=TRUE]

output:
    iid state      start CURRENT_DATE
 1:   1     E 2020-01-02   2020-01-02
 2:   1   C_1 2020-01-03   2020-01-02
 3:   1   C_2 2020-01-05   2020-01-02
 4:   1     E 2020-01-06   2020-01-02
 5:   1     E 2020-01-08   2020-01-02
 6:   2     E 2020-01-10   2020-01-02
 7:   1     E 2020-01-02   2020-01-03
 8:   1   C_1 2020-01-03   2020-01-03
 9:   1   C_2 2020-01-05   2020-01-03
10:   1     E 2020-01-06   2020-01-03
11:   1     E 2020-01-08   2020-01-03
12:   2     E 2020-01-10   2020-01-03
13:   1     E 2020-01-02   2020-01-04
14:   1   C_1 2020-01-03   2020-01-04
15:   1   C_2 2020-01-05   2020-01-04
16:   1     E 2020-01-06   2020-01-04
17:   1     E 2020-01-08   2020-01-04
18:   2     E 2020-01-10   2020-01-04
19:   1     E 2020-01-02   2020-01-05
20:   1   C_1 2020-01-03   2020-01-05
21:   1   C_2 2020-01-05   2020-01-05
22:   1     E 2020-01-06   2020-01-05
23:   1     E 2020-01-08   2020-01-05
24:   2     E 2020-01-10   2020-01-05
25:   1     E 2020-01-02   2020-01-06
26:   1   C_1 2020-01-03   2020-01-06
27:   1   C_2 2020-01-05   2020-01-06
28:   1     E 2020-01-06   2020-01-06
29:   1     E 2020-01-08   2020-01-06
30:   2     E 2020-01-10   2020-01-06
31:   1     E 2020-01-02   2020-01-07
32:   1   C_1 2020-01-03   2020-01-07
33:   1   C_2 2020-01-05   2020-01-07
34:   1     E 2020-01-06   2020-01-07
35:   1     E 2020-01-08   2020-01-07
36:   2     E 2020-01-10   2020-01-07
37:   1     E 2020-01-02   2020-01-08
38:   1   C_1 2020-01-03   2020-01-08
39:   1   C_2 2020-01-05   2020-01-08
40:   1     E 2020-01-06   2020-01-08
41:   1     E 2020-01-08   2020-01-08
42:   2     E 2020-01-10   2020-01-08
43:   1     E 2020-01-02   2020-01-09
44:   1   C_1 2020-01-03   2020-01-09
45:   1   C_2 2020-01-05   2020-01-09
46:   1     E 2020-01-06   2020-01-09
47:   1     E 2020-01-08   2020-01-09
48:   2     E 2020-01-10   2020-01-09
49:   1     E 2020-01-02   2020-01-10
50:   1   C_1 2020-01-03   2020-01-10
51:   1   C_2 2020-01-05   2020-01-10
52:   1     E 2020-01-06   2020-01-10
53:   1     E 2020-01-08   2020-01-10
54:   2     E 2020-01-10   2020-01-10
    iid state      start CURRENT_DATE

data:
library(data.table)
ex <- data.table(iid = c(1, 1, 1, 1, 1, 2),
    state = c("E", "C_1", "C_2", "E", "E", "E"),
    start = c("2020-01-02", "2020-01-03", "2020-01-05", "2020-01-06", "2020-01-08", "2020-01-10"))
ex[, start := as.IDate(start)]


SQL Server 2005 DROP LOGIN syntax error

Question: This snippet of code (it's just a repeater, not the actual code I'm going to run) throws a syntax error on the DROP LOGIN command:
declare @obso_user varchar(16)
set @obso_user = 'BEN_VA\20362781'
DROP LOGIN @obso_user

Does anyone know why?
Answer: You can't use a variable with DROP LOGIN.  To do this you'll need to use dynamic SQL:
DECLARE @obso_user VARCHAR(16)
SET @obso_user = 'BEN_VA\20362781'

DECLARE @sql NVARCHAR(100)
SET @sql = N'DROP LOGIN [' + @obso_user + ']'
EXEC sp_executesql @sql

Put that in a CURSOR to do more than the single login:
DECLARE @obso_user VARCHAR(16)
DECLARE @sql NVARCHAR(100)

DECLARE USER_CUR CURSOR FOR
SELECT [Something] FROM User_Table

OPEN USER_CUR
FETCH NEXT FROM USER_CUR INTO @obso_user
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = N'DROP LOGIN [' + @obso_user + ']'
    EXEC sp_executesql @sql
    FETCH NEXT FROM USER_CUR INTO @obso_user
END

CLOSE USER_CUR
DEALLOCATE USER_CUR

Comment: Thanks.  A co-worker showed me the EXECUTE() command, and I used that.
Comment: Regarding cursors: that's exactly what I was doing.  :)  The snippet was just a simplified "repeater" to show the error.
Comment: Cool.  Regarding EXECUTE() vs. sp_executesql - know that sp_executesql allows for query plan reuse and also parameterized statements (which help to prevent SQL injection attacks).

SQL Server 2005 DROP LOGIN syntax error


Scrapy, crawling a dynamic page with multiple pages

Question: For an assignment I am trying to build a spider which is able to fetch data from the "www.kaercher.com" webshop. All the products in the webshop are being called by an AJAX call. In order to load in more products, a button named "show more products", has to be pressed. I managed to fetch the required data from the corresponding URL which is being called by the AJAX Call.
However, for my assignment, I am suppose to fetch all (all products/pages) of a certain product. I've been digging around but I can't find a solution. I suppose I am suppose to do something with "isTruncated = true", true indicates that more products can be loaded, false means that there are no more products. (FIXED)
When I manage to fetch the data from all the pages, I need to find a way to fetch all the data from a list of products (create a .csv file with multiple kaercher products, each product has a unique ID which can be seen in the URL, in this case the ID 20035386 is for the high pressure washer). (FIXED)
Links:
Webshop: https://www.kaercher.com/nl/webshop/hogedrukreinigers-resultaten.html
High pressure washer: https://www.kaercher.com/nl/webshop/hogedrukreinigers-resultaten.html
API Url (page1): https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/20035386?page=1&size=8&isocode=nl-NL
OLD CODE
Spider file
import scrapy
from krc.items import KrcItem
import json

class KRCSpider(scrapy.Spider):
    name = "krc_spider"
    allowed_domains = ["kaercher.com"]
    start_urls = ['https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/20035386?page=1&size=8&isocode=nl-NL']

    def parse(self, response):
        item = KrcItem()
        data = json.loads(response.text)
        for company in data.get('products', []):
            item["productid"] = company["id"]
            item["name"] = company["name"]
            item["description"] = company["description"]
            item["price"] = company["priceFormatted"]
            yield item

Items file
import scrapy

class KrcItem(scrapy.Item):
    productid=scrapy.Field()
    name=scrapy.Field()
    description=scrapy.Field()
    price=scrapy.Field()
    pass

NEW CODE
EDIT: 15/08/2019
Thanks to @gangabass I managed to fetch data from all of the product pages. I also manages to fetch the data from different products which are listed in a keyword.csv file. This enables me to fetch data from a list of products. See below for the new code:
Spider file (.py)
import scrapy
from krc.items import KrcItem
import json
import os
import csv

class KRCSpider(scrapy.Spider):
    name = "krc_spider"
    allowed_domains = ["kaercher.com"]
    start_urls = ['https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/20035386?page=1&size=8&isocode=nl-NL']

    def start_requests(self):
        """Read keywords from keywords file amd construct the search URL"""

        with open(os.path.join(os.path.dirname(__file__), "../resources/keywords.csv")) as search_keywords:
            for keyword in csv.DictReader(search_keywords):
                search_text=keyword["keyword"]
                url="https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/{0}?page=1&size=8&isocode=nl-NL".format(
                    search_text)
                # The meta is used to send our search text into the parser as metadata
                yield scrapy.Request(url, callback = self.parse, meta = {"search_text": search_text})

    def parse(self, response):
        current_page = response.meta.get("page", 1)
        next_page = current_page + 1

        item = KrcItem()
        data = json.loads(response.text)
        for company in data.get('products', []):
            item["productid"] = company["id"]
            item["name"] = company["name"]
            item["description"] = company["description"]
            item["price"] = company["priceFormatted"].replace("\u20ac","").strip()
            yield item

        if data["isTruncated"]:
            yield scrapy.Request(
                url="https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/20035386?page={page}&size=8&isocode=nl-NL".format(page=next_page),
                callback=self.parse,
                meta={'page': next_page},
            )

Items file (.py)
import scrapy

class KrcItem(scrapy.Item):
    productid=scrapy.Field()
    name=scrapy.Field()
    description=scrapy.Field()
    price=scrapy.Field()
    producttype=scrapy.Field()
    pass

keywords file (.csv)
keyword,keywordtype
20035386,Hogedrukreiniger
20072956,Floor Cleaner

Answer: You can use response.meta to send current page number between requests:
def parse(self, response):
    current_page = response.meta.get("page", 1)
    next_page = current_page + 1

    item = KrcItem()
    data = json.loads(response.text)
    for company in data.get('products', []):
        item["productid"] = company["id"]
        item["name"] = company["name"]
        item["description"] = company["description"]
        item["price"] = company["priceFormatted"]
        yield item

    if data["isTruncated"]:
        yield scrapy.Request(
            url="https://www.kaercher.com/api/v1/products/search/shoppableproducts/partial/20035386?page={page}&size=8&isocode=nl-NL".format(page=next_page),
            callback=self.parse,
            meta={'page': next_page},
        )

Comment: Thank you very much for your solution! I edited my Question and posted my full code in case somebody stumbles upon the same issue.


QBluetooth BLE advertising does not work on ubuntu (BlueZ)

Question: I tried to advertise a BluetoothLE service using Qt today. I started by testing the Bluetooth Low Energy Heart Rate Server Example and it worked just fine on my Android phone. On my Ubuntu 18.04 Laptop though, I could not get it to work.
When I run the example as root, I get the error qt.bluetooth.bluez: received advertising error.
After enabling debug messages, I got this log:
qt.bluetooth: Using BlueZ kernel ATT interface
qt.bluetooth.bluez: Bluez 5 detected.
qt.bluetooth.bluez: Starting to advertise
qt.bluetooth.bluez: sending command; ogf: 8 ocf: 7
qt.bluetooth.bluez: command sent successfully
qt.bluetooth.bluez: HCI event triggered, type: e
qt.bluetooth.bluez: command 7 failed with status 12
qt.bluetooth.bluez: reading power level failed, leaving it out of the advertising data
qt.bluetooth.bluez: advertising parameters: "000800080000000000000000000700"
qt.bluetooth.bluez: advertising data: "18020106100948656172745261746553657276657203030d1800000000000000"
qt.bluetooth.bluez: scan response data: "15100948656172745261746553657276657203030d1800000000000000000000"
qt.bluetooth.bluez: sending command; ogf: 8 ocf: 10
qt.bluetooth.bluez: command sent successfully
qt.bluetooth.bluez: HCI event triggered, type: e
qt.bluetooth.bluez: command 10 failed with status 12
qt.bluetooth.bluez: Advertising disable failed, ignoring
qt.bluetooth.bluez: sending command; ogf: 8 ocf: 6
qt.bluetooth.bluez: command sent successfully
qt.bluetooth.bluez: HCI event triggered, type: e
qt.bluetooth.bluez: command 6 failed with status 12
qt.bluetooth.bluez: received advertising error

Does anybody know what all these error messages mean?
Thanks for help!
Answer: I ran into a similar issue when attempting to use https://github.com/nymea/nymea-networkmanager on Intel NUC devices. Any time it attempted to issue Bluetooth LE commands I would see 'command disallowed' via btmon.
Issuing hcitool cmd 0x03 0x000 would reset the device and get it to start advertising, but it would soon crash again right after. It appears that a lot of bluetooth changes were introduced in the 4.19 kernel and have been causing issues with LE system calls.
I was able to get it to work by using an older Ubuntu version with 4.15 kernel.
More details here
https://github.com/nymea/nymea-networkmanager/issues/46
Comment: Hi Nathan, I actually got it to work by upgrading to Ubuntu 20.04 which come with a 5.X Kernel :)


Codeigniter error shows in my homepage call to a member function on null

Question: I just started my website developing using the CodeIgniter framework.
My working steps are given below:
1. I created a controller named by Home.php
2. Then I go to the view folder and created a file named by index.php under sub-directory 'home'.
3. When I browse my website it shows me an error

Fatal error: Call to a member function view() on null in /home/ontutors/public_html/application/controllers/Home.php on line 18

Home.php code
<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    /**
    *
    */
    class Home extends CI_Controller
    {
        function __construct()
        {

        }
        function index()
        {
            //echo "Hello welcome";
            $this->load->view('home/index');
        }
    }
?>

Comment: Write `parent::__construct();` inside your `__construct()` and check
Comment: Which version of CodeIgniter and PHP are you using?
Comment: Thanks.Its working.
Answer: <?php
        defined('BASEPATH') OR exit('No direct script access allowed');

        /**
        *
        */
        class Home extends CI_Controller
        {
            function __construct()
            {
               parent::__construct();
            }
            function index()
            {
                //echo "Hello welcome";
                $this->load->view('home/index');
            }
        }
    ?>

Overwrite your code like this.
Answer: <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller
{
    function index()
    {
        //echo "Hello welcome";
        $this->load->view('home/index');
    }
}
 ?>


Add custom fields in a serializer based from instance criteria

Question: I created a Django 1.10 app with Rest-Framework, I have trouble making any custom field that will yield the date range of a date field in M2MField, i will explain my problem with my code.
class FoodConsumption(models.Model):
    '''
        An item consume by animal.
    '''

    animal = models.ForeignKey(
        Animal, related_name='animal_food_consumptions', on_delete=models.CASCADE)

    food = models.ForeignKey(
        Food, related_name='food_consumptions', on_delete=models.CASCADE)

    date = models.DateField()

class FoodConsumptionTransaction(models.Model):
    herd = models.ForeignKey(
        Herd, related_name="food_consumption_transactions",
        on_delete=models.CASCADE
    )

    food_consumptions = models.ManyToManyField(FoodConsumption)

And here is my serializer to display the FoodConsumptionTransaction data.
class FoodConsumptionTransactionListSerializer(serializers.ModelSerializer):

    class Meta:
        model = FoodConsumptionTransaction
        fields = ['id', 'herd', 'food_consumptions']

Create a couple of FoodConsumption:
from dateutil import parser

fc1 = FoodConsumption.objects.create(
    animal=animal_obj_1,
    food=food_obj_1,
    date=parser.parse('2017-04-06').date() // min date
)

fc2 = FoodConsumption.objects.create(
    animal=animal_obj_2,
    food=food_obj_2,
    date=parser.parse('2017-04-08').date()
)

fc3 = FoodConsumption.objects.create(
    animal=animal_obj_3,
    food=food_obj_2,
    date=parser.parse('2017-04-10').date() // max date
)

# add to the transaction

fc_trans = FoodConsumptionTransaction.objects.create(
    herd=herd_obj_1
)
fc_trans .food_consumptions.add(fc1, fc2, fc3)

Now when getting data of FoodTransaction by herd i get this list:
[
   {
       "id": 1,
       "herd": 1,
       "food_consumptions": [1, 2, 3], # pks of FoodComsumption
   }
]

But how can i create extra fields based from food_consumptions field , food.date (min and max)?
[
    {
        "id": 1,
        "herd": 1,
        "food_consumptions": [1, 2, 3], # pks of FoodComsumption
        "min_date": '2017-04-6',    # get the min date of food
        "max_date": '2017-04-10',   # get the max date of food
    }
]

Answer: What you want are serializers.
You can override the default result returned from the serializer's to_representation() function. And add min_date and max_date fields to the result produced.
Such as
class  FoodSerializer(serialiers.ModelSerializer):
    def to_representation(self, instance):
        result = super(FoodSerializer, self).to_representation(instance)
        result['min_date'] = min[each.date for each in instance.m2mfield.all()]
        result['max_date'] = max[each.date for each in instance.m2mfield.all()]
        return result

Comment: hi there! thanks for your response, got an invalid syntax in for each loop
Comment: That was some simple logic, you can make your own to get `min` and `max` of dates.
Comment: i think this one help me a lot.. thanks for a good idea, i did learn something useful here
Comment: instead of doing `min/max [each.date for each in instance.m2mfield.all()]` i replace it with `instance.m2mfield.earliest('date').date` for min and `instance.m2mfield.latest('date').date` for max
Comment: nice workaround, thanks for info. I keep forgetting these utilities already in the django.


DefaultObjectPool do not dispose objects

Question: c# documentation says:
When DefaultObjectPoolProvider is used and T implements IDisposable:

Items that are not returned to the pool will be disposed.

I've try some test app to check this out.
Looks like it's not disposing, why?
Example code:
using System;
using Microsoft.Extensions.ObjectPool;

namespace ConsoleApp9
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Started");
            using var a1 = new MyClass("a1");
            using var b1 = new MyClass("b1");

            var pool = new DefaultObjectPool<MyClass>(new DefaultPooledObjectPolicy<MyClass>());
            var a = pool.Get();
            var b = pool.Get();

            //pool.Return(a);
            //pool.Return(b);

            Console.WriteLine("Finished");
        }
    }

    public class MyClass : IDisposable
    {
        public MyClass()
        {
        }

        public MyClass(string id)
        {
            Id = id;
        }

        public void Dispose()
        {
            Console.WriteLine($"Disposing MyClass.Id={Id}");
        }

        public string Id { get; } = Guid.NewGuid().ToString();
    }
}

output:
Started
Finished
Disposing MyClass.Id=b1
Disposing MyClass.Id=a1

Comment: I think you need to use `DefaultObjectPoolProvider.Create(...)` instead of `new DefaultObjectPool(...)`.
Comment: you r right, that's get an DisposableObjectPool instead of DefaultObjectPool
Answer: We should use DefaultObjectPoolProvider.Create<T>, then it will return DisposableObjectPool instead of DefaultObjectPool:
Console.WriteLine("Started");
using var a1 = new MyClass("a1");
using var b1 = new MyClass("b1");

var p = new DefaultObjectPoolProvider();
var pool = p.Create<MyClass>();

var a = pool.Get();
var b = pool.Get();

Console.WriteLine("Finished");

output:
Started
Finished
Disposing MyClass.Id=b1
Disposing MyClass.Id=a1


Threshold voltage for high and low in a BeagleBone

Question: It is said that 0V indicates a LOW and 3.3V indicates a HIGH. I was wondering if there is a threshold voltage for the HIGH and LOW logic level. Tried searching it, but couldn't find anything.
Thanks in advance.
Answer: The specs you want are VIH and VIL.  Download the datasheet from TI for more info.
VIH is 2.0v and VIL is 0.8v.  The hysteresis minimum is 0.265v.


error Uncaught SyntaxError: missing) after argument list

Question: Eu estou querendo colocar um menu no meu blog, que não está funcionando CSS3 Responsive Menu Dropdown não estão funcionando!... E eu acho que isso pode acontecer devido a esse erro no javascript ( mas como não tenho certeza se o que está causando o problema é o erro, separei a pergunta para não ficar confuso):

Uncaught SyntaxError: missing) after argument list

Este é o javascript

E está é a parte do código relacionada ao erro:
cssmenu.find(".has-sub").prepend('');

Eu pesquisei sobre este erro, mas as respostas eram bem exclusivas para cada código... Ai fica difícil olhar um código e saber qual é o problema.
blogs com o menu/erro:
http://95555558888899999999.blogspot.com.br/
http://menuteste158.blogspot.com.br/

Obs.: Existe algum site de erros onde tem listado vários erros e suas
  causas? Seria de longe muito bom.
Answer: É por conta desse trecho na função resizeFix onde tens <= no código:
if ($(window).width() <= mediasize) {
   cssmenu.find('ul').hide().removeClass('open');
}

O correto seria:
if ($(window).width() <= mediasize) {
   cssmenu.find('ul').hide().removeClass('open');
}

Comment: Tente usar [CDATA](http://pt.stackoverflow.com/q/103424/4808) nesse teu código, ficando [assim](http://pastebin.com/raw/12Vtst1G).
Comment: sim @renan obrigada, era por este motivo que o menu não estava funcionando, e eu achava que cdata não servia para nada, agora vale dá uma pesquisada no assunto... ;)
Comment: Mas o blog não aceita `<=`  por isso eu modifiquei...
Comment: acho que se eu criasse um javascript externo, talvez funcionasse. Vou tentar.
Comment: Como assim "não aceita"? Se está dentro de um bloco `
Comment: porque arquivo xml não aceita.. Se eu deixar aparece uma mensagem de erro... `The content of elements must consist of well-formed character data or markup.` http://stackoverflow.com/questions/4338538/error-parsing-xhtml-the-content-of-elements-must-consist-of-well-formed-charact


hide SlidingDrawer on screen tap

Question: By default, in Android SlidingDrawer designed to use a child view as a handle to open or hide its self. But here want to hide my SlidingDrawer by tapping on any place on the screen that are not inside the opening SlidingDrawer area. Just like a pop-up menu hides when user taps on other places on desktop/web based app.
Any way to do that?
My slidingDrawer is made to be not full screen, so it allows user to tap on other places on the screen.
Answer: On the view that is outside the sliding drawer,
Add a TouchListener in which you call slidingDrawer.close(); or slidingDrawer.animateClose(); on the sliding drawer.

hide SlidingDrawer on screen tap


If $ax^2+bx+c<x$, for any $x$ then is $b\ge 1$ or $b <1$ or $c=0$ or $a\le 0$?

Question: I tried by using the following :
 $ax^2+bx+c<x$
$\implies (b-1)^2<4ac$
But $ax^2+bx+c<x$ also $\implies$ a parabola lying underneath a straight line $y=x$ such that the parabola faces the origin from negative y axis. In that case is $b<1$ the correct choice?
Answer: $ax^2+{(b-1)}x + c < 0$ for all $x$ when $a\neq 0$ implies both
(1) $a < 0$
(2) $(b-1)^2-4ac < 0$
For $a=0$, this becomes a linear inequality $(b-1)x+c<0$ and is true for all $x$ when $b=1$ and $c<0$
Comment: (1) is not true. We can  have $a=0,b =1,c=-1$ for example.
Comment: Thank you for the notice. Have edited to include the linear case.
Comment: There is a typo at the end.
Comment: So, what is the answer ?
Answer: Consider the function
$$ax^2+(b-1)x+c<0.$$
If $a\ne0$, it is a parabola. To stay in the negatives, it needs to be concave down, i.e. $a<0$. Then it may not have real roots, $(b-1)^2<4ac$ (implicitly, $c<0$).
If $a=0$, the function is linear and takes both signs, unless $b=1$. In this case, only $c<0$ remains.
The complete condition is
$$(a<0\land (b-1)^2<4ac)\lor (a=0\land b=1\land c<0).$$
Only $a\le0$ is compatible.