What summary statistics to use with categorical or qualitative variables?

Question: Just to clarify, when I mean summary statistics, I refer to the Mean, Median Quartile ranges, Variance, Standard Deviation.
When summarising a univariate which is categorical or qualitative, considering both Nominal and Ordinal cases, does it make sense to find its mean, median, quartile ranges, variance, and standard deviation?
If so is it different than if you were summarising a continuous variable, and how?
Comment: I barely see any difference between categorical and qualitative variable, except one of terminology. Anyway, that would be very difficult to compute anything like mean or SD on a nominal variable (e.g., hair color). Maybe you are thinking of categorical variables with ordered levels?
Comment: Nope, if the categorical data has an order or ranked levels they are said to be Ordinal according to this website: [http://www.stats.gla.ac.uk/steps/glossary/presenting_data.html#orddat], and it says "You can count and order, but not measure, ordinal data"
Comment: But am I wrong?
Answer: In general, the answer is no. However, one could argue that you can take the median of ordinal data, but you will, of course, have a category as the median, not a number. The median divides the data equally: Half above, half below. Ordinal data depends only on order.
Further, in some cases, the ordinality can be made into rough interval level data. This is true when the ordinal data are grouped (e.g. questions about income are often asked this way). In this case, you can find a precise median, and you may be able to approximate the other values, especially if the lower and upper bounds are specified: You can assume some distribution (e.g. uniform) within each category.  Another case of ordinal data that can be made interval is when the levels are given numeric equivalents. For example: Never (0%), sometimes (10-30%), about half the time (50%) and so on.
To (once again) quote David Cox:

There are no routine statistical questions, only questionable
  statistical routines
Comment: You provide good related information but I think in response to chl question, the OP made it clear that he is talking about categorical data that is not ordinal.  So your response is really not an answwer but I am not one who would give a downvote.  But I do think you should change it to a comment.
Comment: No, I won't downvote the answer as I do think it has added some value to my limited understanding. I should have made it clear in my description that I am considering both Ordinal and Nominal Summary statistics, so the fault is mine.
Answer: As has been mentioned, means, SDs and hinge points are not meaningful for categorical data.  Hinge points (e.g., median and quartiles) may be meaningful for ordinal data.  Your title also asks what summary statistics should be used to describe categorical data.  It is standard to characterize categorical data by counts and percentages.  (You may also want to include a 95% confidence interval around the percentages.)  For example, if your data were:
<code>"Hispanic"         "Hispanic"        "White"             "White"
"White"            "White"           "African American"  "Hispanic"
"White"            "White"           "White"             "other"
"White"            "White"           "White"             "African American"
"Asian"
</code>
You could summarize them like so:
<code>White             10 (59%)
African American   2 (12%)
Hispanic           3 (18%)
Asian              1 ( 6%)
other              1 ( 6%)
</code>
Answer: If you have nominal variables there is no ordering or distance function. So how could you define any of the summary statistics that you mention?  I don't think you can. Quartiles and range at least require ordering and means and variance require numerical data. I think bar graphs and pie chart are typical examples of the proper ways to summarize qualitative variables that are not ordinal.
Comment: They are certainly popular! But I think it's part of our responsibility, as experts in the field, to make pie charts *less* popular.
Comment: @Michael "A table is nearly always better than a dumb pie chart; the only worse design than a pie chart is several of them ... pie charts should never be used."--Tufte.  "Data that can be shown by pie charts always can be shown by a dot chart. ... in the 1920's a battle raged on the pages of *JASA* about the relative merits of pie charts and divided bar charts ... both camps lose because other graphs perform far better than either divided bar charts or pie charts."--Cleveland. As you know, Cleveland is not prescriptive: this is as strong as he gets about anything.
Comment: BTW, @Michael, I do agree with you and the arguments you are making in this thread (which I find convincing and well presented), but as a moderator I have to convey strong objections voiced by community members concerning the "tone of voice" you are adopting. Please follow the site's etiquette: stick to the subject and don't attack others. Don't even write stuff that might sound like an attack, even in jest.  Of course the same admonition extends to everybody.
Comment: Cleveland showed, first, that people are worse at perceiving angular measurement than linear distance. Second, that changing the colors in a pie chart changed people's perceptions of the size of the slices. Third, that rotating the pie chart changed people's perceptions of the size of the slices. Fourth that people had trouble ordering the slices from largest to smallest unless they were very different sized. Cleveland dot plots avoid all these.
Comment: We have had some discussions around pie charts on this thread: [Problems with pie charts](http://stats.stackexchange.com/q/8974/930).
Comment: @PeterFlom My point was not to list all the possiblr graphical procedures for summarizing qualitative data.  I really want to emphasize that it is really proportion that can be compared and the way the proportions are distributed across the categories.  For visually recognizing differences in proportions I think bar charts are easier to visualize than pie charts but they are just two popular ways to summarize categorical data.  I don't want to say they are the best as I am not familiar with all the available methods.
Comment: Pie charts distort the data; with large numbers of categories they are unreadable. People are bad at perceiving angles.  See books by William S. Cleveland (he also has a web site).
Comment: Well @MichaelChernick you are certainly free to not see it that way, but the world's experts in graphical perception disagree with you.
Comment: I agree about charts, but not about your recommendation of pie charts. Cleveland dot plots are much superior. I have a presentation on this coming up at NESUG: [Graphics for univariate data: Pie is delicious but not nutritious](http://www.statisticalanalysisconsulting.com/graphics-for-univariate-data-pie-is-delicious-but-not-nutritious/)
Comment: I love Tukey's stem-and-leaf diagrams.  They convey more information than histograms.  But i am not going to say eliminate histograms.  You argue that people display poor pie charts.  Then I think the answer is to teach the proper way to do do pie charts.  Maybe sometimes very small categories should be aggregated.  Other times there may be good reasons to show the very small categories (not to compare the very thin slices but rather to see how many small ones there are and how mcuh smaller they are compared to the big categories).
Comment: When you have 4 equal categories it may be a little easier for some to see a pie chart with 4 right angles or 45 degree angles when there are 8 categories that are equal. Bar charts may not convey this as easily. So while I agree that some people are bad a perceiving angles (other than 180 degree angles, 90 degree angles or 45 angles) and some people make bad charts, I do not see that as justification to ban pie charts.
Comment: I really do think that if you are going to use the logic that misuse of a method means it should never be used when speaking of pie charts then it is not a big stretch to apply that to all of statistics.  Of course I know he wouldn't except that.
Comment: Thanks so much @Macro. Huber is a saint. i can try to be that way but it is hard.
Comment: @PeterFlom.  I don't know. What is your big objection to pie charts?  They never did anything bad to me.
Comment: @Peter Flom I am familiar with some of Bill Cleveland's work and books but not with all this negativity about pie charts. Can you give me a good argument against them?  We all know that there are many presentations of bad graphics and there is an art to presenting good graphics.  Tufte has been excellent at showing the bad and demonstrating ways to improve it. Huff's classic "How to Lie with Statistics" shows the many unethical ways subtle or not so subtle manipulation cn change the story that a graph conveys.  But do we stop showing linear trends because we canlie by doing scla manipulations?
Comment: Gee Peter instead of just standing behind Bill Cleveland why don't you just give me a cogent argument for abolishing them? I can appreciate the argument about difficulty perceiving angle differences and about people's tendency to make poor ones but where is your counter to my argument.  Do Cleveland, Tufte and other go so far as to outright state that pie chart should never be used?  That seems to me would be more controversial than what I am saying.  I wonder what John Tukey would have said about abandoning pie charts just because they can be misused.  Maybe all of statistics should be band.
Comment: Ther probably isn't a single technique or graphical/exploratory method that hasn't been misused by someone!
Comment: @whuber.  I am sorry.  I do not mean to be disrespectful of Peter Flom.  I know him from both here and the consulting statisticians site on the ASA website.  I respect his knowledge of statistics and I wasn't arguing about the content of his comments.  But I did challenge his overwhelmingly strong conclusions and his evasiveness with respect to my arguments. That gets frustrating and I wanted to hear if he had a valid argument or could concede that even if pie chart can give distortion too often that is not enough justification to say they should be banned.
Comment: @Michael, I agree with you - I can't personally imagine a situation where I'd want to use a pie chart but I've yet to see a rational argument for saying that no one should ever use one. But I think, for future discussions of this type, it may suffice to get your opinion on the record and let the community decide their's - that's something whuber and chl have also encouraged me to do, as I also have an argumentative streak in me, which I'm sure you know ;)  Congrats on the 10k rep, btw. I hope you'll have a look at 'tools' - you can now see deleted posts and lots of cool lists. I enjoy them.
Answer: Mode still works!  Is that not an important summary statistic?  (What's the most common category?)  I think the median suggestion has little to no value as a statistic, but the mode does.
Also count distinct would be valuable.  (How many categories do you have?)
You might create ratios, like (most common category) / (least common category) or (#1 most common category) / (#2 most common category).  Also (most common category) / (all other categories), like the 80/20 rule.
You can also assign numbers to your categories and go nuts with all the usual statistics.  AA=1, Hisp=2, etc.  Now you can compute mean, median, mode, SD, etc.
Answer: I do appreciate the other answers, but it seems to me that some topological background would give a much-needed structure to the responses.
Definitions
Let's start with establishing the definitions of the domains:

categorical variable is one whose domain contains elements, but there's no known relationship between them (thus we have only categories). Examples, depend on the context, but I'd say in the general case, it is difficult to compare days of the week: is Monday before Sunday, if so, what about next Monday? Maybe an easier, but less used example are pieces of clothes: without providing some context that would make sense of an order, it is difficult to say whether trousers come before jumpers or vice versa.
ordinal variable is one that has a total order defined over the domain, i.e. for every two elements of the domain, we can tell that either they are identical, or one is bigger than the other. A Likert-scale is a good example of a definition of an ordinal variable. "somewhat agree" is definitely closer to "strongly agree" than "disagree".
interval variable is one, whose domain defines distances between elements (a metric), thus allowing us to define intervals.

Domain examples
As the most common set that we use, natural and real numbers have standard total order and metrics. This is why we need to be careful when we assign numbers to our categories. If we are not careful to disregard order and distance, we practically convert our categorical data in interval data. When one uses a machine learning algorithm without knowing how it works, one risks making such assumptions unwillingly, thus potentially invalidating one's own results. For example, most popular deep learning algorithms work with real numbers taking advantage of their interval and continuous properties. Another example, think of 5-point Likert scales, and how the analysis we apply on them assumes that the distance between strongly agree and agree is the same as disagree and neither agree nor disagree. Hard to make a case for such a relationship.
Another set that we often work with is strings. There are a number of string similarity metrics that come in handy when working with strings. However, these are not always useful. For example, for addresses, John Smith Street and John Smith Road are quite close in terms of string similarity, but obviously represent two different entities that could be miles apart.
Summary statistics
Ok, now let's see how some summary statistics fit in this. Since statistics works with numbers, its functions are well defined over intervals. But let's see examples on whether/how we could generalise them to categorical or ordinal data:

mode - both when working with categorical and ordinal data, we can tell which element is most frequently used. So we have this. Then we can also derive all the other measures that @Maddenker lists in their answer. @gung's confidence interval could also be useful.
median - as @peter-flom says, as long as you have an order, you can derive your median.
mean, but also standard deviation, percentiles, etc. - you get these only with interval data, due to the need for a distance metric.

Example of data contextuality
At the end, I want to stress again that the order and metrics you define on your data are very contextual. This should be obvious by now, but let me give you a last example: when working with geographical locations, we have lots of different way to approach them:

if we are interested in the distance between them, we can work with their geolocation, which basically gives us a two-dimensional numerical space, thus interval.
if we are interested in their part of relationship, we can define a total order (e.g. a street is part of a city, two cities are equal, a continent contains a country)
if we are interested in whether two strings represent the same address, we could work with some string distance that would tolerate spelling mistakes and swapping positions of words, but make sure to distinguish different terms and names. This is not an easy thing, but just to make the case.
There are plenty of other use cases, that all of us encounter daily, where none of this makes sense. In some of them there's nothing more to do than treat the addresses as just different categories, in others it comes down to very smart data modelling and preprocessing.


Timezone with 12h time format (AM/PM)

Question: I have a problem with timezone in debian
I need to set up zone with 12h format (AM/PM) throughout the system. Locale set to "en_US.UTF.8", output "date" command is correctly. Time format in "timedatectl" is incorrect (24h). Any ideas?
Locale:
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

timedatectl
    Local time: Mon 2022-02-21 20:09:15 CET
           Universal time: Mon 2022-02-21 19:09:15 UTC
                 RTC time: Mon 2022-02-21 19:09:14
                Time zone: Europe/Warsaw (CET, +0100)
System clock synchronized: no
              NTP service: active
          RTC in local TZ: no

date
Mon 21 Feb 2022 08:09:08 PM CET

systemctl process status
   Active: active (running) since Mon 2022-02-21 20:09:01 CET; 22s ago

Answer: 12 hour AM/PM or 24 hour format is a matter of locale preference - not the time zone.
Comment: Ok, how can i get 12H (AM/PM) in systemctl process status? In my process i have a errors with bad parsing time. I think changing the time display to AM / PM will solve the problem
Answer: timedatectl will not print 12-hour time, calls strftime() with %a %Y-%m-%d %H:%M:%S
GNU coreutils date calls localized functions which in some cases can call strftime() with hour minute second of %I:%M:%S  %I is 12-hour hours.  Also date can be called with a custom format, for example date +%r

Timezone with 12h time format (AM/PM)


Negative sign in front of a number

Question: Does $-a$ mean $(-1)a$ for a real number $a$?
Comment: Why the downvotes? It's a legitimate question.
Answer: Yes.  -a means both (-1)a and 0 - a, which are usually the same thing.
Note if a is negative -a is positive.  Ex.  -(-7) = 7.  And 0 -(-7) = 7.
You should try to mark this take for the appropriate subject though.
Comment: Also note that $0=-0$.
Answer: Just a small clarification: "$-$" is the abbreviation of "$-1\cdot{}$", hence $-a=-1\cdot a$.  Parentheses are not necessary.

Negative sign in front of a number


Organizing references by year in Pandoc when generating HTML

Question: I am relatively new with Pandoc and I am trying to generate an HTML file with my publications to put up on my website. I'd like to have the publication list numbered and organized by year first, with the most recent first and the oldest last.
I can get the numbering fine with the proper csl file, but I can't get the year sorting. The problem is that I'm not first author in all my publications, so what ends up happening is that they are organized alphabetically first and then by date, which is not what I want.
I can get the result I want when generating a PDF by using biblatex with the option sorting=ydnt (Year (Descending), Name, Title), but since Pandoc doesn't use biblatex to generate a list of references to HTML, I can't use this tactic here.
The only way I can see how to possibly solving this is to get a citation style in the Zotero style repo that does what I want, but I haven't been able to find one. So I'm trying to modify one to do it, but without success.
This answer teaches a way to change the sorting style, so I'm trying to manually change the sorting style of the Proceedings of the Royal Society B style. Specifically I'm changing
    <sort>
      <key variable="citation-number"/>
    </sort>

to
    <sort>
      <key macro="issued" sort="descending"/>
      <key macro="author"/>
    </sort>

But that doesn't work (probably because that only changes the sorting of the text citations, not the reference list). I've tried a couple of other things, but I can't find something that works!
This doesn't matter much I guess, but I'm using Pandoc 2.7.3, citeproc version 0.16.2 and the file that I'm running on is:
---
bibliography: selectedpubs.bib
nocite: '@*'
linestretch: 1.5
fontsize: 12pt
output:
  html:
    output: pubpage.html
    filter: pandoc-citeproc
    csl: prsb2.csl
...

The file prsb2.csl is just the Proceedings of the Royal Society B csl.
Comment: You should probably post the `prsb2.csl` file?
Comment: @mb21 I mean, not sure if it makes much difference because nothing I tried caused any difference in the output. But I included the information anyway,
Answer: You have the right idea, but misunderstood the linked thread. Instead of changing the sort keys for the citation, you'll want to add sorting to the bibliography, i.e.
<bibliography second-field-align="flush" et-al-min="11" et-al-use-first="10">
   <sort>
      <key macro="issued" sort="descending"/>
      <key macro="author"/>
    </sort>
<layout>

Instead of modifying a style, you could also use the APA-CV style that already exsits on the repository
Comment: Worked like a charm. The APA-CV is good too and I wasn't aware of that, but I'd rather modify the other one, since I also want a numbering to appear in front of each citation.


what's the best programming language (and development community) for integer mathematics?

Question: I was wondering what's the best programming language (and development community) for integer mathematics?
I'm an R supporter. However, I'm interested in primality testing and integer factorization, including large integers.  And I'm not sure that R (and CRAN) is the best option.
Any suggestions?
Comment: Here is a list of various [computer algebra systems](https://en.m.wikipedia.org/wiki/List_of_computer_algebra_systems) that may possibly be of some use to you
Comment: "best" is asking for an opinion and depends on individual factors, such as your preference for "GPL". There can be no best answer. My suggestion is PARI/GP.
Comment: *Mathematica*, which has a great deal of optimized number theoretic functions built in, plus great graphics and other benefits.
Comment: I should have mentioned that I prefer LGPL, GPL and other open source licenses.
Comment: am not an expert (like C++ myself but not sure it's especially top of class here) - have heard tell that julia is pretty good which might wrap some GNU libraries, also has something called [arbitrary precision arithmetic](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/index.html#Arbitrary-Precision-Arithmetic-1) which sounds promising! Again not an expert hope you get a good answer!
Comment: Are you planning on writing these factoring algorithms yourself, or just using algorithms someone else wrote?  Are you going for fastest, or some kind of analytical results?
Comment: I've been trying to find an ultra efficient algorithm for years, and obviously, never succeeded. However, it's led to the question, if I were to succeed, what programming language should I use? I would prefer a programming language that would be appealing to others interested in this area. Presumably, such a programming language would already have some tools in this area and related areas. My understanding is that existing algorithms use some form of parallel computing, however, an ultra efficient algorithm would not require this.
Answer: Python has had large-integer support for many years.
Also, the "Sage" mathematics environment includes many, many pre-computed things, and is based on Python.
Both are open-source, etc. Easy choice, if one has even the faintest desire to be open-source.


Como passar um plugin Jquery como parâmetro para o evento On

Question: Eu utilizo o plugin Datatables, e na tabela haverá a opção de editar e excluir, eu quero mostrar uma confirmação caso o usuário clique em excluir. Por isso, utilizarei o plugin "confirm", entretanto, esse plugin só é válido para a primeira página do DataTable, nas outras páginas ou quando faço uma pesquisa ele não funciona.
Pensei no evento "on", mas como faço para passar o plugin "confirm" invés da função (3º parâmetro).
Algo como:
<code>$("html").on("click", ".confirm", $.confirm());
</code>
Comment: Podes testar sem invocar a função... `$("html").on("click", ".confirm", $.confirm);`. Se não funcionar indica por favor que plugin é esse e onde está a documentação e código fonte para podermos perceber melhor o plugin e o problema.
Comment: Não posso invocar sem os parâmetros, por os utilizo (passando a mensagem, os botões, etc)... O plugin em questão é o seguinte:  http://myclabs.github.io/jquery.confirm/
Comment: já tentou $("html").on("click", ".confirm", function(){ $(this).confirm() }); ?
Comment: Já havia tentado dessa forma, assim só funciona no segunda clique :/
Answer: Você pode passar uma função que chame o <code>$.confirm</code>. Por exemplo:
<code>$("#elemento").on("click", function() {
    $.confirm();
});
</code>
Comment: Quando vi seu comentário pensei: "É isso!"... Mas, ao executar o código, também não funcionou... Mesmo problema, funciona na primeira página, mas na segunda não funciona :(
Comment: Valeu, só faltou um parâmetro na sua resposta... Assim funcionou: $("html").on("click", ".confirm", function(){
 $.confirm() });


Verifying that a certain process is not a Brownian motion

Question: Let $B$ be a standard Brownian motion in $1$ dimension. Define
\begin{equation}
\tau = \inf \bigg\{ t \geq 0 : B_t = \max_{0 \leq s \leq 1} B_s \bigg\}.
\end{equation}
We want to show that $(B_{t+ \tau} - B_{\tau} )_{t \geq 0}$ is not a Brownian motion.
My ideas:
Let $\tilde{B}_t= B_{t+ \tau} - B_{\tau} $. Suppose on the contrary that $\{\tilde{B}_t\}$ is a Brownian motion.
I can only deduce by the definition that $$\mathbb{P} \bigg( \bigcup_{q \in \mathbb{Q}} \bigcap_{t \in [0,q]} \{\tilde{B}_t \leq 0 \} \bigg) =1.$$ Any ideas of how that leads to a contradiction?
(Should I somehow use the fact that $\tau <1$ a.s.?)
Comment: Yes, you need to use the fact that $\tau<1$ a.s. Once you know this, the process $\tilde{B}_t$ can't be Brownian because $\tilde{B}_{t} \le 0$ for $t = 1-\tau$, contradicting the fact that $\tilde{B}_{t}$ is normally distributed with mean $0$ and variance $t$.
Comment: @LukasGeyer The fact that $\tilde B_t\leqslant0$ at a random time $t$ does not contradict that $\tilde B$ is a Brownian motion.
Comment: @Did, of course you are correct.
Comment: Got something from the answer?
Answer: Every Brownian motion starting from $0$ enters $(0,+\infty)$ instantaneously, thus, $$\mathbb{P} \bigg( \bigcup_{q \in \mathbb{Q}} \bigcap_{t \in [0,q]} \{B_t \leqslant 0 \} \bigg) =0.$$


node app.js is not working when i am linking with mongod

Question: const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${serverSelectionTimeoutMS} ms, this.description);
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users*\Desktop\MONGODB\NEW SERVER\node_modules\mongodb\lib\sdam\topology.js:278:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 491601161,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users*\Desktop\MONGODB\NEW SERVER\node_modules\mongodb\lib\cmap\connect.js:370:20)
at Socket. (C:\Users\ANMOL\Desktop\MONGODB\NEW SERVER\node_modules\mongodb\lib\cmap\connect.js:293:22)
at Object.onceWrapper (node:events:628:26)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
cause: Error: connect ECONNREFUSED ::1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 27017
},
[Symbol(errorLabels)]: Set(1) { 'ResetPool' }
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
I ran mongod in one terminal and node in other but some connectivity error is showing
Answer: MongoDB might running at a different address or port: This means that the MongoDB service is not running at the default address (localhost or 127.0.0.1) and port (27017). It might be that when you installed MongoDB or when you are running it, you specified a different address or port number.
To resolve this:
Check where MongoDB is running: First, we need to check whether MongoDB is running and on which port. You can find this in the output when you start mongod in the terminal. Typically, it prints a message like:
[initandlisten] waiting for connections on port <port>

Where  is the port number MongoDB is listening on.
If MongoDB is running on a different port, you should see that port number instead of 27017.
Update your connection string: Once you have confirmed the address and port where MongoDB is running, you need to use those in your connection string in your Node.js code.
If for example, MongoDB is running on port 27018 on your localhost, then in your Node.js code where you establish a connection with MongoDB using the MongoDB Node.js driver, you need to update your connection string to reflect this.
So, if your original connection string was something like:
const url = 'mongodb://localhost:27017';

You'd change it to:
const url = 'mongodb://localhost:27018';

Related Issues
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
Answer: This answer from the mongodb community worked for me: https://www.mongodb.com/community/forums/t/econnrefused-27017/131911/6
I changed the localhost to

const uri = 'mongodb://127.0.0.1:27017';

Initially this is what I had:

mongodb://localhost:27017/

I hope my answer helps


Matrix Columns VS Rows

Question: I am confused over the geometric meaning of columns and rows in a matrix.  My understanding from 3Blue1Brown's Linear Algebra series was that the elements of a matrix column are the coordinates which the tip of a single basis vector lands on at the end of the linear transformation, while the elements of a matrix row are the coefficients of an equation (if the matrix is augmented) or expression (if the matrix is not augmented).  However, I am now studying matrix subspaces, and it appears that both columns and rows can be treated as vector tip coordinates to solve for different subspaces.
Is there a second vector geometric interpretation of a matrix where the elements of a row are the coordinates on which the tip of a single basis vector lands (which would presumably be identical to applying the original geometric interpretation to the matrix's transpose)?  If so, what is the geometric or conceptual difference between matrix columns and rows, which justifies the differences in how we operate on them, and how we relate them to linear systems of equations?
Answer: Actually, 3Blue1Brown gives you that interpretation as well, though he doesn't go deep enough to get to this particular aspect. In Chapter 7, he discusses duality - how linear transformations into a 1D line correspond to specific vectors in space, And how when expressed as matrices, the transformation matrix was the column vector flipped over on its side - i.e., converted into a row. These "row vectors" are the "dual vectors" of the normal column vectors. And you can consider linear transformations in terms of row vectors in a very similar fashion to how the videos talk about them in terms of column vectors.
A basis provides a way to identify each vector in space with a specific dual vector. (In the videos he makes it seem like there is one natural to make this assignment - but that is because he has a natural basis, $\hat i, \hat j, \hat k$ that he uses. And covering this more generally was outside the scope of what he was trying to do.) When you transpose a matrix, you are actually making use of this vector-dual vector identification to change your transformation to act on the dual vectors instead of the original vectors.
So, the point is, you can consider either the columns or the rows to be vectors. In either case, the matrix converts vectors of the same form into other vectors of the same form. But when acting on columns, the matrix multiplies in on the left of the column, while when acting on rows, it multiplies in on the right of the row:
$$\begin{bmatrix}a & b\\c & d\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} = \begin{bmatrix}w\\z\end{bmatrix}$$
$$\begin{bmatrix}p&q\end{bmatrix}\begin{bmatrix}a & b\\c & d\end{bmatrix} = \begin{bmatrix}r&s\end{bmatrix}$$

Matrix Columns VS Rows


Given a 2D integer grid, how to choose three points (x,y), (2x,2y) and (3x,3y) such that their distance to the integer grid is maximal?

Question: Given an integer grid $\mathbb{Z}^2=\{...,(0,0), (1,0), (2,0),...,(1,1),(1,2),...\}$, choose $x,y \in \mathbb{R}$ such that the points $(x,y)$, $(2x,2y)$, $(3x, 3y)$ have maximal (Euclidean) distance from any point of the integer grid $\mathbb{Z}^2$ (i.e. the closest point to the grid is as far as possible from the grid).
How to solve this problem mathematically? Or else, which iterative algorithm would be best suited to solve this problem?
Answer: This is supposed to be a comment but it is a little bit long and I need to post a picture to illustrate some points.
For any $k \in \mathbb{R}$ and $m,n \in \mathbb{Z}$. Let $d_{k,mn} : \mathbb{R}^2 \to \mathbb{R}$ be the distance between $(kx,ky)$ and $(m,n)$
$$d_{k,mn}(x,y) = \sqrt{(kx - m)^2 + (ky - n)^2}$$
Let $d_k(x,y) = \min\limits_{m,n \in \mathbb{Z}} d_k(x,y)$. The question at hand is equivalent to:

What is the biggest value of
  $d(x,y) = \min\{\; d_1(x,y), d_2(x,y), d_3(x,y) \;\}$ for $(x,y) \in \mathbb{R}^2$?

It is easy to see $d_k(x,y)$ is a doubly periodic function with fundamental period $(\frac1k,0)$ and $(0,\frac1k)$. Furthermore, for any $m,n \in \mathbb{Z}$,
$$d_k(x,y) = d_{k,mn}(x,y) \quad\text{ for }\; (x,y) \in [\frac{m-1/2}{k},\frac{m+1/2}{k}] \times [\frac{n-1/2}{k},\frac{n+1/2}{k}]$$
and $d_k(x,y)^2$ is equal to a quadratic polynomial in $x$ and $y$.
This means $d(x,y)^2$ is a piecewise smooth doubly periodic function with fundamental period $(1,0)$ and $(0,1)$. It is also clear $d(x,y)$ is an even function in $x$ and in $y$ and remain unchange if we swap the role of $x$ and $y$. To locate the biggest value of $d(x,y)$, one can
restrict the search to points inside the triangle $\triangle ABC = \{\; (x,y) \in \mathbb{R}^2 : 0 \le y \le x \le \frac12 \}$ shown in the figure at end.
Using the periodicity of $d_k(x,y)$, we know over $\triangle ABC$,
$$\begin{align}
d_2(x,y) &= \min\{\; d_{2,00}(x,y), d_{2,10}(x,y), d_{2,11}(x,y) \;\}\\
d_3(x,y) &= \min\{\; d_{3,00}(x,y), d_{3,10}(x,y), d_{3,11}(x,y) \;\}
\end{align}$$
Since $d_{1,00}(x,y) \le d_{2,00}(x,y) \le d_{3,00}(x,y)$, we find over $\triangle ABC$,
$d(x,y)$ is the minimum of the square root of 5 quadratic polynomials.
$$d(x,y) = \min\{\; d_{1,00}(x,y), d_{2,10}(x,y), d_{2,11}(x,y), d_{3,10}(x,y), d_{3,11}(x,y) \;\}$$
Let $R = \frac{\sqrt{23-3\sqrt{31}}}{5}$. The point $(x_0,y_0) = \left(\frac{4+\sqrt{31}}{20}, \frac14 \right)$ mentioned in the comment of another answer is the intersection of 3 circles
$$d_{2,10}(x,y)^2 = d_{2,11}(x,y)^2 = d_{3,11}(x,y)^2 = R^2$$
If one make a plot for the five circular regions when some $d_{k,mn}(x,y) < R$:
$$\begin{array}{lrcrcrl}
(\color{red}{red})       & d^2_{1,00}(x,y) &=& x^2      &+& y^2      &< R^2\\
(\color{orange}{orange}) & d^2_{2,10}(x,y) &=& (2x-1)^2 &+& (2y)^2   &< R^2\\
(\color{yellow}{yellow}) & d^2_{2,11}(x,y) &=& (2x-1)^2 &+& (2y-1)^2 &< R^2\\
(\color{green}{green})   & d^2_{3,10}(x,y) &=& (3x-1)^2 &+& (3y)^2   &< R^2\\
(\color{blue}{blue})     & d^2_{3,11}(x,y) &=& (3x-1)^2 &+& (3y-1)^2 &< R^2
\end{array}$$
One will obtain the figure at end. The interior of these 5 circles cover the triangle $\triangle ABC$ nearly completely and the only exception is the point $(x_0,y_0)$. This implies for all $(x,y) \in \triangle ABC$ and hence $\in \mathbb{R}^2$, we have $$d(x,y) \le d(x_0,y_0) = \frac{\sqrt{23-3\sqrt{31}}}{5}$$
$\hspace1.5in$
Update
The conclusion that $d(x,y)$ reaches maximum on $(x_0,y_0)$, the common intersection of
three contour lines from three different distances $d_{k,mn}$ isn't a coincidence.
It is a consequence of following lemma:

Let $f(x,y), g(x,y)$ be two smooth functions defined over a neighborhood $\mathcal{O}$
  of a point $p \in \mathbb{R}^2$ satisfying following conditions:

$\mathcal{C} = \{\; (x, y) \in \mathcal{O} : f(x,y) = g(x,y)\;\}$ is a regular curve passing through $p$.
$\vec{\nabla} f(p) \ne \vec{\nabla} g(p)$.
the Hessians of $f$ and $g$ are positive definite over $\mathcal{O}$.

then $p$ cannot be a local maximum of $h(x,y) = \min\{\; f(x,y), g(x,y)\;\}$.

To see why this is the case. Assume the contrary and let us parametrize $\mathcal{C}$ by its arc length near a local maximum $p$ of $h(x,y)$.
$$\gamma : ( -\epsilon, \epsilon ) \ni s \quad\mapsto\quad \gamma(s) = (x(s),y(s)) \in \mathcal{C}\quad\text{ with }\quad \gamma(0) = p$$
Let $\vec{t} = \gamma'(0)$ be the tangent vector at $p$ and $\mathcal{H}f$, $\mathcal{H}g$ be the Hessian matrices
$$
\mathcal{H}f =
\begin{bmatrix}
\frac{\partial^2f}{\partial x^2} &
\frac{\partial^2f}{\partial x\partial y} \\
\frac{\partial^2f}{\partial x\partial y} &
\frac{\partial^2f}{\partial y^2}
\end{bmatrix}
\quad\text{ and }\quad
\mathcal{H}g =
\begin{bmatrix}
\frac{\partial^2g}{\partial x^2} &
\frac{\partial^2g}{\partial x\partial y} \\
\frac{\partial^2g}{\partial x\partial y} &
\frac{\partial^2g}{\partial y^2}
\end{bmatrix}$$
Since $p$ is a local maximum of $h(x,y)$, at $s = 0$, we will have
$$\begin{cases}
(f\circ\gamma)'_p
&= \vec{\nabla} f_p \cdot \vec{t} = 0\\
(g\circ\gamma)'_p
&= \vec{\nabla} g_p \cdot \vec{t} = 0\\
\end{cases}
\quad\text{ and }\quad
\begin{cases}
(f\circ\gamma)''_p
&=  \vec{t}\cdot \left[ \mathcal{H}f_p \vec{t} \right] + \vec{\nabla} f_p \cdot \gamma''(0) \le 0\\
(g\circ\gamma)''_p
&=  \vec{t}\cdot\left[ \mathcal{H}g_p \vec{t} \right] + \vec{\nabla} g_p \cdot \gamma''(0) \le 0
\end{cases}$$
Since we parametrize $C$ by arc length, $|\vec{t}| = 1$. Togather with the assumption
$\mathcal{H}f$ and $\mathcal{H}g$ are positive definite, we obtain
$$
\begin{cases}
\vec{t}\cdot \left[ \mathcal{H}f_p \vec{t} \right] > 0\\
\vec{t}\cdot \left[ \mathcal{H}g_p \vec{t} \right] > 0\\
\end{cases}
\quad\implies\quad
\begin{cases}
\vec{\nabla} f_p \cdot \gamma''(0) < 0\\
\vec{\nabla} g_p \cdot \gamma''(0) < 0\\
\end{cases}
$$
This implies all three vectors $\vec{\nabla} f_p$, $\vec{\nabla} g_p$, $\gamma''(0)$
are non-zero and both $\vec{\nabla} f_p$, $\vec{\nabla} g_p$ are pointing in the same direction as $-\gamma''(0)$. A consequence of this if we crosses $C$ along the direction
$-\gamma''(0)$ at $p$, $h(x,y)$ will increases as we move away from $\mathcal{C}$. This contradicts with the assumption that $p$ is a local maximum of $h(x,y)$ $\blacksquare$.
Back to our original problem, the maximization of
$$d(x,y) = \min\{\; d_{1,00}(x,y), d_{2,10}(x,y), d_{2,11}(x,y), d_{3,10}(x,y), d_{3,11}(x,y) \;\}$$
over $\triangle ABC$ is equivalent to the maximization of
$$d^2(x,y) = \min\{\; d^2_{1,00}(x,y), d^2_{2,10}(x,y), d^2_{2,11}(x,y), d^2_{3,10}(x,y), d^2_{3,11}(x,y) \;\}$$
over the same triangle.
Since all the $d^2_{k,mn}(x,y)$ involved are smooth functions with positive definite Hessians. $d^2(x,y)$ cannot achieve a local maximum at $(x,y)$ where only one of the $d^2_{k,mn}$ is larger than all the rest.
Since the set $\mathcal{C}$ corresponds to any two such $d^2_{k,mn}$ are either circles or straight lines. They are regular curves. What this means is the local maximum of $h(x,y)$ can only achieved at those point where either:

at least three of $d_{k,mn}$ coincides.
or only two of $d_{k,mn}$ coincides but the gradient at that point also matches.

i.e. To find the maximum of $d(x,y)$, there are only finitely many $(x,y)$ to check.
Comment: Thank you for this detailed explanation!
Comment: I understand this problem better now, and I'll mark your reply as accepted answer (I believe you showed the proposed solution is correct and unique up to symmetries).
Answer: Hint
First, we can assume that both $0<x\leq 0.5$ and $0<y \leq 1$ with a little transformation
If $(0,0)$ is the closest point of the grid to $(2x, 2y)$, $(0,0)$, $(1,0)$, $(0,1)$ or $(1,1)$ is the closest point of the grid to $(3x, 3y)$
If $(1,0)$ is the closest point of the grid to $(2x, 2y)$, $(1,0)$ or $(1,1)$ is the closest point of the grid to $(3x, 3y)$
If $(0,1)$ is the closest point of the grid to $(2x, 2y)$, $(0,1)$ or $(1,1)$ is the closest point of the grid to $(3x, 3y)$
If $(1,1)$ is the closest point of the grid to $(2x, 2y)$, $(1,1)$ is the closest point of the grid to $(3x, 3y)$
Solve the 11 equations (7 with symetries) and you'll get your answer
Comment: Actually, can't we also assume $0<y<x$, given the reflection symmetry ?
Comment: By assuming $0<x<1/2$, $0<y<x$ and that $(2x,2y)$ and $(3x,3y)$ are closest to $(1,1)$, I found the solution $(x,y)=(1/5+\sqrt{31}/20;1/4)$. I'm not sure how to prove that this solution is the only one for $0<x<1/2$ and $0<y<x$, however.
Comment: @derren with a $\leq$ yes, i used it to go from 11 to 7 equations.


What is the probability (10-faces 3D shape rolled)

Question: In the 10-faces 3D shape:in 4 faces is written number 1, in 3 faces is written number 2, in 3 faces is written number 3. This shape is rolled 8 times. What is the probability that the face with number 3 drops 3 times, with the number 2 drops 2 times and with the number 1 drops 3 times.
I was thinking to do (3/10)^3 * (3/10)^2 * (4/10)^3 but i know that we have to put some combinations here and i'm sucked here. Please solve if you can telling all ur steps. Thank you!
Comment: You are on the right track (+1), except for the required multinomial coefficient to count the number of arrangements of the desired outcomes. See my Answer for that.
Answer: Analytically, this is a multinomial probability. You want three 1s, two 2a and three 3s in 8 rolls, where the probabilities of 1a, 2s, and 3a are
$.4, .3, .3,$ respectively. That is,
$${8 \choose {3,2,3}}(.4)^3(.3)^2(.3)^3 =  0.0870912,$$
where the multinomial coefficient $\frac{8!}{3!\cdot 2!\cdot 4!} = 560.$
This probability can be simulated by imagining a die with ten faces
labeled $100,100,100,100,10,10,10,1,1,1$ instead of $1,1,1,1, 2,2,2, 3,3,3,$
so that the desired total on the eight rolls will be 323. In ten million
rolls of such a die we got this total  $870,670$
times for a simulated probability of $0.087067,$ which agrees with the
analytic result to the anticipated three or four-place accuracy.
die = c(100,100,100,100,10,10,10,1,1,1)
set.seed(2020)
tot= replicate(10^7, sum(sample(die,8,rep=T)))
mean(tot==323)
[1] 0.087067


Opening percentage on Flex Accordion

Question: I'm currently developing a Flex application with an accordion containing 2 canvas: one of them containing a DataGrid and the other one a form.
When the user clicks on one of the DataGrid's rows, it opens the accordion on the second (form) canvas. However, I'd like to open the second part of the accordion only by half, so they can still see the data in de datagrid.
Answer: I don't think an accordion is capable of this. I think accordions are just a viewstacks with a nifty hide/show effect. You are probably going to have to develop something very customized for this. Use your canvases, modify their heights and use a change effect. You will be able to get the view you are looking for and the nifty slide effect.
~Mike
ps: I say "I think" and "probably" a lot cause I'm pretty new to Flex

Opening percentage on Flex Accordion


Customizing views exposed filter drop down list with css

Question: I have a view with few exposed filters. They are all "choose 1 term" type of filters with appearing dropdown list when you click on right side of them.
I want to style appearing dropdown lists. It looks ok in  chrome, but it's especially ugly looking in IE. Also, i can't find them in the code, they just dissapear when i try to "inspect" them.
How can i customize those drop-down lists' appearances? Do they have any css classes? Any other ideas?
Comment: If you can't find them in the code, cut and paste the code into a text editor and then search for the text of the labels/options.  You have to make a basic effort yourself before asking other people for help.
Comment: Also,  this is just a CSS question.  You are asking "Does this element have any classes? How can I style it"; so it actuall belongs on [so].
Answer: Unfortunatelly, every browser has its own rendering of drop downs (as well as fileupload boxes). So direct CSS styling of a dropdown element won't work in all browsers.
What you need to do is to use module called Uniform (http://drupal.org/project/uniform). It provides intergration with uniform library which automatically replaces drop downs, fileuploads, checkboxes and radios with custom styled elements.
This will give you ability to style these custom elements and make dropdown lists look the way you want in almost any browser. Just check if this works for you.
Answer: try this css code and change it to your requirement
<code>.form-select {
   background: transparent;
   width:95px;
   padding: 4px;
   border: 1px solid #d8d9da;
   cursor:pointer;
   border-radius:4px;
    color: #8C8C8C;
    font: 13px "Times New Roman";
    font-style:italic;
     -webkit-transition-duration: 0.1s;
-moz-transition-duration: 0.1s;
-o-transition-duration: 0.1s;

}

.form-select:hover{

    background-color:#CC0;

}
</code>
Comment: This is a *TERRIBLE* idea. This would affect EVERY select box used on pages that use your theme. I'm definitely not saying use an `id`, because that it also bad, but you should target your classes more specifically.


Converting a string number with Dotted Separator in PHP

Question: I have a variable
<?php $number = "10000000"; ?>

I want convert a string to string number with dotted seperator like this: 10000000 -> 10.000.000
What can i do now? Thanks.
Comment: Integer is integer, it's when you have to _display_ it that needs the formatting. Try [`number_format`](https://php.net/manual/en/function.number-format.php).
Comment: it wont be an int if it has multiple dots in it. `number_format()`
Answer: The number_format() function formats a number with grouped thousands. For the dotted separator simply use '.' as the fourth argument:
echo number_format("10000000", 0, ",", "."); //10.000.000

Comment: yea, that's the signature of the function I believe :), but the default values don't suit your needs; they group thousands with `','` and indicate the place of decimal point through `'.'`, but when you group thousands with `'.'` then you should differentiate it  with decimal point representation some how, like to display it via `','`, for example :)


The request was aborted: Could not create SSL/TLS secure channel. for HttpWebRequest and WebClient

Question: Hi I'm getting the above error message when i try to get a response from a URL.
The code i've used is
HttpWebRequest ObjContentfulRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse ObjContentfulResponse = (HttpWebResponse)ObjContentfulRequest.GetResponse();

and I've also tried with
using (WebClient client = new WebClient())
            {
              var json = client.DownloadString(URL);
            }

Please suggest what i'm missing.   The error i'm getting is  The request was aborted: Could not create SSL/TLS secure channel.
Comment: Could you please provide more information: What is the url like (https?), is it hosted by yourself, do you have a trustworthy certificate for this url? What happens when opening the url in the browser?
Comment: You just wrote "In the browser it shows 400 bad request"?! Okay, when working well in the browser can you see whether the certificate is valid or not?
Comment: In the browser it shows 400 bad request. Its a https url. They have given access tokens to access the data. There is no certificates as such.
Comment: The URL format is as below

https://theurliwanttoaccess/myspace/idforspaces/entries?access_token=given accesstoken
Comment: When the url you're trying to open inside your code fails even in the browser, the error does not lay in the code but the url. So before further coding make sure the url is working... either by browser or a tool like "Fiddler2".
Comment: The URL is working fine checked the same in browser and in postman tool
Comment: With some research i found the solution for it.... We are using .net framework 4.0 there is not tls1.2 support for it. So if we use this mentioned piece of code, we can get  a response from the URL.
 **ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;**
Comment: Thanks for the help **OlafW**
Comment: You're welcome, even if my help was not that much! Thanks for the feedback :)
Answer: first install Nuget System.Data.DataSetExtensions
then after use HttpWebRequest
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


Python non-greedy regex to clean xml

Question: I have an 'xml file' file that has some unwanted characters in it
<data>
  <tag>blar </tag><tagTwo> bo </tagTwo>
  some extra
  characters not enclosed that I want to remove
  <anothertag>bbb</anothertag>
</data>

I thought the following non-greedy substitution would remove the characters that were not properly encased in <sometag></sometag>
re.sub("</([a-zA-Z]+)>.*?<","</\\1><",text)
            ^          ^ ^     ^      text is the xml txt.
         remember tag, | |     put tag back without and reopen next tag
               read everything until the next '<' (non-gready)

This regex seems only to find the position indicated with the [[]] in </tag>[[]]<tagTwo>
What am I doing wrong?
EDIT:
The motivation for this question has been solved (see comments, I had a stray & in the xml file which was causing it not to parse - it had nothing to do with the characters that I want to delete).  However, I am still curious as to whether the regex is possible (and what was wrong with my attempt) and so I don't delete the question.
Comment: Sounds like you have a stray ampersand in there.
Comment: The character entity reference for an ampersand is `&`. You can't just do a global S+R though, as that could mess up other valid things.
Comment: @cwallenpoole: I'm not trying to parse xml.  I'm trying to clean the xml file so that I can have it accepted by the parser.  Any non-regex suggestions as to how to do this are welcome
Comment: Which parser doesn't accept that?
Comment: @IgnacioVazquez-Abrams `xml.dom.mindom` I have edited question to include backtrace
Comment: @IgnacioVazquez-Abrams Bah! Looks like I do.  (how did you know that?)
Comment: @IgnacioVazquez-Abrams, okay when I run a substitution to replace "&" with "and" my problem goes away.  However, I would rather keep the text as it was written, how do I keep the & and still have it pass (I tried substuting "\&" but that didn't succeed.
Answer: The dot does not match newlines unless you specify the re.DOTALL flag.
re.sub("</([a-zA-Z]+)>.*?<","</\\1><",text, flags=re.DOTALL)

should work fine. (If it does not, my python is at fault, not the regex. Please correct.)
I think it is good practise to be as precise as possible when defining character classes that are to be repeated. This helps to prevent catastrophic backtracking. Therefore, I'd use [^<]* instead of .*? with the added bonus that it now finds stray characters after the last tag. This would not need the re.DOTALL flag any longer, since [^<] does match newlines.
Answer:  "</[^>]+?>[^<>]+?<"

in ipython:
In [1]: a="<data>  <tag>blar </tag><tagTwo> bo </tagTwo>  some extra   characters not enclosed that I want to remove  <anothertag>bbb</anothertag></data>"

In [2]: import re

In [3]: re.sub( "(</[^>]+?>)[^<>]+?<" ,"\\1<",a)
Out[3]: '<data>  <tag>blar </tag><tagTwo> bo </tagTwo><anothertag>bbb</anothertag></data>'

Python non-greedy regex to clean xml


Cocoapods .podspec cannot find include headers

Question: I have a Objective-C iOS library that uses and ships a third party static C lib. One of my Objective-C source file imports that library using
#import <teamspeak/public_errors.h>

but the generated xcode target by Cocoapods cannot find that header.
This is the simplified structure of my folders

and this is my .podspec file
Pod::Spec.new do |spec|
  spec.name             = 'TSKit'
  spec.version          = '0.1.0'
  spec.summary          = 'An iOS TeamSpeak client'

  spec.description      = <<-DESC
  TSKit is a Objective-C wrapper around the C TeamSpeak client library.
                       DESC

  spec.homepage         = 'https://github.com/JanC/TSKit'
  spec.license          = { :type => 'MIT', :file => 'LICENSE' }
  spec.author           = { 'Jan Chaloupecky' => 'email@example.com' }
  spec.source           = { :git => 'https://github.com/JanC/TSKit.git', :tag => spec.version.to_s }

  spec.ios.deployment_target = '8.0'

  spec.frameworks = 'AVFoundation', 'AudioToolbox'

  spec.source_files = 'TSKit/Classes/**/*'

  spec.ios.vendored_library = 'TSKit/lib/libts3client.a'
  spec.libraries            = "ts3client", 'c++'

  spec.preserve_paths = 'TSKit/include/**'

  # spec.header_dir          = "TSKit/include"
  # spec.header_mappings_dir = 'TSKit/include'

  spec.pod_target_xcconfig  =  {"HEADER_SEARCH_PATHS" => "$(PODS_ROOT)/#{spec.name}/include/**"}

end

As you can see, I tried using both spec.header_dir  and spec.header_mappings_dir without any luck.
pod lib lint fails:
 -> TSKit (0.1.0)
    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')
    - NOTE  | [iOS] xcodebuild:  10:9: fatal error: 'teamspeak/public_errors.h' file not found

You can find a sample project here(be sure to checkoung the so-question branch)
https://github.com/JanC/TSKit/tree/so-question
Answer: spec.header_mappings_dir = 'TSKit/include/**/*.h' should cause cocoapods to recursively traverse the subfolder teamspeak and find the public_error.h header.


Echo uploaded filename from database in no file chosen part

Question: I have form which includes a file upload.After the form is submitted the selected filename is saved in the database.And i have a edit form of saved data.In that form in the file upload button,at the position where shows "no file chosen" i need to echo the saved file form the database.

    File upload
   <?php echo $announcements_details['file'] ; ?>
    <input type="file"  id="captionfile" name="captionfile">
\

I need echo the $announcements_details['file'] in the no file choose chosen part. Can anyone help me?
Answer: It is impossible to do this, due to security reasons. you would be able to select and upload any file from the client to your website.
However, you can display a message below it saying a file has been uploaded. Or you could override the input style using css and display the text that way.
Comment: Oh..thanks for the information Jerodev.So can you suggest me a way to change the text of the file upload button from choose file to "update file"
Comment: No, that is not possible either, you can't change the text. What you can do is make the input invisible using css and place a div under it with your own layout. In this div, you can place any text you like.
Answer: Yes, as Jerodev said it is not possible to takeout the same file in place of the choose file button. But there is a work around for this.

If the uploaded file is an image file, then you could pull up the filename from the database, and display a small thumbnail for the image below the choose upload button.
And if it is any other file, (pdf/txt..etc.), then you could display a link to the file below the choose upload button, so that the users can download the file and see what they have uploaded.

Hope this helps.
Thanks.


NPgsql connection pool and performance counters

Question: First some background:
1. Understand how COnnection Pooling is being used by NPGSQL in ASP.NET REST API
Environment:
- We have a REST API controller that queries first a list of items (to RDS) then per each item in this list we need to obtain some additional values so we use a Parallel.ForEach statement

Every time we use a connection we dispose it properly
I've seen that every time this endpoint is called the number of connections increase and then they are removed ok.

Process:
I've followed http://www.npgsql.org/doc/performance.html#performance-counters to check on how NPGSQL is handling connections, also added the following to the connectionstring:
  "CommandTimeout=50000;TIMEOUT=1024;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=100;Use Perf Counters=true;"
but I found a strange outcome:
NumberOfNonPooledConnections and NumberOfPooledConnections is always the same in my case (56) we are using a Parallel.ForEach to query several items.
The value for NumberOfActiveConnectionPools is 1.
At first I couldn't really understand how this is working, was it really using the connection pool ?
Then I stop the process removed the ";POOLING=True;" from the connection string and I have the same result.
Finally I set ";POOLING=false;" and execute again, now the NumberOfPooledConnections went to the roof  it reached 2378, and then it started timing out opening new connections.
I also noted in RDS performance metrics that the number of connections never exeeced 110 connections.
So the questions would be:
What would be the criteria to set the MaxPoolSize parameter ? 100 seems the usual.
In ASP.NET the connection pool is handled by instance ? so all connections being made from the same Application Pool in IIS will be reused or is per execution?.
Answer: First, ASP.NET (the web side) has absolutely no effect on Npgsql's connection pooling or on ADO.NET in general, so it's better to reason about Npgsql and ADO.NET without thinking about web.
Second, you aren't saying which version of Npgsql you're using.
Beyond that, before looking at performance counters, what exactly is the problem you are seeing? Are you seeing too many connections at the PostgreSQL side? You can check this by querying pg_stat_activity.
If Npgsql pooling is on (Pooling=true in the connection string, it's also the default), then when you call NpgsqlConnection.Open() a physical connection will be taken from the pool if one is available. When you close or dispose that NpgsqlConnection, it will be returned to the pool to be reused later. If you're seeing physical connections going up too much at the PostgreSQL side, that is a probable sign that you are forgetting to close/dispose a connection in your code and you have a leak.
The performance counters feature can be useful to understand what's happening, but unfortunately it isn't well-tested and may contain bugs. So please make sure there's an actual issue before starting to look at it (and at the very least report the Npgsql version you're using).
Comment: Well... thanks for replying... and sorry for not mentioning the version.

1. In production we are using version 3.0.4
2. I upgraded locally to version 4.0.0 to check out the performance counters

The issue was having a "timeout while getting  a connection from the pool" I think the issue was probably related to having a MaxPoolSize set on one env.

Then Npgsql's handles the connection pool per environment.  Just to confirm I created a new app in IIS and deployed the same code, and still have NumberOfActiveConnectionPools = 1
Comment: I don't know what version "3.0.4 2" is :) The latest 3.2 is 3.2.7, which you should be using if 4.0.0 is too bleeding-edge for you. On the other hand, if you're doing to do a major upgrade from an older version, you might as well try to go directly to 4.0.0 - please let us know of any issues you encounter.
Comment: Regarding your exception, having a Max Pool Size is mandatory - you cannot create an infinite number of physical connections (that would quickly overload PostgreSQL). Max Pool Size in itself doesn't cause timeouts; it simply means that open attempts will sometimes block until another physical connection is released and becomes available. You can try to increase the pool size and/or the connection timeout, but this is kind of error frequently points to a simple connection leak in your application (in which case no increase will help).
Comment: Great! thanks well we are using version 3.0.4 (as specified on nuget package manager and on the assembly's version) I think we'll update later this year, we'll let you know how it goes.
awesome explanation on the Max Pool Size thanks !
Keep up the good work with npgsql!
Comment: one last question, when setting pooling=false I saw the NumberOfNonPooledConnections  skyrocketed in the client until I started receiving a message about being unable to create new connections (in the server we are fine). We have
"using (var connection = DatabaseContext.CreateInstance())" and always close the connection  "PgsqlConnection.Close();" not sure why we have so much opened connections ni the client (unless the perf counters are not reliable)
Comment: @MetalGeorge did you resolve this issue because we have the same... we want PgPool to manage the connection pool, but turning off pooling in the application connection string causes all sorts of problems.
Comment: We just kept the transactions small and released connections and it was it. I  believe you should check with @ShayRojansky he may be able to resolve your questions
Comment: @MetalGeorge and others -  if you encounter any issues when turning off pooling, please submit an issue with a code sample on https://github.com/npgsql/npgsql


How to get S3 file path from URI?

Question: So inside my hook function I have URI of some file and I need to find it's S3 path (using s3fs module). How can I do that, to avoid manual string manipulation? Is there some helper function?
I found convertUriToKeyedPath() of S3fsStream class but it's protected and also it says "Converts a Drupal URI path into what is expected to be stored in S3" and that "expect is potentially dangerous.
Answer: Use file_url_generator service:
<code>$uri = 's3://bucket/file.ext';
$url = \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
</code>
An old way (deprecated as of 9.3):
<code>$http_url = file_create_url($uri);
</code>
Comment: No, it's opposite. I have drupal URI of file and I need to find S3 path.

How to get S3 file path from URI?


Which quality measures are available for non-binary problems?

Question: I'm wondering which quality measures are available for non-binary classifiers. I've read this article
https://en.wikipedia.org/wiki/Receiver_operating_characteristic
I understand, that the idea of Confusion matrix can be generalized easily. However, such metrics as sensitivity,specificity or precision seem to make sense only for binary classification problems.
So, my question is: are there any developed methods for evaluting the quality of non-binary classifiers?
Thank you.
Answer: I'm not sure whether you're still interested in this topic or not, but I became curious about it and have read a little on the subject. I've decided to share the following information to store the findings for myself and with hope that this might be helpful for others as well.
It seems that there are quite a number of quality measures, which can be applied to various models, non-binary classification included. Some of them are mentioned on this page and in this paper. Another interesting paper, criticizing AUC measure can be found here. Please note that my answer might be a little more general, as I haven't restricted my search to classification problems.
Answer: Kappa Statistic can be used.
k = (Acc - AccRand) / (1 - AccRand)
k = 1 is ideal, k = 0 means that the classifier is not better than random.
Acc is the accuracy of the classifier measured as the number of correctly classified observations over the total number of observations.
AccRand is the accuracy of a classifier making predictions by chance. AccRand can be easily measured empirically: take the outputs of your classifier, randomly permute predictions over observations, measure the accuracy.
Comment: Thank you. As far as I can see,

Scott's Pi is more appreciate for these purposes.
https://en.wikipedia.org/wiki/Scott%27s_Pi

As far as I can see, Kappa Statistics has some element of randomness(the element appears when you permute answers of your classifier in order to obtain AccRand).

At the same time, Scott's Pi has no element of randomness since the calculation of Pr(e) based of Marginal sums.
Comment: Scott's pi makes the assumption that annotators have the same distribution of responses. This means that the classifier should have the same prior probabilities of the classes as the the dataset. Many classifiers do enforse this constraint. For example, the true classes may be {a,a,b,c,c}, and predictions may be {a,c,b,c,c} - the prior probabilities of the classes are not the same, but the prediction accuracy is good.


Estimate the Euler–Mascheroni constant ($\gamma$) by Monte Carlo simulations

Question: The Euler–Mascheroni constant is defined simply as the limiting difference between harmonic series and the natural logarithm.
$$\gamma =\lim_{n\to \infty}\left(\sum _{k=1}^{n}{\frac {1}{k}}-\ln n\right)$$
I was interested in the estimations of Mathematical constants using Monte Carlo simulations after seeing the following post
Approximate $e$ using Monte Carlo Simulation and reading about the countless experiments to approximate $\pi$ (Buffon's needle/noodle etc.). I want to showcase a few nice examples of MC simulations in calculating mathematical constants to help motivate the idea.
My question is,
How can you design a non-trival MC simulation to estimate $\gamma$ ? (I say non-trivial since I could just integrate $\ln n$ for a sufficiently large $n$ using numeric MC integration, but this doesn't seem to be a very interesting showcase.)
I thought it might be possible to do using the Gumbel Distribution with $\mu=0, \beta=1$ (since mean of the Gumbel Distribution is  $E(X)=\mu+\beta\gamma$). However, I'm not sure how to implement this.
Edit: Thanks to Xi'an and S. Catterall for pointing out that the instructions for simulating the Gumbel Dist were on the Wikipedia article itself.
$${\displaystyle Q(p)=\mu -\beta \ln(-\ln(p)),}$$ where you draw $p$ from $(0,1)$.
Simple Mathematica code for $10^6$ draws,
gumbelrand = RandomReal[{0, 1}, {10^6, 1}];
Mean[-Log[-Log[gumbelrand]]]

Comment: Since the Gumbel distribution is straightforward to simulate (eg by inverse cdf), approximating the expectation by Monte Carlo is also immediate.
Comment: If $U$ is a sample of random numbers uniformly distributed in $(0,1)$ then, as you have found,  $\frac1n \sum -\log(-\log(u_i))$ is a simulated value of $\gamma$.  But there are others which are usually better and not much more complicated, such as  $\frac1n \sum \left(\frac1{u_i}+\frac{1}{\log(1-u_i)}\right)$ which is in a sense typically about $18.2$ or $18.7$ times closer
Comment: @BhorisDhanjal See https://math.stackexchange.com/questions/980593/some-integral-representations-of-the-euler-mascheroni-constant for how Henry's suggestion arises
Comment: In fact there are explicit instructions for simulating from the Gumbel distribution on the wikipedia page you linked to.
Comment: Oh yes you're right. I didn't spot that. Editing it onto the post but I'll leave it open for some time incase there are some other answers.
Comment: Hi Henry, I just tried that relation instead and it does indeed work much better. Can you link me to some reference for it? I couldn't figure out how it arises.
Answer: If you are willing to use the exponential and logarithmic functions for your method, you can estimate the Euler–Mascheroni constant  using importance sampling from exponential random variables.  Letting $X \sim \text{Exp}(1)$ we can write the Euler–Mascheroni constant as:
$$\begin{align}
\gamma
&= - \int \limits_0^\infty e^{-x} \log x \ dx \\[6pt]
&= \int \limits_0^\infty (-\log x) \cdot p_X(x) \ dx \\[12pt]
&= \mathbb{E}(- \log X). \\[12pt]
\end{align}$$
Consequently, we can take $X_1,...,X_n \sim \text{IID Exp}(1)$ and use the estimator:
$$\hat{\gamma}_n
\equiv - \frac{1}{n} \sum_{i=1}^n \log x_i.$$
The law of large numbers ensures that this is a strongly consistent estimator for $\gamma$, so it will converge stochastically to this value as $n \rightarrow \infty$.  We can implement this in R quite simply as follows.  Using $n=10^6$ simulations we get quite close to the true value.
#Create function to estimate Euler–Mascheroni constant
simulate.EM <- function(n) { -mean(log(rexp(n, rate = 1))) }

#Perform simulations
set.seed(1)
simulate.EM(10^6)
[1] 0.5772535

#True value is 0.5772157
#Our simulation is correct to four DP

Comment: +1. (1) Notice that $\log X$ has a [Gumbel distribution](https://en.wikipedia.org/wiki/Gumbel_distribution). (2) Therefore you can efficiently generate the $x_i$ with a standard, universal random number generator that returns a U$(0,1)$ variable: `mean(-log(-log(runif(1e6))))` (in `R`).
Comment: Yes, that is a good point.  I figured that since I am already using the logarithm function, I could reasonably go directly to the exponential distribution.
Answer: The following facts yields an extremely simple method.
If $U \sim U(0,1)$ and $W = 1 - \{1/U\}$ with $\{x\}$ denoting the
fractional part of $x$, we have $E(W) = \gamma$ and variance $Var(W)=
\psi(2)+2\int_0^1\ln\Gamma(t+1)\,dt -(1-\gamma)^2 \approx 0.081915$ where $\psi(x)$ is
the digamma
function.
So add up enough $W$'s.
Comment: This is the best answer as it doesn't anything but uniform numbers
Comment: This is interesting.  Do you know a reference for this fact?
Comment: No, my notes show I derived it 2002 while simulating some Markov chains.
Comment: I have added an additional answer proving the validity of this method.
Answer: Luis Mendo (2020) presented an algorithm that returns 1 with probability equal to $\gamma$, using the series expansion found by Sondow (2010), when the algorithm is given an infinite stream of fair coin flips, rather than a stream of uniform random variables in (0, 1). I have described it in my page on Bernoulli factory algorithms, which also includes algorithms to sample probabilities for many other kinds of functions and constants.
REFERENCES:

Mendo, L., "Simulating a coin with irrational bias using rational arithmetic", arXiv:2010.14901 [math.PR], 2020.
Sondow, Jonathan, "New Vacca-Type Rational Series for Euler's Constant and Its 'Alternating' Analog ln 4/π.". In Additive Number Theory: Festschrift in honor of the sixtieth birthday of Melvyn B. Nathanson, 331 (2010).
Answer: Here I will expand on the method described by Balasubramanian Narasimhan in his answer.
It is possible to simulate the Euler-Mascheroni constant by elementary methods, using uniform random variables and elementary functions.  This is less efficient than the other method I have shown in my other answer, which uses the exponential and logarithmic functions.  However, it has the advantage of requiring only uniform pseudo-random numbers and elementary mathematical operations.
Let $U \sim \text{U}(0,1)$ and define $X = 1 - \{ 1/U \}$, where the latter denotes the fractional part of a number.  We can use the law of the unconscious statistician and change-of-variable $r = 1/u$ to obtain the expectation:
$$\begin{align}
\mathbb{E}(X)
&= \mathbb{E}(1 - \{ 1/U \}) \\[6pt]
&= \int \limits_0^1 (1 - \{ 1/u \}) \ du \\[6pt]
&= \int \limits_1^\infty \frac{1 - \{ r \}}{r^2} \ dr \\[6pt]
&= \int \limits_1^\infty \frac{1 - r + \lfloor r \rfloor}{r^2} \ dr \\[6pt]
&= \int \limits_1^\infty \Big( \frac{\lceil r \rceil}{r^2} -\frac{1}{r} \Big) \ dr \\[6pt]
&= \int \limits_1^\infty \Big( \frac{1}{\lfloor r \rfloor} -\frac{1}{r} \Big) \ dr \\[6pt]
&= \gamma. \\[6pt]
\end{align}$$
(For the penultimate step, see a related question here.)  Since $\mathbb{E}(X) = \gamma$ (and it can also be shown that the variance is finite), taking $X_1,...,X_n \sim \text{IID U}(0,1)$ yields the natural estimator:
$$\hat{\gamma}_n = \frac{1}{n} \sum_{i=1}^n X_i = 1 - \frac{1}{n} \sum_{i=1}^n \{ 1/U_i \}.$$
The law of large numbers ensures that this is a strongly consistent estimator for $\gamma$, so it will converge stochastically to this value as $n \rightarrow \infty$.  We can implement this in R quite simply as follows. Using $n=10^6$ simulations we get reasonably close to the true value (though not as close as with the estimator in my other answer).
#Create function to estimate Euler–Mascheroni constant
simulate.EM <- function(n) {
  U  <- runif(n)
  UU <- 1/U
  Y  <- 1-UU+floor(UU)
  mean(Y) }

#Perform simulations
set.seed(1)
simulate.EM(10^6)
[1] 0.576843

#True value is 0.5772157
#Our simulation is correct to three DP

Comment: +1 Using `microbenchmark` on `simulate.EM <- function(n = 1e+5) {UU <- 1/runif(n); mean(1-UU+floor(UU))}` and `gumbel <- function(n = 1e+5) mean(-log(-log(runif(n))))`, this method actually appear to be about 8% faster than the one based on the Gumbel distribution.
Answer: If you want a horrible but fun way, think of the Coupon collector's problem.
Suppose that you have $n$ different cards which you want to collect. At each time $t\,\in\,\mathbb{N}$, you buy a card, which has probability $1/n$ of being the i-th card (even if you already have it) until you have all cards. You are not allowed to trade with other players. The question is: on average, how long does it take for you to collect all cards?
Being more precise, let $(C_t)_{t=1}^\infty$ an i.i.d. sequence of cards, $C_1 \, \sim \, U(\{1,\ldots,n\})$, and consider $S_t = \{C_1, \ldots, C_t\}$, the set of unique cards that you have at time $t$. Defining
$$\tau = \inf\{t\,\in\,\mathbb{N}: |S_t| = n\},$$
your goal is to compute $E[\tau]$. You can show that (just check wikipedia if you are bored)
$$E[\tau] = n\sum_{t=1}^n\frac{1}{t}\quad. $$
Therefore, you have that
$$\lim_{n\rightarrow\infty} \left(\frac{E[\tau]}{n} - \log(n)\right) = \gamma \quad.$$
Make $n$ large and use Monte Carlo to estimate $E[\tau]$. Or produce a card game with those rules, dare your friends to collect all cards, and do a real life Monte Carlo.


Calling a function at the end of every cmdloop iteration (cmd module)

Question: I am using the cmd module to build a little command line app and I am trying to call a function at the end of every cmd loop.
The only way I found to do this is to override cmdloop and add my function call in it.
Isn't there a much simpler/cleaner way to do this?
Answer: Implement the postcmd method:

Cmd.postcmd(stop, line)
Hook method executed just after a command dispatch is finished. This method is a stub in Cmd; it exists to be overridden by subclasses. line is the command line which was executed, and stop is a flag which indicates whether execution will be terminated after the call to postcmd(); this will be the return value of the onecmd() method. The return value of this method will be used as the new value for the internal flag which corresponds to stop; returning false will cause interpretation to continue.

The method will be called on your object (inheriting from cmd.Cmd) after each cmd has been performed. As long as you return False from this method  execution will continue.


Replace pages with static HTML pages

Question: I have a drupal 7 driven website.
I need to replace a handful of pages on that website using static HTML without impacting the URLs of those pages.
For example I have a page accessible at: example.com/services/
That page is a proper drupal driven page. I want that page to display a proper HTML page I've written from scratch without any input from drupal engine but still retain the same URL so anything that leads to that page (internal & external links) would continue to work.
What's the correct way to set that up.
Thank you!
Comment: 2 ways. 1.- place your html in body and set full HTML, 2.- override tpl.php template
Comment: thanks for your suggestion. Looking into it now I realized that these are not "pages" but some custom content types called "service".
Comment: If you are able, I would recommend creating the page(s) in a custom module for full control. It really depends on whether or not you are utilizing drupal regions or if the page is 100% custom HTML.
Comment: The replacement pages are 100% custom HTML. I ended up using this module to achieve what I need: https://www.drupal.org/project/static_page
Answer: This is an old question, but the answer is still the same now as it would have been then. Drupal only kicks in if a file does not exist at the given URL and path. For example, if you try to access the following path on a Drupal instance, where nonexistentfile.html does not exist, Drupal will kick in and show a Drupal 404 message:
<code>/[WEBROOT]/some/path/nonexistantfile.html
</code>
However, if you then create a file with that name at that location, that file wil be picked up rather than Drupal kicking in.
So the answer to the question is to create an .html file, and place it on the server at the required location, and it will be picked up.
Answer: lets say your url is : example.com/services , and services is 1 content type of drupal , and if you want to replace whole page with static html them create services.html page on root folder of your drupal and add whole html inside it and then run url it will display only html added in that html file.
Note : this html file name must be same as url , if url is example.com/abc then file name should be abc.html
Hope this helps you.

Replace pages with static HTML pages


How can I determine if the means between these two small groups are statistically the same?

Question: Group 1:    0.965116667, 0.972716667, 0.821216667, 0.870816667, 0.816216667
Group 2: 1.046016667, 0.794616667, 0.923416667, 0.927816667, 0.780816667, 0.729616667, 0.710816667, 0.788116667, 0.661316667, 0.750416667, 0.913716667, 1.0147166667
These are numerical data points generated by doing the same experiment in my Biochemistry lab on two different days. How can I conclude if their averages are statistically different from each other?
Would it be appropriate to use equivalence testing? How can I do that?
Can I just do a Welch t test (t.test in R) without testing for Normality?
Answer: Thank you for sharing your data in a convenient format. I will use R to take a careful look at them.
x1 =  c(0.965116667, 0.972716667, 0.821216667, 0.870816667, 0.816216667)
x2 =  c(1.046016667, 0.794616667, 0.923416667, 0.927816667, 0.780816667, 0.729616667,
        0.710816667, 0.788116667, 0.661316667, 0.750416667, 0.913716667, 1.0147166667)

summary(x1)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 0.8162  0.8212  0.8708  0.8892  0.9651  0.9727
var(x1);  sd(x1)
[1] 0.005756155
[1] 0.07586933

summary(x2)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 0.6613  0.7452  0.7914  0.8368  0.9245  1.0460
var(x2);  sd(x2)
[1] 0.01545716
[1] 0.1243268

There is some difference between the two sample means, but in view of the variability
of the samples, this difference does not seem large enough to be statistically significant.
Stripcharts of these observations show that the values of x2 (upper plot) extend below
and above those of x2. This will make it difficult for any test of location to
find a significant difference in the locations of the two samples.
stripchart(list(x1,x2), pch="|", ylim=c(.5,2.5))

Sample sizes are too small for definitive tests of normality, and there is no clear
evidence that the data are not normal. [Shapiro-Wilk tests, done but not shown, do
not reject normality.]
The Welch two-sample t test finds no significant difference in means.
t.test(x1,x2)

         Welch Two Sample t-test

data:  x1 and x2
t = 1.0616, df = 12.341, p-value = 0.3087
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.05484864  0.15971531
sample estimates:
mean of x mean of y
0.8892167 0.8367833

With such small samples, it is no surprise that an F-test finds no
significant difference in variances.
var.test(x1, x2)

        F test to compare two variances

data:  x1 and x2
F = 0.37239, num df = 4, denom df = 11, p-value = 0.3529
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.08710826 3.27466010
sample estimates:
ratio of variances
          0.372394

None of this 'proves' that the two samples are from the same population, but
I can find no evidence that they are not.
Notes: (1) Just as an experiment, suppose you have $n_1 = 5$ observations
from $\mathsf{Norm}(\mu_1 = .889, \sigma_1=.075)$ and $n_2=12$ from
$\mathsf{Norm}(\mu_2 = .937, \sigma_2=.124),$ where population
parameters roughly match your sample values, you would have only
about 1 chance in 10 of finding a significant difference between
those two samples.  Here is a simulation that estimates the poor power
(around 10%) of such experiments.
pv = replicate(10^5, t.test(rnorm(5, .889,.075),
                            rnorm(12, .837, .124))$p.val)
mean(pv <= .025)
[1] 0.09728

(2) Here is a two-sample Kolmogorov-Smirnov test of your data. It compares
empirical CDFs (ECDFs) of the two samples. It is known to have poor power
for such small sample sizes.
ks.test(x1,x2)

        Two-sample Kolmogorov-Smirnov test

data:  x1 and x2
D = 0.58333, p-value = 0.1312
alternative hypothesis: two-sided

The $D$-statistic is the maximum vertical distance between the two ECDF plots. The two plots are sufficiently 'entangled' that $D$ is not significantly large.
plot(ecdf(x2), col="brown",
     main="ECDF Plots of 2 Samples")
 lines(ecdf(x1), col="blue", lwd=2, lty="dotted")

(3) Other than small sample sizes, I see nothing 'quirky' or 'wrong' with your data.


If do retorno de função não reconhece true, apenas false

Question: Tenho esse código abaixo, que era pra se a função retornar true, ele executar o if. Porém ele só executa o if se eu alterar o if para false, ou seja, colocando "if(!linkAlreadySent)" invés de "if(linkAlreadySent)". Alguém sabe apontar o problema?
var link = "http://www.google.com";

function linkAlreadySent(link) {
    mysql.handle.query('SELECT link FROM links WHERE link = ?', [link], function (err, res) {
        if(res.length == 1) {
            console.log('Retornando true, o link ja existe');
            return true;
        } else {
            console.log('Retornando false, o link não existe');
            return false;
        }
    });
}

if(linkAlreadySent(link)) {
  console.log('Entrou no if');
}

Comment: João, a função `linkAlreadySent` não está retornando nada. Quem está retornando `true` ou `false` é a função declarada como callback do método `mysql.handle.query`. Para a função `linkAlreadySent` retornar algo, você precisa declarar esse retorno fora de funções internas. Mas aí você vai precisar tratar o asincronismo desse código, você já é familiar com isso ou precisa de uma resposta?
Comment: Não entendi bem o que quer fazer, mas, faz assim `var teste = linkAlreadySent(link)` e dá um **console.log** em teste pra ver o que retorna.
Comment: A função linkAlreadySent consulta a tabela para verificar se o registro do link já existe na coluna "link". Se existir, retorna true. Se não, false. O if logo abaixo irá ser chamado se a função retornar true
Comment: Hmmm, talvez seja isso mesmo @user140828. Mas não sou muito familiar com isso, pode me dar uma luz?
Answer: O problema é que o seu return está dentro da função de callback de mysql.handle.query. A função de callback está retornando uma valor, mas linkAlreadySent não.
Bom, na verdade, linkAlreadySent está retornando undefined, pois toda função sem um retorno explicito retorna undefined em JS. undefined é um valor "falsy", ele é tratado como falso quando você o utiliza com condição. Então como linkAlreadySent retorna undefined, e você utiliza esse retorno em if(linkAlreadySent(link)) você nunca cai dentro do if.

Como tratar o problema?
Não é tão simples quanto retornar o retorno de mysql.handle.query. O thread principal do JS não espera por processos de entrada e saída para continuar rodando o código. Quando você faz uma query no banco de dados, o JS continua rodando a função até o final, sai da função, e eventualmente quando o banco de dados devolve uma resposta, o JS executa a função de callback que você declarou. Nesse momento já é tarde demais para retornar um valor pela função linkAlreadySent, pois ela já terminou de rodar.

A abordagem funcional
Você pode passar uma função para linkAlreadySent executar quando ela receber a resposta do banco de dados. Da seguinte maneira:
var link = "http://www.google.com";

function linkAlreadySent(link, callback) {
    mysql.handle.query('SELECT link FROM links WHERE link = ?', [link], function (err, res) {
        if(res.length == 1) {
            console.log('Retornando true, o link ja existe');
            // A função é invocada se você entrar na condição desejada
            callback(res);
        } else {
            console.log('Retornando false, o link não existe');
        }
    });
}

// Passo o link e uma função anonima como parametros
linkAlreadySent(link, (res) => {
    console.log('Entrou no "if"');
});

A abordagem procedural
Com a utilização de promises, você pode "pausar" a função (mas não a thread principal) até você obter a resolução da promise. Promise é basicamente um objeto que empacota uma resposta, quando você obtêm a resolução da resposta, basta desempacota-la da promise. Fazemos isso com o operador await.
// Módulo do NodeJS
var util = require('util');

var link = "http://www.google.com";

// Crio uma nova função que retorna promises
var mysqlQuery = util.promisify(mysql.handle.query);

// Funções precisam do modificador async para poderem utilizar await
// Toda função do tipo async retorna resultados empacotados em promises
async function linkAlreadySent(link) {
    // Espero pela resposta
    var res = await mysqlQuery('SELECT link FROM links WHERE link = ?', [link]);

    if (res.length == 1) {
        console.log('Retornando true, o link ja existe');
        return true;
    } else {
        console.log('Retornando false, o link não existe');
        return false;
    }
}

async verificaLink() {
    // Novamente, utilizamos await, pois o retorno de linkAlreadySent está empacotado
    if (await linkAlreadySent(link)) {
        console.log('Entrou no if');
    }
}

verificaLink();

Comment: Agora eu entendi o que você quis dizer. NA [API mysql-libmysqlclient](https://sannis.github.io/node-mysql-libmysqlclient/api/) cada função tem duas versões uma é síncrona e outra é assíncrona, a diferença é que a funções sincronas tem o sufixo `Sync` na frente das funções. Visto isso você tem razão meu código não funciona. Estou o retirando minha resposta.
Comment: Entendi perfeitamente, obrigado!!


CodeIgniter multi-step signup

Question: I'm currently working on a web app that uses CodeIgniter, in specific, I'm building the signup forms.
This form consists out of 3 different steps, I'm just wondering what the best way would be in order to save information between 2 steps?
I only want to save all the information into the database once the final stap has been completed.
Should I just use native PHP session to do this? Or should I use CI flashdata?
Comment: See this::http://stackoverflow.com/questions/5083014/multi-step-page-form-in-php-codeigniter
Comment: `flashdata` would only last for one page.  Why not just use [CI sessions](http://ellislab.com/codeigniter/user-guide/libraries/sessions.html)?
Comment: @AaronW. Possible too, I was looking for suggestions what the best way was.
Answer: I've used hidden fields and sessions. I tend to save all the form values to a text file at the end of each stage. Then if something happens at any given stage I can choose what to do with the data saved in the text file.
For instance, if you wished you could capture an email address on the first page. If the form doesn't get completed (as in all stages were successfully completed), then you could send an email to the prospective user with a link to the signup form at the appropriate stage. The prospective user would not have to re-fill any of the fields they've filled in and you get a chance to recapture them as a user.
I tend to use the text file as a default way to save all the data from any stage of a multi-stage form. At the end of the form I can process the data into the DB and delete the text file. To catch partially complete signups I can write a script that is executed via a cronjob that runs every minute. I always save a timestamp in the data file representing the last time the file was updated. If the timestamp is more than X minutes old you run your didn't finish signup script on it or just delete it.
Saving data at each stage is simple. After the first stage you create the $dataFile with $dataFile = file_put_contents(json_encode($data)); where $data = array('timestamp'=>...) + $_POST;. Each stage after the first I use $data = json_decode(file_get_contents($dataFile), TRUE) + $_POST;, and $data['timestamp'] = ...;. Then use file_put_contents to save it back to the file. Obviously, you will need to track the $dataFile variable but this is easily done via hidden fields or session data.
The file will only be out there for X minutes before being processed and deleted but if you wish to have security on the text file during that short time frame then I'd suggest looking up a cipher or creating your own variation on the simple Ceasar's cipher. Use the cipher on the field names as well as the values.
Back in Dec I was working through the issue of multi-stage forms in the latest CodeIgniter for myself. I can't say this is a generally accepted best practice but the text file has worked well for me in the past. I posted some sample code for a multi-stage form here: Multi-Stage Form Example.
Comment: Can you explain why you think it's a better process to save the array in a file as oppossed to saving in the database with something like serialize(array())?

Also, just to point out, CI has an encryption class which allows you to choose ciphers.
Comment: First I want to preface my comment by saying that my experience is entirely on my own projects using LAMP setups. I suppose that my bias towards text files over DBs can be traced back to a non-optimal LAMP setup. On my dev boxes I have found that too many DB connections (sometimes below 1000) can bring down my quad-core Ubuntu server. I have not found this to be true on my dedicated server but I've also done everything I can to avoid it. I have also found that saving data to DBs is slower than to a text file (again not tested on my dedicated server).
Comment: On my dedicated server I run primarily WordPress sites which can tie up quite a few MySQL connections. As a result of these experiences I control the number of DB connections I open with any tool. The easiest solution I've found is to use text files and post-process via cron job. A cronjob with 1 DB cxn can process a lot of text file in under a minute. Typically, I can handle user feedback from the text file and 1 min delay on it getting to the DB is not an issue.
Comment: One final note on DBs. I run several WordPress sites per server. Some DBs get corrupted while others, on the same server/hard drive, do not. I've never figured out why  but the text files can serve as a backup if need be. If you make daily backups of the DB and discover corruption at the end of the day, simply restore the backup and re-process the text files. To date I have not had any corruption issues with text file. That's my 2 cents.
Answer: You can post the values to the next form where you can use hidden fields to store these values so that when you post the last form you have all the values and you can process them at one time.
Comment: I wouldn't recommend this process. How would you protect me from manipulating the hidden input values using a programme such as firebug and successfully completing to the next step? You'd need to validate the hidden form data was as submitted rendering this process pointless.
Comment: @Timmytjc Thanks for your feedback. I will keep this in mind for future.
Answer: I faced this case in my latest project and I used the session of Codeigniter to save the data submitted after validation in userdata session as array in the last step I send data to the DB

CodeIgniter multi-step signup


Safari linear gradient

Question: I have a trouble using linear-gradient in Safari 15.2.
I wanna to create a block with fade effect at the end if children do not fit.
I have created an example: https://codepen.io/serejich/pen/xxXLvEG.
Code:

* {
  margin: 0;
}

.gradient-container {
  background-color: coral;
  width: 200px;
  height: 30px;
  position: relative;
  display: flex;
  align-items: center;
}

.elements {
  padding: 0 10px;
  display: flex;
  column-gap: 10px;
  overflow-x: hidden;
}

.elements p {
  color: #fff;
  white-space: nowrap;
}

.gradient {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, rgba(255, 255, 255, 0) calc(100% - 50px), coral);
}

Element 1

Element 2

Element 3

If you will open this in Safari, there would be something like a white area at the right of block.
What causes this and are there any ways to fix it?
Answer: This is a bug in safari that has to do with the way browsers interpolate between gradients.
A common answer is to do what you did, using rgba(255, 255, 255, 0), however, Safari still interprets this as white and leads to that unwanted additional of white to the gradient. A fix is to use the same color you are transitioning from (coral in this case) and set it to transparent, which for coral would be rgba(255, 127, 80, 0). The example below uses the code from your pen with the fix applied for safari.
See this stack for more

* {
  margin: 0;
}

.gradient-container {
  background-color: coral;
  width: 200px;
  height: 30px;
  position: relative;
  display: flex;
  align-items: center;
}

.elements {
  padding: 0 10px;
  display: flex;
  column-gap: 10px;
  overflow-x: hidden;
}

.elements p {
  color: #fff;
  white-space: nowrap;
}

.gradient {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /*RGBA equivalent to coral html color with alpha set to 0 - fixes safari interpolation bug*/
  background: linear-gradient(to right, rgba(255, 127, 80, 0) calc(100% - 50px), coral);
}

Element 1

Element 2

Element 3

Safari linear gradient


Limit of $(5n^2+2n)/(n^2-3)$ using limit definition

Question: I'm having trouble using the limit def to find this answer
I have to show $\lim (5n^2+2n)/(n^2-3) = 5$
so I have so far $|(5n^2+2n)/(N^2-3) - 5| < \epsilon$ so from there I got $|(2n+15)/(n^2-3)|<\epsilon$ so then $|(2+15/n)-(n-3/n) \leq 17/(n-3)$
From there I don't know what to do.
The answer I was given says that $n=(1/\epsilon + 3)$ then $n \ge N$ thus the inequality is true. But I'm not exactly sure why
Comment: You seem to be inconsistent when to use N and when to use n.  They aren't supposed to be the same number.
Answer: First note $(5n^2+2n)/(n^2-3) > 5$ for all positive n.
So for an arbitrary $\epsilon$ we want it to be true that
$(5n^2+2n)/(n^2-3) - 5 < \epsilon$ for all large enough n.  So we want to find just how large the n have to be for this to be true.  We assume n > 0 so
$5n/(n^2 - 3) < (5n^2+2n)/(n^2-3) - 5 < \epsilon$
$5n^2 - 5(n^2-3) < (n^2-3) \epsilon$
$15 <  (n^2 - 3)\epsilon$
$15/\epsilon +3 < n^2 $
So this will be true for all $n >= N >= \sqrt(15/\epsilon +3)$.


Is there a math function that takes in a number n and return a number with the digits of n sorted by value?

Question: The idea crossed my mind of a function that would take in an integer n and return an integer of the digits of n in order.
e.g:
g(184729) = 987421
g(1212121212) = 2222211111
g(1234567890) = 9876543210
I looked it up and saw a similar question, but the answer did not satisfy me as I was looking for one using only basic arithmetic operations, summations, logarithme and modulo.
Comment: Were I automating it, I'd do so recursively.  Note that the answer is always a $10-$tuple of non-negative integers (telling you how many $9's, 8's, \cdots$ you need).  Now you just read off the first digit, increment the relevant index by $1$, and proceed.
Comment: are you suggesting having a function that returns how many of each number you have, so that f(9333381) = 1100004010 and then read this number 1 digit at a time to build the sorted output ?
Comment: Yes, exactly.  That way I only need to store the current $10-$tuple.
Comment: Should say, just as a matter of notation, I wouldn't write the $10-$ tuple as a string the way you did.  I'd write $9833331$ as $(1,1,0,0,0,0,4,0,1,0)$.  The way you wrote it would be ambiguous if there were more than $9$ of any given digit(s).
Comment: True, thats what I was about to say. That was actually my first try and worked well until you have more than 9 time the same digit. I changed approach and calculate them individually in my answer because I wanted to avoid storing numbers in an array
Comment: In Mathematica you'd convert the number to its list of digits, sort the list, then reconvert back to the original number.
Comment: That's how you'd do it in any programming language
Answer: So the answer I came up with might not be the best, but I am satisfied with it.
I splited the following answer in 4 functions (for obvious reasons) but it can be made into one single function.
First, i(x,n) returns the nth digit of x
$i\left(x,n\right)=\left\lfloor\operatorname{mod}\left(\frac{x}{10^{n}},10\right)\right\rfloor$
c(x)+1 returns the length of x
$c\left(x\right)=\left\lfloor\log\left(x\right)\right\rfloor$
f(x,n) counts how many time the digit n is in x
$f\left(x,n\right)=\sum_{p=0}^{c\left(x\right)}0^{\left|i\left(x,p\right)-\operatorname{mod}\left(n,10\right)\right|}$
Finally, g(x) checks f(x) for how many time each digit is in x, multiply them by $\frac{1}{9}$ multiply by 10 one last time so the summation of digits don't overlap
$g\left(x\right)=\left(\sum_{n=1}^{9}\left\lfloor\left(\frac{1}{9}\right)\cdot10^{f\left(x,n\right)}\right\rfloor\cdot n\cdot10^{\sum_{k=1}^{n-1}f\left(x,k\right)}\right)\cdot10^{f\left(x,0\right)}$
Comment: What does $\mathrm{mod}(x,y)$ mean?
Comment: I may not have formatted it the correct way, but its modulus, or remainder operation. Mod (x,y) would return what remains of x after substracting y the maximum number of time. So mod(43,10) returns 3, because after substracting 10 4 times you are left with 3.
Comment: If $x = 358$ and $n=2$, I take $\mathrm{mod}(3.58,10)$.  What does it mean to take the remainder of a decimal number mod $10$?
Comment: since you can't substract 10 from 3.58, you are left with 3.58, which is the floored to 3
Comment: OK, so that returns the first (third?) digit of $x$, not the 2nd.
Comment: Excatly, because the count starts at n=0, so i(358,0) returns the first digit. c(x) also returns the number of digit-1, so c(123456) returns 5, not 6, because the first digit counts as 0