Generate XML Document in Python 3 using Namespaces and ElementTree

Question: I am having problems generating a XML document using the ElementTree framework in Python 3. I tried registering the namespace before setting up the document. Right now it seems that I can generate a XML document only by adding the namespace to each element like a=Element("{full_namespace_URI}element_name") which seems tedious.
How do I setup the default namespace and can omit putting it in each element?
Any help is appreciated.
I have written a small demo program for Python 3:
    from io import BytesIO
    from xml.etree import ElementTree as ET

    ET.register_namespace("", "urn:dslforum-org:service-1-0")

    """
    desired output
    ==============
    <?xml version='1.0' encoding='utf-8'?>
    <topNode xmlns="urn:dslforum-org:service-1-0"">
        <childNode>content</childNode>
    </topNode>
    """

    # build XML document without namespaces
    a = ET.Element("topNode")
    b = ET.Element("childNode")
    b.text = "content"
    a.append(b)
    tree = ET.ElementTree(a)

    # build XML document with namespaces
    a_ns = ET.Element("{dsl}topNode")
    b_ns = ET.Element("{dsl}childNode")
    b_ns.text = "content"
    a_ns.append(b_ns)
    tree_ns = ET.ElementTree(a_ns)

    def print_element_tree(element_tree, comment, default_namespace=None):
        """
        print element tree with comment to standard out
        """
        with BytesIO() as buf:
            element_tree.write(buf, encoding="utf-8", xml_declaration=True,
                               default_namespace=default_namespace)
            buf.seek(0)
            print(comment)
            print(buf.read().decode("utf-8"))

    print_element_tree(tree, "Element Tree without XML namespace")
    print_element_tree(tree_ns, "Element Tree with XML namespace", "dsl")

Comment: this may help https://stackoverflow.com/a/4999510/9050514
Answer: I believe you are overthinking this.
Registering a default namespace in your code avoids the ns0: aliases.
Registering any namespaces you will use while creating a document allows you to designate the alias used for each namespace.
To achieve your desired output, assign the namespace to your top element:
a = ET.Element("{urn:dslforum-org:service-1-0}topNode")
The preceding ET.register_namespace("", "urn:dslforum-org:service-1-0") will make that the default namespace in the document, assign it to topNode, and not prefix your tag names.
<?xml version='1.0' encoding='utf-8'?>
<topNode xmlns="urn:dslforum-org:service-1-0"><childNode>content</childNode></topNode>

If you remove the register_namespace() call, then you get this monstrosity:
<?xml version='1.0' encoding='utf-8'?>
<ns0:topNode xmlns:ns0="urn:dslforum-org:service-1-0"><childNode>content</childNode></ns0:topNode>

Comment: Thank you very much, that was exactly the solution I was hoping for. Concise, easy and pythonic. I got confused by the "default_namespace" option in the write method which interferes with the rest. When it is used like you propose, you have to omit the default_namespace option in the write method.
Comment: Thank you! It was helpful for me. How it is possible to add the xml version and encoding values?
Comment: @BernardoTroncoso When calling `element_tree.write()` set `xml_declaration=True`
Comment: Yes thank you! I have figured that out! Thanks :)


Typescript function parameter type based on another parameter

Question: I'll try my best in describing our case, as I have no idea how to title this.
We have a package, which handles requesting our services/cloud functions (if for example service A needs something done by service B, it uses this package). This package exposes a function to call each service (for example for service auth, there would be a auth function exposed. We have a definition of available endpoints for each service and as this grows, it's harder to maintain since we were unable to figure out a way to type the parameters for the requests.
What I wanted to do, is simply have another type which would contain the parameter types for each endpoint of each service.
For example we have endpoint called getEmail, which would return email of a user and requires an id parameter.
Type params type would look like this:
type Params = {
  getEmail: {
    id: number;
  }
}

Very simplified version of the code:
type FunctionName = 'foo' | 'bar' | 'baz';

type FunctionParams = {
  foo: {
    myVar: number;
  };
  bar: {
    myVar: string;
  };
  baz: Record<string, number>;
};

declare const sendRequest: (...args: any[]) => any;

const callFunction = (
  fn: FunctionName,
  params: FunctionParams[typeof fn],
  //                     ^^^^^^^^^ What should I put here?
) => {
  sendRequest(fn, params);
};

callFunction('foo', {
  myVar: 'this should fail',
  // This is allowed, as the type of the second parameter is:
  // { myVar: number; } | { myVar: string; } | Record<string, number>) => void
  // I want it to be { myVar: number; }
});

Answer: You want to use generics! You can parameterize callFunction to bind fn to a specific string, and then use this to index FunctionParams:
function callFunction<T extends FunctionName>(
  fn: T,
  params: FunctionParams[T],
) {
  sendRequest(fn, params);
};

callFunction('foo', {
  myVar: 'this should fail', //BOOM!
});

Also, don't waste energy maintaining types. FunctionName can be as easy as
type FunctionName = keyof FunctionParams

with that, every time you add new params, FunctionName gets updated.
See also this playground
Comment: Generics did come to mind, we already use one for the return type but I had no idea typescript can automatically infer the generic, I thought they had to be either required or have a default value. This actually solves our problem nicely, it will just require quite a lot of time. Thanks a lot!
Comment: Thanks, @ddprrt, the example is clear! This way, the function _call_ is type-safe. But how would you achieve it when defining a function as a callback? So that it is clear when implementing it that if _fn_ is _'foo'_, _params_ will be _{ bar: number }_. E.g.:  _callback (fn: T, params: FunctionParams[T]) => { if (fn === 'foo' ) console.log(params.bar); // OK, will print some string }_
Answer: May be, you will need other kind of implementation, like following:

/**
 * API Functions Mapping.
 */
interface ApiFunctions {
  foo: {
    myVar: number;
  };
  bar: {
    myVar: string;
  };
  baz: Record<string, number>;
}

/**
 * Generic API Functions method.
 *
 * @param args
 * @returns
 */
declare function sendRequest(...args: any[]): any;

/**
 * Call {@link sendRequest} wrapper.
 *
 * @param fn
 * @param params
 */
function callFunction<T extends keyof ApiFunctions>(
  fn: T,
  params: ApiFunctions[typeof fn]
) {
  sendRequest(fn, params);
}

callFunction('foo', {
  myVar: 'this should FAIL', // <<<--- FAIL!
});

callFunction('bar', {
  myVar: 'this should WORK',
});

Probably the following article can help you: https://typeofnan.dev/how-to-make-one-function-argument-dependent-on-another-in-typescript/


fill javascript array from template engine

Question: i want to fill javascript array with raintpl like below code.problem in this line is , end of line. for example this line :
var theSummaries = new Array({loop="lastNewsTicker"}"{$value.subject}",{/loop});

after rendering in firefox create this result:
var theSummaries = new Array("Hello","How","Are",);

comma in after "Are" is my problem and that create error in define javascript array.
how to fill this array and resolv problem?
Answer: You could try something like this:
var theSummaries = new Array(
     {loop="lastNewsTicker"}
         "{$value.subject}"
         {if !$smarty.section.value.last}, {/if}
     {/loop}
     );


How to make sure every related Firebase data have completely loaded?

Question:
Disclaimer: I made this question and answer it myself because I need a "bigger board" to answer extended question of this post (as this whole explanation is never going to fit in comment

I'm having this data:
{
    "users" : {
        "randomUserId" : {
            "books" : {
                "booksId1" : true,
                "booksId2" : true
            }
        }
    },
    "books" : {
        "booksId1" : {
            "title" : "Awesome Book"
        },
        "booksId2" : {
            "title" : "Harry Potter"
        }
    }
}

I know I have to get users/randomUserId/books first then loop the dataSnapshot result to get all the book ids. Then I have to request detail data on each book id by using database reference that point to books/bookId?/. Something like this:
rootRef.child("users/" + user.getUid() + "books").addListenerForSingleValueEvent(new ValueEventListener() {
    ... onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot bookIdSnapshot : dataSnapshot) {
            rootRef.child("books/" + bookIdSnapshot.getValue(String.class))
                .addListenerForSingleValueEvent(...) {
                    // here i get the book detail data
                }
        }
    }
    ...
}

But with that code, each rootRef.child("books/"...) will be executed separately. So how can I know if the data have completely acquired?
Answer: Actually we only need an int or double to indicate if full all related data is completely loaded, but we also need to use those data right? So it's better to use Map or HashMap instead.
int bookTotalCount = -1;
HashMap<String, Book> bookMap = new HashMap<String, Book>();

rootRef.child("users/" + user.getUid() + "books").addListenerForSingleValueEvent(new ValueEventListener() {
    ... onDataChange(DataSnapshot dataSnapshot) {
        bookTotalCount = dataSnapshot.getChildrenCount();
        for (DataSnapshot bookIdSnapshot : dataSnapshot) {
            rootRef.child("books/" + bookIdSnapshot.getValue(String.class))
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    ... onDataChange(DataSnapshot dataSnapshot) {
                        Book book = dataSnapshot.getValue(Book.class);
                        bookMap.put(dataSnapshot.getKey(), book);

                        if (bookMap.size() == bookTotalCount) {
                            // you can place code here
                            // in here, all the related data will be completely loaded
                        }
                    }
                });
        }
    }
    ...
}

Note: Book is a custom object created to contain data/value inside books/bookId?


Tukey depth intuition

Question: In a bagplot, the inner polygon, called the bag, is constructed using the Tukey depth. What is the intuition of Tukey depth and how is it calculated? A simple two-dimensional example with ten to twenty data points may be helpful. Further, how often is is Tukey depth utilised and what alternative methods are similar?
Comment: Not the question, but I have found convex hulls easier to explain and in practice just as useful. The convex hull is just the smallest convex polygon including all data points, where including means that points can be vertices of the polygon or inside it. Then you can take the convex hull of points inside the polygon, and so on.  For mundane examples see https://www.statalist.org/forums/forum/general-stata-discussion/general/1517556-convex-hulls-on-scatter-plots
Answer: Bagplot
A bagplot is a method in robust statistics for visualizing two- or three-dimensional statistical data, analogous to the one-dimensional box plot.

Construction of a Bagplot
The bagplot consists of three nested polygons, called the "bag", the "fence", and the "loop".

The inner polygon, called the bag, is constructed on the basis of Tukey depth, the smallest number of observations that can be contained by a half-plane that also contains a given point. It contains at most $50\%$ of the data points. For more refer to Functional bagplot

An asterisk symbol (*) near the center of the graph is used to mark the depth median, the point with the highest possible Tukey depth.

The outermost of the three polygons, called the fence is not drawn as part of the bagplot but is used to construct it. It is formed by inflating the bag by a certain factor (usually 3). Observations outside the fence are flagged as outliers.
The observations that are not marked as outliers are surrounded by a loop, the convex hull of the observations within the fence.

Tukey Depth
Tukey depth is also known as location depth or halfspace depth. The Tukey depth is a measure of the depth of a point in a fixed set of points.
Given a finite set $S$ of $n$ points and a point $p$ in $\mathbb{R}^d$, the Tukey depth of $p$ is defined as the minimum number of points of $S$ contained in any closed halfspace with $p$ on its boundary. An equivalent definition is the minimum number of points of $S$ contained in any halfspace which also contains $p$.
Many different algorithms have been developed to compute the Tukey depth of a point. This problem is equivalent to the $\textit{maximum feasible subsystem} ~\textrm{(MAX FS)}$ problem which is a long-standing problem and has been extensively studied.

Calculating Tukey Depth
Suppose points in $S$ are in general position (no $d+1$ points of $S\cup\{p\}$ lie on a common hyperplane), an upper bound on the Tukey depth of $p$ can be obtained by selecting any non-trivial vector $v\in\mathbb{R}^d$ and computing the Tukey depth of $p\cdot v$ in the one-dimensional point set
$$S\cdot v=\{x\cdot v:~x {\in} S\}.\tag 1$$
If $v$ is the inner-normal of the boundary of the halfspace $\hbar$ that defines the depth value of $p$, then
$$\mathrm{depth}(p,S)=\mathrm{depth}(p\cdot v,S\cdot v).\tag 2$$
In $\mathbb{R}^1$, we rank the points $S\cup\{p\}$ starting with $0$ from both ends to the median, then the depth of $p$ is its rank. More generally, given any $k$-flat $f$ orthogonal to the boundary of $\hbar$ we have
$\mathrm{depth}(p,S)=\mathrm{depth}(p\cdot f,S\cdot f),\tag 3$$
where $p\cdot f$ is the orthogonal projection of $p$ onto $f,$ and $S\cdot f$ is the orthogonal projection of $S$ onto $f.$

From this paper Absolute approximation of Tukey depth: Theory and experiments
Theorem 1
Let $S$ be a set of $n$ points in general position in $\mathbb{R}^d,~S^1$  be a subset of $d-1$ elements chosen at random and without replacement from $S, v$ be the vector perpendicular to the plane containing $S^1$ and another point $p$, $σ$ be an integer such that
$$0\leqslant\sigma\leqslant\left\lfloor\frac nd\right\rfloor-1.$$
Then
$$\Pr{\{\mathrm{depth}(p\cdot v,S\cdot v)\leqslant\mathrm{depth}(p,S)+\sigma\}}\geqslant\frac{{\sigma+d-1}\choose{d-1}}{n\choose{d-1}}.$$
Under point/hyperplane duality, the selection of $v$ is equivalent to selecting a random vertex in an arrangement of hyperplanes in $d-1$ dimensions. This selection of $v$ approximates $\text{depth}(p, S)$ to within $\sigma$ provided that the vertex is contained in a particular pseudo-ball of radius $\sigma.$ Therefore the proof boils down to showing that the number of vertices of an arrangement in a pseudo-ball of radius $σ$ is sufficiently large. In particular, we show that the number of vertices in such a pseudo-ball is at least ${\sigma+d-1}\choose{d-1}.$

Calculating Tukey Depth in R
Rdocumentation - depth.halfspace to calculate Tukey Depth
<code># NOT RUN {
# 3-dimensional normal distribution
data <- mvrnorm(200, rep(0, 3),
                matrix(c(1, 0, 0,
                         0, 2, 0,
                         0, 0, 1),
                nrow = 3))
x <- mvrnorm(10, rep(1, 3),
             matrix(c(1, 0, 0,
                      0, 1, 0,
                      0, 0, 1),
             nrow = 3))

# default - random Tukey depth
depths <- depth.halfspace(x, data)
cat("Depths: ", depths, "\n")

# default exact method - "recursive"
depths <- depth.halfspace(x, data, exact = TRUE)
cat("Depths: ", depths, "\n")

# method "line"
depths <- depth.halfspace(x, data, method = "line")
cat("Depths: ", depths, "\n")
# }
</code>
Comment: Please could you additionally clarify if the following is correct: The Tukey depth can contain at most 50% of the data (the maximum a Tukey depth could be), and the minimum it could be is presumably just the point itself (100 / n%)? Conceptually (so excluding efficient means of doing it) the bag points are calculated by finding the Tukey depth of all points, and then the n / 2 points with the greatest depth correspond exactly to the points in the bag?
Comment: I hope this [link](https://books.google.co.in/books?id=V0DB_Pq4YbwC&pg=PA204&redir_esc=y#v=onepage&q&f=false) answers your question. Page No 205, 31.2 Functional Bagplot

Tukey depth intuition


Is PERL a must on LFS?

Question: I am building LFS, and I am wondering about building PERL. Call me narrow minded, but I really want to try going on a Linux system without PERL, and have everything implemented in Python.
Since I don't understand if perl is a dependency to another package, I am asking here...
Trying to remove Perl from Debian causes a havoc ... so I am wondering if there is a future for a linux system without Perl...
thanks,
Oz
Answer: Perl needed for the installation and test suites of several LFS packages. After install you can deinstall Perl.
Comment: Thanks, I guess reading a little bit more down the road I would have found it...
Answer: I don't quite understand what the issue is, even the latest version of debian does not come with perl pre-installed, or at least in it's default configuration.
Perl isn't a requirement in any shape form or fashion for a normal installation and dependencies on it are based on the packages that you want to use. If it requires perl then you'll either have to modify the source to utilise python or something else, or install perl.
Comment: @Dan, I just came back to this issue, and I think you are wrong. Check this out http://forums.debian.net/viewtopic.php?f=17&t=45804 according to this Perl is part of the required packages of Debian.
Comment: @Dan, for what I see so far Autoconf depends on Perl...
Comment: @Dan, Kbd depends on Perl too.
Comment: thanks for the answer Dan, I just wanted to hear another opinion. Perl is pretty popular and many packages like TEX and GNOME depended on it later. On my new system I don't want to have gnome, but I would need latex ... So we will see how things develop...
Comment: For a long time Solaris didn't come with Perl either. It was common for admins to add Perl first thing, though.
Comment: @Oz123 Happy to be corrected! I recalled somewhere in the deep dark corners of my memory that I had to install perl to get RT running on Debian 6.0 but I may have been mistaken or it may require something more than perl-base

Is PERL a must on LFS?


use conditional fields to change a label on a field

Question: Is it possible to change the label on a field using the conditional fields module.  What I want is to call a field "Effective Date" in one case and "Published Date" in another but have it be the same field.
is that possible with the conditional fields module or any other module?
Answer: I answered this similar D7 question How to change a fields label name based on another fields value?
<code>(function ($) {

   (function Forever(){ // we create a function called Forever

   var grabselectedtext = $('#edit-field-test-und option:checked').text();
   // this grabs the text of the field you want to depend on.

     $('label').each(function(){
      if ($(this).attr('for') == 'edit-body-und-0-value') {

         if (grabselectedtext != '- None -') {
           $(this).text(grabselectedtext);
         } else {
             $(this).text('Field Two');
           }
         }

     });

   setTimeout(Forever, 1);

   })();

})(jQuery);
</code>
You want to attach this JS code to a library. Then attach the library via a hook_form_alter and check for form ID and content type.


setInterval and setTimeout clear

Question: Is it necessary to clear the setInterval() and setTimeout() methods before doing next action. Because after doing some animation using setInterval and timeout, I am drawing shapes but it is not working...
So please help me...
Comment: can you share your code?
Comment: Use `clearInterval(interval)` and `clearTimeout(timeout)`. Or what is the problem?
Answer: After setTimeout you do not need to clearTimeout.
clearTimeout is used to cancel a setTimout that has not yet happened or to stop an setInterval with clearInterval.
A setTimeout that has executed is done.
For a better answer you need to provide more code as example.

setInterval and setTimeout clear


Confusion with the relation between Sobolev Hilbert space $H^{s}$ and its dual $H^{-s}$

Question: Let $s \in [0,\infty)$ and consider the Hilbert space $H^{s}:=W^{2,s}$. Its dual is usually denoted as $H^{-s}$.
Also, let $\langle, \rangle_s$ be the inner product of $H^2$. Then, according to the general theory of Hilbert space, there exists a unique $y \in H^{s}$ for each $T \in H^{-s}$ such that
\begin{equation}
T(x)=\langle x,y \rangle_s
\end{equation}
for all $x \in H^s$. Thus, there exists a isometric conjugate-linear surjection from $H^s$ to $H^{-s}$.
However, it is also known that $H^{s} \subset L^2 \subset H^{-s}$ and this inclusion is known to be "proper".
How should I understand these two seemingly contradictory facts? I am quite confused..
Comment: the same way that $\mathbb N \subset \mathbb Z$ but there is a bijection between them, $(0,1) \subset \mathbb R$ but there is a homeomorphism between them (so bijection plus structure-preserving) etc; here the structure is the norm but so what...
Comment: @Conrad oh...thank you...
Answer: Well, all separable Hilbert spaces are isomorphic to $\ell^2(\mathbb{N})$, so there is no contradiction in one Hilbert space being a proper subset of another yet being isomorphic to it.
To see how the inclusions $H^s = H^{-s}$ implied by Hilbert space theory and the natural inclusion $H^s \subset H^{-s}$ are different, compute each one. Let $y \in H^s$. Note that the natural inclusion from $H^s$ to $H^{-s}$ is defined (up to a complex conjugate on $y$ depending on the situation) by
$$\langle x, y \rangle_{H^s, H^{-s}} = (x, y)_{L^2}.$$
The image of $y$ under the Hilbert space inclusion, call it $T \in H^{-s}$, satsifies
$$\langle x, T \rangle_{H^{s}, H^{-s}} = (x, y)_{H^s} = (\langle \xi \rangle^{s}\hat{x}, \langle \xi \rangle ^{s}\hat{y})_{L^2} = (x, \mathcal{F}^{-1}(\langle \xi \rangle ^{2s}\hat{y}))_{L^2}.$$
Hence the Hilbert space isomorphism is $y \mapsto T = \mathcal{F}^{-1}(\langle \xi \rangle ^{2s}\hat{y})$. The natural inclusion just maps $y$ to $y$, whereas the isomorphism from $H^s$ to $H^{-s}$ maps $y$ to $\mathcal{F}^{-1}(\langle \xi \rangle ^{2s}\hat{y})$, which is a distribution in $H^{-s}$.
Comment: Thank you for your clarification too..


If $(X_{i})$ are i.i.d nonnegative with $\mathbb{P}(X_{1}=0)>0$, show that $\lim \sqrt[n]{X_{1}\dots X_{n}}=0$ (surely).

Question: I am working on an exercise as follows.

Given i.i.d non-negative random variables $(X_{i})_{i=1}^{\infty}$ with $\mathbb{P}(X_{1}=0)>0$. Show that $$\lim_{n\rightarrow\infty}\sqrt[n]{X_{1}\dots X_{n}}=0.$$

I can show the almost surely convergence but the exercise asks for "surely" convergence. For the almost surely convergence, it is easy to see that $\sum_{n=1}^{\infty}\mathbb{P}(X_{n}=0)=\infty$, and thus using independence and Borel-Cantelli lemma, we have $\mathbb{P}(X_{n}=0\ \text{i.o.})=1$. Hence, with probability $1$, for every $n\geq 1$, there exists $k\geq n$ such that $X_{k}=0$, which implies that $X_{1}\dots X_{k}\dots X_{n}=0$ for all $n\geq k$.

However, how to extend this result to all $\omega\in\Omega$ instead of just almost surely convergence? In other words, I also have to show that $Y_{n}\rightarrow 0$ for all $\omega$ such that  $X_{n}(\omega)\neq 0$ eventually. I've been trying to show that in fact $\{X_{n}\neq 0\ e.v.\}=\varnothing$, but I do not see a doable way to achieve this.
Can this "surely convergence" be true? Is there an example where such a "weighted product" converges almost surely but not convergence surely?
Comment: Whether it's $\emptyset$ or not depends on the probability space. For example you can take the countable product of $([0,1],\lambda,\mathcal{B})$ with $X_{n}=\mathbf{1}_{[0,\frac{1}{2}]}$. Then the element(sequence) $(0,0,...,)$ is in the set $\{X_{n}(0)\neq 0\,\text{e.v.}\}$. Other than this, just for an intuitive example, if $X_{n}$ are coin tosses, then one can have that all coin tosses are heads but this will happen with probability $0$.
Comment: Is this even true? What is the definition of this "sure" convergence when we're working with random variables?
Comment: @BenjaminWang I guess that it is pointwise in the sense that $|Y_{n}(\omega)-0|\rightarrow 0$ for $\omega\in\Omega$, where $Y_{n}$ is that weighted product.
Comment: @Mr.GandalfSauron I just see you second example. Nice Example. Wanna post this counterexample as an answer to this post? So at least I can appreciate it? :)
Comment: @Mr.GandalfSauron wait a second. I think that the example with $X_{n}:=\mathbf{1}_{[0,\frac{1}{2}]}$ does not fit in this question because $X_{n}$s are not independent. For example, if I know that $X_{n}(\omega)=1$ for some $n$, then I know that $X_{n}(\omega)=1$ for all $n$.
Comment: @JacobsonRadical That's why I told to take the inifinite product. So infact, you are dealing with $X_{n}(\omega_{1},\omega_{2},....)=\mathbf{1}_{[0,\frac{1}{2}]}(\omega_{n})$. I did not write this out in full and thought it was implied.
Comment: The $X_{n}$'s or rather the distributions $\mu_{n}$'s are independent if and only if the joint law of the sequence $(X_{1},X_{2}...)$ is given by the product measure.
Comment: @Mr.GandalfSauron oh sorry. I didn't see you mentioned "countable product". Nice example!
Comment: Yeah sorry, maybe I should have used bold font. It's just that you define $X_{n}=\mathbf{1}_{[0,1]}\circ \pi_{n}$. Then they are independent by definition of product measure. I'll write up an answer.
Comment: @Mr.GandalfSauron Really nice. Thank you very much.
Comment: This question is weird, you didn't actually need to use the $n$th root - just the product itself is zero. In fact, you didn't even need the full strength of the "infinitely often"; this sequence is almost surely eventually constant equal to zero, if I'm not mistaken
Comment: @FShrike Since $\mathbb{P}(X_{1}=0)>0$ and their are i.d., we see that $\sum_{n=1}^{\infty}\mathbb{P}(X_{n}=0)=\infty$. Hence, by the independence and the second Borel lemma, we see that $\mathbb{P}(X_{n}=0\ i.o.)=1$. So it is actually almost surely equals to zero infinitely often. That's why the product is eventually zero.
Comment: you didn't understand my comment.
Comment: @FShrike I understood. I also realized that the $n^{th}$-root was not used. So, in fact, my several initial attempt was to bound $Y_{n}$ by $\dfrac{X_{1}+\dots+X_{n}}{n}$ and trying to use Kronecker's lemma or so, but I didn't manage to get anything. What I replied was to say that I do used the strength of the infinitely often, since I do not see why the sequence is not almost surely eventually $0$, instead, for me, it is almost surely infinitely often $0$.  That is what I meant :)
Comment: @FShrike I also feel really weird about this exercise.
Answer: Whether it's $\emptyset$ or not would depend on the probability space and how the random variables (as functions) are defined.
For example, take the countable product of $([0,1],\mathcal{B}([0,1]),\lambda)$.
If you define $X_{n}:[0,1]^{\mathbb{N}}\to\Bbb{R}$ by  $X_{n}=\mathbf{1}_{[0,\frac{1}{2}]}\circ\pi_{n}$ . Then $X_{n}$ are iid. But see that $(0,0,...)\in\{X_{n}\neq 0\ e.v.\}$ (i.e. the $0$ sequence) is in this set and hence it's not empty.
Intuitively, in an infinite coin tossing scenario, you "CAN" end up with all tosses being heads but it will happen with $0$ probability measure.
Comment: $\otimes^{\Bbb{N}}\lambda(X_{n}=0)=\lambda(\{\mathbf{1}_{[0,\frac{1}{2}]}=0\})=\lambda([\frac{1}{2},1])$ @JacobsonRadical
Comment: Do you mind elaborating on why $\lambda(X_{n}=0)>0$ in this case?
Comment: They are indicators. In fact $X_{n}$ are iid $Bern(\frac{1}{2})$. In fact for any borel set of measure $\frac{1}{2}$, you have that $\mathbf{1}_{A}$ is a $bern(\frac{1}{2})$ variate on the probability space $([0,1],\lambda,\mathcal{B})$. @JacobsonRadical
Comment: Beautiful. Thank you


Remote Administration of Server Core WS2008R2 via MMC on Win7

Question: I have searched and found a couple threads that deal with this topic, but they never quite got to solution. They just seemed to peter out.
I have executed the much recommended:
netsh advfirewall firewall set rule group="remote administration" new enable=yes
but I still get "Access is denied".
None of the proposed solutions get beyond this step, though no one that initiated these threads said "that did it!"
Steps that I've taken:
1.Install DNS role on ws2008 r2 server core on server box.
2.Install RSAT for Win7 on the Win7 box.
3.Assigned static IP to the server.
4.Install the Windows Feature for DNS snap-in for MMC.
5.Attempt to target it to the server box.
At this point I get the "Access is denied" error and...well...(flops on the floor)
Sure would appreciate sufficient next steps to admin the box remotely.
Thanks, Greg
Comment: Something occurs to me.  The server core box and the windows 7 box are not in the same domain.  So - how would the mmc session establish identity to the server?
Comment: I found a blog posting by John Howard @ MSFT regarding setting up Hyper-V remote admin in a similar setup.  He went through several variants of security model.  None of them match my situation.  He did workgroup authentication and domain authentication, but I have a server in a workgroup and a client in a domain.  I think this is the root of my problem.  Thoughts?
Comment: "how would the mmc session establish identity to the server" runas? or join the domain? or setup a domain trust?
Answer: You can get Hyper-V management console to work from domain client to a non-domain hyper-v host, I've done it several times and this guide with HVRemote from the very same John Howard actually gives you all the variants.
...do remember to launch the management tool "as another user.." and enter a valid administrative user that exists locally on the server (and/or use the HVRemote guide to set it up properly).
Answer: Greg,
I don't think it is possible for a workgroup machine to use MMC to a server core instance.  MMC uses Kerberos authentication to verify if the user (and machine) connecting to the server can be trusted.  Since a workgroup machine has no kerberos tickets, there is no way to prove the machine's identity to the server.  Ideally, you want both to be in the same domain.  If that is not possible, then you will need to establish a cross-domain trust between the domains of the two machines.
Also, be sure that the client architecture matches the server type (2008 R2 tools should be installed on a Win 7 64-bit OS), since some 32-bit MMC tools do not work on 64-bit servers.


Alternative to table tennis glue?

Question: The rubber of my table tennis bat is starting to peel off one "corner".
Are there any household products I can use to stick it back that will not damage the rubber too much?
Comment: I would use a spray adhesive. I go into that in [this answer](http://sports.stackexchange.com/a/2898) about changing the rubber of a paddle.
Answer: Many players I know, including myself, use Rubber Cement.  It's cheaper than the "Chack" you get from Stiga, Butterfly, etc, and in my experience, seems to work just as well.  It also doesn't leave any bits of the rubber on the blade when you need to remove it.
Just spread a thin layer on the blade and rubber surface, let it dry, then attach them.  These are the 2 that I've been using for many years.
Comment: One of the reasons TT recently moved to water based glue was to eliminate the exposure of players to harmful vapours from glues containing VOC's.
The glues you have recommended are against the rules of Table Tennis and we therefore shouldn't be advocating their use

Alternative to table tennis glue?


Focus Events (or lack thereof)

Question: I am having a hard time understanding the focus events for Entry and Textbox fields in Python version 3 using Tk. I eventually need to validate an Entry box on lost focus if I click a radio option or a button.
If you run the code below then (which serves only to demonstrate Focus issues not the validation i require elsewhere), place the cursor in either of the top row Entry boxes and click between the other widgets, the only time FocusIn and Focus out events occur are on the widgets that accept input ie Text/Entry boxes.
Clicking the button or the radio options, the cursor remains in the Entry or Textbox widgets. Why when i have clearly focused on a radio option or the button.
I have tried .bind FocusIn/Out events and still no joy. if anyone has an explanation I  would be intrigued to know why and possibly how i can overcome it.
<code>from tkinter import *

root = Tk()

root.title("My Widgets")
root.update_idletasks()
root.geometry("350x200+10+300")
root.attributes("-toolwindow",1)
root.resizable(width=FALSE, height=FALSE)
root.config(bg="blue")

# function below output to the console and label the focus results
def Validate(a,b,c,d,e,f,g,h):

    text = g + ' on ' + h
    lblOutputVar.set(text)
    print(f,g,h)
    return True

var = IntVar()
lblOutputVar = StringVar()

vcmd=(root.register(Validate),'%d','%i','%P','%s','%S','%v','%V','%W')

entryOne = Entry(root, name = 'entryBoxOne')
entryOne.config(validate = 'all',vcmd=vcmd)
entryOne.grid(row=1, column=1,padx=(0,0),pady=(10,10),ipady=(1), sticky=E+W)

entryTwo = Entry(root, name = 'entryBoxTwo')
entryTwo.config(validate = 'all',vcmd=vcmd)
entryTwo.grid(row=1, column=2,padx=(10,0),pady=(10,10),ipady=(1), sticky=E+W)

txtBox = Text(root, name = 'textBox', width=10, height=1, takefocus = 0)
txtBox.grid(row=5, column=1, sticky=E+W)

aButton = Button(root, text = 'Click Me!', takefocus=1)
aButton.grid(row=5, column=2)

lblOutput = Label(root, name = 'labelOutput', width=20, height=2, textvariable=lblOutputVar)
lblOutput.grid(row=10, column=1, columnspan =2, pady=(5,0), sticky=E+W)

radioOne = Radiobutton(root, anchor = 'w', text = 'One', variable = var, value = 1, takefocus = 1)
radioOne.grid(row=2, column=1, sticky=E+W)
radioTwo = Radiobutton(root, anchor = 'w', text = 'Two', variable = var, value = 2, takefocus = 1)``
radioTwo.grid(row=3, column=1, sticky=E+W)

root.mainloop()
</code>
Answer: The explanation is simply that tkinter buttons and radiobuttons aren't given focus when you click on them. If you want that to happen, you need to set up a binding to explicitly give them the focus.
Your other option is to use a ttk radiobutton which does get focus. It's unfortunate that the two different radiobuttons have different behavior.
Comment: OK, thank you for clearing that up. I suppose it is just one of those idiosyncrasies that must be circumnavigated. It intrigues me that the option to let a button 'takefocus' is available for keyboard navigation on a form, weird n'est-ce pas.
Comment: @DrJeep: you need to be able to interact with a button if you can't use a mouse, and the only way to do that is if the button can get keyboard focus. It's an accessibility thing. What I think is weird is for a button-like widget to take focus when you click on it -- that's one area where I think ttk got things very wrong.

Focus Events (or lack thereof)


My AffineTransformation has gaps between pixels

Question: First I want to say that I am not using the AffineTransform class that Oracle made. I am doing my "own" AffineTransform. Why? I am making a pixel engine and I couldn't find any other solution. So I began looking a bit at Wikipedia and how matrices work. I then watched javidx9's video at how to implement it. I got it working. Rotation,reflection,shear etc worked fine but I got a pixel gap. Javidx9 mentioned this in his video and he solved the problem. I tried his solution but I got an "ArrayOutOfIndex error" when doing reflection,rotation and shear. I have been trying to solve the issue but I just can't understand what I am doing due to the complex code. That's why I am here! I need your help!
I got it working like this (with gaps):
GIF
The code I use for the transformation with gaps is the following:
Render the image with a matrix
        for (int x = 0; x < image2.getWidth(); x++)
    {
        for (int y = 0; y < image2.getHeight(); y++)
        {

            matrixLast.forward(x,y);
            renderer.setPixel((int)matrixLast.getNx(),(int) matrixLast.getNy(), image2.getPixels()[x+y*image2.getWidth()], null, 255);
        }
    }

However as you can see you need more information than that. I also made a "Matrix3x3" class, here are the important methods/functions:
    public static void Identity(Matrix3x3 mat)
{
    mat.matrix[0][0] = 1.0f; mat.matrix[1][0] = 0.0f; mat.matrix[2][0] = 0.0f;
    mat.matrix[0][1] = 0.0f; mat.matrix[1][1] = 1.0f; mat.matrix[2][1] = 0.0f;
    mat.matrix[0][2] = 0.0f; mat.matrix[1][2] = 0.0f; mat.matrix[2][2] = 1.0f;
}

public static void Translate(Matrix3x3 mat, float ox, float oy)
{
    mat.matrix[0][0] = 1.0f; mat.matrix[1][0] = 0.0f; mat.matrix[2][0] = ox;
    mat.matrix[0][1] = 0.0f; mat.matrix[1][1] = 1.0f; mat.matrix[2][1] = oy;
    mat.matrix[0][2] = 0.0f;    mat.matrix[1][2] = 0.0f;    mat.matrix[2][2] = 1.0f;
}

public static void Rotate(Matrix3x3 mat, float degrees)
{
    double fTheta = Math.toRadians(degrees);
    mat.matrix[0][0] = (float)Math.cos(fTheta);  mat.matrix[1][0] = (float)-Math.sin(fTheta); mat.matrix[2][0] = 0.0f;
    mat.matrix[0][1] = (float)Math.sin(fTheta); mat.matrix[1][1] = (float)Math.cos(fTheta); mat.matrix[2][1] = 0.0f;
    mat.matrix[0][2] = 0.0f;                     mat.matrix[1][2] = 0.0f;        mat.matrix[2][2] = 1.0f;
}

public static void Scale(Matrix3x3 mat, float sx, float sy)
{
    mat.matrix[0][0] = sx;   mat.matrix[1][0] = 0.0f; mat.matrix[2][0] = 0.0f;
    mat.matrix[0][1] = 0.0f; mat.matrix[1][1] = sy;   mat.matrix[2][1] = 0.0f;
    mat.matrix[0][2] = 0.0f;    mat.matrix[1][2] = 0.0f;    mat.matrix[2][2] = 1.0f;
}

public static void Shear(Matrix3x3 mat, float sx, float sy)
{
    mat.matrix[0][0] = 1.0f; mat.matrix[1][0] = sx;   mat.matrix[2][0] = 0.0f;
    mat.matrix[0][1] = sy;   mat.matrix[1][1] = 1.0f; mat.matrix[2][1] = 0.0f;
    mat.matrix[0][2] = 0.0f;    mat.matrix[1][2] = 0.0f;    mat.matrix[2][2] = 1.0f;
}

public static void Reflection(Matrix3x3 mat) {

    mat.matrix[0][0] = -1.0f; mat.matrix[1][0] = 0;   mat.matrix[2][0] = 0.0f;
    mat.matrix[0][1] = 0;   mat.matrix[1][1] = 1.0f; mat.matrix[2][1] = 0.0f;
    mat.matrix[0][2] = 0.0f;    mat.matrix[1][2] = 0.0f;    mat.matrix[2][2] = 1.0f;
}

public void forward(float inX, float inY) { //Ingen aning vad denna gör
    float outX,outY;

    outX = (inX * this.matrix[0][0]) + (inY * this.matrix[1][0]) + this.matrix[2][0];
    outY = (inX * this.matrix[0][1]) + (inY * this.matrix[1][1]) + this.matrix[2][1];

    this.ny = outY;
    this.nx = outX;
}

In the "render" code as you can see above you see that there is a "matrixLast" in there. Here is how I got that one (they are defined elsewhere as for example: "Matrix3x3 name;"):
    Matrix3x3.Translate(matrixFinal3, -image2.getWidth() / 2, -image2.getHeight() / 2);

    Matrix3x3.Rotate(matrixFinal2,rotation);            //Rotation och shear är det något fel på.

    matrixSum = Matrix3x3.multiplyMatrix(matrixFinal2, matrixFinal3);
    Matrix3x3.Translate(matrixFinal, 700, 600);

    matrixLast = Matrix3x3.multiplyMatrix(matrixFinal, matrixSum);

    matrixInv = Matrix3x3.invert(matrixLast);

The issue I am having is basically that I am trying to fix the "gaps". I tried Javidx9's solution which changes the render code to the following (Utils.getMaximum() returns the largest number and the opposite for Utils.getMinimum() ):
    float ex,ey,sx,sy;

    matrixLast.forward(0f, 0f);
    sx = matrixLast.getNx();
    ex = matrixLast.getNx();
    sy = matrixLast.getNy();
    ey = matrixLast.getNy();

    matrixLast.forward((float) image2.getWidth(), (float) image2.getHeight());
    sx = Utils.getMinimum(sx, matrixLast.getNx()); sy = Utils.getMinimum(sy, matrixLast.getNy());
    ex  = Utils.getMaximum(ex, matrixLast.getNx()); ey = Utils.getMaximum(ey, matrixLast.getNy());

    matrixLast.forward(0f, (float) image2.getHeight());
    sx = Utils.getMinimum(sx, matrixLast.getNx()); sy = Utils.getMinimum(sy, matrixLast.getNy());
    ex  = Utils.getMaximum(ex, matrixLast.getNx()); ey = Utils.getMaximum(ey, matrixLast.getNy());

    matrixLast.forward((float) image2.getWidth(), 0f);
    sx = Utils.getMinimum(sx, matrixLast.getNx()); sy = Utils.getMinimum(sy, matrixLast.getNy());
    ex  = Utils.getMaximum(ex, matrixLast.getNx()); ey = Utils.getMaximum(ey, matrixLast.getNy());

    for(int x = (int)sx; x < (int)ex; x++) {

        for(int y = (int)sy; y < (int)ey; y++) {

            matrixInv.forward(x,y);

            renderer.setPixel(x, y, image2.getPixels()[(int)(matrixInv.getNx() + 0.5) + (int)(matrixInv.getNy() + 0.5) * image2.getWidth()], null, 255);

        }
    }

He also added an invert method:
    public static Matrix3x3 invert(Matrix3x3 matIn) {

    float det = matIn.matrix[0][0] * (matIn.matrix[1][1] * matIn.matrix[2][2] - matIn.matrix[1][2] * matIn.matrix[2][1]) -
            matIn.matrix[1][0] * (matIn.matrix[0][1] * matIn.matrix[2][2] - matIn.matrix[2][1] * matIn.matrix[0][2]) +
            matIn.matrix[2][0] * (matIn.matrix[0][1] * matIn.matrix[1][2] - matIn.matrix[1][1] * matIn.matrix[0][2]);

        float idet = 1.0f / det;
        Matrix3x3 matOut = new Matrix3x3();

        matOut.matrix[0][0] = (matIn.matrix[1][1] * matIn.matrix[2][2] - matIn.matrix[1][2] * matIn.matrix[2][1]) * idet;
        matOut.matrix[1][0] = (matIn.matrix[2][0] * matIn.matrix[1][2] - matIn.matrix[1][0] * matIn.matrix[2][2]) * idet;
        matOut.matrix[2][0] = (matIn.matrix[1][0] * matIn.matrix[2][1] - matIn.matrix[2][0] * matIn.matrix[1][1]) * idet;
        matOut.matrix[0][1] = (matIn.matrix[2][1] * matIn.matrix[0][2] - matIn.matrix[0][1] * matIn.matrix[2][2]) * idet;
        matOut.matrix[1][1] = (matIn.matrix[0][0] * matIn.matrix[2][2] - matIn.matrix[2][0] * matIn.matrix[0][2]) * idet;
        matOut.matrix[2][1] = (matIn.matrix[0][1] * matIn.matrix[2][0] - matIn.matrix[0][0] * matIn.matrix[2][1]) * idet;
        matOut.matrix[0][2] = (matIn.matrix[0][1] * matIn.matrix[1][2] - matIn.matrix[0][2] * matIn.matrix[1][1]) * idet;
        matOut.matrix[1][2] = (matIn.matrix[0][2] * matIn.matrix[1][0] - matIn.matrix[0][0] * matIn.matrix[1][2]) * idet;
        matOut.matrix[2][2] = (matIn.matrix[0][0] * matIn.matrix[1][1] - matIn.matrix[0][1] * matIn.matrix[1][0]) * idet;

    return matOut;

}

Here is when he totally lost me, it got too complex and I don't know how to troubleshoot it due to me not knowing exactly what the code does. His solution gave me an ArrayOutOfIndexerror but only when doing reflections,rotations and shear:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -9 out of bounds for length 589824
at com.dubstepzedd.gameengine.gamemanager.GameManager.render(GameManager.java:112)
at com.dubstepzedd.gameengine.GameContainer.loop(GameContainer.java:94)
at com.dubstepzedd.gameengine.GameContainer.run(GameContainer.java:48)
at java.base/java.lang.Thread.run(Thread.java:832)
at com.dubstepzedd.gameengine.GameContainer.start(GameContainer.java:40)
at com.dubstepzedd.gameengine.gamemanager.GameManager.main(GameManager.java:178)

I have no clue why this is happening. If someone has any advice on how to approach the "gap" situation differently or what is wrong with the code please comment or post a question
UPDATE
Here is the how it acts when I added getPixel():
Link to GIF
The code added to the Image class:
public int getPixel(int x, int y) {

    if(0 <= x && x < this.width && 0 <= y && y < this.height) {
        return pixels[x+y*this.width];
    }
    else {
        return Color.black.getRGB();
    }
}

Comment: @akuzminykh Good question! What I mean by that is that I have a difficult time understanding how the invert works. I also don't really get the Borderbox solution that was provided by javidx9. It's difficult for me to understand the logic behind his solution. Implementing his solution into Java isnt difficult. However troubleshooting his solution was not easy.
Comment: I also don't understand how the solution works for him but not for me? Especially when the first with "gaps" worked.
Answer: You are trying to access a pixel that's outside the original image here:
image2.getPixels()[(int)(matrixInv.getNx() + 0.5) + (int)(matrixInv.getNy() + 0.5) * image2.getWidth()]

I would recommend adding a method to your image class that returns the pixel at a x/y position, if x and y are in range, and a default color if not:
// add to image class:
int getPixel(int x, int y) {
    if (0 <= x && x < width && 0 <= y && y < height) {
        return pixelData[x + width*y];
    } else {
        return backgroundColor;
    }
}

Use this method instead of array lookups - for example
for(int x = (int)sx; x < (int)ex; x++) {
    for(int y = (int)sy; y < (int)ey; y++) {
        matrixInv.forward(x,y);
        int pixel = image2.getPixel((int)(matrixInv.getNx() + 0.5, (int)(matrixInv.getNy() + 0.5))
        renderer.setPixel(x, y, pixel);
    }
}

Comment: I got it working, I misstyped as always... Thanks again Joni! You save me every single time... It's almost embarrasing hehe
Comment: I added so that when a pixel is outside the screen it will be ignored, however that was implemented a long time ago. Is that what you mean?
Comment: It works for scaling, why wouldn't it work for rotation? I mean if scaling doesnt output the error why would rotation do it?
Comment: when you rotate, the rectangle is no longer axis-aligned and you are going to access pixels outside the image. I guess your `Utils.getMaximum` and `Utils.getMinimum` methods do the correct thing as long as the rectangle stay axis-aligned but you need a different solution for rotation
Comment: My Utils.getMaximum is very simple: if(number1 > number2) return number1; else return number2;
Comment: How would you adapt it to rotation? Any websites to recommend?
Comment: Perhaps you misunderstood. The problem is not that the pixels will be outside of *screen*. The problem is that the pixel you are looking up in the rotated image is out of bounds of the *source image*. That's why you need to add a function to look up pixels to your image class.
Comment: I get that the issue isn't that the pixels are outside the Frame. Its the image pixel data that is the issue as you said. So You are saying that the solution is to loop through the pixels in a function? How will I know which pixel to draw? I am confused. If I make a function to loop through the image pixels, what purpose Will it have? I still dont know which pixel to draw do I?
Comment: You draw the pixel `(matrixInv.getNx(), matrixInv.getNy())` on the pixel `(x,y)` - you already have code that does this. I'm not really sure what you find confusing
Comment: But if I get the pixel matrixInv.getNx(),matrixInv.getNy() of the source image I will get ArrayOutOfIndex right? The code in my question has the following in the "render" code: image2.getPixels()[matrixInv.getNx() + matrixInv.getNy() * image2.getWidth()]. This gets the color value of the pixel at matrixInv.getNx() and matrixInv.getNy(). Am I not doing what you are asking?
Comment: Never mind, I am tired. I see that you have an example in your answer. I am sorry :/ I will try it out!
Comment: I added the getPixel() code you provided and used it. However it gave a weird result. I will update the question
Comment: That's an interesting result, it looks like there may be something wrong with the rotation matrix or with how it's inverted. Inverting a rotation matrix is easy: it's just a rotation in the opposite direction
Comment: I got the invert method from javidx9's github. It was difficult to understand so I just copied it: https://github.com/OneLoneCoder/olcPixelGameEngine/blob/master/Videos/OneLoneCoder_PGE_SpriteTransforms.cpp. I try to understand everything after, it's easier to understand when it works. I get the same result with Identity etc.


Splitting Instrumental Variable into Discrete Groups

Question: I have been reading Mostly Harmless Econometrics and have been won over by the virtues of splitting my instrument into groups. In particular, I have a continuous variable as an instrument representing percent and would like to split it into 3 groups (0-33%, 34-66%, 67-100%) and use dummies to indicate each group, i.e. if the continuous variable is between 0-33% $\text{dummy}_1=1$ and the others 0, if between 34-66% $\text{dummy}_2=1$ etc. If I run 2SLS would I need to drop the first group (0-33%) in the first stage? And if so, what would be the interpretation of the constant in the first stage and reduced form?
Also would I be able to over identification test? What would be the number of over identification restrictions?
Yes split into 3 groups and use a dummy for each one as a separate instrument. In particular a higher percentage represents a higher randomised treatment intensity, so this would be 3 randomised treatment intensities. I was hoping I could include all 3 in the first stage without facing linear dependence.
Comment: Could you make your title more explicit and descriptive? It seems your question pertains to trichotomizing an item/score (splitting into 3 groups).
Comment: Sorry, is this any clearer? Yes split into 3 groups and use a dummy for each one as a separate instrument. In particular a higher percentage represents a higher randomised treatment intensity, so this would be 3 randomised treatment intensities. I was hoping I could include all 3 in the first stage.
Answer: Say you have your three group dummies $z_1$, $z_2$, and $z_3$. When you run your regressions, you can either keep all dummies and drop the constant or you exclude $z_1$ (or any of the other dummies; in the following suppose we drop $z_1$ if any). The first stages and reduced forms would be
$$
\begin{align}
D &= \beta_1 z_1 \, + \beta_2 z_2 + \beta_3 z_3 + e \\
D &= \beta_1 \quad + \pi_2 z_2 + \pi_3 z_3 + u \\ \newline
Y &= \phi_1 z_1 \, + \phi_2 z_2 + \phi_3 z_3 + \epsilon \\
Y &= \phi_1 \quad + \rho_2 z_2 + \rho_3 z_3 + \eta
\end{align}
$$
where I supress $i$ subscripts for simplicity. Note that $\beta_1$ is the same estimate in both first stages. If you omit $z_1$, this will be your intercept and $\pi_2 = \beta_2 - \beta_1$ will then be the deviation from the reference category (which is $z_1$). The same is true for the reduced form.
If you want a simple (but meaningless) example of this, here is some Stata code to try out:
<code>webuse nlswork
gen d1 = (ttl_exp < 1)
gen d2 = (1 <= ttl_exp & ttl_exp < 5)
gen d3 = (5 <= ttl_exp)
bysort idcode: gen n = _n
keep if n==1
reg ln_wage d1 d2 d3, nocons
reg ln_wage d2 d3
reg collgrad d2 d3
reg collgrad d1 d2 d3, nocons
ivreg ln_wage (collgrad = d2 d3), first
</code>
When you discretize the instrument you gain efficiency but there is also a potential bias which gets introduced when some of your instruments end up being weak. This bias-efficiency trade-off should be kept in mind when splitting continuous instruments into group dummies.
Overidentification tests like the Sargan test in this context don't work because having discretized your instrument you already reject the idea of a linear constant effects model which is an underlying assumption of overidentification tests. Also overidentification tests are rarely useful in general due to their low power and the assumption that at least one instrument is valid. Given that your instruments all come from the same data generating process either all your instruments are valid or all of them are invalid but this is something the test can't tell you.
A side note: discretizing your instrument in this way kills the nice interpretation properties of the second stage. If you use the continuous instrument, the coefficient of the endogenous variable in the second stage can be interpreted as the causal effect of the endogenous variable due to a one percentage point increase in the instrument. This interpretation gets lost with multiple instruments because the coefficient of your endogenous variable becomes a weighted average of the IV estimates from each separate instrument.


f(x) increasing or decreasing

Question:
Let $f(x) = x + 2x^2\sin(1/x)$, with  $x\ne0$,
  and $f(x)= 0$ when  $x= 0$. Detrmine if $f(x)$ is increasing or decreasing at $x= 0$

my attempt: using first principle it is easy to see that $f'(0)=1$ however if we find derivative of $f(x)$ as $f'(x) = 1+4x\sin(1/x) -2\cos(1/x)$ ,as $f'(1/(2k\pi)) = -1$ for integral $k$ hence there is no interval around $0$ where $f(x)$ is increasing. So it shouldn't make sense that $f(x)$ is increasing at $x=0$. so how should we handle this? Any help is greatly appreciated.
Comment: What is the definition of increasing at a point? See [here](https://math.stackexchange.com/questions/364576/can-a-function-be-increasing-at-a-point?rq=1)
Answer: $$f(0)=0$$ and
$$f(\epsilon)=\epsilon+2\epsilon^2\sin\frac1{\epsilon}=\epsilon\left(1+2\epsilon\sin\frac1\epsilon\right)>f(0)$$ for all $\epsilon<\frac12$. Similarly $f(-\epsilon)<f(0)$ and the function is indeed growing at $x=0$.

f(x) increasing or decreasing


Create new column if DataFrame contains specific string

Question: I have one column in the DataFrame that is a name. Inside this name there are patterns that I want to locate, and create a category in other column of same DataFrame. For example :
Name

name first RB LA a
name LB second
RB name third
name LB fourth

I want the name with the same pattern to be in the same category, displayed in the other column
What I want :
       Name                  Example

name first RB LA a          Round Blade category
name LB second              Long Biased category
RB name third               Round Blade category
name LB fourth              Long Biased category

I have a DataFrame, not a list, there are several other columns in it. And there are not only two categories, but several ones.
What I have Tried :
df.loc[df['Name']=="RB", 'Example'] = "RB category"

But it does not work since it must be an exact match
Another attempt :
if df[['Name'].str.contains("RB")] :
    (...)

But it gives me error :
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried to add to .bool() or .any(), but or the error persist or nothing happens when I run the line.
Thank you.
Comment: _But it does not work since it must be an exact match_ Why not use `.str.contains()`, then?
Comment: The str.contains() does find the matches I am looking for, But I don't know how to use this function to create another column with the category I want, hence I came with this question. I tried through If conditional but it gave me an error as displayed above.
Comment: You need to use `.loc[]`, like you did in the first snippet.
Answer: You could use pandas.Series.str.extract to achieve the desired output

import numpy as np
import pandas as pd

df = pd.DataFrame({
    "Name": ["name first RB LA a", "name LB second", "RB name third", "name LB fourth"]
})
df["Example"] = df["Name"].str.extract("(LB|RB)")[0] + " category"

    Name                Example
0   name first RB LA a  RB category
1   name LB second      LB category
2   RB name third       RB category
3   name LB fourth      LB category

Edit
To change category names within Example column use .str.replace:
df["Example"] = (df["Example"]
 .str.replace("RB", "Round Blade")
 .str.replace("LB", "Long Biased")
)

Comment: Isn't extracting and manipulating the strings more awkward than just having a function which checks the conditions and returns the correct category? This only works because the category name contains the extract string we're checking for, which seems quite fragile and hacky.
Comment: @AMC, I thought str.extract was the easiest solution given khouzam's input data (exact matches). Sure, if there are no exact matches, str.contains would work better


Showing a solution is unique

Question:
Let $F:\;[a,b]\times \mathbb{R}^n\to\mathbb{R}^n$ continuous where $0\in [a,b]$ and for some $K>0$:
  $$||F(t,x)-F(t,y)||\leq K||x-y||$$ for all $x,y$ and all $t\in [a,b]$.
I would like to show that there is unique continuous
  $f:[a,b]\to\mathbb{R}^n$ solving: $$f(t)=\int_{0}^t F(s,f(s))\; ds$$

Idea:
I know a weaker statement: if we have the conditions above but instead, for $B_R$ a closed ball of radius $R$ centered at $0:$
$$\mathbf{1.} \;F:\;[a,b]\times B_R\to\mathbb{R}^n\quad \mathbf{2.} \;\sup ||F||\leq R(b-a)^{-1}$$
If a continuous $f:[a,b]\to B_R$ solves the equation, it is unique.
Now I realise I cannot just "let $R\to\infty$", but is it possible to use the fact above to prove the statement?
Comment: Look up the Gronwall lemma, this is a special case of it.
Comment: I think you have mistyped your condition 2, as it stands it makes the result you have pretty weak. Aside from that this is a step in the [Picard Lindelöf theorem](https://en.wikipedia.org/wiki/Picard–Lindelöf_theorem).
Comment: @s.harp Thank you both for the references. Unless there is a mistake in the book condition 2 is as it should be... It seems like an unlikely typo though. So after reading about the lemma and the theorem, it seems that Picard-L (or the fact above) provides a unique local solution, and then I can somehow use the Gronwall lemma to argue that the global solution (the extension of the local one) is unique?
Comment: See this answer to a similar question:  http://math.stackexchange.com/questions/1587806/picards-existence-theorem-successive-approximations-and-the-global-solution/1590414#1590414
Comment: If you are worrying about necessity to increase $R$ to prove your statement for all point in $\mathbb{R}^n$, then don't worry. You can translate any point of interest to the origin and it won't change properties of your functions. But now you'll be able to take small balls around origin to satisfy requirements of your 'weaker statement'.
Answer: Proof of Uniqueness:
Assume that $f,g: [a,b]\to \mathbb R^n$ satisfy the integral equation:
$$
f(t)=\int_0^tF\big(s,f(s)\big)\,ds, \quad
g(t)=\int_0^tF\big(s,g(s)\big)\,ds.
$$
Clearly, $f(0)=g(0)=0$, and
$$
f(t)-g(t)=\int_0^t \Big(F\big(s,f(s)\big)-F\big(s,g(s)\big)\Big)\,ds, \quad t\in[a,b].
$$
We use the fact that $\Big\|\int_c^d G(t)\,dt\,\Big\|\le \int_c^d\|G(t)\|\,dt,$
and obtain that, for $t\ge 0$:
$$
\|f(t)-g(t)\|\le\int_0^t \Big\|F\big(s,f(s)\big)-F\big(s,g(s)\big)\Big\|\,ds\le \int_0^t K\|f(s)-g(s)\|\,ds \quad t\ge 0.\tag{1}
$$
Set $\varphi(t)=\int_0^t \|\,f(s)-g(s)\|\,ds$. Then $\varphi'(t)=\|\,f(t)-g(t)\|$,
and hence $\varphi$ satisfies the inequality:
$$
\varphi'(t)\le K\varphi(t), \quad t\ge 0.
$$
Note that $\varphi(0)=0$. Multiply the above by $\mathrm{e}^{-Kt}$ and obtain
$$
\mathrm{e}^{-Kt}\big(\varphi'(t)- K\varphi(t)\big)\le 0, \quad t\ge 0,
$$
or
$$
\big(\mathrm{e}^{-Kt}\varphi(t)\big)'\le 0, \quad t\ge 0,
$$
and integrating the continuous function above in $[0,t]$ we obtain
$$
\mathrm{e}^{-Kt}\varphi(t)-\varphi(0)=\int_0^t\big(\mathrm{e}^{-Ks}\varphi(s)\big)'\,ds\le 0, \quad t\ge 0.
$$
But $\varphi(0)=0$, and thus
$$
0=\varphi(t)=\int_0^t \|\,f(s)-g(s)\|\,ds, \quad t\ge 0,
$$
and differentiating
$$
0=\varphi'(t)= \|\,f(t)-g(t)\|, \quad t\ge 0.
$$
The case $t\le 0$, is treated similarly.
Answer: Note that your integral equation is equivalent to the differential equation
$$\frac {{\rm d} f} {{\rm d} t} = F(t, f(t))$$
with the initial condition $f(0) = 0$: if $f$ solves the differential equation, then integrate to get
$$\int \limits _0 ^t F(s, f(s)) {\rm d} s = \int \limits _0 ^t \frac {{\rm d} f} {{\rm d} t} (s) {\rm d} s = f(t) - f(0) = f(t) ;$$
conversely, if $f$ solves the integral equation, then it is derivable (with continuous derivative, because $F$ is continuous), so you may derive the integral equation to get the differential one written above. Also, $f(0) = \int \limits _0 ^0 F(s, f(s)) {\rm d} s = 0$.
It is now enough to use the Picard-Lindelöf to obtain what you want, many proofs of which are easy to find with a simple web search.

Showing a solution is unique


How much do I need to learn before I can read about Toric varieties?

Question: I have a copy of the book "Introduction to Toric varieties" by William Fulton, and over the next few months I'd like to make some progress on it.
As a first goal, I'd like to be able to read just the first 3 chapters, which I list some of the sections so people without the book get an idea of the contents:

Definitions and Examples: Convex polyhedral cones, Affine toric varieties, Fans and torics varieties
Singularities and compactness: Local properties of toric varieties, Compactness and properness, Resolution of singularities
Orbits, topology and line bundles: Orbits, Fundamental groups and Euler Characteristics, Cohomology of line bundles

In the next two months I should have covered most of Atiyah-Macdonalds Commutative algebra book. In parallel I was planning to read Fulton's "Algebraic Curves" book (which can be found online here). I already know some cohomology. My question is, will reading "Algebraic Curves" give me enough background in Algebraic geometry to read the 3 chapters on toric varieties? If so, are there any sections of "Algebraic Curves" I could skip? If not, how much more do I need?
Answer: Of the first three chapters, I think the 3rd chapter is a "step up" in difficulty and amount of prerequisites. With a solid background in the very basics (construction of affine and projective algebraic varieties, Zariski topology, definition of singularity, the gluing construction, morphisms...) you should be able to make it through chapter 2. There will be a few places where things don't make sense (for example there is a page and a half where Fulton explains why toric varieties are Cohen-Macaulay), but these can be skipped at first reading without losing the big picture.
Some facility with line bundles and divisors will get you through most of chapter 3, except for the last section on cohomology. Fulton assumes some familiarity with these ideas, and then shows how to work with them in the toric case.
Sometimes, if you run into a gap in your knowledge, you might still be able to follow the toric, or convex, construction. I.e., you can still try to understand what happens on the level of cones, polytopes and fans before working out the algebro-geometric side. After all, the book is a sort of dictionary between the geometry of convex sets and the geometry of algebraic varieties, and one must work to understand the constructions of both categories to get the full translation. Working out the toric side will provide you with examples to understand the general algebraic theory as well.
With regards to Fulton's curves book, there probably are some sections you could skip if your only goal is to read the first three chapters of "Toric Varieties". However, all the material in that book is worthwhile, so I would encourage you to read as much of it, or whatever other introductory book you choose (I like Shafarevich BAG) as you can. For example, part of chapter 2 of "Toric Varieties" is about blow-ups, which are covered in chapter 7 of "Curves". So if you just can't wait to start learning about toric varieties, maybe you can read only the first half of the curves book, then read chapter 1 of the toric book, then go back to curves, etc.
Good luck!


How to use predicted features in prediction?

Question: I have ratings data.
My ratings data contains some technical features like the Originator (Channel), the exact day & hour of the broadcast, duration of the program etc. and, obviously, the label, which is the rating.
So the data looks like that:

+---------+------------+-----------------+----------------+----------------------------------+---------------+
| Program | Originator |      date       | Duration (min) | some other technical features…   | Actual rating |
+---------+------------+-----------------+----------------+----------------------------------+---------------+
| Empire  | FOX        | 24/5/2016 21:00 |             58 | …                                | 4.6%          |
| Gotham  | FOX        | 24/5/2016 21:58 |             32 | …                                | 3.1%          |
+---------+------------+-----------------+----------------+----------------------------------+---------------+

Based on the historical ratings data, I need to predict the future ratings, when all the parameters of the train are given, except the label of course.
My problem is:
A very strong feature for rating prediction is the carry over, or what was the rating of the previous program.
I want to train my model with the feature of the carry over, but I'm not sure how I should add it?
Should I train the model with the real carry-over? (the actual rating of the previous program)?  In the test set the carry-over would be just an approximation to the real carry-over (it will be the prediction, not the actual rating, as oppose to the training data - because I can't know in advance what would be the rating of the previous program) , so the correlation of the carry over with the real ratings in the test set would be less significant then in the train set…
How should I tackle this problem?
Answer: There seem to be  (at least) three types of feature engineering to do here. The first one involves the carry-over (which is the focus of your question). The second one involves transformations of other data to forms more usable by ML algorithms. The third one involves competing programs.
I'll begin with the second one, as it's necessary for the first and third ones.

Leaving carry-over for later at the moment, it looks like some of the features can be manipulated for better use (you might have done this already, but it's not indicated in the question).

The date column - television viewing probably has strong daily seasonal components, probably weekly seasonal components, and possibly yearly seasonal components (see, for example, Prediction Of TV Ratings With Dynamic Models). It's a-priori unlikely that a show airing at 3:30AM before a workday, will have the same rating as a show airing on the early evening of a weekend. It also might be the case that people watch differently in the winter and summer, during vacations, and so on.
Because of this, you might want to transform the date column into a number of features: the daily hour, the day of the week, possibly an indicator variable of whether it's weekend/vacation, the month, possibly an indicator of season, and possibly an indicator of vacation period.
The Program column - Say your test data will be $n$ days into the future, and the particular program is already on the air. The ratings now known for this program are probably some indicator for the future rating. Consequently, you might want to add two columns (at least): the past rating for this program $n$ days or earlier (using an average, for example), and a column for the number of measurements used for the past ratings. (If a show was not on $n$ days earlier, you could encode it as -1 and 0, respectively.) You could go further with this and analyze trends for the program, or ratings for similar shows, but perhaps you should start with this.
The Originator column - you might want something like one-hot encoding here.

(If you haven't done this already,) these transformations might increase the overall prediction accuracy, and decrease the relative importance of the carry-over. Some of these features can also be used as proxies for the carry-on.

I want to train my model with the feature of the carry over, but I'm not sure how I should add it? Should I train the model with the real carry-over? (the actual rating of the previous program)? ... I can't know in advance what would be the rating of the previous program)

In general, it's best to avoid training using one thing, and testing using something that isn't exactly the same. So, as your question implies, it's problematic to train using the actual carry on, and then predict using the predicted carry on.
Instead of predicting the immediately-previous carry on, let's think based on how we would do so.

The popularity of the previous program is possibly affected by its time of day, day of week, and so forth, but that's already "encoded" in the features for the current show, so why repeat it.
Similarly, the popularity of any of the immediately-preceding shows might be determined by their carriers, but that adds nothing (we know that Fox will be airing something just before the show we're predicting, for example). This is already implicitly encoded in the other features.
The one thing that doesn't seem to be already encoded in the other columns, is the past ratings of the shows immediately preceding this show. For example, when Game Of Thrones airs, its probable that the show immediately following it will enjoy large carry-over, but we know this because the past ratings of GoT were high. I think it's best to encode this data as a feature, and let the predictor learn how to use it.
One straightforward feature to add, therefore, would be the rating of the most popular show preceding it based on its past ratings (using the same two-column encoding as before).

Finally, you might want to add as a feature the rating of the most popular show competing with this one, again based on its past ratings.


Installing multiple mysql as rpm vs tarball

Question: Which is the suitable way to install multiple mysql servers without affecting the current installation in fedora( rpm or tarball)?
Answer: Check out the MySQL documentation:
5.3. Running Multiple MySQL Instances on One Machine


Is there a link between the Bunyakovsky conjecture and the Twin Prime conjecture?

Question: Can the proof of one conjecture be considered a proof of the other conjecture?
The general method of building an infinite number of prime producing quadratic polynomials was given in the link below.
What can we learn from prime generating polynomials?
Here we will give just one example of two prime producing polynomials which can generate twin primes. We start with the following polynomial that can only produce even integers and we add a "prime seed part" to it to make it generate primes.
e(n) = (10+n)(1+n) = n^2 + 11n + 10
then we add the "prime seed part" 2n+1 to it to get:
p(n) = n^2 + 13n + 11
Now to get a twin prime we need another prime producing polynomial, g(n),  which differs by 2 from p(n).
g(n) = p(n) + 2 = n^2 + 13n + 13
For n=0,...,10 the combination of these two polynomials produced 5 pairs of twin primes.
So does the proof of one conjecture lead to the proof of the other conjecture?
Answer: I don't know a proof for the implication you stated when we are talking about the simple Bunyakovsky conjecture. ( It seems in the beginning that the implication is true  by construction of two polynomials $P$ and $Q$ such that $P-Q=2$ and apply the conjecture but it's not obvious like i explained below)
The simple Bunyakovsky conjecture states that: any irreducible polynomial with the positive leading coefficient such that :as $n$ runs over the positive integers, the numbers $f(n)$ should be relatively prime, then $f(n)$ is prime for infinitely many positive integers $n$.
Now as you stated we can find two polynomials $P$ and $Q$ which satisfy the conditions of Bunyakovsky conjecture and $P-Q=2$, so $P(n)$ and $Q(n)$ are prime for an infinite number of value of $n$. But in order to prove the twin prime conjecture we need $P(n)$ and $Q(n)$ to be simultaneously prime.
The generalized Bunyakovsky conjecture which was in fact the original, it states that given $n$ polynomial satisfying the conditions above and for any prime $p$ the values of these polynomials are coprime with $p$ for some integer, then these polynomial produces simultaneously an infinite number of primes, this conjecture implies a lot of others:

The twin prime conjecture (when $n = 2$, and the two polynomials are $x$ and $x + 2$)
The infinitude of prime quadruplets (when $n = 4$, and the four polynomials are $x, x + 2, x + 6,$ and $x + 8$),
The conjecture of sexy primes, Sophie Germain primes, Polignac's conjecture
Dickson's conjecture.

There is more general conjecture called Schinzel's hypothesis H.
Comment: Elaqqad, could you please be more specific with what you called "there is problem in the obvious thought you have". I am here to learn. I thought about it but I can't figure out what you meant.
Comment: I'm sorry if what I wrote is not what you thought!
Comment: I am fine with what you wrote and there was really no need to edit your post. I thought you were pointing to something specific that is wrong. The big problem is, like you pointed it out, that there is no reason for these two polynomials to produce **simultaneously** an infinite number of primes that differ by 2 only though there is no reason for them not to produce an infinite number of primes.
So now we run into another problem, can these two conjectures be true yet not imply each other?
Answer: You ask:

Can the proof of one conjecture be considered a proof of the other
  conjecture?

Results of the form "well-known conjecture $\varphi$ implies well-known conjecture $\psi$" are indeed interesting and important. For example, Wiles didn't directly prove Fermat's Last Theorem; what he actually proved was the modularity theorem for semistable elliptic curves. Since this was already known to imply Fermat's Last Theorem, it was enough.


Divide and Conquer Algorithm (MergeSort Algo) calculating number of inversions

Question: I am trying to solve the problem of calculating the number of inversions where the problem is:
`An inversion of a sequence 0, 1, . . . , −1 is a pair of indices 0 ≤  <  <  such
that  > . The number of inversions of a sequence in some sense measures how
close the sequence is to being sorted. For example, a sorted (in non-descending
order) the sequence contains no inversions at all, while in a sequence sorted in descending
order any two elements constitute an inversion (for a total of ( − 1)/2
inversions).
Sample Input is:
6
9 8 7 3 2 1
The output will be:
15
`
Now for this, I am trying to merge the sort algorithm and the idea is whenever I will see nextNo. greater than prevNo. I will add count which is 0 initially.
This is the Merge algorithm:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class MergeSort {

static void Merge(int arr[],int l,int m, int r){

    int n1 = m-l+1;
    int n2 = r-m;

    int L[] = new int[n1];
    int R[] = new int[n2];

    for(int i=0;i<n1;i++)
        L[i] = arr[i+l];
    for(int i=0;i<n2;i++)
        R[i] = arr[m+1+i];

    int i=0,j=0;

    int k=l;

    while(i<n1&&j<n2){
        if(L[i]<R[j]){
            arr[k]=L[i];
            i++;
        }
        else{
            arr[k]=R[j];
            j++;
        }
        k++;
    }

    while(i<n1){
        arr[k] =L[i];
        i++;
        k++;
    }

    while(j<n2){
        arr[k] =R[j];
        j++;
        k++;
    }

   }

static void MergeSortBasic(int arr[],int l,int r) {

    if(l<r){
        int m = (l+r)/2;

        MergeSortBasic(arr,l,m);
        MergeSortBasic(arr,m+1,r);

        Merge(arr,l,m,r);
    }
   }

public static void main(String[] args) {
    QuickSortAlgo.FastScanner scanner = new QuickSortAlgo.FastScanner(System.in);
    int n = scanner.nextInt();
    int[] a = new int[n];
    for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
    }
  MergeSortBasic(a,0,n-1);
    for (int i = 0; i < n; i++) {
        System.out.print(a[i] + " ");
    }
}

static class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStream stream) {
        try {
            br = new BufferedReader(new InputStreamReader(stream));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}
   }

This I understand but here is the solution to this problem that I came across which I could not understand. Can please someone help me in understanding the solution of this algorithm especially the midpoint/ave part.
Solution:
import java.util.*;

public class Inversions {
private static long merge(int[] a, int[] b, int left, int ave, int right) {
    int i = left, j = ave, k = left;
    long inv_count = 0;
    while (i <= ave - 1 && j <= right) {
        if (a[i] <= a[j]) {
            b[k] = a[i];
            i++;
        } else {
            b[k] = a[j];
            inv_count += ave - i;
            j++;
        }
        k++;
    }
    while (i <= ave - 1) {
        b[k] = a[i];
        i++;
        k++;
    }
    while (j <= right) {
        b[k] = a[j];
        j++;
        k++;
    }
    for (i = left; i <= right; i++) {
        a[i] = b[i];
    }
    return inv_count;
}

private static long getNumberOfInversions(int[] a, int[] b, int left, int right) {
    long inv_count = 0;
    if (right <= left) {
        return inv_count;
    }
    int ave = left + (right - left) / 2;
    inv_count += getNumberOfInversions(a, b, left, ave);
    inv_count += getNumberOfInversions(a, b, ave + 1, right);
    inv_count += merge(a, b, left, ave + 1, right);
    return inv_count;
}

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int[] a = new int[n];
    for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
    }
    int[] b = new int[n];
    System.out.println(getNumberOfInversions(a, b, 0, a.length - 1));
}
}

My question is Why do we have
inv_count += ave - i;

Instead of simply:
inv_count++;

Like what is the difference between these two programs?? How is this ave variable working? Also, any idea how can I learn this effectively in the future?
Comment: Try to work out what happens with an unsorted array of 4 elements.
Comment: My question is like how do you reach from mergeSort to this solution!! This will work fine but how one can figure out this solution am sorry I am kind of a novice to algorithms.
Answer:
why: inv_count += ave - i;

The two sub-arrays being merged are already sorted from prior recursions (or an end case where sub-array size is 1 element). Each time an element from the right sub-array is found to be less than the current element in the left sub-array (a[j] < a[i]), the number of elements remaining in the left sub-array (ave - i) is added to the inversion count, because a[j] is less than all of the elements from a[i] through a[ave-1].
Comment: As for how you get there, write a merge sort.  Add a loop to the merge sort to count inversions as they are discovered during the merge.  Then simplify that loop by counting how many things the loop would have found.  Voila!


Would anyone be able to help name this progression?

Question: I've had trouble coming up with a name for this progression and was wondering if there is a jazz term or if it's a type of cadence.
Cm7 D7 Gmaj7
Comment: Cadences are generally the last two chords, so D7>Gmaj7 will be a perfect cadence (on the right of the Atlantic). As in V>I. What comes before doesn't usually affect a cadence. I guess the whole sequence would be iv>V>I, for want of a better term.
Comment: Not everything has a name. And for chord progressions, their names are usually just the names of the chords. So you've already named it.
Answer: There's no name for this progression, but there is a name for the harmonic concept that it demonstrates.
These three chords point towards G major. And in G major, the seventh chord built on C is Cmaj7. But in this example, you have a Cm7.
And that's because the Cm7 is actually the seventh chord built on C in G minor. So this progression, very obviously written in G major, uses the C chord "borrowed" from the parallel key of G minor.
We call this Cm7 a "borrowed chord," and this progression is an example of "mode mixture" because we mix harmonies that occur between the modes of major and minor.
Comment: @Dekkadeci - true, Cm might work, but it would be more convincing if the G was G7, rather than Gmaj7. Don't often come across Gmaj7 in key Cm. D7 as a sec.dom. works best when its F# moves directly to G, rather than stops on F#!
Comment: Weird, I actually think of this chord progression as being in **C minor** instead and the D7 chord is a secondary dominant.
Answer: It's a variation of the stock ii-V-I progression that underpins so much popular music.   If it ends a phrase, the last two chords form a perfect cadence (or whatever people call a V-I cadence in your territory).
Not everything has a precise label.  If you want to describe 'Cm7 D7 Gmaj7', say 'Cm7 D7 Gmaj7'.


Web service error in mvc is UNKNOWN METHOD NAME How to resolve this?

Question: Hello Everyone i have a web service am calling the web service from Jquery ajax the error am getting is
Unknown web method BindDESG.
Parameter name: methodName
My method is like this
<code>[WebMethod]
public static List<ListItem> BindDESG()
    {
        AdoConClass getado = new AdoConClass();
        string query = "select Pk_DeptId,DepartmentName from emp_Master_Department where IsActive=1";
        DataTable dt=getado.retrievedata(query);
        List<ListItem> Desg = new List<ListItem>();

        if (dt.Rows.Count>0)
        {
            Desg.Add(new ListItem
            {
                Value = dt.Rows[0]["Pk_DeptId"].ToString(),
                Text = dt.Rows[0]["DepartmentName"].ToString()
            });
        }
        return Desg;
    }
</code>
JQUERY
<code>$(document).ready(function () {
$(function () {
    $.ajax({
        type: 'GET',
        url: '../../Services/Employee/BindDesgDDL.asmx/BindDESG',
        data: { },
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (r) {
            var ddlCustomers = $("[id*=sDesg]");
            ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
            $.each(r.d, function () {
                ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
            });
        },
        error: function (msg) { alert(msg); }
    });
});
</code>
});
Method is public and static but i don't know why the error is coming...
Please help me...
Comment: you better include your Jquery ajax code as well in the question. is this c# code also in the same page code behind file?
Comment: Does this method appear in the WSDL for the web service?
Comment: I have add my jquery code in question plz find that.... @Damith
Comment: @Raviteja remove the `static` from the web method
Comment: Thanx Damith it is working ........ @Damith
Answer: The method which am using is static method ....
I just removed the static from the method the non-static method is executed ....


From which Apache version onward is LimitRequestFieldSize no longer hardcoded to 8k max?

Question: Title says it all:
from which version onward is the LimitRequestFieldSize no longer hardcoded to a max. of 8k?
It seems 2.2.15 is still affected.
Answer: Ok, so it turns out that it works in 2.2, but you need to increase the max AJP packet size if you have a Tomcat behind it and use mod_proxy_ajp, or else it will still give a 400 Bad Request.
Specifically, set this in Apache:
LimitRequestFieldSize 65536
ProxyIOBufferSize 65536

and this in Tomcat server.xml on the ajp connector:
packetSize="65536"

Only setting LimitRequestFieldSize won't do the trick!
Comment: Regarding where to set in Apache: http://serverfault.com/questions/413984/increase-the-value-of-limitrequestfieldsize-in-apache


If two elements are different there is a functional under where the image is different

Question: I have the following exercise in functional analysis:
Let E be a normed space and $x,y$ different vectors. Prove or disprove finding a counterexample that it exist a function $f \in E^*$ such that $f(x) \not= f(y)$.
I tried do define a function on $<x,y> \subset E $ with different images for $x,y$ and than to extend this with hahn Banach to $E$, but I dont know how to define this. Is this statement even true or is there a counter example?
Comment: Why not just create a functional that is non-zero on $x-y$?
Comment: Related: https://math.stackexchange.com/questions/2579456/dual-space-x-separates-points-of-x
Answer: Yes, the statement is true, and your approach is kind of the way to go. But it is better to state the problem in the following equivalent and more useful way:

Given $x\in E$ there exists $f\in E^*$ such that $f(x)\ne0$.