How do I constrain the output of linear regression (e.g. predicting review scores 1-10)

Question: So my training set labels are bounded by 1-10, but if I fit a regression curve it will predict outside this range as well, including negative numbers. Is there a good way to constrain the model outputs besides simply changing all < 1 values to 1 and > 10 values to 10?
I am coding in Python with sklearn.
Comment: Why are they 1-10. What is the dependent variable specifically? Is it measurement of current through a rheostat in ohms that can be any value infinitely from 1-10 or is it a rating of pain from 1-10? What?
Answer: I think the easiest way to achieve this is to transform your data by dividing every value by 10.  So, instead of ranging from 1-10, it will range from 10% to 100%.
After this transformation, you could use a Logit Regression model.  In Python, you would use the following function: sklearn.linear_model.Logistic Regression.
Comment: I just want to make sure, can binary classification take in a probability as a label rather than just 0 or 1?
Comment: @Vic.  When I read explanation on how to do that stuff in Python, Logit Regression can handle multi class.  Quoting an explanation I saw on line: "In the multiclass case, the training algorithm uses the one-vs-rest (OvR) scheme if the 'multi_class' option is set to 'ovr', and uses the cross- entropy loss if the 'multi_class' option is set to 'multinomial'. (Currently the 'multinomial' option is supported only by the 'lbfgs', 'sag' and 'newton-cg' solvers.)"
Comment: Do you know anything about Ordinal Regression? Would that be more appropriate for this?
Comment: @Vic, there is such a model as an ordinal Logit Regression.  It depends on the true nature of your independent variable.  If that variable does represent an ordinal number, than ordinal Logit Regression would be appropriate.
Answer: It really depends on what you need this analysis for. Quite often a linear regression is still used for this kind of data, with any prediction higher than the maximum interpreted as maximum value and any prediction lower than the minimum as minimum value. This approach also has high external validity, if your data at the end of the scale refers to categories labelled with "or more". E.g. if your data is from a questionnaire on willingness to spend and the final category refers to something like "100$ or more".
A multinomial regression (or multiclass) may also work, but in that case you loose the ordering constraint on your data (assuming the scores imply some kind of order). If your numbers just refer to arbitrary categories without any kind of ordering, then this approach works best.
Any kind of approach that actually limits the data will also suffer from bad prediction at the end of the scale. There are multiple approaches you may try:

Ordinal regression. This approach, however, will only give you probabilities that the answer is below or above a certain range. E.g. in this example the category "Y>=1" is alway Inf, meaning that the value is always larger than 1 (of course it must be, since this is the actual lowest value).
Multiple logistic regression with varying cut points. Ordinal regression assumes that the change from 1 to 2 is driven by the same variables with the same strength as the change from 9 to 10, which may not be the case. To relax this assumption, you can run 9 different logistic regression. the first predicts the probability that the value is higher than 1 (split your data between 1 and other numbers), the second for being higher than 2 etc. This will, however, suffer from the same drawback as ordinal regression, i.e. low values cannot be predicted well.
You may also restructure your data, such that you may be able to use a logistic regression. Simply replicate your dataset 9 times. For answers of 1 in the original dataset make all nine responses 0, for answers of 2 make 1 response a 1 and the other 8 a 0, etc with an answer of 10 being coded as 9 answers of 1. Then you can run a logistic regression on this restructured dataset. The resulting probabilities from the logistic regression can also be re-translated to answers in the range 1-10. In this case you loose any possibility to interpret standard errors of the coefficient or p-values, but you will be able to predict the actual score (not just the probability of the score being higher/lower than k). Also, this approach suffers from bad prediction at the end of the scale, since neither 1 nor 10 is a number that ever lies in the prediction range (because they are just the limits of the transfer function).

Best is to try all these approaches and do some cross-validation to see which one works best on your data.


Counting the number of inversions for the function $f(x+yp)=qx+y$

Question: Let $p,q$, $(p\neq q)$ be odd primes. Define the function $f:\{0,1,\ldots,pq-1\}\to \{0,1,\ldots,pq-1\}$ by $f(x+yp)=qx+y$, where $x\in \{0,1,\ldots,p-1\}$ and $y\in\{0,1,\ldots,q-1\}$. How does one calculate the number of inversions for $f$?
A complete solution would be quite helpful.
Comment: Note that it does not matter whether or not $p$ and $q$ are prime or distinct. A very similar (most likely equivalent, but I am too tired) problem has been posed as Exercise 3 in [UMN Fall 2017 Math 4990 homework set #8](http://www.cip.ifi.lmu.de/~grinberg/t/17f/hw8os.pdf) (the link goes to a PDF that sketches a solution and contains a link to another writeup with a solution).
Comment: "Inversions" are defined for a permutation of an ordered set; you need to define a total order on $\mathbb{Z}_{pq}$. I assume you really want the function $f : \left\{0,1,\ldots,pq-1\right\} \to \left\{0,1,\ldots,pq-1\right\}$ (not $f : \mathbb{Z}_{pq} \to \mathbb{Z}_{pq}$) that sends each $x + yp$ to $qx + y$ for $x \in \left\{0,1,\ldots,p-1\right\}$ and $y \in \left\{0,1,\ldots,q-1\right\}$.
Comment: @darijgrinberg Yes. Appreciate the comments. Thank you :)
Answer: We will consider the case $p=3, q=5$ which is easy to draw.  Hopefully you agree the ideas generalize.  Consider these matrices:
$$
A = \begin{bmatrix}
0 & 1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 & 9 \\
10 & 11 & 12 & 13 & 14
\end{bmatrix},
\ \ \ \ \ \ \ \ \ \ \ \ \
B = \begin{bmatrix}
0 & 3 & 6 & 9 & 12 \\
1 & 4 & 7 & 10 & 13 \\
2 & 5 & 8 & 11 & 14
\end{bmatrix}
$$
Let the rows be numbered $x = 0, 1, 2$ from top to bottom and the columns be numbered $y = 0, 1, 2, 3, 4$ from left to right.  Then $A_{xy} = qx+y$ and $B_{xy} = x+py$.
So here's a recipe for evaluating $f(i)$ where $i \in \mathbb{Z}_{pq}$:

First, find location $(x,y)$ where $B_{xy}= i$
Then, look up the same location in matrix $A$ and we have $f(i) = A_{xy}$.

If I may abuse notation a bit, the chain of mapping that just happened was something like this:
$$i = x+py = B_{xy} \rightarrow (x,y) \rightarrow A_{xy} = qx+y = f(i) = f(x+py)$$
which has an overall effect of $x + py \rightarrow qx+y$ as desired.
OK, so how does this help?  Consider $(i,j) \in \mathbb{Z}_{pq}^2$.  It is an inversion if $i < j$ and $f(i) > f(j)$.  Let their locations be $(x_i, y_i), (x_j, y_j)$, i.e. $i = x_i + p y_i = B_{x_i y_i}$ and $f(i) = q x_i + y_i = A_{x_i y_i}$, and similarly for $j$.

From matrix $B$, it is obvious that $i = B_{x_i y_i} < j = B_{x_j y_j}$ iff $y_j > y_i$ ($j$'s column is to the right of $i$'s column), or, $y_j = y_i$ and $x_j > x_i$ ($j$ is in the same column and below $i$).
From matrix $A$, similarly, $f(i) = A_{x_i y_i} > f(j) = A_{x_j y_j}$ iff $x_j < x_i$ ($j$'s row is above $i$'s row), or, $x_j = x_i$ and $y_j < y_i$ ($j$ is in same row and to the left of $i$).

Since we need both conditions above to be true, this means $(i,j)$ is an inversion iff $y_j > y_i$ and $x_j < x_i$, i.e. $(x_j, y_j)$ must be strictly to the right and above $(x_i, y_i)$.
E.g. in the following colored matrices, the blue cell represents $i=4, f(i) = f(1 + 1 \cdot 3) = 5 \cdot 1 + 1 = 6$.  The red area in the $B$ matrix represents $j> i$ and the red area in the $A$ matrix represents $f(j) < f(i)$.  Clearly the only overlap (for this choice of $i$) are the $3$ cells corresponding to $x=0, y=2,3,4$.  These are the values of $j$ which form inversions with this choice of $i$.
$$
A = \begin{bmatrix}
\color{red}{0} & \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\
\color{red}{5} & \color{blue}{6} & 7 & 8 & 9 \\
10 & 11 & 12 & 13 & 14
\end{bmatrix},
\ \ \ \ \ \ \ \ \ \ \ \ \
B = \begin{bmatrix}
0 & 3 & \color{red}{6} & \color{red}{9} & \color{red}{12} \\
1 & \color{blue}{4} & \color{red}{7} & \color{red}{10} & \color{red}{13} \\
2 & \color{red}{5} & \color{red}{8} & \color{red}{11} & \color{red}{14}
\end{bmatrix}
$$
E.g. $j=6 > i=4$ and $f(j) = f(0 + 2\cdot 3) = 5\cdot 0 + 2 = 2 < f(i) = 6$.
So each $(i,j)$ pair that is an inversion is a pair of cells which form the top-right and bottom-left corners of a rectangle, where the rectangle has $c>1$ columns and $r>1$ rows.  To count such rectangles, simply pick any $2$ distinct columns, and any $2$ distinct rows.  So the final answer, i.e. the total number of inversion pairs, is ${p \choose 2}{q \choose 2}$.


Multiple Variable Limit.x, y tending to 1

Question: I have a multi-variable limit issue that I have no idea how to do.
$$ \lim_{(x,y)\to(1,1)} \frac{(x-1)^{4/3}-(y-1)^{4/3}}{(x-1)^{2/3}+(y-1)^{2/3}} $$
Answer: Hint: $\displaystyle(x-1)^{4/3}-(y-1)^{4/3}=\bigl((x-1)^{2/3}+(y-1)^{2/3}\bigr)\times\bigl((x-1)^{2/3}-(y-1)^{2/3}\bigr)$,
Comment: Thank you so much, sorry for my inattention.
Comment: I'm glad I could help.
Answer: Apply
$$a^2-b^2=(a+b)(a-b)$$
where $a=(x-1)^{2/3}$ and $b=(y-1)^{2/3}$. This gives you
$$\frac{a^2-b^2}{a+b}$$
Comment: Thank you so much!
Answer: HINT
To simplify let:

$ u=(x-1)^{2/3}$
$ v=(y-1)^{2/3}$

then
$$ \lim_{(x,y)\to(1,1)} \frac{(x-1)^{4/3}-(y-1)^{4/3}}{(x-1)^{2/3}+(y-1)^{2/3}} = \lim_{(u,v)\to(0,0)} \frac{u^2-v^2}{u+v} $$
Comment: Thank you so much!


Garbage bytes in http output?

Question: When I use telnet to view my web, I get these weird numbers/letters in the output. This does not happen in a browser and they are definitely not in my source code. I have checked other sites and they do not have them. Any ideas why ? Are they garbage ? How can I get rid of them ?

telnet www.mysite.com 80
    GET / HTTP/1.1
  Host www.mysite.com

Some parts of the webpage are ok but others look like this (I have highlighted them in bold):

51

<a href="/path/to/file
1b
">Link name

<a href="/path-to-another-file
2a

EDIT: more info requested
Apache 2 and CGI (C)
Dynamically generated
Comment: What HTTP server software runs on your web server?  Is your page static or dynamically generated?
Comment: Maybe this question should go to ServerFault?
Answer: The server is using "chunked" encoding, which allows the web server to keep HTTP connections open for reuse after a request for a dynamically generated page (e.g. CGI, PHP, or ASP), without buffering the entire page at the server end before transmission. It works by sending small parts ("chunks") of the web page at a time that each have a specified length in bytes (the hexadecimal numbers you see scattered throughout the page). That allows the client to know when the server is done sending data.
Per RFC 2616 (the HTTP specification), all HTTP/1.1 clients are required to be able to decode chunked encoding. This means that if you do not want to receive a chunked response, you will need to request the page using the version identifier of HTTP/1.0, an older version of the specification:
telnet www.mysite.com 80
GET / HTTP/1.0
Host: www.mysite.com

The server will then close the connection after it has finished responding instead of using chunked encoding and keeping the connection open.
Answer: This is HTTP Chunked transfer encoding, If you don't want to get that, do an HTTP 1.0 request instead.

Garbage bytes in http output?


Counter example to Borell Cantelli when $\sum \mu(A_n)^2 <\infty$

Question: I am looking for a counter example to the Borell Cantelli Lemma when $$\sum \mu(A_n)^2 <\infty$$
There is a probability solution here:A variation of Borel-Cantelli Lemma 2 but I am looking for one in the Lebesgue measure. It seems clear to me that we have to somehow exploit the fact that the harmonic series diverges and $1/n^2$ converges. However, I could not come up with the intevrals.
Answer: One strategy: For $x \in (0,1]$ we can write
$$x = \sum_{n=2}^\infty \frac{a_n}{n!}$$
where $a_n$ is a non-negative integer $0 \leq a_n < n$.  Further, $a_n$ is uniquely determined by $x$ if we assume there is no $N$ such that $a_n = 0$ for all $n \geq N.$  Therefore, we can write $a_n = a_n(x).$
Now define $A_n = \{x \in (0,1] \mid a_n(x) = 1\}.$
(Compare this to the answer given in your link)


Intersection of Turing Machines Languages

Question: Given a Turing Machine A and a Turing Machine B, how can I know if the intersection of the Languages of both Turing Machines is non empty?
L(A) $\cap$ L(B) $\neq$ 0
Answer: Unfortunately, this problem is undecidable! See: Rice's Theorem.
Comment: Is there a way to prove it without using Rice's Theorem? Thanks.
Comment: Try [this](http://cs.stackexchange.com/questions/30077/decidability-of-empty-intersection-of-two-languages-accepted-by-turing-machines) thread!
Comment: Thank you! I answered the thread, can you check if my approach to solve the problem is correct?


how to select the value from sub query result

Question: I have the following 4 tables mentioned below and trying to extract the value ACC_NUMBER from sub query, Please help me with optimized correct syntax
ACCOUNT TABLE
-------------------------
|ACC_NUMBER  |  ACC_NAME|
-------------------------

ACCOUNT_DETAILS TABLE
---------------------------------
|ACC_NUMBER |DEAL_NUMBER|DEAL_TYPE|
---------------------------------

DEALS TABLE
------------------------------------
|DEAL_NUMBER |DEAL_TYPE|DEP_NUMBER |
------------------------------------

DEPARTMENT TABLE
------------------------
|DEP_NUMBER  | DEP_NAME |
------------------------

SELECT
D.DEAL_NUMBER,
D.D_TYPE,
DP.DEPARTMENT_NUMBER
FROM
DEALS D , DEPARTMENT DP
WHERE
D.DEP_NUMBER = DP.DEP_NUMBER
AND D.DEAL_NUMBER NOT IN
                 (SELECT ACD.DEAL_NUMBER
                    FROM ACCOUNT_DETAILS ACD, ACCOUNT AC
                   WHERE AC.ACC_NUMBER = ACD.ACC_NUMBER
                        AND DEAL_TYPE = 'XXXX'
                         AND ACD.DEAL_NUMBER = D.DEAL_NUMBER)

Comment: If you are using "D.DEAL_NUMBER NOT IN" that means that records join is not present in ACCOUNT and ACCOUNT_DETAILS table, How could you expect to get ACC_NUMBER for that particular deals?
Comment: Before anything else you must use `ANSI` sql join rather than this old join technique
Comment: I am trying to extract the ACC_NUMBER with some alteration in the current syntax , But i want the ACC_NUMBER in the result
Answer:
Hi, If you are using D.DEAL_NUMBER NOT IN that means that records
  join is not present in ACCOUNT and ACCOUNT_DETAILS table, You will
  never get  ACC_NUMBER for that particular deals?
If you are ready to do some alteration in the current syntax, You can
  play with joins to get ACC_NUMBER in your query result:

Example:
SELECT
D.DEAL_NUMBER,
D.D_TYPE,
ACD.ACC_NUMBER,
DP.DEPARTMENT_NUMBER
FROM
DEALS D
join DEPARTMENT DP
on D.DEP_NUMBER = DP.DEP_NUMBER
left join ACCOUNT_DETAILS ACD
on ACD.DEAL_NUMBER = D.DEAL_NUMBER
join ACCOUNT AC AC.ACC_NUMBER = ACD.ACC_NUMBER
WHERE DEAL_TYPE = 'XXXX'
             AND ACD.DEAL_NUMBER = D.DEAL_NUMBER

Comment: Thanks for the reply , The earlier requirement was to select the deal_number which is not associated with account_details table , Now the new requirement is to select the deal_number and the account_number
Comment: Am thinking can we achieve this by creating function?
Comment: why to create function if you can achieve it using simple queries.
Comment: If you want only deal_number which are associated with account_details table you can use inner join instead of left join in above query.


How do I manipulate this equation to show they are equal without actually calculating it $\sqrt{3}\cdot \tan(\frac{\pi}{12})+3 = 2\sqrt{3}$

Question: I calculated both sides to be $3.4641016151377544$.
Hopefully, the LaTeX shows up in the title but if not here it is again:
$\sqrt{3}\cdot \tan(\frac{\pi}{12})+3 =  2\sqrt{3}$
I'm cutting 8 congruent shapes out of fabric to build a sphere. Having this equality known will help me construct it more accurately, using a compass instead of a bunch of lengths and angles. I can tell from the calculation that they are equal but my curiosity got the best of me and I am just wondering if the terms can be expressed differently to prove they are equal without calculating the sqrts or the tan.
By the way it totally possible that I forgot some axioms or something and made some assumptions about this shape that aren't true. Here's a pic of my theoretical sketch:
Comment: Per the posted answers, it is difficult to imagine verifying the equation without basing it on **something**.  This means that you specifically have to focus on the fact that the angle that you are taking the tangent of, is $\theta = \dfrac{\pi}{12}.$  I suppose that the only alternative to the trigonometry approaches in the two answers would be some sort of purely geometric demonstration.  For example, you might consider constructing an isosceles triangle, with two of the angles equal to $ \dfrac{\pi}{12},$ and see where this leads.  Hard to imagine a purely geometric demonstration, though.
Comment: @user2661923 It is not possible to have a *purely* geometric demonstration, because one is trying to find the exact value of a trigonometric ratio. However, one way that is pretty much as "geometric" as it gets is as follows: start with a square of side $2$. construct an equilateral triangle within the square such that the base of it is exactly the top side of the square. Now join the bottom two vertices of the sq. to the apex of the triangle. Show by pure geometric axioms (fairly trivial) that the base angles of the resulting isosceles triangle are each equal to $\frac{\pi}{12}$. (continued).
Comment: Now deduce by Pythagoras' (applied to an appropriate bisection of the equilateral triangle) that the height of the isosceles triangle is $2 - \sqrt 3$, and the rest is very obvious.
Comment: @Deepak I request that you transfer your comments into an **answer**, referring (in your answer) to my comment.  I feel that your analysis deserves to be posted as a separate answer.
Comment: @user2661923 Done. Same answer, though. Didn't feel right making a new one.
Comment: Since $\sqrt{3}$ is *irrational*, so is $2\sqrt{3}$, so both sides *do not equal* $3.4641016151377544$; that is only an approximation.
Answer: Note that we have
$$\tan(x/2)=\frac{1-\cos(x)}{\sin(x)}\implies \tan(\pi/12)=\frac{1-\cos(\pi/6)}{\sin(\pi/6)}$$
And you can finish now.
Comment: @Deepak Thank you.  Much appreciated!
Comment: Corrected a small typo. Hope you don't mind.
Answer:
The key thing is to show $\tan \frac{\pi}{12} = 2 - \sqrt 3$.
To do this, note that $\tan \frac {\pi}6 = \frac 1{\sqrt 3}$. This can easily be derived from the special $(1,\sqrt 3, 2)$ right triangle which you can construct by bisecting an equilateral triangle of side $2$.
By the double-angle formula for tangent, $\tan 2x = \frac{2\tan x}{1 - \tan^2x}$
If you let $\tan \frac{\pi}{12} = t$, you can now write:
$\frac{2t}{1-t^2} = \frac 1{\sqrt 3}$.
Try solving that exactly in radicals. The rest should be easy.
EDIT: As requested by user2661923, I have included a more "geometric" approach, with reference to the figure above. The top triangle is equilateral, the bottom one is isosceles, and both triangles exist within a square of side $2$.
Answer: Applying the formula for $\tan(a+b)$ (which has plenty of proofs, including geometric) to $a=\frac\pi3$ and $b=-\frac\pi4$, we get$$\tan\frac\pi{12}=\frac{\sqrt3-1}{1+\sqrt3}=2-\sqrt3.$$


Why are the p-values of this mixed effect model non-uniform when there is no effect (synthetic data)?

Question: I've been looking into using mixed effect models for a project, and along the way wanted to experiment with how they'd work on synthetic data.
However, I'm confused by the results — if I generate what I think is null data (e.g. no true difference between two inputs), the p-value for their difference is skewed towards higher significance than I would expect.
I'm not sure if I'm making an incorrect assumption about how mixed effect models are supposed to work, or if I'm generating it incorrectly. Here's what I've been doing:
I have two sets of inputs, each with N distinct groups, and M samples per group. To draw outputs, for both sets of inputs, I draw N values to represent each group's baseline effect, and then M values per group to represent the random variances. I then add those up for both sets of inputs, run a mixed effect model, and generate a p-value. I repeated this 1000 times and made a histogram of the p-values.
Here's the code:
<code>import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

num_groups = 10
num_samples_per_group = 20
num_iter = 1000

group_var = 1.0
random_var = 1.0

# Build basic dataframe w/ two inputs (input_0 and input_1), each with num_groups different groups, each
# containing num_samples_per_group individual samples.
df = pd.DataFrame({
    'input': ['input_0'] * num_samples_per_group * num_groups + ['input_1'] * num_samples_per_group * num_groups,
    'group': (
        ['input_0_group_%s' % (i % num_samples_per_group) for i in range(num_samples_per_group * num_groups)] +
        ['input_1_group_%s' % (i % num_samples_per_group) for i in range(num_samples_per_group * num_groups)])})

pvals = []
for i in range(num_iter):
    # Draw a group effect for each group w/ variance group_var
    input_one_group_effects = np.random.normal(0, np.sqrt(group_var), num_groups)
    input_two_group_effects = np.random.normal(0, np.sqrt(group_var), num_groups)

    # Draw a random effect for each sample in each group w/ variance random_var
    input_one_random_effects = np.random.normal(0, np.sqrt(random_var), (num_samples_per_group, num_groups))
    input_two_random_effects = np.random.normal(0, np.sqrt(random_var), (num_samples_per_group, num_groups))

    # Add group and random effects.
    input_one_effects = (input_one_group_effects + input_one_random_effects).reshape((num_samples_per_group * num_groups,))
    input_two_effects = (input_two_group_effects + input_two_random_effects).reshape((num_samples_per_group * num_groups,))

    # Insert into dataframe and run stats model.
    df['output'] = np.concatenate((input_one_effects, input_two_effects))
    model = sm.MixedLM.from_formula(
            "output ~ input",
            data=df,
            groups="group",
    ).fit()
    pvals.append(model.pvalues[1])

plt.hist(pvals)
</code>
And here's the resulting histogram:

I'm not sure if this is because I'm misunderstanding the assumptions of mixed effect models, or if this is expected when there's limited #'s of groups?
Thanks a bunch for your help.
Answer: The situation with a small number of groups is very challenging.  If you switch it to 20 groups and 10 samples per group (instead of 10 groups and 20 samples per group), the performance will be much better.
Right now we use a normal reference distribution to get the p-values.  We should add an option to use the t-distribution.  There is a literature on degrees of freedom for this (Satterthwaite, Kenward-Roger, etc.), which we have not implemented yet.
Comment: Thanks, that's very helpful -- does this mean that in a real-world situation where we want to model a similar setup to assess significance, we shouldn't rely on p-values below a certain # of groups? Or is there an alternative you'd suggest?
Comment: The performance depends not only on the number of groups but also on the variance (group and individual).  The Satterthwaite and Kenward-Roger approaches are the best way to handle this, but they are tricky to implement (some details in appendix A here: https://www.jstatsoft.org/article/view/v082i13/0).  I will get to it eventually...  With few groups you could consider fixed effects for groups.  Another alternative is GEE, which also has problems with a small number of large groups (see the work of Mancl, Westgate, etc.).


Examples of Incomplete Spaces

Question: A metric space is complete if every cauchy sequence is convergent.
To make space incomplete either i can change the metric or the ambient space.
For example if I change real numbers into rational number with usual metric ( absolute value ) it would be incomplete. On the other hand if have a some kind of metric on some space it would be incomplete though.
My question is: Can someone give examples of incomplete spaces such that either they have unusual metric or unusual ambient space other than rational numbers etc ?
Comment: Take any complete metric space and remove one (or two) points?
Comment: Actually, any space with the discrete metric is complete: every Cauchy sequence is constant.
Comment: @TöreDenizBoybeyi the definition of trivial is, in this case, rather personal.
Comment: Quang Hoang, it would be trivial.
Comment: Then, are there any examples without removing points ?
Comment: There is a theorem which says that any metric space can be be embedded densely and isometrically in a complete metric space. So in a sense, removing points from a complete metric space is the only way to produce one which is incomplete. Of course, that assumes that you know what all the complete metric spaces are...
Comment: Can anyone say which theorem is it ?
Comment: Look up "completion of metric spaces" in any topology textbook or on the web.
Answer: Here is an example of a metric $d$ on $\mathbf R$ such that  $(\mathbf R,d)$ is not complete.
Set $d(x,y)=\bigl\lvert\mathrm e^{-x}-\mathrm e^{-y}\bigr\rvert$. It trivially satisfies the axioms of a metric.
Now the sequence of natural numbers is a Cauchy sequence. Indeed choose $\varepsilon> 0$ and let $N$ be an integer such that  $\;\mathrm e^{-N}<\dfrac\varepsilon2$. If $p,q>N$, we have:
$$d(p,q)=\bigl\lvert\mathrm e^{-p}-\mathrm e^{-q}\bigr\rvert\le\mathrm e^{-p}+\mathrm e^{-q}<2\cdot\mathrm e^{-N}<\varepsilon. $$
This sequence does not converge. If it did converge to some $u\in \mathbf R$, we would have $\;d(n,u)=\bigl\lvert\mathrm e^{-n}-\mathrm e^{-u}\bigr\rvert<\varepsilon$ if $n>N_1$ for some $N_1$. If $n>\max(N,N_1)$, this implies
$$\mathrm e^{-u}<\mathrm e^{-n}+\varepsilon<\frac{3\varepsilon}2.$$
As $\varepsilon$ was chosen arbitrarily, this means $\mathrm e^{-u}=0$, which cannot be.
Answer: Let $$m_0 := \{t \in \mathbb{R}^\mathbb{N} : \{t_1, t_2, \ldots \} \text{ is finite} \}$$
be the sequences of real numbers that only take on finitely many values.
Equip it with the sup-norm, i.e. $$\| t \|_\infty := \sup_{i \in \mathbb{N}} |t_i|.$$
This is an incomplete normed vector space (so it's also a metric space). To see this, consider the sequences $$t^{(1)} = (1, 0, 0, \ldots),$$ $$t^{(2)} = (1, \frac{1}{2}, 0, 0, \ldots ),$$ $$t^{(3)} = (1, \frac{1}{2}, \frac{1}{3}, 0, 0, \ldots )$$
and so on.
Assume $i > j$, then we have
$$\|t^{(i)} - t^{(j)} \|_\infty = \| (0, \ldots, 0, \frac{1}{j+1}, \ldots, \frac{1}{i}, 0, 0, \ldots ) \|_\infty = \frac{1}{j+1},$$
this converges to $0$ for $i,j \rightarrow \infty.$ Therefore $(t^{(i)})_{i\in \mathbb{N}}$ is a cauchy sequence in $m_0$. But it's limit (in the bigger space $\mathbb{R}^\mathbb{N}$ of sequences of real numbers) is
$$(1, \frac{1}{2}, \frac{1}{3}, \ldots, \frac{1}{i}, \ldots ),$$
which is not an element of $m_0$. Hence, $m_0$ cannot be complete.

Examples of Incomplete Spaces


Trigonometric integral using residues

Question: I am trying to evaluate following integral by using the Residue Theorem:
$$\int_0^{2\pi}\frac{\cos^2x}{13+12\cos x}dx.$$
Then $\cos x=\frac{z^2+1}{2z}$, $dx=\frac{dz}{iz}$ and we have  $f(z)=\frac{(z^2+1)^2}{2z+3}$, $$\int_\gamma\frac{f(z)}{z^2(3z+2)}dz$$
The poles $z=0,\frac{-2}{3}$ are inside $\gamma=\{z\in C:|z|=1\}$, but I can't partition off simple fractions.
Comment: You If you don't show your calculations, what is $\;f\;$ and etc., it is hard to tell what you can do...
Comment: I have mentioned above $$f(z)=\frac{(z^2+1)^2}{2z+3}$$
Answer: Your reduction of the real integral to the complex one is correct
$$\int_0^{2\pi}\frac{\cos^2 x}{13+12\cos x}dx=\frac{1}{4i}\int_{|z|=1}f(z)dz
=\frac{\pi}{2}\left(\text{Res}(f,0)+\text{Res}\left(f,-\frac{2}{3}\right)\right)$$
where $\displaystyle f(z)=\frac{(z^2+1)^2}{z^2(2z+3)(3z+2)}$.
Now by the partial fraction decomposition there are constant $A,B,C,D,E$ such that $$f(3z+2z)=\frac{A}{z}+\frac{B}{z^2}+\frac{C}{2z+3}+\frac{D}{3z+2}+E.$$ You need to find $A$ and $D$.
Instead of looking for the partial fraction decomposition of $f$ why don't you compute the residues directly?
Take a look at Calculating residues. Then
1) For the residue at $-2/3$, which is a simple pole, consider the limit
$\lim_{z\to -2/3}(z+2/3)f(z)$.
2) For the residue at $z=0$, whose order is two, you may use the  limit formula for higher order poles or just note that
$$\frac{(z^2+1)^2}{(2z+3)(3z+2)}=\frac{1+2z^2+z^4}{6(1+\frac{2z}{3})(1+\frac{3z}{2})}=\frac{1}{6}\left(1-\frac{2z}{3}-\frac{3z}{2}+o(z)\right).$$
Can you take it from here?
Comment: @Tamer Közleme Did you find the residues an the final result?
Comment: yes i can find $$f(z)=\frac{1}{6z^2}-\frac{169}{90(2z+3)}+\frac{169}{60(3z+2)}-\frac{13}{36z}+1/6$$ and  finally result $\frac{3\pi}{45}$
Comment: sorry i made a mistake while writing and then I could not change the comment finally result $\frac{13\pi}{45}$

Trigonometric integral using residues


What is overfitting while building model?

Question: What exactly is overfitting while building models ?
Answer: I like @RRMT's answer, but I would like to emphasize this problem: adding many variables could look like it has a significant impact on the prediction power of the model, because it does... only for the data on hand.
Overfitting refers to creating a model trying to explain everything which is happening in the data, using way too many parameters/features compared to what is truly relevant for the model. Usually, the predictions of the model in the training data get better and better as we add (useless) variables, but the out-of-sample predictions could get worse and worse, or at least stagnate, while our expectations were higher because of our accuracy in-sample.
For example, imagine that you have the following relation:
$$Y_i=1+X_i+2X_i^2+\epsilon_i$$
with $X_i =-11+i, i=1,...,21$, and $\epsilon_i\sim N(0,\sigma^2=20^2)$
The data I simulated looks like this:

The green line represents $E(Y_i\vert X_i)$, i.e. the true model without the random errors.
If we try to fit the data using only a linear regression with $X$ as a predictor, then we create a simple line. The model underfits because the regressors we use are not sufficient to understand the underlying logic of the data.

If we add $X^2$ as a regressor, then our fit is very good, because the regressors we used explain very well the actual underlying logic of the data.

However, if you could add many regressors $X^k,k\in\{3,4,...,20\}$, you would end up creating a model which makes perfect predictions because you would have as many regressors as observed data points. The model would then overfit because it fits the data on hand perfectly, but way too much to really understands the logic of the data. The prediction curve (I failed to draw it because my regression becomes unstable due to the high numbers in the regressors) would go through all the observed points $\{x_i,y_i\}$ and it would make big swings between points, making out-of-sample predictions very bad.
I suggest you look at this article for a nice visual example.
EDIT:
I was able to add regressors up to $X^7$, here is what it looks like:

You can see that there is a first sign of overfitting at the last data point: the regression overfits and make the predicition very close to the actual point whereas it should not be that close: we observe a deviation between the green line and the red line, due to overfitting, which corresponds to a worsening of the out-of-sample prediction accuracy in that area.
EDIT:
@RRMT proposed solutions which can be applied in the training data. In addition to these solutions, you can divide your dataset into a training set and a testing set in order to apply some backtesting procedures (such as cross-validation) in order to try to identify is some overfitting is occuring.
EDIT 2:
I changed my data (but kept the general concepts: quadratic true model vs high order polynonial (15)). Here is the result:

You can see that using many predictors allowed the model to create a prediction curve which comes extremely close to all the observations. But this accuracy is only artificial, it creates deviations from the true model. This is an example of overfitting.
In addition, I suggest you have a look the interesting comments in the comments below my answer.
Comment: Thanks for your answer.  So, remedies are used to reduce number of parameters. So that only meaningful parameter's are used as regressor to avoid overfitting. To name these remedies are as mentioned by @RRMT
Comment: Yes, and I added a couple of sentence to propose other solutions.
Comment: This conveys the essence but "too much" (meaning, too many predictors) is the main idea of overfitting while "way too much" conveys extreme cases. As often, a definition can be a fairly useless paraphrase: a definition of fitting an excessively complicated model raises just as many questions, chiefly how to recognise that you are doing it Toy examples like that here make the point but don't really help learners much. That is, if the real model is quadratic plus noise, then clearly it's not a good idea to fit a more complicated polynomial. But this may not be the issue in practice.
Comment: In some fields it is standard and even encouraged to fit many predictors that may have small effects even if they don't qualify as significant for various reasons e.g. they are part of a team or to show that you have taken account of a predictor's influence even if its effect is minor. There are many variants on this: e.g. including the complete set of indicators (dummy variables) for a categorical variable is often encouraged, as is always including main effects as well as interactions even if the main effect appears small. Simplicity can be a matter of statistical style too.
Comment: @NickCox Thanks for your feedback. I agree with your points. I agree that my answer is not perfect and use a very specific example, but I think that extreme examples can help to grasp what could go (really) wrong in a particular case in order to understand the general concept/definition of overfitting. Ideally I would have liked to draw the predictions based on a polynomial of degree 20 to represent the real mess due to overfitting, but failed to do so.
Answer: Say regression as a model, overfitting means adding bunch of independent variables to explain a dependent variable. Adding even one independent variable, will increase the the fitting ability of the model. However, there are several problems/issues to doing this and some remedies to it.
Problems:

The addition of the new independent variable does not significantly improve the model fitting.
The new variable has correlation with other independent variables.
The new variable has little to no real (theoretical) connection to the dependent variable.

Remedies:

Check the significance of the variable. One can use Partial F-test, likelihood ratio test, $AIC$, $BIC$, etc.
Multicollinearity can be detected and solved using variance inflation factor, principal component, etc.
You can use step-wise regression to include or exclude the variable.
Comment: Thanks for your answer and listing the problems with overfitting and remedies to avoid it.
Comment: What is common is that as soon as anyone mentions any of the explicit trade-off criteria (AIC, BIC, etc.) then someone (even the same expert) asserts that it often gives the wrong answer, namely that it indulges or inhibits in the wrong direction. What is easier to agree is that goodness of fit and simplicity of fit are often in tension.
Answer: I like this simple mathematical explanation for the regression case so I'll leave it here:
Overfitting is a consequence of an imbalance between the following three factors:

Number of training samples N. If you have many training samples, you should learn well, even if the model is complex. Conversely, if the model is complex but does not have enough training samples, you will overfit it.
Model order d. This refers to the complexity of the model (d <= N). If your training set is too small, you need to use a less complex model.
Noise variance σ2. This refers to the variance of the error added to the data. If σ increases, it is inevitable that the fitting will become more difficult. Hence it would require more training samples, and perhaps a less complex model would work better.

In short:
MSEtrain = σ2 (1 - d/N)
MSEtest = σ2 (1 + d/N)


How to alter webform programmatically using hook_ENTITY_TYPE_prepare_form()

Question: Using Webform 6.2.0-beta5 on D10.0.9.
I have created a custom webform with a module; this is working perfectly.
Now I need to make several changes to the webform; some values will be calculated within my custom module, used as the default value for fields, and then the fields will be hidden.
With this code I can see the $webform details:
<code>function mymodule_webform_submission_prepare_form(WebformSubmissionInterface $webform_submission, $operation, FormStateInterface $form_state) {
  $webform = $webform_submission->getWebform();
  if ($webform->id() == 'custom_webform_id') {
    \Drupal::messenger()->addStatus($webform);
  }
}
</code>
How do I now, for example:

Add a value to a webform field
Hide that webform field from the user

This will have the effect of programmatically adding values to the webform fields.
Thanks
Comment: Different hooks operate at different stages, so the "best" hook depends on the specific thing you are trying to do. Please update the question with the actual code you have written so far for what you are trying to accomplish.   `hook_form_alter()` and `hook_form_FORM_ID_alter()` are the same except that `hook_form_alter()` runs for all forms and `hook_form_FORM_ID_alter()` runs only for the specified FORM_ID.
Comment: I've not written any code yet.  I want to change the form before it is rendered to the user.
Comment: Is the goal here to "calculate some values in custom code and then submit those values with the webform as a hidden field"?
Comment: @PatrickKenny

Yes, that's exactly what I need to do
Answer: Goal: Add some hidden fields to a webform, calculate the values for those fields in custom code, and then submit them with the webform.
You could use a hook for this, but I think there's a better approach: standard Webform hidden fields with a custom token.
Webfoorm supports creating hidden fields in the UI, so just create the fields you need there.
Then, you can set the value of the hidden fields to a token-- this is where your custom code comes in.  You can create tokens in a custom module, and then, after you rebuild the cache, your tokens will be available for use in webform.
Here's a tutorial about creating tokens, and the Examples module also has sample code for creating tokens.


É possível fazer um SPLIT direto em uma CONSULTA (SELECT) no SQL SERVER 2012?

Question: Tenho a seguinte tabela abaixo e preciso dividir a coluna "NomeCompleto" em colunas em um mesmo SELECT onde cada coluna deverá ser preenchida de acordo com seu nível.

Ex: A coluna NomeCompleto deverá retornar em cada nova coluna o que for referente ao seu nível separado pelo ' \ ', porém o MAIOR e o MENOR nível não deve mudar de coluna conforme o que demonstro no excel abaixo. Alguém tem alguma ideia de como fazer isso?? Pesquisei bastante na internet, mas só achei funções que devem ser usadas em procedures.

O nível que varia de 0 a 3 (4 niveis) ...
4 NÍVEIS = NÍVEL 3 :
MAIOR   |    NIVEL 2   |  NIVEL 1  |   NIVEL 0

3 NÍVEIS = NIVEL 2
MAIOR   |   NULL    |  NIVEL1   |   NIVEL 0

2 NÍVEIS = NÍVEL 1
MAIOR  |   NULL   |  NULL    |  NIVEL 0

Comment: O que determina se um Nome é nível 0, 1 ou 2?
Comment: O nome sempre será o ultimo nível, neste ex, eu alterei na mão algumas colunas, considere a coluna nome igual a coluna "NomeNivel0". Desculpe pelo erro, estou corrigindo.
Comment: Perguntei errado também. O que determina se um **NomeCompleto** irá para a tabela **NomeNível0**, **NomeNível1** ou **NomeNível2**?
Comment: Vou colocar acima pq aqui ficou ruim de entender....
Comment: @JoséDiz esse artigo me ajudou muito. Não tenho palavras pra agradecer.. vlw mesmo.
Comment: @WallaceCarlos Se puder divulgar o artigo para os amigos já ajuda e muito na divulgação do [Porto SQL].
Answer: Para separar colunas que possuem vários valores em uma mesma linha o usual é utilizar funções do tipo string split. Inclusive a partir da versão 2016 existe a função nativa STRING_SPLIT. Entretanto, quando é necessário que a separação dos valores seja retornada na mesma ordem, a função nativa STRING_SPLIT não garante.
No seu caso, versão 2012 do SQL Server, sugiro que utilize a função DelimitedSplit8K, de autoria de Jeff Moden. Informações adicionais sobre a função estão no artigo "Separar conteúdo de texto multivalorado".
Eis solução que utiliza a função DelimitedSplit8K:
-- código #1
with Separado as (
SELECT id, max (nome) as nome, max (Nivel) as Nivel,
    [I1]= max (case when SS.ItemNumber = 1 then SS.Item end),
    [I2]= max (case when SS.ItemNumber = 2 then SS.Item end),
    [I3]= max (case when SS.ItemNumber = 3 then SS.Item end),
    [I4]= max (case when SS.ItemNumber = 4 then SS.Item end)
  from tabela as T1
       cross apply dbo.DelimitedSplit8K (T1.NomeCompleto, '\') as SS
  --where ...
  group by id
)
SELECT id, nome, Nivel,
       I1 as NomeMaiorNivel,
       case when Nivel = 3 then I2 else '' end as NomeNivel2,
       case when Nivel >= 2 then I3 else '' end as NomeNivel1,
       case when Nivel >= 1 then I4 else '' end as NomeNivel0
  from Separado;

Não testei; pode conter erro(s).

Ainda no mesmo artigo há outras soluções. Eis a adaptação da solução proposta no item 3.4 para o seu caso:
-- código #2
with Separado as (
SELECT id, nome, Nivel,
       convert (XML,
                '<r><n>' + replace (NomeCompleto, '\', '</n><n>')
                + '</n></r>'
               ) as Item
  from dbo.tabela
)
SELECT id, nome, Nivel,
       i.value('n[1]', 'varchar(20)') as NomeMaiorNivel,
       case when Nivel = 3 then i.value('n[2]', 'varchar(20)') else '' end as NomeNivel2,
       case when Nivel >= 2 then i.value('n[3]', 'varchar(20)') else '' end as NomeNivel1,
       case when Nivel >= 1 then i.value('n[4]', 'varchar(20)') else '' end as NomeNivel0
  from Separado as SX
       cross apply SX.Item.nodes('/r') Item(i);

Não testei; pode conter erro(s).


How to check which model is better in state space time series analysis?

Question: I am doing time series data analysis by state space methods. With my data the stochastic local level model totally outperformed the deterministic one. But the deterministic level and slope model gives better results than with stochastic level and stochastic/deterministic slope. Is this something usual?
All methods in R require initial values, and I read somewhere that fitting an ARIMA model first and taking values from there as initial values for state space analysis is one way; possible? or any other proposition?
I should confess here that I'm totally new to state space analysis.
Comment: Please provide the example. Now it is unclear what is your actual problem.
Comment: Do you mean an exponential smoothing state space model? What R packages are you using?
Comment: Firstly, as already mentioned, it is not clear what your actual problem is. You write that A outperformed B and B gives better results than A. This is confusing.

 Secondly, the "forecast" R package has some automatic time series methods. They include: auto.arima(), ets(), tbats() and bats().
Comment: are you trying to compare models, or do you want to select a model?
Comment: Can you explain what you mean when you say 'outperformed' and 'gives better results than'?
Comment: One comparison measure that's well suited to implementation in state space is sums of squares of one-step-ahead prediction errors
Answer: To answer your first question. Yes, all is possible. It is not usual or unusual. You should let the data tell you what the correct model is. Try augmenting the model further with seasonals, cycles, and explanatory regressors if possible.
You should not only be comparing the Akaike Information Criterion (AIC) to compare models, but also checking to see that residuals (irregular term) are normal, homoskedastic, and independent (Ljung-Box test). If you can find a model that has all of these desirable properties. This should be your preferred model (it is likely that a model with all these properties will have the best AIC).
Although the initial values will affect which maximum point of the log-likelihood function is found, if your model is well specified, it shouldn't vary too much and there should be an obvious candidate for the best model with the best initial values. I do a lot of this type of analysis in Matlab and I found the best way to find initial values is just to play around for a bit. It can be tedious but it works out well in the end.


How to solve nonlinear optimization problem in R

Question: Suppose I have a set of data and reason to believe the following relation holds
y ~ a0 + a1*x1 + a2*x2 + a3*log(x3)
How can I use R to solve for the coefficients {a0, ... a3}, supposing I want to minimize the squared error of my data?  Also, can I do this using weights?
As an example suppose I give you the data
<code>set.seed(2014)
df <- expand.grid(x1 = c(1,1.5,2,2.5,3),
                 x2 = c(1,1.5,2,2.5,3),
                 x3 = c(5000, 10000, 20000, 40000, 50000))
df$y <- 5 + 10*df$x1 + 5*df$x2 + 3*log(df$x3) + rnorm(nrow(df))
head(df)
   x1  x2   x3     y
1 1.0 1.0 5000 44.99
2 1.5 1.0 5000 50.87
3 2.0 1.0 5000 55.68
4 2.5 1.0 5000 61.90
5 3.0 1.0 5000 64.26
6 1.0 1.5 5000 48.37
</code>
Without cheating, how can you figure out that a0 = 5, a1 = 10, a2 = 5 and a3 = 3?
Comment: Oh wait - I can just set x3 = log(x3) and solve the problem as a standard linear regression.
Comment: Have you tried the optim() function?
Comment: I haven't, but after reading the docs I'm still lost on how to set the problem up in R.
Answer: I am confused here. Why would you treat this as the nonlinear optimization problem, given that you set it up as an ordinary linear regression problem? To me this is exactly the ordinary linear regression. Given that you add random errors to the response, you would not expect the obtain exactly the parameter values.
<code>> fit <- lm(y ~ x1 + x2 + log(x3), data = df)
> summary(fit)

Call:
lm(formula = y ~ x1 + x2 + log(x3), data = df)

Residuals:
     Min       1Q   Median       3Q      Max
-3.02223 -0.58469 -0.01113  0.60692  2.30452

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   7.0027     1.1682   5.994 2.17e-08 ***
x1            9.7412     0.1359  71.684  < 2e-16 ***
x2            4.8860     0.1359  35.955  < 2e-16 ***
log(x3)       2.8834     0.1120  25.737  < 2e-16 ***
---
Signif. codes:
0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.074 on 121 degrees of freedom
Multiple R-squared:  0.9832,    Adjusted R-squared:  0.9828
F-statistic:  2365 on 3 and 121 DF,  p-value: < 2.2e-16
</code>
The estimates are very close to the true values of the parameters $a0, a1, a2, a3$, given that the sample size is 125.
Hope this helps.
Comment: Thank you.  I realized my mistake a few minutes after posting.


Standard equivalent of OLS for minimizing the $L_1$ norm

Question: I've been looking around out of curiosity if there is a standard method equivalent to OLS that finds the coefficients satisfying the minimum absolute error criteria as opposed to squared error, but haven't found anything for
$$
\min \sum_{i=1}^n ||F(x_i)-y_i||.
$$
I'm not talking about a LASSO (which is a penalty on the $L_1$ norm of the coefficients). Obviously a simple custom convex optimization could do this, but just curious if I was overlooking something already commonly available by not using the right buzz words/lingo.
Comment: [Quantile regression](http://stats.stackexchange.com/search?q=quantile+regression) is a generalization of this.
Comment: There's plenty of software around that fits these models, if that's what you're trying to ask.
Comment: whuber's answer is exactly what I was looking for.  Thanks!
Comment: L1 regression can be formulated with li ear programming
Answer: We know from intuition of the necessity of the Bessel correction that $$\arg\min_x \sum_{j=1}^n (x_j - x)^2 = \bar{x},$$ the sample mean. It similarly turns out that $$\arg\min_x \sum_{j=1}^n |x_j - x| = \mathrm{med}(x_1, \dots, x_j),$$ the sample median. Commonly in regression, we minimize the squared error, giving us estimates for the mean, but, if we were to instead minimize the absolute error, we'd get estimates for the median. Of course, when the the regression model is $y \sim \mathcal{N}(X \beta^*, \sigma^2I)$, the median is the mean so the differences between these two methods aren't too pronounced. Indeed, I've commonly seen quantile regression motivated as being useful in the presence of outliers.
An interesting history of the use of absolute error in regression is included here (in section 2):

Portnoy, S., & Koenker, R. (1997). The Gaussian Hare and the Laplacian Tortoise: Computability of Squared-Error versus Absolute-Error Estimators. Statistical Science, 12(4), 279–300.

It's discussed that the idea to use this loss was had long ago, but, due to computational intractability, it didn't gain widespread attention.
More generally, we could this is an example of quantile regression, which just installs weights into the absolute error that works for the median.


How can power and effect size help me interpret my A/B test results?

Question: To give an idea of my use case, I'm performing an A/B test using a two-sample t-test to compare the mean between a control and a treatment sample.
I set $\alpha=0.05$. I set my effect size $d=0.01$  and my power=0.80. Then, using a power solver, I get $n=156978$. I understand that power, effect and alpha help me define the sample size.
Then, I collect the data and compute the p-value. Now I can make a decision: if p-value<$\alpha$, then I can reject $H_{0}$; if not, then I fail to reject $H_{0}$. Clearly, $\alpha$ has a purpose: decide the outcome of my test.
However, what is the purpose of the power and effect size?
Power and effect size don't impact the p-value: I could arbitrarily increase or decrease both power and effect size (and keep constant $n$); it would not change the p-value, therefore, no impact on the test outcome.
So maybe they can help interpret the test results (e.g. give me some probability that my test is correct or something?). The power=0.80 tells me there is an 80% probability of rejecting $H_{0}$, assuming that the effect size I chose is the true effect size. But in real life, we can never know if that is the case (if we knew we would not be doing a test in the first place), so how is it helpful in any way?
Question: I'm struggling to understand how to interpret my test results in light of the power and effect size I chose. Other than "I need them to plug into my power solver". Indeed, they can help me interpret my test results in one way or another.
Comment: At the outset you write (correctly), "power, effect and alpha help me define the sample size."  Why then do you need to ask "what is the purpose of the power and effect size" when you have already answered that question?
Comment: The mistake is in thinking that the p-value tells you whether or not your theory is true.  In fact the p-value tells you how much evidence you gathered to reject your null hypothesis.  The power and effect size you chose tells you how hard you looked for that evidence.
Comment: I think there's some confusion around the p-value, which depends only on the data itself and not the expected effect size or power, and the alpha level, which is the fixed p-value threshold at which you make a decision with particular Type I/II error rates. The data produces some p-value regardless of your expectations, the question is if that's sufficient evidence for your alpha and power levels.
Comment: @NuclearHoagie I agree, p-value depends only on data. But I can see how it relates to alpha: you compare p-value<alpha to decide on test outcome. How does it relate to the power level?
Comment: @KiwiKiwi It relates inasmuch as alpha and power relate. For fixed N and effect size, if you want to be more sure that you will not miss calling a real effect significant (lower Type II error/higher power), you must allow a higher chance of calling no effect significant (higher Type I error/alpha). The p-value is your level of evidence (a result of your N), how you use that evidence to make a decision depends on your prior beliefs (effect size), and the tradeoff of what types of errors you're willing to make (alpha and power).
Comment: @NuclearHoagie What you are saying makes sense, although I need to ponder over it longer for my brain to really make those connections. I would accept that as an answer If you decide to make a reply answer (hopefuly with more details :)).
Comment: @KiwiKiwi And let's not forget the data you collect will allow you to estimate the true effect size (via point and interval estimates).
Answer: Power and effect size together tell you the probability that you'll correctly find a significant result, if the true effect really is as large as the effect size.
You've correctly noted that the p-value you actually observe in any statistical test cannot depend on your desired power, or how big you think the effect is, it's only determined by the data itself. What you can control is the critical value of the p-value, that is, what p-value you need before you call something "significant". For some dataset of N subjects, you can compute a p-value - the question is, when do you reject H0? That decision directly depends on alpha, which is related to power.
How you actually use the p-value you find to make a decision of significant or not significant will depend on your prior beliefs about the effect size, and which types of errors (Type I or Type II) you are willing to make. For fixed N and effect size, if you want a high power to ensure you won't miss real effects, you'll naturally have a higher Type I error rate and high alpha, and will require very little evidence to claim a significant result. On the other hand, if you don't want to erroneously claim significance, you'll require lots of evidence to claim a significant result. The exact same p-value may or may not represent "significance" depending on your expectations and the implied relative impact of Type I and Type II errors.
Power, alpha, N, and effect size are all related, given any 3 you can figure out the last one. For a fixed N and alpha, power and effect size are correlated - if you believe the effect is large, you'll have high power to detect it if it exists.
Comment: I don't see how "a priori believe there is a very large effect size" implies "you'll naturally have a high Type I error rate and high alpha."  Why should your belief about the magnitude of the effect determine the acceptable risk of not rejecting the null (in case the null just happens to be true)?
Comment: @whuber I think I conflated a couple of ideas and miscommunicated the result. The general idea was that if you want to find a small effect, you need lots of data to call significance at some alpha and power level. If you don't have that much data, you need to either look for larger effect, or at a less stringent significance level, or both. They can indeed be adjusted separately, but testing for only very large effects may imply that small-effect tests would have had too high an error rate.
Answer: You wrote

I could arbitrarily increase or decrease both power and effect size
(and keep contant n)

No, this is incorrect, unless no one else will look at your analysis. It's true that if you set a very large effect size, then the required sample size will be small. But people reading your analysis will not believe you.  If you develop a drug for some sort of cancer and say that it will reduce one year mortality rate by 99%, well, prepare for skepticism. You, on the other hand, have set a very low effect size (although it is context dependent).  If you can gather data cheaply and easily, that's not so much of a problem. But how often is that the case?
As to your question, well, no, not really.  Once you have output, the power analysis sort of fades into the background. Your output will include test statistics, standard errors, p values, and confidence intervals. That's what you interpret after doing the test.
However, if you find that you can't gather the required sample size, and that you get non-significant results, then you can say "the test may have been underpowered." But then someone may ask "you did the power analysis, it said you needed an unrealistic sample, so why did you continue?"
Answer: I'll take an unpopular but correct stance. If you want to 'interpret' or understand, or do anything else beyond an automatic decision that would be relevant to acceptance testing of widgets, you should ignore the pre-test $\alpha$ and entirely once you have the data. The accept/reject decision of the Neyman–Pearsonian hypothesis testing method deals with the long run error rate properties of the method, not the evidential meaning of the data.
Sure, you can use $\alpha$ and power in pre-experiment planning, but if you want to know what the data say then the p-value is where it's at (or likelihood function, or a Bayesian analysis), not the all or none larger or smaller than $\alpha$.
You can read much more about it (and other useful things like expected p-value instead of power for sample size estimation) here: https://link.springer.com/chapter/10.1007/164_2019_286
Answer:
Surely, they can help me interpret the results of my test, one way or another?

Power is something computed conditional on some assumption about the true effect size.  If the effect size was at least as large as what you assumed, then the probability you would reject the null correctly is the statistical power.
Once the study is run, power says nothing about what was actually observed.
Power does imply something about the effect you end up detecting.  Assume the alternative you've specified is true but your power is smaller.  Then if you end up detecting an effect, then that effect will be much larger than the truth.  Reason being is you've designed your test in such a way that only those detects much larger than the truth end up being statistically significant.


HTML loaded image goes away after refresh

Question: So this is a weird phenomenon I've never experienced in that I have an HTML form on image upload (exactly the one on W3). It does it's job properly when clicked and pushes the user profile picture into a folder while saving the name of the file on a database. Underneath the upload, I have a little image tag that spits out the uploaded profile picture. The end goal is for that profile picture to be displayed on the page constantly while the session exists. When uploaded, it works perfectly fine and the profile picture appears. After one refresh, the image can't be found and the alt="blank" takes over. The location still stays proper in the database so I don't think that's the issue. Is there an error? Do I need to use JS onload? Does the image tag only work once? Please help and thank you for taking the time to read this.
PHP:
echo '';
//$loaded_profile_picture has the value uploads/photo.jpg

Classes.php:
    public function addProfilePicture(){
        include_once "conn.php";
        $sql=$dbh->prepare("UPDATE users SET UserProfilePicture=:UserProfilePicture WHERE UserName=:UserName;");
        $sql->execute(array(
            'UserProfilePicture'=>$this->getUserProfilePicture(),
            'UserName'=>$_SESSION['UserName'],
            ));
    }

Image Upload (Might be the culprit):
        if($uploadsuccess&&move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
                    echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
                    $user->setUserProfilePicture($target_file);
                    $user->addProfilePicture();
                    if($user->getUserProfilePicture()!=NULL){
                        echo '';
                    }
        }

Edit:
While many users have a similar problem (i.e. Image display with  not working) I have tried looking at localhost:8888/xampp/LocalPost/pages/uploads/photos.jpg and it displays just fine. I have also changed file permissions on Windows 7 with no luck. Thanks for all your hard work I'll keep searching.
Edit:
I got around the problem by using $_SESSION['UserProfilePicture'] and it sticks after a refresh. This doesn't solve the question. I'd still very much like someone to help me find what went wrong, because later on I would like to display other people's Profile Pictures which you obviously can't do with session variables.
Comment: I assume the image exists at uploads/photo.jpg?
Comment: That's right, or at least the user submits photo.jpg and the website drops it into the folder uploads. After it's submitted I see the image in the uploads folder permanently and the database. The fact that uploads/photo.jpg still exists baffles me as to why alt="blank" would occur on the refresh. It's like it's ignoring it the second time through.
Comment: Could it be a permissions problem? The script doesn't have read access once refreshed
Comment: Hmm that may be it I have never heard of such. Do you know how to fix it or any resources to help understand this? Thanks
Comment: Can you use an FTP client to view the permissions of the file? And compare it to other permissions. I'm not sure I'm grasping at straws.. This is strange!
Comment: I'm sorry I'm not sure how to do that. I haven't registered with a webhost or have a means for FTP at the moment. uploads is beneath the xampp folder, I'm running Chrome if that makes a difference. I'll keep trying and edit if I find out what the problem is.
Comment: Oh one more thing, I have tried an sample image on my file before which sticks permanently. For example, instead of echo ''; I put echo ''; and it was permanent through each refresh.
Answer: first of all your missing } et the end of your script
    if($uploadsuccess && move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)){
                    echo " The file ".basename($_FILES['fileToUpload']['name'])." has been uploaded.";
                    $user->setUserProfilePicture($target_file);
                    $user->addProfilePicture();
                    if($user->getUserProfilePicture()!=NULL){
                        echo '';
                    }
}

Comment: Oops, so sorry I forgot to copy that over I had only cut the snippets that were relevant. Yes, I had the } deeper in my file sorry but thanks for your time.
Answer: Check once that after refreshing your page you will be get the value of $loaded_profile_picture variable or not?
If you got the value of $loaded_profile_picture variable then check it is same as your previous path or not?
Comment: Hm I took you're advice with echo $loaded_profile_picture and on the first page load it printed it perfectly uploads/photo.jpg. Then when I refreshed, the echo disappeared; the problem must be in the variable. My declaration looks like $loaded_profile_picture=$user->getUserProfilePicture();
Comment: It's still in the database... as for the session I put   if(!isset($_SESSION)){
   session_start();
  }
which may be irrelevant but jic
Comment: that means you did not get the value of $loaded_profile_picture when you refresh your page. So first try to get the value of $loaded_profile_picture variable.
Comment: So you're saying that the issue is with $loaded_profile_picture=$user->getUserProfilePicture();   ? So what's interesting is right under that line I added $loaded_profile_picture=14; echo $loaded_profile_picture; and it print out 14. But when taken away, nothing is echoed. I think you're right, what should I do next? Please answer thanks so much
Comment: OK, I tried echo $user->getProfilePicture(); and on refresh it blanked out what the heck is going on it's like the object $user doesn't exist. Does a refresh kill the object?
Comment: Other tutorials use $user_data['image'] and I'm unfamiliar with this. Is it advantageous to use over $_SESSION['profile'];?


How to approach piano chords that require you to press high in the keys

Question: I am currently learning a piece where at one point I need to play..
F# C E
..with my right hand.
Not only that, but the chord itself is played on the lower side of the piano (3rd octave) which makes it hard to play at a nice angle.
I am having trouble playing this bit because the angle and the chord itself make me put my fingers high into the keys where its impossible for me to push them down effortlessly. What is the best way to approach this? Where does the energy come from and how should I place my fingers optimally?
My left hand is busy lower on the piano so I can't play it with both hands.
Comment: If this is a question about playing the piano, may we have staff notation rather than guitar chord shapes please?
Comment: My bad, formatted it wrong
Comment: Are you sitting too close to the piano?
Answer: 1,3,5.  Learn to play with finger action, wrist action, arm weight...    Check your seating position and height, wrist height, finger curvature (or not) etc. so that you're mobile enough to reach that position freely.
Who told you everything had to be effortless?  But it shouldn't be THAT much effort to play 'up the keys' a bit.  It's often required.
Answer: R.h. - F# thumb, C middle, E pinky. Since your l.h. is busy even lower, you should have shuffled to your left, so that your body is in a central position to what you are playing, not, as I suspect, a central position to the whole piano. When the music uses higher notes, shuffle right.
Keys get pressed down wherever they get pressed down. Sometimes fingers (and thumbs) need to be deep into the keys. That's the nature of the beast.
Comment: So then since I'm higher in the keys I just 'press' the notes down with a bit of effort from my forearm because theyre harder to push down naturally?
Comment: Or even from your shoulder. But depending what you're doing, from the wrist will probably do the job.
Comment: Might also be the instrument to some degree.  What is it exactly?  (Piano or keyboard, what model?).  Some have the pivot points closer to the front of the keys than others.
Answer: You are most likely creating an ulnar deviation which breaks the fulcrum between the hand and arm making it feel weak and awkward.  A really bad teacher will tell you to "practice more."
You need to employ several movements.  I played that chord but not knowing where you are coming from nor where you are going, I can't give concrete answers. I sort of leaned to the left and brought my arm a little in front of me so as not to twist the wrist.
Of the other movements, if your arm is moving down but then back up the keyboard, you might want to initiate the movement from a little bit of arm weight, a little pronation and a slight forward shift.  My thumb was on the outside of the F# and that put my other two far in the black key area where the keys are heaviest.  So, you need to find power from someplace else without pressing into the keybed.
"Effortlessness" occurs in the fingers when you use your arm to play. There is no such thing as relaxation at the piano otherwise you'd slip off the keys and fall to the floor.
What gives the illusion of effortlessness is when you use ONLY ONE MUSCLE AT A TIME.  For instance, to strike one key with one finger you need only employ your bicep to lift the arm then control the weight or gravity to depress the key.  Try it.  No use of the finger is needed other than the slightest of flexation to make that one finger a little lower. All the other fingers go down too, which gives the whole hand a relaxed effortless feel.  All five fingers can only go on one direction at a time.  To use isolation causes two or four muscles to pull on your bones.
One should never use (only) the down muscles to play the piano.  All our down muscles are weak because of gravity.  You don't need any muscle to go with gravity.  Relax all your muscles and you will fall to the floor.  See?  Effortless.
Your biceps are strong because they battle gravity to lift things up.  Your triceps are weak because your biceps lower things to the ground in defiance to gravity.  Likewise, your quads are very strong because they lift us up while our hammies are weak because, you guessed it, gravity. Walking down stairs for instance uses your quads in defiance.  Each muscle gets to rest while the other is engaged.
Playing the piano is all about up so that your pronators, supinators, bicep, shoulder, elbow, wrist (to some extent) and flexors (to some extent) can do the rest of the work.
Hanon was very close to the answer when he advocated lifting a finger high however, that uses both your extensors and flexors simultaneously and your bones don't know which way to go.   This is why many teachers advocate "strength and endurance" however, you are building "strength and endurance" to the wrong muscles and the other muscles don't need it because of . . . gravity. What Hanon didn't know was that isolation created tension and his answer for tension was to relax.  RELAX WHAT!?!?!?!?! Well, indeed, relax the muscles you are actually using, the wrong muscles.  Replace them with the powerful indefatigable muscles such as the pronator and supinator.  Height is the answer but not through isolation.  We achieve it through lifting the arm, pronating, supinating, shaping, and above all, never stretching because that employs both the flexors and abductors then you have tension all over again.
So, there is no one answer to your question but, several amalgamated into one.


Integral $\int_{-\pi}^{\pi}\frac{\operatorname {d}\phi}{\sin\phi+4} $

Question: $\newcommand{\d}{\mathrm{d}}$I have this integral which I consider as challenging.
\begin{equation}
\int_{-\pi}^{\pi}\frac{\d\phi}{\sin\phi+4}
\end{equation}
I rewrite the denominator using  $\sin\phi=\frac{e^{i\phi}-e^{-i\phi}}{2i}$
\begin{equation}
\int_{-\pi}^{\pi}\frac{\d\phi}{\frac{e^{i\phi}-e^{-i\phi}}{2i}+4}
\end{equation}
So, at this stage it is written with the form for complex numbers. But since I want to solve this using the Residue Theorem, how does one do that on this form?
Thanks
Comment: This integral is easy with the real substitution $t=\tan \phi/2$.
Comment: In mathjax you can use backslashes for almost everything; `\sin,\cos` renders properly. It is also more convenient to write `$$` instead of `\begin{equation}`
Comment: With regards to Rene's comment, look up "Weierstrass substitution"
Comment: By the Residue theorem?
Comment: You shouldn't need to access complex analysis to solve this problem
Comment: There is obviously a mistake in your calculations: $\frac{1}{\sin\phi +4} \ne -\frac{1}{8\sin\phi}$ since the LHS is continuous and the RHS isn't.
Comment: thanks for spotting this. corrected
Answer: You've got $$\int_{-\pi}^\pi {d\phi\over {(e^{i\phi}+e^{-i\phi})/2i}+4}$$
Now let $z=e^{i\phi}$ then the integral becomes $$\oint {dz\over iz[(z+1/z)/2i+4]}=\oint {dz\over z^2/2+4iz+1/2}$$
Can you proceed now?
Comment: Notice that the integral contour is enclosing $|z|\le1$ so the residue of $-i\sqrt2-i$ should not be calculated
Comment: This is exactly what I though would be a tratable form for residue theorem.  Thanks. I need to factorize your denominator form. Then, the poles are considered on a half circle in the original problem. How do I form the new circle from this in z, or maybe this is not required, and I just form the residues for $z_1=i\sqrt{2}-i$ and $z_1=-i\sqrt{2}-i$
Comment: see update in the original post
Comment: Ah, thank you! That is noted.
Comment: PS Richard there may be an error in your reasoning,  sinx=$e^i\phi-e^-i\phi$. It appears you used plus instead of minus in the exponential relation of sine.
Comment: PS Richard there may be an error in your reasoning,  sinx=$1/2i/cdot(e^{i\phi}-e^{-i\phi})$. It appears you used plus instead of minus in the exponential relation of sine. This changes the poles, and they become $\sqrt{3}-2$ and $-\sqrt{3}-2$. Both are outside of the unit circle.


If $f(1) = 2$ and $f(n) = n \cdot f(n-1)$ then $f(n) \gt 2^n$ for all $n \gt 2$

Question: I'm having a little difficulty in proving what are probably simple induction proofs. Here is the question.
Define function $f(n)$ as follows. $f(1) = 2$ and $f(n) = n\cdot f(n-1)$ when $n > 1$.
Use induction to prove that $f(n) > 2^n$ for all $n>2$.
Note that $f(1) =2$, $f(2)=4$. I understand the basis step, so I'm not going to write that out.
Now we have. $f(k) = k \cdot f(k-1)> 2^k$ we want to show the following.
$f(k) + f(k+1) = (k+1)\cdot f(k)> 2^{k+1}$ This is where I'm getting stuck. Can someone give me a hint as to how to proceed?
Comment: $f(k)>2^k$ and $k+1>2$, therefore $2^{k+1}=2^k\times 2<f(k)\times 2<f(k)\times (k+1)=f(k+1)$
Comment: In the inductive step, you assume that $f(k)\gt 2^k$; you want to prove that $f(k+1)\gt 2^{k+1}$. I don't know why you have "$f(k)+f(k+1)$".
Comment: Your inductive step should be $f(k)=k\cdot f(k-1)\ge k\cdot 2^{n-1}$
Comment: I'm confused about the step where we add one. $P(n) \implies p(n+1)$
Answer: HINT $\rm\quad\displaystyle \frac{f(n)}{2^n}\ =\ \frac{2}2\ \frac{2}{2}\ \frac{3}2\ \frac{4}2\ \cdots\ \frac{n}2\ > 1\ $ for $\rm\ n > 2\ $ since each factor is $>1$ after the 2nd factor.
Generally that works to show that factorials grow faster than powers, i.e. $\rm\  f(n) > c^n\ $ for $\rm\ n > n_0\:.\ $ It suffices to show: eventually $\rm\ g(n) = f(n)/c^n > 1\:,\: $ or, equivalently, eventually $\rm\: g(n+1)/g(n) > 1 \ $ since, by multiplicative telescopy, $\rm\:g(n)\:$ is a product of these adjacent term ratios, namely
$$\rm g(0)\ \ \prod_{k\:=\:0}^{n-1}\ \frac{g(k+1)}{g(k)}\ =  \ \ {\rlap{--}g(0)}\frac{\rlap{--}g(1)}{\rlap{--}g(0)}\frac{\rlap{--}g(2)}{\rlap{--}g(1)}\ \ \cdots\ \ \frac{g(n)}{\rlap{----}g(n-1)}\ =\ \ g(n) $$
Yours has $\rm\ g(k+1)/g(k)\ =\ (k+1)/2\ >\ 1\ $ for $\rm\ k > 1\ $ so $\rm\:g(n) > 1\:,\:$ as a product of terms  $> 1\:.$
As I have emphasized here before in many posts,  by means of cancelling complicated expressions, telescopy often reduces induction problems to trivialities (e.g. a product of terms $> 1$ is itself $> 1$). Difficult problems involving hyperrational functions (i.e. $\rm\ f(n+1)/f(n) = $ rational function of $\rm\:n\:,\:$ such as powers and exponentials) are, after application of telescopy, greatly simplified to trivial problems about rational functions - functions so simple that questions about such can be decided mechanically by algorithms.
Answer: $$\begin{align}
P(3):&\quad f(3)=12 > 8 = 2^3 \\
P(k):&\quad f(k)>2^k \\
P(k+1):&\quad f(k+1) = (k+1)f(k) > (k+1) 2^k >2 \cdot 2^k = 2^{k+1}
\end{align}$$
So what we do is first to show that $f(n)>2^n$ for $n=3$. Then we assume that it is also true for $n=k$. At last we prove that it is true for $k+1$ if it is true for $k$.
What I did in the last step is this:

I wrote down the expression for $f(k+1)$.
Because of the step $P(k)$ we know (assume) that $f(k)>2^k$ which implies that $(k+1)f(k) > (k+1) 2^k$ since $k+1>0$.
We also know that $k+1 > 2$ which implies that $(k+1) 2^k >2 \cdot 2^k$.
$2 \cdot 2^k = 2^{k+1}$. So it follows that $f(k+1)>2^{k+1}$.
Answer: It might be clearer if you wrote $f(3)=12>8$ as well, to start you off.
Now with the inductive hypothesis $f(n)>2^n$ and $n>2$ you look at $f(n+1)$ and this implies
$$f(n+1) = n \times f(n) > n \times 2^n > 2 \times 2^n = 2^{n+1} \textrm{ and } n+1>n>2$$ (essentially what Asaf Karagila  wrote backwards), i.e.
$$f(n+1) > 2^{n+1} \textrm{ and } n+1>2$$
which is all you need.


C#: Cheking User Input with String Array List

Question: Basic Info

Programming Language: C# (C Sharp)
Program Type: Console .Net Core 2.0 Application
IDE: Visual Studios Community 2017 v15.8.7

Program Info
Program Name: Calendar Counter
Basics of my Program: User types in a month and it tells them how many days are in that month. The user is free to type any known combination of month name/identity (e.g. March, march, mar, Mar, Mar., mar., 03, or 3).
My Programs Execution: The program asks the user for a month and they type it in. The program then checks what the user typed to a method named MonthDatabase() with a String Array. If it matches it will display the month they typed in with how many days it has.
Issue Info
Program Issue: My main issue is finding a way to check what the user inputted with what's in the String Array. I've tried switches and different if statements, but most give me an error (I won't be posting all of those). I also tried it using the dictionary and a list. Each produced their own errors. So I went back to using a string array and after many searches and trying different things that just didn't work. I even tried to google for what I was looking to accomplish (e.g. check user input with string array) to no avail. Currently, the error I get is:

Delegate to an instance method cannot have null 'this'.

Now being new to C# (and programming in general) I could only assume that it wants me to specify what to check in the string array to match it to the user input. I tried something like:
Console.Write("Enter a month?: ");
string userMonth = (Console.ReadLine());

if (userMonth == mData(1, 1)) //Compare user input to MonthDataBase
        {
            Console.WriteLine("You typed a word found in the datatbase.");
        }
        else
        {
            Console.WriteLine("You didn't type a word in the datatbase.");
        }

AND
Console.Write("Enter a month?: ");
string userMonth = (Console.ReadLine());

if (userMonth == mData[1, 1]) //Compare user input to MonthDataBase
        {
            Console.WriteLine("You typed a word found in the datatbase.");
        }
        else
        {
            Console.WriteLine("You didn't type a word in the datatbase.");
        }

Thinking it would look at the string in that position and match it, but clearly, I am wrong on that assumption. I even tried to Google to see if there was a way to just simply match what the user typed to the whole string array, but found nothing.
I have spent a week trying to figure this out and decided to come here to see if anyone can point me in the right direction. I like the idea of learning it so if possible no direct answers as "do this" and it will work. Maybe an example (different, but close to what I'm trying to do) or "read this" and it should help you.
The Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace Calendar_Counter
{
    class Program
    {
    public static string mData { get; private set; } //Use mData in any method.

    static void Main(string[] args)
        {
            Header(); //Call Header Method & display
            Menu(); //Call Menu Method & display
            CCDatatbase(); //Call Calendar Counter Database Method, execute & display.

            //Console.WriteLine("Hello World!");
            ExitProgram(); //Call exit program, execute & display
        }
        static void Header()
        {
            Console.Clear(); //Clear console buffer & console window of display information
            Console.Write("--------------------\n| Calendar Counter |\n--------------------\n"); //Display Header text
        }
        static void Menu()
        {
            //ADD menu options once basic program is working!!
            Console.WriteLine(); //Space
            Console.Write("MENU: //ADD menu options once basic program is working!!");
            Console.WriteLine("\n"); //Double Space
        }
        static void CCDatatbase()
        {

            Console.Write("Enter a month?: ");
            string userMonth = (Console.ReadLine());

            if (userMonth.Any(mData.Contains)) //Compare user input to MonthDataBase
            {
                Console.WriteLine("You typed a word found in the datatbase.");
            }
            else
            {
                Console.WriteLine("You didn't type a word in the datatbase.");
            }
        }
        public static string[,] MonthDataBase() //Month Database
        {
            //Check user input with Array List.
            string[,] mData = new string[12, 8]
            {
                { "January", "january", "Jan", "jan", "Jan.", "jan.", "1", "01" }, //If user types 1-8 display corisponding message in CCDatatbase()
                { "January", "january", "Feb", "feb", "Feb.", "feb.", "2", "02" },
                { "March", "march", "Mar", "mar", "Mar.", "mar.", "3", "03" },
                { "April", "april", "Apr", "apr", "Apr.", "apr.", "4", "04" },
                { "May", "may", "May", "may", "May", "may", "5", "05" },
                { "June", "june", "Jun", "jun", "Jun.", "jun.", "6", "06" },
                { "July", "july", "Jul", "jul", "Jul.", "jul.", "7", "07" },
                { "August", "august", "Aug", "aug", "Aug.", "aug.", "8", "08" },
                { "September", "september", "Sep", "sep", "Sep.", "sep.", "9", "09" },
                { "October", "october", "Oct", "oct", "Oct.", "oct.", "10", "10" },
                { "November", "november", "Nov", "nov", "Nov.", "nov.", "11", "11" },
                { "December", "december", "Dec", "dec", "Dec.", "dec.", "12", "12" }
            };

            return mData;
        }
        static void ExitProgram()
        {
            //REPLACE later with an actual exit option in menu!!
            Console.Write("EXIT: //REPLACE later with an actual exit option in menu!!\n\n");
            //Prevent Debugging test from closing.
            Console.Write("Press any key to Exit...");
            Console.ReadLine();
        }
    }
}

Comment: Looks like this statement "if (userMonth.Any(mData.Contains))" - The mData is a property and not the variable which is declared in MonthDatabase().
And I can't see from where you were calling MonthDatabase() in your whole program?
Answer: If you don't have any intention to make it a double-dimensional array, then I would suggest go with creating a class. A class will simplifies many things in a long run rather than depending on a double-dimensional array in this case.
Here is my approach for the problem.

I have created a class called Month (Name you can change it as per your needs).
The class "Month" has a List property MonthNames as shown below,
  public class Month {
  public List<string> MonthNames { get; set; }

  public Month()
  {
      MonthNames = new List<string>();
  } }

Create the object of Month type as below in your GetMonths() method. (In this case you can discard MonthDataBase() method).
      private static List<Month> GetMonths()
      {
          var months = new List<Month>();
          var month = new Month();
          month.MonthNames.Add("January");
          month.MonthNames.Add("january");
          month.MonthNames.Add("Jan");
          month.MonthNames.Add("01");
          months.Add(month);

          month = new Month();
          month.MonthNames.Add("Feburary");
          month.MonthNames.Add("feburary");
          month.MonthNames.Add("Feb");
          month.MonthNames.Add("02");
          months.Add(month);
          // Similary add all other to the list.

          return months;
      }

Here is the rest of the code (You add this below code where ever you want in your program),
          Console.Write("Enter a month?: ");
          var userMonth = (Console.ReadLine());
          var months = GetMonths();

          var result = months.Where(x => x.MonthNames.Any(y => y.Equals(userMonth))).ToList();

Note:- I have returned as ToList(), but in your case you can return as FirstOrDefault().
So now you have a Month object with you and based on the data, you can print the no. of days or do some other thing is upto you.
Hope it is clear for you. Give a try and lets know in case of any errors or doubts.
Comment: It throws an error for the `GetMonths()`: "Does not exist in the current context. This is the same error I got when I tried to do it as a class. I tried: `GetMonths = MonthDatabase()`, but that throws its own errors. Visual Studios suggests:

`private static object GetMonths()
        {
            throw new NotImplementedException();
        }`
I also tried adding: `class Program : MonthDatabase`, but that didn't help.
Comment: Alright so according to this topic https://stackoverflow.com/questions/52903218/c-checking-user-input-with-string-array-list my issue would be: `private static List GetMonths()` when I change it to `public static List GetMonths()` the error goes away. Being new I'm going to assume you set it to private for a reason and if so why and how to make it work.
Comment: Few points to note down, When it throws GetMonths() does not exists, I assume that is because of "private" access modifiers. And I am not sure where you have created the "GetMonths()" method. In the above code which I pasted, I have added in "Program" class and hence it make sense for me.
Comment: This statement in your comment "class Program : MonthDatabase" - MonthDatabase is a method and you are trying to inherit a class from a method? That is not correct. A class can inherit another class or an interface. I guess you might need to study OO principles first.
Comment: Sorry for the delayed response, as per your second comment, hope it works for you. In case if any issues, do let us know. If it satisfies your requirements then you mark my answer as accepted.
Answer:
Try to change return type of your MonthDataBase method to List<List<string>>.
Try to change the signature of mData public propery to public static List<List<string>> mData { get; private set; }
Fill above property mData in CCDatatbase method like mData = MonthDataBase();
Then check whether entered string is present in mData or not.

So finally your program will be.
class Program
{
    public static List<List<string>> mData { get; private set; } //Use mData in any method.

    static void Main(string[] args)
    {
        Header();       // Call Header Method & display
        Menu();         // Call Menu Method & display
        CCDatabase();   // Call Calendar Counter Database Method, execute & display.

        //Console.WriteLine("Hello World!");
        ExitProgram(); //Call exit program, execute & display
    }

    static void Header()
    {
        Console.Clear(); //Clear console buffer & console window of display information
        Console.Write("--------------------\n| Calendar Counter |\n--------------------\n"); //Display Header text
    }

    static void Menu()
    {
        //ADD menu options once basic program is working!!
        Console.WriteLine(); //Space
        Console.Write("MENU: //ADD menu options once basic program is working!!");
        Console.WriteLine("\n"); //Double Space
    }

    static void CCDatabase()
    {
        mData = MonthDataBase();
        Console.Write("Enter a month?: ");
        string userMonth = (Console.ReadLine());

        if (mData.Any(x => x.Contains(userMonth))) //Compare user input to MonthDataBase
        {
            var month = mData.Where(x => x.Contains(userMonth)).Select(x => new { Days = x[0], Name = x[1] }).FirstOrDefault();
            Console.WriteLine($"{month.Name} has {month.Days} days in it.");
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("You didn't type a word in the database.");
            Console.WriteLine();
        }
    }

    public static List<List<string>> MonthDataBase() //Month Database
    {
        var mData = new List<List<string>>  {
      new List<string>  { "31", "January", "january", "Jan", "jan", "Jan.", "jan.", "1", "01" }, //If user types 1-8 display corresponding message in CCDatatbase()
      new List<string>  { "28/29", "February", "february", "Feb", "feb", "Feb.", "feb.", "2", "02" },
      new List<string>  { "31", "March", "march", "Mar", "mar", "Mar.", "mar.", "3", "03" },
      new List<string>  { "30", "April", "april", "Apr", "apr", "Apr.", "apr.", "4", "04" },
      new List<string>  { "31", "May", "may", "May", "may", "May", "may", "5", "05" },
      new List<string>  { "30", "June", "june", "Jun", "jun", "Jun.", "jun.", "6", "06" },
      new List<string>  { "31", "July", "july", "Jul", "jul", "Jul.", "jul.", "7", "07" },
      new List<string>  { "31", "August", "august", "Aug", "aug", "Aug.", "aug.", "8", "08" },
      new List<string>  { "30", "September", "september", "Sep", "sep", "Sep.", "sep.", "9", "09" },
      new List<string>  { "31", "October", "october", "Oct", "oct", "Oct.", "oct.", "10", "10" },
      new List<string>  { "30", "November", "november", "Nov", "nov", "Nov.", "nov.", "11", "11" },
      new List<string>  { "31", "December", "december", "Dec", "dec", "Dec.", "dec.", "12", "12" }
    };

        return mData;
    }

    static void ExitProgram()
    {
        //REPLACE later with an actual exit option in menu!!
        Console.Write("EXIT: //REPLACE later with an actual exit option in menu!!\n\n");
        //Prevent Debugging test from closing.
        Console.Write("Press any key to Exit...");
        Console.ReadLine();
    }
}

Input:  jul
Output:

Input:  04
Output:

Input:  abcd
Output:
Comment: Yes, it works. Now I just need to figure out how to check what the user typed with one row (January) and have it display a different message for each row (month).
Comment: @DragonflyCode, i didn't get u correctly could u plz explain with example? :)
Comment: The end result is the program will ask: "Enter a month: ". So let's say the user types in "June" or "Jun" or even "Jun." it will search the list and if it finds any accepted string from the list it will then display to the user: "June has 30 days in it."
Comment: @DragonflyCode, Yes its possible , I'll update my answer soon :)
Comment: @DragonflyCode, I updated my answer, kindly noticed that i had added days in month to each of list at first index and also notice the `if (mData.Any(x => x.Contains(userMonth)))` condition.
Comment: @DragonflyCode, If answer was helpful to you then mark the tick on left side of answer to make it green :)


Closure of a set: Point Set Topology

Question: I am given a set B $=\big\{\big(1-\frac{1}{n}\big)\times \frac{1}{2}:n\in \mathbb{Z}_+\big\}$.
My thoughts: Now when looking at this set I see that 1 is never obtained how ever the points in B are getting very close to it, which leads me to believe that the closure of this set denote $B_{\ closure}= B \cup \{1 \times \frac{1}{2} \}$.
Problem: Determine the closure of the following subset of the ordered square.
Comment: use for closure: $\overline{B}$ to avoid confusion. $B'$ usually stand for the set of accumulation points
Answer: Hint: Can you find another $a<1$ such that $1\times a$ is an upper bound for $B$? What is the least such $a?$ (Upshot: $B$ does have a single limit point, but you have not correctly determined it.)
Comment: Is this set B including the ordered pairs that fall in their respective boxes? For instance does $\frac{1}{2} \times \frac{1}{2}$ contain all ordered pairs of the form $(a,b)$ where $a,b\leq\frac{1}{2}$.
Comment: No. $\frac12\times\frac12$ is alternative notation for the ordered pair $\left(\frac12,\frac12\right).$ The set $B$ is comprised of the ordered pairs $$0\times\frac12,\frac12\times\frac12,\frac23\times\frac12,\frac34\times\frac12,...,\frac{n-1}{n}\times\frac12,....$$ Does that clear things up for you a bit?
Comment: Okay, yes that does clear some things up. I will think a bit more about it.
Comment: Okay. Let me know if you get stuck, and I'll see if I can help you out further.
Comment: Oh, my goodness! I just realized I made an error in my answer. It is now fixed. My sincerest apologies.

Closure of a set: Point Set Topology


TIPTAP editor, uploading info to database with nextjs

Question: I have installed the rich text editor TIPTAP. I am trying to update the data to the database but it doesn't upload any info. I am changing the following textarea(which works) to TIPTAP richtext.
               <textarea className="form form-control" value={formState.description} onChange={handleInputChange} id="description" cols="30" rows="10" name="description" placeholder={!language ? 'description' : 'description'} style={{ marginBottom: '5vh' }} ></textarea>
I have done it like this:
`<TipTap
value={formState.description}
onChange={handleInputChange}
id="description"
name="description"
placeholder={!language ? 'description' : 'description'}
setValue={setFormState.description}
/> ``
The TIPTAP component looks like this:
const TipTap = ({ setValue}) => { const editor = useEditor({ extensions: [StarterKit, Underline, ListItem, TextStyle], content:  `,
       onUpdate: ({ editor }) => {
      const json = editor.getJSON();
        setValue(json);
        },
        });

       return (

       <MenuBar editor={editor} />
       <EditorContent editor={editor} name={name} value={value} />

      );
      };
     export default TipTap;`

Any ideas?
Answer: Did you try getHTML function?

onUpdate() {
      setContentEditor(editor?.getHTML() || '');
    },
Comment: Thanks Delacyr, sorry, just say your response now but that is how I solved it


How to rewrite url in Wordpress?

Question: I would like to rewrite my url like this with .htaccess :
From : http://www.uuuuu.com/category/yop-2015/
To : http://www.uuuuu.com/2015
Only for the category yop-
I tried this, but it doesn't work :
RewriteEngine On
RewriteRule ^category/yop-(.+)$ http://www.uuuuu.com/$1 [R=301,L]
RewriteBase /

Answer: Found the solution:
add_action( 'init', 'example_add_rewrite_rules' );
function example_add_rewrite_rules() {
    add_rewrite_tag( '%category_name%', '([0-9]*)' );
    add_rewrite_rule( '([0-9]*)$', 'index.php?category_name=yop-$matches[1]', 'top' );
}

How to rewrite url in Wordpress?


Why ampps is not selecting my db?

Question: I'm using ampps
The following is my config code. I've not given username and password in it.
<code>$db_name="naatesam_FAMILY"; // Database name
$tbl_name="user"; // Table name

// Connect to server and select database.
$conn=mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
</code>
The output it shows is:
<code>cannot select DB
</code>
I think without username and password it should work....
If anyhow they're required then please tell me how to set username and password for phpMyAdmin in ampps.
Comment: are you sure you have created the `naatesam_FAMILY` database?
Comment: Was about to ask the same... Check with phpMyAdmin if your database exists, and create it if not.
Comment: Surely I've created the database...I also created a table in it named user.
Comment: @DipaliPatil - dont use mysql_connect. Because mysql_connect is deprecated, use  mysqli_connect. For your question you got any solution?
Comment: @Fathah Rehman P Yes I've got a solution. Hardik's comment on his answer has given the solution.
Answer: Your code should be :
<code>$db_name="naatesam_FAMILY"; // Database name
$tbl_name="user"; // Table name

// Connect to server and select database.
$conn=mysql_connect("localhost", "root", "mysql")or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
</code>
Comment: I tried doing that too as I've read in other questions in stackoverflow that the default username is root and password is empty
Comment: @DipaliPatil Did you put $conn variable in to your mysql_select_db() function?
Comment: @DipaliPatil : Default MySQL root password is "mysql" for ampps so you need to add password "mysql" in your connection. See my edit i hope that will help for you.
Comment: Thanks hardik....default password for mysql in ampps is mysql...this solved my problem....pls edit your answer so that I can tick it
Answer: <code>$conn=mysql_connect("localhost", "", "")or die("cannot connect");
</code>
change to
<code>$conn=mysql_connect("localhost", "root", "mysql")or die("cannot connect");
</code>
Comment: How do you know his mysql server user name is "root"??
Comment: I tried doing that too as I've read in other questions in stackoverflow that the default username is root and password is empty
Comment: @virendra Thanks for the generous help but hardik's solution worked.

Why ampps is not selecting my db?