CSS for semi transparent content area over an image, similar to lightbox

Question: I need some css to display a semi transparent box over an image which I will display non transparent text or other content over, similar to how the lightbox style text descriptions work.
I ended up coming up with the following css:
.widgetTitle
{
    top: 0;
    left: 0;
    height:20px;
    width:100%;
    position: absolute;
}

.widgetContent
{
    background-color: transparent;
    bottom: 0;
    left: 0;
    position: absolute;
    width: 310px;
    padding: 5px 10px 5px 10px;
    background: url(../images/white.png) repeat;
}

.widgetContent h4
{
    margin: 0px;
    font-size: 20px;
    color: #ED1666;
}

.widgetContent p
{
    color: black;
    margin-bottom: 0px;
    padding: 0px;
    width: 315px;
}

And then the items are displayed as follows:

Some Heading

Some Text

Seems to work ok
Comment: What you tried so far? This is not a "gimme the codes" site. Show what you've attempted, and we'll try to help fix it. But at least show you've put **SOME** effort into this.
Comment: And if you throw it up on jsfiddle.net, that'll make it easier for everyone.
Answer: /* Fallback for web browsers that doesn't support RGBa */
background: rgb(0, 0, 0) transparent;
/* RGBa with 0.6 opacity */
background: rgba(0, 0, 0, 0.6);
/* For IE 5.5 - 7*/
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
/* For IE 8*/
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,endColorstr=#99000000)";

Comment: rgba support is still limited
Comment: That's true, I updated my answer for a wider range of compatibility.
Answer: The easier and most supported way of doing this would be to create a semi transparent gif tile, and put it as the background-image on the div.
background:url(/path/to/image.gif) repeat transparent top left
Comment: Wouldn't a png be better? The only real exception to using png's is IE6. In that case, he can use the DXImage filter and get the same result. The only reason I say png is because it gives you true alpha transparency, whereas the gif only gives you index transparency.
Comment: I would probably use a png for that reason but a gif works across the board because you will just need it to be a square
Comment: I never really tried it with a gif. I may have to try it out of curiosity.


Power Utility Function Inverse

Question: If power utility is $p = \frac{x^{1- \gamma} -1}{1 - \gamma}$ then is the inverse of the power utility function just  $\frac{1 - \gamma}{p^{1- \gamma} -1}$?
Comment: The inverse of the function $f(x)$ usually means the function $g$ such that $g(f(x))=f(g(x))=x$...
Comment: So what I have above is not the inverse for power utility?
Comment: I don't know what "power utility" is, and I don't know what it is you are trying to calculate, but you do not have the **inverse function** of the first function, no. But you might have what you need. I can't know that.
Comment: It would be better if you give the definition of `power utility`.
Comment: It's also called the isoelastic utility function does that help?
Comment: Do you want to compute the inverse function of $$p=\frac{x^{1-\gamma}-1}{1-\gamma}$$?
Comment: Yes, I would like to know what the inverse function is.
Answer: Given that $p= \frac{x^{1-\gamma}- 1}{1-\gamma}$ we find the inverse by solving for x-  Multiply both sides by $1-\gamma$:  $p(1-\gamma)= x^{1-\gamma}-1$.
Add 1 to both sides: $p(1-\gamma)+ 1= x^{1-\gamma}$.
Finally, take the $1-\gamma$ root of both sides:  $(p(1-\gamma)+ 1)^{\frac{1}{1-\gamma}}= x$.

Power Utility Function Inverse


Operator satisfying $\langle Pu, \phi\rangle=\langle u, ^tP\phi\rangle$?

Question: Let $\Omega\subseteq \mathbb R^n$ be an open subset and $P:\mathscr{D}^\prime(\Omega)\longrightarrow \mathscr{D}^\prime(\Omega)$ be a continuous linear operator. Is it true that there exists only one continuous linear operator $^tP:C^\infty_0(\Omega)\longrightarrow C^\infty_0(\Omega)$ such that $$\langle Pu, \phi\rangle=\langle u, ^tP\phi\rangle?$$
Thanks
Answer: The unicity is quite easy to prove: suppose that you have two endomorphisms of $D(\Omega)$ with the property
$$\forall u\in D'(\Omega),\,\forall \phi\in D(\Omega)\quad \langle  P[u],\phi\rangle=\langle  u,A[\phi]\rangle=\langle  u,B[\phi]\rangle.$$
This implies
 $$\forall u\in D'(\Omega),\,\forall \phi\in D(\Omega)\quad  \langle  u,(A-B)[\phi]\rangle=0.$$
The function $(A-B)[\phi]$ is a test function. If the operator $A-B\ne 0$, then for at least one test function $\phi$ the test function $(A-B)[\phi]$ has a non-empty Lebesgue set $E_0=\{x\in \Omega:\,(A-B)[\phi](x)>0\}$. Take now $u=\mathbf 1_{x\in E_0}(x)$. This ensures us that $\langle  u,(A-B)[\phi]\rangle>0$.
Therefore, $\forall \phi \quad (A-B)[\phi]=0$, or, in other words, $A=B$.
The existence of such an operator $A$ ($^tP$ in your notations), however, is a trickier thing to prove.
Comment: Thanks for your answer. Could you recommend me a reference for the proof of the existence?
Comment: @PtF you are welcome. Unfortunately, I don't know such a reference. If you find one - ping me here, I'm interested, too.


Is there a function to add WOE, calculated on Training data, to the whole data set? (python)

Question: I am working on some python code to predict Default rate of loans handed out by a bank.
I have calculated the WOE and information value (IV) on the training set
(using the following code: https://github.com/Sundar0989/WOE-and-IV/blob/master/WOE_IV.ipynb?fbclid=IwAR1MvEfyGsdyTre0uPJC5WRl91dfue_t0vH5qJezwm2mAg6sjHZJg9MyDYo).
We have also concluded 2 high cardinality variables. We don't know however how to add these WOE scores to the whole set. How do we tackle this problem? How can we go further to use WOE to predict the target variable?
code:
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy, pylab

Reading the data received from bank, feature selection part 1, splitting up whole set (Training) into training set: indices_traintrain, validation set: indices_val and test set: indices_test (70/30 split training and validation set - test set and 70/30 split training - validation)
Training =
pd.read_excel('/Users/enjo/Documents/Master/DM/Data_DSC2019_STUDENTS/DSC2019_Training.xlsx', na_values=np.nan)

Status = Training.iloc[:,-1]
Data = Training.iloc[:,0:45]

Data_missing = Data.isna()
Data_missing = Data_missing.sum()
print(Data_missing/len(Data))

"""
drop variables with more than 80% missing
"""

Drop = ['FREE_CASH_FLOW_AMT',
 'A2_MTHS_FIRST_PCX_COREPROF_CNT', 'A2_MONTHS_IN_BELGIUM_CNT', 'A2_MTHS_SNC_FIRST_COREPROF_CNT', 'MONTHS_SINCE_LAST_REFUSAL_CNT']

DroppedTraining = Training.copy()
for element in Drop:
    DroppedTraining.drop(element, axis=1,inplace=True)

import numpy as np
from sklearn import datasets
from sklearn import svm
from sklearn import preprocessing
Data_preprocessed=[] #contains preprocessed data
from Preprocessing_continuous import Preprocessing_continuous #import function for preprocessing
from Preprocessing_discrete import Preprocessing_discrete #import function for preprocessing
from sklearn.model_selection import train_test_split

indices=np.arange(26962)
indices_train, indices_test  = train_test_split(indices, test_size=0.3, random_state=0)
indices_traintrain, indices_val  = train_test_split(indices_train, test_size=0.3, random_state=0)

Training['target']= Training['Label_Default'].apply(lambda x:1 if x=='Y' else 0)

Highcardinalityset=[]
Highcardinalityset = Training[['Type',
                                       'INDUSTRY_CD_3',
                                       'INDUSTRY_CD_4',
                                       'Managing_Sales_Office_Nbr',
                                       'Postal_Code_L',
                                       'Product_Desc',
                                       'CREDIT_TYPE_CD',
                                       'ACCOUNT_PURPOSE_CD',
                                       'A2_MARITAL_STATUS_CD',
                                       'FINANCIAL_PRODUCT_TYPE_CD',
                                       'A2_EMPLOYMENT_STATUS_CD',
                                       'A2_RESIDENT_STATUS_CD',
                                       'target']]
Highcardinalityset = Highcardinalityset.iloc[indices_traintrain]

function found on github
import pandas as pd
import numpy as np
import pandas.core.algorithms as algos
from pandas import Series
import scipy.stats.stats as stats
import re
import traceback
import string

max_bin = 20
force_bin = 3

# define a binning function
def mono_bin(Y, X, n = max_bin):

    df1 = pd.DataFrame({"X": X, "Y": Y})
    justmiss = df1[['X','Y']][df1.X.isnull()]
    notmiss = df1[['X','Y']][df1.X.notnull()]
    r = 0
    while np.abs(r) < 1:
        try:
            d1 = pd.DataFrame({"X": notmiss.X, "Y": notmiss.Y, "Bucket": pd.qcut(notmiss.X, n)})
            d2 = d1.groupby('Bucket', as_index=True)
            r, p = stats.spearmanr(d2.mean().X, d2.mean().Y)
            n = n - 1
        except Exception as e:
            n = n - 1

    if len(d2) == 1:
        n = force_bin
        bins = algos.quantile(notmiss.X, np.linspace(0, 1, n))
        if len(np.unique(bins)) == 2:
            bins = np.insert(bins, 0, 1)
            bins[1] = bins[1]-(bins[1]/2)
        d1 = pd.DataFrame({"X": notmiss.X, "Y": notmiss.Y, "Bucket": pd.cut(notmiss.X, np.unique(bins),include_lowest=True)})
        d2 = d1.groupby('Bucket', as_index=True)

    d3 = pd.DataFrame({},index=[])
    d3["MIN_VALUE"] = d2.min().X
    d3["MAX_VALUE"] = d2.max().X
    d3["COUNT"] = d2.count().Y
    d3["EVENT"] = d2.sum().Y
    d3["NONEVENT"] = d2.count().Y - d2.sum().Y
    d3=d3.reset_index(drop=True)

    if len(justmiss.index) > 0:
        d4 = pd.DataFrame({'MIN_VALUE':np.nan},index=[0])
        d4["MAX_VALUE"] = np.nan
        d4["COUNT"] = justmiss.count().Y
        d4["EVENT"] = justmiss.sum().Y
        d4["NONEVENT"] = justmiss.count().Y - justmiss.sum().Y
        d3 = d3.append(d4,ignore_index=True)

    d3["EVENT_RATE"] = d3.EVENT/d3.COUNT
    d3["NON_EVENT_RATE"] = d3.NONEVENT/d3.COUNT
    d3["DIST_EVENT"] = d3.EVENT/d3.sum().EVENT
    d3["DIST_NON_EVENT"] = d3.NONEVENT/d3.sum().NONEVENT
    d3["WOE"] = np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)
    d3["IV"] = (d3.DIST_EVENT-d3.DIST_NON_EVENT)*np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)
    d3["VAR_NAME"] = "VAR"
    d3 = d3[['VAR_NAME','MIN_VALUE', 'MAX_VALUE', 'COUNT', 'EVENT', 'EVENT_RATE', 'NONEVENT', 'NON_EVENT_RATE', 'DIST_EVENT','DIST_NON_EVENT','WOE', 'IV']]
    d3 = d3.replace([np.inf, -np.inf], 0)
    d3.IV = d3.IV.sum()

    return(d3)

def char_bin(Y, X):

    df1 = pd.DataFrame({"X": X, "Y": Y})
    justmiss = df1[['X','Y']][df1.X.isnull()]
    notmiss = df1[['X','Y']][df1.X.notnull()]
    df2 = notmiss.groupby('X',as_index=True)

    d3 = pd.DataFrame({},index=[])
    d3["COUNT"] = df2.count().Y
    d3["MIN_VALUE"] = df2.sum().Y.index
    d3["MAX_VALUE"] = d3["MIN_VALUE"]
    d3["EVENT"] = df2.sum().Y
    d3["NONEVENT"] = df2.count().Y - df2.sum().Y

    if len(justmiss.index) > 0:
        d4 = pd.DataFrame({'MIN_VALUE':np.nan},index=[0])
        d4["MAX_VALUE"] = np.nan
        d4["COUNT"] = justmiss.count().Y
        d4["EVENT"] = justmiss.sum().Y
        d4["NONEVENT"] = justmiss.count().Y - justmiss.sum().Y
        d3 = d3.append(d4,ignore_index=True)

    d3["EVENT_RATE"] = d3.EVENT/d3.COUNT
    d3["NON_EVENT_RATE"] = d3.NONEVENT/d3.COUNT
    d3["DIST_EVENT"] = d3.EVENT/d3.sum().EVENT
    d3["DIST_NON_EVENT"] = d3.NONEVENT/d3.sum().NONEVENT
    d3["WOE"] = np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)
    d3["IV"] = (d3.DIST_EVENT-d3.DIST_NON_EVENT)*np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)
    d3["VAR_NAME"] = "VAR"
    d3 = d3[['VAR_NAME','MIN_VALUE', 'MAX_VALUE', 'COUNT', 'EVENT', 'EVENT_RATE', 'NONEVENT', 'NON_EVENT_RATE', 'DIST_EVENT','DIST_NON_EVENT','WOE', 'IV']]
    d3 = d3.replace([np.inf, -np.inf], 0)
    d3.IV = d3.IV.sum()
    d3 = d3.reset_index(drop=True)

    return(d3)

def data_vars(df1, target):

    stack = traceback.extract_stack()
    filename, lineno, function_name, code = stack[-2]
    vars_name = re.compile(r'\((.*?)\).*$').search(code).groups()[0]
    final = (re.findall(r"[\w']+", vars_name))[-1]

    x = df1.dtypes.index
    count = -1

    for i in x:
        if i.upper() not in (final.upper()):
            if np.issubdtype(df1[i], np.number) and len(Series.unique(df1[i])) > 2:
                conv = mono_bin(target, df1[i])
                conv["VAR_NAME"] = i
                count = count + 1
            else:
                conv = char_bin(target, df1[i])
                conv["VAR_NAME"] = i
                count = count + 1

            if count == 0:
                iv_df = conv
            else:
                iv_df = iv_df.append(conv,ignore_index=True)

    iv = pd.DataFrame({'IV':iv_df.groupby('VAR_NAME').IV.max()})
    iv = iv.reset_index()
    return(iv_df,iv)

final_iv, IV = data_vars(Highcardinalityset,Highcardinalityset.target)

final_iv
IV.sort_values('IV')

IV.to_csv('test.csv')

transform_vars_list = Highcardinalityset.columns.difference(['target'])
transform_prefix = 'new_' # leave this value blank if you need replace the original column values

transform_vars_list

for var in transform_vars_list:
    small_df = final_iv[final_iv['VAR_NAME'] == var]
    transform_dict = dict(zip(small_df.MAX_VALUE.astype(str),small_df.WOE.astype(str)))
    replace_cmd = ''
    replace_cmd1 = ''
    for i in sorted(transform_dict.items()):
        replace_cmd = replace_cmd + str(i[1]) + str(' if x <= ') + str(i[0]) + ' else '
        replace_cmd1 = replace_cmd1 + str(i[1]) + str(' if x == "') + str(i[0]) + '" else '
    replace_cmd = replace_cmd + '0'
    replace_cmd1 = replace_cmd1 + '0'
    if replace_cmd != '0':
        try:
            Highcardinalityset[transform_prefix + var] = Highcardinalityset[var].apply(lambda x: eval(replace_cmd))
        except:
            Highcardinalityset[transform_prefix + var] = Highcardinalityset[var].apply(lambda x: eval(replace_cmd1))

Highcardinalityset['Postal_Code_L'].value_counts()
Highcardinalityset['new_Postal_Code_L'].value_counts()

Highcardinalityset['Managing_Sales_Office_Nbr'].value_counts()
Highcardinalityset['new_Managing_Sales_Office_Nbr'].value_counts()

Nice to see when high WOE: interesting for that postal code: high risk for default!
Highcardinalityset.to_excel("Highcardinalitysettraintrain.xlsx")

TrainingWOE = DroppedTraining[['Managing_Sales_Office_Nbr', "Postal_Code_L"]]
TrainingWOE["Postal_Code_L_WOE"]=Highcardinalityset[["new_Postal_Code_L"]]
TrainingWOE["Managing_Sales_Office_Nbr_WOE"]=Highcardinalityset[["new_Managing_Sales_Office_Nbr"]]

drop variables that are not relevant because of low IV value
Drop = ["ACCOUNT_PURPOSE_CD", "A2_MARITAL_STATUS_CD", "A2_EMPLOYMENT_STATUS_CD", "A2_RESIDENT_STATUS_CD",
                  "INDUSTRY_CD_3", "INDUSTRY_CD_4","Type"]
DroppedTrainingAfterIVcalc = DroppedTraining.copy()
for element in Drop:
    DroppedTrainingAfterIVcalc.drop(element, axis=1,inplace=True)

preprocess remaining (44-5 (because of too many missing) - 7 (because of low iv) + 1 (target variable added))
Comment: Hi @EnjoFaes, welcome to Stackoverflow. We generally ask that you include the relevant code in your post, that way we get a nice archive of the full question even if the repo disappears later on. Could you take the concise relevant code from your github example and post it here?
Comment: You might be more interested in asking this question is stats exchange or maybe data science given the nature of the problem you are facing.
Comment: someone an idea?
Answer: Thanks for asking this question. Here is the code to do the required transformation which is shown in the notebook as well.
transform_vars_list = df.columns.difference(['target'])
transform_prefix = 'new_' # leave this value blank to replace the original column

#apply transformations
for var in transform_vars_list:
    small_df = final_iv[final_iv['VAR_NAME'] == var]
    transform_dict = dict(zip(small_df.MAX_VALUE,small_df.WOE))
    replace_cmd = ''
    replace_cmd1 = ''
    for i in sorted(transform_dict.items()):
        replace_cmd = replace_cmd + str(i[1]) + str(' if x <= ') + str(i[0]) + ' else '
        replace_cmd1 = replace_cmd1 + str(i[1]) + str(' if x == "') + str(i[0]) + '" else '
    replace_cmd = replace_cmd + '0'
    replace_cmd1 = replace_cmd1 + '0'
    if replace_cmd != '0':
        try:
            df[transform_prefix + var] = df[var].apply(lambda x: eval(replace_cmd))
        except:
            df[transform_prefix + var] = df[var].apply(lambda x: eval(replace_cmd1))

In addition, there is a package Xverse which does the same. Please refer to it here - https://github.com/Sundar0989/XuniVerse


How the best way to make a function await response ECMA6

Question: I have a small function to compare two arrays. After comparison, return an array with the values for me to use as a callback.
However, I need to wait while the system obtains the data and generates the array, in the example, it is necessary to wait for the response of the functions before making the comparison.
I am starting ECMA6, and I found some possibilities to do this, such as AWAIT, YIELD, PROMISES, so what would be the best way to make this function wait for processing before processing the callback?

  function check (callback) {

   const friends = all_friends_id(callback)
   const family = all_family_id(callback)

   let difference = new Set(
     [...friends].filter(x => !family.has(x)))

   callback(diference)

  }

Comment: Where are you even doing anything async here?  Certainly `.filter()` isn't async.  Not even sure why you're using a callback at all.
Answer: You asked for the "best" way.  This is the "best" because it is cleaner code.  And the [...friends].filter(x => !family.has(x)) thing seems invalid so I corrected that.
const check = async () => {
   const friends = await all_friends_id(id);
   const family = await all_family_id(id);

   return friends.filter(f=> !family.includes(f))
}

Comment: It would be nice to have some preamble commentary explaining why this is an answer
Comment: @Soren, I will add it, but "why this is an answer" is questioning whether it is an answer or not, which it obviously is, so thats just an insult, and given the question the preamble is just going to be hot-air.  Its self-explanatory.
Comment: your answer was flagged for deletion by the system "due to low quality" -- so im not insulting you but trying to advice you on how to make your answer better.
Comment: The OP asked about ES6, but async and await are ES7.
Answer: If you're new to EC6/JS, you might find it easier to start with using callbacks. So your example would look something like this:
function check (callback) {
    all_friends_id( (friends) => {
        all_family_id( (family) => {
            let difference = new Set(
                 [...friends].filter(x => !family.has(x)))
            callback(difference)
        })
    })
}

After you've seen how to solve this task with callbacks, you might want to read up on promises. Promises solve the problem of "callback hell", in which a program has either a large number of nested callbacks, or complex callback relationships, or both.  But for this particular task you may find that a simple callback paradigm will work just fine.
Comment: this works fine =) I'm watching a YouTube video of how to use promise, to my understanding, it seems like it's doing the same thing ... only in a different way. I learned both ways \ o /

In your opinion, using promises is better in what situation?


Is nullifying with memory leaks and performance enough?

Question: Is setting a null value to an object good enough for garbage collection to collect and clear up memory in Java (Android)?
Comment: make sure there are no reference to the original object, then garbage collection can be made, but I don't think its guaranteed.
Comment: Setting a `null` value to an object is completely unrelated to its GC.  It's much more common for objects to get GC'd just because nobody references them anymore.
Answer: If all references to an object are set to null then it will eventually be cleaned up by the garbage collector.  Whether that's enough depends on what other resources the object holds (like file handles or db handles that can be leaked) and whether the garbage collector runs frequently enough to free it before you need the memory.  Large objects like bitmaps frequently have a function that can be called to free the memory early to avoid that problem (bitmap's recycle() is an example).
Answer: Yes. That's how objects are freed in Java.  Once they are freed, they become eligible for garbage collection, and are collected at some later time.
Answer: The garbage colletor does mark and sweep.
When using mark-and-sweep, unreferenced objects are not reclaimed immediately. Instead, garbage is allowed to accumulate until all available memory has been exhausted. When that happens, the execution of the program is suspended temporarily while the mark-and-sweep algorithm collects all the garbage. Once all unreferenced objects have been reclaimed, the normal execution of the program can resume.
http://www.brpreiss.com/books/opus5/html/page424.html. The link has details regarding how mark and sweep works.
http://chaoticjava.com/posts/how-does-garbage-collection-work/
Technical details of Android Garbage Collector
If you set your variable to null. the variable will be available for garbage collection. However it depends on the virtual machine (dalvik) to decide when to garbage  collect. It also depends on the heap size. More the heap size more frequent garbage collection. http://www.youtube.com/watch?v=_CruQY55HOk. The guy in the video gives a big warning about using large heap since it leads to more frequent garbage collection there causing your application to pause.
If the current running operation requires more memory and the memory is not available, the garbage collector kicks in to free memory.( even after which required memory space is not avaialable you will get memory leaks).
http://developer.android.com/training/articles/perf-tips.html.
A quote from the above link. As you allocate more objects in your app, you will force a periodic garbage collection, creating little "hiccups" in the user experience.


Can the sample space contain outcomes that do not occur?

Question: The definition of the probability density function of the continuous uniform distribution on Wikipedia is:
$$
   f(x) = \left\{
     \begin{array}{}
       \frac{1}{b-a} & \text{for } a \leq x \leq b \\
       0 & \text{for } x < a \text{ or } x > b
     \end{array}
   \right.
$$
As usual, $\mathbb{P}(X=x) = 0$ for any $x \in [a,b]$. But this just mean that the it "almost surely" does not occurs, rather than it is "impossible".
However, when $x < a$ or $x > b$, $\mathbb{P}(X= x) = 0$ and in this case, it actually is impossible. Values in intervals such as $[c,d]$ where $c > b$ never ever occurs either, rather than just "almost surely" not occur.
I thought a nice way to resolve this would be to simply restrict the sample space to exclude outcomes that is impossible or never occurs. So the pdf would instead be:
$$f(x) = \frac{1}{b-a} \text{ for } a \leq x \leq b.$$
This way, a probability of $0$ always mean that the event almost surely does not occur, which (to me) feels intuitively satisfying.
I was wondering if this is already being done in measure theoretical probability theory? The definition of the sample space $\Omega$ that I have read always define it as the set of all possible outcomes.
Comment: There currently is a discussion of sample spaces going on at http://stats.stackexchange.com/questions/64167; much of it is relevant here. Note that you indeed *can* restrict the sample space to exclude outcomes that cannot occur. But that is separate from the definition of a probability distribution (or density) function of a random variable, which *always* is defined on the entire Real line. Sample spaces are almost never sets of Real numbers (except perhaps in math texts): they are sets of *things in which we are interested,* like people, experimental outcomes, or processes.
Comment: @whuber - thanks for the explanation and +1.
Answer: I think you are confusing the whole concept of probability density distribution. Any smooth continuous distribution has $P(X=x)=0$ for any $x$. This is the whole concept of probability distribution - its values don't have direct connection to probability, it represents a concentration of probability (hence the name of it). This is logically easy to get as there is infinite count of numbers between $a$ and $b$, so the probability of each of them is $1/\infty$ which is $0$. Makes perfect sense to me.
I don't see a problem with possible and impossible events as well. If $f(x)=0$ the event is impossible, otherwise possible. Your redefinition makes it only worse because now your distribution is undefined on some pieces - so, that makes the probability undefined as well...
Comment: f(x) is not P(X=x). There is no contradiction.
Comment: Thanks! $f(x)=0$ for impossible events basically resolved my issue! +1 and accepted.


React basics - updating var and using it in render doesn't work

Question: New to ReactJS with experience in android
I simply set a value to variable on input change and use it in display
But the input freezes and new value is also not applied
I also declared the var msg outside render function so it isn't initialized every time
This is certainly not how react works and i'm doing trivial mistake but it may be so trivial that there are no answers out there
What am i doing wrong
import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";
var msg = "initial value";

const Message = () => {
  return (

      <input
        type="text"
        value={msg}
        placeholder="Enter a message"
        onChange={(e) => {
          alert(e.target.value);
          msg = e.target.value;
        }}
      />

        {msg}

  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<Message />, rootElement);

Here's a link to live demo
https://codesandbox.io/s/usestate-01-forked-f2vdq?file=/src/index.js:0-543
Comment: Because if you don't use state the input freezes and new value is also not applied.
Comment: Why don't you use `useState`? You even import it.
Comment: Because i want to know why is that necessary.In java the above code would work as expected.Why do i really need useState ?
Comment: And this is exactly my question.Why it freezes.If i do this in a normal html js page it'll work too but why not here?
Comment: If you just change the value of a variable in a "normal" HTML page it will certainly not re-render the value.
Comment: Sorry i meant using jquery $("#myId").val(msg)
Answer: Nothing has told React that it needs to re-render the component.
The variable has changed, but it isn't watching the variable.
This is what state is for. Changing the state explicitly informs React that it needs to check to see if anything should be updated in the DOM. Define the variable with a state hook.
const Message = () => {
  let [msg, setMsg] = useState("initial value");

  return (

      <input
        type="text"
        value={msg}
        placeholder="Enter a message"
        onChange={(e) => {
          alert(e.target.value);
          setMsg(e.target.value);
        }}
      />

        {msg}

  );
};

Comment: My understanding was that react periodically refreshes the ui even if no component asks it to. Similar to onDraw in android where the view draws itself on canvas. But it seems like if nobody asks react to update,it'll never update itself.But if i dont provide value param to input the screen doesnt freeze.Now how does  react know to render ui
Answer: React is not tracking your variable, so you will never see an update. You need to use component state, which will cause React to be aware of it and trigger a render when it changes. I've updated your code to use the useState hook:
import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";
var msg = "initial value";

const Message = () => {
  const [message, setMessage] = useState(msg);
  return (

      <input
        type="text"
        value={message}
        placeholder="Enter a message"
        onChange={(e) => {
          alert(e.target.value);
          setMesage(e.target.value);
        }}
      />

        {msg}

  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<Message />, rootElement);

We set the initial internal state based on the msg value, and then update that internal state on change, which will cause a re-render
Answer: Typically, variables are contained only within the classes that use them, instead of making a global variable.  Full Working Demo of what I am proposing.
The state is a data object that holds whatever an instance of that class needs to work with.  You have a message component, so, if we make it store a message in its state, that message value will be used anywhere in the render() function whenever used.  So, we can do value={this.state.msg}, as well as
{this.state.msg}
.  The state and component lifecycle are key to good ReactJS development.
React is stateful, as we say. If you put value="hello!", it's the same to ReactJS as putting
hello!
. Unless the state says that has changed because of a setState() call, ReactJS sees no reason for anything in the display to change. Only by value={this.state...} and
{this.state....}
 are we able to make anything dynamic.
If you make a state like this, notice here we are setting a default...
constructor(props) {
  super(props);
  this.state = {'msg':'Initial Message'};
}

Then in your input, you can display it, like...
<input
    type="text"
    value={this.state.msg}

And also in the display of the value, like...
{this.state.msg}

Comment: The demo doesnt seem to display the updated value.The freezing is solved though
Comment: Oh, woops!  Sorry, forgot to click save on that demo, I just updated it now: just needed this `{this.state.msg}`.  I also re-added in the 'initial message' you had into the state.  Let me know if this helps!
Comment: It works now.Can you please explain why the ui doesnt freeze if i dont use state and dont provide default value to input.There i dont even call any setstate so how does react rerenders properly
Comment: Heres an example - https://codesandbox.io/s/usestate-01-forked-f2vdq?file=/src/index.js
Comment: Hey, Omkar: Sure thing!  I updated the demo *and* my answer with a default value *and* a better explanation.
Comment: Thanks i've understood using state already.I was just curious about its internal working.Especially the case when if i provide a value to an input and doesn't trigger state update by any means, the ui freezes but when i don't provide the value attribute and its just a simple input,then i don't need to call setstate or anything just to let user see what he's typing.How does this really work?Why dont i need to use setstate for default input value update.This is maybe a deeper question when i have a very beginner understanding of react.Maybe i'll have to just say "that's how it works & u use it"
Comment: *"Why dont i need to use setstate for default input value update."*  Well, React is *stateful*, as we say.  If you put `value="hello!"`, it's the same to ReactJS as putting `hello!`.  Unless the state says that has changed because of a `setState()` call, ReactJS sees no reason for anything in the display to change.  Only by `value={this.state...}` and `{this.state....}` are we able to make anything dynamic.  I'll update my answer with some of this info!
Comment: I'll take it as "React Basics"! Thanks for the guidance.Appreciate it


NSUserDefaults doesn't save after app quits

Question: When I quit the app, the values stored in NSUserDefaults isn't saved. I saved the values like this:
[[NSUserDefaults standardUserDefaults] setValue:@"enabled" forKey:@"notifications"];
[[NSUserDefaults standardUserDefaults] synchronize];

How do I store something with NSUserDefaults even after the app quits?
This is how I retrieve the values:
[[NSUserDefaults standardUserDefaults] valueForKey:@"notifications"]

The first two lines do run, because I put them to run when a button is pressed. When I go to a different view and back to the one with the button, it returns the value that I set.
I see what was wrong. I has this code in the first view and assumed it was working properly:
NSUserDefaults *Def = [NSUserDefaults standardUserDefaults];
NSString *Ver = [Def stringForKey:@"Version"];
NSString *CurVer = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
if(Ver == nil || [Ver compare:CurVer] != 0)
{
    if(Ver == nil)
    {
        [[NSUserDefaults standardUserDefaults] setValue:@"disabled" forKey:@"notifications"];
    }
}

Comment: where is your value which you put in setValue? Right now, you set it as a string "enabled". And how do you refer to it?
Comment: When does the app quit, and how, in relation to that code running (are you sure it did run)?
Comment: What is the printout of the "notifications" key of nsuserdefaults?
Comment: use this:NSLog("%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"notifications"]);
can you tell me what it print?
Comment: That log prints "enabled"
Comment: I believe I can just use `setValue` to save a string?
Comment: Saying "because I put them to run when a button is pressed" is not proof they ran, there may be a problem with the button. You have to prove the save code ran, one way is to put a `NSLog()` right after them. Then immediately retrieve and `NSLog()` the result. How do you quite the app?
Comment: There is a log after it, and it says the value i set it to. I quit the app by either double pressing home button and sliding it up or pressing stop button in xcode.
Comment: Add a retrieval and NSLog in `didFinishLaunchingWithOptions` `applicationWillTerminate` and `applicationDidEnterBackground` and `applicationWillEnterForeground` to determine if is valid prior to termination and immediately after becoming active.
Answer: You're using the wrong methods.
valueForKey and setValue:forKey: are methods pertaining to Key Value Coding, and aren't related to setting your persistent data in NSUserDefaults.
You need to be reading your values with
[[NSUserDefaults standardUserDefaults] objectForKey:@"notifications"];

and writing with
[[NSUserDefaults standardUserDefaults] setObject:@"enabled" forKey:@"notifications"];

Comment: If you're testing this in the simulator, I've noticed that NSUserDefaults doesn't save if you stop debugging or quit the simulator. Instead, you have to return the simulator springboard with Cmd+Shift+H first, and then stop debugging.
Comment: That doesn't save it after the app quits still.
Comment: @somepers I didn't include this in my answer, but you'll still need the synchronize call if you kill the app immediately after setting the new object (i.e. before the defaults have the opportunity to be automatically synchronized).


Cadastrar uma lista de registros

Question: Bom dia,
Tenho um programa que realiza cadastro de demandas para a produção de uma fabrica, estou com dificuldades em criar uma maneira de fazer com que ele cadastre vários registros vindo de uma lista em uma tabela só.
Exemplo: codigo1 produto1 , codigo2 produto2, codigo3 produto3 ...
Eu queria saber como faço para cadastrar todos de uma vez, gerando um id diferente para cada um.
meu controller:
<code> [HttpPost]
    public async Task<IActionResult> Index(OP model, List<string> observadores, List<string> demanda)
    {
        if (!ModelState.IsValid)
            return View(model);

        string EmailObservacao = null;

        if (observadores != null)
        {
            List<string> listaEmailCadastrar = new List<string>();

            foreach (var i in observadores)
            {
                var ObterEmail = AcessoServico.EmailUsuario(i.ToString());

                if (ObterEmail[0].ToString() != "")
                {
                    listaEmailCadastrar.Add(ObterEmail[0].ToString());
                }

            }

            foreach (var item in listaEmailCadastrar)
            {
                EmailObservacao += item.ToString() + ";";

            }

        }

        var resultado = _contexto.OPs.ToList();
        List<OP> listaResultados = new List<OP>();
        foreach (var item in resultado)
        {
            if (model.CodigoProduto == item.CodigoProduto && model.CpfCnpjCliente == item.CpfCnpjCliente)
            {
                listaResultados.Add(item);
            }
        }

        if (listaResultados.Count() > 0)
        {
            ViewBag.ListaResultado = listaResultados.OrderByDescending(p => p.DataCadastro);

            TempData["CadastroExistente"] = $"Já temos solicitações para o cliente {model.CpfCnpjCliente} com o produto {model.CodigoProduto}.";
            TempData["CnpjCpf"] = model.CpfCnpjCliente;
            TempData["CodigoProduto"] = model.CodigoProduto;

            #region Preencher formulario
            if (EmailObservacao != null)
            {
                TempData["Observadores"] = EmailObservacao.Remove(EmailObservacao.Length - 1);
            }
            else
            {
                TempData["Observadores"] = null;
            }

            var obterUsuario = AcessoServico.OrigemUsuario(User.Identity.Name);
            TempData["Departamento"] = obterUsuario[0];
            TempData["Solicitante"] = User.Identity.Name;
            ViewBag.Observadores = new SelectList(AcessoServico.ObterUsuario(_contexto.Permissoes.ToList())).OrderBy(item => item.Text);
            ViewBag.TipoRequisicao = new SelectList(AcessoServico.ListaTipoRequisicao(), "Id");
            ViewBag.ListaDestino = new SelectList(_repositorio.ObterTipoCadastro(User.Identity.Name), "Id", "Tipo");
            ViewBag.ListaPrioridade = new SelectList(AcessoServico.ListaPrioridade());
            #endregion

            return View(model);

        }

        if (EmailObservacao != null)
        {
            model.ObservadoresSolicitante = EmailObservacao.Remove(EmailObservacao.Length - 1);
        }

        model.DataCadastro = DateTime.Now;
        model.Status = "Aberto";
        _contexto.OPs.Add(model);

        await _contexto.SaveChangesAsync();

        return RedirectToAction(nameof(Sucesso));
    }
</code>
MEU MODELO:
<code>[Table("OP")]
public class OP
{
    [Key]
    [Column("IdOP")]
    public int Id { get; set; }

    public string Origem { get; set; }

    public DateTime DataCadastro { get; set; }

    public string Solicitante { get; set; }

    [Column("CodProduto")]
    public int CodigoProduto { get; set; }

    [Column("DescProduto")]
    public string DescricaoProduto { get; set; }

    [Column("Qtde")]
    public int Quantidade { get; set; }

    public string CpfCnpjCliente { get; set; }

    [Column("DescCliente")]
    public string DescricaoCliente { get; set; }

    [Column("Obs")]
    public string Observacoes { get; set; }

    public string Prioridade { get; set; }

    public string Status { get; set; }

    public string Responsavel { get; set; }

    public DateTime? Prazo { get; set; }

    public string TipoRequisicao { get; set; }

    public int IdTipoCadastro { get; set; }

    public string ObservadoresSolicitante { get; set; }

    [ForeignKey("IdTipoCadastro")]
    public TipoCadastro TiposCadastro { get; set; }
</code>
Answer: os produtos estão na listaResultados, se sim, tente fazer dessa forma:
ela vai pecorrer a listaderesultado e vai adicionar produtos por produto.
<code>var resultado = _contexto.OPs.ToList();
    List<OP> listaResultados = new List<OP>();
    foreach (var item in resultado)
    {
        if (model.CodigoProduto == item.CodigoProduto && model.CpfCnpjCliente == item.CpfCnpjCliente)
        {
            listaResultados.Add(item);

 if (listaResultados.Count() > 0)
    {
        ViewBag.ListaResultado = listaResultados.OrderByDescending(p => p.DataCadastro);

        TempData["CadastroExistente"] = $"Já temos solicitações para o cliente {model.CpfCnpjCliente} com o produto {model.CodigoProduto}.";
        TempData["CnpjCpf"] = model.CpfCnpjCliente;
        TempData["CodigoProduto"] = model.CodigoProduto;

        #region Preencher formulario
        if (EmailObservacao != null)
        {
            TempData["Observadores"] = EmailObservacao.Remove(EmailObservacao.Length - 1);
        }
        else
        {
            TempData["Observadores"] = null;
        }

        var obterUsuario = AcessoServico.OrigemUsuario(User.Identity.Name);
        TempData["Departamento"] = obterUsuario[0];
        TempData["Solicitante"] = User.Identity.Name;
        ViewBag.Observadores = new SelectList(AcessoServico.ObterUsuario(_contexto.Permissoes.ToList())).OrderBy(item => item.Text);
        ViewBag.TipoRequisicao = new SelectList(AcessoServico.ListaTipoRequisicao(), "Id");
        ViewBag.ListaDestino = new SelectList(_repositorio.ObterTipoCadastro(User.Identity.Name), "Id", "Tipo");
        ViewBag.ListaPrioridade = new SelectList(AcessoServico.ListaPrioridade());
        #endregion

        return View(model);

    }

    if (EmailObservacao != null)
    {
        model.ObservadoresSolicitante = EmailObservacao.Remove(EmailObservacao.Length - 1);
    }

    model.DataCadastro = DateTime.Now;
    model.Status = "Aberto";
    _contexto.OPs.Add(model);

    await _contexto.SaveChangesAsync();
        }
    }
</code>
Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).

Cadastrar uma lista de registros


É possível fazer um tooltip com CSS puro?

Question: É possível fazer um tooltip com CSS puro? Ou seja, sem jQuery, sem Javascript, mas somente com CSS.
Por exemplo, quero fazer isso baseando no seguinte elemento:
LINK

Comment: Quem sabe em um futuro não distante, não possamos [usar a tag `dialog`](https://pt.stackoverflow.com/questions/263286/como-funciona-a-tag-dialog-ela-tem-futuro) para fazer isso.
Answer: Vou contribuir...

body {
  margin: 30px;
}
a.tooltip {
  text-decoration: none;
  color: #000;
  font-family: "Roboto";
  font-size: 16px;
  transition: all 0.4s ease;
  -webkit-transition: all .4s ease;
  padding: 5px;
  background-color: #FFF;
  position: relative;
}
a.tooltip:before {
  content: attr(data-title);
  background-color: #000;
  color: #FFF;
  font-size: 13px;
  padding: 10px;
  box-sizing: border-box;
  font-family: "Roboto";
  position: absolute;
  left: 0;
  bottom: -50px;
  width: 250px;
  opacity: 0;
  transition: all .4s ease;
}
a.tooltip:after {
  content: "";
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  left: 5px;
  bottom: -16px;
  border-style: solid;
  border-width: 0 10px 10px 10px;
  border-color: transparent transparent #000 transparent;
  transition: all .4s ease;
}
a.tooltip:hover:after,
a.tooltip:hover:before {
  opacity: 1;
}
 Teste de Link
Answer: Existe outra forma de fazer isso, onde você possua o elemento que irá receber o tooltip e um outro para "ser" o tooltip. Seria basicamente assim:

/*Desconsidere essa parte, é apenas para o snippets do SOpt*/
div {
  position: absolute;
    left: 20%;
  }
/*Desconsidere essa parte, é apenas para o snippets do SOpt*/

div.tooltips {
  position: relative;
  display: inline;
  cursor: pointer;
}
div.tooltips span {
  position: absolute;
  width:140px;
  color: #FFFFFF;
  background: #178BFF;
  height: 49px;
  line-height: 49px;
  text-align: center;
  visibility: hidden;
  border-radius: 17px;
}
div.tooltips span:after {
  content: '';
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;
  width: 0; height: 0;
  border-bottom: 8px solid #178BFF;
  border-right: 8px solid transparent;
  border-left: 8px solid transparent;
}
div:hover.tooltips span {
  visibility: visible;
  opacity: 0.9;
  top: 30px;
  left: 50%;
  margin-left: -76px;
  z-index: 999;
}

Estou aqui!
  Alá, sou diferente...

Apenas para fins de conhecimento, existe geradores de tooltip online.
Comment: Você é um gênio!  +1. A única coisa que eu faria era usar uma classe ao invés do `span`. Seu tooltip ganhou do meu ;)
Comment: Legal, e assim da suporte a navegadores mais antigos (se bem que é dificil alguém usa-los) +1
Comment: @WallaceMaxters Colocando como `span` eu limito ao elemento, mas colocando como classe a pessoa pode querer colocar em "qualquer coisa", e poderia ter problemas. Mas não creio que o meu está melhor que o seu. Essa é uma abordagem muito "simplista".
Comment: @GuilhermeNascimento Sim, concordo com você, e realmente acho difícil alguém usar com as ferramentes que temos hoje (eu mesmo não uso).
Comment: Temos que concordar que é mais rápido do que usar javascript. E ultimamente estou preferindo fazer com CSS puro do que com jQuery (animates por exemplo)
Comment: @WallaceMaxters Nisso tenho que concordar com você. Mas tudo depende "do que" é sua necessidade. As vezes essa diferença "não importa", outras é de grande importância.
Comment: Na verdade eu me referia a navegadores antigos rs, se bem que em questão de alguns mobiles o seu código é o mais indicado :)
Comment: Só um comentário @WallaceMaxters o exemplo com `span` é apenas um exemplo de entendimento, você pode trocar por classse, id, atributos, etc. Mas em pratica podemos dizer que é apenas realmente para o entendimento do efeito ;)
Comment: @GuilhermeNascimento Sim, havia esquecido de mobile. Alterei a resposta para não utilizar um link e sim qualquer texto.
Comment: Muito bom estes efeitos com `border` transparente pra fazer os balões ou setas !
Comment: Detalhe mais importante e por isso disse que a resposta é melhor: Vocês dois colocaram a setinha no tooltip
Answer: Sim, é possível!
Podemos utilizar o atributo :after para isso, veja:

.wm-tooltip{
    position:relative;
}

.wm-tooltip:after
{
    background-color:rgba(0, 0, 0, .6);
    color: white;
    box-sizing:border-box;
    content: attr(data-tooltip);
    display:none;
    padding:5px;
    position:absolute;
    right: -105px;
    bottom: -55px;
    z-index:3;
    box-shadow: 0 0 3px #000;
    border-radius: 0px 10px 10px 10px;
}

.wm-tooltip:hover:after {
    display:block;
}

Testando

Os pontos mais importantes aqui é:

:after com display:none
:hover:after com display:block. O :after estando após o :hover indica que após o hover do elemento o display:block do after ficará ativo.
content: attr(data-tooltip) aqui é onde ocorre a mágica que transforma o texto do data no texto do tooltip.
Comment: Lembrando que seria legal usar o fallback no `content`, acaso esqueça de colocar o atributo, algo assim: `content: attr(data-tooltip) "Não foi definido o atributo"`
Comment: @GuilhermeNascimento, Edição autorizada com successo ;)
Comment: Acho que você mesmo poderia editar, mas obrigado por me permitir colaborar :)
Comment: Mas porque o padrão aparece junto ?
Comment: @TaoPaiPai Sério, não notei, eu segui a documentação Mozilla,  vou fazer o rollback e depois edito novamente.
Comment: Perdoe-me removi o fallback porque ele é experimental, parece que a maior parte das features que acompanham o `content:` são experimentais. O uso correto é (deveria ser) `attr( attribute-name ? [,  ]? )`, mas parece que ainda não é suportado. @TaoPaiPai
Answer: Minha ideia é usar o próprio atributo data-tooltip como seletor, e permitir usar em qualquer tipo de tag:

[data-tooltip] {
  position: relative;
  cursor: pointer;
}
[data-tooltip]:before {
  content: attr(data-tooltip);
  display: none;
  position: absolute;
  top: 20px;
  width: 250px;
  z-index: 100;
  padding: 10px;
  text-decoration: none;
  font: 14px normal;
  border: 1px solid red;
  background: white;
  color: black;
  border-radius: 5px;
  box-shadow: 3px 3px 10px grey;
}
[data-tooltip]:hover:before {
  display: inline-block;
}
Lorem ipsum.

Lorem ipsum.

Lorem ipsum.


AWS CLI for S3 - Errno 2 No such file or directory: u'

Question: I'm trying to extract a few hundred GB of data from an S3 bucket to a Windows 10 computer's external hard drive and the command I'm using (in a .bat file with AWSCLI-64bit installed) is:
aws s3 sync s3://aws-extraction/ F:\Bowral-PE\ --delete --region ap-southeast-2 > "%SyncLogFile%"

For the most part it seems to be working because files seem to be downloading and directories seem to be getting created.  So far there's 82GB of data downloaded, but every single line says in the cmd verbose output shows:

download failed: s3://aws-extraction/Archive/somedirectory/ to
  F:\Bowral-PE\Archive\somedirectory Errno 2 No such file or directory:
  u'F:\Bowral-PE\Archive\somedirectory '

Any ideas why I'm getting this and how to stop it?
Please note that the AWSCLI seems to be using Python or something similar (a guess based on results when searching for the error), and I am a complete newbie when it comes to Python.
Comment: Could you please add what version of the AWS CLI you are using? (`aws --version`)
Comment: The `u` in `u'F:..` is fine, it is the way Python sometime prints unicode strings. What bothers me is the `Archive*somedirctory*` string with stars.  Are there on S3 some files on paths with stars in path names?
Comment: no Jan, there's not.  That was just me failing to italicise the quote properly.
`aws-cli/1.10.14 Python/2.7.9 Windows/8 botocore/1.4.5`
Comment: Did you ever get this resolved @ReeceDodds? I'm having the same issue on Windows 10 `aws-cli/1.10.67 Python/2.7.9 Windows/8 botocore/1.4.57`
Comment: No I didn't mate.  The downloads all completed though.  A strange one.  Best of luck with your issue
Answer: I hit the similar problem when using the msi version of latest awscli on my windows 10 today:
C:>aws s3 sync s3://<my_bucket_name>/ <local_dir>
   fatal error [Errno 2] No such file or directory

The above sync command works if I backup the bucket content to a unix machine.
After some googling, seems the "fatal error [Errno 2] No such file or directory" is coming out from python.
I realised the awscli's windows msi version uses the self contained python (v2.7), which may be too old for windows 10.
I then installed the latest python (v3.6), used pip way to install awscli following the instructions here.
After this the direct backup / restoration works as expected.
Comment: yup s4 sync should work for directory syncs and resolve the above error you getting
Answer: For me, I was getting this notification at the same time:

By clicking on it and allowing Python to access the folder I was trying to save it in, I was able to rerun the command and it succeeded.
Answer: I had the same issue, and after a lot of debugging, I enabled Windows long paths, and the issue went away.
Here's how I did it:

The registry key Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled (Type: REG_DWORD) must exist and be set to 1. The key's value will be cached by the system (per process) after the first call to an affected Win32 file or directory function (see below for the list of functions). The registry key will not be reloaded during the lifetime of the process. In order for all apps on the system to recognize the value of the key, a reboot might be required because some processes may have started before the key was set.


Erro ao utilizar outputPanel com autoupdate=true no primefaces

Question: Tenho um componente customizado onde utilizo o carousel. Quando clico em um item do carousel atualizo o meu managebean e preciso que a tela seja atualizada. Para isso utilizo o outputpanel com autoupdate=true, mas sem sucesso, ele até atualiza a página, mas o carousel deixa de rolar para os lados. O erro impresso no console do browser é o seguinte:
Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
Componente:

<composite:interface>
    <composite:attribute name="productList" />
    <composite:attribute name="carouselWidgetVar" />
    <composite:attribute name="selectedProduct" />
</composite:interface>

<composite:implementation>

                        <f:setPropertyActionListener value="#{product}"
                            target="#{cc.attrs.selectedProduct}" />

                                    <h:outputText value="#{product.name}"  />

</composite:implementation>

Chamada:

TELEFONIA

        <components:carouselSimulador
            productList="#{selectComboController.productList}"
            carouselWidgetVar="carouselTelefonia"
            selectedProduct="#{selectComboController.selectedProduct}"  />

Comment: Poderia dar mais detalhes sobre o que você quer fazer? A sua pergunta está vaga demais.
Comment: Atualizei a pergunta.
Answer: Resolvi o problema utilizando dois pontos antes do id dentro do atributo update no composite que queria atualizar, ficou assim:
Chamada do composition:

TELEFONIA

        <components:carouselSimulador
            productList="#{selectComboController.productList}"
            carouselWidgetVar="carouselTelefonia"
            selectedProduct="#{selectComboController.selectedProduct}"
            elementUpdate=":fProdcutsBan, :fProdcutsTel" />

composition:

    <f:setPropertyActionListener value="#{product}"
        target="#{cc.attrs.selectedProduct}"  />

                <h:outputText value="#{product.name}"  />

            <h:outputText value="#{product.value}" style="margin-top: 10px;display:block;color: red;" >
                <f:convertNumber maxFractionDigits="2" integerOnly="false" locale="pt-br" currencySymbol="R$"  />
            </h:outputText>

fonte:https://stackoverflow.com/questions/18489057/jsf2-updating-composite-elements-using-ajax


Extracting n rows of text from a large csv file

Question: I have a CSV file (foo.csv) with 200,000 rows.  I need to break it into four files (foo1.csv, foo2.csv... etc.) with 50,000 rows each.
I already tried simple ctrl-v/-c using gui text editors, but the my computer slows to a halt.
What unix command(s) could I use to accomplish this task?
Answer: you should use head and tail.
head -n 50000 myfile > part1.csv
head -n 100000 myfile | tail -n 50000 > part2.csv
head -n 150000 myfile | tail -n 50000 > part3.csv

etc ...
Else, but with no control on file names, you can use unix command split.
Answer: I don't have a terminal handy to try it out, but it should be just split -d -l 50000 foo.csv.
Hopefully the naming isn't terribly important because with the -d option, the output files will be named foo.csv00 .. foo.csv03.  You can add the -a 1 option so that the suffixes are 0-3, but there's no simple way to get the suffix to be injected into the middle of the filename.
Answer: sed -n 2000,4000p somefile.txt

will print from lines 2000 to 4000 to stdout.
Answer: split -l50000 foo.csv
Answer: You can use sed
Answer: I wrote this little shell script for this topic very similar at yours.
This shell script + awk works fine for me:
#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
    if (NR >= initial_line && NR <= end_line)
    print $0
}' $3

Used with this sample file (file.txt):
one
two
three
four
five
six

The command (it will extract from second to fourth line in the file):
edu@debian5:~$./script.sh 2 4 file.txt

Output of this command:
two
three
four

Of course, you can improve it, for example by testing that all argument values are the expected :-)


Google Maps MarkerClusterer in React with @react-google-maps/api

Question: I am getting an error while I want to implement MarkerClusterer from @react-google-maps/api.
The error that I am getting is
No overload matches this call.
Overload 1 of 2, '(props: MarkerClustererProps | Readonly): ClustererComponent', gave the following error.
Type 'Element[]' is missing the following properties from type 'ReactElement<any, any>': type, props, key
Overload 2 of 2, '(props: MarkerClustererProps, context: any): ClustererComponent', gave the following error.
Type 'Element[]' is not assignable to type 'Element'.ts(2769)
index.d.ts(378, 15): The expected type comes from the return type of this signature.
index.d.ts(378, 15): The expected type comes from the return type of this signature.
You can check the codes below. (I am sharing only the relevant parts of my whole code)

import React, { useEffect, useState, useRef } from 'react';
import { GoogleMap, useJsApiLoader, Marker, MARKER_LAYER, OverlayView, MarkerClusterer } from '@react-google-maps/api';
import './app.css';
import PlacesAutocomplete from './components/PlacesAutocomplete';

const containerStyle = {
    width: '100vw',
    height: '100vh',
};

const App = () => {
    const [center, setCenter] = useState({ lat: 39.015137, lng: 34.97953 });
    const [zoom, setZoom] = useState(6);
    const [markers, setMarkers] = useState<{ id: string; name: string; avatar: string; latLng?: google.maps.LatLng }[]>([]);
    const [users, setUsers] = useState<{ id: string; name: string; role: string; avatar: string; color: string; latLng: boolean }[]>([]);

    const mounted = useRef(false);
    const markerRef = useRef<Marker>(null);

    const { isLoaded } = useJsApiLoader({
        id: 'google-map-script',
        googleMapsApiKey: 'API_KEY',
        libraries: ['places'],
    });

    let markersInfo: { id: string; name: string; avatar: string; latLng?: google.maps.LatLng }[] = [];

    return isLoaded ? (
        <>
            <PlacesAutocomplete setCenter={setCenter} setZoom={setZoom} />
            <GoogleMap mapContainerStyle={containerStyle} center={center} zoom={zoom} onClick={handleAddMarker}>
                <>
                    <OverlayView
                        position={{ lat: -34.397, lng: 150.644 }}
                        mapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}
                    >
                        <>
                            <MarkerClusterer>
                              {clusterer => markers?.map((obj: any, i) => (
                                <Marker
                                    onClick={handleMarkerHover}
                                    ref={markerRef}
                                    key={i}
                                    position={obj.latLng}
                                    label={{
                                        // text: obj.name,
                                        text: obj.avatar
                                            ? obj.name
                                            : obj.name
                                                  .split(' ')
                                                  .map((name: string) => name[0])
                                                  .join('')
                                                  .toUpperCase(),
                                        className: obj.avatar ? `marker-label-with-avatar` : 'marker-label-without-avatar',
                                    }}
                                    icon={{
                                        scaledSize: new google.maps.Size(50, 50),
                                        url: obj.avatar ? obj.avatar + '#custom_marker' : 'avatar_default.png' + '#custom_marker_w/o_avatar',
                                        origin: new google.maps.Point(0, 0),
                                        anchor: new google.maps.Point(25, 50),
                                    }}
                                    animation={google.maps.Animation.DROP}
                                    // draggable={true}
                                />
                            ))}
                            </MarkerClusterer>
                        </>
                    </OverlayView>
                </>
            </GoogleMap>
        </>
    ) : (
        <></>
    );
};

export default React.memo(App);

Answer: The expected return from function(clusterer: Clusterer) is a JSX.Element. So I would suggest the following changes:
          <MarkerClusterer>
        {(clusterer) => (

            {markers?.map((obj: any, i) => (
              <Marker
                onClick={handleMarkerHover}
                ref={markerRef}
                key={i}
                position={obj.latLng}
                label={{
                  // text: obj.name,
                  text: obj.avatar
                    ? obj.name
                    : obj.name
                        .split(' ')
                        .map((name: string) => name[0])
                        .join('')
                        .toUpperCase(),
                  className: obj.avatar ? `marker-label-with-avatar` : 'marker-label-without-avatar',
                }}
                icon={{
                  scaledSize: new google.maps.Size(50, 50),
                  url: obj.avatar
                    ? obj.avatar + '#custom_marker'
                    : 'avatar_default.png' + '#custom_marker_w/o_avatar',
                  origin: new google.maps.Point(0, 0),
                  anchor: new google.maps.Point(25, 50),
                }}
                animation={google.maps.Animation.DROP}
                // draggable={true}
              />
            ))}

        )}
      </MarkerClusterer>


How to manipulate arrays?

Question: I have an array with 2 spots in it(it must have 2 spots in the beginning ). i need to get user input and double the array  when user enters more than the array can hold. Also Upon entering -1 the user input should stop. The array must also not accept duplicates.
i don't know how to have the not accept duplicates and end the program when the user entters -1 while still doubling the array every time it fills up.
Here's what i have so far but its flawed and wont run properly.
    public class GroupArrays {

public static void main(String[] args)  {

 Scanner input = new Scanner (System.in);
int [] numbers   =  new int[2];
int x;
int n=0;

    for( int i = 0; i<numbers.length; i++){
        System.out.println("Please enter number");
        int number =  input.nextInt();

         while ( input.hasNextInt() )
         {
            x = input.nextInt();

            if ( n == numbers.length )
            {

               int[] h;
               h = new int[ 2*numbers.length ];

               for ( int i1 = 0; i1 < numbers.length; i1++ )
               h[i1] = numbers[i1];

               numbers = h;

             }
          if(x == -1){
                break;
            }

             numbers[n] = x;
             n++;
         }

         for ( int i1 = 0; i1 < numbers.length; i1++ )
                System.out.println( numbers[i1] );
             }
          }

I can figure out how to do all the things individually but it becomes difficult when i have to do them all to 1 array and do user input.
PS. I feel like i have made this more complicated than it needs to be.
Answer: I'm only give you hints on how to solve the problem, and on how to write code that is easier to understand and maintain, even (and most importantly) by yourself.
Split the code in methods, doing simple tasks. Don't try to write everything as a giant piece of code.
The pseudo code of the algorithm would be
loop
    ask and get input number

    if (number is -1)
        break out of the loop

    else if array already contains number
        print error message

    else

       if no space left in array
           array = copy of array with doubled length

       store number in next available index in array and increment the next available spot index
end of loop

print the array

Now, you can see that there are things that can be implemented easily as  separate methods:

ask and get input number
check if array already contains number
copy of array with doubled length
print the array

Start by writing the signature of these simple methods, without implementing them. Then write the algorithm by calling these methods.
Then implement the simple methods one by one and test them one by one. You can do that, for now, by just writing a main method that calls them with hard-coded arguments, and see if they do what you expect them to do.
Once every of them is written and tested, test the main algorithm. If you made a mistake, iterate, by re-testing the methods you had to modify.
While coding all the methods, choose names that indicate clearly what the variables represent, and what the methods do, even if they're long. For example, nextAvailableIndex is much much clearer than x. Using good names helps everyone, including you, to understand the code and find bugs.
Also, indent the code strictly, as you type. The code you posted is not indented correctly, which makes it difficult to understand its structure. Stick to 4 spaces as indentation level, and always use curly braces around the body of your if, for, etc. ALways place them after the if/for, and not on the next line. Be consistent.
Comment: Thanks for the help. I should start psudocoding on paper first. i have a problem of figuring it out as i type and usually i'm wrong.
Comment: That's why it's very important to split it in small, simple parts. Those are easier to grok, implement and test. And if you messed up, they're easier to modify. Once you have good simple building blocks, assembling them becomes easier.
Answer: public class GroupArrays {

 public static void main(String[] args)  {

 Scanner input = new Scanner (System.in);
 int [] numbers   =  new int[2];
 int x;
 int n=0;

for( int i = 0; i<numbers.length; i++){
    System.out.println("Please enter number");
    int number =  input.nextInt();

     while ( input.hasNextInt() )
     {
        x = input.nextInt();

        if ( n == numbers.length )
        {

           int[] h;
           h = Arrays.copyOf[numbers,2*numbers.length ]; //change

         }
      if(x == -1){
            break;
        }

         numbers[n] = x;
         n++;
     }

     for ( int i1 = 0; i1 < numbers.length; i1++ )
            System.out.println( numbers[i1] );
         }
      }

Use Arrays.copyOf(array_name,array_length); to create new arrays with varying lengths.

How to manipulate arrays?


Help with MLE regression

Question: I have a data set containing two variables x and y. I want to estimate the parameters for a regression model.
The regression model is:

The maximum likelihood function that estimates the parameters is:

Here is a link to the data set: http://www.filedropper.com/data_14
Is there a way to estimate the parameters of this function? I've tried and spent many hours with R but can't get it to work. Matlab will do just fine as well.
Thanks in advance!
Comment: what is the model that this comes from? There may already be something that estimates it.
Comment: Hi Glen, I have added the model in the question now. Thanks!
Comment: You have part of it. What's the distribution on $\varepsilon$? Is it t-distributed?? If so, shouldn't $\nu$ appear on the right hand side of your log-likelihood?
Comment: Hi Glen, sorry for my unclear question. Yes, ε is t-distributed. In this case, ν=3. I've updated the log likelihood function. I would appreciate if you could help me out with this problem. Thanks in advance!
Comment: Perhaps you could tell us what is unclear after trying the examples on the `optim` help page?
Answer: Your log-likelihood is wrong:
A t-density with location $\mu$, scale $\sigma$ and degrees of freedom $\nu$ is given by:
$f(t) = \frac{\Gamma(\frac{\nu+1}{2})} {\sqrt{\nu\pi}\,\sigma\,\Gamma(\frac{\nu}{2})} \left(1+\frac{(t-\mu)^2}{\nu\sigma^2} \right)^{-\frac{\nu+1}{2}}\!.$
setting $\nu=3$ and replacing $\sigma$ with $\sigma x^\lambda$.
$f(t) = \frac{2} {\sqrt{3}\pi\,\sigma x^\lambda\,} \left(1+\frac{(t-\mu)^2}{3\sigma^2x^{2\lambda}} \right)^{-2}\!.$
Writing the likelihood (using $y_i$ for $t$ and $\beta_0+\beta_1 x_i$ for $\mu$)
$$\cal{L} = \prod_{i=1}^{n} \frac{2} {\sqrt{3}\pi\,\sigma x^\lambda\,} \left(1+\frac{(y_i-\beta_0+\beta_1 x_i)^2}{3\sigma^2x^{2\lambda}} \right)^{-2}\!.$$
So the log-likelihood is
$$\cal{l} = k+ \sum_{i=1}^{n} -\log(\sigma x^\lambda\,) -2\log\left(1+\frac{(y_i-\beta_0+\beta_1 x_i)^2}{3\sigma^2x^{2\lambda}} \right)\!.$$
(though the $k$ may freely be dropped; it's still the log of the likelihood)

With pre-specified df, as here, MLE might be used*, but you may have to take some care with the heteroskedasticity parameter $\lambda$.
One possibility is to start with a robust line, estimate an approximate $\lambda$ from the residuals, and then cycle between estimating the parameters (e.g. possibly via the package heavy) of the line and the value of $\lambda$ (via univariate maximization).
Alternatively, you could maximize all the parameters simultaneously, for example, via something like maxLik::maxLik (again, starting with a robust line).
Note that if you use a function which doesn't have weights, you'd need to use a transformation to achieve the weighting (which in turn would require the ability
to fit a regression without a constant term).
* see, for example, http://davegiles.blogspot.com.au/2013/12/more-on-student-t-regression-models.html
Comment: Wow! Thanks Glen!

Is there a easy way to estimate the parameters using for example R or Matlab or any other statistical software?

Help with MLE regression


What is the fastest route to drop off weight when time is proportional to weight x distance?

Question: You have a lorry at the starting point which is carrying all the parcels for the day.
$$\rm Time\ taken = Total\ Lorry\ Weight \times Distance\ travelled $$
After visiting each zone you have to return to the starting point.
Lorry's weight is $30$. You must take all undelivered parcels with you on every trip. Your fuel has no weight, your fuel is everlasting and you lose no time when dropping off packages.
What is the optimal route to delivering all the packages and returning to the starting point?

My initial thoughts for solving this was to work backwards and work out which delivery would add the smallest amount of total time across the whole journey. Then repeat this until I got to the first stop.
I have been told that isn't the correct way to work this out so not sure what would make the route I got for that more optimal.
Comment: I think the weight of the lorry itself is irrelevant since it must travel a round trip to each zone independent of the order of the zones. That said, I suspect there is no straightorward algorithm. You'll need something like backtracking: https://en.wikipedia.org/wiki/Backtracking (I hope I am wrong and someone contributes a good answer for you.)
Comment: I understand the optimization problem, but "time" sounds like the wrong name for what you want  optimize. The lorry will just creep along at the start of the day. Fuel consumption is more likely to be proportional to that product. The fuel weight will be (essentially) constant for an electric lorry.
Comment: @EthanBolker I have added the fuel to be everlasting, as the point is fuel is not a factor to be concerned with.
Comment: I understand that fuel is not a factor. What looks unreasonable to me is that equation for time, which (in real life) isn't close to proportional to weight. Of course the optimization question is independent of what you call that quantity.
Answer: This is not an answer as I can't prove it, but perhaps others can (or show the error of my ways).
If all parcels weighed the same, the optimal strategy would be to unload the nearest parcels first. This means we could rank the zones to visit first by ranking them by their distance $d$, i.e. rank $r=d$.
If all distances were the same, the optimal strategy would be to unload the heaviest parcels first. This means we could rank the zones to visit first by ranking them inversely by their weight $w$, i.e. $r=\frac{1}{w}$.
Putting these two observations together, it would seem a good first approximation would be to rank zones according to $r=\frac{d}{w}$. If I do this, then computer simulation shows this gives exactly the optimal route for the first $2$ zones, the first $3$ zones, etc up to the first $6$ zones (after which my recursive algorithm encountered overflow).
It seems to me then that a simple ranking of the zones by $r=\frac{d}{w}$ will give the optimal route.
EDIT
Changed my simulation from recursive to non-recursive and found the answer holds up to at least $8$ zones.
EDIT2
Improved simulation shows it works for $11$ zones. I'm convinced. :) It only remains to prove it will always work.
Answer: The answer by @Jens is correct: it is sufficient to rank the zones by means of $\frac dw$ from lowest to highest and follow this order.
Let us compare two different orders of delivery that differ just by a swap in two consecutive destinations. So suppose that in the first case at a certain point you serve zone A at distance $d$ and weight $w$, and immediately after you serve zone B at distance $d'$
and weight $w'$. In the second case you reverse the order of these two destinations. The cost for the other zones is the same in the two cases, so it is sufficient to compare the costs of these two. Say that before  serving zone A you have weight W. Then the cost in the first case is
$$C_1=Wd+(W-w) (d+d') +(W-w-w')d'.$$
In the second case the cost is
$$C_2=Wd'+(W-w')(d+d')+(W-w-w')d.$$
Then
$$C_1\leq C_2\iff  -2wd'\leq - 2w'd\iff \frac{d}{w} \leq \frac{d'} {w'}. $$
Since through consecutive swapping we can reach any permutation then we can lower the cost until we reach the desired increasing order, when we can not decrease anymore.
Comment: Thank you for your proof! I gave it a (+1) about a week ago, but was too distracted at the time to make a comment. Thanks again.


Get the max value using kotlin-exposed

Question: I want to simulate this query using kotlin exposed framework:
SELECT max(episodes.season_number) FROM episodes WHERE episodes.parent_id = 944947

I tried the next one query:
fun countSeasonsForParentId(id: Int) = transaction {
   EpisodeTable.slice(EpisodeTable.season)
       .select { EpisodeTable.parentId eq id }
       .map { it[EpisodeTable.season] }.maxOrNull()
}

But this query just results in:
SELECT episodes.season_number FROM episodes WHERE episodes.parent_id = 944947

and the max value is selected in the code.
How to perform finding max value on the side of the database and map it to Int?
Answer: I tried similar query and I believe it fits your need.
The query is:
        Reservations
            .slice(Reservations.date.max())
            .select { Reservations.note eq "it" }
            .maxByOrNull { Reservations.date }

And it generates SQL query:
SELECT MAX(reservations."date") FROM reservations WHERE reservations.note = 'it'

Your query can be used as:
    EpisodeTable
   .slice(EpisodeTable.season.max())
   .select { EpisodeTable.parentId eq id }
   .maxByOrNull { EpisodeTable.season }
   ?.get(EpisodeTable.season.max())

Alternative, for extracting one value seems more appropriate:
   EpisodeTable
   .slice(EpisodeTable.season.max())
   .select { EpisodeTable.parentId eq id }
   .firstOrNull()
   ?.get(EpisodeTable.season.max())

Comment: thanks, I only replaced `maxByOrNull()` with `firstOrNull()`
Comment: this type of query returns `ResultRow` but `Int` is required
Comment: You are right, I missed last part - to get the value out of the result row. Try the updated answer please.
Comment: Agreed, that is much better for extracting one value.

Get the max value using kotlin-exposed


how to append string

Question: I am parsing a file to get the selected string and build into a single line, however, I don't know how to do it(as shown in //add...) in shell scripts
while read line
do
    tt=`echo $line | cut -d'|' -f2 | cut -d'"' -f1`
    //add a $total = add all tt parts into a big string seperate by ", "
done < tmp_file

echo $total >> outfile

thank you
Comment: Just echo whatever in `tt` (plus comma) to the output?
Comment: i need to build one line($total) for each file, and that line is made with many $tt pieces.
Comment: It is possible to not print a new line with `echo`. Just that my idea will incur quite a large number of disk access.
Answer: You append in the shell using assignment and variable expansion:
total="${total}, ${tt}"

The curly braces ({}) aren't necessary in this case but I find they help distinguish variables when they're next to each other like this.
This will give you a leading ", ".  You can work around it like this:
total="${total:+${total}, }${tt}"

The ${variable:+value} construct only expands to value if variable is set.
Answer: The Code have to be like this i think ^_^"
while read line

do

    $tt=`echo $line | cut -d'|' -f2 | cut -d'"' -f1`

    $total .= $tt.", ";

done < tmp_file

echo $total >> outfile

i think that's all ^^ :D just a concatenation with a point missed  :p lol :D

how to append string


Nginx redirect *:port to subdomain

Question: I'm working with Nginxand I want to know how can I redirect all request with a specific port to a subdomain ?
This is my default.conf :
server{
        listen 80 default_server;
        server_name localhost;

        location / {
                root /usr/share/nginx/html;
        }
}

server{
        listen 80;
        server_name blog.mydomain.com;

        location / {
                proxy_pass   http://my-ip:8080;
        }
}

So with this I have the default mydomain.com serve the html folder, and a subdomain blog.mydomain.com serve an application running port 8080.
My problem is when I try to access directly my-ip:8080, or mydomain.com:8080 or blog.mydomain.com:8080 the server serve the application running port 8080 and I want to redirect all these requests to blog.mydomain.com without the :8080.
How can I do that ? Automatically redirect to blog.mydomain.com if I specify :8080 in the url ?
Answer: I think you don't quite get how it works.
NGINX is not the one serving the files on port 8080 but the application running on that port. If you want to disable port 8080 serving the files, you should bind the application to 127.0.0.1:8080 or use a firewall to prevent connecting to it.
Moreover, since your configuration shows you proxying requests to your IP (if it has been redacted I imagine it is your public address), achieving a redirect on port 8080 will lead to a redirect loop.
Comment: Also note that AFAIK, :8080 is not actually part of the URL so you would have to have the application running on port 8080 determine if the request is being proxied through NGINX or not which is a hassle for very little gain.

Nginx redirect *:port to subdomain


Unkown 6-sided dice. After 600 rolls frequency for all sides exactly equal. What is the chance, that rolling "6" with this dice has frequency > 1/6?

Question: Although it is unknown dice, the symmetry of the evidence tells us, that we can treat the dice as fair, so the chance should be exactly 50%.
But if we simulate it by hand, the result is less then 50%:

Mathematica code

In[1]:= nsym = 200000 (*Some very large number of simulations*)
In[46]:= sample = RandomReal[DirichletDistribution[Table[100, {6}]], nsym];
In[51]:= counts = If[#1 > 1/6, 1, 0] & /@ #1 & /@ Transpose[sample];
In[52]:= N[Plus @@ Transpose[counts]/nsym]
Out[52]= {0.49387, 0.48934, 0.487965, 0.488245, 0.49123}

R code

require(gtools)
nsym=200000  #Some very large number of simulations
sample<-rdirichlet(nsym,rep(100,6))
apply(sample,2,function(x,testval){sum(x>testval)},testval=1/6)/nsym
# [1] 0.489715 0.490490 0.489800 0.490315 0.491900 0.488165

What am I doing wrong??

update:
I've found out empirically, that the mean of the difference between the above probabilities and the "expected" 0.5 equals approximately $0.096/\sqrt{100}$, where "100" is the number of prior trials.
Comment: Thank you.  I think it would help to edit your question to make that approach clearer, rather than relying on the code to convey what you are doing. Consider improving the title, too: because it refers to "frequency" it suggests something other than the (posterior) *probabilities* which you seem to be estimating.
Comment: @whuber I do Bayesian reasoning here: I don't simulate outcomes of the rolls of die - for that I'd use categorial distribution. I simulate the probabilities with which the die rolls, given the already observed historical outcomes, i.e. the posterior distribution of the parameters $\pi_{1\ldots6}$ that define the die.
Comment: What justifies drawing from the Dirichlet distribution to simulate outcomes of the rolls of a die?
Answer: Seems like 2% of the time you probably get exactly 100 rolls which is neither greater or less than 1/6. It is exactly 1/6.
Answer: Dirichlet distribution is not multivariate normal, so it makes no sense to expect that mean ($1/6$) equals median (i.e. one observes values below mean with frequency 0.5).
The bigger is evidence (i.e. the number "100" is greater), the more the dirichlet distribution converges to normal distribution.
Why the expected 0.5 frequency is bigger then the observed?
It so happens, that (similar to the beta distribution) dirichlet distribution is positively skewed when the shape parameter is < 0.5 (in our case it equals 1/6). Hence the mean (0.5) is greater then the median.


Why does SQL Server perform a clustered scan when IN clause has a subquery?

Question: If I search for users like so:
SELECT *
FROM userprofile
WHERE userid IN (1, 2, 3)

The execution plan shows that UserProfile is using a Clustered Index Seek
If I change the IN clause to use a subquery:
SELECT *
FROM userprofile
WHERE userid IN (
    SELECT DISTINCT senders.UserId
    FROM messages m
    JOIN UserMessages recipients ON recipients.MessageId = m.MessageId
    JOIN UserMessages senders ON senders.MessageId = m.MessageId
    WHERE recipients.TypeId = 2
        AND recipients.UserId = 1
        AND senders.UserId <> 1
        AND senders.TypeId = 1
)

The execution plan shows the subquery is using a Clustered Index Seek but the UserProfile outer query is using a Clustered Index Scan.
How can I write this so both inner and outer queries are using Seeks?
Answer: A set of seeks is only cheaper than a full scan if the rowcount is low.  SQL Server is pretty conservative so if there is a chance many records will be found, it prefers to scan.  In your example, it's pretty clear that userId in (1,2,3) will not return many rows.  But with the subquery, SQL Server probably can't tell.
You can force a seek with:
from userprofile with (forceseek)

Comment: Exactly - and since the optimizer can't decide how many rows the inner subquery would produce, it's not using what basically amounts to *key lookups* (index seeks into the clustered index) to find all those matching rows - it just defaults to a clustered index **scan** instead.
Answer: Forcing it to seek will not necessarily be any faster and it might be significantly slower.
if you rewrite your query to use EXISTS instead of IN, it may be faster:
select * from userprofile    -- << note that '*' is slow here
where EXISTS(
  select * from messages m   -- << but '*' is not slow here
  join UserMessages recipients on recipients.MessageId = m.MessageId
  join UserMessages senders on senders.MessageId = m.MessageId
  where recipients.TypeId = 2
    and recipients.UserId = 1
    and senders.UserId <> 1
    and senders.TypeId = 1
    and senders.UserId = userprofile.userid
)

It certainly won't be any slower.
Comment: Unless the query optimizer does something really smart, `exists` will result in one subquery per row in `userprofile`.  That can be much slower than a clustered index scan.
Comment: No more than the original `IN(..)` would. In fact, all modern professional SQL DBMS's are smart enough to optimize this, *if its reasonably possible*. Anyway, there is virtually no case where `IN(..)` is faster than the corresponding `EXISTS(..)` in SQL Server. So try it and see.


Prove that integral $I_{p} = \int_{0}^{\frac{\pi}{4}}\frac{\ln\cos x}{x^p\cos^2x}dx$ is convergent for $p < 3$

Question: For $p \in \mathbb{R}$ we have integral $$I_{p} = \int_{0}^{\frac{\pi}{4}}\frac{\ln\cos x}{x^p\cos^2x}dx$$
So I have to prove that this integral $I_{p}$ is convergent for $p < 3$
Any help how to start?
Answer: Using Taylor's series, you have around $0$
$$\frac{\ln\cos x}{x^p\cos^2x} \sim -\frac{1}{2}\frac{1}{x^{p-2}}$$ and
$\int_0^1 \frac{dx}{x^q}$ converges if and only if $q<1$.
Comment: Is this the only way or is also another way to solve to this problem? I do not know how did you get this result using Taylor ' s series
Comment: I won't venture to say it is the only way! But at the end, *evaluating the size* of $\ln \cos x$ is a natural way. And Taylor's series are appropriate for that.


How to show annihilator has dimension m-n (with Proof)

Question: I would like to show the following:
Given a vector spaces $V$, a subspace $S \subset V$ and an the dual space $V^*$ to $V$.
Show that: $$\dim(N)+\dim(S) = \dim(V) = \dim(V^*)$$, where $N \subset V^*$ is the annihilator to $S$. Assume only finite dimensional vector spaces.
I tried a proof but I am not sure if this is correct:
Proof:
I give a try :-)
Assume that
$\dim(S) = m$,
$\dim(V) = \dim(V^*) = n$
The annihilator is given as:
$$N:= \{ \boldsymbol{\alpha} \mid   \langle \boldsymbol{\alpha},\mathbf{x} \rangle =0 \quad ,\quad \forall \mathbf{x} \in S \}$$
Where $ \langle \boldsymbol{\alpha},x \rangle$ is the duality pairing, it is a bilinear form as:
$$ \begin{aligned} \textrm{B}(  \boldsymbol{\alpha},\mathbf{x} ) :   V^* \times V &\rightarrow \mathbb{R} \\
\boldsymbol{\alpha},\mathbf{x} &\mapsto \langle \boldsymbol{\alpha} ,\mathbf{x} \rangle
\end{aligned}
$$
We fix basis vectors for the following spaces:
Basis for $V$:   $\{\mathbf{e}_i\}$ with $i \in \{1,n\}$
Basis for $S$: $\{\mathbf{e}_i\}$ with $i \in \{1,m\}$
Basis for $V^*$: $\{\boldsymbol{\alpha}^i\}$ with $i \in \{1,n\}$
Now with $\boldsymbol{\beta} =\beta_j \boldsymbol{\alpha}^j \in V^*$ and $\mathbf{x} = x^i \mathbf{e}_i  \in V$, it follows that
$$ \langle \boldsymbol{\beta} ,\mathbf{x} \rangle = \langle \beta_j  \boldsymbol{\alpha}^j ,x^i \mathbf{e}_i\rangle = \beta_j \langle \boldsymbol{\alpha}^j , \mathbf{e}_i \rangle  x^i  = \beta_j \ B^j_{\ i} \ x^i \quad i,j \in \{1,n\}$$
We want now for every $\mathbf{x} \in S$ and a corresponding $\boldsymbol{\alpha} \in N$ that:
$$\begin{aligned}
\langle \boldsymbol{\beta} ,\mathbf{x} \rangle = 0 &= (\beta_1, \cdots, \beta_n)\left( \begin{array}{ccc}
B^1_{\ 1} & \cdots  & B^1_{\ m} \\
\vdots & \cdots  & \vdots \\
\vdots & \cdots  & \vdots \\
B^n_{\ 1} & \cdots  & B^n_{\ m}
\end{array} \right) \left(\begin{array}{c} x^1 \\ \vdots \\ x^m\end{array} \right)
\end{aligned} = [\boldsymbol{\beta}]^\top\mathbf{B}[\mathbf{x}] =[\mathbf{x}]^\top\mathbf{B}^\top [\boldsymbol{\beta}], \quad \forall [\mathbf{x}]
$$
From where follows:
$$\mathbf{B}^\top [\boldsymbol{\beta}] = \mathbf{0}$$
If we assume that $\boldsymbol{\alpha}^i$ is the dual basis vector to $\mathbf{e}_i$ with the property:
$$
\langle \boldsymbol{\alpha}^i, \mathbf{e}_j \rangle = \delta^i_{\ j}
$$, where $\delta^i_{\ j}$ is the Kroenecker Delta.
It follows that $\delta^j_{\ i} = B^j_{\ i}$. Thus $[\boldsymbol{\beta}]$ lies in the Nullspace of $N$, $[\boldsymbol{\beta}] \in \text{Null}(\mathbf{B}^\top)$.
The dimension of $\text{dim}(\text{Null}(\mathbf{B}^\top)) = n-m$
which means what now? and how can I construct a basis from the given ones for $N$?
Thanks for the inputs!
Comment: You seem to assume $\dim V < \infty$ in your statement, as otherwise $\dim V = \dim V^*$ won't hold.
Comment: I assume you mean $\dim V<\infty$. The restriction $\phi_{|S}$ of a linear functional $\phi\in V^*$ yields a functional in $S^*$. This gives a surjective linear map $V^*\longrightarrow S^*$ whose nullspace is the annihilator of $S$. Apply the rank-nullity theorem (or the first isomorphism theorem if you prefer).
Comment: Take a look at [this post](http://math.stackexchange.com/questions/611575/dimension-of-annihilator-textdim-u0-textdim-u-textdim-v).
Answer: Let $V$ a finite dimensional vector space with $dim\;V=n$ and $V'$ be its dual space over a field $\Bbb F$.
This can be proved in two  ways:
$\mathbf {Proof \;1}:$ Let $B_S=\{s_1,...,s_k\}$ be a basis for the subspace $S$. So $dim\;S=k$. Extend $B_S$ to a basis of $V$ say $B_V=\{s_1,...,s_k,s_{k+1},...,s_n\}$. Let $B^{'}_V=\{f_1,...,f_n\}$ be the dual basis to $B_V$ where $f_i(s_j)=\delta_{i,j}$, the Kronecker Delta.
My Claim is that $\{f_{k+1},...,f_n\}$ is the basis of $N=S^0$. Indeed let $f\in V'$ then $f=a_1f_1+...+a_nf_n$ for some $a_1,...,a_n \in \Bbb F$. Now $f(s_j)=a_jf_j(s_j)=a_j$. So $f=f(s_1)f_1+...+f(s_n)f_n$. If $i\in \{k+1,...,n\}$ and $j \in \{1,...,k\}$ then $f_i(s_j)=0$. So if $s\in S$ then $s=b_1s_1+...+b_ns_k$ for some $b_1,....,b_k\in \Bbb F $. Then $f_i(s)=0\; \forall i \in \{k+1,...,n\}$. Hence $f_{k+1},...,f_n \in S^0$. Then $ span \{f_{k+1},...,f_n \}\subseteq S^0$ since $S^0$ is a subspace. Now suppose $f\in S^0$ then $f(s_1)=...=f(s_k)=0$ since $s_1,...,s_k \in S$. Then writing $f=f(s_1)f_1+...+f(s_n)f_n$, the first $k$ terms are zero i.e $f=f(s_{k+1})f_{k+1}+...+f(s_n)f_n$. So  $f\in span\{f_{k+1},...,f_n\}$. Hence $S^0\subseteq span\{f_{k+1},...,f_n\}$. Hence $S^0=span\{f_{k+1},...,f_n\}$, and of course $\{f_{k+1},...,f_n\}$ is linearly independent being a subset of a set of basis vectors.So $dim\;S^0=n-k=dim\;V-dim\;S$.So $dim\;V=dim\;S + dim\;S^0$ . $\lhd$
$\mathbf {Proof \;2}:$ Now suppose $i:S\rightarrow V$ be the natural inclusion map i.e $i(s)=s,\; \forall s \in S$. Consider its dual  map $i':V'\rightarrow S'$ defined as follows $i'(\phi)=\phi\,\circ i$. Thus $i'$ is a linear map from $V'$ to $S'$. Then $dim\,range(i')+dim\,null(i')=dim\,V'=dim\,V$. Now my first claim is,
$\it{Claim \;1}:$ $null(i')=S^0.$
$\it{Pf}:$ If $\phi \in null(i')\Leftrightarrow i'(\phi)=0\Leftrightarrow \phi\,\circ i=0\Leftrightarrow(\phi\,\circ i)(s)=0,\,\forall s\in S\Leftrightarrow\phi(i(s))=0,\,\forall s\in S$ $\Leftrightarrow\phi(s)=0,\, \forall s\in S\Leftrightarrow \phi \in S^0\,\bullet$
Now we have $dim\,range(i')+dim\,S^0=dim\,V$. This means that  $dim\,range(i')$ must be equal to $dim\,S=dim\,S'$. But $range\,(i')\subseteq S'$. So it means that $i'$ must be surjective i.e $range(i')=S'$. Indeed,
$\it{Claim\;2}:$ $range(i')=S'$.
$\it{Pf}:$ Let $\phi \in S'$. So $\phi:S\rightarrow \Bbb F$. We extend $\phi $ to a functional $\psi :V\rightarrow \Bbb F$ such that $\psi(s)=\phi(s),\,\forall s\in S$. So $i'(\psi)=\psi\,\circ i$. But $(\psi\, \circ i)(s)=\psi(i(s))=\psi(s)=\phi(s),\,\forall s \in S$. Hence $\psi\, \circ i = \phi \Leftrightarrow i'(\psi)=\phi$. So $S'=range(i')\,\bullet$.
Finally we have $dim\,S'+dim\,S^0=dim\,V\Rightarrow dim\,S+dim\,S^0=dim\,V.\,\lhd$


Are foreign key constraints needed?

Question: In a perfect world, are foreign key constraints ever really needed?
Comment: Only if you have more than one table.
Comment: @Charles: A foreign key can reference the same table in which it's defined, e.g. `employees.manager_id`.
Comment: What's the problem with FK constraints? Why would you not using them?
Comment: Possible duplicate of [Are foreign keys really necessary in a database design?](https://stackoverflow.com/questions/18717/are-foreign-keys-really-necessary-in-a-database-design)
Answer: Foreign keys enforce consistency in an RDBMS.  That is, no child row can ever reference a non-existent parent.
There's a school of thought that consistency rules should be enforced by application code, but this is both inefficient and error-prone.  Even if your code is perfect and bug-free and never introduces a broken reference, how can you be certain that everyone else's code that accesses the same database is also perfect?
When constraints are enforced within the RDBMS, you can rely on consistency.  In other words, the database never allows a change to be committed that breaks references.
When constraints are enforced by application code, you can never be quite sure that no errors have been introduced in the database.  You find yourself running frequent SQL scripts to catch broken references and correct them.  The extra code you have to write to do this far exceeds any performance cost of the RDBMS managing consistency.
Answer: In addition to protecting the integrity of your data, FK constraints also help document the relationships between your tables within the database itself.
Comment: +1 - good point. They can be very handy when using schema-aware code generation tools such as CodeSmith.
Answer: The world is not perfect that's why they are needed.
Answer: A world cannot be perfect without foreign keys.
Answer: Yes, if you want to ensure referential integrity.
Answer: In addition to consistency enforcement and documentation, they can actually speed up queries. The query optimizer can see a foreign constraint, understand its effect, and make a plan optimization that would be impossible w/o the constraint in place. See Foreign Key Constraints (Without NOCHECK) Boost Performance and Data Integrity. (SQL Server specific)
Answer: Additionally to the documentation effect Dave mentioned, FK constraints can help you to have write lesser code and automate some bits.
If you for example delete a customer record, all his invoices and invoice lines are also deleted automatically if you have "ON DELETE CASCADE" on their FK constrainst.
Comment: ... but why would you want that? If you delete a customer, wouldn't you still want a record of purchases made by them for bookkeeping purposes?
Comment: Chris: I see what you're saying, but in that case you probably don't want to delete the customer, just set a flag in their record saying they are no longer active.
Comment: @chris : that's why it's configurable!  For parent-child relationships, it might be ok, but for a purely referential relationship, obviously you wouldn't...

Are foreign key constraints needed?


O "ON()" não está agindo conforme o esperado, por quê?

Question: Caros amigos, sou um estudante e curioso no assunto, não um programador experiente, daí minha pergunta.
Vamos lá, o método "ON()", do JQUERY, pode estar associado a diversos tipos de eventos ("click", "mouseleave", etc.), inclusive a mais de um ao mesmo tempo, certo?! Pois bem, por meio do método "ON" e de um evento "CLICK", tentei associar uma mudança no "SRC" da imagem  (no "ALT" e no "TITLE", também). O que esperava que iria acontecer, que era, basicamente, mudar a imagem ao se clicar nela, acontece de modo insatisfatório, a imagem troca muito rapidamente e não permanece com a alteração, conforme se esperaria. Só ocorre no instante do click e volta ao que era depois de soltar o botão do mouse.
Seguem o HTML, as imagens e o JQUERY para análise. (Desde já, grato por sua ajuda!)
HTML

IMAGENS
(Bandeiras)

JQUERY
$(document).ready(function() {

    $(".img-esp").on("click", function() {

       //Mudar a bandeirinha para a do Brasil (idioma português)
       $(this).attr("src", "img/bra.png");

       //Mudar o atributo ALT da bandeirinha (idioma português)
       $(this).attr("alt", "Português (Brasil)");

       //Mudar o atributo TITLE da bandeirinha (idioma português)
       $(this).attr("title", "Português (Brasil)");
    });
});

Comment: Creio que isso acontece devido o atributo `href` estar presente na tag `a`, para resolver, altere `"click", function(e) { e.preventDefault(); ... ` ou apenas adicione um `return false;` ou simplesmente remova o atributo.
Comment: Retirei o A HREF e funcionou! Muito obrigado! Valeu!
Comment: O que é o "e.preventDefault();"? Desculpe a curiosidade em te perguntar.
Comment: preventDefault serve para alterar o comportamento padrão de um elemento no html.  https://jsfiddle.net/pq8Lkuso/3/ veja se é isso que deseja. Bastou adicionar
Comment: Se o  não está servindo, vc pode retirar ele todo e não apenas o href.
Comment: Obrigado pela ajuda, pessoal!
Answer: Jovem, permita-me fazer algumas observações sobre o seu código:
O uso da tag  pode ser dispensada se você está usando o evento click diretamente na imagem. Não remova apenas o href, remova a tag  toda. O que estava acontecendo era que ao clicar na bandeira a página estava sendo atualizada e você nem percebeu.
Ao alterar os atributos do mesmo elemento várias vezes, faça isso de uma só vez, usando a estrutura:
$(this).attr({
   "src": "img/bra.png",
   "alt": "Português (Brasil)",
   "title": "Português (Brasil)"
});

Basta separar os pares com dois-pontos (:) e um par de outro com uma vírgula (,), e incluir tudo entre chaves ({}). Não precisa ficar repetindo $(this).attr várias vezes.
E, para deixar a bandeira com o cursor padrão de um link, inclua no CSS:
.img-esp{
   cursor: pointer;
}

Isso é importante para que o usuário veja que ali há alguma ação ao ser clicado.
Se está usando <!DOCTYPE html> (o que deveria usar), não use auto-fechamento nas tags:

                                                                  ↑
                                                          auto-fechamento

Seu código com as sugestões citadas ficaria assim:

$(document).ready(function() {
   $(".img-esp").on("click", function(){
      //Mudar a bandeirinha para a do Brasil (idioma português)
      $(this).attr({
         "src": "img/bra.png",
         "alt": "Português (Brasil)",
         "title": "Português (Brasil)"
      });
   });
});
.img-esp{
   cursor: pointer;
}

Comment: Obrigado, amigo! Estou usando ``, sim! Só não entendi porque não posso usar o auto-fechamento das TAGs, assim não é o mais adequado, semântico?! A sua sugestão para aglutinar os atributos é muito boa também, meu caro! Não fica com aquela "tripa" enorme de códigos! Rê! Rê! Rê! Rê! Valeu!
Comment: =] O auto-fechamento de tags não é necessário em HTML5, exceto se você necessitar que a página seja **parseada** como documento XML.
Comment: Obrigado pelo esclarecimento!