Can you help me with my capacity planning?

Question:
This is a canonical question about capacity planning
Related:

How do you do load testing and capacity planning for web sites?
How do you do load testing and capacity planning for databases?

I have a question regarding capacity planning. Can the Server Fault community please help with the following:

What kind of server do I need to handle some number of users?
How many users can a server with some specifications handle?
Will some server configuration be fast enough for my use case?
I'm building a social networking site: what kind of hardware do I need?
How much bandwidth do I need for some project?
How much bandwidth will some number of users use in some application?
Answer: The Server Fault community generally can't help you with capacity planning - the best answer we can offer is "Benchmark your code on hardware similar to what you'll be using in production, identify any bottlenecks, then determine how much of a workload your current hardware can handle, and/or how much hardware horsepower you need to handle your target workload".

There are a number of factors at play in capacity planning which we can't adequately assess on a Question and Answer site:

The requirements of your particular code/software
External resources (databases, other software/sites/servers)
Your workload (peak, average, queueing)
The business value of performance (cost/benefit analysis)
The performance expectations of your users
Any service level agreements/contractual obligations you may have

Doing a proper analysis on these factors, and others, is beyond the scope of a simple question-and-answer site: They require detailed knowledge about your environment and requirements which only your team (or an adequately-compensated consultant) can gather efficiently.

Some Capacity Planning Axioms

RAM is cheap
If you expect your application to use a lot of RAM you should put in as much RAM as you can afford / fit.
Disk is cheap
If you expect to use a lot of disk you should buy big drives - lots of them.
SAN/NAS storage is less cheap, and should also usually be spec'd large rather than small to avoid costly upgrades later.
Workloads grow over time
Assume your resource needs will increase.
Bear in mind that the increase may not be symmetrical (CPU and RAM may rise faster than disk), and it may not be linear.
Electricity is expensive
Even though RAM and disks have decreased in price considerably, the cost of electricity has gone up steadily. All those extra disks and RAM, not to mention CPU power, will increase your electricity bill (or the bill you pay to your provider). Plan accordingly.
Comment: I'd add to this: Disk _capacity_ is cheap. Disk _performance_ gets expensive. Especially as we see a growth in disk sizes over 10 yearrs, but the laws of physics haven't changed. Rule of thumb I use (as of today; June 2014) is that for optimal performance: 75 IOPs per spindle on SATA, 200 IOPs per spindle on FC, and 1500 IOPs per SSD. Big SATA drives give really quite poor IO per gigabyte ratios.
Comment: You should totally drop that and use integration by parts!
Comment: In mid-2019 nobody should be buying spindle drives anymore.
Comment: I'd like to add one important planning point: assume that you'll get hostile connections. If  you want to run a website and do not mind about FISA letters, CloudFlare is your friend. Otherwise, you'll be paying a lot or be vulnerable. Never ever assume that not upgrading your internet facing OS is okay. Never ever assume that some magic firewall will protect your vulnerable OS.
Comment: +1. And RAM, as you suggest in axiom #1, is one of those things that has massive benefits.  For instance, it increases your ability to better utilize caching, which in turn allows you to make fewer database queries, which in turn lightens the load on the disk and CPU.  I'm often frustrated by hosting providers that offer a fast CPU with their servers and a minimal amount of RAM.
Comment: @RobertHarvey nobody should be buying **SAS** spindle drives anymore. Getting capacity for petabox scale servers still requires getting lots of SATA drives or you end up paying a lot.
Answer: Virtual Machine Count planning
When it comes to figuring out how many VMs you should plan for on a single host, there are actually no really good rules of thumb. In fact, there is only one, and it is only kind of good:

Virtual-Machine counts are usually bounded by RAM, except for when they're not.

Which isn't terribly helpful. If those VMs are going to be running low-CPU applications, then your limiter is going to be based on RAM. Each VM platform has its own abilities to oversubscribe RAM, so it isn't as easy as TOTAL_RAM / Per-VM-RAM = MachineCount, but that number is a good planning item.
But what if your VMs are doing things besides low-CPU packet-slinging?

Virtual-machine counts are bounded by seven discrete resources available to the host machine:

Hypervisor VMware, Xen, HyperV, KVM, whatever. Each has their own count-impacting features. Some are very good at memory-page deduplication, others not so much. Some don't permit oversubscription of CPU capacity, some do.
CPU Core Speed This limits the maximum single-threaded performance a VM will be able to run. 36 cores of a 1.8 GHz CPU may be 64.8 GHz of CPU on a host, but no single thread will run faster than 1.8 GHz.
CPU Core Count This, with core-speed, describes the ceiling of maximal CPU performance you can experience.
System RAM As described above, this limits the number of VMs you can run. Certain hypervisors are better than others at things like memory-page deduplication, so if you're running 100 identical VMs you can pack a lot more of these on such deduplicating systems than if you were running 100 completely different VMs.
Disk Size Each OS image takes a certain amount of space. You need enough space to store it all. Therefore, disk-size puts an upper limit on how many VMs you can host.
I/O Bandwidth The disk underlying the VMs has a maximum on how many I/Os per second it can handle. If you throw too much at it, systems will bog down waiting for the I/O to complete. This puts an upper limit on how many I/O consuming VMs you can run.
Network Bandwidth For network-using VMs, the available network bandwidth will put a ceiling on how many such VMs you can run on a given host.

All of these can be the thing you trip over, it all depends on what you're doing with your VMs. Some things to remember:

There is no such thing as a generic system.
There is no such thing as a generic web-server, since application code can run from barely-moves-the-needle CDN-style serving, to big deep-crack stuff like video transcoding.
There is no such thing as a generic database server. These can run from tiny systems used just for session-state-tracking, to very big ones.

To figure out how many VMs you can pack into a host-system, you need to know how your systems run and what they require to run well. Once you know that, you can then do the count-planning. And better yet, figure out how beefy you need to make your host-systems!
Comment: The thing that I've seen undersized most often in VM implementations is disk I/O.  Most people understand disk space, CPU speed, and memory.  They forget about that disk performance.
Comment: above all else, do use vm based systems on two separate physical servers with unbound vm's. this allows for hardware failure without loss of the entire system. vm's can move between identical servers without loss of data. just sessions get lost, then rebuilt. personally, i would outsource to a hosting company that offers these services (google or amazon). they are expensive but a lot less than running your own.
Comment: @danpritts  Unfortunately, you are not wrong.
Answer: Make sure you're asking the right question.

Computers are cheap
Future needs are very hard to predict
Plan how to scale, not what to buy ahead of time

If you don't know what you'll need, that implies you don't need very much.  If you have a hot web site, you also probably also have an operations team who knows how much ram, disk, io, network etc... your app needs.  If you're in the dreaming stage, you should start with your desktop and work your way up.
Make sure you have some idea how you're going to scale when things get bigger.  Can you add more servers behind the load balancer?  Can you shard the redis server?
Also, having your own data center sucks.  A data center (even if it's just one computer) is a distraction from your actual purpose.  You can not just buy a computer, turn it on, and walk away.  You need air conditioning, air filtration, reliable power, reliable internet, backups, spare parts, physical room to grow, power capacity to grow, power cables that don't get tripped on and a zillion other headaches.
Answer: Start with estimating "queries per second".  100 ie easy to handle.  1000 may be an issue.  1 million is virtually impossible on a single machine.
"Number of users" -- Thousands of users not doing much is less trouble than one user predicting tomorrow's weather.
"Bandwidth" -- How many bytes need to be shoveled into/out of the server?  How much data will you have on disk?
"Social networking" -- Build a prototype, get a thousand users, and gather metrics. Then let's discuss how you should rewrite the schema and redesign the server topology.
"Sharding", "Caching", etc.  When you get to a million "users", you may need the data and activity split among a hundred servers.  Without more details, we cannot discuss how to do the split.  Furthermore, it is really premature to design for scale until you discover what brick walls you are going to hit.
Comment: This might be a better answer for this other canonical question  : https://serverfault.com/questions/350454/how-do-you-do-load-testing-and-capacity-planning-for-web-sites


Determining (parity of) maximum path length from adjacency matrix. Can I avoid having to square?

Question: Setup: I have an undirected (multi)graph $G$ with $n+1$ vertices. Each vertex has degree $\leq 2$, and vertex number $0$ has degree exactly one.
What I'm looking for: I know that any graph with maximum degree $\leq 2$ can be split up into just paths and cycles. Since vertex zero has degree exactly $1$, it must be part of a path. What I'd really like to know is the maximum length (diameter, I guess) of this path.
My thoughts: Since vertex $0$ is part of a path, the part of the graph that it's in is not a multigraph. That means that multiplying the adjacency matrix $A$ by itself ($A^k$) will eventually yield a matrix with all zeros, except for a $1$ in the $0,j$ entry, where $j$ is the end of the path. So one way to get the path length would be to keep squaring $A$ until it looks like this, and then note the parity of $k$.
My question: Is there any way to determine the parity just by looking at the adjacency matrix? I know that maximum path length is in the general case very hard (NP-hard, I think), but given the number of constraints I have on my graph, and that I don't even need path length, just parity, I figured there might be some easier way of doing it.
Comment: The obvious method of simply following the path from vertex $0$ takes $O(n^2)$. Is that too slow?
Comment: If the graph has maximal degree $\le2$, you shouldn't be storing its adjacency matrix as a full matrix but as a sparse matrix. Then following a path of length $L$ would only take $O(L)$ time.
Comment: @HagenvonEitzen I'm impatient :)
Comment: Well, if the graph has $m$ connected components, then you can write $A=A_1\oplus\dots\oplus A_m$ Then, if, for example, vertex $0$ lies in component $1$, then the length of this path is exactly the size of $A_1$.
Answer: In general, (i.e., general graphs $G$ on $n$ vertices), if you take a high enough power $k$ of adjacency matrix $A$ of a graph $G$ (where the $ij$-th entry of $A$ is 1 iff $i$ and $j$ are adjacent in $G$), then $A^k$ will be a block matrix: You can partition $V(G)$ into sets $V_1,\ldots, V_m$ for some $m$ where
(i) the $ij$-th entry of $A$ is positive iff $i$ and $j$ are both in the same $V_l$, and
(ii) the $V_l$'s are the connected components of $G$; i.e., $i$ and $j$ are in the same connected compoent of $G$ iff $i$ and $j$ are in the same $V_l$.
In fact, taking $k = 2^{\lceil\log n\rceil}$ will do where $n$ is the number of vertices of $G$.
Meanwhile is there a reason why you just cannot follow the path starting from vertex 0. If the graph has maximum degree 2, then storing $G$ as an adjacency matrix is extremely inefficient use of space.


Wizard com Kendo UI

Question: A Kendo Ui não desenvolveu o componente wizard, então desenvolvemos em javascript um componente próprio, porém ao "misturá-lo" com a Kendo eu recebo erros no arquivo kendo.web.min.js da kendo. Em buscas de solução encontrei esse link que ocorre o mesmo problema ao clicar em next.
Alguém sabe como resolver? Alguém cohece algum outro componente pronto que funcione com a Kendo?
Obrigada
Comment: até hoje não encontrei um script que consegui-se co-existir em harmonia com o Kendo UI.
Comment: Na verdade aquele código só está mal implementado, mas ele basicamente funciona, fora alguns erros bobos: http://jsfiddle.net/vKBa8/31/
Answer: Procurei bastante e na época não encontrei nada, então um colega me disse pra usar o seguinte código (Pode não ser o melhor e mais prático, mas funciona)

                   Passo

                    01

                   Passo

                    02

                   Passo

                    03

                   Passo

                    04

                   Passo

                    05

Wizard com Kendo UI


What's the complete rule regarding double bouncing balls inside the popping crease?

Question: I've been trying to figure out the possible outcomes in case a ball double bounces off the cricket pitch. I read Law 20 and Law 21. Law 20 does not reference double bounced balls. Law 21 says:

The umpire shall call and signal No ball if a ball which he/she considers to have been delivered, without having previously touched bat or person of the striker,

bounces more than once or rolls along the ground before it reaches the popping crease

But what if double bounces inside the popping crease? And then possibly stumps the batsman? Is there another rule or law regarding that?
Answer: That's it - if the ball bounces twice before it reaches the popping crease, it's a no ball. If it bounces twice on or after the popping crease, it's a fair delivery.

And then possibly stumps the batsman?

A ball cannot stump a batsman; that can only be accomplished by the wicket-keeper. If you mean the ball hits the wicket and permanently dislodges the bails, then the batsman is out bowled.
Comment: The ball looks to have made the second bounce between crease and stumps. Decision: out. @RamChandraGiri
Comment: I believe it would be out, but I'm not a qualified referee in any way, so don't put any money on it!
Comment: Sir, would [this](https://www.youtube.com/watch?v=mfW-Mus5b8c) be out?
Comment: No, because the umpire signalled "No ball" :-) That video is from a 1999 match so would have been played under the [1980 code](http://static.espncricinfo.com/db/ABOUT_CRICKET/LAWS/1980_CODE/); in that, [Law 24](http://static.espncricinfo.com/db/ABOUT_CRICKET/LAWS/1980_CODE/LAW_24_NO_BALL.html) makes no mention of no balls for bouncing twice, so this must have been due to the playing conditions for the specific match. Good luck finding those!
Comment: Sir, I wanted to ask as per this new rule would that be out or not.
Comment: "A ball cannot stump a batsman" haha yeah I meant the other meaning. Thanks though :)
Answer: If the ball bounces once before it reaches the striker , then it will be No Ball Call And signal , remember it not a dead ball , so If the striker hits the ball and attempts a run or its a boundary  , it will be scored and recorded in the credit of striker , there is no Rule MCC 2019 edition ,  which say that , it will be called dead .... Read Law MCC 21 .7
See it is Amended and the change is  more than once , not more than twice , for now onwards MCC
Comment: The ball bouncing only once before the striker is no reason to call and signal No ball; this is how the vast majority of balls delivered occur, and would make the game a farce.
Comment: This is wrong in all respects.


PMD rule test classes source for benchmarking

Question: Are there any test classes where PMD rule can be tested ? Basically i want to benchmark some other tool again PMD's basic rule set. Like PMd must be testing its rule against some test code..
I dugg through PMD source but could not figure out where such classes are.
Answer: The rule guidelines page describes the -benchmark flag where you can see how long a rule takes against given source code - they suggest the JDK code itself.
Comment: You can run JTest and PMD against the same codebase.  Then you don't need -baseline anymore.  I recently was in a project that migrated from JTest to PMD.  The performance was about the same for the configurations we were using (except for very large code bases).  That said, we had turned off a lot of JTest rules because they were dragging performance and had PMD with many more rules on.
Comment: Actually, I am trying to test JTest and PMD to see which is better. So i was thinking if there is like tests for "Bad Code" which PMD provides, i can test against it.


Add jquery event to joomla mainmenu module

Question: I have a joomla site that loads the main menu module like this:
<jdoc:include type="modules" name="topmenu"/>

This module load an  tag. I want to use jquery with that  tag.
The problem is that my jquery code doesn't work doing this:
$("menu")

It seems that the
 must be loaded before i use jquery, other way jquery doesn't find the class.
Any idea on how can i use jquery with my mainmenu module?
Thanks
Answer: $(document).ready(function(){
 // $('menu') is available here
});


What is the tonic of a musical mode?

Question: Let's say I'm playing Dorian D mode. Is my tonic:
C, because Dorian mode is derived from the diatonic scale of C?
Or D, because my tonal center is in D?
Answer: The tonic of Dorian D is D.
In fact this is more or less what makes the difference between the modes. Altough they can have exactly the same notes, the function of those notes, how they are used, is different.
In dorian D, D is used as the tonic: i.e. the piece ends with D, the main phrases end in D, the D minor chord is the most important chord...
Comment: This is mixing up the original definition of the mode with the modern terminology of tonal harmony. Modes don't have a "tonic". The do have a "final" (which is approximately the same as the "tonic") and a "dominant", but the final is not necessarily the "lowest note" of the mode, and the dominant is not necessarily a 5th higher than the final!
Answer: A tonic is a tonal concept, so it doesn't transfer 100% when you talk about modes as they are not tonal ideas.
The simple answer is yes to be in D Dorian you need to make it sound like you have D as your "tonic" or else you are not in D Dorian. If your tonic is C then you are in C major, not D Dorian.
Comment: @Kevin nope. Modes don't imply functional harmony which is a big part of tonality. Structure alone does not equal tonal.  There's a reason we use major and minor to denote keys and key signatures rather than Ionian and Aeolian. This has been [discussed a lot on the site already](http://music.stackexchange.com/questions/6401/whats-the-difference-between-modal-music-and-tonal-music).
Comment: that's nicely worded. I suppose the answer was in my question 'tonal center'. However, i was a little confused the way people talk about thi. Thanks @Dom
Comment: -1 "Modes ... are not tonal ideas." Yes, they are. They give structure to how we interpret different pitches.
Comment: @Kevin Modes are a pitch-based concept, but not a tonal one. "Tonal" refers to the system of Major/Minor keys: i.e., "Tonality". Modes and pitch-centered, but they're non-tonal.
Answer: The tonic is D (d-minor as a chord). The cadence in d-dorian would be
X: 1
M: C
K: Cmaj
L: 1/2
[Gdgb] [A^cea] | [Ddfa]2

Though I daresay you'll find rather more often the relative dominant C-major used, rather than the actual dominant A-major.
X: 1
M: C
K: Cmaj
L: 1/2
[Gdgb] [Cegc'] | [Ddfa]2

Comment: @JohnandLynHenry you can ask that as a separate question as it will be more beneficial to everyone then posting an answer here in the comments.
Comment: Would it HAVE to have a C#, as it's not in the key.
Comment: i haven't seen 'cadence' before. how is the cadence derived or is it a loose idea?
Comment: @Tim classically, if the A-chord is supposed to be a dominant, it has to have the C♯, yes. But pieces in Dorian will quite often not use that dominant.
Comment: @JohnandLynHenry: in a nutshell, the [cadence](https://en.wikipedia.org/wiki/Cadence_(music)) is a sequence of harmonies that _necessarily leads up to_ the tonic, thereby clearly defining it.
Comment: The song "Greensleeves" is in Dorian mode, and I've heard it performed both with and without a raised 7th; if the melody uses a non-raised seventh, that would suggest that the harmony should use a v rather than a V.  I don't know if there's any solid evidence how people performed it at the time it was written.
Comment: @supercat: indeed Greensleeves is often (usually?) performed with a minor v at the end of the first phrase, but I think I've always heard the period end with an actual i-V-i, with raised 7th degree. And I'd be surprised if what wasn't also the way it was originally played. But of course, that ending cadence could also be interpreted as just borrowed from the melodic minor mode. — Anyway... what's clear is that there exist chords with dominant function leading to the tonic, which for D-Dorian is d-minor, and not C-major.
Comment: Except when used with "What child is this?" lyrics, I think I most commonly hear the song with a minor v at the end of the first phrase and a major V-i cadence at the end; the melody sounds very different without the raised seventh at the cadences, but I have no idea which form would have prevailed in the late 1500s.
Answer: A good question! I'd say technically in D Dorian, D is the tonic, as that's the 'key' it's in, and the start point. However, musically, it could feel like it wants to 'come home', and depending on how it's sounding, it could come to rest, sounding good, back at C. So then, one could say the tonic is C.
Comment: If it sounds/feels like C is the resting point then the piece would not really be in Dorian mode, IMO.
Comment: Once again, an unexplained dv! Please come back and tell why you think this answer deserves a dv. That way, we're all just a little wiser. Thanks.
Comment: I'm not the one that downvoted this, but to quote my comment to a downvoted answer below, "[In modal music, with the mode being D dorian], one could not really consider C as a 'tonic' under any meaning of that term; as one can plainly see in typical examples of modal Dorian music (for ex. the gregorian chants 'Victimae paschali laudes' or 'Dies irae')". Of course, it's possible to have a temporary "tonal center" which deviates from the "tonic" (just the same as in common practice period music), but that wouldn't make it into a "tonic".

What is the tonic of a musical mode?


Original sources for note-increment dot

Question: A dot after a note adds half of its duration. This is called a "dotted note". I am interested in the history of the dot itself. How is it called, and what is its origin. I'm looking for earliest original sources.
Answer: The formal name is the Dot of Augmentation (or Point of Augmentation, particularly in British sources).  This is to distinguish it among the four classes of dots/points that were used in earlier music.
This link gives a good summary.  The thing one must understand about dot usage in early music (so-called "mensural notation," the earlier form of Western rhythmic notation) is that there were complex rules revolving around the interpretation of the duration of notes.  In our modern notation, note shapes always denote the same relative duration to other note shapes.  That was not true in early music, where the same note shape could have various lengths relative to other note shapes, depending on a lot of complicated rules.  (The first few pages here attempt to summarize the most common rules for typical renaissance-era notation.)  The one essential concept had to do with "perfection" which was generally when a note had a value of three times a smaller note value, versus "imperfection" where that same note shape had a value of two times the smaller note value.
Basically, the four classes of dot in medieval and early renaissance music were:

Dot of Augmentation (punctus augmentationis) - essentially equivalent to our modern dot, it had an effect of adding half the value to the preceding note.
Dot of Perfection (punctus perfectionis) - a dot that restored an imperfect note (which was interpreted as imperfect due to its context) to a perfect note value, often in effect similar to the dot of augmentation.
Dot of Alteration (punctus alterationis) - a dot that was used to create an alteration, which in mensural notation meant that a note was doubled in length to complete a perfection (group of 3).  There were rules where alteration happened automatically according to context of the rhythm, but in some cases it might be confusing to keep track of where it occurred.  Hence the dot of alteration, which doubled ("altered") the duration of the note that occurred two notes later.
Dot of Division (punctus divisionis) - a dot merely present to function as a sort of "barline" in ambiguous rhythms.  It simply showed were one perfection (group of 3) ended and another began.

All of these dots basically look the same, so it can be confusing to those unfamiliar with mensural music when one of the more obscure types occurs.  (The last two types sometimes are notated above or below noteheads, rather than following them, but this usage is inconsistent.)
As noted, the Dot of Augmentation and the Dot of Perfection basically had similar effects (though on different rhythmic levels, and sometimes required in different contexts).  And that effect of adding 50% of the note duration is equivalent to our modern dot.  The latter two types of dots fell out of use with the demise of mensural rhythmic notation in the 17th century, where they were rendered unnecessary as note shapes now always determined relative duration.
As for the origin of the Dot of Augmentation, it appears even in early chant sources predating the use of exact rhythmic notation, so it's hard to give it an exact origin.  At that point, it denoted a general lengthening of a note, rather than specifically adding 50% of the length.  Once the earliest rhythmic notation develops in the late 1100s (modal rhythmic notation), the Dot of Augmentation was adapted into the Dot of Perfection to show groups of 3 by lengthening a note with duration 2 into 3.  However, if I remember correctly, this also created the Dot of Division, as a dot that made a complete perfection (three "beats") by transforming a note of length 2 into 3 also could serve a showing a division point between two perfections.
In any case, the emphasis of the 2 vs. 3 element in modal notation seemed to cement the idea of the Dot of Augmentation/Perfection in a specific 2:3 ratio for notes with the added dot.  That ultimately resulted in its application to other note values with a 50% addition in value, hence our modern Dot of Augmentation.
To summarize the earliest history: dots appear in various early chant sources, just indicating some sort of general lengthening of a note.  When rhythmic notation first emerged around 1200, it emphasized groups of 2 vs. 3 subdivisions, so the dot was newly interpreted to specifically indicate this 2:3 ratio, as it still does.
Answer: Great question, as far as in know it's called a dot, or dotted note, no other name I'm aware of. Before the modern day use of the dot which adds 1/2 to a note's duration it was used in Gregorian Chant but it had a different meaning then. Since specific rhythms were not notated it simply meant a note held for a longer duration. There is a bit of info on it towards the bottom of this web page:
http://www.lphrc.org/Chant/

Original sources for note-increment dot


Reload vaadin table

Question: I have a Vaadin table in a View. There is a button to create a new row in the table, a subwindow opens which allows you to specify the new item and create it.
Once created a notification tells you it was created successfully, at this time I would like to reload the current UI (which is a View) so that the new element is visible (if pagination would make it visible).
I tried UI.getCurrent().markAsDirty(); before closing the subwindow but it does not work.
Comment: Is this table immediate (`table.setImmediate(true);`)?
Comment: Do you have your own Container for the table? does it fire an itemSetChangeEvent after insertion?
Comment: you might try table.refreshRowCache()
Comment: The table is immediate. The method refreshRowCache does not work. The table has its own container, the problem here is that this container is filled in at the beginning of the enter method, and when the new item is created in the subwindow, the container should be entirely reloaded. I'm also considering the event mentioned in the second comment.
Answer: Finally I solved the problem by taking advantage of the Table method addItem. This works, although there must be more elegant solutions.
Answer: The vaadin table can be automatically reloaded by changing the main layout that you are using to static.
Comment: SO site provide relevant ans so provide ans with detail explanation,or some example
Answer: I have just run into a similar problem. I had a table backed by sql, and a subwindow to edit details + delete. There was no update of the table, no matter of having markAsDirty(), setImmediate(true) on either subwindow or the table.
The solution was to get the SQLContainer from the table, and call refresh on that:
 Table table = new Table("Contact items");
 ... // add dataSource, headers etc, and after several calls:

 @Override
    public void windowClose(CloseEvent e) {
        SQLContainer c =  (SQLContainer)table.getContainerDataSource();
        c.refresh();
    }

Answer: I have just struggled along this problem. I used a PagedTable... extended by me, and added a method like this:
Object[] visibleColumns = getVisibleColumns();
getDataSource().refresh();
setContainerDataSource(getContainerDataSource());
setVisibleColumns(visibleColumns);
refreshRowCache();

This way I could use a popup for inserting a new item into a Table, and get the Table refreshed with the new line.(There were some additional code to page where the new record can be seen)
Answer: If you want just to refresh the table, you can use the table container data source to do this refresh by remove all items and add again, just like the following code:
List<Item> items = new ArrayList<>();
for(Object object : table.getContainerDataSouce().getItemIds()){
    items.add(table.getContainerDataSource().getItem(object));
}

table.getContainerDataSource().removeAllItems();
table.removeAllItems();

items.add(<yourItem>);

for(Item item : items){
    table.getContainerDataSource().addItem(item);
}

Comment: If that table had ten thousands of rows, how fast would removing and re-adding them all be? There must be a better solution than this.
Comment: Vaadin is an UI display framework, good luck having ten thousands of rows stored in memory and displayed to the user.
Comment: If your software uses a web front end, 'AG Grid' is a very good solution, plus it is very well documented. See https://ag-grid.com/

Reload vaadin table


Adicionando pontos em um intervalo de dados conhecidos

Question: Estou trabalhando em análise de dados utilizando Python e para isso estou treinando algoritmos do tipo SVC e K-means. Os dados utilizados para o treinamento contam com um espaçamento fixo entre cada amostra, pois são amostrados por um osciloscópio em períodos fixos de tempo, em contrapartida, tenho dados obtidos também por simulação, que por questões de performance, apresentam espaçamento variado entre as amostras e um número menor de pontos, o que dificulta a utilização destas duas fontes de dados na mesma análise. Existe um método usando numpy ou pandas para realizar esse pré-processamento dos meus dados de simulação?
Um Ex do que deve ser feito:
Array da simulação = [ 1.0, 2.0, 3.0]
Array processada = [ 1.0, 1.5, 2.0, 2.5, 3.0]
Comment: Fazer isso nos dados simulados não alteraria o resultado? Dependendo de como foram utilizados esses dados, inserir valores intermediários poderia gerar falsos resultados na sua análise.
Comment: Os pontos a serem adicionados devem fazer sentido, algo como um valor obtido através de interpolação, só adicionaria mais passos na silhueta que já existe. Eu não conheço profundamente o pandas ou numpy, não sei se existe essa possibilidade.
Answer: Concordo plenamente com o comentário sobre a mudanca dos dados que você dará como entrada. Porém tb sei que nem sempre temos os dados como precisamos.
Seria mais certo talvez fazer um downsampling de um dos dados, reduzindo os erros devido a dados extras devido a interpolacão.
Uma vez dado o warning, o que você quer é uma interpolação!
Para fazer isso, use a funcão interp do numpy
import numpy as np
x = np.linspace(0, 2*np.pi, 10)
y = np.sin(x)
xvals = np.linspace(0, 2*np.pi, 50)
yinterp = np.interp(xvals, x, y)

Esse exemplo é o mesmo do manual, que lá explica condicões limites tb, que podem ser relevantes conforme você está aplicando.
np.interp também funciona para downsampling.
Comment: Muitíssimo obrigado pela ajuda!
Answer: Seguindo a sua dica, li a documentação do Numpy e do Scipy, e usando próprio numpy que possui uma função chamada "interp" (como na documentação acima), mas minha preferência é pelo pacote "scipy" que possuiu diversas formas de interpolação, como no exemplo a seguir:
Primeiro importo numpy e scipy
import numpy as np
from scipy import interpolate
Crio agora os dados:
dados_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
dados_y = [10.0, 20.0, 30.0, 40.0, 50.0, 70.0, 90.0, 100.0, 200.0, 300.0]
Faço agora a interpolação:
interp = interpolate.interp1d(dados_x, dados_y)
tenha em mente que agora a variável interp contém um objeto que é capaz de fazer a interpolação
Por fim, passo para a interpolação novos dados:
new_x = np.arange(1, 10, 0.1)
new_y = interp(new_x)
Agora, a variável new_y contém um array numpy, como esse no caso do exemplo:
array([ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86., 88., 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., 100., 110., 120., 130., 140., 150., 160., 170., 180., 190., 200., 210., 220., 230., 240., 250., 260., 270., 280., 290.])
Ademais, caso precise interpolar valores em 1D, 2D, 3D, dê uma olhada nas docs:
Função no numpy: https://docs.scipy.org/doc/numpy/reference/generated/numpy.interp.html
Função no scipy: https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.interpolate.interp1d.html
Todas as interpolações: https://docs.scipy.org/doc/scipy-0.19.1/reference/interpolate.html
Muito obrigado pela ajdua


Ranking/polling field or webform module

Question: I am trying to find a field (ideally, for use with Entity Forms), or a webform module that allows me to poll users.
I have found several modules like "Fivestar" and others that do basic polling, but I want to be able to do relative/comparative ranking.  Essentially I want to be able to give users a list of options (say A, B, C), and allow them to rank in order (1, 2, 3) by their opinions.  Ideally this could be done via a draggable table.
For what it is worth, it appears to be on the roadmap for the pollfield module (https://drupal.org/project/pollfield), but that module doesn't appear to be supported.
Any suggestions or leads would be greatly appreciated.
Answer: Rather than use a field for polling, I would recommend using a polling entity created by a module specific to polls. There is a core poll module.
There are also many contributed modules, the choice would depend on your use case: Advanced Poll, Poll Improved, Poll Extras -- and Poll Blocks provide a block for every core poll.
Then add some theming to make the selections draggable. I have created front-end doodads that populate a hidden element on the page - even an entire node body - so you could register the selections that way if you cannot theme the actual poll items.
You may need to use panels/views/context or template file to add the poll to your page.
My favorite place to search for Drupal contributted modules is http://drupalmodules.com/search/node/poll
Answer: If you are open to using a webform instead of an entity form, there's a module for that!  Check out https://www.drupal.org/project/webform_draggable_list

Ranking/polling field or webform module


using Protobuf-Net with f# class

Question: I'm playing f# and making a server program for fun.
I think I can solve this question by using f# to access c# data class but I want to try the f# syntax.
I got f# record with [<CLIMutable>] works but f# class got error
my test code :
open System
open System.IO
open ProtoBuf

[<ProtoContract; Serializable>]
type Point (m_x : int, m_y : int) =
    [<ProtoMember(1)>]
    member this.x = m_x
    [<ProtoMember(2)>]
    member this.y = m_y

[<EntryPoint>]
let main argv =
    let p : Point = new Point(10, 10)
    let out = Console.OpenStandardOutput()
    Serializer.Serialize<Point>(out, p)
    printfn "finish"
    0

and I got following output:
Unhandled Exception:
System.InvalidOperationException: Cannot apply changes to property Program+Point.x
  at ProtoBuf.Serializers.PropertyDecorator.SanityCheck (ProtoBuf.Meta.TypeModel model, System.Reflection.PropertyInfo property, IProtoSerializer tail, System.Boolean& writeValue, Boolean nonPublic, Boolean allowInternal) [0x00000] in <filename unknown>:0
  at ProtoBuf.Serializers.PropertyDecorator..ctor (ProtoBuf.Meta.TypeModel model, System.Type forType, System.Reflection.PropertyInfo property, IProtoSerializer tail) [0x00000] in <filename unknown>:0
...

Answer: Although I don't know much about Protobuf I suspect that its serialization cannot assign a new value to class Point fields because in F# they are immutable by default. You could change your class definition to use mutable fields/properties:
open System
open System.IO
open ProtoBuf

[<ProtoContract; Serializable>]
type Point (m_x : int, m_y : int) =
    let mutable vx = 0
    let mutable vy = 0
    do
        vy <- m_y
        vx <- m_x

    [<ProtoMember(1)>]
    member this.x with get() = vx and set v = vx <- v
    [<ProtoMember(1)>]
    member this.y with get() = vy and set v = vy <- v

Maybe it'll help.
Also if F# record with [] works then maybe it is easier to use records? Records in F# support members as well as classes.
Comment: I just found this myself. I forgot the setter function. Thanks for your quick reply.

using Protobuf-Net with f# class


Correct way to block user from interacting with a table view child while computer updates view

Question: Hi I'm trying to build a simple game, called SOS.  It's a slightly more complicated version of tic tac toe.  The goal of the game is to complete the word SOS.  Players take turns placing letters on a grid. If one player completes the word SOS they score a point and they get to go again until they don't score any points.  I wrote the logic for a computer player and now I'm try to figure out how to update the UI in these steps.
The user is blocked from interacting with the game board ( Table View ) during computers turn.
The computer places a letter on the board and the border is high lighted for half a second to show where the computer is placing it's letter.
If the computer scores a point it places the next letter on the board half a second later.
I'm having trouble keeping the  user from interacting with the board (TableView) while the computer is updating the game board.  For example I tried to block all the tiles with view.setEnabled method to false, then using the postDelayed method to high light the tile where the computer is making it's move, after which I used post delayed again to reset the tile to a S or O.  However the post delayed method is running on a separate thread and the view.setEnabled code is executed to enabled before the postDelayed method finishes.  I think if I used AsyncTask I'll have the same problem where onPostExecute method will execute before the postDelayed method finishes. Does anyone have any suggestions on how I can accomplish this, I've been searching on here but haven't found anything. Thanks for your time.
<code>public Object[] computerMove() {
    // remove players ability to change board
    enableDisableView(game_board, false);

    // used to tell if the move var is null and the
    // game is over
    boolean end = false;

    //declare and initialize the computer player
    AI computer = new AI(game, difficulty);

    // returns null if board is full
    Object[] move = computer.move();

    if (move == null) {
        end = true;
    }

    if (!end && move[0] instanceof Integer &&  move[1] instanceof Integer
            && move[2] instanceof Types) {
        final int row = (Integer) move[0];
        final int col = (Integer) move[1];
        Types type = (Types) move[2];

        // set the tile background image
        tiles[row][col].setTile(type);

        // set occupied
        tiles[row][col].setOccupied(true);
        // update the array in the GamePlayClass
        game.placeOnBoard(type, row, col);

        //add a delay and show where button is placed
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               tiles[row][col].setTile(tiles[row][col].getType());
           }
         }, 1000);
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               tiles[row][col].setBackgroundColor(getResources().getColor(R.color.white));;
           }
         }, 100);

        //find points and then update score
        int points = scorePoints();
        updateScore(points);

    }// end of if statement checking for a null move

    // one less tile remaining,
    --remainingTiles;
    game.decrementRemainingTiles();

    //highlight the correct player
    if (game.getTurn() == 0) {
        // highlight background to indicate turn
        player1Label.setBackgroundColor(getResources().getColor(
                R.color.tomato));
        player1Score.setBackgroundColor(getResources().getColor(
                R.color.tomato));

        player2Label.setBackgroundColor(color.black);
        player2Score.setBackgroundColor(color.black);

    } else {
        player2Label.setBackgroundColor(getResources().getColor(
                R.color.tomato));
        player2Score.setBackgroundColor(getResources().getColor(
                R.color.tomato));

        player1Label.setBackgroundColor(color.black);
        player1Score.setBackgroundColor(color.black);
    }

    // restore players ability to change board
    enableDisableView(game_board, true);

    return move;

}

//copied from stack overflow
//http://stackoverflow.com/questions/3818013/is-there-a-way-to-disable-all-the-items-in-a-specific-layout-programmaticaly/5257691#5257691
private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}
</code>
Comment: Enable the view after you finish updating its color in the UI thread.
Comment: Does that mean run the postDelayed method on the UI thread instead of starting a separate thread?
Comment: Ok I think I understand, your saying just move the enableDisableView method inside the postDelayed method and after the update.  It works, thanks for your suggestion
Answer: Ok so I found a solution.  First I found all the moves the computer player could make and stored the info in Move objects and added each move to a array list.  Second I recursively ( Handler post delay not work in for loop ) iterated through the arraylist, updating the UI with a handler and the postDelayed method.
public void computerPlayerUpdateUI(int index, boolean done, int score) {
<code>    // repeat loop until no more points are found
    while (game.getTurn() == 1 && !done) {

        Object[] moveObj = computerMove();
        int points = 0;
        int player = game.getTurn();

        if (moveObj != null) {
            // calculate points and add to moves arraylist
            points = scorePoints();

            Move move = new Move(moveObj, points, player);
            moves.add(move);
        }

        if (points == 0) {
            // exit loop and update user interface
            done = true;
        }
    }
    if (index < moves.size()) {

            final int x = index;
            final int nextIndex = x+1;
            final int updateScore = moves.get(x).getScore() + score;

            // add a delay and show where button is placed
            final int row = (Integer) moves.get(x).getObject()[0];
            final int col = (Integer) moves.get(x).getObject()[1];
            Types type = (Types) moves.get(x).getObject()[2];

            // set the tile background image
            tiles[row][col].setTile(type);

            // set occupied
            tiles[row][col].setOccupied(true);

            // add a delay and show where button is placed
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    tiles[row][col].setTile(tiles[row][col].getType());
                    player2Score.setText(updateScore + "");
                    // restore players ability to change board
                    enableDisableView(game_board, true);
                    computerPlayerUpdateUI(nextIndex, true, updateScore);
                }
            }, 1000);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    tiles[row][col].setBackgroundColor(getResources()
                            .getColor(R.color.white));

                }
            }, 100);

            // highlight the correct player
            if (game.getTurn() == 0) {
                // highlight background to indicate turn
                player1Label.setBackgroundColor(getResources().getColor(
                        R.color.tomato));
                player1Score.setBackgroundColor(getResources().getColor(
                        R.color.tomato));

                player2Label.setBackgroundColor(color.black);
                player2Score.setBackgroundColor(color.black);

            } else {
                player2Label.setBackgroundColor(getResources().getColor(
                        R.color.tomato));
                player2Score.setBackgroundColor(getResources().getColor(
                        R.color.tomato));

                player1Label.setBackgroundColor(color.black);
                player1Score.setBackgroundColor(color.black);
        }

    }// end of if
    //check for game over only once at end of recursion
    if (index == (moves.size() - 1)) {
        gameOver();
    }
}// end if

public Object[] computerMove() {
    // remove players ability to change board
    enableDisableView(game_board, false);

    // used to tell if the move var is null and the
    // game is over
    boolean end = false;

    //initialize the computer player clones the GamePlay object game
    final AI computer = new AI(game, difficulty);

    // returns null if board is full
    Object[] move = computer.move();

    if (move == null) {
        end = true;
    }

    if (!end && move[0] instanceof Integer &&  move[1] instanceof Integer
            && move[2] instanceof Types) {
        final int row = (Integer) move[0];
        final int col = (Integer) move[1];
        Types type = (Types) move[2];

        // update the array in the GamePlayClass
        game.placeOnBoard(type, row, col);

    }// end of if statement checking for a null move

    // one less tile remaining,
    --remainingTiles;
    game.decrementRemainingTiles();

    return move;

}
</code>


What is the meaning of a line that passes between the G-clef and the F-clef?

Question: What is the meaning of those lines:
Answer: It's actually very simple: it means that the right hand (which was originally playing in the top-most clef) is to play the half note pointed to in the lower clef.
So basically, you are supposed to play the chords in both your hands, then your left hand jumps down to play the octave on the D, and the right hand goes down to play the D below middle C.
The line that goes back up shows that the right hand moves back up into the higher clef to play the chord in the next measure.
Comment: @NeilMeyer, a glissando is a wavy line.
Comment: @EvSunWoodard Because there are big jumps here, the lines might be there to emphasize this (useful when sight-reading). In other cases, these lines might also be used to emphasize a melody line that goes from one hand (/staff) to another.
Comment: A glissando line isn't necessarily wavy, but it doesn't make much sense to write a gliss starting on a 4-note chord and ending on a single note. (Is the single note supposed to be the top note or the bottom one?) Actually, writing a gliss on a 4-note chord doesn't make much sense for piano, period, though it might make more sense on an "unweighted" organ keyboard.
Comment: What's the point of using the line to denote the hand movement when it is already implied by the left hand notes having upsidedown stems?
Comment: How is an indication of a glissando different?
Comment: @EvSunWoodard: In the absence of other markings, the placement of notes in the upper staff indicates that the right hand should play them, and likewise for the lower staff and left hand.  In most cases where simultaneous up-stem and down-stem notes appear, the intention is that they represent different parts which have different rhythms, *but should be played by the same hand in the absence of markings indicating otherwise*.  Also, without the diagonal lines a performer might play the bass clef D's with the left hand and *not even notice* the notes down below.  BTW, I don't think this...
Comment: ...is very well printed, because it would suggest that a performer should hold the notes played on the first beat of each measure while playing notes that are many octaves away.  I think the score would be clearer if the first beat used quarter notes, and had a pedal marking on the first beat to show that the pedal should sustain those notes for the rest of the measure.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/56506/discussion-between-kyle-strand-and-supercat).
Comment: @EvSunWoodard Karlo is correct; also, stems are not consistently/reliably used to indicate which hand should play a note.
Comment: @supercat It is up to the composer to select note durations, and it's quite common to have durations that aren't reasonable without the pedal--often without actually specifying how the pedal is to be used. Also, in any real piece, any indication of which hand should play which notes is strictly a courtesy; performers should always feel free to switch which hand plays a note if that makes it easier to play well.
Comment: @KyleStrand: The right-hand part has a dotted half note followed by a half note.  If the half note is supposed to start on the second beat, there should be either a note or a rest of single-beat duration *in the right-hand part*.  Using a quarter rest near the bottom of the treble clef might be better than making the dotted half notes into quarter notes, but there IMHO there should be something that indicates where the second note starts that isn't printed way at the bottom.
Comment: @supercat What the composer is indicating here are the *voices* involved--the hands of the pianist are merely a means to an end. Two vertically-aligned quarter rests could be appropriate, but there certainly should *not* be a rest that is in completely separate staff from the *voice* to which it corresponds.
Comment: @KyleStrand: As I would read/notate it, each staff has two voices during beat 1, one of which has a rest.  The lower voice from the upper staff then crosses down to the lower staff.  Otherwise, there's nothing in the upper staff that would represent beat 1 of the voice from the upper staff which is playing the half notes on beat 2.
Comment: @supercat There is only one voice in the upper staff, though.
Comment: @KyleStrand: What voice is affected by the diagonal lines?  It looks to me like it's the one playing the "d" half note in the middle of the bass clef in the first measure.  The first diagonal line would suggest that the voice came from the upper staff.  So what did it do there prior to that first diagonal line?  By my reckoning, during beat 1 of the first two measures, there should be two voices in each staff, with the lower ones having rests.  During beats 2-3, there would be one voice in the top staff (with the sustained chord), and three voices in the lower staff.
Comment: The upper and lower voices of the lower staff would have half notes, while the "middle" voice of the lower staff would have the dotted half notes sustained from beat 1.  I would interpret the diagonal lines as saying "treat the following as though it were on the treble clef with a lot of ledger lines".  If the "d" had been on the treble clef with four ledger lines, it would have needed a rest before it.  I would thus consider the rest appropriate here as well.
Answer: Perhaps this is a comment and not an answer.
In music scored for the Barbershop style, such a diagonal line indicates that the melody is passing from one part to another.  Reasoning by similarity, the diagonal line may indicate that the melodic line moves in an unexpected way, which could alter the interpretation.


Trocando link conforme input text

Question: Preciso criar um input de texto que, conforme eu insira uma quantidade, ele mude o link de um botão, mas somente uma parte do link.
Por exemplo, tenho esse link:
https://sterilair.vtexcommercestable.com.br/checkout/cart/add?sku=17&qty=1&seller=1&sc=1

Quero que sempre que a pessoa coloque 1,2,3,4... no input ele mude o valor qty=1 do link que a pessoa vai clicar a abaixo.
Answer: Sim, é possível. Você irá usar o evento change para fazer isso.

$(document).ready(function(){
    $("#qty").change(function(){
       val = $(this).val();
       newURL = "https://sterilair.vtexcommercestable.com.br/checkout/cart/add?sku=17&qty=" + val + "&seller=1&sc=1";
      // alert(newURL);
       $(".log").append("URL trocada para:
"+newURL+"
");
       $(".url").attr({href:newURL});
    });
});
.log{
  height: 400px;
  overflow: auto;
  border: 1px solid black;
}

<input name="qty" id="qty" type="number" />

UFL

Dessa forma, sempre que o valor da quantidade for trocado, a URL será alterada para a quantidade inserida.
Answer: Você pode fazer isso com jQuery. Por exemplo:
Quantidade: <input type="text" name="qtd">

<input id="botao" type="button" value="Go to Google" />

Veja que, de acordo com o que for preenchido no input, o script jQuery irá formar a URL.
Comment: Olá luã, irei testar o mesmo pra ver se funciona, mais agradeço desde já ! :D
Comment: Caso você tenha algum problema para implementa ou duvida com o jquery estou a disposição. Boa Sorte
Answer: Possível é, afinal de contas, na informatica tudo é possível até que se prove o contrário. Segue a solução abaixo:

$("div#div_do_link #input_link").keyup(function() {
  var value = this.value;
  if (value) {
    var link = "https://sterilair.vtexcommercestable.com.br/checkout/cart/add?sku=17&qty=" + value + "&seller=1&sc=1";
    $('div#div_do_link a#ancora_link').attr('href', link);
  }
})

  <input type="text" name="input_quantidade" id="input_link" />

  Link

Trocando link conforme input text


How can I show that $|Y|\leq c$ implies $|E[Y\mid{\mathcal G}]|\leq c$?

Question: This is an exercise mentioned in this question:

Let $Y$ be an integrable random variable on the space $(\Omega,{\mathcal A},{\bf P})$ and $\mathcal{G}$ be a sub $\sigma$-algebra of $\mathcal{A}$. Show that $|Y|\leq c$ implies $|E[Y\mid{\mathcal G}]|\leq c$.

It has been shown that using $E[|y|\mid{\mathcal G}]$ to estimate $|E[Y \mid{\mathcal G}]|$ is not a good idea due to did's answer to the previous question.
One more step I can go so far is $E[Y]=E[E[Y\mid{\mathcal G}]]$. I don't think this would work since expectation only gives the average.

So how can I get $|E[Y\mid{\mathcal G}]|\leq c$ using $|Y|\leq c$?
Answer: Let $F_n:=\{E[Y\mid\cal G]\geqslant c+n^{-1}\}\in\cal G$. Then
$$c\mathbf P(F_n)\geqslant (c+n^{-1})\mathbf P(F_n),$$
so $\mathbf P(F_n)=0$ for all $n$, hence $E[Y\mid\cal G]\leqslant c$ a.s. Doing the same for $-Y$, we get the wanted result.
Answer: First
$$
\left|E[Y\mid\mathcal G]\right|\leq E[|Y|\mid \mathcal G]
$$
by Jensen's inequality. Now, let $Z=E[|Y|\mid \mathcal G]$, then for every $G\in\mathcal G$ we have
$$
\int_G Z=\int_G|Y|\,\mathrm dP\leq \int_G c\;\mathrm dP,
$$
and since both $Z$ and $c$ are non-negative and $\mathcal{G}$-measurable, we have that $Z\leq c$ almost surely.

To see that the last statement is true, we consider the non-negative random variable $(Z-c)1_{\{Z>c\}}$ which has mean $0$ as
$$
0\leq E\left[(Z-c)1_{\{Z>c\}}\right]= \int_{\{Z>c\}} Z\,\mathrm dP-\int_{\{Z>c\}} c\,\mathrm dP\leq 0
$$
since ${\{Z>c\}}\in\mathcal{G}$. This implies $(Z-c)1_{\{Z>c\}}=0$ a.s. and hence $Z=c$ a.s.
Comment: thank you for your answer. how do you come up with the last sentence?
Comment: @Jack: See the edit.
Answer: If $P(Z \leq X)=1$ then $E[Z \vert \mathcal{G}]\leq E[X\vert \mathcal{G}])$ a.s.
Use $Z=|Y|$ and that $c$ is a $\mathcal{G}$-measurable stochastic variable. Then your own observation that $|E[Y\vert \mathcal{G}] | \leq E[|Y| \vert \mathcal{G}]$ pretty much seals the deal.


How to add country phone number prefix in webform?

Question: I have a webform, with a field for phone number.
I would like to have another select dropdown field before phone number, with country name and country phone number prefix.
Eg:
Singapore (65)
United Kingdom (44)
Venezuela (58)
Comment: Have you tried https://www.drupal.org/project/webform_phone ?
Comment: Yes I had tried this, but it does not provides a drop down in the webform, for user to choose their country with country phone code
Answer: Webform allows you to load options in bulk. Just google for this list and load it into a spreadsheet. Use a bit of find/replace to get it into the format needed by a webform select element, which is:
<code>65|Singapore (65)
44|United Kingdom (44)
58|Venezuela (58)
</code>
Then create a webform select field and paste it in.
To get the fields to line up side-by-side (with country code right before phone number and not on a separate line) use the webform_layout module.
Comment: I forgot about this options in bulk! Thank you so much!
Comment: This is not a good solution because some countries have the same prefix, so some keys have the same value and this is not allowed
Comment: You'd have to consolidate the duplicates onto a single line, e.g. if both Country A and Country B share the code `123`, then put them on the same line like this:

`123|Country A/CountryB (123)`


How do I modify the "has_js" cookie?

Question: I have a web application deployed through Drupal 7.43. I need to modify the has_js cookie that is set by Drupal to make it both Http-only and Secure for a site security review.
What are pros and cons of making the has_js cookie Http-only and Secure?
Answer: The has_js cookie is set, to indicate JavaScript is enabled, in drupal.js with the following code.
// 'js enabled' cookie.
document.cookie = 'has_js=1; path=/';

The HttpOnly flag makes the cookie not available from JavaScript.
For that reason, it's not possible to set the HttpOnly flag when creating a cookie from JavaScript. It may cause issues if there is code that checks for that cookie to determine if JavaScript or cookie support is enabled.
The Secure flag is used to make the cookie only available over HTTPS, which may cause issues with checking for JavaScript support on non HTTPS pages. Nevertheless, if your site is fully HTTPS, that should be fine.
With that being said, you could look into setting the HttpOnly flag on the has_js cookie server side, by checking for that cookie and updating it to use the HttpOnly flag via setcookie().

How do I modify the "has_js" cookie?


Caused by: android.database.sqlite.SQLiteException: near "Appliances": syntax error (code 1):

Question: I have 5 tables in SQLITE when I try to compile it says "Syntax error" But I can't find any syntax error in the code.
Here is my Code
override fun onCreate(db: SQLiteDatabase?) {
        var ClothTable = "CREATE TABLE $Cloth (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description TEXT ,IMAGE BLOB)"
        var DairyTable = "CREATE TABLE $Dairy (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description TEXT ,IMAGE BLOB)"
        var EATable = "CREATE TABLE $ElectricalAppliances (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description TEXT ,IMAGE BLOB)"
        var FoodTable = "CREATE TABLE $Food (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description TEXT ,IMAGE BLOB)"
        var VegetableTable = "CREATE TABLE $vegetable (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description TEXT ,IMAGE BLOB)"

        db?.execSQL(ClothTable)
        db?.execSQL(DairyTable)
        db?.execSQL(EATable)
        db?.execSQL(FoodTable)
        db?.execSQL(VegetableTable)

    }

Error is :
Caused by: android.database.sqlite.SQLiteException: near "Appliances": syntax error (code 1): , while compiling: CREATE TABLE Electrical Appliances (_id INTEGER PRIMARY KEY AUTOINCREMENT,Description STRING ,IMAGE BLOB)

Comment: You cannot have space in table names
Answer: I guess you have declared the variable ElectricalAppliances like this:
val ElectricalAppliances = "Electrical Appliances"

as a table name but the space is not allowed.
Change it to:
val ElectricalAppliances = "[Electrical Appliances]"

The name must be surrounded with square brackets [ ] or backticks ` (ascii code 096).
Comment: Thanks alot brother , It works I did'nt know before that sqlite not accpet white spaces
Comment: No white spaces for table names, columns, indexes etc.
Comment: If this answer solved your problem consider accepting it.


How can i parse to TextField iOS Keychain in Swift?

Question: I saved my account through Locksmith Keychain, everything works fine, right now i need to parse the data stored to a textField, how can i do it please?
Here is the code that i use to save my account:
let accountsave = Locksmith.saveData(["account": self.txtAccount.text], forUserAccount: "myUserAccount", inService: "myAccount").

Right now i need to parse the data store to a textfield in swift. Please help
Answer: From what I understand of your question you're asking for something similar to this:
let (dictionary, error) = Locksmith.loadDataForUserAccount("myUserAccount", inService: "myAccount")
if let userData = dictionary as? [String:String] {
    if let account = userData["account"] {
        self.txtAccount.text = account
    }
}

Comment: i have this error in the second line : 'NSDictionary? does not have a ember named 'subcript'
Comment: Check my answer again, I forgot to unwrap the optional value.
Comment: I have another problem, try to see if you can help me to resolve it. Here is the link: http://stackoverflow.com/questions/29972336/fatal-error-array-index-out-of-range-in-uitableview-on-back-to-previous-control?noredirect=1#comment48064812_29972336


PHP - Converting HTML Table to CSV adds empty lines

Question: I have a simple PHP script that converts a HTML Table into a CSV file. Due to UTF-8 converting reasons I added fwrite($fp,"\xEF\xBB\xBF"); and everything works finde besides of additional empty lines (every second one) while opening the CSV files in MS Excel (Mac and Windows - latest versions).
Opening this with Macs Preview doesn't show that empty lines - and when I remove the fwrite($fp,"\xEF\xBB\xBF");it also doesn't add the empty lines but then of course my text is destroyed.
This is my PHP script:
<?php

include('simplehtmldom_1_5/simple_html_dom.php');

$table = file_get_contents('hotel.html');
$html = str_get_html($table);

//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-Encoding: UTF-8");
header("Content-type: text/csv; charset=UTF-8");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header('Content-Disposition: attachment; filename=pkb.csv');

$fp = fopen("php://output", "w");
fwrite($fp,"\xEF\xBB\xBF");

foreach($html->find('tr') as $element)
{
    $td = array();
    foreach( $element->find('th') as $row)
    {
        $td [] = $row->plaintext;
    }
    fputcsv($fp, $td, ';');
    $td = array();
    foreach( $element->find('td') as $row)
    {
       $td [] = $row->plaintext;
    }
    fputcsv($fp, $td, ';');
}
fclose($fp);

?>

The HTML Table is quite common:

...

And the converted CSV opened with Excel looks like that:
CSV opened in Excel with empty rows
Maybe you find my mistake which generates these additional empty lines.
Thanks a lot guys
Answer: Problem is duplicate iterations
foreach($html->find('tr') as $element) {
    $td = [];
    foreach( $element->find('th') as $row) {
        $td[] = $row->plaintext;
    }

    if (!empty($td)) {
        fputcsv($fp, $td, ';');
    }

    $td = [];
    foreach( $element->find('td') as $row) {
       $td[] = $row->plaintext;
    }

    fputcsv($fp, $td, ';');
}

Something like that is a simple fix, but I would suggest rewriting the entire foreach.
Comment: Hi decanus, thanks for your fast reaction. I also thought about that, but there is definitely no empty value in my table structure.
Comment: @neilhurt I found the issue, its the first foreach. When you foreach on th.  only exists in one row. But you iterate and add an empty td for every row. See my edited answer
Comment: Wow, that's it. Thanks a lot decanus


How can you map server dependencies in an undocumented environment?

Question: Imagine this situation:
CompanyA is acquiring a subdivision of CompanyB. CompanyB lays off most of their staff for that subdivision and is not helpful when asking for documentation. Now, you must use ADMT to migrate users, groups, workstations, and servers from CompanyB's child domain into CompanyA's Active Directory.
How can you determine which servers hosting custom LOB or pre-packaged applications have dependencies on other servers in the environment so that a migration strategy can be planned accordingly? Something like VMware vCenter Infrastructure Navigator can do some of this, but are there other ways besides spending a large amount of time just poking around?
Answers can assume an all Windows environment running on vSphere 5.1, though answers for other situations are OK as well.
Comment: One approach (among others that you'll need to pursue) is to start with the end user and work your way out from there. Identify a key user in each functional area (accounting, shipping, etc.) (preferably a department head or someone who has a keen grasp of the LOB's in use) and identify the LOB's they're using. Use that information to "build" a dependency tree/view of the systems that support those LOB's. Things like AD/DNS, email, etc. should be fairly easy to flesh out. Others, like CRM, ERP, custom shipping applications, etc. will probably take more involved digging.
Comment: If you interview users for this, which should certainly form at least part of your approach, make sure you specifically dig for special occasions such as accountancy year end or any similar periodic things that they take for granted but won't think of because of how rarely they occur.
Answer: I've been in your shoes. There is no single answer that will do it all.
You can spend a lot of money on a discovery product and hope that it knows all about your applications. They do exist and some are supposed to be quite good.
Of course, it might not be as good as you need or want. And nothing will find the "dependency" that is introduced by a daily or weekly scheduled script on a seemingly-unrelated server that is responsible for taking an extract from a work-order system and FTP-ing it into a payroll system. Or the physical fax line on the Linux box that you somehow didn't notice during your inventory ...
I like Joe's answer a lot - starting from each department and working with their "power users" (who definitely might not be computer "power users") is a vital part of a comprehensive discovery project. That's the bottom-up approach. This is also where you'll find people running business-critical apps on their own machines, possibly shared out to their own workgroup.
Another part of the approach is to get into every machine and run something that shows, or even better logs, TCP and UDP connections for $period_of_time and see if you can catch traffic (ports and endpoints, probably don't want a full network capture) related to unknown applications. Likewise with inventorying scheduled tasks, service accounts, etc. That's the BFMI top-down approach.
Because of the possibility of non-connection-oriented asynchronous processes, and things that don't run on servers (or client apps that simply run from fileshares), I don't think there can be a single automated approach for this. It's going to be manpower-intensive to do it right. Of course, you can simply aim for 80% and then start migrating or decomming, with enough communication to catch the things that make users scream when they break.
Comment: The users screaming when things don't work is a good method to get out the "smaller" stuff that's overlooked during migration :) although, probably not the best approach.
Comment: There's always an element of risk when migrating or decomming undocumented business systems. That's what they get for technical debt, thanks for playing IT with us this week.
Answer: The single most effective way is to turn off each server, one-by-one, and note what breaks and what complains (but still functions).
It's especially helpful if you have good monitoring, alerting, log analysis (logstash, splunk, etc), and metrics in place beforehand.
This is not a Best-Practice.
Or...
Another approach is to take note of all services and processes running on each server, excluding the ones common to every machine (base Windows processes/services, Antivirus, etc...)
Comment: I've got to admit this is the first thing I thought of too... Definitely not a best practice but probably the most expedient option.
Comment: And yes, that's a poor way to run things...


By using SMOTE the classification of the validation set is bad

Question: I want to do classification with 2 classes. When I classify without smote I get:
Precision       Recall            f-1
0,640950987     0,815410434       0,714925374

When I use smote: (oversample the minority class at 200% and k = 5)
Precision        Recall           f-1
0,831024643      0,783434343      0,804894232

As you can see this works well.
However, when I test this trained model on validation data (which hasn't got any synthetic data)
Precision        Recall           f-1
0,644335755      0,799044453      0,709791138

which is awful. I used a random decision forest to classify.
Has anyone got any idea why this is happening? Any useful tips regarding extra tests I can try to get more insight are welcome too.
More info:
I do not touch the majority class. I work in Python with scikit-learn and this algorithm for smote.
The confusion matrix on the test data (which has synthetic data):

The confusion matrix on the validation data with the same model (real data, which was not generated by SMOTE)

Edit: I read that the problem possibly lies in the fact that Tomek Links were created. Therefore I wrote some code to remove the tomek links. Though this doesn't improve the classification scores.
Edit2: I read that the problem possibly lies in the fact there is too much of an overlap. A solution for this is a more intelligent synthetic sample generation algorithm. Therefore I implemented

ADASYN: Adaptive Synthetic Sampling Approach for Imbalanced Learning

My implementation can be found here. It performed worse than smote.
Answer: If you oversample the training data to change the relative class frequencies in the training set, you are implicitly telling the classifier to expect the validation set to have those same class frequencies.  As the class frequencies influence the decision boundary, if the validation set class frequencies are different, then the classification boundary will be wrong as a result and performance will be sub-optimal.
Now it is true that for some classifiers, having imbalanced classes can cause it to perform badly, and re-sampling the data can help to correct this.  However if you oversample the minority class too much, you will over-correct and performance will be suboptimal because the the difference in class frequencies between the training and validation sets is more than is needed to correct the imbalance problem.
My recipe then would be to use cross-validation to choose the degree of oversampling that is required to just compensate for the "class imbalance problem" experienced by your classifier, but no more.  Note however this leads to an additional parameter that needs to be tuned, which leads to greater computational expense, and a higher likelihood of over-fitting in model selection (if the total number of tunable parameters is non-negligible).
Comment: This extra parameter you talk about, is this the % I use ? (in my case 200%). I can't choose a smaller one, they have to be in steps of 100. Though if I understand correctly, if I make validationset which is balanced like the training set, it will give better results ?
Comment: yes, the %age was indeed the parameter I meant; it is a pity if the software only allows this to be changed in steps of 100.  Some machine learning algorithms (e.g. the SVM) allow you to weight positive and negative patterns differently (for the SVM have a different value of the parameter C for each class).  This is directly equivalent to SMOTE in the case of the SVM, so that may be another way to get around the problem.
Comment: I tried the SVM solution. In sci-kit learn it is possible to set the weights inversely proportional to class frequencies (automatically). Though this gives very bad results ( for linear, polynomial, rbf and sigmoid kernel)
Comment: If you just set them inversely proportional to the class frequencies that tells the SVM to act as if the class frequencies in the validation set are equal, which again is likely to over-compensate for the class balance problem.  Try tuning the weights via cross-validation, it normally works for me.  The other thing to do is to consider whether the misclassification costs actually are the same for false-positive and false-negative errors, and if not, include this in setting the C parameters for each class.
Comment: Hmmm I've tried tuning the weights manually though what the SVM predicts is or all 1s or all 0s. No in-between. (Btw I've added confusion matrices to the question for clarification, as you can see by using SMOTE, the unbalance is gone when using a test set which is also composed of synthetic data but there is still in unbalance when a test is run on the validationset.)
Answer: Have you considered using the R implementation of random forest via Rpy? It has an option that allows you to choose both the sample size that each tree is trained on and the fraction falling into each class. I've found it to be the most effective way to deal with imbalance when using random forest.


Proposal for Custom Off-Topic Close Reason

Question: I have created a proposal for a new option for closing homework questions that don't meet the standards that we've set forth. I anticipate the benefit of this being that the reason for closing will be a little more clear for new-comers posting homework questions. Also, there will be less work involved in closing a question because you wont have to write a comment describing the reason every time you vote to close a homework question.
When you choose to close a question, there is an option to edit reasons.

I clicked this and proposed the following reason as a new option:

This question does not meet the standards for homework questions as
  spelled out in the relevant meta posts. For more information, see
  here
  and here
  .

I'd like to get some feedback on if you think this should be a new option and if it needs any re-wording. When/if we decide on it, it needs another moderator to approve it. The link to approve it can be found here: https://economics.stackexchange.com/admin/flags/close-as-off-topic
Comment: I just saw this. I can't believe I missed it earlier. +100000
Comment: There seems to be a tpyo in the new option: question -> questions
Comment: Fixed it. Tahnks! :)
Comment: Seems like a good feature to add.~
Answer: Edit 13 November 2015:
I have now approved this and we should be able to start applying the new close reason.

Original post
Thanks, I didn't realise that we had the capacity to add such reasons. I think we should definitely ratify this, and will gladly approve it! But first I wondered if it might be better to construct a canonical FAQ for homework questions and link to that, rather than linking to meta discussion pages where people may encounter conflicting (and thus confusing) opinions.
I took the libery of editing the proposal along the lines that I had in mind

This question does not meet the standards for homework questions as spelled out in the relevant meta posts. For more information, see our policy on homework question and the general FAQ.

Let me know if you think this is okay and I will "approve" it.
Comment: Cool. I agree. Thanks for setting that up. Looks good to me so far. As soon as we get some more votes, let's go ahead and confirm it.


Is it okay if someone offers money for providing an answer?

Question: I've seen such situations where users are so desperate to get quick solution that they often offer money to get their issue solved.
I personally feel that we must offer free solution without any expectation of monetory benefits. So, I've refused such offers everytime. But I'm interested to know if someone accepts the offer, is he breaking rules?
Answer: Short answer: NO.
Long answer: You should flag any question/answer/comment in which the OP requests or offers money.


zsh function case condition parse error near `)'

Question: I'm following this blog to setup a zsh function to switch aws cli profiles
: https://mads-hartmann.com/2017/04/27/multiple-aws-profiles.html
This is the zsh function in the blog:
function aws-switch() {
    case ${1} in
        "")
        clear)
            export AWS_PROFILE=""
            ;;
        *)
            export AWS_PROFILE="${1}"
            ;;
    esac
}

#compdef aws-switch
#description Switch the AWS profile

_aws-switch() {

    local -a aws_profiles

    aws_profiles=$( \
        grep '\[profile' ~/.aws/config \
        | awk '{sub(/]/, "", $2); print $2}' \
        | while read -r profile; do echo -n "$profile "; done \
    )

    _arguments \
        ':Aws profile:($(echo ${aws_profiles}) clear)'
}

_aws-switch "$@"

I added these lines to my ~/.zshrc, when I run source ~/.zshrc
It gives /.zshrc:4: parse error near `)'
I read the zsh function doc but still not very good at understanding the syntax and how could I fix this.
Answer: Have a look at the zsh man page (man zshmisc):
case word in [ [(] pattern [ | pattern ] ... ) list (;;|;&|;|) ] ... esac
As you see, you have to separate multiple pattern by |:
case $1 in
  |clear)
    ....

Comment: For C-like fall through, you can also use `"") : ;&`, which executes the no-op `:` and terminates the case with `;&`, which causes the next block to be execute as well as if it matched `$1`.
Comment: so where exactly should I put this | at? I tried to put infront of clear like in your suggestion and many other positions, but none of them worked.
Comment: Post your complete modified code. It should work. I tried it out with my Zsh, version 5.5.1.


A puzzle from Perfectly Reasonable Deviations from the Beaten Track by Richard Feynman

Question: The following is a puzzle I found while reading Perfectly Reasonable Deviations from the Beaten Track by Richard P Feynman.
There are 2 shops which sell oranges.At Shop A you get 2 oranges for 5 cents.At Shop B you have to pay 3 cents for each orange.Richard bought some oranges from both the shops.He totally spent 19 cents.How many oranges did Richard buy?How many did he buy from each shop?
Note that Richard did NOT buy 7 oranges.I tried all the conventional methods I know and still ended up with 7 but it's specifically mentioned that the answer is not 7.
Thanks in advance.
Comment: At the risk of sounding facetious: 6. -2 from Shop A and 8 from Shop B for a total of -5 + 24 = 19 cents.
Comment: Are you sure it isn't a typo or some stupid linguistic trick? You are right that the only solution to $5a + 3b = 19$ where $a, b$ are positive integers is $a = 2, b = 3$ (since we have $b \equiv 3 \bmod 5$ and $b = 8$ is too large)., so Richard must have bought $4$ oranges at the first shop and $3$ oranges at the second shop. Any other answer is a cheat. Can you tell us what page the puzzle appears on?
Comment: @Qiaochu: See the link in my answer.
Comment: It's even "better" if we allow our shopper to buy negative oranges from Shop B. Then you can use the price difference to buy an unbounded number of oranges. E.g. $-7-5k$ oranges from Shop B (leaving him with $19 + (-3)\cdot(-7) + (-3)\cdot(-5k) = 40 + 15k$ cents) and ${40 + 15k \over 5/2} = 16+6k$ oranges from Shop A for a grand total of $9+k$ oranges. [Insert something about silly questions and silly answers here :-)]
Answer: I think you misunderstood what Feynman was saying. If you're referring to the February 29, 1944 letter to his mother: He says that unfortunately he didn't have any problems whose answer was $7$ oranges, and then he makes one up whose answer is $7$ oranges; as far as I can tell he doesn't say anywhere that the answer to this one isn't $7$ oranges.
Comment: Yes, I just found that page and I agree with your interpretation.
Comment: @joriki Yes, you do have a point.I think I misunderstood.But even then, is it possible? Have you considered the idea of selling oranges?
Comment: @Green: not unless you do something outside of the implicit confines of the puzzle, and there are a million ways to go outside the confines of the puzzle.
Comment: @Yuan Just for curiosity's sake can you mention one of them?
Comment: @Green: kahen considered the idea of selling oranges in a comment under the question. Other ideas mentioned in a now-deleted answer were that one of the shop owners may have given him a discount, or he may have spent money on other things.
Comment: @Qiaochu Are you notified when he used \@Yuan instead of the first characters of your name?
Comment: @mixedmath: No, I don't think I was.
Comment: @J.M.: How did you obtain that "decrufted" link? :-)
Comment: Well, for reasons too long to get into, I've figured out partially what stuff to retain in an otherwise inconveniently long Google Books URL... :)


Calling protocol default implementation from regular method, when protocol has associated type

Question: I have a protocol that has a static method with a default parameter. I want to change the default value in a class that implements the protocol. Essentially doing what is easily done with classes and super.
I only have a solution when the Protocol has no associated type.
The following code works, but as soon as you uncomment the associated type declaration, it doesn't compile.
protocol Protocol {
//    associatedtype AssociatedType
}

extension Protocol {
    func sayHello(name: String = "World") {
        print("Hello, \(name)!")
    }
}

class Class<T>: Protocol {
    typealias AssociatedType = T

    func sayHello(name: String = "Stack Overflow") {
        // Uncommenting the Protocol.AssociatedType causes:
        // Protocol can only be used as a generic constraint because it has associated type requirements
        (self as Protocol).sayHello(name)
    }
}

Class<()>().sayHello()

I do understand why it doesn't compile: Protocol has no concrete type for AssociatedType.
So maybe the question should read "Can I explicitly specialize a protocol?", to which I believe the answer is no.
I have a partial workaround. But even when it works, it sucks.
Especially when you consider that I'm writing a library where sayHello is public, so the following workaround forces me to have a second protocol, which has to be public, but is useless.
Here's the workaround:
protocol Parent {}

protocol Protocol: Parent {
    associatedtype AssociatedType
}

extension Parent {
    func sayHello(name: String = "World") {
        print("Hello, \(name)!")
    }
}

class Class<T>: Protocol {
    typealias AssociatedType = T

    func sayHello(name: String = "Stack Overflow") {
        (self as Parent).sayHello(name)
    }
}

Class<()>().sayHello()

But this doesn't work for me, because my sayHello uses the associated type. So it can't be extracted to another protocol.
Just to be sure I'm clear, here's what I'd like, only substituting the class for a protocol:
class Protocol<T> {
    func sayHello(name: String = "World") {
        print("Hello, \(name)!")
    }
}

class Class<T>: Protocol<T> {
    override func sayHello(name: String = "Stack Overflow") {
        super.sayHello(name)
    }
}

Class<()>().sayHello()

Answer: You're trying to reinvent inheritance in protocols, and there is no such thing. But it is trivial to get what you're talking about; just say what you mean. You don't mean "I want to do the thing I inherited." You mean "I want to do some common behavior." Just provide a name for that common behavior. This removes all ambiguity about which one you mean.
protocol Protocol {
        associatedtype AssociatedType
}

extension Protocol {
    // Put the default behavior on the protocol, not on the instance
    // Of course you could also put it on the instance if that were convenient.
    static func defaultSayHello(_ name: String = "World") {
        print("Hello, \(name)!")
    }

    // If you want a default on the instance, too, provide one that we an override
    func sayHello(_ name: String = "World") {
        Self.defaultSayHello(name)
    }
}

class Class<T>: Protocol {
    typealias AssociatedType = T

    func sayHello(name: String = "Stack Overflow") {
        // Now the default behavior lives on my type
        Class.defaultSayHello(name)
    }
}

// But other types can get default behavior
class OtherClass<T>: Protocol {
    typealias AssociatedType = T
}

Class<()>().sayHello() // Hello, Stack Overflow!
OtherClass<()>().sayHello() // Hello, World!

The one frustrating part about this is that Swift provides no way to limit defaultSayHello to implementers of Protocol. So technically anyone can call it. It can sometimes be worth prefixing it with an _ to indicate that outsiders shouldn't. This is a basic access control problem in protocols, having nothing to do with this specific question; it comes up all the time when you want "things my implementers can use on themselves, but shouldn't be called randomly." Swift doesn't have a solution for that today.
Comment: I don't want to accept this answer because, as you pointed out, it pollutes the public API, which I'm really trying to avoid. It did inspire me though, so I posted my own answer (which also doesn't answer the question).
Answer: Inspired by Rob Napier's answer, here's what I went with; good old overloading for defaults:
protocol Protocol {
    associatedtype AssociatedType
}

extension Protocol {
    func sayHello(name: String = "World") {
        print("Hello, \(name)!")
    }
}

class Class<T>: Protocol {
    typealias AssociatedType = T

    func sayHello() {
        self.sayHello("Stack Overflow")
    }
}

Class<()>().sayHello()      // Hello, Stack Overflow!
Class<()>().sayHello("you") // Hello, you!

This does fit my needs, but doesn't answer the question. So I'm not 100% satisfied.
I believe Rust gets this one right by allowing traits/protocols to be generic both using X<T> and associated types.


React: What happens on consecutive renders where a list shuffles?

Question: I am trying to create a list which shuffles randomly with some animation.
Here is the fiddle for it where I have used key prop to identify each child.
http://jsfiddle.net/1wcpLLg4/
var ListAnimate = React.createClass({
    list: [
        {id: 1, caption: "Hello"},
        {id: 2, caption: "There"},
        {id: 3, caption: "Whatsup"},
        {id: 4, caption: "Sanket"},
        {id: 5, caption: "Sahu"},
    ],
    shuffle: function() {
        this.list.shuffle(); // Shuffles array!
        this.forceUpdate();
    },
    render: function() {
        return

            <button onClick={this.shuffle}>Shuffle</button>

                {this.list.map(function(el, i){
                    return
{el.caption} {el.id}
;
                })}

;
    }
});

React.render(<ListAnimate />, document.body);

From the React docs about Dynamic Children, it states that key prop is used in order to identify the elements in an array during successive renders. So, that the items which are just re-ordered must not unmount and mount to new position rather they should just be re-positioned but the does not seem to be happening in the fiddle, where the Nodes at the top of the list are always being unmounted and being mounted at a different position.
But for the elements at the bottom seems to be working well with animation.
Comment: You need to click Fork in the fiddle when you're done making changes.  The link just goes to the hello world fiddle.  Also you need to include the code from your fiddle in the question, in case jsfiddle goes away.
Comment: Sorry, updated it now! (y)
Answer: Keep in mind that for the kind of animation you are looking for, you need to always keep the DOM in the same order and only update their position in the render function of your component.
I modified your first fiddle using this strategy: http://jsfiddle.net/0maphg47/1/
render: function() {
    // create a sorted version of the list to render the DOM
    var sortedCopy = this.state.list.slice().sort(function(a, b) {
        return a.id - b.id;
    });

    return

        <button onClick={this.shuffle}>Shuffle</button>

            {sortedCopy.map(function(el, i) {
                // find the position of the element in the shuffled list
                // which gives the position the element must be
                var pos = this.state.list.indexOf(el);
                return

                    {el.caption} {el.id}

;
            }, this)}

;
}

There is still room for improvement but I'll leave that up to you.
Comment: Have you read this article about React's algorithm? In the last section, "[...] you can express the fact that a sub-tree has been moved amongst its siblings, but you cannot tell that it has moved somewhere else." http://facebook.github.io/react/docs/reconciliation.html
Comment: Thanks for your detailed answer and the fiddle! This works well but my solution should also work. It looks like there is a problem with React's render function (as mentioned in the comment of the other answer)
Comment: Thanks!  You helped me with a separate problem I was having with copying JS arrays, `.slice()` was the key piece I needed in `this.state.list.slice().sort...`
Answer: I have created a fiddle to show that li elements are not actually being mounted / unmounted.
http://jsfiddle.net/jq9p7hnd/
I have converted li element to MyLi element and logged messages when the componentDidMount and componentWillUnmount functions are called. Only the componentDidMount callbacks are called during the first render and none of them are called after shuffle:
var MyLi = React.createClass({

    componentDidMount : function(){
        console.log("MyLi component did mount.");
    },

    componentWillUnmount : function(){
        console.log("MyLi component will unmount.");
    },

    render : function(){

        return
{this.props.children}

    }
});

var ListAnimate = React.createClass({
    list: [
        {id: 1, caption: "Hello"},
        {id: 2, caption: "There"},
        {id: 3, caption: "Whatsup"},
        {id: 4, caption: "Sanket"},
        {id: 5, caption: "Sahu"},
    ],
    shuffle: function() {
        this.list.shuffle();
        this.forceUpdate();
    },
    render: function() {
        return

            <button onClick={this.shuffle}>Shuffle</button>

                {this.list.map(function(el, i){
                    return <MyLi key={el.id} style={ {top: (i*60)+'px'} }>{el.caption} {el.id}</MyLi>;
                })}

;
    }
});
window.React = React;
React.render(<ListAnimate />, document.body);

Array.prototype.shuffle = function() {
  var i = this.length, j, temp;
  if ( i == 0 ) return this;
  while ( --i ) {
     j = Math.floor( Math.random() * ( i + 1 ) );
     temp = this[i];
     this[i] = this[j];
     this[j] = temp;
  }
  return this;
}

Comment: If it re-orders the existing DOM, then why do only those elements are animating which are moving from bottom to top and does not work with the elements from top to bottom? In Google Chrome's debugger, it loses the selected element in Elements panel if it moves down but the selection remains for the elements which rise up.
Comment: Good debug, but what could be wrong with the positioning? That's not a CSS issues, I checked in Inspector Elements panel.
Comment: I think React reorders the actual DOM elements (it does not throw away existing DOM elements and React class instances and create new ones) and updates the `top` style attribute of them.
Comment: I see your point. I cant give an authoritative answer for that but I think it makes sense if re-ordering algorithm works that way. "Move elements from bottom to top to accomplish the given ordering." But probably more complex than that.
Comment: Currently reordering doesn't always work, it's an implementation detail mostly.  There's an issue for it that I can't seem to find right now, but it explains the situation in more detail.  If you find it, comment on it so they know it's important to you.


Correctly expressing improvement in AUC?

Question: If a set of features, A, results in an AUC of 0.5, then an improved set of features, B, were used resulting in an AUC of 0.75, how do I express this improvement in words:

50% improvement (0.75-0.5)?
25% improvement (0.75-0.5)/0.5?

it seems like simple statistics, but I am mixed up on this issue.
Comment: The % numbers seem to be with the wrong expressions in the list.
Answer: You can equivalently use the following expressions:

50% relative improvement
25% absolute improvement, or a 25 percentage point improvement.

Speaking of correct terms, AUROC (if that is your metric) should be used instead of AUC, as the latter term is ambiguous.
Comment: Your second measure of improvement is called kappa.
Answer: I just posted a somewhat related question here: Compare and quantify relative improvement in ROC AUC scores? that has this as a component to it.
I'm not sure, though given the ROC AUC represents the probability that a classifier scores a positive case higher than a negative case, perhaps comparing the magnitude of the odds ratios would be an appropriate method for comparison.
So rather than 0.75 / 0.50, this becomes (3/1) / (1/1), so the improvement is 3x, i.e. the new model has 3x the odds of scoring a positive case more highly than a negative case compared to the old model.
(To the point above, 0.50 represents random chance, so perhaps these should be rescaled to between 0.5 and 1 which changes the interpretation though. Also in the case when you're comparing against a model with ROC AUC of 0.5, any improvement would represent infinite improvement. Comparing the odds ratios under this rescaled version also means that an improvement from 0.51 to 0.59 would represent the same amount of improvement as 0.91 to 0.99, however the former may feel more likely to arise randomly -- so would probably want to also consider some notion of confidence when comparing ROC AUCs.)
Answer: To calculate your relative improvement assumed to be $x$, you should use $x=(0.75-0.5)/0.5$.
I believe the relative improvement is more intuitive in the AUC case. Also, AUC is commomly used in literature, so both AUC and AUROC arr okay
Comment: I don't think we should encourage the use of ambiguous terms :-)