Magical Summation Interchange

Question: In this answer I use the following result:
$$\lim_{p\to\infty}\sum_{n=1}^p\sum_{k=0}^\infty\frac{(-1)^{n+k}}{n(2k+1)^n} = \lim_{p\to\infty}\sum_{k=0}^p\sum_{n=1}^\infty\frac{(-1)^{n+k}}{n(2k+1)^n}$$
This came purely by intuitition though - I have no idea how to prove this result!
I have numerically checked this and it seems to hold; moreover, using this interchange I derived the correct result in the linked post. If I were simply interchanging the summations I could produce a trivial proof, though I am also interchanging summation limits.
Is there an easy proof of this?
Comment: Since the family of terms where $n \geqslant 2$ and $k \geqslant 1$ is absolutely summable, one needs only care about $n = 1$ or $k = 0$. Splitting these off, you get the result straightforwardly.
Answer: You may notice that
$$ \int_{0}^{1} x^{2k}\frac{(-\log x)^{n-1}}{(n-1)!}\,dx = \frac{1}{(2k+1)^n} $$
hence both the RHS and LHS equal
$$ \int_{0}^{1} \sum_{k\geq 0}\sum_{n\geq 1}\frac{(-1)^n(-\log x)^{n-1}}{n!}(-1)^k x^{2k}\,dx =\int_{0}^{1}\frac{1-x}{\log(x)}\cdot\frac{1}{1+x^2}\,dx$$
by the dominated convergence theorem, since the exponential function is an entire function and $\frac{1}{1+z^2}$ is an analytic function whose radius of convergence at the origin equals one. Through the substitution $x=e^{-t}$ and Frullani's theorem the last integral equals $$\log\,\left(\frac{\pi\sqrt{2\pi}}{\Gamma\left(\frac{1}{4}\right)^2}\right)=\log\,\left(\frac{\Gamma\left(\frac{3}{4}\right)^2}{\sqrt{2\pi}}\right)\approx -0.512376630342 $$
Comment: Genius. Simply genius. I'll check your work later, but it looks perfect. Thank you!
Answer: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
\begin{align}
\lim_{p \to \infty}\sum_{n = 1}^{p}
\sum_{k = 0}^{\infty}{\pars{-1}^{n + k} \over n\pars{2k + 1}^{n}} & =
\lim_{p \to \infty}\sum_{n = 1}^{p}{\pars{-1}^{n} \over n}
{1 \over \ic}\sum_{k = 0}^{\infty}{\ic^{2k + 1} \over \pars{2k + 1}^{n}}
\\[5mm] &=
\lim_{p \to \infty}\sum_{n = 1}^{p}{\pars{-1}^{n} \over n}\,
{1 \over \ic}\sum_{k = 1}^{\infty}{\ic^{k} \over k^{n}}\,
{1 - \pars{-1}^{k} \over 2} =
\lim_{p \to \infty}\Im\sum_{n = 1}^{p}{\pars{-1}^{n} \over n}\,
\sum_{k = 1}^{\infty}{\ic^{k} \over k^{n}}
\\[5mm] &=
\lim_{p \to \infty}\Im\sum_{k = 1}^{\infty}\ic^{k}
\sum_{n = 1}^{p}{\pars{-1/k}^{n} \over n} =
-\Im\sum_{k = 1}^{\infty}\ic^{k}\ln\pars{1 + {1 \over k}}
\\[5mm] & =
-\,\Im\sum_{k = 1}^{\infty}\ic^{k}\int_{0}^{1}{\dd t \over t + k} =
-\int_{0}^{1}\Im\sum_{k = 1}^{\infty}{\ic^{k} \over k + t}\,\dd t
\\[5mm] & =
-\,{1 \over 2}\int_{0}^{1}
\sum_{k = 0}^{\infty}{\pars{-1}^{k} \over k + 1/2 + t/2}\,\dd t
\\[5mm] & =
{1 \over 4}\int_{0}^{1}
\sum_{k = 0}^{\infty}\pars{{1 \over k + 3/4 + t/4} -
{1 \over k + 1/4 + t/4}}\,\dd t
\\[5mm] & =
{1 \over 4}\int_{0}^{1}\bracks{\Psi\pars{{t \over 4} + {1 \over 4}} -
\Psi\pars{{t \over 4} + {3 \over 4}}}\,\dd t\quad
\pars{~\substack{\Psi:\ Digamma\ Function}~}
\\[5mm] & =
\left.\ln\pars{\Gamma\pars{t/4 + 1/4} \over \Gamma\pars{t/4 + 3/4}}
\right\vert_{\ 0}^{\ 1} =
\ln\pars{{\Gamma\pars{1/2} \over \Gamma\pars{1}}
\,{\Gamma\pars{3/4} \over \Gamma\pars{1/4}}}
\\[5mm] & =
\ln\pars{\root{\pi}
\,{\Gamma^{\,2}\pars{3/4} \over \Gamma\pars{3/4}\Gamma\pars{1/4}}} =
\ln\pars{\root{\pi}\Gamma^{\,2}\pars{3/4} \over \pi/\sin\pars{\pi/4}}
\\[5mm] & =
\bbx{\ln\pars{\Gamma^{\,2}\pars{3/4} \over \root{2\pi}}} \approx -0.5123
\end{align}

Magical Summation Interchange


BTYD R package. Why dropping the first puchase in the BG/NBD model?

Question: I am using the BTYD package on R to estimate the probability of a customer churning after my calibration period. At the moment, I have been following the walkthrough provided on the web which you could find here: https://cran.r-project.org/web/packages/BTYD/vignettes/BTYD-walkthrough.pdf. I have also been working with the newly released package CLVTools which basically does the same as the BTYD package.
My question is regarding a specific point in both packages I do not understand in the BG/NBD model. On page 17 of the walkthrough, it is written the following :

The final cleanup step is a very important one.  In the calibration period, the BG/NBD model is generally concerned with repeat transactions—that is, the first transaction is ignored.  This is convenient for firms using the model in  practice,  since  it  is  easier  to  keep  track  of  all  customers  who  have  made at least one transaction (as opposed to trying to account for those who have not made any transactions at all)

Despite this sentence, I do not understand the logic why we should drop the first transaction. But more importantly, the probability of being alive, which you can calculate with the bgnbd.PAlive function provided in the BTYD package always gives 1 for customers who have made a single transaction in the past (calibration period). That looks wrong to me, or at least I do not understand it.
I have been exploring the package with the dataset "apparelTrans" provided in the CLVTools package and my calibration period is the 40 first weeks. With this setup, customer 1 (Id==1) has a probability of being alive of 1 at the end of the calibration period despite the fact that he made one single purchase at the very beginning of the calibration period (I would expect a higher probability of churning). I have compared the BTYD and the CLVTools package and both give the same results, which hints I am missing something.
Would someone be kind enough to explain to me why we drop the first purchase in this model and why the probability of being alive is always 1 for customers who made one purchase only?
Any help would be really appreciated
Answer: The BG/NBD model assumes that all customers are active at the beginning of the observation period and that a customer can only drop out immediately following a transaction. See section 3 of this paper for details: http://brucehardie.com/papers/018/fader_et_al_mksc_05.pdf. Customers with no repeat transactions during the observation period haven't had a chance to drop out so their probability of being alive equals 1. In essence, the model can't determine if those customers without any repeat transactions have "died" or if their shopping behaviors are such that they make very infrequent purchases.
Answer: this is old but I thought I'd leave the information here in case someone else bumps into it while searching. You could use the modified BG/NBD, which does account for the possibility that customers can drop out (never purchase again) after the first purchase
Comment: It would be helpful to clarify how this answer is a useful solution for the question.


creating a function that only returns odd numbers

Question: I've spent an embarrassing amount of time on this question only to realize my function is only right 50% of the time. So the goal here is to return only the odd numbers of all the numbers in between the two arguments. (for instance if the arguments are 1 and 5 i'd need to return 2 & 3) the function I wrote is completely dependent on the first argument. if it's even my function will return odds, but if the first number is odd it'll return evens. does anyone know how i can fix this?

function oddNumbers(l, r) {
  const arr = [];
  const theEvens = [];
    for (let i= l; i<r; i++) {
      arr.push(i)
    }
  console.log(arr)
  for (let i= 0; i < arr.length; i+= 2 ) {
      const evens = arr[0] + i;
      theEvens.push(evens);
  }
  theEvens.forEach(item => arr.splice(arr.indexOf(item), 1));
  console.log(arr)
}

oddNumbers(2, 20);
Comment: _"to return only the odd numbers"_, _"need to return 2 & 3"_ - `2` is odd?
Comment: Your function never tests the elements of the array if they are even or odd.
Answer: I modified the code a bit to return only odd numbers
We use the % operator that behaves like the remainder operator in math:
so when we say i % 2 if the number is even the result of the operation will be 0
but when the "i" is an odd number the result will be 1
so now we can filter the even from the odd numbers using this operation

function oddNumbers(l, r) {
  const arr = [];
    for (let i= l; i<r; i++) {
      if(i % 2 !== 0) arr.push(i);
    }
  console.log(arr);
}

oddNumbers(2, 20);
Comment: added a short explinaton
Comment: While this solution works, it's mostly code and doesn't explain why it works. It would help if you expanded on the use of the [remainder operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder) which op didn't use in their code.
Answer: You can loop from initial to end parameters and get odd numbers using modulo, try this:

let result = [];
let returnOdd = (n1, n2) => {
    for(i = n1; i < n2; i++){
        if(i % 2 != 0){
            result.push(i)
        }
    }
    return result;
}

console.log(returnOdd(2, 20));
Answer: You could use the filter method.
This method creates a new array based on the condition it has. In this case it will to go through all the numbers in the array, and check if the number is odd (though the remainder operator).
For example:
1 % 2 = 1 ( true, keep in the new array )
2 % 2 = 0 ( false ignore in the new array )

function OddNumbers(start, end) {
  // Create an array from the given range
  const nums = Array(end - start + 1).fill().map((_, idx) => start + idx);
  // Use filter to return the odd numbers via the % operator
  return nums.filter(num => num % 2);
}

console.log(OddNumbers(2,20))


Show the improper integral $\int^{\infty}_0 \frac 1 y e^{-y} dy$ doesn't converge.

Question:
Show the improper integral $\int^{\infty}_0 \frac 1 y e^{-y} dy$ doesn't converge.

Using Wolfram Alpha: http://www.wolframalpha.com/widgets/view.jsp?id=8ab70731b1553f17c11a3bbc87e0b605 the result is as stated (divergent)
I know I can prove that $$\int^{\infty}_0 \frac 1 y e^{-y} dy$$ doesn't converge if I can find a divergent function $f(y): 0 \le f(y) \le \frac 1 y e^{-y}$ for $y \rightarrow \infty$.
Can someone come up with such a function ? I was thinking $f(y) = \frac 1 y$ because I know the series corresponding to $\{\frac 1 n\}$ is divergent, but this function doesn't have the desired property.
Comment: The problem with divergence lies in the point $0$, not in $\infty$.
Comment: @user111854  i think it diverges because it blows up near zero so just consider the definite integral for 0 to 1 and limit it by using 1/y
Comment: Near $0$ the integrand behaves as $1/y$.
Answer: For $y>1$, the value $\frac{1}{y}e^{-y}$ is smaller than $e^{-y}$, and since the integral $\int_1^\infty e^{-y}dy$ converges, the integral $$\int_1^\infty\frac{1}{y}e^{-y}dy$$ also converges. That should tell you that the problem with the integral may lie elsewhere.
Comment: Ahh ... But what function can I use to show the integral of $\frac 1 y e^{-y}$ on $(0,1]$ is arbirarily large ?
Comment: My hint would be this: on $(0,1]$, the function $e^{-y}$ takes only values in $[1,e]$.
Comment: I see, so we can use $\frac e y \ge \frac 1 y e^{-y}$ and $\frac e y \rightarrow \infty$ for $y \rightarrow 0$.
Comment: Yes, but that does not yet prove that the integral does not exist. For example, the integral $$\int_0^1\frac{1}{\sqrt y}dy$$ exists.
Comment: But $\int \frac e y \ dy = e \ln(y)$ and $e \ln(y) ]^1_{0_+} = 0 - (\lim_{y \rightarrow 0_+} e \ln(y)) = \infty$ ? Is that enough ?
Comment: Indeed it is. You have proven that your function is smaller than a function which has no integral on $[0,1]$, thus proving the same for your function.
Comment: On $(0,1]$ the function $e^{-y}$ takes values in $[\frac 1 e,1)$ not $[1,e]$.
Comment: Sorry, my bad. But still, the point remains that it is bounded.


Convert integer retrieved from node field to uid?

Question: I've created a rule that sends a push notification to a specific user when a certain content type node is published. That said, in order to specify which user receives the notification, the 'Send Push Notification to Specific User' rule requires that I type in an existing uid.
That said, I've pulled an integer from a content type field, and I want to set that integer as the uid to send the notification to (as the user receiving a notification changes often). Naturally, Drupal won't let me do that, even after I convert the retrieved string (numeral, e.g. 7) to an integer.
Is it possible to achieve this? I'm stumped.
Comment: try views rules, see https://drupal.stackexchange.com/questions/261253/how-to-configure-the-contextual-filter-in-a-view-for-rules/261254#261254
Answer: You have to fetch the User entity by ID using the value from the content type field without any conversion.
Then in the "Send Push Notification to Specific User" rule, you can use the ID from the fetched entity.
Not sure why you can't use the variable directly, but this is the case.


Format an f-string for each dataframe object

Question: Requirement
My requirement is to have a Python code extract some records from a database, format and upload a formatted JSON to a sink.
Planned approach
1. Create JSON-like templates for each record. E.g.
 json_template_str = '{{
   "type": "section",
   "fields": [
       {{
           "type": "mrkdwn",
           "text": "Today *{total_val}* customers saved {percent_derived}%."
       }}
     ]
 }}'

2. Extract records from DB to a dataframe.
3. Loop over dataframe and replace the {var} variables in bulk using something like .format(**locals()))
Question
I haven't worked with dataframes before.
What would be the best way to accomplish Step 3 ? Currently I am
3.1 Looping over the dataframe objects 1 by 1 for i, df_row in df.iterrows():
3.2 Assigning
    total_val= df_row['total_val']
    percent_derived= df_row['percent_derived']

3.3 In the loop format and add str to a list block.append(json.loads(json_template_str.format(**locals()))
I was trying to use the assign() method in dataframe but was not able to figure out a way to use like a lambda function to create a new column with my expected value that I can use.
As a novice in pandas, I feel there might be a more efficient way to do this (which may even involve changing the JSON template string - which I can totally do). Will be great to hear thoughts and ideas.
Thanks for your time.
Answer: I would not write a JSON string by hand, but rather create a corresponding python object and then use the json library to convert it into a string. With this in mind, you could try the following:
import copy
import pandas as pd

# some sample data
df = pd.DataFrame({
    'total_val': [100, 200, 300],
    'percent_derived': [12.4, 5.2, 6.5]
})

# template dictionary for a single block
json_template = {
    "type": "section",
    "fields": [
        {"type": "mrkdwn",
         "text": "Today *{total_val:.0f}* customers saved {percent_derived:.1f}%."
        }
    ]
}

# a function that will insert data from each row
# of the dataframe into a block
def format_data(row):
    json_t = copy.deepcopy(json_template)
    text_t = json_t["fields"][0]["text"]
    json_t["fields"][0]["text"] = text_t.format(
        total_val=row['total_val'], percent_derived=row['percent_derived'])
    return json_t

# create a list of blocks
result = df.agg(format_data, axis=1).tolist()

The resulting list looks as follows, and can be converted into a JSON string if needed:
[{
    'type': 'section',
    'fields': [{
        'type': 'mrkdwn',
        'text': 'Today *100* customers saved 12.4%.'
    }]
}, {
    'type': 'section',
    'fields': [{
        'type': 'mrkdwn',
        'text': 'Today *200* customers saved 5.2%.'
    }]
}, {
    'type': 'section',
    'fields': [{
        'type': 'mrkdwn',
        'text': 'Today *300* customers saved 6.5%.'
    }]
}]

Comment: Thanks so much for taking the time to reply in detail.
The goal was to have the format in a json file so the file can be changed without need for a change to py code. That was also the reason for looking at `.format(**locals()))` because we have the list of attributes in a data dictionary and the diff teams can use diff formats with the attributes they are using.
I left that detail out in the question, so I actually will not use a string like `json_template_str = '{{` but read a json file that has the template code.
Comment: Having said that, thanks again for taking the time and sharing your approach. And as I am a newbie here I am not allowed to upvote your answer.


How to show that there is no relationship

Question: I have some data which I am using to show that there is no relationship between two variables. (Or only a weak one.) In a previous writeup, I included the scatterplot showing no visible relationship, as well as the Pearson's and Spearman's correlation coefficients, which were both low. One of the reviewers commented that the statistics in the paper were weak and we 'only showed some correlations'. Is there any better way to show that there is little/no relationship between these variables? I have transformed the data using every reasonable transformation (log, square root, exponentiation), and even averaged the values for each datapoint, though it seems to me that that is falling prey to the ecological fallacy. Nothing. Not even a hint of a pattern anywhere.
Obviously, I can't show everything I tried. I want to show that there is no (or at least little) relationship, because common wisdom is that there should be a strong one. The fact that there is not a strong relationship is a surprising result. I know that you can't prove the null hypothesis, but I would like to show as much as possible that any other options are unlikely.
How do I convincingly show that there is no relationship between the variables? (Other than hypothesis-testing my correlations, which I am planning to do, but that doesn't show that there is no relationship.)
Note that both variables have heavy-tailed distributions - does that make a difference to the answer?
Below is a scatterplot of my data.
Comment: It might help if you provided some images of your data or a description of what they actually are. And maybe discuss why you WANT to show that there is no relationship. In general, the burden of proof is on demonstrating a relationship. You can no more PROVE the null hypothesis than you can prove that there is no flying spaghetti monster (aside from laying out a convincing argument related to the lack of evidence, which is what you're really asking about - how to bolster your story).
Comment: I added some explanation as to why I want to do this - thanks!
Comment: If "common wisdom" says that there "should" be a relationship, you must address that at the theoretical level with reasoning that is discipline-specific. The stats answers given below help, however the burden of proof relies on discipline-specific theory. There are plenty of spurious correlations between variables, which people acknowledge have no basis in theory so therefore they are just taken to be curious coincidences, not a sign of some un-discovered theory.
Answer: One approach is to start by determining what you consider to be little relationship (ideally this should be done before any look at the data, possibly having an expert in the field who has not seen your data or summaries help her will make this more objective).  For example you might consider any correlations between -0.1 and 0.1 to be "little" or meaningless, or some other boundaries.  This region is sometimes called the equivalence region.  Now compute a confidence interval on your correlation, if the CI falls completely inside your equivalence or little importance region, then that is strong evidence that if there is a relationship, it is not worth getting excited about or worrying about.  If part of the confidence interval extends past your bounds then that implies that there is a chance that the true relationship could be in the interesting region and you need more study/data to narrow the interval.
If you (or the reviewers) are concerned that there may be some transformations of the variables that show more of a relationship, then you could first run the ACE (Alternating Conditional Expectations) algorithm on the data.  This tries to find the transformations that give the highest correlation between variables.  If ACE cannot find a transformation (or pair of transformations) with a high correlation then there likely are not any reasonably smooth transformations that would.
Edit:
Additionally you may want to consider this paper:

<code> Buja, A., Cook, D. Hofmann, H., Lawrence, M. Lee, E.-K., Swayne,
 D.F and Wickham, H. (2009) Statistical Inference for exploratory
 data analysis and model diagnostics Phil. Trans. R. Soc. A 2009
 367, 4361-4383 doi: 10.1098/rsta.2009.0120
</code>

Including some of the suggested plots where you have permuted to guarantee no real relationship as a comparison may be convincing to the reviewers/audience that your date is consistent with no (or little) relationship.
Answer: If you have enough data, an alternative way would be to estimate the empirical joint distribution, and pit it against the product of the marginal distributions. Here too the null hypothesis would be $f_{XY}(x,y) = f_X(x)f_Y(y)$  (i.e. independence), and you will try to disprove it.
This CV post How to compare joint distribution to product of marginal distributions?
elaborates, and the various comments there provide also references you can look up.


Has anyone got processing.js working in IE?

Question: I'm looking for examples of processing.js working in Internet Explorer via ExplorerCanvas or similar.
Answer: It can be done! There are some gotchas, however. The page htxt links to is fine, as far as it goes, but please note the following:
1) Both script and canvas elements must have id attributes. The init function uses these attribute id's to associate a given script with a given canvas. I found the simplified init function easier to understand than the official one. You will want to master the official one if you have multiple canvases on one page.
2) If you use internet-style color designations, like #23ff9a, watch out! IE 8 wants all upper case hexadecimal color numbers from Processing.js/canvas. Write #23FF9A! This is what the documentation shows, so it shouldn't be a complete surprise. The error is a sometime thing, which makes it crazy to figure out. Mostly, larger numbers (for lighter colors) with lots of f's seem to be afflicted. White, #ffffff, is OK, but #ff00ff is not. Firefox and Safari are case-insensitive in this regard. The documentation says you can use an alternate hex notation with alpha channel (the CC) that looks like 0xCC006699. This didn't work for me; maybe it's on the to-do list.
3) The .equals() method on strings is missing! Andor Salga, one of the Seneca College crew working on Processing.js, wrote a simple boolean stringsEqual(str1, str2) function you can see here. This will do until the matter is definitively fixed.
4) It's not true that stroke() doesn't work with excanvas.js. It does. However, if your Processing.js code has even one little syntax error (I can't really categorize which kinds, but trying to use .equals() will do it) your routine will probably fail silently in IE8, whereas, in Safari or Firefox, your rectangles may lose their outlines, i.e. stroke() will quit working. IE on Vista, and Safari on the Mac, have both exhibited stronger syntax checking than Safari or Firefox on Vista, which will blow by certain errors and render a defective graphic.
5) Text, invoked using the text() function, renders in Firefox (in an unchangeable font of Firefox's choosing), but, as far as I can tell, not in IE8 or Safari. The Glyph Method is suggested here. The code is in place, but getting the fonts looks like a problem. Inkscape looks pretty impenetrable to me. As far as I can tell, what is needed is a lot like old pen-plotter fonts - a vector path with pen-up and pen-down commands between runs of nodes. Turns out FSF/GNU has some that might be massaged into the right format without too much trouble. I don't know where the format is defined, but it's probably over at W3C somewhere. The approach with real potential for presentable fonts is the IE/VML wing of Cufon. See How does it work? I really want this last link in the chain, but I could use some help.
Processing.js is one whale of a project that deserves our support. It has enormous potential. I would encourage you to pitch in if you are able.
Answer: The sparklines example on the processing.js exhibition page uses ExplorerCanvas. It seems like it's just a drop-in solution, no extra coding necessary.
Comment: Thanks, that was helpful. It looks like the sparklines example has its own onload method, while other examples use some type of init.js, which doesn't seem to work in IE + excanvas
Answer: This page describes how to get processing.js + excanvas working together.
It basically involves writing your own onload init method that IE can understand.
Comment: Unfortunately even though it works, the VML IE uses doesn't seem to support some essential stuff. If you're porting a processing app, don't expect much beyond 2D line drawing from IE :(


How can I plot relationship between two variables with the constraint of initiating the S-shaped curve from (0,0) as depicted in the example below?

Question: I don't want to run logistic regression or any such regression model. I have 8 to 10 pairs of data points and want to analyze the logistic growth model by fitting any function-generating S-shaped curve like tanh() or so on. I am using R for the assigned task.
The blue points in the graph represent the data points and I want to fit the curve as depicted in the graph.
Comment: @BenBolker I tried plotting as you suggested but not yielding expected result, Can you please refer the chart I have attached and suggest something based on it? Thanks in advance.
Answer: Using the data sent to me:
dd <- structure(list(x=c(18.6,13.13,14.3,5.72,9.1,11.7,7.15 ,4.55, 1.17),
                     y =c( 26.4, 15.28,12.27,10.34,7.7,7.24,6.83,3.47,0.99),
                     class = "data.frame")
)

The Hill function of order 2 (i.e. $y=ax^2/(1+(bx)^2)$) has a sigmoidal shape but goes through $y=0$ when $x=0$. In this parameterization, $a$ is the initial slope and $b$ is the reciprocal of the half-maximum point. Eyeballing the graph for reasonable starting points (an essential step in non-linear fitting), we get $a \approx 0.5$, $b \approx 1/15$.
Fit with nls():
fit1 <- nls(y~a*x^2/(1+(b*x)^2),
             start=list(a=0.5,b=1/15), data=dd)

In order to do this without eyeballing every time you either have to (1) trust that the scales are reasonably similar for all of your different data sets or (2) set up a self-starting function for the nls fit.
For this parameterization, the initial slope is $a$, the half-maximum is $1/b$, and the asymptote is $a/b^2$:
cc_nls <- as.list(coef(fit1))
res <- with(cc_nls,c(init_slope=a,half_max=1/b,asymptote=a/b^2))
##   init_slope     half_max    asymptote
##   0.08813282  39.18785991 135.34455071

Generate predicted values:
xmax <- 100; ymax <- 150
pframe <- data.frame(x=seq(0,xmax,length=51))
pframe$y <- predict(fit1, newdata=pframe)

Construct confidence intervals by the delta method (could also use bootstrapping):
vv <- suppressWarnings(emdbook::deltavar(a*pframe$x^2/(1+(b*pframe$x)^2),
                        meanval=coef(fit1),
                        Sigma=vcov(fit1)))
pframe$lwr <- pframe$y-2*sqrt(vv)
pframe$upr <- pframe$y+2*sqrt(vv)

Find points where the tangent to the curve is parallel to the linear regression slope (I initially tried to do this analytically, but it gets very ugly ...)
cc_lm <- coef(lm(y~x, dd))["x"] ## slope of linear model
dfun <- deriv(expression(a*x^2/(1+(b*x)^2)),"x",
              function.arg=c("x","a","b"))
dfun2 <- function(x) with(cc_nls,
                       attr(dfun(x,a,b),"gradient")-cc_lm)
u1 <- uniroot(dfun2,c(0,1/cc_nls$b))$root
u2 <- uniroot(dfun2,c(1/cc_nls$b,xmax))$root

Plot everything:
png("hillfun.png")
par(las=1,bty="l")
plot(dd,xlim=c(0,xmax),ylim=c(0,ymax),pch=16,
     xlab="x",ylab="y")
with(pframe,polygon(x=c(x,rev(x)),y=c(lwr,rev(upr)),
                    col=adjustcolor("black",alpha=0.3)))
abline(lm(y~x, data=dd), lty=2)
abline(v=c(u1,u2),lty=3,col="blue")
abline(h=res["asymptote"],lty=3,col="purple")
with(pframe,lines(x,y,col="red"))
dev.off()

Comment: What will be the best way to find the saturation point and critical mass on the curve that you have suggested?
Comment: can you clarify the definition of "saturation point" and "critical mass"?  Do you mean the asymptotic $x$ value (i.e. $\lim_{x\to\infty}(y)$) and inflection point respectively? (Do these have anything to do with the vertical segments on your graph?)
Comment: Yes, they are the vertical lines on the graph in question. The points where tangent of S- curve is parallel to the linear fit generated.
Comment: Also, while experimenting with the dd <- structure(list(x=c(18.6,13.13,14.3,5.72,9.1,11.7,7.15 ,4.55, 1.17), (y =c( 26.4,
15.28,12.27,10.34,7.7,7.24,6.83,3.47,0.99)),  class = "data.frame", row.names = c(NA, -8L))  The curve goes till infinity and not bending at the top to form a S. @BenBolker
Comment: the coefficients are (a=0.088, b= 0.025), so the half-maximum is at x=1/b=39 and the asymptote is at y=a/b^2 = 135
Comment: Is there any way to make the starting point(a and b) dynamic according to data instead of changing it every time with the change in data? @BenBolker


Define path using string value with pathlib

Question: I am trying to design a function that iterates over a script. The arguments of this function are first_name and second_name. Among other things, this loop should create folders and subfolders  as follows:
def script(first_name='', second_name=''):
  (...)
  first_name='first_name'
  second_name='second_name'
  project_path = pathlib.Path.home()/'Desktop/Project_folder'
  name_path = pathlib.Path.home()/'Desktop/Project_folder/'+first_name+second_name
  subfolder = pathlib.Path.home()/'Desktop/Project_folder/'+first_name+second_name+'/subfolder'
  (...)

However, when I try to run the script, I get the following error when creating the folders:
script(first_name, second_name)
(...)
>>> TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'

Since I am not very experienced with the pathlib module, I was wondering whether there was a way to fix this and create folders using string values inside pathlib without specifying the full path in advance.
Answer: Paths are specified using forward slashes:
pathlib.Path.home()/'Desktop/Project_folder' / first_name / second_name / 'subfolder'

Example:
>>> import pathlib
>>> first_name, second_name = "Force", "Bru"
>>> pathlib.Path.home()/'Desktop/Project_folder' / first_name / second_name / 'subfolder'
PosixPath('/.../Desktop/Project_folder/Force/Bru/subfolder')
>>>

Comment: What if I need the folder name to contain both `first_name` and `second_name`, i.e. in your example `PosixPath('/.../Desktop/Project_folder/Force_Bru/subfolder')`
Comment: @BorisDonnelly, use parentheses: `pathlib.Path.home() / "whatever" / ("Force" + "_" + "Bru")` or f-strings: `the_path / f"myfolder_{name}_{surname}" / "other_folder"`
Comment: Should there be space before and after each slashes (`/`)?  If yes why there is not any after `home()`
Comment: @alper, the slashes are actually division operators. Do you need spaces in `1/2`? Python doesn't care - it's just a matter of legibility and code style. Spaces in _strings_ matter very much, though: `"hello/" != "hello /"`.
Comment: I was considering for : `pathlib.Path.home() / "Desktop" / "Project_folder"` vs `pathlib.Path.home()/"Desktop"/"Project_folder"`
Comment: @alper, there's absolutely no difference between these because spaces between operators don't matter


Google map not displaying in magento

Question: <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

have added this is the head.phtml
and

placed this in my phtml
it seems to be putting lots of values in the div, so i know the script is working but nothing is being displayed, could anyone point out the mistake and guide me to a correct direction???
Answer: Don't put the call to initialize in document.ready(), use Google's own event listener instead:
google.maps.event.addDomListener(window, 'load', initialize);

Comment: where do i place it in the head.phtml?
Comment: well that didn't help, most of the divs coming due to the script are still hidden remains hidden
Answer: Hello check following code may be help you

< script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
< script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&sensor=true">

< script type="text/javascript">
function initialize() {
var latitude = parseFloat("22.258652"); // Latitude get from above
  variable
var longitude = parseFloat("71.1923805"); // Longitude from same
var latlngPos = new google.maps.LatLng(latitude, longitude);
 var mapOptions = {
  center: latlngPos,
  zoom: 10,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
 var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);

var marker = new google.maps.Marker({ position: latlngPos, map: map,
  title: "test" });
}
< script>
Answer: I had the same issue and added a surrounding div with a fixed size as per this answer, worked for me.
Map isn't showing on Google Maps JavaScript API v3 when nested in a div tag

Google map not displaying in magento


efficient way to find cumulative count for the elements of a sublist in a list

Question: To find the cumulative count for a list of elements in another list , I can loop through the list for each element of the sublist (via list.count) and sum them up. Is it possible to the same with a more efficient Pythonic way?
Example:
test_list = [1,1,2,2,2,3,3,3,4,5,5,5]
to_be_counted = [1,2]
result = 5 (1 two times, 2 three times)

Comment: E.g. `len([n for n in test_list if n in to_be_counted])`.
Comment: why do you want to use `list.count` internal function? Time complexity of the O(n) according to this post [https://stackoverflow.com/a/44813154/5222075].
So over all time complexity O(n*k), where n is number of elements in test_list, k is number of elements in to_be_counted.
Answer: try map and sum:
test_list = [1,1,2,2,2,3,3,3,4,5,5,5]
to_be_counted = [1, 2]
print(sum(map(test_list.count, to_be_counted)))


number of features in feature selection for text mining problems

Question: Let's say for a text mining problem (e.g creating a predictive model using text analysis), using a feature selection method (e.g TF-IDF) we come up with 1000 features/words/tokens.
Is there some principals that suggest what number of features we should use? What I currently do is creating the model based on all 1000 features, then reducing the number of features until reaching the highest accuracy when doing train and test.
Is it the correct way to deal with number of features or is it somehow fishing a model which might result in a non-robust model?
Answer: It is usual to use all sorts of feature derivation you think are probably useful for your problem, then apply feature selection, feature reduction, and similar concepts to make best use of those features in your models. Not using a helpful feature derivation in the first place causes this information to be missing for your model, which will likely cause worse-than-possible results. So your feature selection and reduction approaches should account for not-helpful and redundant information (this is frequently coupled with training/evaluating models to avoid overfitting/selection bias).
Concerning robustness of the resulting model: this should rely on using a thorough evaluation approach and choosing a reasonable simple model from all evaluated models - instead of limiting feature derivation in the first place. Using repeated CV (training and evaluating different model types and parametrizations, then selecting best model based on resample performances) together with data partitioning appropriate for your setup (completely held-back test partition for estimating performance of final model, poss. done population independent like leave-subject-out-CV) will be OK for most setups.


Vyatta and DNS Rewrite (aka hairpin or doctoring)

Question: I'm trying to make my public IP reachable also from inside LAN.
I know that it's better to spilt DNS in order to have an internal zone that solves hosts with internal IP, but for a lot of reasons this is not applicable to my environment.
I have a "simple" configuration, a server and few NAT ports:
set nat destination rule 4002 description 'NAT inbound'
set nat destination rule 4002 destination address 'x.y.z.k'
set nat destination rule 4002 destination port '80,443,10050,10051,11051'
set nat destination rule 4002 inbound-interface 'bond1'
set nat destination rule 4002 protocol 'tcp'
set nat destination rule 4002 translation address '10.0.0.190'

set nat source rule 4002 description 'NAT outbound'
set nat source rule 4002 outbound-interface 'bond1'
set nat source rule 4002 source address '10.0.0.190'
set nat source rule 4002 translation address 'x.y.z.k'

When I try to access the public IP from internal network I cannot reach it. It's a problem similar to How to implement Nat loopback/reflection?
I've found a solution here: http://onebadpixel.com/blog/2014/01/22/part-5-nat-translation/
And so I've tried to add this to configuration:
set nat source rule 1000 description 'NAT hairpin'
set nat source rule 1000 destination address '10.0.0.128/24'
set nat source rule 1000 outbound-interface 'bond0'
set nat source rule 1000 source address '10.0.0.128/24'
set nat source rule 1000 translation address 'masquerade'

But it still don't work.
Any suggestion on how to change my configuration in order to use DNS hairpin?
I'm not expert in Vyatta, I came from Cisco ASA world where it was easy... just add "dns" on the NAT rule ;)
Thanks
Fabio
Comment: I did this a while back so I might not be remembering things correctly but don't you also need the appropriate firewall rules on the interface
Answer: You can view an example here  (answer from SteveP):
http://forum.vyos.net/showthread.php?tid=6554
(VyOs is an open source fork of Vyatta and this should be applicable)
Note the the hairpin is done through a nat destination rule and not a nat source.
NAT destination change the destination IP address (which is what you need in this case) and is performed prior to the routing decision while NAT source rewrite the source IP address is processed after the routing decision.
Also in your configuration "10.0.0.128/24" is curious. You should use either 10.0.0.0/24 for the network or 10.0.0.128 for the host.
Comment: Thanks,
about the network is correct 10.0.0.0/24, 10.0.0.128/24 was an error due to various tests putting the single IP and/or the network with mask.

I've checked the message on the vyatta forum and the configuration seems similar to mine. I will ask there, maybe someone have a solution.
Comment: did you test with a nat destination rule?


Linear Diophantine equation in two variables with additional constraints

Question: Given,

$$aX + bY = c$$

where,

$$c > b > a > 0;\quad X, Y > 0;\quad b\nmid c, a\nmid c$$

I want to find out if a solution exists as efficiently as possible (I'm not interested in the solutions). Are there any calculations I can make before (or without the need for) finding $\operatorname{gcd}(a, b)$ that can possibly save some time (even if for only few special cases)? $c, b, a$  can be very large numbers.
"Probably not" still counts as answer for me. You don't have to be 100% certain. I just want to make sure I'm not missing something that's very obvious.
P.S.,
English is not my first language.
Answer: I would say probably not. Finding $\gcd(a,b)$ is computationally fast (with technology). If you get $\gcd(a,b)\nmid c$, then there are no solutions. See this for more general theory and examples.
Comment: Thanks (Sorry for not upvoting you, I'm a guest).
Answer: If gcd(a,b) divides c, then there are infinitely many solutions for the equation
aX + bY = c
Divide the equation by gcd(a,b) to get the reduced equation
sX + tY = u
where gcd(s,t)=1
With the extended algorithm for the gcd-calculation, you can calculate a solution (m,n).
All solutions are given by
X = kt + m
Y = -ks + n
for some integer k.
If there is an integer k, such that X and Y satisfy all conditions, a solution is
found. In order to check this, you have to find bounds for the number k by solving
the desired inequalities and check, if the intervals found have a non-empty
intersection.
So, this is the way to find solutions.
Now, I come to your additional restrictions. Since 3x + 4y = 5 is an
equation not having integer solutions with x>0 and y>0, it is not enough
to check, whether gcd(a,b) divides c, even if the additional restrictions hold.
But the way I described is very efficient even for very large numbers, since it only requires modulo-calculations.
I doubt that there are faster ways to decide if there are solutions of the desired form.
Comment: Thank you. The updated version is more clear.


okhttp connection tries and connect time

Question: I have a small trouble with okhttp.

How to set a connect time (for ex. 10 sec and this 10 sec okhttp lib should try to get a response from server)?
How to set a connect tries (for ex. when this 10 sec is ended)?
Answer: <code>        client = new OkHttpClient();
        client.setConnectTimeout(10, TimeUnit.SECONDS);
        client.setReadTimeout(10, TimeUnit.SECONDS);
        client.setWriteTimeout(10, TimeUnit.SECONDS);
</code>
Comment: Please explain your solution.
Comment: you can set the connect time (timeout) using the code above.

To retry request please create a custom interceptor

http://stackoverflow.com/questions/24562716/how-to-retry-http-requests-with-okhttp-retrofit


Atiyah-Macdonald Chapter 3 Qn 19 viii

Question: I am having a problem with a question in Atiyah-Macdonald Chapter 3 qn 19(viii). The question is as follows:
If $f:A\rightarrow B$ is a ring homomorphism, and $M$ is finitely generated $A$-module, then $\mbox{Supp}(B\otimes_{A}M)=f^{*-1}(\mbox{Supp}(M))$
I tried to show $\mbox{Supp}(B\otimes_{A}M)\subseteq f^{*-1}(\mbox{Supp}(M))$. Suppose ${\frak{p}}\notin f^{*-1}(\mbox{Supp}(M))$, then $f^{-1}(\mathfrak{p})\notin \mbox{Supp}(M)$. Thus for each generator $m_{i}$ of $M$, there exists $s_{i}\in A-f^{-1}(\mathfrak{p})$ such that $s_{i}m_{i}=0$. If $f(s_{i})=0$ then since $f(s_{i})\in B-\mathfrak{p}$, thus $0\in B-\mathfrak{p}$ and we are done. Otherwise for all $f(s_{i})$ if they are not zero, we have $f(s_{1})f(s_{2})\ldots f(s_{k})(b\otimes m)=b\otimes (s_{1}s_{2}\ldots s_{k})m=0$
But I am stuck with $f^{*-1}(\mbox{Supp}(M))\subseteq \mbox{Supp}(B\otimes_{A}M)$. The problem is if I start letting $\mathfrak{p}\in LHS$, so each of $m_{i}$ is not zero, but I can't show that $1\otimes m_{i}$ is not zero. Or if I start from $\mathfrak{p}\notin RHS$, I can't show that $(A-f^{-1}\mathfrak{p})M=0$.
How should I approach? Thanks!
Comment: @navigetor23: Thanks! Noted.
Answer: Since no one answered you (because it's Sunday?), let me try it.
Let $q\in f^{*-1}(\mbox{Supp}(M))$. Then $p=f^{-1}(q)\in\mbox{Supp}(M)$, that is, $M_p\neq 0$. I want to prove that $q\in\mbox{Supp}(B\otimes_{A}M)$. Since  $(B\otimes_{A}M)_q\cong B_q\otimes_{A_p}M_p$ (why?) we can reduce the problem as follows: let $f:(A,m)\to(B,n)$ be a homomorphism of local rings with $m=f^{-1}(n)$ and $M\neq 0$ a finitely generated $A$-module. If $B\otimes_AM=0$, then tensor this by $B/n$ and get that $B/n\otimes_AM=0$. But $B/n\otimes_AM\cong B/n\otimes_{A/m}M/mM$, so we obtained a field extension $A/m=K\subset L=B/n$ and $V=M/mM$ a nonzero (why?) $K$-vectorspace such that $L\otimes_KV=0$, a contradiction. Therefore $B\otimes_AM\neq 0$.
Comment: One question: to answer why it leads to a contradiction, am I right in saying that because

$M/mM\cong K\otimes_{K}M/mM\cong \phi(K)\otimes_{K}M/mM\subseteq L\otimes_{K}V$

where $\phi$ is the field homomorphism from $K$ to $L$. so since if we assume that $L\otimes_{K} M/mM$ is zero, implies that so is $M/mM$ and hence $M=0$ by Nakayama's lemma.

Other than that, I think they are correct! How do you manage to come up with this proof (especially on your first try!). What is a reduction technique in commutative algebra?
Comment: Well, it seems that you are not used with the reduction techniques in commutative algebra. Here $A$ denotes $A_p$ and $m$ is $pA_p$ (similar for $B$). If you have an extension of rings $R\subset S$ and $P$ a prime of $S$ whose contraction to $R$ is $p$, then $R/p\subset S/P$. $M/mM=0$ leads to $M=0$ via Nakayama. (Btw, the new $M$ is $M_p$.)
Comment: What are (A,m) and (B,n)? How does $B/n\otimes_{A}M\cong B/n\otimes_{A/m}M/mM$ leads to the field extension? Why is $M/mM$ a non-zero K-vsp, and what does it contradict?

Thanks!
Comment: sorry if i ask too many questions but why $B/n$ is a field extension of $A/m$? (and I probably have other questions on some identities but i will try to figure them out first). Anyway, what is the reduction technique in commutative algebra?
Comment: Because they are fields and there is a ring homomorphism from one to another (which is necessarily injective, right?).
Comment: @enoughsaid05 I am very late to the party, but I wanted to add some comments for future reference: First, $M/mM$ is a $K$-module, which is flat. You may either apply the absolute flatness condition of a ring on $K$, or argue that vector spaces are free, and that free modules are flat (the latter goes beyond the scope of A&MD, imo.) Therefore, tensoring $0 \to K \to L$ with $M/mM$ results in $0 \to M/mM \to 0$, so $M=mM$. Also, to show that the 'new $M$' $M_p$ is finitely generated, we may use the exactness properties of localization on $A^n \to M \to 0$. I think this reduces sloppiness.

Atiyah-Macdonald Chapter 3 Qn 19 viii


Why am i getting " java.lang.NullPointerException" in my android app?

Question: I want to show "hello" message on the screen but my app is not working.
What is causing "java.lang.NullPointerException"?
the debugger says value of longitude is null. Even if that is the case, shouldn't my "hello" message still appear?
I have the following code:
 package autogenie.maptrial;

 import android.Manifest;

 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.location.Location;
 import android.location.LocationManager;
 import android.support.v4.app.ActivityCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Location location;
private Double latitude, longitude;
private LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    longitude=location.getLongitude();
    latitude=location.getLatitude();

    Context context = getApplicationContext();
    String s="hello";
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context,s, duration);
    toast.show();

}
}

What debugger shows:

After that it crashes.
Comment: plz check if (location != null) an then proceed.
Comment: Please add the stacktrace of the error (the stuff you see in the debugger)
Comment: added the stacktrace
Answer: Replace
longitude=location.getLongitude();
latitude=location.getLatitude();

with
if( location != null ) {
    longitude=location.getLongitude();
    latitude=location.getLatitude();
}

It is not longitude field that is null, but the location field, on which you are trying to invoke a method call. This produces the exception. When you replace the code as noted above, you will get the program to work without a crash.
Answer: Please check if GPS_Provider is enabled. It looks like GPS is not enabled and you try to use that. location will be null in that case
Answer: add below check -
if(location != null){
    longitude=location.getLongitude();
    latitude=location.getLatitude();
}

Answer: Set a condition on location instance , if it is not null , only then proceed.
You should use FusedLocationProviderApi for getting the current location as per android documentation. Read more here : Getting the Last Known Location


Find probability that last drawn ball is white.

Question: Urn contains $w$ white and $b$ black balls. We draw balls with the following procedure:

We draw ball and put it aside.
We draw another ball. If its color is different than last drawn ball then we put it back to urn and go to state 1. Otherwise we put this ball aside and go to state 2.
Comment: Have you tried setting up the equations to calculate $P(w, b)$ recursively?
Comment: You can prevent text from being italicized in math mode by enclosing it in `\text{...}`.
Comment: @CalvinLin thanks for hint. I edited the question I put my current attempt can you let me know if I'm on the right path? Joriki thanks for letting me know!
Comment: Interestingly, it seems that the probability is 1/2 for any initial distribution with at least one ball of each color. This might be solvable using induction.
Answer: As was pointed out in a comment the probability in question is $\frac12$ if at least one ball of each color is present.
First of all we prove the following combinatorial lemma:
Let $m$ and $n$ be integer numbers such that $n\ge0$, $m>0$. Then:
$$
\sum_{k=0}^n \frac{n!}{k!}\frac{(m+k)!}{(m+n)!}\frac{m}{m+k}=1.\tag1
$$
Obviously the lemma holds for $n=0$ and any $m>0$. We demonstrate now that if the lemma is valid for $n$ it holds for $n+1$ as well:
$$\begin{align}
\sum_{k=0}^{n+1} \frac{(n+1)!}{k!}\frac{(m+k)!}{(m+n+1)!}\frac{m}{m+k}
&=\frac{m}{m+n+1}+\frac{n+1}{m+n+1}\sum_{k=0}^n\frac{n!}{k!}\frac{(m+k)!}{(m+n)!}\frac{m}{m+k}\\
&\stackrel{I.H}=\frac{m}{m+n+1}+\frac{n+1}{m+n+1}=1.
\end{align}
$$
Let $P(w,b)$ be the probability of the last drawn ball being white if we start from the state 1 with $w$ white and $b$ black balls.
Observe that to return to state 1 (with a different number of balls) we need to draw first $k$ balls of the same color and then a ball of the other color. In the case of a series of black balls the probability of such event is:
$$
\frac{b}{w+b}\frac{b-1}{w+b-1}\cdots\frac{b-k+1}{w+b-k+1}\frac{w}{w+b-k}=
\frac{b!}{(b-k)!}\frac{(w+b-k)!}{(w+b)!}\frac{w}{w+b-k}
$$
and similar expression for the series of white balls. Thus, we have
for $w,b>0$:
$$\begin{align}
P(w,b)
&=\sum_{k=1}^b\frac{b!}{(b-k)!}\frac{(w+b-k)!}{(w+b)!}\frac{w}{w+b-k}P(w,b-k)\\
&+\sum_{k=1}^{w}\frac{w!}{(w-k)!}\frac{(w+b-k)!}{(w+b)!}\frac{b}{w+b-k}P(w-k,b),
\end{align}
$$
with $P(w,0)=1$, $P(0,b)=0$.
We claim: $$\forall w,b>0: P(w,b)=\frac12.\tag2$$
For $P(1,1)$ the claim is obviously valid. Assume it is valid for all $P(w,b-k)$ with $0<k<b$ and $P(w-k,b)$ with $0<k<w$.
We have:
$$\begin{align}
P(w,b)
&\stackrel{I.H.}=\frac12\sum_{k=1}^b\frac{b!}{(b-k)!}\frac{(w+b-k)!}{(w+b)!}\frac{w}{w+b-k}
+\frac{b!}2\frac{w!}{(w+b)!}\\
&+\frac12\sum_{k=1}^{w}\frac{w!}{(w-k)!}\frac{(w+b-k)!}{(w+b)!}\frac{b}{w+b-k}
-\frac{w!}2\frac{b!}{(w+b)!}\\
&\stackrel{(1)}=\frac12\left(1-\frac{b}{w+b}\right)+\frac12\left(1-\frac{w}{w+b}\right)=\frac12.
\end{align}
$$
Thus, the claim (2) is proved.
Answer: Let denote by:
$a_n$ - from urn we thrown $n$ balls and last drawn ball was white.
$b_n$ - from urn we thrown $n$ balls and last drawn ball was black.
$A$ - white ball was drawn
$B$ - black ball was drawn
then
$$P(a_n) = P(A|a_{n-1})P(a_{n-1})+P(AA|b_{n-1})P(b_{n-1})+P(AB|a_{n-1})P(a_{n-1})$$
$$P(a_n) = P(a_n)P(a_{n-1})+P(a_n)P(a_{n-1})(1-P(a_{n-1}))+P(a_n)(1-P(a_n))P(a_{n-1})$$
$$P(a_n)(1-P(a_{n-1}))(2P(a_{n-1})-1) = 0$$
this give a solution
$$P(a_n) = 0 \text{ for } w=0$$
$$P(a_n) = 1 \text{ for } b=0$$
$$P(a_n) = \frac{1}{2} \text{ otherwise }$$
Comment: Could you explain $P(A|a_{n−1})=P(a_n)$ and the other two similar equalities? I would assume that the conditional probability depends on the exact nature of the $a_{n−1}$ state, i. e. on the number of remaining white and black balls.


Create JavaScript array of a negative number series which decreases in increments of -100 based on a single integer

Question: I need to create a JavaScript array of values decreasing in increments of -100%. The number of increments will be dictated by a variable.
Example 1.
var items = 3;
var position = [ "0", "-100%", "-200%" ];

Example 2.
var items = 5;
var position = [ "0", "-100%", "-200%", "-300%", "-400%" ];

I've managed to create the list of numbers:
var end = (items - 1) * -100;
for (var j = 0; j >= end; j-=100)
    position[position.length] = j;

But I'm struggling to add the % unit and I'm worried the -= approach is bad practice.
Comment: You can just append the symbol to the end of the value: `... = j + '%';`. Also, a negative step is not bad practice.
Comment: Thank you @RoryMcCrossan, whilst searching for a solution I remember reading a bad practice comment about negative steps, I guess it was just one opinion or my misinterpretation, thank you for clarifying and the tip.
Answer: You could take a template literal and map the values.

const getArray = length  => Array.from({ length }, (_, i) => i ? `-${100 * i}%` : '0');

console.log(getArray(3));


Is there a way to force a type in Typescript?

Question: I have this:
      const res: any = await table.find({}).toArray();
      const data: Type[] = res;

Is there a way to do something like this:
      const res: Type[] = await table.find({}).toArray();

without the error : Type 'Document[]' is not assignable to type 'Type[]'. ?
Comment: I think you should use [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to do that.
Answer: Check out Type Guards and Differentiating Types.
If you are sure that res always will be array of Type, you can force type using as:
const res = await table.find({}).toArray() as Type[]; // res: Type[] now


Sorting multiple-line records in alphabetical order using shell

Question: There is a file which has the following structure:
L. feritas
E. ferity

L. ars
E. art

L. solitudo
E. solitude

L. celeritas
E. celerity

Each line is a field, and a record spans multiple lines, comprising an L. field, an E. field, and a blank line. How can I sort the records by the fields that are tagged L., keeping the multiple-line records intact?
L. ars
E. art

L. celeritas
E. celerity

L. feritas
E. ferity

L. solitudo
E. solitude

Answer: The quick way of coming up with something is:
$ cat file | awk 'BEGIN{RS=""; FS="\n"; OFS="|"}{$1=$1}1' \
  | sort | awk 'BEGIN{FS="|";OFS="\n";ORS="\n\n"}{$1=$1}1'

Or you can write it in a single Gnu AWK,
$ awk 'BEGIN{RS=""; ORS="\n\n"; FS=OFS="\n"; PROCINFO["sorted_in"]="@val_str_asc"}
       {a[NR]=$0}END{for(i in a) print a[i]}' file

If you don't want the last line to be empty, you can do the following:
$ cat file | awk 'BEGIN{RS=""; FS="\n"; OFS="|"}{$1=$1}1' \
  | sort | awk 'BEGIN{FS="|";OFS="\n"}{$1=$1}1' | sed '$d'

$ awk 'BEGIN{RS=""; FS=OFS="\n"; PROCINFO["sorted_in"]="@val_str_asc"}
       {a[NR]=$0}END{for(i in a) print a[i] (--NR?"\n":"")}' file

Comment: The second one would remove duplicates if there were any. You could still use gawk sorting with something like `a[NR] = $0` and `END{asort(a); ...}`.
Comment: @BenjaminW. I have added an updated version. Thanks for pointing this out.
Comment: It works perfectly except one thing: the last line of the file is blank.
Comment: Thanks! What about the first solution?
Comment: @J.Doe if this answered your question, have a look at [Someone answers](https://stackoverflow.com/help/someone-answers)


managing page navigation at swift2

Question: I'm trying to learn page navigation without using "Navigation Controller".
Created a button and function like;
@IBAction func backPressed(sender: AnyObject) {
    self.performSegueWithIdentifier("page2ToMain", sender: self)
}

It works fine.
I need to pass data from page 2 to page 3. So, inside "didSelectRowAtIndexPath" I used "performSegueWithIdentifier" and override "prepareForSegue" function as below;
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.selectedRow = self.dataArray[indexPath.row]

    self.performSegueWithIdentifier("page2ToPage3", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let nextViewController = segue.destinationViewController as! ViewController3
    nextViewController.data = self.selectedRow
    print(self.selectedRow)

}

When I click the table cell it works well, jumps to other view controller and I see data variable.

But when I press back button it fails with;
 Could not cast value of type 'test3.ViewControllerMain' (0x1045469f0) to 'test3.ViewController3' (0x1045466e0).

How can I solve the problem?
Answer: You can test for this with optional binding:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let nextViewController = segue.destinationViewController as? ViewController3 {
        nextViewController.data = self.selectedRow
        print(self.selectedRow)
    }
}

Or look at the segue identifier and then test for that:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "page2ToPage3" {
        let nextViewController = segue.destinationViewController as! ViewController3
        nextViewController.data = self.selectedRow
        print(self.selectedRow)
    }
}

managing page navigation at swift2


Strip version number from certain dependencies in Gradle custom plugin

Question: We have a custom plugin where we would like to build a special tar file used for deployment. We would like to strip the version number on certain dependencies.
The code for the plugin:
void addTarTask(Project project) {
    Tar tar = project.tasks.create("tar", Tar)
    tar.classifier = "bin"
    project.afterEvaluate {
        tar.into("${project.name}-${project.version}"){
            into('lib'){
                from project.tasks.jar
                from project.configurations.runtime
            }

            //snip more custom requirements
        }
    }
}

I have tried something where we use a regex to remove the version from the file - like this:
into('lib'){
    from project.tasks.jar
    from project.configurations.runtime {
        rename '(.*)-[0-9]+\\..*.jar', '$1.jar' // not sure regex is okay - it is just to demo
    }
}

Is it not possible to get a type dependency/artifact object so i can skip the regex and make the code more clean. Something like (pseducode):
if(${artifact.extension} == "so") {
    return ${artifact.name}.${artifact.extension}
} else {
    // just return normal file
}

Any help is appreciated :-)
Answer: The runtime Configuration you are referencing has an dependencies property that is a DependencySet holding Dependency objects.
That means you can iterate trough the dependencies to to get their name.
You can use that to write specific rename rules. There's also some caveats to consider.
I generated a sample project to exemplify with:
gradle init --type java-library

Then edited build.gradle to:
apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'
}

task log << {
    configurations.runtime.allDependencies.each { println it }
    println ""
    configurations.testCompile.allDependencies.each { println it }
}

task copy(type: Copy) {
    from configurations.runtime
    into  "$buildDir/lib"
    configurations.runtime.allDependencies.each {
        rename "-${it.version}", ""
    }
}

This has the following result:
[:~/tmp] % ./gradlew log copy
:log
DefaultExternalModuleDependency{group='org.slf4j', name='slf4j-api', version='1.7.21', configuration='default'}

DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}
DefaultExternalModuleDependency{group='org.slf4j', name='slf4j-api', version='1.7.21', configuration='default'}
[...]

[:~/tmp] % ./gradlew dependencies
[...]
testCompile - Dependencies for source set 'test'.
+--- org.slf4j:slf4j-api:1.7.21
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

[...]

[:~/tmp] % ls build/lib
slf4j-api.jar

Note that the hamcrest dependency is shown by gradle dependencies but it's not printed. This is because the dependencies are not resolved at configuration time, so gradle does not yet know about the transitive dependencies. In other words you can implement something like the copy task above in your plugin but it will only work for the dependencies you specify directly in your build script. Of course you could specify the transitive dependencies explicitly too if you have to rename some of those and it will still work. The testCompile configuration correctly prints both slf4j and junit because the extendsFrom rules are followed.
Comment: I ended up doing it in a different way. But thank you very much for the answer. Learned a lot by this.
Answer: This code snippet will also copy transitive dependencies and is already gradle 7 ready:
task copyDependencies(type: Copy) {
  from configurations.runtimeClasspath
  into "$buildDir/lib"
  configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.each {
      rename "${it.artifact.name}-${it.artifactId.componentIdentifier.version}", "${it.artifact.name}"
  }
}


Jesu Joy of Man's Desiring: What term is appropriate for the instrumental ornaments in Bachs Choral arrangements?

Question: a related question asks if there is a specific term for the voicing in Bach's Chorals:
What is the term for the sung voice found especially in Bach's chorale music (eg. Jesu Joy of Man's Desire BWV147)?
I would have said this is the Choral.
But  what I am wondering is how we name the instrumental accompaniment of the solo violin in Jesu Joy or the Bassoon Solo in BWV 140 Zion hört die Wächter singen?
I think we could call it: Ornament, Garland or Ritornell.
Other ideas?
(As I'm planning an analysis and introduction "how to learn and teach"
 this arrangement/setting for piano).
Comment: Just to nitpick... the instrumental accompaniment isn't limited to solo violin, it's full four-part strings. 1st violin is the triplets that everyone knows; 2nd violin is in a dotted swing rhythm; violas and basso continuo are mostly in quarters, as seen in this manuscript: https://www.youtube.com/watch?v=M3DMuLRsr9A. Note when the chorus comes in, their parts are written in between the violas and the continuo (as was typical). Also note the poly-meter: 1st violin part is notated in 9/8, others are in 3/4.
Comment: @Caleb:Yes, it is, of course.But  I'm wondering how you would name these sections and how you'd call the kind of ornaments of the Violin tune.
Answer: In the cantata form as used by JS Bach, these "chorale fantasias" often have an elaborate orchestral introduction. The opening of BWV 244 (the St Matthew Passion) has often been described as such, and both the opening movement and the final movement of BWV 147 Herz und Mund und Tat und Leben fit this pattern.
However, the structure of the chorale fantasia conforms fairly well to a sort of ritornello form, and hence as the opening theme of the movement does return and punctuate the chorale setting itself, to call this theme a ritornello is appropriate.


Compute Ricci curvature for constant curvature Riemannian manifold

Question: If we have a Riemannian manifold $(M,g)$ with constant sectional curvature $C$ I have proven that the $(1,3)$ curvature tensor
$$R(X,Y)Z = C(\langle Y,Z\rangle X - \langle X,Z\rangle Y)$$
and that the $(0,4)$ Riemannian curvature tensor is given by
$$\operatorname{Rm}(X,Y,Z,W) = C(\langle X,W\rangle\langle Y,Z\rangle - \langle X,Z\rangle \langle Y,W\rangle)$$
now I would like to prove that the Ricci curvature, defined by $\operatorname{Ric}(X,Y) = \sum_{i=1}^n \operatorname{Rm}(X,e_i,e_i,Y)$ is given by $C(n-1)g(X,Y)$. I'm sure this should be a somewhat simple exercise given that we know what the Riemannian curvature is in this situation, but I cannot get any further than:
$$C\sum_{i=1}^n (g(X,Y)g(e_i,e_i) - g(X,e_i)g(Y,e_i))$$
Any help is appreciated!
Comment: Note that proper notation is $\langle X,W\rangle$, not $$. And if you google "latex symbols", you can find things like this. $\qquad$
Comment: @MichaelHardy : $\langle\cdot, \cdot\rangle $ is definitely more good looking, but $<\cdot, \cdot>$ is also used a lot even in research paper.
Comment: @JohnMa : That form is for occasions when you're limited to characters on the keyboard. It's not used in published journals whose typesetting software is not so limited, and it's not used in competently written LaTeX documents. Where LaTeX or MathJax is available, it's an incorrect usage.
Comment: @JohnMa : Notice also the lack of proper spacing: $3 - \langle a,b\rangle$ versus $3 - < a,b> \qquad$
Answer: Recall that $\{e_i\}$ is an orthonormal basis so $g(e_i, e_i) = 1$.
Writing $X = \sum_{j=1}^nX^je_j$ and $\sum_{k=1}^nY^ke_k$ we see that
$$g(X, e_i)g(Y, e_i) = g\left(\sum_{j=1}^nX^je_j, e_i\right)g\left(\sum_{k=1}^nY^ke_k, e_i\right) = \sum_{j,k=1}^nX^jY^kg(e_j, e_i)g(e_k, e_i) = X^iY^i.$$
Now note that
$$g(X, Y) = g\left(\sum_{j=1}^nX^je_j, \sum_{k=1}^nY^ke_k\right) = \sum_{j,k=1}^n X^jY^kg(e_j, e_k) = \sum_{j=1}^nX^jY^j = \sum_{i=1}^nX^iY^i.$$
Therefore,
\begin{align*}
C\sum_{i=1}^n(g(X, Y)g(e_i, e_i) - g(X, e_i)g(e_i, Y)) &= C\sum_{i=1}^n(g(X, Y) - X^iY^i)\\
&= C\sum_{i=1}^ng(X, Y) - C\sum_{i=1}^nX^iY^i\\
&= Cng(X, Y) - Cg(X, Y)\\
&= C(n-1)g(X, Y).
\end{align*}


How to find dropdown selected value inside repeater in dropdown selected index change event?

Question:  <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>

                    <%#subtypes.FindByPk(Convert.ToInt32(Eval("SubitemID"))).title%>
                    <%#Eval("quantity")%>
                    <%#ThanaRecord.FindByPk(Convert.ToInt32(Eval("Thanaid"))).title%>
                    <%#Eval("created_at")%>
                     <% if (Employee.GetCurrentEmployee().role == "Admin") { %>
                        <a href="AddDemand.aspx?type=update&id=<%#Eval("id")%>">EDIT

                            <a href="AddDemand.aspx?type=delete&id=<%#Eval("id")%>">DELETE
                        <% } %>
                    <%if (Employee.GetCurrentEmployee().role == "SuperVisor")
                       { %>
                      <asp:DropDownList ID="DropDownList1" runat="server" Width="120px"  AutoPostBack="true"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="form-control">
                 <asp:ListItem Text="Status" Value="0">Status</asp:ListItem>
                          <asp:ListItem Text="Accept" Value="1">Accept</asp:ListItem>
                <asp:ListItem Text="Reject" Value="2">Reject</asp:ListItem>

            </asp:DropDownList>

                        <%--<asp:textbox runat="server" id="textTest"></asp:textbox>--%>

                    <%} %>

            </ItemTemplate>
        </asp:Repeater>

Code:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    //DropDownList DropDownList1 = (DropDownList)sender;
    //string SelectedValue = DropDownList1.SelectedValue;
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DropDownList ddldrop = (DropDownList)e.Item.FindControl("DropDownList1");
        int  value =Convert.ToInt32( ddldrop.SelectedValue);
        Supervisor sup = new Supervisor();
    if (value ==1 ) {
            sup.Status = "Accept";
            sup.Save();
        }
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Supervisor sup = new Supervisor();
    //sup.Status = "Accept";
    //sup.Save();
}

Comment: Have already gave you the solution in your previous post. Pls refer that and give it a shot  http://stackoverflow.com/questions/28217680/i-want-to-call-a-function-immediately-as-the-user-select-any-item-from-dropdown
Answer: You just have to cast the sender argument in the event-handler to DropDownList. It is  always the source of the event. So no need to use FindControl:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   DropDownList ddl = (DropDownList) sender;
   string selectedValue = ddl.SelectedValue;
   // ...
}


mvc bootstrap tooltip closer to text

Question: I am doing an MVC Application with Bootstrap and I have a tooltip like this inside a

  NAME

What it shows is this

What I need is to show the tooltip closer to the bottom.
How can I do it?
Thanks
Answer: Tooltips appear right next to element
Make sure that element does not have paddings which are moving the tooltip away from text (maybe another span inside the span but without the padding)?
Or if there is another css rule moving the tooltip away, try to override it using some transformation?
Example:
    transform: translateY(10px);

Good luck!
https://getbootstrap.com/docs/5.3/components/tooltips/#overview
Comment: Thanks I checked and there is no padding... also I put transform: translateY(10px); and nothing happened..

mvc bootstrap tooltip closer to text


Python script for automatic background changing in Ubuntu

Question: Could you write(or hint at) a Python script that changes the Ubuntu desktop background automatically from a website(like Flickr or deviantArt)? How can I start the script at startup?
Answer: Not python but a direction: http://www.webupd8.org/2009/09/real-time-earth-wallpaper-for-linux.html


template to add a h2 tag around a field group title

Question: I have a field group in my content type. I want to wrap an h2 tag around the title of the field group. I have tried looking at the node.tpl.php and field.tpl.php for a way to wrap just the title, and have not succeeded.
Where can I target this?
Comment: Field groups are just used to group content when editing nodes. I've never seen a field group title being output when viewing a node. What page do you want this h2 tag to appear on?
Answer: The way I dealt with this was to change the wrapper in the Manage Display section from a fieldset to a div. Then, options for the label element could be changed from inside the UI.


How to trigger a Rules action based on which content pane of a panel is used?

Question: I have a page with a path like www.example.com/newsroom. On this page nodes are displayed with help of 2 different views:

View 1 : contains voted nodes by user.
View 2 : contains not voted nodes by user.

The 2 views are content panes and www.example.com/newsroom is a panel page. So there is no difference in the path of both views, and the user can see both views together.
I am using the Rules module to perform some action when vote is cast on any of node. And using Rules one can check the path from where a user performs an action on a node, simply by using the token [site:current-page:url]. But  in my case that is www.example.com/newsroom (no matter where the vote is related to).
Is there any way that I can differentiate if a node on that page is voted from via View 1 or View 2? As [site:current-page:url] returns path www.example.com/newsroom ... Based on which view it is, I want Rules to perform different actions. So how to check in Rules if the node being voted on is from View 1 or View 2?
I do have different classes of both views, but I'm not sure how to check those with Rules.
Any suggestions?
Comment: I've tried to further enhance (condense) you question, mostly by removing duplicate stuff in it. Please review/double check it (again), to make sure I didn't make any mistakes or cleaned it up too much. Where needed edit your question again. I think you should also add the tag "7" to it. PS: I doubt it'll get any more close votes now ... Hopefully some extra upvotes ... I find it a very interesting question (= challenge)!
Answer: Have a look at the Rules panes module. Some details about it (from its project page):

Rules Panes allow embedding Rules components as content panes in Panels (or so).
By default any parameters for the component are entered manually by the end user, but they can also be pre-populated with CTools contexts on the page (which will also hide the form input).

So if you can find a way to transform your "... rules to perform some action ..." in a Rules component, you should be good to go.
Some pointers to some more docu/demos about it (also mentioned on its project page):

Videos introducing this module.
Presentation from DrupalCamp Stockholm 2013.
Video about Rules panes.
Comment: Thank you :) for your all help and understanding my question and pointing towards right direction  !!
Comment: Thank you for your feedback (and "accept ...). I'd be interested in getting some more details about how my "pointer" helped you to get it to work. Maybe you want to ADD your own answer also, something like "using the hint about 'Rues panes',  this is how I got it to work, and this is how my exported Rule looks like". I bet it would be upvoted! If you want to do so: hurry (before your question eventually gets put on hold). PS: I voted against closing your question. And with my edits of your question, I hope the current close counter (= 3) will not increase anymore ...
Comment: Thanks for spending more time and efforts and clearing up things. Frankly speaking I haven't accomplished it  yet but I am going thorough videos etc and it seems it can solve problem. I did accept as for efforts but if this is confusing other users and close votes are coming. Please guide me what to do with this situation. Do I un-accept it until I am not solve this completely. But as far I can tell you changed title this is what exactly I am trying.  Please let me know what I am supposed to do now with answer
Comment: Thank you again for your efforts and time. I am working and hopefully will try to provide updates as soon I will be done hopefully. But I think for current situation at least you understood question and pointed towards using module and trying solution.
Comment: OK for me. If some day you add another "answer" to this question here, then please let me know about it via another comment to my answer here (so that I get notified about it). For now: au revoir!


How to prove $(\hat{X}-\mu)/(\hat{S}/\sqrt{n})$ is student t with $n-1$ degrees of freedom if $X_i$ are iid $N(\mu, \sigma)$?

Question: It is commonly stated that if $X_i$ are iid $N(\mu, \sigma)$, then with $\hat{X}$ the sample mean, and $\hat{S}$ the sample error (sample standard deviation), then $\frac{ \hat{X}-\mu}{\hat{S}/\sqrt{n}}$ follows a t distribution with $n-1$ degrees of freedom.
e.g., wikipedia link states it, and another Wikipedia link states it as the natural way a t distribution arises, but nothing proves this fact.
How do we know/prove this?
The definition of a t distributed variable is $\frac{U}{\sqrt{W/(n-1)}}$ with $U$~$N(0,1)$, $W$~$\chi^2(n-1)$.
So we can write   $$\frac{ \hat{X}-\mu}{\hat{S}/\sqrt{n}} = \frac{  \frac{\hat{X}-\mu}{\sigma/\sqrt{n}}}{ \frac{\hat{S}}{\sigma}} = \frac{U}{D}$$.
$U$ is $N(0,1)$ b/c $X_i$ are, meaning $\hat{X}\sim N(\mu, \sigma/\sqrt n)$.
We need to show that the bottom,  $D = \sqrt{W/(n-1)}$ with  $W = \frac{(n-1)S^2}{\sigma^2}  \sim \chi^2(n-1)$. Looking at other post here, it seems the proof is not quite complete, but it gives a good start. Basically, the proof is:
(1.) define auxiliary variable $V=\sum_{i=1}^n(\frac{X_i-\mu}{\sigma})^2$, clearly $\chi^2(n)$.
(2.) Algebra shows that $V= W + Y$, where $W  = \frac{(n-1)\hat{S}^2}{\sigma^2 }$ (quantity of interest) and $ Y = (\frac{n(\bar{X}-\mu)}{\sigma})^2$ which is $\chi^2(1)$.
(3.) Conclude that this implies $W$ is $\chi^2(n-1)$.
So I can follow (1.) and (2.). I do not think (3.) follows directly from (2.) without justification. (e.g., if $W$ and $Y$ were independent, I think the relationship above shows $W$ is $\chi^2(n+1)$!
Overall, how do we prove (3.)?  OR Please provide another proof of this whole thing. I just want to understand this rigorously.
Comment: This is Theorem 5.3.1c, Page 218 of Casella Berger https://mybiostats.files.wordpress.com/2015/03/casella-berger.pdf
Comment: Everything but the independence of numerator and denominator is proven at https://stats.stackexchange.com/questions/151854 and https://stats.stackexchange.com/questions/52906/ (using a different technique).  The independence is stated in many of our threads and proven in several: https://stats.stackexchange.com/questions/462747 is one of them.
Answer: For  $\zeta\sim \mathcal N(0,1),~ \chi^2\sim\chi^2_{n},$ both being independent to each other,
\begin{align}\textrm{Fisher's t} := ~ \frac{\zeta}{\sqrt{\frac{\chi^2}n}}\sim t_n.\tag I\end{align}
Now take $\zeta:= \frac{\bar x-\mu}{\sigma/\sqrt n}\sim \mathcal N(0, 1), \chi^2 :=\frac{\sum(x_i-\bar x) ^2}{\sigma^2}=\frac{ns^2}{\sigma^2}\sim \chi^2_{n-1},$ both being independent (to be proved)
\begin{align}
\text{Student's t}&:= \frac{\sqrt n (\bar x-\mu)}{\sigma}\times \frac\sigma{\sqrt{\sum(x_i-\bar x) ^2/(n-1)}}\\&= \frac{\bar x-\mu}{S/\sqrt n} \sim t_{n-1}.\tag{II}
\end{align}

Fisher's lemma: If $X_i\mapsto Y_i$ via an orthogonal linear transformation, where $X_i, ~i\in\{1, 2,\ldots, n\}$ are independent $\mathcal N(0, \sigma^2), $ then $Y_i\sim \mathcal N(0, \sigma^2)$ and independent.
The proof is easy. Let $\mathbf A$ be the matrix corresponding to the transformation i.e. $\bf Y = AX; $ since $\bf A$ is orthogonal, $\bf Y^\mathsf TY = X^\mathsf TX$ and $\bf A^\mathsf TA= I. $ Now one can evaluate the joint density of $Y_i$ using the relation stated; the jacobian would yield $1$ and the rest would follow.

Now, take $\bf A$ such that
$$a_{11} = a_{12} =\ldots= a_{1n}= \frac1{\sqrt n}; $$ this would imply $$y_1 = \sqrt n \bar x. $$
Now, as the transformation is orthogonal,
\begin{align}
\sum y_i^2 &= \sum x_i^2\\ &= \sum (x_i-\bar x) ^2 +\underbrace{ n\bar x^2}_{=y_1^2}\\\implies \sum_{i= 2}^n y_i^2 &= \sum_{i=1}^n (x_i -\bar x) ^2;
\end{align}
moreover
$$\sum_{i=1}^n (x_i -\mu) ^2= \sum_{i= 2}^n y_i^2 + n(\bar x - \mu)^2.$$
Therefore, following Fisher's lemma and the results found,
\begin{align}\mathrm dG(y_1, y_2, \ldots,,y_n) &=
\frac1{\sigma^n(2\pi)^{n/2} }\exp\left [-\frac1{2\sigma^2}\left\{  \sum_{i= 2}^n y_i^2 + n(\bar x - \mu)^2 \right\}\right]|\mathcal J|\prod \mathrm dy_i\\ &= \left[\frac{1}{\sqrt{2\pi}(\sigma/\sqrt n) }\exp\left\{-\frac{ n}{2\sigma^2}(\bar x -\mu)^2\right\}~\mathrm d\bar x\right]\times \left[\frac{1}{\left(\sigma{\sqrt {2\pi}}\right)^{n-1} }\exp\left\{-\sum_{i=2}^n\frac{ y_i^2}{2\sigma^2}\right\}~\prod \mathrm dy_i\right];
\end{align}
proving the independence of $\bar X$ and $\sum_{i=2}^n Y_i^2 = ns^2.$
Now, since $Y_i \sim \mathcal N(0, \sigma^2), ~i\in\{2, 3,\ldots, n\}, $
$$ \sum_{i=2}^n \frac{Y_i^2}{\sigma^2} = \sum_{i=1}^n \left(\frac{X_i -\bar X}{\sigma}\right)^2\sim \chi^2_{n-1}.$$

Reference:
Fundamentals of Mathematical Statistics, S. C. Gupta, V. K. Kapoor, Sultan Chand & Sons, 2014.
Comment: So if you reduce to the case \mu = 0, \sigma =1 initially (WLOG), then the proof required is completed as soon as you derive \sum_{i>1} Y_i^2 = sum_{i=1}^n (X_i - \barX)! very slick!. As for the independence (which doesn't seem to be needed but we get for free from Fisher's Lemma): I think it is easy to show that if random variables Y_1, ..., Y_n are all mutually independent, then for any i, j, Y_i is independent of Y_j^2 and Y_1 independent of \sum_{i>1} Y_i. This means Y_1^2 is independent of sum_{i>1} Y_i^2.


Proofs that the zero vector is not linearly independent

Question: Re-checking my linear algebra, using Lax textbook Linear algebra and its applications.
I want to know if my attempt of proving the following theorem it's ok (it has been a while since I attended this lecture).

Show that if the vectors $x_1, x_2,\dots,x_j$ are linearly independent, then
  none of them is the zero vector.

As far as I understand, this is a proof by contradiction. I have to exhibit a
nontrivial linear combination that fulfills
$$
k_1x_1+k_2x_2+\dots+k_jx_j=0,
$$
where $k_j\in K$ are elements in the field $K$.
Without loss of generality, suppose that $x_1$ is the zero vector. Then
$$
k_1x_1+\dots+k_jx_j=0,
$$
where all the $k_i$ must be zero. We can sum on both sides the term $\tilde{k}x_1$ (where $\tilde{k}\in K$), and by Eq. (10): $0x_1=0$. Therefore
$$
(\tilde{k}+k_1)x_1+\dots+k_jx_j=\tilde{k}x_1+\dots+k_jx_j=0,
$$
and I have showed that there is a non-trivial linear combination with the zero vector, which is a contradiction.
Comment: I don't really understand this - what is $\overline{k}$? This is much more complicated than it needs to be (we can explicitly write down a nontrivial linear combination equaling zero ...).
Answer: Unfortunately, as it stands, this is not a good proof.

You don't every introduce your assumption. You are assuming that the set is linearly independent, and that is why you can conclude something about $k_i$.
You don't say what $\overline{k}$ is; you need $\overline{k}$ to be non-zero, or else you've concluded nothing.
The proof is pretty circuitous and comes to the correct conclusion in a very roundabout way.

The key idea is just your last line: exhibit a combination that has a non-zero coefficient. Taking $k_1 = 1$ and $k_i = 0$ for $i > 1$ is good enough. And this gives a proof by contrapositive, which is quite a bit cleaner than the contradiction proof you were going for.
Comment: The set is linearly independent. That is given. I bought you the $\tilde{k}$, but to say it's not defined is a little bit pedantic. The proof is long because this is what usually happens when one is learning math, first long proofs, then hone the results and after synthesize
Comment: @user2820579 You never introduced $\overline{k}$! Is it a real number, is it zero, is it something else? This is an important thing - you need to introduce your variables and say what they are before using them; after all, as I pointed out, if $\overline{k} = 0$ you cannot draw the conclusion that you want. It is also very important, especially for students who are just learning how to prove things for the first time, to *clearly* include their assumptions and techniques; the reader shouldn't come to the end of the proof and be surprised it's a contradiction.
Comment: If someone would explain the reason for the downvote, I would really appreciate it so I can improve the answer. OP asked for critique of their proof and I gave it (pinging @user2820579 if you have any other comments...).
Answer:
As far as I understand, this is a proof by contradiction. I have to
  exhibit a nontrivial linear combination that fulfills
$$ k_1x_1+k_2x_2+\dots+k_jx_j=0, $$
where $k_j\in K$ are elements in the field $K$.

So far so good.

Without loss of generality, suppose that $x_1$ is the zero vector.
  Then
$$ k_1x_1+\dots+k_jx_j=0, $$
  where all the $k_i$ must be zero.

No, no, stop the bus here.  You just correctly stated your goal: you have to exhibit the $k_i$.  That is:  you have to come up with explicit values to assign to them.  In this case set $k_1 = 1$, $k_2 = k_3 = \cdots = k_j = 0$ and you are done.  Then the contradiction is that this is a nontrivial linear combination, contradicting the assumed linear independence.
This is actually more cleanly done by the proving the contrapositive: "If one of $x_i$ is $0$ then $\{x_1, \ldots, x_j\}$ is not a linearly independent set."  Then you can give a direct proof of that statement rather than having to come up with a contradiction.  The contrapositive is always equivalent to the original statement.
Comment: (+1); this is a nice clean explanation that shows why the setup for the contradiction wasn't correct in the posting.
Comment: @user296602 Thanks.  I've found that in beginning proofs like these, at least 50% of the errors are due to basic confusion about logic, like the difference in how to prove $\forall$ statements vs. $\exists$ statements etc.  Once they have the basic proof-outline in place it's easy to fill in the "actual math" bits.
Answer: Let $V$ be a vector space over $K$. I guess it suffices to prove that if $v$ is the zero vector, then $\lambda v = 0_{V}$ for all $\lambda \in K$. Then, for $v_1, ..., v_n \in V$, if $v_i$ is the zero vector:
$$\lambda_1 v_1 + ... + \lambda_i v_i + ... + \lambda_n v_n = 0_V $$
is a non-trivial combination for $ \lambda_1 = ... = \lambda_{i-1} = \lambda_{i+1} = ... = \lambda_n = 0_K $ and any non-zero $\lambda_i \in K$.
To see that $\lambda 0_v = 0_V$ for all $\lambda$, see that $\lambda 0_V = \lambda (0_V + 0_V) = \lambda 0_V + \lambda 0_V$. Applying the inverse of $\lambda 0_V$ in both sides of the equation - which exists by the axioms of vector spaces - we get $\lambda 0_V = 0_V$.


A footnote about outer measure

Question:
This is the theorem about in Royden's real analysis book. And in the book there is a footnote I am confusing:

Can anyone help me understanding it with examples~~~
Comment: @MichaelHardy I think we agree. But I still felt like making the remark since especially in the given context I think one will want $\sup  \emptyset $ equal $0$ while in others it would be $-\infty$.
Comment: I don't agree that $\inf\varnothing=+\infty$ is merely a convention.  It is a provable result if one does things carefully.  One place where is useful is this: suppose one defined distance from $a$ to $b$ to be the length of the shortest path from $a$ to $b$; then the distance is $+\infty$ if there is no path from $a$ to $b$, because the minimum of the empty set is $+\infty$. $\qquad$
Comment: @MichaelHardy it depends on the "ambient" set. It is not always the case that it is $+ \infty $. I agree though one could say that since we chose for the "ambient" set  $[0,+ \infty]$ it is this.
Comment: @quid : Generally $\inf\varnothing$ or $\min\varnothing$ should be the biggest of all things, but yes, it depend on what sort of "things" there are. $\qquad$
Comment: $\min\emptyset$ is undefined by definition (and by convention) otherwise you must sacrifice $\min X\in X$.
Answer: The outer measure of $E$ is defined of as the infimum of the following set
$$\left \{\sum_{k=1}^{\infty} \mu(E_k) \colon \{E_k\}_{k=1}^\infty \text{with $E_k\in S$ such that  $E \subset \bigcup_{k=1}^\infty E_k$ } \right \}$$
Now nothing guarantees that for some set $E$ there is even one $\{E_k\}_{k=1}^\infty$ with $E_k\in S$ such that  $E \subset \bigcup_{k=1}^\infty E_k$. In this case the set above is empty, and it the outer measure of such an $E$ is then the infimum of the empty set. It is thus necessary to know what the infimum of the empty set would be, and this is what is stated in the footnote.
Comment: Normally $\cup$ is used in things like $A\cup B$ and $A_1\cup\cdots\cup A_n$, and $\bigcup$ in things like $\displaystyle\bigcup_{k=1}^\infty E_k$. I edited accordingly. $\qquad$
Comment: That the infimum of the empty set is the largest member of the ordered set is not merely a convention, but a theorem.
Comment: I changed the formulation avoiding the use of the word convention. I hope this settles this issue.
Comment: one more question : can $E_k$ be $\varnothing$?
Answer: There is only one example: $\inf\emptyset=+\infty$. I can explain you the reason. The definition of $\inf$, i.e., greatest lower bound, which is equivalent to $\sup\{x\in\mathbb R:x<y\:\forall y\in\emptyset\}$.  Think about it: this means
$\inf\emptyset=\sup\mathbb R$ and thus $+\infty$.
Answer: Let $S := \{ \{x\} \mid x \in \mathbb{R}\}$. Then $\mathbb{R}$ cannot be covered by countably many elements of $S$.

A footnote about outer measure


Cordova build fails if I set 'type: modules' in package.json. How do I resolve this, yet still carry out unit testing?

Question: I've built a simple app for Android, using the Cordova framework.  I have now now implemented a simple unit test (using Tape), which runs in Node.  Since the files I'm testing use ES6 modules, in order to run the tests in Node, I had to add "type": "module", to my package.json file.  But when I did that, my app would no longer build--the Cordova build command fails with the output below.  What is the proper way to resolve this issue?

Is it possible to programmatically set "type": "module",?  If so, I can perhaps set it only when unit tests are running.
Or is some part of my method flawed in this case? That is certainly a possibility.  Maybe if I test in the browser (rather than in Node), this issue will be avoided?

Thanks.

Build Output
$ cordova build android
Unable to load PlatformApi from platform. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\snarl\my-project\platforms\android\cordova\Api.js
require() of ES modules is not supported.
require() of C:\Users\snarl\my-project\platforms\android\cordova\Api.js from C:\Users\snarl\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\util.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename Api.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\snarl\my-project\package.json.

Unhandled error. ('The platform "android" does not appear to be a valid cordova platform. It is missing API.js. android not supported.')

Answer: In case anyone else gets stuck on this, here is the solution I used (more of a workaround I guess).  I bundled all files needed for unit testing into one single JS file--bundle-test.js--using Browserify (with a Babelify transform).  Then I ran that file from the Node command line:  node test-bundle.js.  My unit tests ran without issue, and I didn't have to set "type": "module" in package.json.


Do ear trained pianists think in absolute or relative pitches?

Question: I wonder if ear trained pianists think of melody in terms of intervals (relative) or notes (absolute).
Meaning, when you're on a given note. To go to the next note, do you just know what piano key needs to be hit based on what the interval sounds like, or when you hit that next note you know what that note is supposed to sound like?
Comment: I assume you are referring to piano playing since you mention piano keys. For instrumentalists we play melodies using muscle memory think about making it interesting and adding expression. We don't usually think about individual notes or intervals. (This only applies to piano and other instruments though)
Comment: should be relative; there is a reason you are always given a starting pitch
Comment: Maybe it would be useful to define a musical context. (For example, sight reading certainly wouldn't be the correct context since that utilizes neither perfect not relative pitch.) It also might be useful to solicit answers from people who possess *both* perfect pitch and good relative pitch.
Answer: When I play by ear, I think in relative pitches. After playing from a score for a while, I also do this. My idea is to be able to transpose more easily. I was taught how to do this when I was about 5.
I don't know how other people do it. I do know one person with absolute pitch. She learns the piece first in absolute pitches then moves over to relative pitch afterwards. I would guess that most pianists learn however (sometimes through muscle feel) then think of the relative pitches later.
Answer: Ear-trained pianists who have taken music history lessons think in relative pitches (intervals). At least in the Royal Conservatory of Music history lessons I took, I learned about sonata-allegro and sonata-rondo form. Both forms involve sections and their transposed versions in the same piece, and in order for listeners to recognize the transposed versions, they need to think in terms of intervals.
Some video game soundtracks also encourage listeners to think in terms of intervals--for example, Mario Kart: Double Dash raises the keys of its racetrack themes by one semitone during the final lap, and UnderTale's soundtrack contains leitmotifs for several characters and then transposes those leitmotifs in different situations where those characters appear. In both cases, the transposed versions are expected to be recognizably similar to their originals.
Comment: May I ask why you clarify "who have taken music history lessons"?
Comment: I expand on that a bit further down the first paragraph. I emphasize that sonata-allegro and sonata-rondo form encourage thinking in relative pitch pretty quick because they transpose their key melodies around. I also note that I learned those forms in music history class. I suspect that music harmony class will not encourage thinking in relative pitch anywhere near as much (unless recognizing major/minor/dominant 7th/augmented/etc. chords is heavily emphasized).
Comment: Perhaps it's a continental difference; in my experiences in America, those concepts are more likely to be taught in a theory class, and only brushed on in a history class. Interesting!
Answer: I was trained 5 year of violin when I was young. I use relative. Transpose is so easy for me. But when the key signature changes in a passage in a song, I have to pay extra attention to avoid landing in the wrong pitch. My son's piano teacher uses Absolute. He said 99% children start from relative, then trained to use Absolute. My son uses Absolute from day1. After  3 years learning piano, he now has been taught what is "interval" for his first exam. The poor kid has to look into the ceiling and counts the distance between two absolute notes. while for me, identify the interval is a millisecond thing. However, when mimic a sound from nature, like fart, leaf-trembling, door slam, bird chips, my son always land in the right pitch in his first try. So I think which come first relative or absolute, is totally a born-with preference.


The Bi-linear Functional Equation $\sum_{i=1}^{n}f_i(x)g_i(y)=0$

Question:
Consider the following so-called bi-linear functional equation
  $$\sum_{i=1}^{n}f_i(x)g_i(y)=f_1(x)g_1(y)+f_2(x)g_2(y)+\cdot\cdot\cdot+f_n(x)g_n(y)=0 \tag{1}$$
  where $f_i(x)$ and $g_i(y)$ are arbitrary functions.
  What conditions are required for such a functional equation to hold?

For example, for $n=2$ we have
$$\begin{align}
f_1(x)g_1(y)+f_2(x)g_2(y) &= 0 \\
\frac{f_1(x)}{f_2(x)}+\frac{g_2(y)}{g_1(y)} &= 0
\end{align} \tag{2}$$
where I assumed that $f_2(x) \ne 0$ and $g_1(y) \ne 0$. Consequently, we can conclude that Eq.$(2)$ will hold if and only if
$$\begin{align}
f_1(x) &= +\lambda f_2(x) \\
g_2(y) &= -\lambda g_1(y)
\end{align} \tag{3}$$
where $\lambda$ is some constant. Considering other cases, I ended up with
$$\boxed{
\begin{array}{ll}
1.\, \text{if} \, f_2(x)=0 \, \text{and} \, g_1(y)=0 \, \text{then} & 0=0 \\
2.\, \text{if} \, f_2(x)\ne0 \, \text{and} \, g_1(y)=0 \, \text{then} & g_2(y)=0 \\
3.\, \text{if} \, f_2(x)=0 \, \text{and} \, g_1(y)\ne0 \, \text{then} & f_1(x)=0 \\
4.\, \text{if} \, f_2(x)\ne0 \, \text{and} \, g_1(y)\ne0 \, \text{then} & f_1(x) = +\lambda f_2(x) \, \text{and} \, g_2(y) = -\lambda g_1(y)
\end{array}
}\tag{4}$$
How can I generalize the result in $(4)$ for functional equation $(1)$?
Any help is appreciated.
Comment: $F(x)=(f_1(x),f_2(x),\cdots)$ and $G(y)=(g_1(y),g_2(y),\cdots)$ are two orthogonal vectors in an $n$-dimensional Euclidean space.
Comment: @GeorgSaliba: Does that help? :)
Comment: Well, for $2$D space, the product of the slopes of two perpendicular lines is equal to $-1$ which is equivalent to $(3)$. Reading Robert's answer I realize that my comment is similar, but in much simpler, less detailed terms.
Answer: I'll suppose $f_i$ and $g_i$ are defined on sets $D_f$ and $D_g$ respectively, taking values in a field $\mathbb F$ (you're probably thinking of $\mathbb F = \mathbb R$ or $\mathbb C$).
Suppose the linear span of $(f_1, \ldots, f_n)$ (as functions of $x \in D_f$) has dimension $r$.  Thus the vectors $(c_1, \ldots, c_n)$ such that
$c_1 f_1 + \ldots + c_n f_n = 0$ form a subspace $V$ of $\mathbb F^n$ of dimension $n-r$. Then (1) is equivalent to: $(g_1(y), \ldots, g_n(y)) \in V$ for all $y \in D_g$.
For example, in the case $n=2$ your solution corresponds to the case $r=1$, where $V$ is spanned by $(1, -\lambda)$.
Comment: That's not at all what I said.  Let me try to clarify.  Take any $f_1, \ldots, f_n$.  What is the condition on $(g_1, \ldots, g_n)$ to make your equation (1) hold?   Let $r$ be the dimension of the span of  $f_1, \ldots, f_n$.   There is a  subspace $V$ of $\mathbb F^n$ of dimension $n-r$ consisting of those $(c_1, \ldots, c_n)$ such that $c_1 f_1 + \ldots + c_n f_n$.  Then (1) says $g_1(y) f_1 + \ldots + g_n(y) f_n = 0$ for all $y \in D_g$, i.e. that $(g_1(y), \ldots, g_n(y))$ is a member of $V$ for all $y \in D_g$.
Comment: It should have dimension at most $n-r$.
Comment: Rank-nullity theorem.  If $W$ is the linear span of $f_1, \ldots, f_n$, the linear operator $T: c \mapsto c_1 f_1 + \ldots + c_n f_n$  from $\mathbb F^n$ to $W$ is surjective and thus has rank $r$.  Its null space $V$ has dimension $n - r$.
Comment: Sorry Robert but I encountered some questions! :) Here is what I got from your answer. We introduce two spaces $V=\text{Span}\{f_1,f_2,..,f_n\}$ and $W=\text{Span}\{g_1,g_2,..,g_n\}$. Afterwards, we claim that if $\text{Dim}(V)=r$ and $\sum_{i=1}^{n}f_i(x)g_i(y)=0$ then $\text{Dim}(W)=n-r$. Can you explain that why the claim is valid? Sorry if this is a rudimentary question! I am confused! :)
Comment: Ah! Thanks. I just mixed things in my head! :) What can we say about the dimension of $W=\text{Span}\{g_1,g_2,..,g_n\}$? :)
Comment: Hi, Robert. :) I again encountered this question in another context. While reading your answer again, I did not understand that why the dimension of the vector space containing the vectors $(c_1, \ldots, c_n)$ such that
$c_1 f_1 + \ldots + c_n f_n = 0$ is $n-r$? I know that the sum of dimensions of two orthogonal complement subspaces of some vector space is equal to the dimension of vector space