How to access weight variables in Keras layers in tensor form for clip_by_weight?

Question: I'm implementing WGAN and need to clip weight variables.
I'm currently using Tensorflow with Keras as high-level API. Thus building layers with Keras to avoid manually creation and initialization of variables.
The problem is WGAN need to clip weight varibales, This can be done using tf.clip_by_value(x, v0, v1) once I got those weight variable tensors, but I don't know to how to get them safely.
One possible solution maybe using tf.get_collection() to get all trainable variables. But I don't know how to get only weight variable without bias variables.
Another solution is layer.get_weights(), but it get numpy arrays, although I can clip them with numpy APIs and set them using layer.set_weights(), but this may need CPU-GPU corporation, and may not be a good choice since clip operation needs to be performed on each train step.
The only way I know is access them directly using exact variable names which I can get from TF lower level APIs or TensorBoard, but this is may not be safe since naming rule of Keras is not guaranteed to be stable.
Is there any clean way to perform clip_by_value only on those Ws with Tensorflow and Keras?
Answer: You can use constraints(here) class to implement new constraints on parameters.
Here is how you can easily implement clip on weights and use it in your model.
from keras.constraints import Constraint
from keras import backend as K

class WeightClip(Constraint):
    '''Clips the weights incident to each hidden unit to be inside a range
    '''
    def __init__(self, c=2):
        self.c = c

    def __call__(self, p):
        return K.clip(p, -self.c, self.c)

    def get_config(self):
        return {'name': self.__class__.__name__,
                'c': self.c}

import numpy as np
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(30, input_dim=100, W_constraint = WeightClip(2)))
model.add(Dense(1))

model.compile(loss='mse', optimizer='rmsprop')

X = np.random.random((1000,100))
Y = np.random.random((1000,1))

model.fit(X,Y)

I have tested the running of the above code, but not the validity of the constraints. You can do so by getting the model weights after training using model.get_weights() or model.layers[idx].get_weights() and checking whether its abiding the constraints.
Note: The constrain is not added to all the model weights .. but just to the weights of the specific layer its used and also W_constraint adds constrain to W param and b_constraint to b (bias) param
Comment: Thank you for your reply. I've tried it on a toy problem for y=ax+b, it works perfectly!
Comment: There is problem when implementing it with Tensorflow. When using model.fit, it works perfectly, however when using sess.run(tran_step,...) with TF commands, it seems that the constrains does not work. Any ideas about how to fix it?
Comment: You are probably using optimizer object from tensorflow. keras constraints are applied in the optimizer classes implemented in Keras. For example for SGD, here are the constraints applied: https://github.com/fchollet/keras/blob/master/keras/optimizers.py#L184-L187. So you need to use keras optimizer for getting the train_step. `tran_step = model.optimizer.get_updates(model.trainable_weights, model.constraints, model.total_loss)`. Otherwise you can do is use `model.train_on_batch()` which will take care of all the keras internals & trains just on single batch just as `sess.run(tran_step,...)`
Comment: Got it. Thank you. I'll try to use the first solution. The reason I use TF with Keras API instead of Kears directly is that I'm trying to avoid using model, thus I can add some complected architecture (like GatedCNN in PixelCNN) to the network in the future. By the way, it seems that `keraslayers.W.name` can return name of weight variables. However I still can't apply clip to `Convolution2D` layers since TF report that shape of weight variable is not given explicitly.
Comment: Well good luck. If you have other doubts ask a new question. Please accept the answer if it helped.


Eigenvector for rank one matrix $\mathbf{x} \mathbf{x}^{\top}$ disagrees with statistical software

Question: Consider a matrix $\mathbf{M} = \mathbf{xx}^{\top}$. Then it has one non-zero eigenvalue (since it is rank $1$) which is $\lambda \triangleq \mathbf{x}^{\top} \mathbf{x}$:
$$
\begin{aligned}
\mathbf{M x} \mathbf{x} &= \mathbf{x x}^{\top} \mathbf{x}
\\
&= \mathbf{x}^{\top} \mathbf{x x}
\\
&= \lambda \mathbf{x}.
\end{aligned}
$$
This also immediately implies that the eigenvector associated with $\lambda$ is simply $\mathbf{x}$ or any scaled version of $\mathbf{x}$, say $c \mathbf{x}$ for $c \neq 0$, since we could propagate $c$ thru the calculations. However, when I try this with statistical software, I get that my calculation for $\lambda$ is correct but not for the eigenvector.
>>> x = np.array([1, 2, 3, 4])
>>> M = np.outer(x, x)
>>> np.dot(x, x)
30
>>> vals, vecs = np.linalg.eig(M)
>>> vals[1]
29.999999999999993
>>> x / np.linalg.norm(x)
array([0.18257419, 0.36514837, 0.54772256, 0.73029674])
>>> vecs[1] / np.linalg.norm(vecs[1])
array([ 0.11528971, -0.62085412, -0.66034186, -0.40644443])

This may be a statistical computing question, but I'm asking here in case I'm misunderstanding something about the possible constraints on the eigenvector $c \mathbf{x}$.
Answer: You should be using vecs[:,1] instead of vecs[1].
vecs is the matrix whose columns are eigenvectors, but you've accidentally selected a row of this matrix instead of a column.


Understanding a certain solution in combinatorics (distinguishable objects into distinguishable boxes)

Question:
What are the number of ways for $42$ students to be in different classes with $6$ teachers, we know that the first two teachers will have 8 students each , $3$ others will have $7$ students and the last one will have $5$ students

the solution in the book ${42 \choose 8}  \cdot {34 \choose 8} \cdot {26 \choose 7} \cdot {19 \choose 7} \cdot {12 \choose 7} \cdot {5 \choose 5}= \frac{42!}{8!8!7!7!7!5!}$
what I thought about was
first pick one of the two teachers then the other and so on for each one but why is this wrong?
${2 \choose 1} \cdot {42 \choose 8} \cdot {34 \choose 8} + {3 \choose 1} \cdot {26 \choose 7} \cdot {2 \choose 1} \cdot {19 \choose 7} \cdot {1 \choose 1}\cdot{12 \choose 7}+{1 \choose 1} \cdot {5 \choose 5}$
why is this wrong? don't we have to choose one of the teachers? it says that they are distinguishable
I tried to look at this problem as distributing $42$ distinguishable balls into $6$ different boxes with each having their unique capacity $ 8 = x_1,x_2$ $\space$ $7 = x_3,x_4,x_5$ and $5 = x_6$
Answer: First, the solution in book is correct but there's a typo. It should be  ${42 \choose 8}  \cdot {34 \choose 8} \cdot {26 \choose 7} \cdot {19 \choose 7} \cdot {12 \choose 7} \cdot {5 \choose 5}= \frac{42!}{8!8!7!7!7!5!}$
The idea is that you can start with any teacher. You will have 42 choose X students for her. Then chose the second teacher, you will have (42 - X) choose Y students and so on. Since each choice impacts the other choice you would need to multiply them.
Comment: thank you but you say start with any teacher , why not multiply by $8 \choose 1$?
Answer: You are overcounting. Let's say $X$ and $Y$ are the teachers who will get $8$ students each, and let's say you have students $A_1, B_1, C_1, D_1, E_1, F_1, G_1, H_1, A_2, B_2, C_2, D_2, E_2, F_2, G_2, H_2$. For you, the outcomes where:

First $X$ gets $A_1, B_1, C_1, D_1, E_1, F_1, G_1, H_1$ and then $Y$ gets $A_2, B_2, C_2, D_2, E_2, F_2, G_2, H_2$
First $Y$ gets $A_2, B_2, C_2, D_2, E_2, F_2, G_2, H_2$ and then $X$ gets $A_1, B_1, C_1, D_1, E_1, F_1, G_1, H_1$

are counted as different outcomes, whereas they are actually the same.
Answer: It is putting students into labeled (by teacher) teams, which is one of the three common ways of representing a  multinomial coefficient, viz
$$\small{42\choose 8}{34\choose 8}{26\choose 7}{19\choose 7}{12\choose 7}{5\choose 5}$$
$$=\binom{42}{8,8,7,7,7,5}$$
$$= \frac{42!}{8!8!7!7!7!5!}$$

Addressing comment of @adamrk to @linkja answer
The question header should more precisely have been distinguishable objects into particular distinguishable boxes (which is what the multinomial coefficient signifies)
If the objects could go to any of the boxes, we would have needed to multiply the answer by $\Large\frac{6!}{2!3!1!}$
Answer: I would say the solution on your book is right. My though process:
We can just pick one teacher find out how many possibilitys are for him and then move on to the next, as their order doesn't matter
For the first teacher with 8 students there are ${42\choose 8}$ possible ways to give him 8 students.
For the second teacher there are now, when the first one already has 8 students, $42-8 = 34$ possible students left, as he also gets 8 students there are now ${34\choose 8}$ possibilities.
This we repeat and get to all the possibilities by multiplying those for each teacher, similarly as when we multiply the possibilities for a combination lock, and we arrive at $${42\choose 8}{34\choose 8}{26\choose 7}{19\choose 7}{12\choose 7}{5\choose 5} = \frac{42!}{8!8!7!7!7!5!}$$
Comment: thank you , I had a typo
my question is that wouldn't we need to choose the teacher? like I did ? for example we know that 2 teachers must have 8 students , why don't we choose one of those 2 and multiply? $2 \choose 1$?
Comment: Because the order of the teachers doesn't matter but it matters which teacher they end up with as they are two distinct teachers and not one teacher teaching two classes of 8


Cross Validation for Time Series Classification (Not Forecasting!)

Question: Is it possible to use regular k-fold cross validation where the folds contain entire time series in time series classification? I'm asking because most sources discussing cross validation with time series say a specific rolling window approach should be used.
I'm doing a time series classification project where I have a large number of multivariate time series of equal length with the same dimensions. I understand that one should not arbitrarily split the time series themselves in the time dimension, and that a sliding window should be used in such a case (where the folds are supersets of preceding folds). However, I do not understand why the folds cannot contain entire time series (e.g training on Time Series 0-100, testing on 101-200, then training on time series 101-200 and testing on 0-100).
I also noticed that in sktime (a python package for time series analysis) the rolling window approach to cross validation splitting was only implemented in their forecasting module, and not in their classification module. Therefore I'm wondering if the sliding window approach to cross validation is only relevant for forecasting tasks where you perhaps only have one time series (and therefore must split in the time dimension) and not for time series classification (where you have multiple entire time series that can be used in distinct folds).
Comment: Direct application of cross-validation on temporal data is not possible due to serial correlation and absurdity of using future data to predict past. Though there are proposal of block sampling, see [Stationary Bootstrap](https://www.jstor.org/stable/2290993). However, we have also  proposed a meta algorithm so called [rCV](https://arxiv.org/abs/1910.09394) at a fixed window and that each fold is tested on the out-of-sample portion. Essentially, we could generate folds by removing data points at random and reconstruct them using interpolation or similar using secondary model.
Comment: I don't see the issue with using entire time series - the issue is that the blocks need to be statistically independent. Maybe this paper is helpful: Roberts, David R., et al. "Cross‐validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure." Ecography 40.8 (2017): 913-929.
Comment: What do you mean by classification? What exactly are you trying to do?
Comment: This type of classification: https://developersbay.se/time-series-classification-an-overview/
Answer: I understand that your problem is that your dataset is a time series but that you are not interested in making forecast (i.e. making prediction of future state from past state). In this case, you should avoid random shuffling in your cross-validation because it may lead to overfitting. Indeed, you would have points in your testing set that are surrounded by points in your training set. Therefore, the performance of your model would be unrealistically high.
The scikit-learn user guide has an interesting note about this : https://scikit-learn.org/stable/modules/cross_validation.html#a-note-on-shuffling
Answer: There is no problem with using K-fold cross-validation using the entire time series in time series classification. It is commonly used with algorithms such as NN-DTW (Nearest Neighbour Dynamic Time Warping) to select the size of the warping window. The sktime package you referred to uses this type of cross-validation to select hyper-parameters for some of its classification algorithms - for example, Hive-Cote Lines, Taylor, and Bagnall: HIVE-COTE: The Hierarchical Vote Collective of Transformation-Based Ensembles for Time Series Classification.


Piano arpeggio symbol: split vs drawn through

Question: This may be an obvious thing for more experienced piano players, but I was wondering whether there is a difference between having a single arpeggio symbol that spans both the left and right hand and having two separate arpeggio symbols for each hand.

I am thinking that they likely mean the same thing (play all the notes consecutively, first the left, then the right hand), but I thought that it might be possible that the split arpeggios are meant to played in parallel (each hand plays its notes consecutively, but both hands play in parallel).
So my question is whether the two notation types are actually the same (maybe due to a limitation in notation software or because of readability), or whether they could notate different ways of playing the arpeggio(s). And if they are the same, are there cases of "parallel" arpeggios? How would they be notated?
Edit:
I want to thank everyone for clearing up that the two are in fact different.
However, by accident I came across this article, which claims

When playing an arpeggio with both hands on a keyboard instrument a distinction is made between chords where from the bottom of the left hand chord to the top of the right hand chord no note is missing and those where one or more chord notes are missing.

So it seems that the second version with the split arpeggios should be interpreted as a continuous arpeggio based on whether or not the left and right hand chords are complete or not.
This sounds very confusing to me and would require the person writing the score to know this small difference and it would require the player to guess whether or not the notator was aware of this rule. It sounds kind of arcane and prone to interpretation errors. Is this actually widely accepted? I would be happy if you could incorporate this aspect in your answers.
Comment: BTW With a tempo  of allegretto there's hardly any audible difference between the two techniques. Arpeggio squiggles crossing the staves get in the way of the expression marks.The precise interpretation of arpeggios is often left up to the pianist. In harp music parallel arpeggios can be marked *brisé* which, confusingly, means *broken*.
Answer: The long one in the first example sounds starting from the bottom note in the left hand and runs through to the top note in the right hand.  In your second example, you start and finish both arpeggiated chords at the same time.
Comment: Wait, you always finish both arpeggiated chords at the same time if they're started simultaneously? I thought a 4-note chord might not end at the same time as a 3-note chord.
Comment: @Dekkadeci probably part of interpretation at that point
Comment: @Dekkadeci I agree that it's up to interpretation, but I'm gleaning (perhaps incorrectly) from your question that you're suggesting the 1st note of each chord should line up, 2nd of each, and so on, hence the 3-note chord ends at same time as 3rd note of 4-note chord. I think such an interpretation is not in the spirit of a rolled chord and risks turning a flourish into something rigid and metronomic, particularly through the effort required to sync each note at quick speed. Perhaps there's a style where this is works, but I would suggest almost any timing over that one.
Answer: No, they're not the same. Yes, if several divided arpeggio lines appear underneath each other, then the arpeggios are supposed to be played in parallel. (In practice, this is almost always one arpeggio for the left and one for the right hand, so you're never going to see more than two such lines.)


Average of values from an array with php

Question: How from this array I can make an average ?
$array=Array
(
    "Element_1" => 255,
    "Element_2" => 95,
    "Element_3" => 100
);

I should get 150.
Answer: You can sum the values in the array and then divide them by the count of elements in the array.
$sum = array_sum($array);
$count = count($array);

echo $avg = $sum / $count;

Answer: Try this
$average = array_sum($array) / count($array);

Answer: Use below code :

array_sum() : Returns the sum of values in an array
count() : Count all elements in an array, or something in an object

Code:
<?php
$array=Array
(
    "Element_1" => 255,
    "Element_2" => 95,
    "Element_3" => 100
);
echo $average = array_sum($array) / count($array);

Output:
150

Demo:  Click Here


If $x_n \in c_c$ then if $ \|x_n-x\|_{\infty} \to 0 \not \Rightarrow \|x_n-x\|_{1} \to 0$

Question: I just want to know if I am going on the right track. i was trying to do my functional analysis problem set which requires me to proof that the 1-norm and the $\infty$-norm are not equivalent on $c_c$(the space of all real/complex valued sequences which have at most finitely many non-zero element.)
Proof: I am trying to use the lemma that two norms are equivalent iff the topologies they generate are the same.
So if I can show that if a sequence $(x_n)_{n \in \mathbb{N}}$ converges to $x$( assumed to be in $c$(set of all sequences with $\lim_{n \to \infty} x_n \to 0) $as it is the closure of $c_c$(and hence Banach) then it does not necessarily converge in the $\infty$-norm
My question is whether the following argument is valid
$\|x_n-x\|_{\infty} \to 0 \Leftrightarrow \sup_{i \in \mathbb{N}}|{x_n}^i-x^i |\to 0 \implies |{x_n}^i-x^i | \to 0$ $ \forall  $ i
ie $\forall \epsilon >0$ $\exists N$ s.th $\forall n>N $ $|{x_n}^i-x^i | < \epsilon$
But now $\sum_{i \in \mathbb{N}} |{x_n}^i-x^i| \not \to 0$ because
$\sum_{i \in \mathbb{N}} |{x_n}^i-x^i|<\sum_{i \in \mathbb{N}} \epsilon$
but then however small $\epsilon$ is we can never be sure that the sum is arbitrarily small and therefore I can conclude that all the sequences  $(x_n)_{n \in \mathbb{N}} \in c_c$ do not always converge in the $\infty$-norm
Does this make sense?
Thank you
Comment: It makes sense, but it's not a valid proof that the implication doesn't hold in general. You've shown one possible argument does not work. There may be others that do. You need to somehow rule those out too (e.g. by giving a counterexample).
Answer: We show the implication does not hold in general by producing a counterexample. Let $x_n$ be an element with $n$ nonzero entires each equal to $1/n$. Then $$\|x_n\|_\infty = 1/n\rightarrow 0,$$
but $\|x_n\|_1=1$ for every $n$.
Comment: Thank you , it was very helpful. I will keep this in mind next time


Maximum Likelihood of single observation

Question: I'm stumped on this problem... I keep getting an undefined answer of having to solve -20 = 0. The likelihood function I get is $e^{-20\alpha}$.
So I have

$y_i=$ $ \begin{cases}
      1& w/probability \ p_i \\
      0& w/probability \ 1-p_i
   \end{cases}$
And $p_i=1-e^{-\alpha x_i}$, i=1,2,...
$\alpha>=0$   and $x_i>=0$

If I have a sample of a single observation $(y_1,x_1)=(0,20)$, I am looking for the maximum likelihood of this sample.
Thank you for any help :)
Answer: Hint:

You have a likelihood of $L(\alpha\mid y_1=0) = e^{-20\alpha}$ and you also know $\alpha\ge 0$
If $e^{-20\alpha}$, which is continuous and differentiable, does not have a turning point for positive $\alpha$, then it is monotonic and will have a maximum either when $\alpha=0$ or when $\alpha \to \infty$, and it is fairly obvious which
Comment: Ohhhh, thanks :)
Comment: Clearly it is 0. Can you confirm my likelihood is right though?
Comment: The likelihood looks right to me, as does $0$


How to navigate URL after moving drupal site from one server to other server?

Question: I have just moved my drupal 7 website from one server to other server.
But after moving it i am not bale to see any css effect and not able to open naviagte any link. I am using linux server.
Can somebody suggest how to do this.
Comment: Yes, the first thing to check is the value of `$base_url`, which just needs to be the URL of the site. URLs can contain a domain name (drupal.stackexchange.com) or an IP (127.0.0.1). Drupal documentation explain well how to set `$base_url`. For the rest, we cannot suggest much as debugging a site for which we don't have access is not possible.
Comment: And a URL starts with the protocol type (e.g. http://, https://, ftp://).
Comment: Comments have been deleted. Please don't use them for extended discussion. Add the relevant information in the question.
Comment: I have already provided same information but OP unable to understand. I have suggested same which you have mentioned in comments.
Answer: These kinds of problems (after transfer to a new host) might be caused by some type of permission problems. Some of the extra comments from the OPer in the question makes me believe that is the case here also.
So I'd verify that the userid of your webserver has appropriate file permissions.
Another option is to verify the various items mentioned in the community documentation about Migrating a site. One of the typical issues is (as mentioned in this link):

Attempt to log on to the new site. You will probably find that you are locked out by a combination of site maintenance mode and the non-clean URLs. In that case navigate to newsite/?q=user to login, then turn on clean URLs.


ModelForms in Django where the underlying model depends on another model (via OneToOneField)

Question: I have two models in my Django application, for the purposes of storing search parameters used for some homology search programs:
# models.py
class Search(models.Model):
    """A class to represent search runs."""

    program = models.CharField(max_length=20)
    results_file = models.FileField(
        upload_to=(SEARCH_RESULTS_DIR)
    )
    timestamp = models.DateTimeField()

    def __unicode__(self):
        return u'%s %s' % (self.program, self.timestamp)

class FastaRun(models.Model):

    search = models.OneToOneField('Search', primary_key=True)
    # the user-input FASTA formatted protein sequence
    query_seq = models.TextField()
    # -b "Number of sequence scores to be shown on output."
    number_sequences = models.PositiveIntegerField(blank=True)
    # -E "Limit the number of scores and alignments shown based on the
    # expected number of scores." Overrides the expectation value.
    highest_e_value = models.FloatField(default=10.0,
            blank=True)
    # -F "Limit the number of scores and alignments shown based on the
    # expected number of scores." Sets the highest E-value shown.
    lowest_e_value = models.FloatField(blank=True)
    mfoptions = [
            ('P250', 'PAM250'),
            ('P120', 'PAM120'),
            ('BL50', 'BLOSUM50'),
            ('BL62', 'BLOSUM62'),
            ('BL80', 'BLOSUM80')
    ]
    matrix_file = models.CharField(
            max_length=4,
            choices=mfoptions,
            default='BL50'
    )
    database_option = models.CharField(
            max_length=25,
            choices=BLAST_DBS,
            default=INITIAL_DB_CHOICE
    )
    ktupoptions = [(1, 1), (2, 2)]
    ktup = models.PositiveIntegerField(
            choices=ktupoptions,
            default=2,
            blank=True
    )

Note here that FastaRun is a kind of Search. FastaRun extends search in that there are more parameters defined for a FastaRun. A FastaRun must have a Search instance to which it is linked, and this Search instance is the primary key of FastaRun.
I have a ModelForm for the FastaRun class.
# views.py
class FastaForm(forms.ModelForm):

    class Meta:
        model = models.FastaRun

I have a view function which I need to use to populate a FastaForm and save a new Search instance and a new FastaRun instance based on the user submitted form. The form does not include the option to choose a Search instance. This is impossible to do because the Search instance can only exist once the user actually submits this search.
Below is an outline of what the function needs to do:
# also in views.py
def fasta(request, ...):
    # populate a FastaForm from the information POSTed by the user--but
    # how to do this when there's no Search information coming in from
    # the user's request. We need to create that Search instance, too,
    # but we also have to...

    # validate the FastaForm
    # ... before we can ...

    # create a Search instance and save() it

    # use this saved Search instance and give it to the FastaForm [how?]

    # save() the FastaForm [save the world]

    pass

Because the Search and FastaRun (and therefore FastaForm) are
intertwined, I feel like I'm getting into a Catch-22. I need to
save a Search instance, whose parameters are stored in the POST
request, but whose parameters must be validated using the FastaForm's
validation. However, I think the FastaForm can't be instantiated until
I have instantiated a Search instance. Yet, I can't instantiate a
Search instance until I've validate using the FastaForm... You get
the idea.
What am I missing here? There must be a fairly clean way to do this, but
I lack the clarity to see it.
Also, correct me if I'm wrong, but this same dependency situation could occur any time you have some sort of relationship between models (e.g., also for ForeignKey and ManyToMany fields). Thus, someone's surely figured this out.
Answer: This is a case where I would use inheritance to take care of this:
# models.py
class Search(models.Model):
    """A class to represent search runs."""
    ...

class FastaRun(Search):
    # one-to-one field has been removed
    ....

Now, instantiating a FastaRun is by definition also instantiating a Search. Django handles the database side of this properly, too, by setting up a separate table for FastaRun with a key into Search. Your validation should work as expected with the form. The only thing you might want to add if you're going to do any queries on the Search object would be to add a type field to Search that is the overridden by all subclasses, so you can filter out those results.
Comment: program is a CharField that identifies the program that was run to do the search (e.g., 'fasta35' or 'ssearch35' or 'blast'). The 'fasta35' and 'ssearch35' must be saved as FastaRun instances. This should suffice as a filter, correct? Is there a way to go from a Search instance to its corresponding FastaRun instance (we may assume it has one)?
Comment: Yeah, seems that would be a good enough filter, though it seems you'd only want to use it to exclude subclassed items. (For those, you'd want to use the subclass, e.g. FastaRun.objects.filter(...).)

If you do need to go from a Search object s to FastaRun (or any other subclass), you can always just do FastaRun.objects.get(id=s.id).


Number of $0$ in great number

Question: For example,  $11111111111111100$ ends with  $2$ zeros ,when we did know the decimal representation like $100!$ also.
I would like a justified  answer for the following question . How many $0$ are in the end of decimal representation of $ 100!$?
Is there  a general process to know this number for greater number like $2^{100!}\times5^{39!}$?
Comment: $2^{100!}$ does not end in zero because it is not divisible by $5$.
Comment: yes i just make a typo
Comment: $2^{100!}*5^{39!}$ isn't very different, there are less 5 than 2 in its factorization so it ends with $5^{39!}$ zeros
Answer: It is fairly easy with factorials, a number ends with as many $0$s as the number of $5*2$ in his factorization.
It is obvious that there are more 2 than 5 in the factorization of any factorial so we only need to count how many $5$s are there in the factorization of $100!$.
These are $\frac{100}{5}+\frac{100}{5^2}=24$ since $5^3>100$
Answer: A very useful result to know, which also helps here, is Legendre's Theorem :

The number $n!$ contains the prime factor $p$ exactly $$\sum_{k\geq 1} \lfloor n/p^k \rfloor$$
Answer: Of the 100 numbers multiplying to give 100! ten of them end in 0 (10, 20, ...100) giving 11 zeros. The only other way to get a 0 is to mutliply an even number by a number ending in 5. There are ten numbers ending in 5 (5, 15,...,95) and multiplying by any ten even numbers (except the multiples of 10) will give 10 more zeros. So that is 21 zeros on the end of 100!
Comment: This is not correct. See Alessandro's answer.
Comment: Yes, 50, 25 and 75 contribute two 5's which can be multiplied by even numbers to contribute 3 more zeros.
Comment: Also, $100!$ is "small" enough to calculate it and then simply count how many $0$s are there at the end of $93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000$

Number of $0$ in great number


Unable to load variables in Tensorflow

Question: I have a following code :
sess = tf.Session()
model_file = './runs/1488368349/checkpoints/model-42200.meta'
checkpoint_dir = './runs/1488368349/checkpoints/'

new_saver = tf.train.import_meta_graph(model_file)
#new_saver.restore(sess, MODEL_PATH)
new_saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir))

all_vars = tf.get_collection('vars')
print len(all_vars)
for v in all_vars:
        v_ = sess.run(v)
        print(v_)

But the above code is unable to load variables. I am getting the length of all_vars as 0.
Can someone tell me what is the problem?
Answer: Can you try this?
tf.get_collection(tf.GraphKeys.VARIABLES)

This should give you all trainable and untrainable variables...

Unable to load variables in Tensorflow


How to handle large numbers of connections from an IP address?

Question: Every couple of weeks we have an issue whereby a single IP address opens a huge number of of Apache processes at the same time.
It seems to connect to similar pages each time, but our Apache processes goes from the normal ~20-25 busy slots to our max (256).
This IP address then refreshes all the requests relentlessly, taking the load on our server way up and slowing down the site dramatically.
The solution in each instance is simple; I find out the IP from Apache "server status" and block the IP using an IP tables rule.
Unfortunately the IP address changes each time (it's always similar & when I lookup host says "bt openworld"), and I'm not available 24/7 to login and add an IP tables rule.
To be clear, we handle crawls from Google and other search ehgines without issue, I'm not sure whether what we are experiencing is malicious or not.
How can I limit the effect that this has?
Our setup is a single server handling both MySQL and Apache.
I have been advised that a load balancer is the only way to rate-limit the number of connections from single (but changing per attack) IP addresses.
Comment: possible duplicate of: http://serverfault.com/questions/168294/in-apache-is-there-a-way-to-limit-the-number-of-new-connections-per-second-hour
Answer: maybe something like this will help. it will block any hosts, that open more than 150 connections within 2 minutes (180 seconds):
<code>iptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 180 --hitcount 150 -j REJECT
</code>
you have to tune the script, so that normal users will never get blocked
Answer: Well you can use some iptables rules to limit the number of concurrent connections.
The other solution would be to optimize your application so that they are handled faster using less resources.


XmlPullParser issues in Android

Question: So on review of the code there is no exception thrown when the xml is read in...works fine.
public void ReadXML() throws XmlPullParserException, IOException
   {

      factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(false);
      xpp = factory.newPullParser();

      InputStream raw = getApplicationContext().getAssets().open("trial_info.xml");
      xpp.setInput(raw,null);
   }
}

The problem is in the doXML method where i'm trying to read the text b/w two tags...
eg.
<trial>
<title>The Book</title>
<author></author>
....
</trial>

The error that is generated is  18:19:41.654: W/System.err(329): org.xmlpull.v1.XmlPullParserException: name expected (position:START_TAG @59:57 in java.io.InputStreamReader@44f049c8)
I think the code is probably more complicated than it needs to be...but can't figure out the error. Thanks
public void doXML() throws  XmlPullParserException, IOException{

          int eventType = XmlPullParser.START_TAG;

          while(eventType != XmlPullParser.END_DOCUMENT)
          {
              if(eventType == XmlPullParser.TEXT && xpp.getText().equals(selectedTrial))
              {
                   trialTxt = (TextView)findViewById(R.id.TrialAbbrevData);
                   trialTxt.setText(xpp.getText());
                   Log.d("what is this", xpp.getText());

           while(!(eventType == XmlPullParser.END_TAG && xpp.getName().equals("...")))
                   {

           if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialName"))
                      {
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtDesc = (TextView)findViewById(R.id.TrialNameData);
                            trialTxtDesc.setText(xpp.getText());
                            Log.d("in loop",xpp.getText());
                         }
                      }

         if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialDetails"))
                      {
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtConc = (TextView)findViewById(R.id.TrialDescData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

         if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialResults"))
                      {
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtConc = (TextView)findViewById(R.id.TrialResultsData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

        if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("trialConclusion"))
                      {
                         eventType = xpp.next();
                         if(eventType == XmlPullParser.TEXT)
                         {
                            trialTxtConc=(TextView)findViewById(R.id.TrialConclusionData);
                            trialTxtConc.setText(xpp.getText());
                         }
                      }

        if(eventType == XmlPullParser.START_TAG && xpp.getName().equals("whatItMeans"))
                      {
                          eventType = xpp.next();
                          if(eventType == XmlPullParser.TEXT)
                          {
                             trialTxtMeans = (TextView)findViewById(R.id.TrialWIMData);
                             trialTxtMeans.setText(xpp.getText());
                          }
                      }

                      eventType = xpp.next();

                   }

            }
              eventType = xpp.next();
        }
    }

Comment: can you post line 37-42 of your XML file?
Comment: I think this can happen if you mistype or don't use the same letter case in your tags. Have you tried on a short test xml file first?
Answer: The error message seems to indicate an error in the XML file, not the code.

XmlPullParser issues in Android


How to write a view of a user signed up nodes?

Question: There is a content-type (event) with signup enabled. I want to show, on a user profile, the events this user has already signed up. I am having several frustrations with the signup relations and arguments. Is this possible with the current 2.x release?
Comment: is this using a contrib event signup module or some custom code?
Comment: As barraponto is referring to the "current 2.x release," I think he is not referring to a custom module. @barraponto Can you provide the link to the project page? Without to know to which module you are referring, users cannot answer correctly, or without making any guess.
Comment: I am referring to http://drupal.org/project/signup
Comment: That project doesn't seem to have a 2.x release, does it?
Comment: @kiamlaluno it seems it hasn't been released, i got it from git. any way, i reverted to 1.x
Answer: There is a default view called signup_current_signups that provides this functionality out of the box.


How to block all traffic between two interfaces?

Question: Let's say I have two interfaces on one host, eth0 and eth1.  The behavior I'm seeing currently is:
Receive traffic on eth0, it finds a suitable route on eth1 and tries to send it.
I'm already intercepting the packets coming in on eth0 so I would like to stop this forwarding and drop all packets coming in on eth0 instead of sending them on eth1.
In essence, I'd like to block all traffic between two interfaces on one host.  Is this possible with iptables or routing?
Answer: You should be able to control this at the kernel level with sysctl.  Setting the net.ipv4 and/or net.ipv6 forwarding values off.
net.ipv4.conf.all.forwarding = 0
net.ipv4.conf.eth0.forwarding = 0
net.ipv4.conf.eth1.forwarding = 0

Depending on the distribution you can configure this at startup by editing /etc/sysctl.conf or adding a file in /etc/sysctl.d.  Normally forwarding is disabled by default, so you may have a line enabling forwarding.
Some firewall builders will enable or disable forwarding depending on the configuration.
Comment: let's say, you want to block traffic from eth0 to eth2, but you want to allow traffic from eth0 to eth1. This approach doesn't work.
Answer: iptables -A FORWARD -i eth0 -o eth1 -j DROP should do what you're requesting.


How to make gmap marker tooltip display node title without tags?

Question: My project uses gmap of nodes locations. When marker is hovered  popup tooltip displays link to node with node title in it. Any possibility  to display node titles in marker tooltips without any tags?
Answer: Quote from the Comparison of mapping modules:

GMap is considered a legacy module.

So maybe you want to have a look at the IP Geolocation Views & Maps module. Partial quote from its project page:

Despite what the name may suggest, IP Geolocation Views & Maps (IPGV&M) is first and foremost a mapping engine.
The module makes it very easy to create Views-based mapping solutions using the map renderer of your choice (Google, OpenLayers or Leaflet) with any location storage module of your choice, e.g.:

Get Locations,
Geofield,
Geolocation Field
Location.

This allows you to bring together modules and feature sets that without IPGV&M you would not be able to combine.
Like beautiful animated marker clustering on Google or Bing or Mapbox ... any of various map providers.
But more importantly IPGV&M adds some features you won't find as conveniently packaged elsewhere.
These include marker differentiation based on field values or ranges (2nd screenshot) with or without font icons, various centering options, super-imposing the current visitor's location, allowing multiple content types to be displayed on the same map, full screen button and mini-map inset (bottom screenshot), polygon support (Geofield), marker tags and tool tips and cross-highlighting of associated content on the page when markers are hovered (and vice versa).

Head over to its (impressive) project page for even more information about it. That project page also includes various screenshots.


Transformation to linearize dataset

Question: How can I transform the following dataset to a more linear representation? R code:
x <- 1:20
y <- c(101, 84, 81, 80, 73, 69, 67, 63, 63, 62, 62, 61, 61, 60, 59, 58, 57, 57, 57, 55)
d <- data.frame(x, y)

ggplot(d, aes(x, y)) + geom_point()

Here is a log version, almost no change:
ggplot(d, aes(x, log(y))) + geom_point()

That is, I have several datasets that look somewhat exponential, like this example, but taking a simple log isn't good enough to produce the level of linearity I need. What can I do to get more linearity?
Comment: Why do you want to do this?
Answer: I would suggest using lm(log(x),log(y)) to look for power law relationships between the variables. The gradient it outputs in this case is very close to -5, suggesting we try plot(x,y^-5).

Transformation to linearize dataset


Prove or disprove $f(n)-g(n)=O(s(n)-r(n))$

Question: If $f(n)=O(s(n))$ and $g(n)=O(r(n))$ use the formal definition of the asymptotic notations. Can anyone help me with this with a proof or a counter example. I am lost on this one.
Comment: Think $s(n) = r(n)$. What then?
Comment: I defined it. just now thank you
Answer: $x\in O(x^2),x/2\in O(x^2)$ but $x-x/2=x/2\notin O(x^2-x^2)$.


hybris search and filters in product cockpit

Question: In the product cockpit of the backoffice there is a search feature to which you can add some filters , I would like to add some of my own attributes to the filters.
In the my custom backoffice extension in the config.xml i added
<code><context type="MyProduct" component="pcmbackoffice-fulltext-search" module="pcmbackoffice">
    <ful:fulltext-search xmlns:ful="http://www.hybris.com/cockpitng/config/fulltextsearch" xmlns:y="http://www.hybris.com/cockpit/config/hybris">
        <ful:field-list>
            <ful:field name="MyProduct.Name"/>
            <ful:field name="MyProduct.Type"/>
            <ful:field name="MyProduct.isActive"/>
        </ful:field-list>
        <ful:preferred-search-strategy>solr</ful:preferred-search-strategy>
        <ful:operator>OR</ful:operator>
    </ful:fulltext-search>
</context>
</code>
i then ran a clean all and an update from the hac , but the changes are not shown. in the console i see
<code>[DefaultFieldQueryFieldRenderer] Unable to determine field configuration for
field 'MyProduct.Name' in 'pcmbackoffice-collectionBrowser-
fulltextsearch'
</code>
I have also tried the config below.
<code> <context type="MyProduct" component="pcmbackoffice-fulltext-search" module="pcmbackoffice">
    <ful:fulltext-search xmlns:ful="http://www.hybris.com/cockpitng/config/fulltextsearch" xmlns:y="http://www.hybris.com/cockpit/config/hybris">
        <ful:field-list>
            <ful:field name="MyProduct.Name"/>
            <ful:field name="MyProduct.Type"/>
            <ful:field name="MyProduct.isActive"/>
        </ful:field-list>
        <ful:preferred-search-strategy>solr</ful:preferred-search-strategy>
        <ful:operator>OR</ful:operator>
    </ful:fulltext-search>
</context>
</code>
which does not change the default settings (product).
any help on how to configure the search and filters in the product cockpit would be great
Answer: You can follow step which is described in document.
You need to configure your variant for backoffice search solr indexing at System > Search and Navigation > Indexed Types.
Comment: Hi thanks , i looked at the like and was able to get the filters present. But I can't make this persistent if I initialize the configuration is gone.
Comment: Some impex may be remove existing before importing news one. You can search all impex files for that item type.


Using Maple to work out error terms

Question: I have the exponential matrix using a Jordan form matrix and transition matrix:
$E(t) = e^{At}= \left[
 \begin{array}{ccc}
   e^{3t}+3te^{2t} & 3e^{3t}-3e^{2t}+6te^{2t} & 6e^{3t}-6e^{2t}+9te^{2t}  \\
   -2e^{3t}+2e^{2t}-3te^{2t} & -6e^{3t}+7e^{2t}-6te^{2t} & -12e^{3t}+12e^{2t}-9te^{2t} \\
  e^{3t}-e^{2t}+te^{2t} & 3e^{3t}-3e^{2t}+2te^{2t} & 6e^{3t}-5e^{2t}+3te^{2t} \\
  \end{array}
\right]$
where $A =  \left[
 \begin{array}{ccc}
   6 & 9 & 15\\
   -5 & -10 & -21\\
   2 & 5 & 11 \\
  \end{array}
\right]$ and $t$ is an input value.
Question:
Approximate $E(2.5)$ using the Taylor expansion of the exponential up to the order $k$. Give the results $E_k$ for $k = 5,10,20,50,100$ and compute the error terms $e_k = \left|\left|E_k-e^{2.5A}\right|\right|$ in each case. Note here $\left|\left|M\right|\right|$ means the greatest absolute value of the entries of a matrix $M$.
I have easily (using maple) computed the approximation for each order using the sum:
$\sum\limits_{k=0}^{\infty}\frac{1}{k!}(At)^k$
but how do i work out the error terms $e_k$? what Maple command do i use to get $\left|\left|M\right|\right|$
Comment: The `abs` command automatically maps over a Matrix. So `max(abs(M))` should return the greatest absolute value from the entries of Matrix `M`.
Comment: By the way, $e^{A t}$ can be computed using MatrixExponential in the LinearAlgebra package.
Answer: I don't think there is a built-in function for that. There is VectorNorm(v,infinity), but you would first have to flatten the matrix into a vector in order to use that.
Given that the matrices are 3 by 3, it is easier to write a loop than to search for clever tricks:
with(LinearAlgebra):
A:=<<6 | 9 | 15>, <-5 | -10 | -21>, <2 | 5 | 11>>;
max(seq(seq(abs(A[i,j]),i=1..3),j=1..3));

Output: 21.

Using Maple to work out error terms


Can we calculate the probability that a null hypothesis is true, in general?

Question: I have a very simple question for Hypothesis testing. Is there a way to calculate the probability that the null hypothesis is false?
Since in the literature they're always talking about the significance level and the power of a test, these values are calculated assuming something about the null hypothesis (being true or false in each case).
But, in general terms, can we calculate the probability that a given null hypothesis is false, or true?
Comment: Well, maybe if you're Bayesian ... but people may not agree with your priors, on which such calculations would depend.
Comment: See http://stats.stackexchange.com/questions/166323/misunderstanding-a-p-value/166327#166327
Comment: You can also check threads tagged [tag:tost]
Answer: The term "null hypothesis" is usually used in a frequentist setting, where characteristics of the population, such as its mean, are regarded as fixed, not random. There, it makes no sense to talk about the probability of the null hypothesis.
In a Bayesian setting, these characteristics are regarded as random and we can talk about things like the probability of a population mean equalling 0. However, a typical Bayesian would give a prior probability of 0 to many common frequentist null hypotheses, such as the hypothesis that the mean of a normal distribution exactly equals a prespecified value.
Comment: So i can never measure the probability that a Null Hypothesis stated by someone is false right? All i can do (from the frequentist point of view) is Reject Ho if my data is telling me that.
Can you give me a recomendation for an introductory Book of Bayesian Inference? Thanks a lot!
Comment: Yes and yes. Try John K. Kruschke's *Doing Bayesian Data Analysis* for a gentle introduction or Gelman et al.'s *Bayesian Data Analysis* for a deeper book that assumes some familiarity with mathematical statistics.


Microsoft Access find field in table with known value

Question: We have a fairly large Oracle database that we are able to connect via Microsoft Access and ODBC with read-only access. We work with a front-end system that does not match the structure behind the scenes and often I need to query the system via Microsoft Access. The problem is that we are not provided any documentation as to structure and the structure needs serious attention. Searching for the field that I need is very time consuming.
With our front end, I'm able to view the values that I want to query, and I know the key fields, but I need to find the field that contains the known value.
If I have a record where I know the value of field "A", and have the value of field "X", is it possible to query field "X"?
Front end shows
Student ID: 12345678
Payments: 23456

Back end
TechID: 12345678
???: 23456

Can I query "???"
Answer: You can do this by iterating over the collection of tables, and for each table, the collection of fields.
Open Database
Get all Tables
For Each Table
   Get all Fields
   For Each Field
       If Field type is text ... and
       If Field size is not TOO Long ...
           Search for string
           If found, write to a results bucket
   Next
Next

Here is an example of code for cataloging tables (source here)
Public Function GenerateDataDictionary(aDataDictionaryTable As String)
'***       Usage: GenerateDataDictionary("MyDataDictionaryTable")
'*** Extracts the information about the tables for the data dictionary
'*** and inserts it to a table named aDataDictionaryTable

    Dim tdf As TableDef, fldCur As Field, colTdf As TableDefs
    Dim rstDatadict As Recordset
    Dim i As Integer, j As Integer, k As Integer
    Set rstDatadict = CurrentDb.OpenRecordset(aDataDictionaryTable)
    Set colTdf = CurrentDb.TableDefs

    'Go through the database and get a tablename
    For Each tdf In CurrentDb.TableDefs
    'Do what you want with the table names here.
    rstDatadict.AddNew
    rstDatadict.Update
rstDatadict.AddNew
    rstDatadict![Table] = tdf.NAME
    rstDatadict![Field] = "----------------------------"
    rstDatadict![Display] = "----------------------------"
    rstDatadict![Type] = ""
    rstDatadict.Update
    rstDatadict.AddNew
    rstDatadict![Table] = "Table Description:"
    For j = 0 To tdf.Properties.Count - 1
          If tdf.Properties(j).NAME = "Description" Then
              rstDatadict![Field] = tdf.Properties(j).Value
          End If
    Next j

    rstDatadict.Update
    rstDatadict.AddNew
    rstDatadict.Update

For i = 0 To tdf.Fields.Count - 1
          Set fldCur = tdf.Fields(i)
          rstDatadict.AddNew
          rstDatadict![Table] = tdf.NAME
          rstDatadict![Field] = fldCur.NAME
          rstDatadict![Size] = fldCur.Size

          Select Case fldCur.Type
            Case 1
              FieldDataType = "Yes/No"
            Case 4
              FieldDataType = "Number"
            Case 8
              FieldDataType = "Date"
            Case 10
              FieldDataType = "String"
            Case 11
              FieldDataType = "OLE Object"
            Case 12
              FieldDataType = "Memo"
            Case Else    ' Other values.
              FieldDataType = fldCur.Type
          End Select

          rstDatadict![Type] = FieldDataType
                For j = 0 To tdf.Fields(i).Properties.Count - 1
                    If fldCur.Properties(j).NAME = "Description" Then
                        rstDatadict![DESCRIPTION] = fldCur.Properties(j).Value
                    End If

                    If fldCur.Properties(j).NAME = "Caption" Then
                        rstDatadict![Display] = fldCur.Properties(j).Value
                    End If

                    If fldCur.Properties(j).NAME = "Rowsource" Then
                        rstDatadict![LookupSQL] = fldCur.Properties(j).Value
                    End If
                Next j

            rstDatadict.Update

    Next i
    Debug.Print "  " & tdf.NAME
    Next tdf

End Function

You can catalog your findings in Access by making a table of field-names which joins to a table of table-names.  Then your searches are based on the catalog instead of raw collections.
I reverse-engineered the schema for MAS 90 (with JobOps add-in) this way.  There's no map, but I had a read-only ODBC connection which I used in precisely the way you propose.  The purchasing accountant would give me a distinctive Product Number and I'd run it through this comprehensive engine.  Over time I succeeded in distilling 700 tables comprising 18k fields down to 20 tables and a few hundred fields.  That allowed us to export our data.
Comment: Thanks, S.  I plan to play with this later.  I like `dbAppendOnly` with `OpenRecordset` here.  That option seems to speed up row additions.  Thanks again.
Comment: I tracked down the new link and it should work now.  Congratulations we aren't blind.
Answer: The answer to your question is simple.  No, you cannot do that.
There are two solutions that I can think of.  The first is to manually concatenate all the values together and then look for the row that contains the value.  This is imperfect, but might work:
select *
from (select t.*, ('|'""col1||'|'||col2+'|' . . .||'|') as allcols
      from t
     ) t
where instr('|23456|', allcols) > 0

This would find any row that has that value in a column.  Probably close enough for what you want.
The second is to use UNPIVOT to do essentially the same thing.
I would strongly suggest that you invest a little bit of time to find the mapping between the fields, and then create a view in Oracle that has the field names as seen in the application.  It sounds like this would save you a lot of effort in the medium term.


NSWindows' setFrame:display:animate: doesn't work, setFrame:display: does

Question: I try to call setFrame:display:animate: on my NSWindow and doesn't do anything, but if I call setFrame:display: on the same window, it works.
Do I have to do something else to enable resizing with animation?
PS: In C++, if I want to mention a specific class' method I write, for example: NSWindow::setFrame() (not only in code but to refer to it in posts) is there such a convention in Objective-C? [NSWindow setFrame:display:animate:] ?
Answer: Well, you could just using Core Animation, as in [[NSWindow animator] setFrame: display:];
Comment: Great! still don't know why a documented method does nothing, but this one worked fine. thanks


Consumer Predicted Probability Function

Question: I have the following data for the last year for several thousand clients:

Client ID
Last Interaction Date with Business
Last Buy Date
No of items bought
Total value spend in $

I want to create a function that can predict the probability that a client will buy again. In other words; I want to get a function, where I can enter a client's last interaction date, last bid date, number of items bought and total value spend and get the probability that they will buy again.

What should be the first step? (Distribution? CDF?)
Which model would be the best one to use?
Do I need more data?
Can I use Excel or R?
Answer:
The first step would be to add to your rows $(X_1,X_2,X_3,X_4,X_5)$ the information $Y$ whether there the client with ID $X_1$ came also later than on interaction date $X_2$. This means an inspection of the table.
Personally, I would use the decomposition of the model in the following way:
$$
f(Y|X_1,X_2,X_3,X_4,X_5)=f(Y|X_2,X_3,X_4,X_5)f(X_2,X_3,X_4,X_5|X_1)
$$
where $f(Y|X_2,X_3,X_4,X_5)$ would be a model of new purchase for general atributes and $f(X_2,X_3,X_4,X_5|X_1)$ would be a model how the general atributes are distributed for the specific customer ID.
To construct $f(Y|X_2,X_3,X_4,X_5)$, you can use logistic regression, for instance, eventually any other classification technique. For logistic regression in R, see this.
For the construction of $f(X_2,X_3,X_4,X_5|X_1)$, i.e. typical behavior of the specific customer, I would use Gaussian mixtures that can capture wide class of distribution (for Gaussian mixtures R, see this). Practically, the data for specific ID would be queried. Having that model, the prediction that the buyer will come again is like this
$$
f(Y|X_1) = \iiint f(Y|X_2,X_3,X_4,X_5)f(X_2,X_3,X_4,X_5|X_1)dX_2dX_3dX_4dX_5
$$
the integration could be approximated by some Monte-Carlo method, i.e. sampling from $f(X_2,X_3,X_4,X_5|X_1)$ for given $X_1$ samples $(X_2^{k},X_3^{k},X_4^{k},X_5^{k})$ where $k=1,\dots,K$ and then summing
$$
f(Y|X_1) = \frac{1}{K}\sum_{k=1}^K f(Y|X_2^{k},X_3^{k},X_4^{k}, X_5^{k})
$$
Practically, this would mean substitution of the samples into the logistic model and calculation average for given customer ID. This is the number you are looking for.
Instead of constructing $f(X_2,X_3,X_4,X_5|X_1)$ and sampling, you can use simply the measurements you have for the given customer ID and substitute them into the sum above. This approach will be more reasonable when you do not have too many data for given customer ID.

Consumer Predicted Probability Function


Find out the $\angle PRQ$

Question: please, help me to solve this.How can I proceed.I just need help.
$PQR$ is a triangle. $M$ is a point on $QR$.here,$QM=1/3RM$ , $\angle RPM=30^ \circ$ and $ \angle QPM=20^ \circ$ now,$ \angle PRQ=??$
Comment: What you need is m-n theorem
Answer:
m-n theorem states that $(m+n)\cot\theta=m\cot\alpha-n\cot\beta$

Find out the $\angle PRQ$


Why do you need a separate criterion (Sequential model-based global optimization) in hyperparameter tuning?

Question: In the paper 'Algorithms for Hyper-Parameter Optimization' (pdf), where they explain the 'Sequential Model-based Global optimization method (SMBO)', the authors made a comment that,

SMBO algorithms differ in what criterion they use to obtain the next candidate of hyper-parameters given a model (or surrogate of the expensive model $f$), and in model $f$ via observation history.

I unable to wrap my head around the need of criterion why not just use the best set of hyper-parameter that gives the best result for the surrogate of $f$. Elaborated, as author uses Gaussian processes as a surrogate - why you require criterion instead of querying for all candidate which is likely to improve just like in Gaussian regression.
Note: Here, SMBO refers to a method where hyper-parameters are being tuned for a given model $f$ when training operation is very expensive. In SMBO, a surrogate model of $f$ is used as it would be cheaper to train and thereby increase the hyper-parameter tuning operation.
Answer:
I unable to wrap my head around the need of criterion why not just use the best set of hyper-parameter that gives the best result for the surrogate of $f$.

In GP surrogate models, the minimum of the surrogate is occurs at the argmin of the points queried so far, so choosing that point doesn't help at all -- you've already tested that hyper-parameter tuple, but when using SMBO, you're looking for promising points to try at the next iteration.

Elaborated, as author uses Gaussian processes as a surrogate - why you require criterion instead of querying for all candidate which is likely to improve just like in Gaussian regression.

The phrase "likely to improve" begs the question. Since the purpose of SMBO is to identify and test promising points, you'd need some way to compare the relative merits of two alternative points, which is what these criterions measure, using the previous points visited and the surrogate model to inform that measurement.


Xamarin.Forms.Shell: how to get the bottom TabBar height?

Question: I work on a Xamarin.Forms.Shell app using the default bottom TabBar, and I need to know the TabBar height to adjust some items.
I've found a way to get the StatusBar height on both platforms there, but I didn't found a solution for the TabBar.
Is it possible? I only found requests about changing the TabBar height on Stack...
Comment: Does it work now ?
Answer: We could  get the height by using Custom Renderer
in AppShell
public partial class AppShell : Xamarin.Forms.Shell
{
   public static double TabHeight { get; set; }

  //...
}

in iOS project
Option 1:

using App33;
using App33.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace App33.iOS
{
    public class ShellCustomRenderer : ShellRenderer
    {
        protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
        {
            return new TabBarAppearance();
        }

    }

    public class TabBarAppearance : IShellTabBarAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(UITabBarController controller)
        {

        }

        public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
        {
            base.ViewWillAppear(animated);

            UITabBar myTabBar = this.TabBarController.TabBar;

            myTabBar.TintColor = UIColor.Red;
            myTabBar.BarTintColor = UIColor.LightGray;
            //... you can set style like above in iOS

            AppShell.TabHeight  = myTabBar.Bounds.Height;

        }

        public void UpdateLayout(UITabBarController controller)
        {

        }
    }
}

Option 2:
Actually, The height of UITabbar on iOS is a fixed value . On iPhone 8 and before is 49 and iPhone X and after(full-screen) will have an extra safeArea bottom height . So if you just want to get it (don't need to set the height) , you could get it directly by using DependencyService  like following
AppShell.TabHeight = 49 + UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom;

in Android
 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
    //        var hei=SupportActionBar.Height;
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            int resourceId =Resources.GetIdentifier("design_bottom_navigation_height", "dimen", this.PackageName);
            int height = 0;
            if (resourceId > 0)
            {
                height = Resources.GetDimensionPixelSize(resourceId);
                AppShell.TabHeight = height;
            }

        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Comment: Hi @Lucas. It seems to work on **iOS**, but I lose the style specified in **Shell.xaml** for the `TabBar`'s text color. On **Android**, the height is not recovered until I change tab at least one time. As I need this information on the HomePage, I can't use this solution. In add, I also lose the default style.
Comment: You could re-set the style after get the height .
Answer: As far as i know, in Xamarin.Forms it is not (yet?) possible to retrieve the TabBar Height, so you might have to go and collect that information per Platform. Then, making use of DependencyService you would be able to make that information available from your Xamarin.Forms shared code. So, lets take a look at how to do it:
iOS
For iOS, there have already been published answers like this to explain how this can be done.
Android
For Android you can retrieve the TabBar height as follows

Create Interface for Dependency

Note: This interface should also be used on the iOS part ;)
namespace Tabbarheight
{
    public interface IDisplayHeights
    {
        float GetTabBarHeight();
    }
}

Create a class in Android that implements that interface and returns the desired value

(thanks to @LucasZhang-MSFT for the identifier string!)
using Android.App;
using Tabbarheight.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(AndroidDisplayHeights))]
namespace Tabbarheight.Droid
{
    class AndroidDisplayHeights : IDisplayHeights
    {
        public static Activity Activity { get; set; }

        public float GetTabBarHeight()
        {
            int resourceId = Activity.Resources.GetIdentifier("design_bottom_navigation_height", "dimen", Activity.PackageName);
            int height = 0;
            if (resourceId > 0)
            {
                height = (int)(Activity.Resources.GetDimensionPixelSize(resourceId) / Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density);
            }

            return height;

        }
    }
}

where Activity is set from MainActivity.cs as follows
protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    AndroidDisplayHeights.Activity = this;

    LoadApplication(new App());
}

And finally, you can consume this as follows

public AboutPage()
{
    InitializeComponent();

    SizeChanged += (s, a) =>
    {
        label.HeightRequest = DependencyService.Get<IDisplayHeights>().GetTabBarHeight();
        label.BackgroundColor = Color.Red;

    };

}

In the solution above i defined a Label (label) simply to try to demonstrate the accuracy of the gotten TabBar's Height value by setting the Label's height.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Tabbarheight.Views.AboutPage"
             xmlns:vm="clr-namespace:Tabbarheight.ViewModels"
             Title="{Binding Title}">

    <StackLayout>

                <x:String>
                    Hola mundo
                </x:String>

    </StackLayout>

</ContentPage>

The label looks like this on my side:


Cartesian product using family of sets

Question: I have been reading both "Naive set theory - Halmos" and "Joy of sets - Delvin". I couldn't really get what the family of sets mean. $I$ set that they frequently use is I guess a subset of $N$. Other than that I don't seem to understand anything at all.
The cartesian product defined as
$$\prod X_i = \{f | (f: I \rightarrow \bigcup X_i) \wedge (\forall i \in I)(f(i) \in x_i)\}$$
I totally don't get what's happening here. $$\{1,2\} \times \{2,3\} = \{(1,2),(1,3),(2,2),(2,3)\}$$ Can somebody please enlighten me what's that definition is with that example?
EDIT: Is he trying to define all the functions where $$\{\{1 \rightarrow 1,2 \rightarrow 2\},\{1 \rightarrow 1, 2 \rightarrow 3\},\{1 \rightarrow 2,2 \rightarrow 2\} \{1 \rightarrow 2, 2 \rightarrow 3\}\}$$
Where $I = \{1,2\}$
Comment: Think of a Cartesian product as all the ways of picking I elements such that the i'th element belongs to the set Xi.
This is exactly the definition above only in formal terms of functions.
Comment: Is it like i shown in edit? If so what this family of sets really mean. Wikipedia explains it differently, these two books define them differently. What is this family really mean?
Comment: Exactly!
In the context of set theory, it's often easier to think of functions as a set of (single valued) ordered pairs.
Comment: So this family is basically a notion to iterate over the sets of sets?
Answer: If there are e.g. $2$ sets $X,Y$ then the Cartesian product $X\times Y$
can be looked at as a set of ordered pairs: $\left\{ \left\langle x,y\right\rangle \mid x\in X\wedge y\in Y\right\} $.
But what if you want a notion of Cartesian product concerning a whole
bunch of sets? Let's say we have the sets $X_{i}$ where $i$ ranges
over indexset $I$. Then functions take the place of the ordered pairs.
Elements of $\prod_{i\in I}X_{i}$ are functions $f$ having $I$
as domain and with $f\left(i\right)\in X_{i}$ for each $i\in I$.
We want to give these functions a common codomain. Note that for every
$i\in I$ the set $X_{i}$ must be a subset of the codomain of these
functions leading to the set $\bigcup_{i\in I}X_{i}$ as codomain.
This together gives the definition that you mention in your question.
In the example that you mention: $\left\{ 1,2\right\} \times\left\{ 2,3\right\} =\left\{ \left\langle 1,2\right\rangle ,\left\langle 1,3\right\rangle ,\left\langle 2,2\right\rangle ,\left\langle 2,3\right\rangle \right\} $
we need an indexset $I$ that has exactly $2$ elements and we can
do it with $I=\left\{ 1,2\right\} $ as you propose.
Then e.g. element
$\left\langle 1,3\right\rangle $ corresponds with function $f:\left\{ 1,2\right\} \rightarrow\left\{ 1,2,3\right\} $
that is prescribed by $1\mapsto1$ and $2\mapsto3$.
Comment: Just one more question. As i mentioned in the other comment. Does family is really a notion of a group of sets?
Comment: For a formal definition of a family of sets $(A_i)_{i\in I}$ you could take a look [here](http://katmat.math.uni-bremen.de/acc/acc.pdf) on page 14 (at the bottom)
Comment: @drhab, Sir in the last example that you gave you said that $\langle 1,3 \rangle$ corresponds to the function $f:\{1,2\}\rightarrow \{1,2,3\}$ but sir I want you to please explain how did the ordered pair form appear merely with this function?

Cartesian product using family of sets


Only allow requests containing certain get variables through nginx reverse proxy

Question: I have an nginx reverse proxy which is going to handle callbacks from the API of another server. These callbacks go to https://callbackserver.com/?wc_api=App&secret=SECRET but I want any other requests without the wc_api and secret to be rejected.
Here is my simple nginx reverse proxy config...
server {
        listen 443 ssl;
        server_name redacted.domain;
        ssl_certificate /etc/letsencrypt/live/redacted.domain/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/redacted.domain/privkey.pem;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;
        ssl_prefer_server_ciphers on;
        ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_dhparam /etc/ssl/certs/dhparam.pem;
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";

        location / {
                 proxy_pass_header Authorization;
                 proxy_pass http://127.0.0.1:8000;
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_http_version 1.1;
                 proxy_set_header Connection "";
                 proxy_buffering off;
                 client_max_body_size 0;
                 proxy_read_timeout 36000s;
                 proxy_redirect off;
        }
}

server {
        server_name redacted.domain;
        listen 80;
        return 301 https://redacted.domain$request_uri;
}

Please tell me how I can make this reject things per my description above.
Answer: You could test the $args against a regular expression
location / {
    if ( $args !~* "^wc_api=.+&secret=[^&;]" ) {
        return 403;
    }
    proxy_pass_header ...


Significant digits

Question: We use currency conversion rates for financial calculations. Our currency conversion table stores conversion rates to and from each currency (about 150 world currencies) for each day, going back 20 years. The table has millions of rows. Each conversion rate is stored with 4 decimal places (e.g. USD to EUR $1.5105$).
I was wondering if it's possible to only store rates from other currencies to USD with increased precision and then divide rates for other currencies. For example, if the rate for USD to EUR is $1.5105$ and the rate for USD to Pound is $2.2324$, then the rate  from Pound to Euro would be $1.5101 / 2.2324$.
If I want the result of division to have 4 significant decimal places, unaffected by rounding, how many decimal places should dividend and divisor have?
Thanks.
Comment: No matter how many decimal places you store, there'll always be a possibility that you'll get a different result in the fourth decimal place from the one that you currently store.  How important is it that all four decimal places are correct?  Also, what do you do about "tiny" currencies such as Indonesian Rupiahs - four decimal places of IDR is roughly equivalent to eight decimal places of USD?  Do you really need to be able to show four decimal places for currencies like this?  If you can answer these questions, I may be able to produce a coherent answer for you.
Answer: First, we must understand the nature of inaccuracy. As for exchange rates, there are random error caused by fluctuations at the market, and measurement error caused by finite number of digits after point in values. Random error is much greater than measurement at almost all liquid financial markets.
Considering random error we can notice, that it has relative nature, i.e. absolute error is  is proportional to the value:
$$\varepsilon_x = \frac{\Delta x}{x}$$
Mean relative error of product of statistically independent values is a sum of relative errors of multipliers:
$$ \varepsilon_x = \sqrt{\sum \varepsilon_{x_i}^2} \qquad for\quad x = \prod x_i$$
Particularly, in your case relative variances are summed: both measurement error and random, and random is much more than measurement. This is answer to your question. But notice, quotes are often statistically dependend, this way you get biased (usually overestimated) value of relative random error.
In this case is more appropriate to consider relative variances, or stochastic volatility. There are different numerical methods to estimate volatilities.
Another approach is to operate in logarithm values $y_i=\ln x_i$
and calculate their volatilities. Using logarithms you can rewrite you product of quotes as a sum, and their absolute volatilities can be summed. Moreover for for small volatilities:
$$\varepsilon_{x_i} \approx \Delta y_i$$
All above is applicable to shares or currencies (which actually have lognormal distribution), but not to interest rates or discount factors. Also one should keep in mind, that almost all quotes are dependend each other, and it is more proper to use covariance matrices.
Comment: Thank you for such an elaborate answer. My math skills are pretty basic, so I won't pretend I understood all the facets of your explanations. I'd appreciate it if you could state a solution in some lament terms. It appears that the logarithm option is a feasible one, but it'd be great if you could provide an explanation on how to apply it to the USD/Pound example listed in my original post. Thank you.
Comment: As for systematic error you can estimate it as $$\varepsilon = \sqrt{\varepsilon_1^2 + \varepsilon_2^2}$$. If you use floating point type to store quotes, use the same type for product.
Answer: "Significant places" is often shorthand for an approximate error bound.  The worst case is if you want to represent $9999$ within $1$, which is a fractional error of $10^{-4}$.  You are then asking what fractional error is allowable on $a$ and $b$ if you want the fractional error on $\frac ab$ to be less than $10^{-4}$.  Since the numbers are small, and we want fractional errors, it is useful to take the logarithm:  $\log \frac ab=\log a - \log b$, then take the derivative $\frac {\Delta \frac ab}{\frac ab}=\frac {\Delta a}a-\frac {\Delta b}b$.  Roughly, this shows the fractional error in $\frac ab$ might be as high as twice the fractional error in $a$ and $b$ (if they are in opposite directions).  If you are storing in decimal and only store five places, you could have still an error of one part in $10^4$ if the number you store is $10000$, but storing six places is safe.

Significant digits


Why are neural networks smooth functions?

Question: I am reading Chapter 11 of Elements of Statistical learning and came across this sentence:
"Unlike methods like CART and MARS, neural networks are smooth
functions of real-valued parameters"
What is meant by 'smooth functions' here? I have come across things such as smoothing splines, but am unsure what a 'smooth function' means more generally.
Following on from the above, what makes neural networks specifically smooth functions?
Answer: A smooth function has continuous derivatives, up to some specified order. At the very least, this implies that the function is continuously differentiable (i.e. the first derivative exists everywhere and is continuous). More specifically, a function is $C^k$ smooth if the 1st through $k$th order derivatives exist everywhere, and are continuous.
Neural nets can be written as compositions of elementary functions (typically affine transformations and nonlinear activation functions, but there are other possibilities). For example, in feedforward networks, each layer implements a function whose output is passed as input to the next layer. Historically, neural nets have tended to be smooth, because the elementary functions used to construct them were themselves smooth. In particular, nonlinear activation functions were typically chosen to be smooth sigmoidal functions like $\tanh$ or the logistic sigmoid function.
However, the quote is not generally true. Modern neural nets often use piecewise linear activation functions like the rectified linear (ReLU) activation function and its variants. Although this function is continuous, it's  not smooth because the derivative doesn't exist at zero. Therefore, neural nets using these activation functions are not smooth either.
In fact, the quote isn't generally true, even historically. The McCulloch-Pitts model was the first artificial neural net. It was composed of thresholded linear units, which output binary values. This is equivalent to using a step function as the activation function. This function isn't even continuous, let alone smooth.
Comment: They indeed do mean smooth in the parameters, although this barely changes your discussion here: "_Unlike methods like CART and MARS, neural networks are smooth functions of real-valued parameters. This facilitates the development of Bayesian inference for these models._" And in the book, they only mention sigmoid or Gaussian RBF activations. (The most recent second edition was written in 2008,  a few years before ReLUs became popular.)
Comment: Note: I have interpreted the quote as referring to smoothness w.r.t. the input. However, it's possible they're referring to smoothness w.r.t. the parameters. I'll try to edit later to address this possibility
Comment: ReLU neural nets with integer weights are tropical rational maps, i.e. piecewise linear. cf https://arxiv.org/pdf/1805.07091.pdf
Answer: They refer to the smoothness, as understood in mathematics, so a function that is continuous and differentiable. As explained by Nick S on math.stackexchange.com:

A function being smooth is actually a stronger case than a function
being continuous. For a function to be continuous, the epsilon delta
definition of continuity simply needs to hold, so there are no breaks
or holes in the function (in the 2-d case). For a function to be
smooth, it has to have continuous derivatives up to a certain order,
say k.

Some of the answers at math.stackexchange.com mention infinite differentiability, but in machine learning the term would be rather used in looser sense of not-necessary-infinite differentiability, since we rather wouldn't need infinite differentiability for anything.
This can be illustrated using the figure used on scikit-learn site (below), showing decision boundaries of different classifiers. If you look at decision tree, random forest, or AdaBoost, the decision boundaries are overlayed rectangles, with sharp, rapidly changing boundaries. For neural network, the boundary is smooth both in mathematical sense and in common, everyday sense, where we say that something is smooth, i.e. something rather roundish, without sharp edges. Those are decision boundaries of classifiers, but regression analogs of those algorithms work almost the same.

Decision tree is an algorithm that outputs a number of, automatically generated, if ... else ... statements that lead to final nodes where it makes the final prediction, e.g. if age > 25 and gender = male and nationality = German then height = 172 cm. By design, this would produce predictions that are characterized by "jumps", because one node would predict height = 172 cm while other height = 167 cm and there might be nothing in-between.
MARS regression is build in terms of piecewise linear units with "breaks", so the regression equation when using single feature $x$, and two breaks, could be something like below
$$
y = b + w_1 \max(0, x - a_1) + w_2 \max(0, x - a_2)
$$
notice that the $\max$ function is an element that is continuous, but not differentiable (it is even used as an example in Wikipedia), so the output would not be smooth.
Neural networks are build in terms of layers, where each layer is build from neurons like
$$
h(x) = \sigma(wx + b)
$$
so when the neurons are smooth, the output would be smooth as well. Notice however that if you used neural network with one hidden layer using two neurons, $\operatorname{ReLU}(x) = \max(0, x)$ activation on hidden layer, and linear activation on output layer, then the network could be something like
$$
\newcommand{\relu}{\operatorname{ReLU}}
y = b + w^{(2)}_1 \relu(w^{(1)}_1 x + a_1) + w^{(2)}_2 \relu(w^{(1)}_2 x + a_2)
$$
that is almost the same model as MARS, so isn't smooth as well... There are also other examples where modern neural networks architectures do not need to lead to smooth solutions, so the statement is not generally true.
Comment: when nobody used ReLU your last equation was smooth. hence the quote
Answer: When the book was written nobody was using relu . It's not even mentioned in the book. All activations were smooth sigmoids. In this case neural net output is indeed a smooth function of its parameters such as weights and biases. That's how you make backpropagation work nicely but slowly. Once relu came to the picture derivatives calculations became much faster, because it became piecewise linear instead of smooth nonlinear
Comment: It's a bit too strong to say that no one was using ReLU activations, though I agree that ReLUs were not widespread until recently. See: https://stats.stackexchange.com/questions/447674/when-was-the-relu-function-first-used-in-a-neural-network


Printing a character specific line equivalent to string in Java

Question: To keep it short and concise I'm attempting to write a program that not only turns user input into a title. but also prints out a user generated decorative underline beneath the output.
example;
"prompt to get user input"
input: i am a title, *
output:
I Am A Title
* ** * *****

I was thinking of using a method that receives a String and a char and uses that to calculate the number of chars it has to output. I was already able to get the title to output in capitals thanks to a post here. Now it's just the characters. Even if it's just ways to think about the problem. Any help would be appreciated.
Comment: with decorative, you also mean fancy colored letters or just the line under it?
Comment: Just the line made with the character. I was able to do it already though with the help of another user. Still, Thank you.
Answer: public static void printUnderline(String s, char c){
    char[] arr =s.toCharArray();
    for(int i=0;i<arr.length;i++)
        if(arr[i] == ' ')
            System.out.print(' ');
        else
            System.out.print(c);
}

Comment: Simple and effective, Thank you.
Comment: First thing I tried but I don't have the reputation for it yet. When I do I will. :)
Comment: @JohnFranklinGomez If this is not the answer, what do you expect than this in order to accept this as the answer ?
Comment: @JudeNiroshan he meant.. he wants to accept, but can't. :)
Comment: Yup, I can't vote up on an answer until I gain more reputation.
Answer:         String str = "i am a title";
        String aux = "";
        String[]titles = str.split(" ");

        for(int i=0; i<titles.length;i++){
            aux += titles[i].replaceFirst(titles[i].charAt(0)+"", Character.toUpperCase(titles[i].charAt(0))+"") + " ";
        }

        System.out.println(aux);
        if(str.contains(" ")){
            aux = str;
            aux = aux.replaceAll("[A-Za-z]",  "*");

            System.out.println(aux);

        }else{
            System.out.println(false);
        }